diff options
Diffstat (limited to 'nx-X11/lib/xtrans/Xtranssock.c')
-rw-r--r-- | nx-X11/lib/xtrans/Xtranssock.c | 1854 |
1 files changed, 967 insertions, 887 deletions
diff --git a/nx-X11/lib/xtrans/Xtranssock.c b/nx-X11/lib/xtrans/Xtranssock.c index 06c7719bf..62bfca734 100644 --- a/nx-X11/lib/xtrans/Xtranssock.c +++ b/nx-X11/lib/xtrans/Xtranssock.c @@ -24,9 +24,30 @@ /**************************************************************************/ /* + * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ +/* Copyright 1993, 1994, 1998 The Open Group -Copyright 2002 Sun Microsystems, Inc. All rights reserved. Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that @@ -50,9 +71,7 @@ not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from the copyright holders. -*/ - -/* Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA + * Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA * * All Rights Reserved * @@ -108,21 +127,14 @@ from the copyright holders. #ifdef UNIXCONN #ifndef X_NO_SYS_UN -#ifndef Lynx #include <sys/un.h> -#else -#include <un.h> -#endif #endif #include <sys/stat.h> #endif -#if defined(hpux) || (defined(MOTOROLA) && defined(SYSV)) -#define NO_TCP_H -#endif #ifndef NO_TCP_H -#if defined(__osf__) || defined(linux) || defined(__GLIBC__) || defined(AIXV5) +#if defined(linux) || defined(__GLIBC__) #include <sys/param.h> #endif /* osf */ #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) @@ -133,17 +145,11 @@ from the copyright holders. #endif /* !NO_TCP_H */ #include <sys/ioctl.h> -#if defined(SVR4) && !defined(DGUX) && !defined(_SEQUENT_) +#if defined(SVR4) || defined(__SVR4) #include <sys/filio.h> #endif -#if (defined(i386) && defined(SYSV)) && !defined(SCO325) && !defined(sun) -#include <net/errno.h> -#endif - -#if (defined(i386) && defined(SYSV)) && (!defined(ISC) || !defined(I_NREAD) || defined(SCO325)) || defined(_SEQUENT_) -#include <sys/stropts.h> -#endif +#include <unistd.h> #else /* !WIN32 */ @@ -169,37 +175,10 @@ from the copyright holders. #undef SO_DONTLINGER #endif -#if defined(__UNIXOS2__) -#if defined(NOT_EMX09A) -static int IBMsockInit = 0; -#define SocketInitOnce()\ - if (!IBMsockInit) {\ - sock_init();\ - IBMsockInit = 1;\ - } - - -#undef EINTR -#define EINTR SOCEINTR -#undef EINVAL -#define EINVAL SOCEINVAL -#undef errno -#define errno sock_errno() -#undef close -#define close soclose -#undef ioctl -#define ioctl sockioctl -#else -#define SocketInitOnce() /**/ -#endif -/* this is still not there */ -#define SOCKET int -#else /* others don't need this */ #define SocketInitOnce() /**/ -#endif -#if defined(linux) +#ifdef linux #define HAVE_ABSTRACT_SOCKETS #endif @@ -212,6 +191,7 @@ static int IBMsockInit = 0; #ifndef BACKLOG #define BACKLOG MIN_BACKLOG #endif + /* * This is the Socket implementation of the X Transport service layer * @@ -220,8 +200,8 @@ static int IBMsockInit = 0; * */ -typedef struct _Sockettrans2dev { - char *transname; +typedef struct _Sockettrans2dev { + const char *transname; int family; int devcotsname; int devcltsname; @@ -255,36 +235,6 @@ static int TRANS(SocketINETClose) (XtransConnInfo ciptr); #ifdef UNIXCONN -#ifdef hpux - -#if defined(X11_t) -#define UNIX_PATH "/usr/spool/sockets/X11/" -#define UNIX_DIR "/usr/spool/sockets/X11" -#define OLD_UNIX_PATH "/tmp/.X11-unix/X" -#endif /* X11_t */ -#if defined(XIM_t) -#define UNIX_PATH "/usr/spool/sockets/XIM/" -#define UNIX_DIR "/usr/spool/sockets/XIM" -#define OLD_UNIX_PATH "/tmp/.XIM-unix/XIM" -#endif /* XIM_t */ -#if defined(FS_t) || defined(FONT_t) -#define UNIX_PATH "/usr/spool/sockets/fontserv/" -#define UNIX_DIR "/usr/spool/sockets/fontserv" -#endif /* FS_t || FONT_t */ -#if defined(ICE_t) -#define UNIX_PATH "/usr/spool/sockets/ICE/" -#define UNIX_DIR "/usr/spool/sockets/ICE" -#endif /* ICE_t */ -#if defined(TEST_t) -#define UNIX_PATH "/usr/spool/sockets/xtrans_test/" -#define UNIX_DIR "/usr/spool/sockets/xtrans_test" -#endif -#if defined(LBXPROXY_t) -#define UNIX_PATH "/usr/spool/sockets/X11/" -#define UNIX_DIR "/usr/spool/sockets/X11" -#endif - -#else /* !hpux */ #if defined(X11_t) #define UNIX_PATH "/tmp/.X11-unix/X" @@ -302,16 +252,7 @@ static int TRANS(SocketINETClose) (XtransConnInfo ciptr); #define UNIX_PATH "/tmp/.ICE-unix/" #define UNIX_DIR "/tmp/.ICE-unix" #endif /* ICE_t */ -#if defined(TEST_t) -#define UNIX_PATH "/tmp/.Test-unix/test" -#define UNIX_DIR "/tmp/.Test-unix" -#endif -#if defined(LBXPROXY_t) -#define UNIX_PATH "/tmp/.X11-unix/X" -#define UNIX_DIR "/tmp/.X11-unix" -#endif -#endif /* hpux */ #endif /* UNIXCONN */ @@ -321,17 +262,12 @@ static int TRANS(SocketINETClose) (XtransConnInfo ciptr); #define MAXHOSTNAMELEN 255 #endif -/* - * This provides compatibility for apps linked against system libraries - * that don't have IPv6 support. - */ -#if defined(IPv6) && defined(AF_INET6) -static const struct in6_addr local_in6addr_any = IN6ADDR_ANY_INIT; -#pragma weak in6addr_any = local_in6addr_any -#ifndef __USLC__ -#pragma weak getaddrinfo -#endif -static int haveIPv6 = 1; +#if defined HAVE_SOCKLEN_T || (defined(IPv6) && defined(AF_INET6)) +# define SOCKLEN_T socklen_t +#elif defined(SVR4) || defined(__SVR4) || defined(__SCO__) +# define SOCKLEN_T size_t +#else +# define SOCKLEN_T int #endif #ifndef X11_t @@ -347,6 +283,10 @@ static int haveIPv6 = 1; #ifdef NX_TRANS_SOCKET +#ifdef XTRANS_SEND_FDS + #error XTRANS_SEND_FDS is unsupported for NX! +#endif + #ifdef TRANS_CLIENT #include <nx/NX.h> @@ -366,172 +306,8 @@ static _NXProxyConnInfo *_NXProxyConnInfoTab[NX_PROXY_CONN_LIMIT]; #endif /* #ifdef TRANS_CLIENT */ -/* - * Override the UNIX_DIR and UNIX_PATH settings and - * make them configurable, based on the NX_TEMP or - * the TEMP environment. - * - * We must be careful as the same defines are used - * for different directories, based on the subsystem - * that is compiling this, while we want to override - * only the '/tmp/.X11-unix' and '/tmp/.X11-unix/X' - * settings. - */ - -static char _NXUnixDir[1024]; -static char _NXUnixPath[1024]; - -static char *_NXGetUnixDir(char *dir) -{ - const char *tempDir; - - PRMSG (3, "_NXGetUnixDir(%s)\n", dir, 0, 0); - - if (strcmp(dir, UNIX_DIR) != 0) - { -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixDir: Returning other Unix directory [%s].\n", dir); -#endif - return dir; - } - - /* - * Check the environment only once. - */ - - if (*_NXUnixDir != '\0') - { - return _NXUnixDir; - } - -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixDir: Trying with the NX_TEMP environment.\n"); -#endif - - tempDir = getenv("NX_TEMP"); - - if (tempDir == NULL || *tempDir == '\0') - { -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixDir: Trying with the TEMP environment.\n"); -#endif - - tempDir = getenv("TEMP"); - } - - if (tempDir != NULL && *tempDir != '\0') - { - if (strlen(tempDir) + strlen("/.X11-unix") + 1 > 1024) - { -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixDir: WARNING! Maximum length of X11 Unix directory exceeded.\n"); -#endif - goto _NXGetUnixDirError; - } - - strcpy(_NXUnixDir, tempDir); - strcat(_NXUnixDir, "/.X11-unix"); - -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixDir: Using X11 Unix directory [%s].\n", _NXUnixDir); -#endif - - return _NXUnixDir; - } - -_NXGetUnixDirError: - - strcpy(_NXUnixDir, dir); - -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixDir: Returning default X11 Unix directory [%s].\n", _NXUnixDir); -#endif - - return _NXUnixDir; -} - -static char *_NXGetUnixPath(char *path) -{ - const char *unixDir; - - PRMSG (3, "_NXGetUnixPath(%s)\n", path, 0, 0); - - if (strcmp(path, UNIX_PATH) != 0) - { -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixPath: Returning other X11 Unix path [%s].\n", path); -#endif - return path; - } - - /* - * Check the environment only once. - */ - - if (*_NXUnixPath != '\0') - { - return _NXUnixPath; - } - - unixDir = _NXGetUnixDir(UNIX_DIR); - -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixPath: Got X11 Unix directory [%s].\n", unixDir); -#endif - - if (strlen(unixDir) + strlen("/X") + 1 > 1024) - { -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixPath: WARNING! Maximum length of X11 Unix path exceeded.\n"); -#endif - - goto _NXGetUnixPathError; - } - - strcpy(_NXUnixPath, unixDir); - strcat(_NXUnixPath, "/X"); - -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixPath: Returning X11 Unix path [%s].\n", _NXUnixPath); -#endif - - return _NXUnixPath; - -_NXGetUnixPathError: - - strcpy(_NXUnixPath, path); - -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetUnixPath: Returning default X11 Unix path [%s].\n", _NXUnixPath); -#endif - - return _NXUnixPath; -} - -#ifdef hpux - -static char *_NXGetOldUnixPath(char *path) -{ - PRMSG (3, "_NXGetOldUnixPath(%s)\n", path, 0, 0); - - if (strcmp(path, OLD_UNIX_PATH) == 0) - { -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetOldUnixPath: Returning X11 Unix path [%s].\n", - _NXGetUnixPath(path)); -#endif - - return _NXGetUnixPath(path); - } - -#ifdef NX_TRANS_TEST - fprintf(stderr, "_NXGetOldUnixPath: Returning other old X11 Unix path [%s].\n", path); -#endif - - return path; -} - -#endif /* #ifdef hpux */ +static char *_NXGetUnixDir(char *dir); +static char *_NXGetUnixPath(char *path); /* * Forcibly close any connection attempt on the @@ -549,7 +325,7 @@ void TRANS(SocketRejectConnection) (XtransConnInfo ciptr) struct timeval t; int f; - PRMSG (3, "SocketRejectConnection(%x)\n", ciptr, 0, 0); + prmsg (3, "SocketRejectConnection(%p)\n", ciptr); FD_ZERO(&fs); FD_SET(ciptr -> fd, &fs); @@ -570,7 +346,7 @@ void TRANS(SocketRejectConnection) (XtransConnInfo ciptr) /* * If there is one, close it. */ - + if ((f = accept(ciptr -> fd, &sa, &sa_l)) >= 0) { #ifdef NX_TRANS_TEST @@ -608,7 +384,7 @@ static XtransConnInfo TRANS(SocketCreateConnInfo) () if ((ciptr = (XtransConnInfo) calloc (1, sizeof(struct _XtransConnInfo))) == NULL) { - PRMSG (1, "SocketCreateConnInfo: malloc failed\n", 0, 0, 0); + prmsg (1, "SocketCreateConnInfo: malloc failed\n"); return NULL; } @@ -623,7 +399,7 @@ static XtransConnInfo TRANS(SocketCreateConnInfo) () if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0) { - PRMSG (1, "SocketCreateConnInfo: socketpair() failed.\n", 0, 0, 0); + prmsg (1, "SocketCreateConnInfo: socketpair() failed.\n"); free ((char *) ciptr); return NULL; } @@ -646,15 +422,15 @@ static XtransConnInfo TRANS(SocketCreateConnInfo) () if (ciptr->fd >= NX_PROXY_CONN_LIMIT) { - PRMSG (1, "SocketCreateConnInfo: No space for a new _NXProxyConnInfo for [%d].\n", - ciptr->fd, 0, 0); + prmsg (1, "SocketCreateConnInfo: No space for a new _NXProxyConnInfo for [%d].\n", + ciptr->fd); free ((char *) ciptr); return NULL; } else if (_NXProxyConnInfoTab[ciptr->fd] != NULL) { - PRMSG (1, "SocketCreateConnInfo: _NXProxyConnInfo for [%d] is not NULL. Exiting.\n", - ciptr->fd, 0, 0); + prmsg (1, "SocketCreateConnInfo: _NXProxyConnInfo for [%d] is not NULL. Exiting.\n", + ciptr->fd); exit(1); } @@ -662,7 +438,7 @@ static XtransConnInfo TRANS(SocketCreateConnInfo) () if (_NXProxyConnInfoTab[ciptr->fd] == NULL) { - PRMSG (1, "SocketCreateConnInfo: Alloc of _NXProxyConnInfo failed.\n", 0, 0, 0); + prmsg (1, "SocketCreateConnInfo: Alloc of _NXProxyConnInfo failed.\n"); free ((char *) ciptr); return NULL; } @@ -702,21 +478,20 @@ static int TRANS(SocketConnectConnInfo) (XtransConnInfo ciptr, char *host, char if (proxy_conn == NULL) { - PRMSG (1, "SocketConnectConnInfo: Pointer to _NXProxyConnInfo is NULL. Exiting.\n", 0, 0, 0); + prmsg (1, "SocketConnectConnInfo: Pointer to _NXProxyConnInfo is NULL. Exiting.\n"); exit(1); } else if (_NXProxyConnInfoTab[ciptr->fd] != (_NXProxyConnInfo *) ciptr->priv) { - PRMSG (1, "SocketConnectConnInfo: Can't find _NXProxyConnInfo in table. Exiting.\n", - 0, 0, 0); + prmsg (1, "SocketConnectConnInfo: Can't find _NXProxyConnInfo in table. Exiting.\n"); exit(1); } if (strlen(host) + strlen(port) + 1 >= 1023) { - PRMSG (1, "SocketConnectConnInfo: Length of NX display string '%s:%s' would exceed %d characters.\n", + prmsg (1, "SocketConnectConnInfo: Length of NX display string '%s:%s' would exceed %d characters.\n", host, port, 1023); return TRANS_CONNECT_FAILED; @@ -737,8 +512,8 @@ static int TRANS(SocketConnectConnInfo) (XtransConnInfo ciptr, char *host, char #endif #endif { - PRMSG (1, "SocketConnectConnInfo: Cannot set close-on-exec on local NX descriptor [%d].\n", - proxy_conn -> local, 0, 0); + prmsg (1, "SocketConnectConnInfo: Cannot set close-on-exec on local NX descriptor [%d].\n", + proxy_conn -> local); return TRANS_CONNECT_FAILED; } @@ -750,8 +525,7 @@ static int TRANS(SocketConnectConnInfo) (XtransConnInfo ciptr, char *host, char if (NXTransCreate(NX_FD_ANY, NX_MODE_CLIENT, display) < 0) { - PRMSG (1, "SocketConnectConnInfo: Cannot create the NX transport.\n", - 0, 0, 0); + prmsg (1, "SocketConnectConnInfo: Cannot create the NX transport.\n"); return TRANS_CONNECT_FAILED; } @@ -781,28 +555,27 @@ static void TRANS(SocketCloseConnInfo) (XtransConnInfo ciptr) if (proxy_conn == NULL) { - PRMSG (1, "SocketCloseConnInfo: Pointer to _NXProxyConnInfo is NULL. Exiting.\n", 0, 0, 0); + prmsg (1, "SocketCloseConnInfo: Pointer to _NXProxyConnInfo is NULL. Exiting.\n"); exit(1); } else if (ciptr->fd >= NX_PROXY_CONN_LIMIT || _NXProxyConnInfoTab[ciptr->fd] != (_NXProxyConnInfo *) ciptr->priv) { - PRMSG (1, "SocketCloseConnInfo: Can't find _NXProxyConnInfo in table. Exiting.\n", - 0, 0, 0); + prmsg (1, "SocketCloseConnInfo: Can't find _NXProxyConnInfo in table. Exiting.\n"); exit(1); } else if (_NXProxyConnInfoTab[ciptr->fd] -> info != ciptr || _NXProxyConnInfoTab[ciptr->fd] -> local != ciptr->fd) { - PRMSG (1, "SocketCloseConnInfo: Invalid _NXProxyConnInfo structure for [%d]. Exiting.\n", - ciptr->fd, 0, 0); + prmsg (1, "SocketCloseConnInfo: Invalid _NXProxyConnInfo structure for [%d]. Exiting.\n", + ciptr->fd); exit(1); } else if (proxy_conn->local < 0 || proxy_conn->remote < 0) { - PRMSG (1, "SocketCloseConnInfo: Invalid socket pair in NX connection for [%d]. Exiting.\n", - ciptr->fd, 0, 0); + prmsg (1, "SocketCloseConnInfo: Invalid socket pair in NX connection for [%d]. Exiting.\n", + ciptr->fd); exit(1); } @@ -846,7 +619,7 @@ int TRANS(SocketCongestionChange) (XtransConnInfo ciptr, int *state) _NXProxyConnInfo *proxy_conn; - PRMSG (3, "SocketCongestionChange(%x)\n", ciptr, 0, 0); + prmsg (3, "SocketCongestionChange(%p)\n", ciptr); proxy_conn = (_NXProxyConnInfo *) ciptr->priv; @@ -893,12 +666,12 @@ int TRANS(SocketCongestionChange) (XtransConnInfo ciptr, int *state) */ static int -TRANS(SocketSelectFamily) (int first, char *family) +TRANS(SocketSelectFamily) (int first, const char *family) { int i; - PRMSG (3,"SocketSelectFamily(%s)\n", family, 0, 0); + prmsg (3,"SocketSelectFamily(%s)\n", family); for (i = first + 1; i < NUMSOCKETFAMILIES;i++) { @@ -921,40 +694,32 @@ TRANS(SocketINETGetAddr) (XtransConnInfo ciptr) { #if defined(IPv6) && defined(AF_INET6) struct sockaddr_storage socknamev6; -#endif - struct sockaddr_in socknamev4; - void *socknamePtr; -#if defined(SVR4) || defined(__SCO__) -# define SOCKLEN_T_PTR void* - size_t namelen; #else -# define SOCKLEN_T_PTR socklen_t* - socklen_t namelen; + struct sockaddr_in socknamev4; #endif + void *socknamePtr; + SOCKLEN_T namelen; - PRMSG (3,"SocketINETGetAddr(%p)\n", ciptr, 0, 0); + prmsg (3,"SocketINETGetAddr(%p)\n", ciptr); #if defined(IPv6) && defined(AF_INET6) - if (haveIPv6) - { namelen = sizeof(socknamev6); socknamePtr = &socknamev6; - } - else -#endif - { +#else namelen = sizeof(socknamev4); socknamePtr = &socknamev4; - } +#endif + + bzero(socknamePtr, namelen); if (getsockname (ciptr->fd,(struct sockaddr *) socknamePtr, - (SOCKLEN_T_PTR)&namelen) < 0) + (void *)&namelen) < 0) { #ifdef WIN32 errno = WSAGetLastError(); #endif - PRMSG (1,"SocketINETGetAddr: getsockname() failed: %d\n", - EGET(),0, 0); + prmsg (1,"SocketINETGetAddr: getsockname() failed: %d\n", + EGET()); return -1; } @@ -962,24 +727,18 @@ TRANS(SocketINETGetAddr) (XtransConnInfo ciptr) * Everything looks good: fill in the XtransConnInfo structure. */ - if ((ciptr->addr = (char *) malloc (namelen)) == NULL) + if ((ciptr->addr = malloc (namelen)) == NULL) { - PRMSG (1, - "SocketINETGetAddr: Can't allocate space for the addr\n", - 0, 0, 0); + prmsg (1, + "SocketINETGetAddr: Can't allocate space for the addr\n"); return -1; } #if defined(IPv6) && defined(AF_INET6) - if (haveIPv6) - { ciptr->family = ((struct sockaddr *)socknamePtr)->sa_family; - } - else -#endif - { +#else ciptr->family = socknamev4.sin_family; - } +#endif ciptr->addrlen = namelen; memcpy (ciptr->addr, socknamePtr, ciptr->addrlen); @@ -1001,14 +760,10 @@ TRANS(SocketINETGetPeerAddr) (XtransConnInfo ciptr) #endif struct sockaddr_in socknamev4; void *socknamePtr; -#if defined(SVR4) || defined(__SCO__) - size_t namelen; -#else - int namelen; -#endif + SOCKLEN_T namelen; #if defined(IPv6) && defined(AF_INET6) - if (haveIPv6 && ciptr->family == AF_INET6) + if (ciptr->family == AF_INET6) { namelen = sizeof(socknamev6); socknamePtr = &socknamev6; @@ -1020,7 +775,9 @@ TRANS(SocketINETGetPeerAddr) (XtransConnInfo ciptr) socknamePtr = &socknamev4; } - PRMSG (3,"SocketINETGetPeerAddr(%p)\n", ciptr, 0, 0); + bzero(socknamePtr, namelen); + + prmsg (3,"SocketINETGetPeerAddr(%p)\n", ciptr); if (getpeername (ciptr->fd, (struct sockaddr *) socknamePtr, (void *)&namelen) < 0) @@ -1028,8 +785,8 @@ TRANS(SocketINETGetPeerAddr) (XtransConnInfo ciptr) #ifdef WIN32 errno = WSAGetLastError(); #endif - PRMSG (1,"SocketINETGetPeerAddr: getpeername() failed: %d\n", - EGET(), 0, 0); + prmsg (1,"SocketINETGetPeerAddr: getpeername() failed: %d\n", + EGET()); return -1; } @@ -1037,11 +794,10 @@ TRANS(SocketINETGetPeerAddr) (XtransConnInfo ciptr) * Everything looks good: fill in the XtransConnInfo structure. */ - if ((ciptr->peeraddr = (char *) malloc (namelen)) == NULL) + if ((ciptr->peeraddr = malloc (namelen)) == NULL) { - PRMSG (1, - "SocketINETGetPeerAddr: Can't allocate space for the addr\n", - 0, 0, 0); + prmsg (1, + "SocketINETGetPeerAddr: Can't allocate space for the addr\n"); return -1; } @@ -1058,20 +814,11 @@ TRANS(SocketOpen) (int i, int type) { XtransConnInfo ciptr; - PRMSG (3,"SocketOpen(%d,%d)\n", i, type, 0); - -#if defined(IPv6) && defined(AF_INET6) - if (getaddrinfo == NULL) - haveIPv6 = 0; - - if (!haveIPv6 && Sockettrans2devtab[i].family == AF_INET6) - return NULL; -#endif + prmsg (3,"SocketOpen(%d,%d)\n", i, type); - if ((ciptr = (XtransConnInfo) calloc ( - 1, sizeof(struct _XtransConnInfo))) == NULL) + if ((ciptr = calloc (1, sizeof(struct _XtransConnInfo))) == NULL) { - PRMSG (1, "SocketOpen: malloc failed\n", 0, 0, 0); + prmsg (1, "SocketOpen: malloc failed\n"); return NULL; } @@ -1079,17 +826,17 @@ TRANS(SocketOpen) (int i, int type) Sockettrans2devtab[i].protocol)) < 0 #ifndef WIN32 #if (defined(X11_t) && !defined(USE_POLL)) || defined(FS_t) || defined(FONT_t) - || ciptr->fd >= TRANS_OPEN_MAX + || ciptr->fd >= sysconf(_SC_OPEN_MAX) #endif #endif ) { #ifdef WIN32 errno = WSAGetLastError(); #endif - PRMSG (2, "SocketOpen: socket() failed for %s\n", - Sockettrans2devtab[i].transname, 0, 0); + prmsg (2, "SocketOpen: socket() failed for %s\n", + Sockettrans2devtab[i].transname); - free ((char *) ciptr); + free (ciptr); return NULL; } @@ -1110,6 +857,27 @@ TRANS(SocketOpen) (int i, int type) } #endif + /* + * Some systems provide a really small default buffer size for + * UNIX sockets. Bump it up a bit such that large transfers don't + * proceed at glacial speed. + */ +#ifdef SO_SNDBUF + if (Sockettrans2devtab[i].family == AF_UNIX) + { + SOCKLEN_T len = sizeof (int); + int val; + + if (getsockopt (ciptr->fd, SOL_SOCKET, SO_SNDBUF, + (char *) &val, &len) == 0 && val < 64 * 1024) + { + val = 64 * 1024; + setsockopt (ciptr->fd, SOL_SOCKET, SO_SNDBUF, + (char *) &val, sizeof (int)); + } + } +#endif + return ciptr; } @@ -1117,22 +885,82 @@ TRANS(SocketOpen) (int i, int type) #ifdef TRANS_REOPEN static XtransConnInfo -TRANS(SocketReopen) (int i, int type, int fd, char *port) +TRANS(SocketReopen) (int i _X_UNUSED, int type, int fd, const char *port) { XtransConnInfo ciptr; + int portlen; + struct sockaddr *addr; + size_t addrlen; - PRMSG (3,"SocketReopen(%d,%d,%s)\n", type, fd, port); + prmsg (3,"SocketReopen(%d,%d,%s)\n", type, fd, port); - if ((ciptr = (XtransConnInfo) calloc ( - 1, sizeof(struct _XtransConnInfo))) == NULL) + if (port == NULL) { + prmsg (1, "SocketReopen: port was null!\n"); + return NULL; + } + + portlen = strlen(port) + 1; // include space for trailing null +#ifdef SOCK_MAXADDRLEN + if (portlen < 0 || portlen > (SOCK_MAXADDRLEN + 2)) { + prmsg (1, "SocketReopen: invalid portlen %d\n", portlen); + return NULL; + } + if (portlen < 14) portlen = 14; +#else + if (portlen < 0 || portlen > 14) { + prmsg (1, "SocketReopen: invalid portlen %d\n", portlen); + return NULL; + } +#endif /*SOCK_MAXADDRLEN*/ + + if ((ciptr = calloc (1, sizeof(struct _XtransConnInfo))) == NULL) { - PRMSG (1, "SocketReopen: malloc failed\n", 0, 0, 0); + prmsg (1, "SocketReopen: malloc(ciptr) failed\n"); return NULL; } ciptr->fd = fd; + addrlen = portlen + offsetof(struct sockaddr, sa_data); + if ((addr = calloc (1, addrlen)) == NULL) { + prmsg (1, "SocketReopen: malloc(addr) failed\n"); + free (ciptr); + return NULL; + } + ciptr->addr = (char *) addr; + ciptr->addrlen = addrlen; + + if ((ciptr->peeraddr = calloc (1, addrlen)) == NULL) { + prmsg (1, "SocketReopen: malloc(portaddr) failed\n"); + free (addr); + free (ciptr); + return NULL; + } + ciptr->peeraddrlen = addrlen; + + /* Initialize ciptr structure as if it were a normally-opened unix socket */ + ciptr->flags = TRANS_LOCAL | TRANS_NOUNLINK; +#ifdef BSD44SOCKETS + addr->sa_len = addrlen; +#endif + addr->sa_family = AF_UNIX; +#if defined(HAVE_STRLCPY) || defined(HAS_STRLCPY) + strlcpy(addr->sa_data, port, portlen); +#else + strncpy(addr->sa_data, port, portlen); +#endif + ciptr->family = AF_UNIX; + memcpy(ciptr->peeraddr, ciptr->addr, addrlen); + ciptr->port = rindex(addr->sa_data, ':'); + if (ciptr->port == NULL) { + if (is_numeric(addr->sa_data)) { + ciptr->port = addr->sa_data; + } + } else if (ciptr->port[0] == ':') { + ciptr->port++; + } + /* port should now point to portnum or NULL */ return ciptr; } @@ -1146,13 +974,13 @@ TRANS(SocketReopen) (int i, int type, int fd, char *port) #ifdef TRANS_CLIENT static XtransConnInfo -TRANS(SocketOpenCOTSClientBase) (char *transname, char *protocol, - char *host, char *port, int previndex) +TRANS(SocketOpenCOTSClientBase) (const char *transname, const char *protocol, + const char *host, const char *port, int previndex) { XtransConnInfo ciptr; int i = previndex; - PRMSG (2, "SocketOpenCOTSClient(%s,%s,%s)\n", + prmsg (2, "SocketOpenCOTSClient(%s,%s,%s)\n", protocol, host, port); SocketInitOnce(); @@ -1168,8 +996,8 @@ TRANS(SocketOpenCOTSClientBase) (char *transname, char *protocol, if (ciptr == NULL) { - PRMSG (1, "SocketOpenCOTSClient: Unable to create the NX connection info for %s.\n", - transname, 0, 0); + prmsg (1, "SocketOpenCOTSClient: Unable to create the NX connection info for %s.\n", + transname); return NULL; } @@ -1182,16 +1010,20 @@ TRANS(SocketOpenCOTSClientBase) (char *transname, char *protocol, #endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ if ((ciptr = TRANS(SocketOpen) ( - i, Sockettrans2devtab[i].devcotsname)) != NULL) + i, Sockettrans2devtab[i].devcotsname)) != NULL) { + /* Save the index for later use */ + + ciptr->index = i; break; + } } if (i < 0) { if (i == -1) - PRMSG (1,"SocketOpenCOTSClient: Unable to open socket for %s\n", - transname, 0, 0); + prmsg (1,"SocketOpenCOTSClient: Unable to open socket for %s\n", + transname); else - PRMSG (1,"SocketOpenCOTSClient: Unable to determine socket type for %s\n", - transname, 0, 0); + prmsg (1,"SocketOpenCOTSClient: Unable to determine socket type for %s\n", + transname); return NULL; } @@ -1201,16 +1033,12 @@ TRANS(SocketOpenCOTSClientBase) (char *transname, char *protocol, #endif - /* Save the index for later use */ - - ciptr->index = i; - return ciptr; } static XtransConnInfo -TRANS(SocketOpenCOTSClient) (Xtransport *thistrans, char *protocol, - char *host, char *port) +TRANS(SocketOpenCOTSClient) (Xtransport *thistrans, const char *protocol, + const char *host, const char *port) { return TRANS(SocketOpenCOTSClientBase)( thistrans->TransName, protocol, host, port, -1); @@ -1223,14 +1051,14 @@ TRANS(SocketOpenCOTSClient) (Xtransport *thistrans, char *protocol, #ifdef TRANS_SERVER static XtransConnInfo -TRANS(SocketOpenCOTSServer) (Xtransport *thistrans, char *protocol, - char *host, char *port) +TRANS(SocketOpenCOTSServer) (Xtransport *thistrans, const char *protocol, + const char *host, const char *port) { XtransConnInfo ciptr; int i = -1; - PRMSG (2,"SocketOpenCOTSServer(%s,%s,%s)\n", protocol, host, port); + prmsg (2,"SocketOpenCOTSServer(%s,%s,%s)\n", protocol, host, port); SocketInitOnce(); @@ -1241,11 +1069,11 @@ TRANS(SocketOpenCOTSServer) (Xtransport *thistrans, char *protocol, } if (i < 0) { if (i == -1) - PRMSG (1,"SocketOpenCOTSServer: Unable to open socket for %s\n", - thistrans->TransName, 0, 0); + prmsg (1,"SocketOpenCOTSServer: Unable to open socket for %s\n", + thistrans->TransName); else - PRMSG (1,"SocketOpenCOTSServer: Unable to determine socket type for %s\n", - thistrans->TransName, 0, 0); + prmsg (1,"SocketOpenCOTSServer: Unable to determine socket type for %s\n", + thistrans->TransName); return NULL; } @@ -1287,165 +1115,17 @@ TRANS(SocketOpenCOTSServer) (Xtransport *thistrans, char *protocol, #endif /* TRANS_SERVER */ -#ifdef TRANS_CLIENT - -static XtransConnInfo -TRANS(SocketOpenCLTSClient) (Xtransport *thistrans, char *protocol, - char *host, char *port) - -{ - XtransConnInfo ciptr; - int i = -1; - - PRMSG (2,"SocketOpenCLTSClient(%s,%s,%s)\n", protocol, host, port); - - SocketInitOnce(); - - while ((i = TRANS(SocketSelectFamily) (i, thistrans->TransName)) >= 0) { - -#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) - - if ((!strcmp(protocol, "local") || !strcmp(protocol, "nx")) && - (!strcasecmp(host, "nx") || !strncasecmp(host, "nx,", 3))) - { - ciptr = TRANS(SocketCreateConnInfo) (); - - if (ciptr == NULL) - { - PRMSG (1, "SocketOpenCLTSClient: Unable to create the NX connection info for %s.\n", - thistrans->TransName, 0, 0); - - return NULL; - } - - ciptr->index = i; - - return ciptr; - } - -#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ - - if ((ciptr = TRANS(SocketOpen) ( - i, Sockettrans2devtab[i].devcotsname)) != NULL) - break; - } - if (i < 0) { - if (i == -1) - PRMSG (1,"SocketOpenCLTSClient: Unable to open socket for %s\n", - thistrans->TransName, 0, 0); - else - PRMSG (1,"SocketOpenCLTSClient: Unable to determine socket type for %s\n", - thistrans->TransName, 0, 0); - return NULL; - } - -#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) - - ciptr->priv = NULL; - -#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ - - /* Save the index for later use */ - - ciptr->index = i; - - return ciptr; -} - -#endif /* TRANS_CLIENT */ - - -#ifdef TRANS_SERVER - -static XtransConnInfo -TRANS(SocketOpenCLTSServer) (Xtransport *thistrans, char *protocol, - char *host, char *port) - -{ - XtransConnInfo ciptr; - int i = -1; - - PRMSG (2,"SocketOpenCLTSServer(%s,%s,%s)\n", protocol, host, port); - - SocketInitOnce(); - - while ((i = TRANS(SocketSelectFamily) (i, thistrans->TransName)) >= 0) { - if ((ciptr = TRANS(SocketOpen) ( - i, Sockettrans2devtab[i].devcotsname)) != NULL) - break; - } - if (i < 0) { - if (i == -1) - PRMSG (1,"SocketOpenCLTSServer: Unable to open socket for %s\n", - thistrans->TransName, 0, 0); - else - PRMSG (1,"SocketOpenCLTSServer: Unable to determine socket type for %s\n", - thistrans->TransName, 0, 0); - return NULL; - } - -#ifdef IPV6_V6ONLY - if (Sockettrans2devtab[i].family == AF_INET6) - { - int one = 1; - setsockopt(ciptr->fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(int)); - } -#endif - /* Save the index for later use */ - - ciptr->index = i; - - return ciptr; -} - -#endif /* TRANS_SERVER */ - - #ifdef TRANS_REOPEN static XtransConnInfo -TRANS(SocketReopenCOTSServer) (Xtransport *thistrans, int fd, char *port) - -{ - XtransConnInfo ciptr; - int i = -1; - - PRMSG (2, - "SocketReopenCOTSServer(%d, %s)\n", fd, port, 0); - - SocketInitOnce(); - - while ((i = TRANS(SocketSelectFamily) (i, thistrans->TransName)) >= 0) { - if ((ciptr = TRANS(SocketReopen) ( - i, Sockettrans2devtab[i].devcotsname, fd, port)) != NULL) - break; - } - if (i < 0) { - if (i == -1) - PRMSG (1,"SocketReopenCOTSServer: Unable to open socket for %s\n", - thistrans->TransName, 0, 0); - else - PRMSG (1,"SocketReopenCOTSServer: Unable to determine socket type for %s\n", - thistrans->TransName, 0, 0); - return NULL; - } - - /* Save the index for later use */ - - ciptr->index = i; - - return ciptr; -} - -static XtransConnInfo -TRANS(SocketReopenCLTSServer) (Xtransport *thistrans, int fd, char *port) +TRANS(SocketReopenCOTSServer) (Xtransport *thistrans, int fd, const char *port) { XtransConnInfo ciptr; int i = -1; - PRMSG (2, - "SocketReopenCLTSServer(%d, %s)\n", fd, port, 0); + prmsg (2, + "SocketReopenCOTSServer(%d, %s)\n", fd, port); SocketInitOnce(); @@ -1456,11 +1136,11 @@ TRANS(SocketReopenCLTSServer) (Xtransport *thistrans, int fd, char *port) } if (i < 0) { if (i == -1) - PRMSG (1,"SocketReopenCLTSServer: Unable to open socket for %s\n", - thistrans->TransName, 0, 0); + prmsg (1,"SocketReopenCOTSServer: Unable to open socket for %s\n", + thistrans->TransName); else - PRMSG (1,"SocketReopenCLTSServer: Unable to determine socket type for %s\n", - thistrans->TransName, 0, 0); + prmsg (1,"SocketReopenCOTSServer: Unable to determine socket type for %s\n", + thistrans->TransName); return NULL; } @@ -1478,7 +1158,7 @@ static int TRANS(SocketSetOption) (XtransConnInfo ciptr, int option, int arg) { - PRMSG (2,"SocketSetOption(%d,%d,%d)\n", ciptr->fd, option, arg); + prmsg (2,"SocketSetOption(%d,%d,%d)\n", ciptr->fd, option, arg); #ifdef NX_TRANS_TEST fprintf(stderr, "SocketSetOption: WARNING! Not setting option [%d] with value [%d] on descriptor [%d].\n", @@ -1519,16 +1199,16 @@ set_sun_path(const char *port, const char *upath, char *path, int abstract) #ifdef TRANS_SERVER static int -TRANS(SocketCreateListener) (XtransConnInfo ciptr, +TRANS(SocketCreateListener) (XtransConnInfo ciptr, struct sockaddr *sockname, int socknamelen, unsigned int flags) { - int namelen = socknamelen; + SOCKLEN_T namelen = socknamelen; int fd = ciptr->fd; int retry; - PRMSG (3, "SocketCreateListener(%x,%p)\n", ciptr, fd, 0); + prmsg (3, "SocketCreateListener(%p,%d)\n", ciptr, fd); if (Sockettrans2devtab[ciptr->index].family == AF_INET #if defined(IPv6) && defined(AF_INET6) @@ -1552,10 +1232,9 @@ TRANS(SocketCreateListener) (XtransConnInfo ciptr, else return TRANS_ADDR_IN_USE; } - + if (retry-- == 0) { - PRMSG (1, "SocketCreateListener: failed to bind listener\n", - 0, 0, 0); + prmsg (1, "SocketCreateListener: failed to bind listener\n"); close (fd); return TRANS_CREATE_LISTENER_FAILED; } @@ -1586,11 +1265,11 @@ TRANS(SocketCreateListener) (XtransConnInfo ciptr, if (listen (fd, BACKLOG) < 0) { - PRMSG (1, "SocketCreateListener: listen() failed\n", 0, 0, 0); + prmsg (1, "SocketCreateListener: listen() failed\n"); close (fd); return TRANS_CREATE_LISTENER_FAILED; } - + /* Set a flag to indicate that this connection is a listener */ ciptr->flags = 1 | (ciptr->flags & TRANS_KEEPFLAGS); @@ -1605,7 +1284,8 @@ TRANS(SocketCreateListener) (XtransConnInfo ciptr, #ifdef TCPCONN static int -TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int flags) +TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, const char *port, + unsigned int flags) { #if defined(IPv6) && defined(AF_INET6) @@ -1614,7 +1294,7 @@ TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int struct sockaddr_in sockname; #endif unsigned short sport; - int namelen = sizeof(sockname); + SOCKLEN_T namelen = sizeof(sockname); int status; long tmpport; #ifdef XTHREADS_NEEDS_BYNAMEPARAMS @@ -1625,8 +1305,8 @@ TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int #ifdef X11_t char portbuf[PORTBUFSIZE]; #endif - - PRMSG (2, "SocketINETCreateListener(%s)\n", port, 0, 0); + + prmsg (2, "SocketINETCreateListener(%s)\n", port); #ifdef X11_t /* @@ -1642,7 +1322,7 @@ TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int { /* fixup the server port address */ tmpport = X_TCP_PORT + strtol (port, (char**)NULL, 10); - sprintf (portbuf,"%lu", tmpport); + snprintf (portbuf, sizeof(portbuf), "%lu", tmpport); port = portbuf; } #endif @@ -1655,9 +1335,9 @@ TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int { if ((servp = _XGetservbyname (port,"tcp",sparams)) == NULL) { - PRMSG (1, + prmsg (1, "SocketINETCreateListener: Unable to get service for %s\n", - port, 0, 0); + port); return TRANS_CREATE_LISTENER_FAILED; } /* we trust getservbyname to return a valid number */ @@ -1666,11 +1346,11 @@ TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int else { tmpport = strtol (port, (char**)NULL, 10); - /* + /* * check that somehow the port address isn't negative or in * the range of reserved port addresses. This can happen and - * be very bad if the server is suid-root and the user does - * something (dumb) like `X :60049`. + * be very bad if the server is suid-root and the user does + * something (dumb) like `X :60049`. */ if (tmpport < 1024 || tmpport > USHRT_MAX) return TRANS_CREATE_LISTENER_FAILED; @@ -1712,30 +1392,28 @@ TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, char *port, unsigned int if ((status = TRANS(SocketCreateListener) (ciptr, (struct sockaddr *) &sockname, namelen, flags)) < 0) { - PRMSG (1, - "SocketINETCreateListener: ...SocketCreateListener() failed\n", - 0, 0, 0); + prmsg (1, + "SocketINETCreateListener: ...SocketCreateListener() failed\n"); return status; } if (TRANS(SocketINETGetAddr) (ciptr) < 0) { - PRMSG (1, - "SocketINETCreateListener: ...SocketINETGetAddr() failed\n", - 0, 0, 0); + prmsg (1, + "SocketINETCreateListener: ...SocketINETGetAddr() failed\n"); return TRANS_CREATE_LISTENER_FAILED; } return 0; } -#endif /* SOCKCONN */ +#endif /* TCPCONN */ #ifdef UNIXCONN static int -TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, char *port, +TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, const char *port, unsigned int flags) { @@ -1751,8 +1429,8 @@ TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, char *port, abstract = ciptr->transptr->flags & TRANS_ABSTRACT; #endif - PRMSG (2, "SocketUNIXCreateListener(%s)\n", - port ? port : "NULL", 0, 0); + prmsg (2, "SocketUNIXCreateListener(%s)\n", + port ? port : "NULL"); /* Make sure the directory is created */ @@ -1766,12 +1444,12 @@ TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, char *port, #endif #ifdef NX_TRANS_SOCKET if (!abstract && trans_mkdir(_NXGetUnixDir(UNIX_DIR), mode) == -1) { - PRMSG (1, "SocketUNIXCreateListener: mkdir(%s) failed, errno = %d\n", - _NXGetUnixDir(UNIX_DIR), errno, 0); + prmsg (1, "SocketUNIXCreateListener: mkdir(%s) failed, errno = %d\n", + _NXGetUnixDir(UNIX_DIR), errno); #else if (!abstract && trans_mkdir(UNIX_DIR, mode) == -1) { - PRMSG (1, "SocketUNIXCreateListener: mkdir(%s) failed, errno = %d\n", - UNIX_DIR, errno, 0); + prmsg (1, "SocketUNIXCreateListener: mkdir(%s) failed, errno = %d\n", + UNIX_DIR, errno); #endif (void) umask (oldUmask); return TRANS_CREATE_LISTENER_FAILED; @@ -1785,28 +1463,23 @@ TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, char *port, snprintf (tmpport, sizeof(tmpport), "%s%ld", UNIX_PATH, (long)getpid()); port = tmpport; } - if (port && *port) { #ifdef NX_TRANS_SOCKET - if (set_sun_path(port, _NXGetUnixPath(UNIX_PATH), sockname.sun_path, abstract) != 0) { + if (set_sun_path(port, _NXGetUnixPath(UNIX_PATH), sockname.sun_path, abstract) != 0) { #else if (set_sun_path(port, UNIX_PATH, sockname.sun_path, abstract) != 0) { #endif - PRMSG (1, "SocketUNIXCreateListener: path too long\n", 0, 0, 0); + prmsg (1, "SocketUNIXCreateListener: path too long\n"); return TRANS_CREATE_LISTENER_FAILED; } - } else { -#ifdef NX_TRANS_SOCKET - sprintf (sockname.sun_path, "%s%ld", _NXGetUnixPath(UNIX_PATH), (long)getpid()); -#else - sprintf (sockname.sun_path, "%s%ld", UNIX_PATH, (long)getpid()); -#endif - } -#if (defined(BSD44SOCKETS) || defined(__UNIXWARE__)) && !defined(Lynx) +#if (defined(BSD44SOCKETS) || defined(__UNIXWARE__)) sockname.sun_len = strlen(sockname.sun_path); +#endif + +#if defined(BSD44SOCKETS) || defined(SUN_LEN) namelen = SUN_LEN(&sockname); #else - namelen = strlen(sockname.sun_path) + sizeof(sockname.sun_family); + namelen = strlen(sockname.sun_path) + offsetof(struct sockaddr_un, sun_path); #endif #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST) @@ -1823,9 +1496,8 @@ TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, char *port, if ((status = TRANS(SocketCreateListener) (ciptr, (struct sockaddr *) &sockname, namelen, flags)) < 0) { - PRMSG (1, - "SocketUNIXCreateListener: ...SocketCreateListener() failed\n", - 0, 0, 0); + prmsg (1, + "SocketUNIXCreateListener: ...SocketCreateListener() failed\n"); (void) umask (oldUmask); return status; } @@ -1839,11 +1511,10 @@ TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, char *port, namelen = sizeof (sockname); /* this will always make it the same size */ - if ((ciptr->addr = (char *) malloc (namelen)) == NULL) + if ((ciptr->addr = malloc (namelen)) == NULL) { - PRMSG (1, - "SocketUNIXCreateListener: Can't allocate space for the addr\n", - 0, 0, 0); + prmsg (1, + "SocketUNIXCreateListener: Can't allocate space for the addr\n"); (void) umask (oldUmask); return TRANS_CREATE_LISTENER_FAILED; } @@ -1860,6 +1531,7 @@ TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, char *port, return 0; } + static int TRANS(SocketUNIXResetListener) (XtransConnInfo ciptr) @@ -1877,16 +1549,17 @@ TRANS(SocketUNIXResetListener) (XtransConnInfo ciptr) abstract = ciptr->transptr->flags & TRANS_ABSTRACT; #endif - PRMSG (3, "SocketUNIXResetListener(%p,%d)\n", ciptr, ciptr->fd, 0); + prmsg (3, "SocketUNIXResetListener(%p,%d)\n", ciptr, ciptr->fd); if (!abstract && ( stat (unsock->sun_path, &statb) == -1 || ((statb.st_mode & S_IFMT) != -#if (defined (sun) && defined(SVR4)) || defined(NCR) || defined(SCO325) || !defined(S_IFSOCK) - S_IFIFO)) +#if defined(NCR) || defined(SCO325) || !defined(S_IFSOCK) + S_IFIFO #else - S_IFSOCK))) + S_IFSOCK #endif + ))) { int oldUmask = umask (0); @@ -1898,12 +1571,12 @@ TRANS(SocketUNIXResetListener) (XtransConnInfo ciptr) #endif #ifdef NX_TRANS_SOCKET if (trans_mkdir(_NXGetUnixDir(UNIX_DIR), mode) == -1) { - PRMSG (1, "SocketUNIXResetListener: mkdir(%s) failed, errno = %d\n", - _NXGetUnixDir(UNIX_DIR), errno, 0); + prmsg (1, "SocketUNIXResetListener: mkdir(%s) failed, errno = %d\n", + _NXGetUnixDir(UNIX_DIR), errno); #else if (trans_mkdir(UNIX_DIR, mode) == -1) { - PRMSG (1, "SocketUNIXResetListener: mkdir(%s) failed, errno = %d\n", - UNIX_DIR, errno, 0); + prmsg (1, "SocketUNIXResetListener: mkdir(%s) failed, errno = %d\n", + UNIX_DIR, errno); #endif (void) umask (oldUmask); return TRANS_RESET_FAILURE; @@ -1954,14 +1627,13 @@ TRANS(SocketINETAccept) (XtransConnInfo ciptr, int *status) { XtransConnInfo newciptr; struct sockaddr_in sockname; - int namelen = sizeof(sockname); + SOCKLEN_T namelen = sizeof(sockname); - PRMSG (2, "SocketINETAccept(%p,%d)\n", ciptr, ciptr->fd, 0); + prmsg (2, "SocketINETAccept(%p,%d)\n", ciptr, ciptr->fd); - if ((newciptr = (XtransConnInfo) calloc ( - 1, sizeof(struct _XtransConnInfo))) == NULL) + if ((newciptr = calloc (1, sizeof(struct _XtransConnInfo))) == NULL) { - PRMSG (1, "SocketINETAccept: malloc failed\n", 0, 0, 0); + prmsg (1, "SocketINETAccept: malloc failed\n"); *status = TRANS_ACCEPT_BAD_MALLOC; return NULL; } @@ -1972,7 +1644,7 @@ TRANS(SocketINETAccept) (XtransConnInfo ciptr, int *status) #ifdef WIN32 errno = WSAGetLastError(); #endif - PRMSG (1, "SocketINETAccept: accept() failed\n", 0, 0, 0); + prmsg (1, "SocketINETAccept: accept() failed\n"); free (newciptr); *status = TRANS_ACCEPT_FAILED; return NULL; @@ -1991,15 +1663,14 @@ TRANS(SocketINETAccept) (XtransConnInfo ciptr, int *status) #endif /* - * Get this address again because the transport may give a more + * Get this address again because the transport may give a more * specific address now that a connection is established. */ if (TRANS(SocketINETGetAddr) (newciptr) < 0) { - PRMSG (1, - "SocketINETAccept: ...SocketINETGetAddr() failed:\n", - 0, 0, 0); + prmsg (1, + "SocketINETAccept: ...SocketINETGetAddr() failed:\n"); close (newciptr->fd); free (newciptr); *status = TRANS_ACCEPT_MISC_ERROR; @@ -2008,9 +1679,8 @@ TRANS(SocketINETAccept) (XtransConnInfo ciptr, int *status) if (TRANS(SocketINETGetPeerAddr) (newciptr) < 0) { - PRMSG (1, - "SocketINETAccept: ...SocketINETGetPeerAddr() failed:\n", - 0, 0, 0); + prmsg (1, + "SocketINETAccept: ...SocketINETGetPeerAddr() failed:\n"); close (newciptr->fd); if (newciptr->addr) free (newciptr->addr); free (newciptr); @@ -2033,18 +1703,13 @@ TRANS(SocketUNIXAccept) (XtransConnInfo ciptr, int *status) { XtransConnInfo newciptr; struct sockaddr_un sockname; -#if defined(SVR4) || defined(__SCO__) - size_t namelen = sizeof sockname; -#else - int namelen = sizeof sockname; -#endif + SOCKLEN_T namelen = sizeof sockname; - PRMSG (2, "SocketUNIXAccept(%p,%d)\n", ciptr, ciptr->fd, 0); + prmsg (2, "SocketUNIXAccept(%p,%d)\n", ciptr, ciptr->fd); - if ((newciptr = (XtransConnInfo) calloc ( - 1, sizeof(struct _XtransConnInfo))) == NULL) + if ((newciptr = calloc (1, sizeof(struct _XtransConnInfo))) == NULL) { - PRMSG (1, "SocketUNIXAccept: malloc() failed\n", 0, 0, 0); + prmsg (1, "SocketUNIXAccept: malloc() failed\n"); *status = TRANS_ACCEPT_BAD_MALLOC; return NULL; } @@ -2052,29 +1717,28 @@ TRANS(SocketUNIXAccept) (XtransConnInfo ciptr, int *status) if ((newciptr->fd = accept (ciptr->fd, (struct sockaddr *) &sockname, (void *)&namelen)) < 0) { - PRMSG (1, "SocketUNIXAccept: accept() failed\n", 0, 0, 0); + prmsg (1, "SocketUNIXAccept: accept() failed\n"); free (newciptr); *status = TRANS_ACCEPT_FAILED; return NULL; } + ciptr->addrlen = namelen; /* * Get the socket name and the peer name from the listener socket, * since this is unix domain. */ - if ((newciptr->addr = (char *) malloc (ciptr->addrlen)) == NULL) + if ((newciptr->addr = malloc (ciptr->addrlen)) == NULL) { - PRMSG (1, - "SocketUNIXAccept: Can't allocate space for the addr\n", - 0, 0, 0); + prmsg (1, + "SocketUNIXAccept: Can't allocate space for the addr\n"); close (newciptr->fd); free (newciptr); *status = TRANS_ACCEPT_BAD_MALLOC; return NULL; } - /* * if the socket is abstract, we already modified the address to have a * @ instead of the initial NUL, so no need to do that again here. @@ -2083,18 +1747,17 @@ TRANS(SocketUNIXAccept) (XtransConnInfo ciptr, int *status) newciptr->addrlen = ciptr->addrlen; memcpy (newciptr->addr, ciptr->addr, newciptr->addrlen); - if ((newciptr->peeraddr = (char *) malloc (ciptr->addrlen)) == NULL) + if ((newciptr->peeraddr = malloc (ciptr->addrlen)) == NULL) { - PRMSG (1, - "SocketUNIXAccept: Can't allocate space for the addr\n", - 0, 0, 0); + prmsg (1, + "SocketUNIXAccept: Can't allocate space for the addr\n"); close (newciptr->fd); if (newciptr->addr) free (newciptr->addr); free (newciptr); *status = TRANS_ACCEPT_BAD_MALLOC; return NULL; } - + newciptr->peeraddrlen = ciptr->addrlen; memcpy (newciptr->peeraddr, ciptr->addr, newciptr->addrlen); @@ -2117,7 +1780,7 @@ TRANS(SocketUNIXAccept) (XtransConnInfo ciptr, int *status) #if defined(IPv6) && defined(AF_INET6) struct addrlist { struct addrinfo * addr; - struct addrinfo * firstaddr; + struct addrinfo * firstaddr; char port[PORTBUFSIZE]; char host[MAXHOSTNAMELEN]; }; @@ -2126,7 +1789,8 @@ static struct addrlist *addrlist = NULL; static int -TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) +TRANS(SocketINETConnect) (XtransConnInfo ciptr, + const char *host, const char *port) { struct sockaddr * socketaddr = NULL; @@ -2136,23 +1800,23 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) struct addrinfo hints; char ntopbuf[INET6_ADDRSTRLEN]; int resetonce = 0; -#endif +#else struct sockaddr_in sockname; + struct hostent *hostp; + struct servent *servp; + unsigned long tmpaddr; +#endif #ifdef XTHREADS_NEEDS_BYNAMEPARAMS _Xgethostbynameparams hparams; _Xgetservbynameparams sparams; #endif - struct hostent *hostp; - struct servent *servp; - unsigned long tmpaddr; #ifdef X11_t char portbuf[PORTBUFSIZE]; #endif - long tmpport; char hostnamebuf[256]; /* tmp space */ - PRMSG (2,"SocketINETConnect(%d,%s,%s)\n", ciptr->fd, host, port); + prmsg (2,"SocketINETConnect(%d,%s,%s)\n", ciptr->fd, host, port); if (!host) { @@ -2173,14 +1837,14 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) if (is_numeric (port)) { - tmpport = X_TCP_PORT + strtol (port, (char**)NULL, 10); - sprintf (portbuf, "%lu", tmpport); + long tmpport = X_TCP_PORT + strtol (port, (char**)NULL, 10); + snprintf (portbuf, sizeof(portbuf), "%lu", tmpport); port = portbuf; } #endif #if defined(IPv6) && defined(AF_INET6) - if (haveIPv6) { + { if (addrlist != NULL) { if (strcmp(host,addrlist->host) || strcmp(port,addrlist->port)) { if (addrlist->firstaddr) @@ -2203,7 +1867,7 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) res = getaddrinfo(host,port,&hints,&addrlist->firstaddr); if (res != 0) { - PRMSG (1, "SocketINETConnect() can't get address " + prmsg (1, "SocketINETConnect() can't get address " "for %s:%s: %s\n", host, port, gai_strerror(res)); ESET(EINVAL); return TRANS_CONNECT_FAILED; @@ -2212,24 +1876,24 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) addrlist->addr ; res++) { addrlist->addr = addrlist->addr->ai_next; } - PRMSG(4,"Got New Address list with %d addresses\n", res, 0, 0); + prmsg(4,"Got New Address list with %d addresses\n", res); res = 0; addrlist->addr = NULL; } while (socketaddr == NULL) { if (addrlist->addr == NULL) { - if (resetonce) { + if (resetonce) { /* Already checked entire list - no usable addresses */ - PRMSG (1, "SocketINETConnect() no usable address " - "for %s:%s\n", host, port, 0); + prmsg (1, "SocketINETConnect() no usable address " + "for %s:%s\n", host, port); return TRANS_CONNECT_FAILED; } else { /* Go back to beginning of list */ resetonce = 1; addrlist->addr = addrlist->firstaddr; } - } + } socketaddr = addrlist->addr->ai_addr; socketaddrlen = addrlist->addr->ai_addrlen; @@ -2237,12 +1901,12 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) if (addrlist->addr->ai_family == AF_INET) { struct sockaddr_in *sin = (struct sockaddr_in *) socketaddr; - PRMSG (4,"SocketINETConnect() sockname.sin_addr = %s\n", + prmsg (4,"SocketINETConnect() sockname.sin_addr = %s\n", inet_ntop(addrlist->addr->ai_family,&sin->sin_addr, - ntopbuf,sizeof(ntopbuf)), 0, 0); + ntopbuf,sizeof(ntopbuf))); - PRMSG (4,"SocketINETConnect() sockname.sin_port = %d\n", - ntohs(sin->sin_port), 0, 0); + prmsg (4,"SocketINETConnect() sockname.sin_port = %d\n", + ntohs(sin->sin_port)); if (Sockettrans2devtab[ciptr->index].family == AF_INET6) { if (strcmp(Sockettrans2devtab[ciptr->index].transname, @@ -2264,26 +1928,24 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) Sockettrans2devtab[newciptr->index].family != AF_INET) { socketaddr = NULL; - PRMSG (4,"SocketINETConnect() Cannot get IPv4 " - " socketfor IPv4 address\n", 0,0,0); + prmsg (4,"SocketINETConnect() Cannot get IPv4 " + " socketfor IPv4 address\n"); } if (newciptr) free(newciptr); } else { socketaddr = NULL; - PRMSG (4,"SocketINETConnect Skipping IPv4 address\n", - 0,0,0); + prmsg (4,"SocketINETConnect Skipping IPv4 address\n"); } } } else if (addrlist->addr->ai_family == AF_INET6) { struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) socketaddr; - - PRMSG (4,"SocketINETConnect() sockname.sin6_addr = %s\n", + + prmsg (4,"SocketINETConnect() sockname.sin6_addr = %s\n", inet_ntop(addrlist->addr->ai_family, - &sin6->sin6_addr,ntopbuf,sizeof(ntopbuf)), - 0, 0); - PRMSG (4,"SocketINETConnect() sockname.sin6_port = %d\n", - ntohs(sin6->sin6_port), 0, 0); + &sin6->sin6_addr,ntopbuf,sizeof(ntopbuf))); + prmsg (4,"SocketINETConnect() sockname.sin6_port = %d\n", + ntohs(sin6->sin6_port)); if (Sockettrans2devtab[ciptr->index].family == AF_INET) { if (strcmp(Sockettrans2devtab[ciptr->index].transname, @@ -2302,8 +1964,8 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) Sockettrans2devtab[newciptr->index].family != AF_INET6) { socketaddr = NULL; - PRMSG (4,"SocketINETConnect() Cannot get IPv6 " - "socket for IPv6 address\n", 0,0,0); + prmsg (4,"SocketINETConnect() Cannot get IPv6 " + "socket for IPv6 address\n"); } if (newciptr) free(newciptr); @@ -2311,8 +1973,7 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) else { socketaddr = NULL; - PRMSG (4,"SocketINETConnect() Skipping IPv6 address\n", - 0,0,0); + prmsg (4,"SocketINETConnect() Skipping IPv6 address\n"); } } } else { @@ -2321,9 +1982,9 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) if (socketaddr == NULL) { addrlist->addr = addrlist->addr->ai_next; } - } - } else -#endif + } + } +#else { /* * Build the socket name. @@ -2350,22 +2011,24 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) tmpaddr = INADDR_NONE; } - PRMSG (4,"SocketINETConnect() inet_addr(%s) = %x\n", host, tmpaddr, 0); + prmsg (4,"SocketINETConnect() inet_addr(%s) = %x\n", host, tmpaddr); if (tmpaddr == INADDR_NONE) { if ((hostp = _XGethostbyname(host,hparams)) == NULL) { - PRMSG (1,"SocketINETConnect: Can't get address for %s\n", - host, 0, 0); + prmsg (1,"SocketINETConnect: Can't get address for %s\n", + host); ESET(EINVAL); return TRANS_CONNECT_FAILED; } if (hostp->h_addrtype != AF_INET) { /* is IP host? */ - PRMSG (1,"SocketINETConnect: not INET host%s\n", host, 0, 0); + prmsg (1,"SocketINETConnect: not INET host%s\n", host); ESET(EPROTOTYPE); return TRANS_CONNECT_FAILED; } + memcpy ((char *) &sockname.sin_addr, (char *) hostp->h_addr, sizeof (sockname.sin_addr)); + } else { sockname.sin_addr.s_addr = tmpaddr; } @@ -2378,23 +2041,24 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) if (!is_numeric (port)) { if ((servp = _XGetservbyname (port,"tcp",sparams)) == NULL) { - PRMSG (1,"SocketINETConnect: can't get service for %s\n", - port, 0, 0); + prmsg (1,"SocketINETConnect: can't get service for %s\n", + port); return TRANS_CONNECT_FAILED; } sockname.sin_port = htons (servp->s_port); } else { - tmpport = strtol (port, (char**)NULL, 10); + long tmpport = strtol (port, (char**)NULL, 10); if (tmpport < 1024 || tmpport > USHRT_MAX) return TRANS_CONNECT_FAILED; sockname.sin_port = htons (((unsigned short) tmpport)); } - PRMSG (4,"SocketINETConnect: sockname.sin_port = %d\n", - ntohs(sockname.sin_port), 0, 0); + prmsg (4,"SocketINETConnect: sockname.sin_port = %d\n", + ntohs(sockname.sin_port)); socketaddr = (struct sockaddr *) &sockname; socketaddrlen = sizeof(sockname); } +#endif /* * Turn on socket keepalive so the client process will eventually @@ -2440,12 +2104,12 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) * number of errors that made us quit before, since those * could be caused by trying to use an IPv6 address to contact * a machine with an IPv4-only server or other reasons that - * only affect one of a set of addresses. + * only affect one of a set of addresses. */ if (olderrno == ECONNREFUSED || olderrno == EINTR #if defined(IPv6) && defined(AF_INET6) - || (haveIPv6 && ((addrlist->addr->ai_next != NULL) || + || (((addrlist->addr->ai_next != NULL) || (addrlist->addr != addrlist->firstaddr)) && (olderrno == ENETUNREACH || olderrno == EAFNOSUPPORT || olderrno == EADDRNOTAVAIL || olderrno == ETIMEDOUT @@ -2460,38 +2124,36 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) res = TRANS_IN_PROGRESS; else { - PRMSG (2,"SocketINETConnect: Can't connect: errno = %d\n", - olderrno,0, 0); + prmsg (2,"SocketINETConnect: Can't connect: errno = %d\n", + olderrno); - res = TRANS_CONNECT_FAILED; + res = TRANS_CONNECT_FAILED; } } else { res = 0; - + /* * Sync up the address fields of ciptr. */ - + if (TRANS(SocketINETGetAddr) (ciptr) < 0) { - PRMSG (1, - "SocketINETConnect: ...SocketINETGetAddr() failed:\n", - 0, 0, 0); + prmsg (1, + "SocketINETConnect: ...SocketINETGetAddr() failed:\n"); res = TRANS_CONNECT_FAILED; } else if (TRANS(SocketINETGetPeerAddr) (ciptr) < 0) { - PRMSG (1, - "SocketINETConnect: ...SocketINETGetPeerAddr() failed:\n", - 0, 0, 0); + prmsg (1, + "SocketINETConnect: ...SocketINETGetPeerAddr() failed:\n"); res = TRANS_CONNECT_FAILED; } } #if defined(IPv6) && defined(AF_INET6) - if (haveIPv6 && res != 0) { + if (res != 0) { addrlist->addr = addrlist->addr->ai_next; } #endif @@ -2510,25 +2172,18 @@ TRANS(SocketINETConnect) (XtransConnInfo ciptr, char *host, char *port) */ static int -UnixHostReallyLocal (char *host) +UnixHostReallyLocal (const char *host) { char hostnamebuf[256]; -#if defined(IPv6) && defined(AF_INET6) - if (getaddrinfo == NULL) - haveIPv6 = 0; -#endif - TRANS(GetHostname) (hostnamebuf, sizeof (hostnamebuf)); if (strcmp (hostnamebuf, host) == 0) { return (1); - } + } else { #if defined(IPv6) && defined(AF_INET6) - else if (haveIPv6) - { struct addrinfo *localhostaddr; struct addrinfo *otherhostaddr; struct addrinfo *i, *j; @@ -2545,7 +2200,7 @@ UnixHostReallyLocal (char *host) for (j = otherhostaddr; j != NULL && equiv == 0; j = j->ai_next) { if (i->ai_family == j->ai_family) { if (i->ai_family == AF_INET) { - struct sockaddr_in *sinA + struct sockaddr_in *sinA = (struct sockaddr_in *) i->ai_addr; struct sockaddr_in *sinB = (struct sockaddr_in *) j->ai_addr; @@ -2556,9 +2211,9 @@ UnixHostReallyLocal (char *host) equiv = 1; } } else if (i->ai_family == AF_INET6) { - struct sockaddr_in6 *sinA + struct sockaddr_in6 *sinA = (struct sockaddr_in6 *) i->ai_addr; - struct sockaddr_in6 *sinB + struct sockaddr_in6 *sinB = (struct sockaddr_in6 *) j->ai_addr; struct in6_addr *A = &sinA->sin6_addr; struct in6_addr *B = &sinB->sin6_addr; @@ -2570,14 +2225,11 @@ UnixHostReallyLocal (char *host) } } } - + freeaddrinfo(localhostaddr); freeaddrinfo(otherhostaddr); return equiv; - } -#endif - else - { +#else /* * A host may have more than one network address. If any of the * network addresses of 'host' (specified to the connect call) @@ -2603,13 +2255,13 @@ UnixHostReallyLocal (char *host) * from the 1st call, so we must save the address list. */ - specified_local_addr_list[scount][0] = + specified_local_addr_list[scount][0] = hostp->h_addr_list[scount][0]; - specified_local_addr_list[scount][1] = + specified_local_addr_list[scount][1] = hostp->h_addr_list[scount][1]; - specified_local_addr_list[scount][2] = + specified_local_addr_list[scount][2] = hostp->h_addr_list[scount][2]; - specified_local_addr_list[scount][3] = + specified_local_addr_list[scount][3] = hostp->h_addr_list[scount][3]; scount++; } @@ -2625,17 +2277,17 @@ UnixHostReallyLocal (char *host) while (hostp->h_addr_list[j]) { - if ((specified_local_addr_list[i][0] == + if ((specified_local_addr_list[i][0] == hostp->h_addr_list[j][0]) && - (specified_local_addr_list[i][1] == + (specified_local_addr_list[i][1] == hostp->h_addr_list[j][1]) && - (specified_local_addr_list[i][2] == + (specified_local_addr_list[i][2] == hostp->h_addr_list[j][2]) && - (specified_local_addr_list[i][3] == + (specified_local_addr_list[i][3] == hostp->h_addr_list[j][3])) { /* They're equal, so we're done */ - + equiv = 1; break; } @@ -2646,15 +2298,17 @@ UnixHostReallyLocal (char *host) i++; } return (equiv); +#endif } } static int -TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, char *host, char *port) +TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, + const char *host, const char *port) { struct sockaddr_un sockname; - int namelen; + SOCKLEN_T namelen; int abstract = 0; @@ -2662,14 +2316,8 @@ TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, char *host, char *port) abstract = ciptr->transptr->flags & TRANS_ABSTRACT; #endif -#if defined(hpux) && defined(X11_t) - struct sockaddr_un old_sockname; - int old_namelen; -#endif - + prmsg (2,"SocketUNIXConnect(%d,%s,%s)\n", ciptr->fd, host, port); - PRMSG (2,"SocketUNIXConnect(%d,%s,%s)\n", ciptr->fd, host, port); - /* * Make sure 'host' is really local. If not, we return failure. * The reason we make this check is because a process may advertise @@ -2679,15 +2327,15 @@ TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, char *host, char *port) */ #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) - if (strcmp(host, "unix") != 0 && strcasecmp(host, "nx") != 0 && + if (host && *host && host[0]!='/' && strcmp(host, "unix") != 0 && strcasecmp(host, "nx") != 0 && strncasecmp(host, "nx,", 3) != 0 && !UnixHostReallyLocal (host)) #else - if (strcmp (host, "unix") != 0 && !UnixHostReallyLocal (host)) + if (host && *host && host[0]!='/' && strcmp (host, "unix") != 0 && !UnixHostReallyLocal (host)) #endif { - PRMSG (1, + prmsg (1, "SocketUNIXConnect: Cannot connect to non-local host %s\n", - host, 0, 0); + host); return TRANS_CONNECT_FAILED; } @@ -2698,15 +2346,14 @@ TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, char *host, char *port) if (!port || !*port) { - PRMSG (1,"SocketUNIXConnect: Missing port specification\n", - 0, 0, 0); + prmsg (1,"SocketUNIXConnect: Missing port specification\n"); return TRANS_CONNECT_FAILED; } /* * Build the socket name. */ - + sockname.sun_family = AF_UNIX; #ifdef NX_TRANS_SOCKET @@ -2714,34 +2361,21 @@ TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, char *host, char *port) #else if (set_sun_path(port, UNIX_PATH, sockname.sun_path, abstract) != 0) { #endif - PRMSG (1, "SocketUNIXConnect: path too long\n", 0, 0, 0); + prmsg (1, "SocketUNIXConnect: path too long\n"); return TRANS_CONNECT_FAILED; } -#if (defined(BSD44SOCKETS) || defined(__UNIXWARE__)) && !defined(Lynx) +#if (defined(BSD44SOCKETS) || defined(__UNIXWARE__)) sockname.sun_len = strlen (sockname.sun_path); +#endif + +#if defined(BSD44SOCKETS) || defined(SUN_LEN) namelen = SUN_LEN (&sockname); #else - namelen = strlen (sockname.sun_path) + sizeof (sockname.sun_family); + namelen = strlen (sockname.sun_path) + offsetof(struct sockaddr_un, sun_path); #endif -#if defined(hpux) && defined(X11_t) - /* - * This is gross, but it was in Xlib - */ - old_sockname.sun_family = AF_UNIX; -#ifdef NX_TRANS_SOCKET - if (set_sun_path(port, _NXGetOldUnixPath(OLD_UNIX_PATH), old_sockname.sun_path) != 0) { -#else - if (set_sun_path(port, OLD_UNIX_PATH, old_sockname.sun_path) != 0) { -#endif - PRMSG (1, "SocketUNIXConnect: path too long\n", 0, 0, 0); - return TRANS_CONNECT_FAILED; - } - old_namelen = strlen (old_sockname.sun_path) + - sizeof (old_sockname.sun_family); -#endif #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) @@ -2762,8 +2396,9 @@ TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, char *host, char *port) * Done here because otherwise all the strlen() calls above would fail. */ - if (abstract) + if (abstract) { sockname.sun_path[0] = '\0'; + } /* * Do the connect() @@ -2773,26 +2408,19 @@ TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, char *host, char *port) { int olderrno = errno; int connected = 0; - -#if defined(hpux) && defined(X11_t) - if (olderrno == ENOENT) - { - if (connect (ciptr->fd, - (struct sockaddr *) &old_sockname, old_namelen) >= 0) - { - connected = 1; - } - else - olderrno = errno; - } -#endif + if (!connected) { errno = olderrno; - + /* - * If the error was ENOENT, the server may be starting up - * and we should try again. + * If the error was ENOENT, the server may be starting up; we used + * to suggest to try again in this case with + * TRANS_TRY_CONNECT_AGAIN, but this introduced problems for + * processes still referencing stale sockets in their environment. + * Hence, we now return a hard error, TRANS_CONNECT_FAILED, and it + * is suggested that higher level stacks handle retries on their + * level when they face a slow starting server. * * If the error was EWOULDBLOCK or EINPROGRESS then the socket * was non-blocking and we should poll using select @@ -2805,16 +2433,17 @@ TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, char *host, char *port) return TRANS_IN_PROGRESS; else if (olderrno == EINTR) return TRANS_TRY_CONNECT_AGAIN; - else if (olderrno == ENOENT || olderrno == ECONNREFUSED) - /* If opening as abstract failed, try again normally */ + else if (olderrno == ENOENT || olderrno == ECONNREFUSED) { + /* If opening as abstract socket failed, try again normally */ if (abstract) { ciptr->transptr->flags &= ~(TRANS_ABSTRACT); return TRANS_TRY_CONNECT_AGAIN; } else { return TRANS_CONNECT_FAILED; + } } else { - PRMSG (2,"SocketUNIXConnect: Can't connect: errno = %d\n", - EGET(),0, 0); + prmsg (2,"SocketUNIXConnect: Can't connect: errno = %d\n", + EGET()); return TRANS_CONNECT_FAILED; } @@ -2832,12 +2461,11 @@ SocketUNIXConnectPost: * since this is unix domain. */ - if ((ciptr->addr = (char *) malloc(namelen)) == NULL || - (ciptr->peeraddr = (char *) malloc(namelen)) == NULL) + if ((ciptr->addr = malloc(namelen)) == NULL || + (ciptr->peeraddr = malloc(namelen)) == NULL) { - PRMSG (1, - "SocketUNIXCreateListener: Can't allocate space for the addr\n", - 0, 0, 0); + prmsg (1, + "SocketUNIXCreateListener: Can't allocate space for the addr\n"); return TRANS_CONNECT_FAILED; } @@ -2849,7 +2477,7 @@ SocketUNIXConnectPost: ciptr->peeraddrlen = namelen; memcpy (ciptr->addr, &sockname, ciptr->addrlen); memcpy (ciptr->peeraddr, &sockname, ciptr->peeraddrlen); - + return 0; } @@ -2862,7 +2490,7 @@ static int TRANS(SocketBytesReadable) (XtransConnInfo ciptr, BytesReadable_t *pend) { - PRMSG (2,"SocketBytesReadable(%p,%d,%p)\n", + prmsg (2,"SocketBytesReadable(%p,%d,%p)\n", ciptr, ciptr->fd, pend); #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) @@ -2895,7 +2523,7 @@ TRANS(SocketBytesReadable) (XtransConnInfo ciptr, BytesReadable_t *pend) * NX _XSelect(), so it is generally possible to let the client * yield the control to NX and let it handle the I/O on the proxy * descriptors even if the application is not explicitly designed - * to work as a NX agent. + * to work as a NX agent. */ #ifdef NX_TRANS_DEBUG @@ -2916,34 +2544,129 @@ TRANS(SocketBytesReadable) (XtransConnInfo ciptr, BytesReadable_t *pend) #endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ -#if defined(QNX4) - *pend = 0L; /* FIONREAD only returns a short. Zero out upper bits */ -#endif #ifdef WIN32 { int ret = ioctlsocket ((SOCKET) ciptr->fd, FIONREAD, (u_long *) pend); - errno = WSAGetLastError(); + if (ret == SOCKET_ERROR) errno = WSAGetLastError(); return ret; } #else -#if (defined(i386) && defined(SYSV) && !defined(SCO325)) || (defined(_SEQUENT_) && _SOCKET_VERSION == 1) - return ioctl (ciptr->fd, I_NREAD, (char *) pend); -#else -#if defined(__UNIXOS2__) - return ioctl (ciptr->fd, FIONREAD, (char*) pend, sizeof(int)); -#else return ioctl (ciptr->fd, FIONREAD, (char *) pend); -#endif /* __UNIXOS2__ */ -#endif /* i386 && SYSV || _SEQUENT_ && _SOCKET_VERSION == 1 */ #endif /* WIN32 */ } +#if XTRANS_SEND_FDS + +static void +appendFd(struct _XtransConnFd **prev, int fd, int do_close) +{ + struct _XtransConnFd *cf, *new; + + new = malloc (sizeof (struct _XtransConnFd)); + if (!new) { + /* XXX mark connection as broken */ + close(fd); + return; + } + new->next = 0; + new->fd = fd; + new->do_close = do_close; + /* search to end of list */ + for (; (cf = *prev); prev = &(cf->next)); + *prev = new; +} + +static int +removeFd(struct _XtransConnFd **prev) +{ + struct _XtransConnFd *cf; + int fd; + + if ((cf = *prev)) { + *prev = cf->next; + fd = cf->fd; + free(cf); + } else + fd = -1; + return fd; +} + +static void +discardFd(struct _XtransConnFd **prev, struct _XtransConnFd *upto, int do_close) +{ + struct _XtransConnFd *cf, *next; + + for (cf = *prev; cf != upto; cf = next) { + next = cf->next; + if (do_close || cf->do_close) + close(cf->fd); + free(cf); + } + *prev = upto; +} + +static void +cleanupFds(XtransConnInfo ciptr) +{ + /* Clean up the send list but don't close the fds */ + discardFd(&ciptr->send_fds, NULL, 0); + /* Clean up the recv list and *do* close the fds */ + discardFd(&ciptr->recv_fds, NULL, 1); +} + +static int +nFd(struct _XtransConnFd **prev) +{ + struct _XtransConnFd *cf; + int n = 0; + + for (cf = *prev; cf; cf = cf->next) + n++; + return n; +} + +static int +TRANS(SocketRecvFd) (XtransConnInfo ciptr) +{ + prmsg (2, "SocketRecvFd(%d)\n", ciptr->fd); + return removeFd(&ciptr->recv_fds); +} + +static int +TRANS(SocketSendFd) (XtransConnInfo ciptr, int fd, int do_close) +{ + appendFd(&ciptr->send_fds, fd, do_close); + return 0; +} + +static int +TRANS(SocketRecvFdInvalid)(XtransConnInfo ciptr) +{ + errno = EINVAL; + return -1; +} + +static int +TRANS(SocketSendFdInvalid)(XtransConnInfo ciptr, int fd, int do_close) +{ + errno = EINVAL; + return -1; +} + +#define MAX_FDS 128 + +union fd_pass { + struct cmsghdr cmsghdr; + char buf[CMSG_SPACE(MAX_FDS * sizeof(int))]; +}; + +#endif /* XTRANS_SEND_FDS */ static int TRANS(SocketRead) (XtransConnInfo ciptr, char *buf, int size) { - PRMSG (2,"SocketRead(%d,%p,%d)\n", ciptr->fd, buf, size); + prmsg (2,"SocketRead(%d,%p,%d)\n", ciptr->fd, buf, size); #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) @@ -2975,156 +2698,376 @@ TRANS(SocketRead) (XtransConnInfo ciptr, char *buf, int size) } else { + /* FIXME: same code as below, should be possible without duplication */ +#if XTRANS_SEND_FDS + struct iovec iov = { + .iov_base = buf, + .iov_len = size + }; + union fd_pass cmsgbuf; + struct msghdr msg = { + .msg_name = NULL, + .msg_namelen = 0, + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = cmsgbuf.buf, + .msg_controllen = CMSG_LEN(MAX_FDS * sizeof(int)) + }; + + size = recvmsg(ciptr->fd, &msg, 0); + if (size >= 0) { + struct cmsghdr *hdr; + + for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) { + if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) { + int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int); + int i; + int *fd = (int *) CMSG_DATA(hdr); + + for (i = 0; i < nfd; i++) + appendFd(&ciptr->recv_fds, fd[i], 0); + } + } + } + return size; +#else return read (ciptr->fd, buf, size); +#endif /* XTRANS_SEND_FDS */ } #else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ -#if defined(WIN32) || defined(__UNIXOS2__) +#if defined(WIN32) { int ret = recv ((SOCKET)ciptr->fd, buf, size, 0); #ifdef WIN32 - errno = WSAGetLastError(); + if (ret == SOCKET_ERROR) errno = WSAGetLastError(); #endif return ret; } #else +#if XTRANS_SEND_FDS + { + struct iovec iov = { + .iov_base = buf, + .iov_len = size + }; + union fd_pass cmsgbuf; + struct msghdr msg = { + .msg_name = NULL, + .msg_namelen = 0, + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = cmsgbuf.buf, + .msg_controllen = CMSG_LEN(MAX_FDS * sizeof(int)) + }; + + size = recvmsg(ciptr->fd, &msg, 0); + if (size >= 0) { + struct cmsghdr *hdr; + + for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) { + if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) { + int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int); + int i; + int *fd = (int *) CMSG_DATA(hdr); + + for (i = 0; i < nfd; i++) + appendFd(&ciptr->recv_fds, fd[i], 0); + } + } + } + return size; + } +#else return read (ciptr->fd, buf, size); +#endif /* XTRANS_SEND_FDS */ #endif /* WIN32 */ #endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ } - static int -TRANS(SocketWrite) (XtransConnInfo ciptr, char *buf, int size) +TRANS(SocketReadv) (XtransConnInfo ciptr, struct iovec *buf, int size) { - PRMSG (2,"SocketWrite(%d,%p,%d)\n", ciptr->fd, buf, size); + prmsg (2,"SocketReadv(%d,%p,%d)\n", ciptr->fd, buf, size); #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) /* * If we have a valid priv pointer then this * is an internal connection to the proxy. - * In this case we should emulate the write. + * In this case we should emulate the readv. */ if (ciptr->priv) { - int result; - - result = NXTransWrite(ciptr->fd, buf, size); - -#ifdef NX_TRANS_DEBUG - if (result < 0 && EGET() == EAGAIN) - { - fprintf(stderr, "SocketWrite: Write on descriptor [%d] would block.\n", - ciptr->fd); - } - else - { - fprintf(stderr, "SocketWrite: Written [%d] bytes on descriptor [%d].\n", - result, ciptr->fd); - } -#endif - return result; + return NXTransReadVector(ciptr->fd, buf, size); } else { - return write (ciptr->fd, buf, size); + /* FIXME: same code as below, should be possible without duplication */ +#if XTRANS_SEND_FDS + union fd_pass cmsgbuf; + struct msghdr msg = { + .msg_name = NULL, + .msg_namelen = 0, + .msg_iov = buf, + .msg_iovlen = size, + .msg_control = cmsgbuf.buf, + .msg_controllen = CMSG_LEN(MAX_FDS * sizeof(int)) + }; + + size = recvmsg(ciptr->fd, &msg, 0); + if (size >= 0) { + struct cmsghdr *hdr; + + for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) { + if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) { + int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int); + int i; + int *fd = (int *) CMSG_DATA(hdr); + + for (i = 0; i < nfd; i++) + appendFd(&ciptr->recv_fds, fd[i], 0); + } + } + } + return size; +#else + return READV (ciptr, buf, size); +#endif } #else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ -#if defined(WIN32) || defined(__UNIXOS2__) - { - int ret = send ((SOCKET)ciptr->fd, buf, size, 0); -#ifdef WIN32 - errno = WSAGetLastError(); -#endif - return ret; +#if XTRANS_SEND_FDS + { + union fd_pass cmsgbuf; + struct msghdr msg = { + .msg_name = NULL, + .msg_namelen = 0, + .msg_iov = buf, + .msg_iovlen = size, + .msg_control = cmsgbuf.buf, + .msg_controllen = CMSG_LEN(MAX_FDS * sizeof(int)) + }; + + size = recvmsg(ciptr->fd, &msg, 0); + if (size >= 0) { + struct cmsghdr *hdr; + + for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) { + if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) { + int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int); + int i; + int *fd = (int *) CMSG_DATA(hdr); + + for (i = 0; i < nfd; i++) + appendFd(&ciptr->recv_fds, fd[i], 0); + } + } + } + return size; } #else - return write (ciptr->fd, buf, size); -#endif /* WIN32 */ + return READV (ciptr, buf, size); +#endif #endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ } static int -TRANS(SocketReadv) (XtransConnInfo ciptr, struct iovec *buf, int size) +TRANS(SocketWritev) (XtransConnInfo ciptr, struct iovec *buf, int size) { - PRMSG (2,"SocketReadv(%d,%p,%d)\n", ciptr->fd, buf, size); + prmsg (2,"SocketWritev(%d,%p,%d)\n", ciptr->fd, buf, size); #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) /* * If we have a valid priv pointer then this * is an internal connection to the proxy. - * In this case we should emulate the readv. + * In this case we should emulate the writev. */ if (ciptr->priv) { - return NXTransReadVector(ciptr->fd, buf, size); + return NXTransWriteVector(ciptr->fd, buf, size); } else { - return READV (ciptr, buf, size); + /* FIXME: same code as below, should be possible without duplication */ +#if XTRANS_SEND_FDS + if (ciptr->send_fds) + { + union fd_pass cmsgbuf; + int nfd = nFd(&ciptr->send_fds); + struct _XtransConnFd *cf = ciptr->send_fds; + struct msghdr msg = { + .msg_name = NULL, + .msg_namelen = 0, + .msg_iov = buf, + .msg_iovlen = size, + .msg_control = cmsgbuf.buf, + .msg_controllen = CMSG_LEN(nfd * sizeof(int)) + }; + struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); + int i; + int *fds; + + hdr->cmsg_len = msg.msg_controllen; + hdr->cmsg_level = SOL_SOCKET; + hdr->cmsg_type = SCM_RIGHTS; + + fds = (int *) CMSG_DATA(hdr); + /* Set up fds */ + for (i = 0; i < nfd; i++) { + fds[i] = cf->fd; + cf = cf->next; + } + + i = sendmsg(ciptr->fd, &msg, 0); + if (i > 0) + discardFd(&ciptr->send_fds, cf, 0); + return i; + } +#endif + return WRITEV (ciptr, buf, size); } #else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ - return READV (ciptr, buf, size); +#if XTRANS_SEND_FDS + if (ciptr->send_fds) + { + union fd_pass cmsgbuf; + int nfd = nFd(&ciptr->send_fds); + struct _XtransConnFd *cf = ciptr->send_fds; + struct msghdr msg = { + .msg_name = NULL, + .msg_namelen = 0, + .msg_iov = buf, + .msg_iovlen = size, + .msg_control = cmsgbuf.buf, + .msg_controllen = CMSG_LEN(nfd * sizeof(int)) + }; + struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); + int i; + int *fds; + + hdr->cmsg_len = msg.msg_controllen; + hdr->cmsg_level = SOL_SOCKET; + hdr->cmsg_type = SCM_RIGHTS; + + fds = (int *) CMSG_DATA(hdr); + /* Set up fds */ + for (i = 0; i < nfd; i++) { + fds[i] = cf->fd; + cf = cf->next; + } + + i = sendmsg(ciptr->fd, &msg, 0); + if (i > 0) + discardFd(&ciptr->send_fds, cf, 0); + return i; + } +#endif + return WRITEV (ciptr, buf, size); #endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ } static int -TRANS(SocketWritev) (XtransConnInfo ciptr, struct iovec *buf, int size) +TRANS(SocketWrite) (XtransConnInfo ciptr, char *buf, int size) { - PRMSG (2,"SocketWritev(%d,%p,%d)\n", ciptr->fd, buf, size); + prmsg (2,"SocketWrite(%d,%p,%d)\n", ciptr->fd, buf, size); #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) /* * If we have a valid priv pointer then this * is an internal connection to the proxy. - * In this case we should emulate the writev. + * In this case we should emulate the write. */ if (ciptr->priv) { - return NXTransWriteVector(ciptr->fd, buf, size); + int result; + + result = NXTransWrite(ciptr->fd, buf, size); + +#ifdef NX_TRANS_DEBUG + if (result < 0 && EGET() == EAGAIN) + { + fprintf(stderr, "SocketWrite: Write on descriptor [%d] would block.\n", + ciptr->fd); + } + else + { + fprintf(stderr, "SocketWrite: Written [%d] bytes on descriptor [%d].\n", + result, ciptr->fd); + } +#endif + return result; } else { - return WRITEV (ciptr, buf, size); + /* FIXME: same code as below, should be possible without duplication */ +#if XTRANS_SEND_FDS + if (ciptr->send_fds) + { + struct iovec iov; + + iov.iov_base = buf; + iov.iov_len = size; + return TRANS(SocketWritev)(ciptr, &iov, 1); + } +#endif /* XTRANS_SEND_FDS */ + return write (ciptr->fd, buf, size); } #else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ - return WRITEV (ciptr, buf, size); +#if defined(WIN32) + { + int ret = send ((SOCKET)ciptr->fd, buf, size, 0); +#ifdef WIN32 + if (ret == SOCKET_ERROR) errno = WSAGetLastError(); +#endif + return ret; + } +#else +#if XTRANS_SEND_FDS + if (ciptr->send_fds) + { + struct iovec iov; + + iov.iov_base = buf; + iov.iov_len = size; + return TRANS(SocketWritev)(ciptr, &iov, 1); + } +#endif /* XTRANS_SEND_FDS */ + return write (ciptr->fd, buf, size); +#endif /* WIN32 */ #endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ } - static int TRANS(SocketDisconnect) (XtransConnInfo ciptr) { - PRMSG (2,"SocketDisconnect(%p,%d)\n", ciptr, ciptr->fd, 0); + prmsg (2,"SocketDisconnect(%p,%d)\n", ciptr, ciptr->fd); #ifdef WIN32 - { + { int ret = shutdown (ciptr->fd, 2); - errno = WSAGetLastError(); + if (ret == SOCKET_ERROR) errno = WSAGetLastError(); return ret; } #else @@ -3138,12 +3081,12 @@ static int TRANS(SocketINETClose) (XtransConnInfo ciptr) { - PRMSG (2,"SocketINETClose(%p,%d)\n", ciptr, ciptr->fd, 0); + prmsg (2,"SocketINETClose(%p,%d)\n", ciptr, ciptr->fd); #ifdef WIN32 { int ret = close (ciptr->fd); - errno = WSAGetLastError(); + if (ret == SOCKET_ERROR) errno = WSAGetLastError(); return ret; } #else @@ -3157,7 +3100,6 @@ TRANS(SocketINETClose) (XtransConnInfo ciptr) #ifdef UNIXCONN static int TRANS(SocketUNIXClose) (XtransConnInfo ciptr) - { /* * If this is the server side, then once the socket is closed, @@ -3167,7 +3109,7 @@ TRANS(SocketUNIXClose) (XtransConnInfo ciptr) struct sockaddr_un *sockname = (struct sockaddr_un *) ciptr->addr; int ret; - PRMSG (2,"SocketUNIXClose(%x,%d)\n", ciptr, ciptr->fd, 0); + prmsg (2,"SocketUNIXClose(%p,%d)\n", ciptr, ciptr->fd); #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) @@ -3178,6 +3120,9 @@ TRANS(SocketUNIXClose) (XtransConnInfo ciptr) #endif +#if XTRANS_SEND_FDS + cleanupFds(ciptr); +#endif ret = close(ciptr->fd); #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST) @@ -3192,14 +3137,16 @@ TRANS(SocketUNIXClose) (XtransConnInfo ciptr) && sockname->sun_path[0]) { #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST) - if (!(ciptr->flags & TRANS_NOUNLINK)) + if (!(ciptr->flags & TRANS_NOUNLINK + || ciptr->transptr->flags & TRANS_ABSTRACT)) { fprintf(stderr, "SocketUNIXClose: Unlinking path [%s] for ciptr at [%p].\n", sockname->sun_path, (void *) ciptr); unlink (sockname->sun_path); } #else - if (!(ciptr->flags & TRANS_NOUNLINK)) + if (!(ciptr->flags & TRANS_NOUNLINK + || ciptr->transptr->flags & TRANS_ABSTRACT)) unlink (sockname->sun_path); #endif } @@ -3217,8 +3164,8 @@ TRANS(SocketUNIXCloseForCloning) (XtransConnInfo ciptr) int ret; - PRMSG (2,"SocketUNIXCloseForCloning(%p,%d)\n", - ciptr, ciptr->fd, 0); + prmsg (2,"SocketUNIXCloseForCloning(%p,%d)\n", + ciptr, ciptr->fd); #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) @@ -3229,6 +3176,9 @@ TRANS(SocketUNIXCloseForCloning) (XtransConnInfo ciptr) #endif +#if XTRANS_SEND_FDS + cleanupFds(ciptr); +#endif ret = close(ciptr->fd); return ret; @@ -3239,7 +3189,7 @@ TRANS(SocketUNIXCloseForCloning) (XtransConnInfo ciptr) #ifdef TCPCONN # ifdef TRANS_SERVER -static char* tcp_nolisten[] = { +static const char* tcp_nolisten[] = { "inet", #if defined(IPv6) && defined(AF_INET6) "inet6", @@ -3259,15 +3209,8 @@ Xtransport TRANS(SocketTCPFuncs) = { tcp_nolisten, TRANS(SocketOpenCOTSServer), #endif /* TRANS_SERVER */ -#ifdef TRANS_CLIENT - TRANS(SocketOpenCLTSClient), -#endif /* TRANS_CLIENT */ -#ifdef TRANS_SERVER - TRANS(SocketOpenCLTSServer), -#endif /* TRANS_SERVER */ #ifdef TRANS_REOPEN TRANS(SocketReopenCOTSServer), - TRANS(SocketReopenCLTSServer), #endif TRANS(SocketSetOption), #ifdef TRANS_SERVER @@ -3283,6 +3226,10 @@ Xtransport TRANS(SocketTCPFuncs) = { TRANS(SocketWrite), TRANS(SocketReadv), TRANS(SocketWritev), +#if XTRANS_SEND_FDS + TRANS(SocketSendFdInvalid), + TRANS(SocketRecvFdInvalid), +#endif TRANS(SocketDisconnect), TRANS(SocketINETClose), TRANS(SocketINETClose), @@ -3299,15 +3246,8 @@ Xtransport TRANS(SocketINETFuncs) = { NULL, TRANS(SocketOpenCOTSServer), #endif /* TRANS_SERVER */ -#ifdef TRANS_CLIENT - TRANS(SocketOpenCLTSClient), -#endif /* TRANS_CLIENT */ -#ifdef TRANS_SERVER - TRANS(SocketOpenCLTSServer), -#endif /* TRANS_SERVER */ #ifdef TRANS_REOPEN TRANS(SocketReopenCOTSServer), - TRANS(SocketReopenCLTSServer), #endif TRANS(SocketSetOption), #ifdef TRANS_SERVER @@ -3323,6 +3263,10 @@ Xtransport TRANS(SocketINETFuncs) = { TRANS(SocketWrite), TRANS(SocketReadv), TRANS(SocketWritev), +#if XTRANS_SEND_FDS + TRANS(SocketSendFdInvalid), + TRANS(SocketRecvFdInvalid), +#endif TRANS(SocketDisconnect), TRANS(SocketINETClose), TRANS(SocketINETClose), @@ -3340,15 +3284,8 @@ Xtransport TRANS(SocketINET6Funcs) = { NULL, TRANS(SocketOpenCOTSServer), #endif /* TRANS_SERVER */ -#ifdef TRANS_CLIENT - TRANS(SocketOpenCLTSClient), -#endif /* TRANS_CLIENT */ -#ifdef TRANS_SERVER - TRANS(SocketOpenCLTSServer), -#endif /* TRANS_SERVER */ #ifdef TRANS_REOPEN TRANS(SocketReopenCOTSServer), - TRANS(SocketReopenCLTSServer), #endif TRANS(SocketSetOption), #ifdef TRANS_SERVER @@ -3364,6 +3301,10 @@ Xtransport TRANS(SocketINET6Funcs) = { TRANS(SocketWrite), TRANS(SocketReadv), TRANS(SocketWritev), +#if XTRANS_SEND_FDS + TRANS(SocketSendFdInvalid), + TRANS(SocketRecvFdInvalid), +#endif TRANS(SocketDisconnect), TRANS(SocketINETClose), TRANS(SocketINETClose), @@ -3388,15 +3329,8 @@ Xtransport TRANS(SocketLocalFuncs) = { NULL, TRANS(SocketOpenCOTSServer), #endif /* TRANS_SERVER */ -#ifdef TRANS_CLIENT - TRANS(SocketOpenCLTSClient), -#endif /* TRANS_CLIENT */ -#ifdef TRANS_SERVER - TRANS(SocketOpenCLTSServer), -#endif /* TRANS_SERVER */ #ifdef TRANS_REOPEN TRANS(SocketReopenCOTSServer), - TRANS(SocketReopenCLTSServer), #endif TRANS(SocketSetOption), #ifdef TRANS_SERVER @@ -3412,6 +3346,10 @@ Xtransport TRANS(SocketLocalFuncs) = { TRANS(SocketWrite), TRANS(SocketReadv), TRANS(SocketWritev), +#if XTRANS_SEND_FDS + TRANS(SocketSendFd), + TRANS(SocketRecvFd), +#endif TRANS(SocketDisconnect), TRANS(SocketUNIXClose), TRANS(SocketUNIXCloseForCloning), @@ -3419,10 +3357,10 @@ Xtransport TRANS(SocketLocalFuncs) = { #endif /* !LOCALCONN */ # ifdef TRANS_SERVER # if !defined(LOCALCONN) -static char* unix_nolisten[] = { "local" , NULL }; +static const char* unix_nolisten[] = { "local" , NULL }; # endif # endif - + Xtransport TRANS(SocketUNIXFuncs) = { /* Socket Interface */ "unix", @@ -3442,15 +3380,8 @@ Xtransport TRANS(SocketUNIXFuncs) = { #endif TRANS(SocketOpenCOTSServer), #endif /* TRANS_SERVER */ -#ifdef TRANS_CLIENT - TRANS(SocketOpenCLTSClient), -#endif /* TRANS_CLIENT */ -#ifdef TRANS_SERVER - TRANS(SocketOpenCLTSServer), -#endif /* TRANS_SERVER */ #ifdef TRANS_REOPEN TRANS(SocketReopenCOTSServer), - TRANS(SocketReopenCLTSServer), #endif TRANS(SocketSetOption), #ifdef TRANS_SERVER @@ -3466,9 +3397,158 @@ Xtransport TRANS(SocketUNIXFuncs) = { TRANS(SocketWrite), TRANS(SocketReadv), TRANS(SocketWritev), +#if XTRANS_SEND_FDS + TRANS(SocketSendFd), + TRANS(SocketRecvFd), +#endif TRANS(SocketDisconnect), TRANS(SocketUNIXClose), TRANS(SocketUNIXCloseForCloning), }; #endif /* UNIXCONN */ + +#ifdef NX_TRANS_SOCKET +/* + * Override the UNIX_DIR and UNIX_PATH settings and + * make them configurable, based on the NX_TEMP or + * the TEMP environment. + * + * We must be careful as the same defines are used + * for different directories, based on the subsystem + * that is compiling this, while we want to override + * only the '/tmp/.X11-unix' and '/tmp/.X11-unix/X' + * settings. + */ + +static char _NXUnixDir[1024]; +static char _NXUnixPath[1024]; + +static char *_NXGetUnixDir(char *dir) +{ + const char *tempDir; + + prmsg (3, "_NXGetUnixDir(%s)\n", dir); + + if (strcmp(dir, UNIX_DIR) != 0) + { +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixDir: Returning other Unix directory [%s].\n", dir); +#endif + return dir; + } + + /* + * Check the environment only once. + */ + + if (*_NXUnixDir != '\0') + { + return _NXUnixDir; + } + +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixDir: Trying with the NX_TEMP environment.\n"); +#endif + + tempDir = getenv("NX_TEMP"); + + if (tempDir == NULL || *tempDir == '\0') + { +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixDir: Trying with the TEMP environment.\n"); +#endif + + tempDir = getenv("TEMP"); + } + + if (tempDir != NULL && *tempDir != '\0') + { + if (strlen(tempDir) + strlen("/.X11-unix") + 1 > 1024) + { +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixDir: WARNING! Maximum length of X11 Unix directory exceeded.\n"); +#endif + goto _NXGetUnixDirError; + } + + strcpy(_NXUnixDir, tempDir); + strcat(_NXUnixDir, "/.X11-unix"); + +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixDir: Using X11 Unix directory [%s].\n", _NXUnixDir); +#endif + + return _NXUnixDir; + } + +_NXGetUnixDirError: + + strcpy(_NXUnixDir, dir); + +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixDir: Returning default X11 Unix directory [%s].\n", _NXUnixDir); +#endif + + return _NXUnixDir; +} + +static char *_NXGetUnixPath(char *path) +{ + const char *unixDir; + + prmsg (3, "_NXGetUnixPath(%s)\n", path); + + if (strcmp(path, UNIX_PATH) != 0) + { +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixPath: Returning other X11 Unix path [%s].\n", path); +#endif + return path; + } + + /* + * Check the environment only once. + */ + + if (*_NXUnixPath != '\0') + { + return _NXUnixPath; + } + + unixDir = _NXGetUnixDir(UNIX_DIR); + +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixPath: Got X11 Unix directory [%s].\n", unixDir); +#endif + + if (strlen(unixDir) + strlen("/X") + 1 > 1024) + { +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixPath: WARNING! Maximum length of X11 Unix path exceeded.\n"); +#endif + + goto _NXGetUnixPathError; + } + + strcpy(_NXUnixPath, unixDir); + strcat(_NXUnixPath, "/X"); + +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixPath: Returning X11 Unix path [%s].\n", _NXUnixPath); +#endif + + return _NXUnixPath; + +_NXGetUnixPathError: + + strcpy(_NXUnixPath, path); + +#ifdef NX_TRANS_TEST + fprintf(stderr, "_NXGetUnixPath: Returning default X11 Unix path [%s].\n", _NXUnixPath); +#endif + + return _NXUnixPath; +} + +#endif /* NX_TRANS_SOCKET */ |