?? xtranssock.c
字號:
/* $XConsortium: Xtranssock.c /main/58 1996/12/04 10:22:50 lehors $ *//* $XFree86: xc/lib/xtrans/Xtranssock.c,v 3.25.2.4 1998/02/01 16:04:34 robin Exp $ *//*Copyright (c) 1993, 1994 X ConsortiumPermission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:The above copyright notice and this permission notice shall be includedin all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OROTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of the X Consortium shallnot be used in advertising or otherwise to promote the sale, use orother dealings in this Software without prior written authorizationfrom the X Consortium.*//* Copyright (c) 1993, 1994 NCR Corporation - Dayton, Ohio, USA * * All Rights Reserved * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose and without fee is hereby granted, provided * that the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name NCR not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. NCR makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * * NCR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN * NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */#include <ctype.h>#ifdef XTHREADS#include <X11/Xthreads.h>#endif#ifndef WIN32#if defined(TCPCONN) || defined(UNIXCONN)#include <netinet/in.h>#else#ifdef ESIX#include <lan/in.h>#endif#endif#if defined(TCPCONN) || defined(UNIXCONN)#define X_INCLUDE_NETDB_H#define XOS_USE_NO_LOCKING#include <X11/Xos_r.h>#endif#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(__EMX__)#define NO_TCP_H#endif /* hpux */#ifdef MOTOROLA#ifdef SYSV#define NO_TCP_H#endif /* SYSV */#endif /* MOTOROLA */#ifndef NO_TCP_H#ifdef __osf__#include <sys/param.h>#endif /* osf */#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) #include <machine/endian.h>#endif /* __NetBSD__ || __OpenBSD__ || __FreeBSD__ */#include <netinet/tcp.h>#endif /* !NO_TCP_H */#include <sys/ioctl.h>#if defined(SVR4) && !defined(SCO325)#include <sys/filio.h>#endif#if (defined(i386) && defined(SYSV)) || defined(_SEQUENT_)#if !defined(_SEQUENT_) && !defined(ESIX) && !defined(sco)#include <net/errno.h>#endif /* _SEQUENT_ || ESIX || SCO */#if !defined(ISC) || !defined(I_NREAD) || defined(SCO325)#include <sys/stropts.h>#endif#endif /* i386 && SYSV || _SEQUENT_ */#else /* !WIN32 */#include <X11/Xwinsock.h>#include <X11/Xw32defs.h>#undef close#define close closesocket#define ECONNREFUSED WSAECONNREFUSED#define EADDRINUSE WSAEADDRINUSE#define EPROTOTYPE WSAEPROTOTYPE#undef EWOULDBLOCK#define EWOULDBLOCK WSAEWOULDBLOCK#undef EINTR#define EINTR WSAEINTR#define X_INCLUDE_NETDB_H#define XOS_USE_MTSAFE_NETDBAPI#include <X11/Xos_r.h>#endif /* WIN32 */#if defined(SO_DONTLINGER) && defined(SO_LINGER)#undef SO_DONTLINGER#endif#if defined(__EMX__)#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#define MIN_BACKLOG 128#ifdef SOMAXCONN#if SOMAXCONN > MIN_BACKLOG#define BACKLOG SOMAXCONN#endif#endif#ifndef BACKLOG#define BACKLOG MIN_BACKLOG#endif/* * This is the Socket implementation of the X Transport service layer * * This file contains the implementation for both the UNIX and INET domains, * and can be built for either one, or both. * */typedef struct _Sockettrans2dev { char *transname; int family; int devcotsname; int devcltsname; int protocol;} Sockettrans2dev;static Sockettrans2dev Sockettrans2devtab[] = {#ifdef TCPCONN {"inet",AF_INET,SOCK_STREAM,SOCK_DGRAM,0}, {"tcp",AF_INET,SOCK_STREAM,SOCK_DGRAM,0},#endif /* TCPCONN */#ifdef UNIXCONN {"unix",AF_UNIX,SOCK_STREAM,SOCK_DGRAM,0},#if !defined(LOCALCONN) {"local",AF_UNIX,SOCK_STREAM,SOCK_DGRAM,0},#endif /* !LOCALCONN */#endif /* UNIXCONN */};#define NUMSOCKETFAMILIES (sizeof(Sockettrans2devtab)/sizeof(Sockettrans2dev))#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#else /* !hpux */#if defined(X11_t)#define UNIX_PATH "/tmp/.X11-unix/X"#define UNIX_DIR "/tmp/.X11-unix"#endif /* X11_t */#if defined(XIM_t)#define UNIX_PATH "/tmp/.XIM-unix/XIM"#define UNIX_DIR "/tmp/.XIM-unix"#endif /* XIM_t */#if defined(FS_t) || defined(FONT_t)#define UNIX_PATH "/tmp/.font-unix/fs"#define UNIX_DIR "/tmp/.font-unix"#endif /* FS_t || FONT_t */#if defined(ICE_t)#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#endif /* hpux */#endif /* UNIXCONN *//* * These are some utility function used by the real interface function below. */static intTRANS(SocketSelectFamily) (family)char *family;{ int i; PRMSG (3,"SocketSelectFamily(%s)\n", family, 0, 0); for (i = 0; i < NUMSOCKETFAMILIES;i++) { if (!strcmp (family, Sockettrans2devtab[i].transname)) return i; } return -1;}/* * This function gets the local address of the socket and stores it in the * XtransConnInfo structure for the connection. */static intTRANS(SocketINETGetAddr) (ciptr)XtransConnInfo ciptr;{ struct sockaddr_in sockname;#if defined(SVR4) || defined(SCO325) size_t namelen = sizeof sockname;#else int namelen = sizeof sockname;#endif PRMSG (3,"SocketINETGetAddr(%x)\n", ciptr, 0, 0); if (getsockname (ciptr->fd,(struct sockaddr *) &sockname, &namelen) < 0) { PRMSG (1,"SocketINETGetAddr: getsockname() failed: %d\n", EGET(),0, 0); return -1; } /* * Everything looks good: fill in the XtransConnInfo structure. */ if ((ciptr->addr = (char *) xalloc (namelen)) == NULL) { PRMSG (1, "SocketINETGetAddr: Can't allocate space for the addr\n", 0, 0, 0); return -1; } ciptr->family = sockname.sin_family; ciptr->addrlen = namelen; memcpy (ciptr->addr, &sockname, ciptr->addrlen); return 0;}/* * This function gets the remote address of the socket and stores it in the * XtransConnInfo structure for the connection. */static intTRANS(SocketINETGetPeerAddr) (ciptr)XtransConnInfo ciptr;{ struct sockaddr_in sockname;#if defined(SVR4) || defined(SCO325) size_t namelen = sizeof sockname;#else int namelen = sizeof sockname;#endif PRMSG (3,"SocketINETGetPeerAddr(%x)\n", ciptr, 0, 0); if (getpeername (ciptr->fd, (struct sockaddr *) &sockname, &namelen) < 0) { PRMSG (1,"SocketINETGetPeerAddr: getpeername() failed: %d\n", EGET(), 0, 0); return -1; } /* * Everything looks good: fill in the XtransConnInfo structure. */ if ((ciptr->peeraddr = (char *) xalloc (namelen)) == NULL) { PRMSG (1, "SocketINETGetPeerAddr: Can't allocate space for the addr\n", 0, 0, 0); return -1; } ciptr->peeraddrlen = namelen; memcpy (ciptr->peeraddr, &sockname, ciptr->peeraddrlen); return 0;}static XtransConnInfoTRANS(SocketOpen) (i, type)int i;int type;{ XtransConnInfo ciptr; PRMSG (3,"SocketOpen(%d,%d)\n", i, type, 0); if ((ciptr = (XtransConnInfo) xcalloc ( 1, sizeof(struct _XtransConnInfo))) == NULL) { PRMSG (1, "SocketOpen: malloc failed\n", 0, 0, 0); return NULL; } if ((ciptr->fd = socket(Sockettrans2devtab[i].family, 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#endif#endif ) { PRMSG (1, "SocketOpen: socket() failed for %s\n", Sockettrans2devtab[i].transname, 0, 0); xfree ((char *) ciptr); return NULL; }#ifdef TCP_NODELAY if (Sockettrans2devtab[i].family == AF_INET) { /* * turn off TCP coalescence for INET sockets */ int tmp = 1; setsockopt (ciptr->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &tmp, sizeof (int)); }#endif return ciptr;}#ifdef TRANS_REOPENstatic XtransConnInfoTRANS(SocketReopen) (i, type, fd, port)int i;int type;int fd;char *port;{ XtransConnInfo ciptr; PRMSG (3,"SocketReopen(%d,%d,%s)\n", type, fd, port); if ((ciptr = (XtransConnInfo) xcalloc ( 1, sizeof(struct _XtransConnInfo))) == NULL) { PRMSG (1, "SocketReopen: malloc failed\n", 0, 0, 0); return NULL; } ciptr->fd = fd; return ciptr;}#endif /* TRANS_REOPEN *//* * These functions are the interface supplied in the Xtransport structure */#ifdef TRANS_CLIENTstatic XtransConnInfoTRANS(SocketOpenCOTSClient) (thistrans, protocol, host, port)Xtransport *thistrans;char *protocol;char *host;char *port;{ XtransConnInfo ciptr; int i; PRMSG (2, "SocketOpenCOTSClient(%s,%s,%s)\n", protocol, host, port); SocketInitOnce(); if ((i = TRANS(SocketSelectFamily) (thistrans->TransName)) < 0) { PRMSG (1, "SocketOpenCOTSClient: Unable to determine socket type for %s\n", thistrans->TransName, 0, 0); return NULL; } if ((ciptr = TRANS(SocketOpen) ( i, Sockettrans2devtab[i].devcotsname)) == NULL) { PRMSG (1,"SocketOpenCOTSClient: Unable to open socket for %s\n", thistrans->TransName, 0, 0); return NULL; } /* Save the index for later use */ ciptr->index = i; return ciptr;}#endif /* TRANS_CLIENT */#ifdef TRANS_SERVERstatic XtransConnInfoTRANS(SocketOpenCOTSServer) (thistrans, protocol, host, port)Xtransport *thistrans;char *protocol;char *host;char *port;{ XtransConnInfo ciptr; int i; PRMSG (2,"SocketOpenCOTSServer(%s,%s,%s)\n", protocol, host, port); SocketInitOnce(); if ((i = TRANS(SocketSelectFamily) (thistrans->TransName)) < 0) { PRMSG (1, "SocketOpenCOTSServer: Unable to determine socket type for %s\n", thistrans->TransName, 0, 0); return NULL; } if ((ciptr = TRANS(SocketOpen) ( i, Sockettrans2devtab[i].devcotsname)) == NULL) { PRMSG (1,"SocketOpenCOTSServer: Unable to open socket for %s\n", thistrans->TransName, 0, 0); return NULL; } /* * Using this prevents the bind() check for an existing server listening * on the same port, but it is required for other reasons. */#ifdef SO_REUSEADDR /* * SO_REUSEADDR only applied to AF_INET */ if (Sockettrans2devtab[i].family == AF_INET) { int one = 1; setsockopt (ciptr->fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (int)); }#endif /* Save the index for later use */ ciptr->index = i; return ciptr;}#endif /* TRANS_SERVER */#ifdef TRANS_CLIENTstatic XtransConnInfoTRANS(SocketOpenCLTSClient) (thistrans, protocol, host, port)Xtransport *thistrans;char *protocol;char *host;char *port;{ XtransConnInfo ciptr; int i; PRMSG (2,"SocketOpenCLTSClient(%s,%s,%s)\n", protocol, host, port); SocketInitOnce(); if ((i = TRANS(SocketSelectFamily) (thistrans->TransName)) < 0) { PRMSG (1, "SocketOpenCLTSClient: Unable to determine socket type for %s\n", thistrans->TransName, 0, 0); return NULL; } if ((ciptr = TRANS(SocketOpen) ( i, Sockettrans2devtab[i].devcotsname)) == NULL) { PRMSG (1,"SocketOpenCLTSClient: Unable to open socket for %s\n", thistrans->TransName, 0, 0); return NULL; } /* Save the index for later use */ ciptr->index = i; return ciptr;}#endif /* TRANS_CLIENT */#ifdef TRANS_SERVERstatic XtransConnInfoTRANS(SocketOpenCLTSServer) (thistrans, protocol, host, port)Xtransport *thistrans;char *protocol;char *host;char *port;{ XtransConnInfo ciptr; int i; PRMSG (2,"SocketOpenCLTSServer(%s,%s,%s)\n", protocol, host, port); SocketInitOnce(); if ((i = TRANS(SocketSelectFamily) (thistrans->TransName)) < 0) { PRMSG (1, "SocketOpenCLTSServer: Unable to determine socket type for %s\n", thistrans->TransName, 0, 0); return NULL; } if ((ciptr = TRANS(SocketOpen) ( i, Sockettrans2devtab[i].devcotsname)) == NULL) { PRMSG (1,"SocketOpenCLTSServer: Unable to open socket for %s\n", thistrans->TransName, 0, 0); return NULL; } /* Save the index for later use */ ciptr->index = i; return ciptr;}#endif /* TRANS_SERVER */#ifdef TRANS_REOPENstatic XtransConnInfoTRANS(SocketReopenCOTSServer) (thistrans, fd, port)Xtransport *thistrans;int fd;char *port;{ XtransConnInfo ciptr; int i; PRMSG (2,
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -