1 /******************************************************************************
4 Copyright 1993, 1998 The Open Group
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
26 Author: Ralph Mor, X Consortium
27 ******************************************************************************/
33 #include <X11/Xfuncs.h>
35 #include <X11/ICE/ICEproto.h>
36 #include <X11/ICE/ICEconn.h>
37 #include <X11/ICE/ICEmsg.h>
38 #include <X11/ICE/ICEutil.h>
40 #include <X11/Xwindows.h>
51 #define IceVendorString "MIT"
52 #define IceReleaseString "1.0"
56 * Pad to a 64 bit boundary
59 #define PAD64(_bytes) ((8 - ((unsigned int) (_bytes) % 8)) % 8)
61 #define PADDED_BYTES64(_bytes) (_bytes + PAD64 (_bytes))
65 * Pad to 32 bit boundary
68 #define PAD32(_bytes) ((4 - ((unsigned int) (_bytes) % 4)) % 4)
70 #define PADDED_BYTES32(_bytes) (_bytes + PAD32 (_bytes))
74 * Number of 8 byte units in _bytes.
77 #define WORD64COUNT(_bytes) (((unsigned int) ((_bytes) + 7)) >> 3)
81 * Number of 4 byte units in _bytes.
84 #define WORD32COUNT(_bytes) (((unsigned int) ((_bytes) + 3)) >> 2)
88 * Given a string, compute the number of bytes for the STRING representation
91 #define STRING_BYTES(_string) \
92 (2 + strlen (_string) + PAD32 (2 + strlen (_string)))
96 * Size of ICE input/output buffers
99 #define ICE_INBUFSIZE 1024
101 #define ICE_OUTBUFSIZE 1024
105 * Maxium number of ICE authentication methods allowed, and maxiumum
106 * number of authentication data entries allowed to be set in the
107 * IceSetPaAuthData function.
109 * We should use linked lists, but this is easier and should suffice.
112 #define MAX_ICE_AUTH_NAMES 32
113 #define ICE_MAX_AUTH_DATA_ENTRIES 100
120 struct _IceListenObj {
121 struct _XtransConnInfo *trans_conn; /* transport connection object */
123 IceHostBasedAuthProc host_based_auth_proc;
128 * Some internal data structures for processing ICE messages.
131 typedef void (*_IceProcessCoreMsgProc) (
132 IceConn /* iceConn */,
134 unsigned long /* length */,
136 IceReplyWaitInfo * /* replyWait */,
137 Bool * /* replyReadyRet */,
138 Bool * /* connectionClosedRet */
144 _IceProcessCoreMsgProc process_core_msg_proc;
152 #define STORE_CARD8(_pBuf, _val) \
154 *((CARD8 *) _pBuf) = _val; \
160 #define STORE_CARD16(_pBuf, _val) \
162 *((CARD16 *) _pBuf) = _val; \
166 #define STORE_CARD32(_pBuf, _val) \
168 *((CARD32 *) _pBuf) = _val; \
174 #define STORE_CARD16(_pBuf, _val) \
181 memcpy (_pBuf, &_d, 2); \
185 #define STORE_CARD32(_pBuf, _val) \
191 memcpy (_pBuf, &_d, 4); \
197 #define STORE_STRING(_pBuf, _string) \
199 CARD16 _len = strlen (_string); \
200 STORE_CARD16 (_pBuf, _len); \
201 memcpy (_pBuf, _string, _len); \
203 if (PAD32 (2 + _len)) \
204 _pBuf += PAD32 (2 + _len); \
212 #define EXTRACT_CARD8(_pBuf, _val) \
214 _val = *((CARD8 *) _pBuf); \
220 #define EXTRACT_CARD16(_pBuf, _swap, _val) \
222 _val = *((CARD16 *) _pBuf); \
225 _val = lswaps (_val); \
228 #define EXTRACT_CARD32(_pBuf, _swap, _val) \
230 _val = *((CARD32 *) _pBuf); \
233 _val = lswapl (_val); \
238 #define EXTRACT_CARD16(_pBuf, _swap, _val) \
240 _val = *(_pBuf + 0) & 0xff; /* 0xff incase _pBuf is signed */ \
242 _val |= *(_pBuf + 1) & 0xff;\
245 _val = lswaps (_val); \
248 #define EXTRACT_CARD32(_pBuf, _swap, _val) \
250 _val = *(_pBuf + 0) & 0xff; /* 0xff incase _pBuf is signed */ \
252 _val |= *(_pBuf + 1) & 0xff;\
254 _val |= *(_pBuf + 2) & 0xff;\
256 _val |= *(_pBuf + 3) & 0xff;\
259 _val = lswapl (_val); \
264 #define EXTRACT_STRING(_pBuf, _swap, _string) \
267 EXTRACT_CARD16 (_pBuf, _swap, _len); \
268 _string = (char *) malloc (_len + 1); \
269 memcpy (_string, _pBuf, _len); \
271 _string[_len] = '\0'; \
272 if (PAD32 (2 + _len)) \
273 _pBuf += PAD32 (2 + _len); \
276 #define EXTRACT_LISTOF_STRING(_pBuf, _swap, _count, _strings) \
279 for (_i = 0; _i < _count; _i++) \
280 EXTRACT_STRING (_pBuf, _swap, _strings[_i]); \
284 #define SKIP_STRING(_pBuf, _swap, _end, _bail) \
287 EXTRACT_CARD16 (_pBuf, _swap, _len); \
288 _pBuf += _len + PAD32(2+_len); \
289 if (_pBuf > _end) { \
294 #define SKIP_LISTOF_STRING(_pBuf, _swap, _count, _end, _bail) \
297 for (_i = 0; _i < _count; _i++) \
298 SKIP_STRING (_pBuf, _swap, _end, _bail); \
307 /* byte swap a long literal */
308 #define lswapl(_val) ((((_val) & 0xff) << 24) |\
309 (((_val) & 0xff00) << 8) |\
310 (((_val) & 0xff0000) >> 8) |\
311 (((_val) >> 24) & 0xff))
313 /* byte swap a short literal */
314 #define lswaps(_val) ((((_val) & 0xff) << 8) | (((_val) >> 8) & 0xff))
319 * ICE replies (not processed via callbacks because we block)
322 #define ICE_CONNECTION_REPLY 1
323 #define ICE_CONNECTION_ERROR 2
324 #define ICE_PROTOCOL_REPLY 3
325 #define ICE_PROTOCOL_ERROR 4
332 } _IceConnectionReply;
337 } _IceConnectionError;
355 _IceConnectionReply connection_reply;
356 _IceConnectionError connection_error;
357 _IceProtocolReply protocol_reply;
358 _IceProtocolError protocol_error;
363 * Watch for ICE connection create/destroy.
366 typedef struct _IceWatchedConnection {
368 IcePointer watch_data;
369 struct _IceWatchedConnection *next;
370 } _IceWatchedConnection;
372 typedef struct _IceWatchProc {
373 IceWatchProc watch_proc;
374 IcePointer client_data;
375 _IceWatchedConnection *watched_connections;
376 struct _IceWatchProc *next;
384 #define IceLockConn(_iceConn)
385 #define IceUnlockConn(_iceConn)
389 * Extern declarations
392 extern IceConn _IceConnectionObjs[];
393 extern char *_IceConnectionStrings[];
394 extern int _IceConnectionCount;
396 extern _IceProtocol _IceProtocols[];
397 extern int _IceLastMajorOpcode;
399 extern int _IceAuthCount;
400 extern const char *_IceAuthNames[];
401 extern IcePoAuthProc _IcePoAuthProcs[];
402 extern IcePaAuthProc _IcePaAuthProcs[];
404 extern int _IceVersionCount;
405 extern _IceVersion _IceVersions[];
407 extern _IceWatchProc *_IceWatchProcs;
409 extern IceErrorHandler _IceErrorHandler;
410 extern IceIOErrorHandler _IceIOErrorHandler;
412 extern IceAuthDataEntry _IcePaAuthDataEntries[];
413 extern int _IcePaAuthDataEntryCount;
415 extern void _IceErrorBadMajor (
416 IceConn /* iceConn */,
417 int /* offendingMajor */,
418 int /* offendingMinor */,
422 extern void _IceErrorNoAuthentication (
423 IceConn /* iceConn */,
424 int /* offendingMinor */
427 extern void _IceErrorNoVersion (
428 IceConn /* iceConn */,
429 int /* offendingMinor */
432 extern void _IceErrorSetupFailed (
433 IceConn /* iceConn */,
434 int /* offendingMinor */,
435 const char * /* reason */
438 extern void _IceErrorAuthenticationRejected (
439 IceConn /* iceConn */,
440 int /* offendingMinor */,
441 const char * /* reason */
444 extern void _IceErrorAuthenticationFailed (
445 IceConn /* iceConn */,
446 int /* offendingMinor */,
447 const char * /* reason */
450 extern void _IceErrorProtocolDuplicate (
451 IceConn /* iceConn */,
452 const char * /* protocolName */
455 extern void _IceErrorMajorOpcodeDuplicate (
456 IceConn /* iceConn */,
457 int /* majorOpcode */
460 extern void _IceErrorUnknownProtocol (
461 IceConn /* iceConn */,
462 const char * /* protocolName */
465 extern void _IceAddOpcodeMapping (
466 IceConn /* iceConn */,
471 extern char *_IceGetPeerName (
472 IceConn /* iceConn */
475 extern void _IceFreeConnection (
476 IceConn /* iceConn */
479 extern void _IceAddReplyWait (
480 IceConn /* iceConn */,
481 IceReplyWaitInfo * /* replyWait */
484 extern IceReplyWaitInfo *_IceSearchReplyWaits (
485 IceConn /* iceConn */,
486 int /* majorOpcode */
489 extern void _IceSetReplyReady (
490 IceConn /* iceConn */,
491 IceReplyWaitInfo * /* replyWait */
494 extern Bool _IceCheckReplyReady (
495 IceConn /* iceConn */,
496 IceReplyWaitInfo * /* replyWait */
499 extern void _IceConnectionOpened (
500 IceConn /* iceConn */
503 extern void _IceConnectionClosed (
504 IceConn /* iceConn */
507 extern void _IceGetPoAuthData (
508 const char * /* protocol_name */,
509 const char * /* address */,
510 const char * /* auth_name */,
511 unsigned short * /* auth_data_length_ret */,
512 char ** /* auth_data_ret */
515 extern void _IceGetPaAuthData (
516 const char * /* protocol_name */,
517 const char * /* address */,
518 const char * /* auth_name */,
519 unsigned short * /* auth_data_length_ret */,
520 char ** /* auth_data_ret */
523 extern void _IceGetPoValidAuthIndices (
524 const char * /* protocol_name */,
525 const char * /* address */,
526 int /* num_auth_names */,
527 const char ** /* auth_names */,
528 int * /* num_indices_ret */,
529 int * /* indices_ret */
532 extern void _IceGetPaValidAuthIndices (
533 const char * /* protocol_name */,
534 const char * /* address */,
535 int /* num_auth_names */,
536 const char ** /* auth_names */,
537 int * /* num_indices_ret */,
538 int * /* indices_ret */
541 #endif /* _ICELIBINT_H_ */