ICElibint.h revision 1009a292
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) ((unsigned int) _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) ((unsigned int) _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 * Maximum number of ICE authentication methods allowed, and maximum
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#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
171#define STORE_STRING(_pBuf, _string) \
172{ \
173    CARD16 _len = (CARD16) strlen (_string); \
174    STORE_CARD16 (_pBuf, _len); \
175    memcpy (_pBuf, _string, _len); \
176    _pBuf += _len; \
177    if (PAD32 (2 + _len)) \
178        _pBuf += PAD32 (2 + _len); \
179}
180
181
182/*
183 * SEND FOO - write to connection instead of buffer
184 */
185#define SEND_STRING(_iceConn, _string) \
186{ \
187    char _padding[3] = { 0 }; \
188    CARD16 _len = (CARD16) strlen (_string); \
189    IceWriteData32 (_iceConn, 2, &_len); \
190    if (_len) \
191        IceSendData (_iceConn, _len, (char *) _string);  \
192    if (PAD32 (2 + _len)) \
193        IceSendData (_iceConn, PAD32 (2 + _len), _padding); \
194}
195
196/*
197 * EXTRACT FOO
198 */
199
200#define EXTRACT_CARD8(_pBuf, _val) \
201{ \
202    _val = *((CARD8 *) _pBuf); \
203    _pBuf += 1; \
204}
205
206#define EXTRACT_CARD16(_pBuf, _swap, _val) \
207{ \
208    _val = *((CARD16 *) _pBuf); \
209    _pBuf += 2; \
210    if (_swap) \
211        _val = lswaps (_val); \
212}
213
214#define EXTRACT_CARD32(_pBuf, _swap, _val) \
215{ \
216    _val = *((CARD32 *) _pBuf); \
217    _pBuf += 4; \
218    if (_swap) \
219        _val = lswapl (_val); \
220}
221
222
223#define EXTRACT_STRING(_pBuf, _swap, _string) \
224{ \
225    CARD16 _len; \
226    EXTRACT_CARD16 (_pBuf, _swap, _len); \
227    _string = malloc (_len + 1); \
228    memcpy (_string, _pBuf, _len); \
229    _pBuf += _len; \
230    _string[_len] = '\0'; \
231    if (PAD32 (2 + _len)) \
232        _pBuf += PAD32 (2 + _len); \
233}
234
235#define EXTRACT_LISTOF_STRING(_pBuf, _swap, _count, _strings) \
236{ \
237    int _i; \
238    for (_i = 0; _i < _count; _i++) \
239        EXTRACT_STRING (_pBuf, _swap, _strings[_i]); \
240}
241
242
243#define SKIP_STRING(_pBuf, _swap, _end, _bail) \
244{ \
245    CARD16 _len; \
246    EXTRACT_CARD16 (_pBuf, _swap, _len); \
247    _pBuf += _len + PAD32(2+_len); \
248    if (_pBuf > _end) { \
249	_bail; \
250    } \
251}
252
253#define SKIP_LISTOF_STRING(_pBuf, _swap, _count, _end, _bail) \
254{ \
255    int _i; \
256    for (_i = 0; _i < _count; _i++) \
257        SKIP_STRING (_pBuf, _swap, _end, _bail); \
258}
259
260
261
262/*
263 * Byte swapping
264 */
265
266/* byte swap a long literal */
267#define lswapl(_val) ((((_val) & 0xff) << 24) |\
268		   (((_val) & 0xff00) << 8) |\
269		   (((_val) & 0xff0000) >> 8) |\
270		   (((_val) >> 24) & 0xff))
271
272/* byte swap a short literal */
273#define lswaps(_val) ((((_val) & 0xff) << 8) | (((_val) >> 8) & 0xff))
274
275
276
277/*
278 * ICE replies (not processed via callbacks because we block)
279 */
280
281#define ICE_CONNECTION_REPLY	1
282#define ICE_CONNECTION_ERROR	2
283#define ICE_PROTOCOL_REPLY	3
284#define ICE_PROTOCOL_ERROR	4
285
286typedef struct {
287    int		  type;
288    int 	  version_index;
289    char	  *vendor;
290    char          *release;
291} _IceConnectionReply;
292
293typedef struct {
294    int		  type;
295    char	  *error_message;
296} _IceConnectionError;
297
298typedef struct {
299    int		  type;
300    int 	  major_opcode;
301    int		  version_index;
302    char	  *vendor;
303    char	  *release;
304} _IceProtocolReply;
305
306typedef struct {
307    int		  type;
308    char	  *error_message;
309} _IceProtocolError;
310
311
312typedef union {
313    int			type;
314    _IceConnectionReply	connection_reply;
315    _IceConnectionError	connection_error;
316    _IceProtocolReply	protocol_reply;
317    _IceProtocolError	protocol_error;
318} _IceReply;
319
320
321/*
322 * Watch for ICE connection create/destroy.
323 */
324
325typedef struct _IceWatchedConnection {
326    IceConn				iceConn;
327    IcePointer				watch_data;
328    struct _IceWatchedConnection	*next;
329} _IceWatchedConnection;
330
331typedef struct _IceWatchProc {
332    IceWatchProc		watch_proc;
333    IcePointer			client_data;
334    _IceWatchedConnection	*watched_connections;
335    struct _IceWatchProc	*next;
336} _IceWatchProc;
337
338
339/*
340 * Locking
341 */
342
343#define IceLockConn(_iceConn)
344#define IceUnlockConn(_iceConn)
345
346
347/*
348 * Extern declarations
349 */
350
351extern IceConn			_IceConnectionObjs[];
352extern char			*_IceConnectionStrings[];
353extern int			_IceConnectionCount;
354
355extern _IceProtocol		_IceProtocols[];
356extern int			_IceLastMajorOpcode;
357
358extern int			_IceAuthCount;
359extern const char		*_IceAuthNames[];
360extern IcePoAuthProc		_IcePoAuthProcs[];
361extern IcePaAuthProc		_IcePaAuthProcs[];
362
363extern const int		_IceVersionCount;
364extern const _IceVersion	_IceVersions[];
365
366extern _IceWatchProc		*_IceWatchProcs;
367
368extern IceErrorHandler		_IceErrorHandler;
369extern IceIOErrorHandler	_IceIOErrorHandler;
370
371extern IceAuthDataEntry		_IcePaAuthDataEntries[];
372extern int			_IcePaAuthDataEntryCount;
373
374extern void _IceErrorBadMajor (
375    IceConn		/* iceConn */,
376    int			/* offendingMajor */,
377    int			/* offendingMinor */,
378    int			/* severity */
379);
380
381extern void _IceErrorNoAuthentication (
382    IceConn		/* iceConn */,
383    int			/* offendingMinor */
384);
385
386extern void _IceErrorNoVersion (
387    IceConn		/* iceConn */,
388    int			/* offendingMinor */
389);
390
391extern void _IceErrorSetupFailed (
392    IceConn		/* iceConn */,
393    int			/* offendingMinor */,
394    const char *	/* reason */
395);
396
397extern void _IceErrorAuthenticationRejected (
398    IceConn		/* iceConn */,
399    int			/* offendingMinor */,
400    const char *	/* reason */
401);
402
403extern void _IceErrorAuthenticationFailed (
404    IceConn		/* iceConn */,
405    int			/* offendingMinor */,
406    const char *	/* reason */
407);
408
409extern void _IceErrorProtocolDuplicate (
410    IceConn		/* iceConn */,
411    const char *	/* protocolName */
412);
413
414extern void _IceErrorMajorOpcodeDuplicate (
415    IceConn		/* iceConn */,
416    int			/* majorOpcode */
417);
418
419extern void _IceErrorUnknownProtocol (
420    IceConn		/* iceConn */,
421    const char *	/* protocolName */
422);
423
424extern void _IceAddOpcodeMapping (
425    IceConn		/* iceConn */,
426    int			/* hisOpcode */,
427    int			/* myOpcode */
428);
429
430extern char *_IceGetPeerName (
431    IceConn		/* iceConn */
432);
433
434extern void _IceFreeConnection (
435    IceConn		/* iceConn */
436);
437
438extern void _IceAddReplyWait (
439    IceConn		/* iceConn */,
440    IceReplyWaitInfo *	/* replyWait */
441);
442
443extern IceReplyWaitInfo *_IceSearchReplyWaits (
444    IceConn		/* iceConn */,
445    int			/* majorOpcode */
446);
447
448extern void _IceSetReplyReady (
449    IceConn		/* iceConn */,
450    IceReplyWaitInfo *	/* replyWait */
451);
452
453extern Bool _IceCheckReplyReady (
454    IceConn		/* iceConn */,
455    IceReplyWaitInfo *	/* replyWait */
456);
457
458extern void _IceConnectionOpened (
459    IceConn		/* iceConn */
460);
461
462extern void _IceConnectionClosed (
463    IceConn		/* iceConn */
464);
465
466extern void _IceGetPoAuthData (
467    const char *	/* protocol_name */,
468    const char *	/* address */,
469    const char *	/* auth_name */,
470    unsigned short *	/* auth_data_length_ret */,
471    char **		/* auth_data_ret */
472);
473
474extern void _IceGetPaAuthData (
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
482extern void _IceGetPoValidAuthIndices (
483    const char *	/* protocol_name */,
484    const char *	/* address */,
485    int			/* num_auth_names */,
486    const char **	/* auth_names */,
487    int	*		/* num_indices_ret */,
488    int	*		/* indices_ret */
489);
490
491extern void _IceGetPaValidAuthIndices (
492    const char *	/* protocol_name */,
493    const char *	/* address */,
494    int			/* num_auth_names */,
495    const char **	/* auth_names */,
496    int	*		/* num_indices_ret */,
497    int	*		/* indices_ret */
498);
499
500#endif /* _ICELIBINT_H_ */
501