Xtransint.h revision 3d2ed3e3
1/*
2
3Copyright 1993, 1994, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27 * Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA
28 *
29 * All Rights Reserved
30 *
31 * Permission to use, copy, modify, and distribute this software and its
32 * documentation for any purpose and without fee is hereby granted, provided
33 * that the above copyright notice appear in all copies and that both that
34 * copyright notice and this permission notice appear in supporting
35 * documentation, and that the name NCR not be used in advertising
36 * or publicity pertaining to distribution of the software without specific,
37 * written prior permission.  NCR makes no representations about the
38 * suitability of this software for any purpose.  It is provided "as is"
39 * without express or implied warranty.
40 *
41 * NCR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
42 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
43 * NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
44 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
45 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
46 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
47 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 */
49
50#ifndef _XTRANSINT_H_
51#define _XTRANSINT_H_
52
53/*
54 * XTRANSDEBUG will enable the PRMSG() macros used in the X Transport
55 * Interface code. Each use of the PRMSG macro has a level associated with
56 * it. XTRANSDEBUG is defined to be a level. If the invocation level is =<
57 * the value of XTRANSDEBUG, then the message will be printed out to stderr.
58 * Recommended levels are:
59 *
60 *	XTRANSDEBUG=1	Error messages
61 *	XTRANSDEBUG=2 API Function Tracing
62 *	XTRANSDEBUG=3 All Function Tracing
63 *	XTRANSDEBUG=4 printing of intermediate values
64 *	XTRANSDEBUG=5 really detailed stuff
65#define XTRANSDEBUG 2
66 *
67 * Defining XTRANSDEBUGTIMESTAMP will cause printing timestamps with each
68 * message.
69 */
70
71#if !defined(XTRANSDEBUG) && defined(XTRANS_TRANSPORT_C)
72#  define XTRANSDEBUG 1
73#endif
74
75#ifdef WIN32
76# define _WILLWINSOCK_
77#endif
78
79#include "Xtrans.h"
80
81#ifndef _X_UNUSED  /* Defined in Xfuncproto.h in xproto >= 7.0.22 */
82# define _X_UNUSED  /* */
83#endif
84
85#ifdef XTRANSDEBUG
86# include <stdio.h>
87#endif /* XTRANSDEBUG */
88
89#include <errno.h>
90
91#ifndef WIN32
92#  include <sys/socket.h>
93# include <netinet/in.h>
94# include <arpa/inet.h>
95
96/*
97 * Moved the setting of NEED_UTSNAME to this header file from Xtrans.c,
98 * to avoid a race condition. JKJ (6/5/97)
99 */
100
101# if defined(_POSIX_SOURCE) || defined(USG) || defined(SVR4) || defined(__SVR4) || defined(__SCO__)
102#  ifndef NEED_UTSNAME
103#   define NEED_UTSNAME
104#  endif
105#  include <sys/utsname.h>
106# endif
107
108#  define ESET(val) errno = val
109# define EGET() errno
110
111#else /* WIN32 */
112
113# include <limits.h>	/* for USHRT_MAX */
114
115# define ESET(val) WSASetLastError(val)
116# define EGET() WSAGetLastError()
117
118#endif /* WIN32 */
119
120#include <stddef.h>
121
122#ifdef X11_t
123#define X_TCP_PORT	6000
124#endif
125
126#if XTRANS_SEND_FDS
127
128struct _XtransConnFd {
129    struct _XtransConnFd   *next;
130    int                    fd;
131    int                    do_close;
132};
133
134#endif
135
136struct _XtransConnInfo {
137    struct _Xtransport     *transptr;
138    int		index;
139    char	*priv;
140    int		flags;
141    int		fd;
142    char	*port;
143    int		family;
144    char	*addr;
145    int		addrlen;
146    char	*peeraddr;
147    int		peeraddrlen;
148    struct _XtransConnFd        *recv_fds;
149    struct _XtransConnFd        *send_fds;
150};
151
152#define XTRANS_OPEN_COTS_CLIENT       1
153#define XTRANS_OPEN_COTS_SERVER       2
154
155typedef struct _Xtransport {
156    const char	*TransName;
157    int		flags;
158
159#ifdef TRANS_CLIENT
160
161    XtransConnInfo (*OpenCOTSClient)(
162	struct _Xtransport *,	/* transport */
163	const char *,		/* protocol */
164	const char *,		/* host */
165	const char *		/* port */
166    );
167
168#endif /* TRANS_CLIENT */
169
170#ifdef TRANS_SERVER
171    const char **	nolisten;
172    XtransConnInfo (*OpenCOTSServer)(
173	struct _Xtransport *,	/* transport */
174	const char *,		/* protocol */
175	const char *,		/* host */
176	const char *		/* port */
177    );
178
179#endif /* TRANS_SERVER */
180
181#ifdef TRANS_REOPEN
182
183    XtransConnInfo (*ReopenCOTSServer)(
184	struct _Xtransport *,	/* transport */
185        int,			/* fd */
186        const char *		/* port */
187    );
188
189#endif /* TRANS_REOPEN */
190
191
192    int	(*SetOption)(
193	XtransConnInfo,		/* connection */
194	int,			/* option */
195	int			/* arg */
196    );
197
198#ifdef TRANS_SERVER
199/* Flags */
200# define ADDR_IN_USE_ALLOWED	1
201
202    int	(*CreateListener)(
203	XtransConnInfo,		/* connection */
204	const char *,		/* port */
205	unsigned int		/* flags */
206    );
207
208    int	(*ResetListener)(
209	XtransConnInfo		/* connection */
210    );
211
212    XtransConnInfo (*Accept)(
213	XtransConnInfo,		/* connection */
214        int *			/* status */
215    );
216
217#endif /* TRANS_SERVER */
218
219#ifdef TRANS_CLIENT
220
221    int	(*Connect)(
222	XtransConnInfo,		/* connection */
223	const char *,		/* host */
224	const char *		/* port */
225    );
226
227#endif /* TRANS_CLIENT */
228
229    int	(*BytesReadable)(
230	XtransConnInfo,		/* connection */
231	BytesReadable_t *	/* pend */
232    );
233
234    int	(*Read)(
235	XtransConnInfo,		/* connection */
236	char *,			/* buf */
237	int			/* size */
238    );
239
240    int	(*Write)(
241	XtransConnInfo,		/* connection */
242	char *,			/* buf */
243	int			/* size */
244    );
245
246    int	(*Readv)(
247	XtransConnInfo,		/* connection */
248	struct iovec *,		/* buf */
249	int			/* size */
250    );
251
252    int	(*Writev)(
253	XtransConnInfo,		/* connection */
254	struct iovec *,		/* buf */
255	int			/* size */
256    );
257
258#if XTRANS_SEND_FDS
259    int (*SendFd)(
260	XtransConnInfo,		/* connection */
261        int,                    /* fd */
262        int                     /* do_close */
263    );
264
265    int (*RecvFd)(
266	XtransConnInfo		/* connection */
267    );
268#endif
269
270    int	(*Disconnect)(
271	XtransConnInfo		/* connection */
272    );
273
274    int	(*Close)(
275	XtransConnInfo		/* connection */
276    );
277
278    int	(*CloseForCloning)(
279	XtransConnInfo		/* connection */
280    );
281
282} Xtransport;
283
284
285typedef struct _Xtransport_table {
286    Xtransport	*transport;
287    int		transport_id;
288} Xtransport_table;
289
290
291/*
292 * Flags for the flags member of Xtransport.
293 */
294
295#define TRANS_ALIAS	(1<<0)	/* record is an alias, don't create server */
296#define TRANS_LOCAL	(1<<1)	/* local transport */
297#define TRANS_DISABLED	(1<<2)	/* Don't open this one */
298#define TRANS_NOLISTEN  (1<<3)  /* Don't listen on this one */
299#define TRANS_NOUNLINK	(1<<4)	/* Don't unlink transport endpoints */
300#define TRANS_ABSTRACT	(1<<5)	/* Use abstract sockets if available */
301#define TRANS_NOXAUTH	(1<<6)	/* Don't verify authentication (because it's secure some other way at the OS layer) */
302#define TRANS_RECEIVED	(1<<7)  /* The fd for this has already been opened by someone else. */
303
304/* Flags to preserve when setting others */
305#define TRANS_KEEPFLAGS	(TRANS_NOUNLINK|TRANS_ABSTRACT)
306
307#ifdef XTRANS_TRANSPORT_C /* only provide static function prototypes when
308			     building the transport.c file that has them in */
309
310#ifdef __clang__
311/* Not all clients make use of all provided statics */
312#pragma clang diagnostic push
313#pragma clang diagnostic ignored "-Wunused-function"
314#endif
315
316/*
317 * readv() and writev() don't exist or don't work correctly on some
318 * systems, so they may be emulated.
319 */
320
321#ifdef WIN32
322
323#define READV(ciptr, iov, iovcnt)	TRANS(ReadV)(ciptr, iov, iovcnt)
324
325static	int TRANS(ReadV)(
326    XtransConnInfo,	/* ciptr */
327    struct iovec *,	/* iov */
328    int			/* iovcnt */
329);
330
331#else
332
333#define READV(ciptr, iov, iovcnt)	readv(ciptr->fd, iov, iovcnt)
334
335#endif /* WIN32 */
336
337
338#ifdef WIN32
339
340#define WRITEV(ciptr, iov, iovcnt)	TRANS(WriteV)(ciptr, iov, iovcnt)
341
342static int TRANS(WriteV)(
343    XtransConnInfo,	/* ciptr */
344    struct iovec *,	/* iov */
345    int 		/* iovcnt */
346);
347
348#else
349
350#define WRITEV(ciptr, iov, iovcnt)	writev(ciptr->fd, iov, iovcnt)
351
352#endif /* WIN32 */
353
354
355static int is_numeric (
356    const char *	/* str */
357);
358
359#ifdef TRANS_SERVER
360static int trans_mkdir (
361    const char *,	/* path */
362    int			/* mode */
363);
364#endif
365
366#ifdef __clang__
367#pragma clang diagnostic pop
368#endif
369
370/*
371 * Some XTRANSDEBUG stuff
372 */
373
374#ifdef XTRANSDEBUG
375#include <stdarg.h>
376
377/*
378 * The X server provides ErrorF() & VErrorF(), for other software that uses
379 * xtrans, we provide our own simple versions.
380 */
381# if defined(XSERV_t) && defined(TRANS_SERVER)
382#  include "os.h"
383# else
384static inline void _X_ATTRIBUTE_PRINTF(1, 0)
385VErrorF(const char *f, va_list args)
386{
387    vfprintf(stderr, f, args);
388    fflush(stderr);
389}
390
391static inline void  _X_ATTRIBUTE_PRINTF(1, 2)
392ErrorF(const char *f, ...)
393{
394    va_list args;
395
396    va_start(args, f);
397    VErrorF(f, args);
398    va_end(args);
399}
400# endif /* xserver */
401#endif /* XTRANSDEBUG */
402
403static inline void  _X_ATTRIBUTE_PRINTF(2, 3)
404prmsg(int lvl, const char *f, ...)
405{
406#ifdef XTRANSDEBUG
407    va_list args;
408
409    va_start(args, f);
410    if (lvl <= XTRANSDEBUG) {
411	int saveerrno = errno;
412
413	ErrorF("%s", __xtransname);
414	VErrorF(f, args);
415
416# ifdef XTRANSDEBUGTIMESTAMP
417	{
418	    struct timeval tp;
419	    gettimeofday(&tp, 0);
420	    ErrorF("timestamp (ms): %d\n",
421		   tp.tv_sec * 1000 + tp.tv_usec / 1000);
422	}
423# endif
424	errno = saveerrno;
425    }
426    va_end(args);
427#endif /* XTRANSDEBUG */
428}
429
430#endif /* XTRANS_TRANSPORT_C */
431
432#endif /* _XTRANSINT_H_ */
433