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