ICEmsg.h revision a3129944
1266e564dSmrg/******************************************************************************
2266e564dSmrg
3266e564dSmrg
4266e564dSmrgCopyright 1993, 1998  The Open Group
5266e564dSmrg
6266e564dSmrgPermission to use, copy, modify, distribute, and sell this software and its
7266e564dSmrgdocumentation for any purpose is hereby granted without fee, provided that
8266e564dSmrgthe above copyright notice appear in all copies and that both that
9266e564dSmrgcopyright notice and this permission notice appear in supporting
10266e564dSmrgdocumentation.
11266e564dSmrg
12266e564dSmrgThe above copyright notice and this permission notice shall be included in
13266e564dSmrgall copies or substantial portions of the Software.
14266e564dSmrg
15266e564dSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16266e564dSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17266e564dSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18266e564dSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19266e564dSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20266e564dSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21266e564dSmrg
22266e564dSmrgExcept as contained in this notice, the name of The Open Group shall not be
23266e564dSmrgused in advertising or otherwise to promote the sale, use or other dealings
24266e564dSmrgin this Software without prior written authorization from The Open Group.
25266e564dSmrg
26266e564dSmrgAuthor: Ralph Mor, X Consortium
27266e564dSmrg******************************************************************************/
28266e564dSmrg
29266e564dSmrg#ifndef _ICEMSG_H_
30266e564dSmrg#define _ICEMSG_H_
31266e564dSmrg
32266e564dSmrg#include <X11/Xfuncproto.h>
33266e564dSmrg
34266e564dSmrg#include <X11/ICE/ICEconn.h>
35266e564dSmrg
36266e564dSmrg_XFUNCPROTOBEGIN
37266e564dSmrg
38266e564dSmrg/*
39266e564dSmrg * Function prototypes for internal ICElib functions
40266e564dSmrg */
41266e564dSmrg
42266e564dSmrgextern Status _IceRead (
43266e564dSmrg    IceConn		/* iceConn */,
44266e564dSmrg    unsigned long	/* nbytes */,
45266e564dSmrg    char *		/* ptr */
46266e564dSmrg);
47266e564dSmrg
48266e564dSmrgextern void _IceReadSkip (
49266e564dSmrg    IceConn		/* iceConn */,
50266e564dSmrg    unsigned long	/* nbytes */
51266e564dSmrg);
52266e564dSmrg
53266e564dSmrgextern void _IceWrite (
54266e564dSmrg    IceConn		/* iceConn */,
55266e564dSmrg    unsigned long	/* nbytes */,
56266e564dSmrg    char *		/* ptr */
57266e564dSmrg);
58266e564dSmrg
59266e564dSmrg
60266e564dSmrgextern void _IceErrorBadMinor (
61266e564dSmrg    IceConn		/* iceConn */,
62266e564dSmrg    int			/* majorOpcode */,
63266e564dSmrg    int			/* offendingMinor */,
64266e564dSmrg    int			/* severity */
65266e564dSmrg);
66266e564dSmrg
67266e564dSmrgextern void _IceErrorBadState (
68266e564dSmrg    IceConn		/* iceConn */,
69266e564dSmrg    int			/* majorOpcode */,
70266e564dSmrg    int			/* offendingMinor */,
71266e564dSmrg    int			/* severity */
72266e564dSmrg);
73266e564dSmrg
74266e564dSmrgextern void _IceErrorBadLength (
75266e564dSmrg    IceConn		/* iceConn */,
76266e564dSmrg    int			/* majorOpcode */,
77266e564dSmrg    int			/* offendingMinor */,
78266e564dSmrg    int			/* severity */
79266e564dSmrg);
80266e564dSmrg
81266e564dSmrgextern void _IceErrorBadValue (
82266e564dSmrg    IceConn		/* iceConn */,
83266e564dSmrg    int			/* majorOpcode */,
84266e564dSmrg    int			/* offendingMinor */,
85266e564dSmrg    int			/* offset */,
86266e564dSmrg    int			/* length */,
87266e564dSmrg    IcePointer		/* value */
88266e564dSmrg);
89266e564dSmrg
90c5629e66Smrgextern IcePoAuthStatus _IcePoMagicCookie1Proc (
91c5629e66Smrg    IceConn		/* iceConn */,
92c5629e66Smrg    IcePointer *	/* authStatePtr */,
93c5629e66Smrg    Bool 		/* cleanUp */,
94c5629e66Smrg    Bool		/* swap */,
95c5629e66Smrg    int     		/* authDataLen */,
96c5629e66Smrg    IcePointer		/* authData */,
97c5629e66Smrg    int *		/* replyDataLenRet */,
98c5629e66Smrg    IcePointer *	/* replyDataRet */,
99c5629e66Smrg    char **		/* errorStringRet */
100c5629e66Smrg);
101c5629e66Smrg
102c5629e66Smrgextern IcePaAuthStatus _IcePaMagicCookie1Proc (
103c5629e66Smrg    IceConn		/* iceConn */,
104c5629e66Smrg    IcePointer *	/* authStatePtr */,
105c5629e66Smrg    Bool		/* swap */,
106c5629e66Smrg    int     		/* authDataLen */,
107c5629e66Smrg    IcePointer		/* authData */,
108c5629e66Smrg    int *		/* replyDataLenRet */,
109c5629e66Smrg    IcePointer *	/* replyDataRet */,
110c5629e66Smrg    char **		/* errorStringRet */
111c5629e66Smrg);
112c5629e66Smrg
113266e564dSmrg
114266e564dSmrg/*
115266e564dSmrg * Macro to check if IO operations are valid on an ICE connection.
116266e564dSmrg */
117266e564dSmrg
118266e564dSmrg#define IceValidIO(_iceConn) _iceConn->io_ok
119266e564dSmrg
120266e564dSmrg
121266e564dSmrg/*
122266e564dSmrg * Macros for writing messages.
123266e564dSmrg */
124266e564dSmrg
125266e564dSmrg#define IceGetHeader(_iceConn, _major, _minor, _headerSize, _msgType, _pMsg) \
126266e564dSmrg    if ((_iceConn->outbufptr + _headerSize) > _iceConn->outbufmax) \
127266e564dSmrg        IceFlush (_iceConn); \
128266e564dSmrg    _pMsg = (_msgType *) _iceConn->outbufptr; \
129266e564dSmrg    _pMsg->majorOpcode = _major; \
130266e564dSmrg    _pMsg->minorOpcode = _minor; \
131266e564dSmrg    _pMsg->length = (_headerSize - SIZEOF (iceMsg)) >> 3; \
132266e564dSmrg    _iceConn->outbufptr += _headerSize; \
133266e564dSmrg    _iceConn->send_sequence++
134266e564dSmrg
135266e564dSmrg#define IceGetHeaderExtra(_iceConn, _major, _minor, _headerSize, _extra, _msgType, _pMsg, _pData) \
136266e564dSmrg    if ((_iceConn->outbufptr + \
137266e564dSmrg	_headerSize + ((_extra) << 3)) > _iceConn->outbufmax) \
138266e564dSmrg        IceFlush (_iceConn); \
139266e564dSmrg    _pMsg = (_msgType *) _iceConn->outbufptr; \
140266e564dSmrg    if ((_iceConn->outbufptr + \
141266e564dSmrg	_headerSize + ((_extra) << 3)) <= _iceConn->outbufmax) \
142266e564dSmrg        _pData = (char *) _pMsg + _headerSize; \
143266e564dSmrg    else \
144266e564dSmrg        _pData = NULL; \
145266e564dSmrg    _pMsg->majorOpcode = _major; \
146266e564dSmrg    _pMsg->minorOpcode = _minor; \
147266e564dSmrg    _pMsg->length = ((_headerSize - SIZEOF (iceMsg)) >> 3) + (_extra); \
148266e564dSmrg    _iceConn->outbufptr += (_headerSize + ((_extra) << 3)); \
149266e564dSmrg    _iceConn->send_sequence++
150266e564dSmrg
151266e564dSmrg#define IceSimpleMessage(_iceConn, _major, _minor) \
152266e564dSmrg{ \
153266e564dSmrg    iceMsg *_pMsg; \
154266e564dSmrg    IceGetHeader (_iceConn, _major, _minor, SIZEOF (iceMsg), iceMsg, _pMsg); \
155266e564dSmrg}
156266e564dSmrg
157266e564dSmrg#define IceErrorHeader(_iceConn, _offendingMajorOpcode, _offendingMinorOpcode, _offendingSequenceNum, _severity, _errorClass, _dataLength) \
158266e564dSmrg{ \
159266e564dSmrg    iceErrorMsg	*_pMsg; \
160266e564dSmrg\
161266e564dSmrg    IceGetHeader (_iceConn, _offendingMajorOpcode, ICE_Error, \
162266e564dSmrg	SIZEOF (iceErrorMsg), iceErrorMsg, _pMsg); \
163266e564dSmrg    _pMsg->length += (_dataLength); \
164fb5e8d76Smrg    _pMsg->offendingMinorOpcode = (CARD8) _offendingMinorOpcode; \
165fb5e8d76Smrg    _pMsg->severity = (CARD8) _severity; \
166fb5e8d76Smrg    _pMsg->offendingSequenceNum = (CARD32) _offendingSequenceNum; \
167fb5e8d76Smrg    _pMsg->errorClass = (CARD16) _errorClass; \
168266e564dSmrg}
169266e564dSmrg
170266e564dSmrg
171266e564dSmrg/*
172266e564dSmrg * Write data into the ICE output buffer.
173266e564dSmrg */
174266e564dSmrg
175266e564dSmrg#define IceWriteData(_iceConn, _bytes, _data) \
176266e564dSmrg{ \
177266e564dSmrg    if ((_iceConn->outbufptr + (_bytes)) > _iceConn->outbufmax) \
178266e564dSmrg    { \
179266e564dSmrg	IceFlush (_iceConn); \
180266e564dSmrg        _IceWrite (_iceConn, (unsigned long) (_bytes), _data); \
181266e564dSmrg    } \
182266e564dSmrg    else \
183266e564dSmrg    { \
184266e564dSmrg        memcpy (_iceConn->outbufptr, _data, _bytes); \
185266e564dSmrg        _iceConn->outbufptr += (_bytes); \
186266e564dSmrg    } \
187266e564dSmrg}
188266e564dSmrg
189266e564dSmrg#define IceWriteData16(_iceConn, _bytes, _data) \
190266e564dSmrg    IceWriteData (_iceConn, _bytes, (char *) _data)
191266e564dSmrg
192266e564dSmrg#define IceWriteData32(_iceConn, _bytes, _data) \
193266e564dSmrg    IceWriteData (_iceConn, _bytes, (char *) _data)
194266e564dSmrg
195266e564dSmrg
196266e564dSmrg/*
197266e564dSmrg * The IceSendData macro bypasses copying the data to the
198266e564dSmrg * ICE connection buffer and sends the data directly.  If necessary,
199266e564dSmrg * the ICE connection buffer is first flushed.
200266e564dSmrg */
201266e564dSmrg
202266e564dSmrg#define IceSendData(_iceConn, _bytes, _data) \
203266e564dSmrg{ \
204266e564dSmrg    if (_iceConn->outbufptr > _iceConn->outbuf) \
205266e564dSmrg	IceFlush (_iceConn); \
206266e564dSmrg    _IceWrite (_iceConn, (unsigned long) (_bytes), _data); \
207266e564dSmrg}
208266e564dSmrg
209266e564dSmrg
210266e564dSmrg/*
211266e564dSmrg * Write pad bytes.  Used to force 32 or 64 bit alignment.
2129ef0b394Smrg * A maximum of 7 pad bytes can be specified.
213266e564dSmrg */
214266e564dSmrg
215266e564dSmrg#define IceWritePad(_iceConn, _bytes) \
216266e564dSmrg{ \
217a3129944Smrg    char _dummy[7] = { 0 }; \
218a3129944Smrg    IceWriteData (_iceConn, (_bytes), _dummy); \
219266e564dSmrg}
220266e564dSmrg
221266e564dSmrg
222266e564dSmrg/*
223266e564dSmrg * Macros for reading messages.
224266e564dSmrg */
225266e564dSmrg
226266e564dSmrg#define IceReadCompleteMessage(_iceConn, _headerSize, _msgType, _pMsg, _pData)\
227266e564dSmrg{ \
228266e564dSmrg    unsigned long _bytes; \
229266e564dSmrg    IceReadMessageHeader (_iceConn, _headerSize, _msgType, _pMsg); \
230266e564dSmrg    _bytes = (_pMsg->length << 3) - (_headerSize - SIZEOF (iceMsg)); \
231266e564dSmrg    if ((_iceConn->inbufmax - _iceConn->inbufptr) >= _bytes) \
232266e564dSmrg    { \
233266e564dSmrg	_IceRead (_iceConn, _bytes, _iceConn->inbufptr); \
234266e564dSmrg	_pData = _iceConn->inbufptr; \
235266e564dSmrg	_iceConn->inbufptr += _bytes; \
236266e564dSmrg    } \
237266e564dSmrg    else \
238266e564dSmrg    { \
239fb5e8d76Smrg	_pData = malloc (_bytes); \
240266e564dSmrg        if (_pData) \
241266e564dSmrg	    _IceRead (_iceConn, _bytes, _pData); \
242266e564dSmrg        else \
243266e564dSmrg	    _IceReadSkip (_iceConn, _bytes); \
244266e564dSmrg    } \
245266e564dSmrg}
246266e564dSmrg
247266e564dSmrg#define IceDisposeCompleteMessage(_iceConn, _pData) \
248266e564dSmrg    if ((char *) _pData < _iceConn->inbuf || \
249266e564dSmrg	(char *) _pData >= _iceConn->inbufmax) \
250fb5e8d76Smrg        free (_pData);
251266e564dSmrg
252266e564dSmrg
253266e564dSmrg#define IceReadSimpleMessage(_iceConn, _msgType, _pMsg) \
254266e564dSmrg    _pMsg = (_msgType *) (_iceConn->inbuf);
255266e564dSmrg
256266e564dSmrg#define IceReadMessageHeader(_iceConn, _headerSize, _msgType, _pMsg) \
257266e564dSmrg{ \
258266e564dSmrg    _IceRead (_iceConn, \
259266e564dSmrg	(unsigned long) (_headerSize - SIZEOF (iceMsg)), \
260266e564dSmrg	_iceConn->inbufptr); \
261266e564dSmrg    _pMsg = (_msgType *) (_iceConn->inbuf); \
262266e564dSmrg    _iceConn->inbufptr += (_headerSize - SIZEOF (iceMsg)); \
263266e564dSmrg}
264266e564dSmrg
265266e564dSmrg#define IceReadData(_iceConn, _bytes, _pData) \
266266e564dSmrg    _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
267266e564dSmrg
268266e564dSmrg#define IceReadData16(_iceConn, _swap, _bytes, _pData) \
269266e564dSmrg{ \
270266e564dSmrg    _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
271266e564dSmrg}
272266e564dSmrg
273266e564dSmrg#define IceReadData32(_iceConn, _swap, _bytes, _pData) \
274266e564dSmrg{ \
275266e564dSmrg    _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
276266e564dSmrg}
277266e564dSmrg
278266e564dSmrg
279266e564dSmrg/*
280266e564dSmrg * Read pad bytes (for 32 or 64 bit alignment).
281266e564dSmrg * A maxium of 7 pad bytes can be specified.
282266e564dSmrg */
283266e564dSmrg
284266e564dSmrg#define IceReadPad(_iceConn, _bytes) \
285266e564dSmrg{ \
286266e564dSmrg    char _dummy[7]; \
287266e564dSmrg    _IceRead (_iceConn, (unsigned long) (_bytes), _dummy); \
288266e564dSmrg}
289266e564dSmrg
290266e564dSmrg_XFUNCPROTOEND
291266e564dSmrg
292266e564dSmrg#endif /* _ICEMSG_H_ */
293