Xtransint.h revision 6a3641a6
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#define XTRANS_OPEN_CLTS_CLIENT       3
155#define XTRANS_OPEN_CLTS_SERVER       4
156
157
158typedef struct _Xtransport {
159    const char	*TransName;
160    int		flags;
161
162#ifdef TRANS_CLIENT
163
164    XtransConnInfo (*OpenCOTSClient)(
165	struct _Xtransport *,	/* transport */
166	const char *,		/* protocol */
167	const char *,		/* host */
168	const char *		/* port */
169    );
170
171#endif /* TRANS_CLIENT */
172
173#ifdef TRANS_SERVER
174    const char **	nolisten;
175    XtransConnInfo (*OpenCOTSServer)(
176	struct _Xtransport *,	/* transport */
177	const char *,		/* protocol */
178	const char *,		/* host */
179	const char *		/* port */
180    );
181
182#endif /* TRANS_SERVER */
183
184#ifdef TRANS_CLIENT
185
186    XtransConnInfo (*OpenCLTSClient)(
187	struct _Xtransport *,	/* transport */
188	const char *,		/* protocol */
189	const char *,		/* host */
190	const char *		/* port */
191    );
192
193#endif /* TRANS_CLIENT */
194
195#ifdef TRANS_SERVER
196
197    XtransConnInfo (*OpenCLTSServer)(
198	struct _Xtransport *,	/* transport */
199	const char *,		/* protocol */
200	const char *,		/* host */
201	const char *		/* port */
202    );
203
204#endif /* TRANS_SERVER */
205
206
207#ifdef TRANS_REOPEN
208
209    XtransConnInfo (*ReopenCOTSServer)(
210	struct _Xtransport *,	/* transport */
211        int,			/* fd */
212        const char *		/* port */
213    );
214
215    XtransConnInfo (*ReopenCLTSServer)(
216	struct _Xtransport *,	/* transport */
217        int,			/* fd */
218        const char *		/* port */
219    );
220
221#endif /* TRANS_REOPEN */
222
223
224    int	(*SetOption)(
225	XtransConnInfo,		/* connection */
226	int,			/* option */
227	int			/* arg */
228    );
229
230#ifdef TRANS_SERVER
231/* Flags */
232# define ADDR_IN_USE_ALLOWED	1
233
234    int	(*CreateListener)(
235	XtransConnInfo,		/* connection */
236	const char *,		/* port */
237	unsigned int		/* flags */
238    );
239
240    int	(*ResetListener)(
241	XtransConnInfo		/* connection */
242    );
243
244    XtransConnInfo (*Accept)(
245	XtransConnInfo,		/* connection */
246        int *			/* status */
247    );
248
249#endif /* TRANS_SERVER */
250
251#ifdef TRANS_CLIENT
252
253    int	(*Connect)(
254	XtransConnInfo,		/* connection */
255	const char *,		/* host */
256	const char *		/* port */
257    );
258
259#endif /* TRANS_CLIENT */
260
261    int	(*BytesReadable)(
262	XtransConnInfo,		/* connection */
263	BytesReadable_t *	/* pend */
264    );
265
266    int	(*Read)(
267	XtransConnInfo,		/* connection */
268	char *,			/* buf */
269	int			/* size */
270    );
271
272    int	(*Write)(
273	XtransConnInfo,		/* connection */
274	char *,			/* buf */
275	int			/* size */
276    );
277
278    int	(*Readv)(
279	XtransConnInfo,		/* connection */
280	struct iovec *,		/* buf */
281	int			/* size */
282    );
283
284    int	(*Writev)(
285	XtransConnInfo,		/* connection */
286	struct iovec *,		/* buf */
287	int			/* size */
288    );
289
290#if XTRANS_SEND_FDS
291    int (*SendFd)(
292	XtransConnInfo,		/* connection */
293        int,                    /* fd */
294        int                     /* do_close */
295    );
296
297    int (*RecvFd)(
298	XtransConnInfo		/* connection */
299    );
300#endif
301
302    int	(*Disconnect)(
303	XtransConnInfo		/* connection */
304    );
305
306    int	(*Close)(
307	XtransConnInfo		/* connection */
308    );
309
310    int	(*CloseForCloning)(
311	XtransConnInfo		/* connection */
312    );
313
314} Xtransport;
315
316
317typedef struct _Xtransport_table {
318    Xtransport	*transport;
319    int		transport_id;
320} Xtransport_table;
321
322
323/*
324 * Flags for the flags member of Xtransport.
325 */
326
327#define TRANS_ALIAS	(1<<0)	/* record is an alias, don't create server */
328#define TRANS_LOCAL	(1<<1)	/* local transport */
329#define TRANS_DISABLED	(1<<2)	/* Don't open this one */
330#define TRANS_NOLISTEN  (1<<3)  /* Don't listen on this one */
331#define TRANS_NOUNLINK	(1<<4)	/* Don't unlink transport endpoints */
332#define TRANS_ABSTRACT	(1<<5)	/* Use abstract sockets if available */
333#define TRANS_NOXAUTH	(1<<6)	/* Don't verify authentication (because it's secure some other way at the OS layer) */
334#define TRANS_RECEIVED	(1<<7)  /* The fd for this has already been opened by someone else. */
335
336/* Flags to preserve when setting others */
337#define TRANS_KEEPFLAGS	(TRANS_NOUNLINK|TRANS_ABSTRACT)
338
339#ifdef XTRANS_TRANSPORT_C /* only provide static function prototypes when
340			     building the transport.c file that has them in */
341
342#ifdef __clang__
343/* Not all clients make use of all provided statics */
344#pragma clang diagnostic push
345#pragma clang diagnostic ignored "-Wunused-function"
346#endif
347
348/*
349 * readv() and writev() don't exist or don't work correctly on some
350 * systems, so they may be emulated.
351 */
352
353#if defined(SYSV) && defined(__i386__) && !defined(__SCO__) && !defined(sun) || defined(WIN32)
354
355#define READV(ciptr, iov, iovcnt)	TRANS(ReadV)(ciptr, iov, iovcnt)
356
357static	int TRANS(ReadV)(
358    XtransConnInfo,	/* ciptr */
359    struct iovec *,	/* iov */
360    int			/* iovcnt */
361);
362
363#else
364
365#define READV(ciptr, iov, iovcnt)	readv(ciptr->fd, iov, iovcnt)
366
367#endif /* CRAY || (SYSV && __i386__) || WIN32 || __sxg__ || */
368
369
370#if defined(SYSV) && defined(__i386__) && !defined(__SCO__) && !defined(sun) || defined(WIN32)
371
372#define WRITEV(ciptr, iov, iovcnt)	TRANS(WriteV)(ciptr, iov, iovcnt)
373
374static int TRANS(WriteV)(
375    XtransConnInfo,	/* ciptr */
376    struct iovec *,	/* iov */
377    int 		/* iovcnt */
378);
379
380#else
381
382#define WRITEV(ciptr, iov, iovcnt)	writev(ciptr->fd, iov, iovcnt)
383
384#endif /* CRAY || WIN32 || __sxg__ */
385
386
387static int is_numeric (
388    const char *	/* str */
389);
390
391#ifdef TRANS_SERVER
392static int trans_mkdir (
393    const char *,	/* path */
394    int			/* mode */
395);
396#endif
397
398#ifdef __clang__
399#pragma clang diagnostic pop
400#endif
401
402/*
403 * Some XTRANSDEBUG stuff
404 */
405
406#ifdef XTRANSDEBUG
407#include <stdarg.h>
408
409/*
410 * The X server provides ErrorF() & VErrorF(), for other software that uses
411 * xtrans, we provide our own simple versions.
412 */
413# if defined(XSERV_t) && defined(TRANS_SERVER)
414#  include "os.h"
415# else
416static inline void _X_ATTRIBUTE_PRINTF(1, 0)
417VErrorF(const char *f, va_list args)
418{
419    vfprintf(stderr, f, args);
420    fflush(stderr);
421}
422
423static inline void  _X_ATTRIBUTE_PRINTF(1, 2)
424ErrorF(const char *f, ...)
425{
426    va_list args;
427
428    va_start(args, f);
429    VErrorF(f, args);
430    va_end(args);
431}
432# endif /* xserver */
433#endif /* XTRANSDEBUG */
434
435static inline void  _X_ATTRIBUTE_PRINTF(2, 3)
436prmsg(int lvl, const char *f, ...)
437{
438#ifdef XTRANSDEBUG
439    va_list args;
440
441    va_start(args, f);
442    if (lvl <= XTRANSDEBUG) {
443	int saveerrno = errno;
444
445	ErrorF("%s", __xtransname);
446	VErrorF(f, args);
447
448# ifdef XTRANSDEBUGTIMESTAMP
449	{
450	    struct timeval tp;
451	    gettimeofday(&tp, 0);
452	    ErrorF("timestamp (ms): %d\n",
453		   tp.tv_sec * 1000 + tp.tv_usec / 1000);
454	}
455# endif
456	errno = saveerrno;
457    }
458    va_end(args);
459#endif /* XTRANSDEBUG */
460}
461
462#endif /* XTRANS_TRANSPORT_C */
463
464#endif /* _XTRANSINT_H_ */
465