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