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