Home | History | Annotate | Line # | Download | only in src
      1 /******************************************************************************
      2 
      3 
      4 Copyright 1993, 1998  The Open Group
      5 
      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
     10 documentation.
     11 
     12 The above copyright notice and this permission notice shall be included in
     13 all copies or substantial portions of the Software.
     14 
     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.
     21 
     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.
     25 
     26 Author: Ralph Mor, X Consortium
     27 ******************************************************************************/
     28 
     29 #ifndef _ICELIBINT_H_
     30 #define _ICELIBINT_H_
     31 
     32 #include <X11/Xos.h>
     33 #include <X11/Xfuncs.h>
     34 #include <X11/Xmd.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>
     39 #ifdef WIN32
     40 #include <X11/Xwindows.h>
     41 #endif
     42 
     43 #include <stdlib.h>
     44 #include <stddef.h>
     45 
     46 #ifdef O_CLOEXEC
     47 #define FOPEN_CLOEXEC "e"
     48 #else
     49 #define FOPEN_CLOEXEC ""
     50 #define O_CLOEXEC 0
     51 #endif
     52 
     53 /*
     54  * Vendor & Release
     55  */
     56 
     57 #define IceVendorString  "MIT"
     58 #define IceReleaseString "1.0"
     59 
     60 
     61 /*
     62  * Pad to a 64 bit boundary
     63  */
     64 
     65 #define PAD64(_bytes) ((8 - ((unsigned int) (_bytes) % 8)) % 8)
     66 
     67 #define PADDED_BYTES64(_bytes) ((unsigned int) _bytes + PAD64 (_bytes))
     68 
     69 
     70 /*
     71  * Pad to 32 bit boundary
     72  */
     73 
     74 #define PAD32(_bytes) ((4 - ((unsigned int) (_bytes) % 4)) % 4)
     75 
     76 #define PADDED_BYTES32(_bytes) ((unsigned int) _bytes + PAD32 (_bytes))
     77 
     78 
     79 /*
     80  * Number of 8 byte units in _bytes.
     81  */
     82 
     83 #define WORD64COUNT(_bytes) (((unsigned int) ((_bytes) + 7)) >> 3)
     84 
     85 
     86 /*
     87  * Number of 4 byte units in _bytes.
     88  */
     89 
     90 #define WORD32COUNT(_bytes) (((unsigned int) ((_bytes) + 3)) >> 2)
     91 
     92 
     93 /*
     94  * Given a string, compute the number of bytes for the STRING representation
     95  */
     96 
     97 #define STRING_BYTES(_string) \
     98     (2 + strlen (_string) + PAD32 (2 + strlen (_string)))
     99 
    100 
    101 /*
    102  * Size of ICE input/output buffers
    103  */
    104 
    105 #define ICE_INBUFSIZE 1024
    106 
    107 #define ICE_OUTBUFSIZE 1024
    108 
    109 
    110 /*
    111  * Maximum number of ICE authentication methods allowed, and maximum
    112  * number of authentication data entries allowed to be set in the
    113  * IceSetPaAuthData function.
    114  *
    115  * We should use linked lists, but this is easier and should suffice.
    116  */
    117 
    118 #define MAX_ICE_AUTH_NAMES 32
    119 #define ICE_MAX_AUTH_DATA_ENTRIES 100
    120 
    121 
    122 /*
    123  * ICE listen object
    124  */
    125 
    126 struct _IceListenObj {
    127     struct _XtransConnInfo 	*trans_conn; /* transport connection object */
    128     char			*network_id;
    129     IceHostBasedAuthProc 	host_based_auth_proc;
    130 };
    131 
    132 
    133 /*
    134  * Some internal data structures for processing ICE messages.
    135  */
    136 
    137 typedef void (*_IceProcessCoreMsgProc) (
    138     IceConn 		/* iceConn */,
    139     int			/* opcode */,
    140     unsigned long	/* length */,
    141     Bool		/* swap */,
    142     IceReplyWaitInfo *  /* replyWait */,
    143     Bool *		/* replyReadyRet */,
    144     Bool *		/* connectionClosedRet */
    145 );
    146 
    147 typedef struct {
    148     int 			major_version;
    149     int 			minor_version;
    150     _IceProcessCoreMsgProc	process_core_msg_proc;
    151 } _IceVersion;
    152 
    153 
    154 /*
    155  * STORE FOO
    156  */
    157 
    158 #define STORE_CARD8(_pBuf, _val) \
    159 do { \
    160     *((CARD8 *) _pBuf) = _val; \
    161     _pBuf += 1; \
    162 } while (0)
    163 
    164 #define STORE_CARD16(_pBuf, _val) \
    165 do { \
    166     *((CARD16 *) _pBuf) = _val; \
    167     _pBuf += 2; \
    168 } while (0)
    169 
    170 #define STORE_CARD32(_pBuf, _val) \
    171 do { \
    172     *((CARD32 *) _pBuf) = _val; \
    173     _pBuf += 4; \
    174 } while (0)
    175 
    176 
    177 #define STORE_STRING(_pBuf, _string) \
    178 do { \
    179     CARD16 _len = (CARD16) strlen (_string); \
    180     STORE_CARD16 (_pBuf, _len); \
    181     memcpy (_pBuf, _string, _len); \
    182     _pBuf += _len; \
    183     if (PAD32 (2 + _len)) \
    184         _pBuf += PAD32 (2 + _len); \
    185 } while (0)
    186 
    187 
    188 /*
    189  * SEND FOO - write to connection instead of buffer
    190  */
    191 #define SEND_STRING(_iceConn, _string) \
    192 do { \
    193     char _padding[3] = { 0 }; \
    194     CARD16 _len = (CARD16) strlen (_string); \
    195     IceWriteData32 (_iceConn, 2, &_len); \
    196     if (_len) \
    197         IceSendData (_iceConn, _len, (char *) _string);  \
    198     if (PAD32 (2 + _len)) \
    199         IceSendData (_iceConn, PAD32 (2 + _len), _padding); \
    200 } while (0)
    201 
    202 /*
    203  * EXTRACT FOO
    204  */
    205 
    206 #define EXTRACT_CARD8(_pBuf, _val) \
    207 do { \
    208     _val = *((CARD8 *) _pBuf); \
    209     _pBuf += 1; \
    210 } while (0)
    211 
    212 #define EXTRACT_CARD16(_pBuf, _swap, _val) \
    213 do { \
    214     _val = *((CARD16 *) _pBuf); \
    215     _pBuf += 2; \
    216     if (_swap) \
    217         _val = lswaps (_val); \
    218 } while (0)
    219 
    220 #define EXTRACT_CARD32(_pBuf, _swap, _val) \
    221 do { \
    222     _val = *((CARD32 *) _pBuf); \
    223     _pBuf += 4; \
    224     if (_swap) \
    225         _val = lswapl (_val); \
    226 } while (0)
    227 
    228 
    229 #define EXTRACT_STRING(_pBuf, _swap, _string) \
    230 do { \
    231     CARD16 _len; \
    232     EXTRACT_CARD16 (_pBuf, _swap, _len); \
    233     _string = malloc (_len + 1); \
    234     if (_string != NULL) { \
    235         memcpy (_string, _pBuf, _len); \
    236         _string[_len] = '\0'; \
    237     } \
    238     _pBuf += _len; \
    239     if (PAD32 (2 + _len)) \
    240         _pBuf += PAD32 (2 + _len); \
    241 } while (0)
    242 
    243 #define EXTRACT_LISTOF_STRING(_pBuf, _swap, _count, _strings) \
    244 do { \
    245     int _i; \
    246     for (_i = 0; _i < _count; _i++) \
    247         EXTRACT_STRING (_pBuf, _swap, _strings[_i]); \
    248 } while (0)
    249 
    250 
    251 #define SKIP_STRING(_pBuf, _swap, _end, _bail) \
    252 do { \
    253     CARD16 _len; \
    254     EXTRACT_CARD16 (_pBuf, _swap, _len); \
    255     _pBuf += _len + PAD32(2+_len); \
    256     if (_pBuf > _end) { \
    257 	_bail; \
    258     } \
    259 } while (0)
    260 
    261 #define SKIP_LISTOF_STRING(_pBuf, _swap, _count, _end, _bail) \
    262 do { \
    263     int _i; \
    264     for (_i = 0; _i < _count; _i++) \
    265         SKIP_STRING (_pBuf, _swap, _end, _bail); \
    266 } while (0)
    267 
    268 
    269 
    270 /*
    271  * Byte swapping
    272  */
    273 
    274 /* byte swap a long literal */
    275 #define lswapl(_val) ((((_val) & 0xff) << 24) |\
    276 		   (((_val) & 0xff00) << 8) |\
    277 		   (((_val) & 0xff0000) >> 8) |\
    278 		   (((_val) >> 24) & 0xff))
    279 
    280 /* byte swap a short literal */
    281 #define lswaps(_val) ((((_val) & 0xff) << 8) | (((_val) >> 8) & 0xff))
    282 
    283 
    284 
    285 /*
    286  * ICE replies (not processed via callbacks because we block)
    287  */
    288 
    289 #define ICE_CONNECTION_REPLY	1
    290 #define ICE_CONNECTION_ERROR	2
    291 #define ICE_PROTOCOL_REPLY	3
    292 #define ICE_PROTOCOL_ERROR	4
    293 
    294 typedef struct {
    295     int		  type;
    296     int 	  version_index;
    297     char	  *vendor;
    298     char          *release;
    299 } _IceConnectionReply;
    300 
    301 typedef struct {
    302     int		  type;
    303     char	  *error_message;
    304 } _IceConnectionError;
    305 
    306 typedef struct {
    307     int		  type;
    308     int 	  major_opcode;
    309     int		  version_index;
    310     char	  *vendor;
    311     char	  *release;
    312 } _IceProtocolReply;
    313 
    314 typedef struct {
    315     int		  type;
    316     char	  *error_message;
    317 } _IceProtocolError;
    318 
    319 
    320 typedef union {
    321     int			type;
    322     _IceConnectionReply	connection_reply;
    323     _IceConnectionError	connection_error;
    324     _IceProtocolReply	protocol_reply;
    325     _IceProtocolError	protocol_error;
    326 } _IceReply;
    327 
    328 
    329 /*
    330  * Watch for ICE connection create/destroy.
    331  */
    332 
    333 typedef struct _IceWatchedConnection {
    334     IceConn				iceConn;
    335     IcePointer				watch_data;
    336     struct _IceWatchedConnection	*next;
    337 } _IceWatchedConnection;
    338 
    339 typedef struct _IceWatchProc {
    340     IceWatchProc		watch_proc;
    341     IcePointer			client_data;
    342     _IceWatchedConnection	*watched_connections;
    343     struct _IceWatchProc	*next;
    344 } _IceWatchProc;
    345 
    346 
    347 /*
    348  * Locking
    349  */
    350 
    351 #define IceLockConn(_iceConn)
    352 #define IceUnlockConn(_iceConn)
    353 
    354 
    355 /*
    356  * Extern declarations
    357  */
    358 
    359 extern IceConn			_IceConnectionObjs[];
    360 extern char			*_IceConnectionStrings[];
    361 extern int			_IceConnectionCount;
    362 
    363 extern _IceProtocol		_IceProtocols[];
    364 extern int			_IceLastMajorOpcode;
    365 
    366 extern int			_IceAuthCount;
    367 extern const char		*_IceAuthNames[];
    368 extern IcePoAuthProc		_IcePoAuthProcs[];
    369 extern IcePaAuthProc		_IcePaAuthProcs[];
    370 
    371 extern const int		_IceVersionCount;
    372 extern const _IceVersion	_IceVersions[];
    373 
    374 extern _IceWatchProc		*_IceWatchProcs;
    375 
    376 extern IceErrorHandler		_IceErrorHandler;
    377 extern IceIOErrorHandler	_IceIOErrorHandler;
    378 
    379 extern IceAuthDataEntry		_IcePaAuthDataEntries[];
    380 extern int			_IcePaAuthDataEntryCount;
    381 
    382 extern void _IceErrorBadMajor (
    383     IceConn		/* iceConn */,
    384     int			/* offendingMajor */,
    385     int			/* offendingMinor */,
    386     int			/* severity */
    387 );
    388 
    389 extern void _IceErrorNoAuthentication (
    390     IceConn		/* iceConn */,
    391     int			/* offendingMinor */
    392 );
    393 
    394 extern void _IceErrorNoVersion (
    395     IceConn		/* iceConn */,
    396     int			/* offendingMinor */
    397 );
    398 
    399 extern void _IceErrorSetupFailed (
    400     IceConn		/* iceConn */,
    401     int			/* offendingMinor */,
    402     const char *	/* reason */
    403 );
    404 
    405 extern void _IceErrorAuthenticationRejected (
    406     IceConn		/* iceConn */,
    407     int			/* offendingMinor */,
    408     const char *	/* reason */
    409 );
    410 
    411 extern void _IceErrorAuthenticationFailed (
    412     IceConn		/* iceConn */,
    413     int			/* offendingMinor */,
    414     const char *	/* reason */
    415 );
    416 
    417 extern void _IceErrorProtocolDuplicate (
    418     IceConn		/* iceConn */,
    419     const char *	/* protocolName */
    420 );
    421 
    422 extern void _IceErrorMajorOpcodeDuplicate (
    423     IceConn		/* iceConn */,
    424     int			/* majorOpcode */
    425 );
    426 
    427 extern void _IceErrorUnknownProtocol (
    428     IceConn		/* iceConn */,
    429     const char *	/* protocolName */
    430 );
    431 
    432 extern void _IceAddOpcodeMapping (
    433     IceConn		/* iceConn */,
    434     int			/* hisOpcode */,
    435     int			/* myOpcode */
    436 );
    437 
    438 extern char *_IceGetPeerName (
    439     IceConn		/* iceConn */
    440 );
    441 
    442 extern void _IceFreeConnection (
    443     IceConn		/* iceConn */
    444 );
    445 
    446 extern void _IceAddReplyWait (
    447     IceConn		/* iceConn */,
    448     IceReplyWaitInfo *	/* replyWait */
    449 );
    450 
    451 extern IceReplyWaitInfo *_IceSearchReplyWaits (
    452     IceConn		/* iceConn */,
    453     int			/* majorOpcode */
    454 );
    455 
    456 extern void _IceSetReplyReady (
    457     IceConn		/* iceConn */,
    458     IceReplyWaitInfo *	/* replyWait */
    459 );
    460 
    461 extern Bool _IceCheckReplyReady (
    462     IceConn		/* iceConn */,
    463     IceReplyWaitInfo *	/* replyWait */
    464 );
    465 
    466 extern void _IceConnectionOpened (
    467     IceConn		/* iceConn */
    468 );
    469 
    470 extern void _IceConnectionClosed (
    471     IceConn		/* iceConn */
    472 );
    473 
    474 extern void _IceGetPoAuthData (
    475     const char *	/* protocol_name */,
    476     const char *	/* address */,
    477     const char *	/* auth_name */,
    478     unsigned short *	/* auth_data_length_ret */,
    479     char **		/* auth_data_ret */
    480 );
    481 
    482 extern void _IceGetPaAuthData (
    483     const char *	/* protocol_name */,
    484     const char *	/* address */,
    485     const char *	/* auth_name */,
    486     unsigned short *	/* auth_data_length_ret */,
    487     char **		/* auth_data_ret */
    488 );
    489 
    490 extern void _IceGetPoValidAuthIndices (
    491     const char *	/* protocol_name */,
    492     const char *	/* address */,
    493     int			/* num_auth_names */,
    494     const char **	/* auth_names */,
    495     int	*		/* num_indices_ret */,
    496     int	*		/* indices_ret */
    497 );
    498 
    499 extern void _IceGetPaValidAuthIndices (
    500     const char *	/* protocol_name */,
    501     const char *	/* address */,
    502     int			/* num_auth_names */,
    503     const char **	/* auth_names */,
    504     int	*		/* num_indices_ret */,
    505     int	*		/* indices_ret */
    506 );
    507 
    508 #endif /* _ICELIBINT_H_ */
    509