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(SVR4) || defined(__SVR4)
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	const 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)	/* This previously meant that abstract sockets should be used available.  For security
301                                 * reasons, this is now a no-op on the client side, but it is still supported for servers.
302                                 */
303#define TRANS_NOXAUTH	(1<<6)	/* Don't verify authentication (because it's secure some other way at the OS layer) */
304#define TRANS_RECEIVED	(1<<7)  /* The fd for this has already been opened by someone else. */
305
306/* Flags to preserve when setting others */
307#define TRANS_KEEPFLAGS	(TRANS_NOUNLINK|TRANS_ABSTRACT)
308
309#ifdef XTRANS_TRANSPORT_C /* only provide static function prototypes when
310			     building the transport.c file that has them in */
311
312#ifdef __clang__
313/* Not all clients make use of all provided statics */
314#pragma clang diagnostic push
315#pragma clang diagnostic ignored "-Wunused-function"
316#endif
317
318/*
319 * readv() and writev() don't exist or don't work correctly on some
320 * systems, so they may be emulated.
321 */
322
323#ifdef WIN32
324
325#define READV(ciptr, iov, iovcnt)	TRANS(ReadV)(ciptr, iov, iovcnt)
326
327static	int TRANS(ReadV)(
328    XtransConnInfo,	/* ciptr */
329    struct iovec *,	/* iov */
330    int			/* iovcnt */
331);
332
333#else
334
335#define READV(ciptr, iov, iovcnt)	readv(ciptr->fd, iov, iovcnt)
336
337#endif /* WIN32 */
338
339
340#ifdef WIN32
341
342#define WRITEV(ciptr, iov, iovcnt)	TRANS(WriteV)(ciptr, iov, iovcnt)
343
344static int TRANS(WriteV)(
345    XtransConnInfo,	/* ciptr */
346    struct iovec *,	/* iov */
347    int 		/* iovcnt */
348);
349
350#else
351
352#define WRITEV(ciptr, iov, iovcnt)	writev(ciptr->fd, iov, iovcnt)
353
354#endif /* WIN32 */
355
356#ifdef TRANS_SERVER
357static int trans_mkdir (
358    const char *,	/* path */
359    int			/* mode */
360);
361#endif
362
363#ifdef __clang__
364#pragma clang diagnostic pop
365#endif
366
367/*
368 * Some XTRANSDEBUG stuff
369 */
370
371#ifdef XTRANSDEBUG
372#include <stdarg.h>
373
374/*
375 * The X server and the font server both provide ErrorF() & VErrorF(). For
376 * other software that uses xtrans, we provide our own simple
377 * versions.
378 */
379# if (defined(XSERV_t) || defined(TRANS_HAS_ERRORF)) && defined(TRANS_SERVER)
380#  include "os.h"
381# else
382static inline void _X_ATTRIBUTE_PRINTF(1, 0)
383VErrorF(const char *f, va_list args)
384{
385    vfprintf(stderr, f, args);
386    fflush(stderr);
387}
388
389static inline void  _X_ATTRIBUTE_PRINTF(1, 2)
390ErrorF(const char *f, ...)
391{
392    va_list args;
393
394    va_start(args, f);
395    VErrorF(f, args);
396    va_end(args);
397}
398# endif /* xserver */
399#endif /* XTRANSDEBUG */
400
401static inline void  _X_ATTRIBUTE_PRINTF(2, 3)
402prmsg(int lvl, const char *f, ...)
403{
404#ifdef XTRANSDEBUG
405    va_list args;
406
407    va_start(args, f);
408    if (lvl <= XTRANSDEBUG) {
409	int saveerrno = errno;
410
411	ErrorF("%s", __xtransname);
412	VErrorF(f, args);
413
414# ifdef XTRANSDEBUGTIMESTAMP
415	{
416	    struct timeval tp;
417	    gettimeofday(&tp, 0);
418	    ErrorF("timestamp (ms): %d\n",
419		   tp.tv_sec * 1000 + tp.tv_usec / 1000);
420	}
421# endif
422	errno = saveerrno;
423    }
424    va_end(args);
425#endif /* XTRANSDEBUG */
426}
427
428#endif /* XTRANS_TRANSPORT_C */
429
430#endif /* _XTRANSINT_H_ */
431