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