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