?? xtranstli.c
字號(hào):
/* $XConsortium: Xtranstli.c /main/26 1995/12/13 18:07:13 kaleb $ *//* $XFree86: xc/lib/xtrans/Xtranstli.c,v 3.5 1996/09/01 04:14:14 dawes 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 <sys/un.h>#include <stropts.h>#include <poll.h>#include <tiuser.h>#include <netdir.h>#include <netconfig.h>/* * This is the TLI implementation of the X Transport service layer */typedef struct _TLItrans2dev { char *transname; char *protofamily; char *devcotsname; char *devcltsname; int family;} TLItrans2dev;static TLItrans2dev TLItrans2devtab[] = { {"inet","inet","/dev/tcp","/dev/udp",AF_INET}, {"tcp","inet","/dev/tcp","/dev/udp",AF_INET}, {"tli","loopback","/dev/ticots","/dev/ticlts",AF_UNIX},};#define NUMTLIFAMILIES (sizeof(TLItrans2devtab)/sizeof(TLItrans2dev)) /* * The local TLI connection, is a form of a local connection, so use a * sockaddr_un for the address so that it will be treated just like the other * local transports such as UNIX domain sockets, pts, and named. */ #if defined(X11_t)#define TLINODENAME "TLI:xserver"#endif #if defined(XIM_t)#define TLINODENAME "TLI:xim"#endif #if defined(FS_t) || defined(FONT_t)#define TLINODENAME "TLI:fontserver"#endif #if defined(ICE_t)#define TLINODENAME "TLI:ICE"#endif #if defined(TEST_t)#define TLINODENAME "TLI:test"#endif /* * These are some utility function used by the real interface function below. */static intTRANS(TLISelectFamily)(family)char *family;{ int i; PRMSG(3,"TLISelectFamily(%s)\n", family, 0,0 ); for(i=0;i<NUMTLIFAMILIES;i++) { if( !strcmp(family,TLItrans2devtab[i].transname) ) return i; } return -1;}/* * This function gets the local address of the transport and stores it in the * XtransConnInfo structure for the connection. */static intTRANS(TLIGetAddr)(ciptr)XtransConnInfo ciptr;{ Xtransaddr sockname; struct netbuf netbuf; PRMSG(3,"TLIGetAddr(%x)\n", ciptr, 0,0 ); netbuf.buf=(char *)&sockname; netbuf.len=sizeof(sockname); netbuf.maxlen=sizeof(sockname); if( t_getname(ciptr->fd,&netbuf,LOCALNAME) < 0 ) { PRMSG(1,"TLIGetAddr: t_getname(LOCALNAME) failed: %d\n", errno, 0,0 ); return -1; } PRMSG(4,"TLIGetAddr: got family %d len %d\n", ((struct sockaddr *) &sockname)->sa_family ,netbuf.len, 0 ); /* * Everything looks good: fill in the XtransConnInfo structure. */ if( ciptr->addr ) xfree(ciptr->addr); if( (ciptr->addr=(char *)xalloc(netbuf.len)) == NULL ) { PRMSG(1, "TLIGetAddr: Can't allocate space for the addr\n", 0,0,0); return -1; } ciptr->family=((struct sockaddr *) &sockname)->sa_family; ciptr->addrlen=netbuf.len; 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(TLIGetPeerAddr)(ciptr)XtransConnInfo ciptr;{ Xtransaddr sockname; struct netbuf netbuf; PRMSG(3,"TLIGetPeerAddr(%x)\n", ciptr, 0,0 ); netbuf.buf=(char *)&sockname; netbuf.len=sizeof(sockname); netbuf.maxlen=sizeof(sockname); if( t_getname(ciptr->fd,&netbuf,REMOTENAME) < 0 ) { PRMSG(1,"TLIGetPeerAddr: t_getname(REMOTENAME) failed: %d\n", errno, 0,0 ); return -1; } PRMSG(4,"TLIGetPeerAddr: got family %d len %d\n", ((struct sockaddr *) &sockname)->sa_family ,netbuf.len, 0 ); /* * Everything looks good: fill in the XtransConnInfo structure. */ if( ciptr->peeraddr ) xfree(ciptr->peeraddr); if( (ciptr->peeraddr=(char *)xalloc(netbuf.len)) == NULL ) { PRMSG(1, "TLIGetPeerAddr: Can't allocate space for the addr\n", 0,0,0); return -1; } ciptr->peeraddrlen=netbuf.len; memcpy(ciptr->peeraddr,&sockname,ciptr->peeraddrlen); return 0;}/* * This function will establish a local name for the transport. This function * do extra work for the local tli connection. It must create a sockaddr_un * format address so that it will look like an AF_UNIX connection to the * higher layer. * * This function will only be called by the OPENC?TSClient() functions since * the local address is set up in the CreateListner() for the server ends. */static intTRANS(TLITLIBindLocal)(fd,family,port)int fd;int family;char *port;{ struct sockaddr_un *sunaddr; struct t_bind *req=NULL; PRMSG(2, "TLITLIBindLocal(%d,%d,%s)\n", fd, family, port); if( family == AF_UNIX ) { if( (req=(struct t_bind *)t_alloc(fd,T_BIND,0)) == NULL ) { PRMSG(1, "TLITLIBindLocal() failed to allocate a t_bind\n", 0,0,0 ); return -1; } if( (sunaddr=(struct sockaddr_un *) malloc(sizeof(struct sockaddr_un))) == NULL ) { PRMSG(1, "TLITLIBindLocal: failed to allocate a sockaddr_un\n", 0,0,0 ); t_free((char *)req,T_BIND); return -1; } sunaddr->sun_family=AF_UNIX; #ifdef nuke if( *port == '/' ) { /* A full pathname */ (void) strcpy(sunaddr->sun_path, port); } else { (void) sprintf(sunaddr->sun_path,"%s%s", TLINODENAME, port ); }#endif /*NUKE*/ (void) sprintf(sunaddr->sun_path,"%s%d", TLINODENAME, getpid()^time(NULL) ); PRMSG(4, "TLITLIBindLocal: binding to %s\n", sunaddr->sun_path, 0,0); req->addr.buf=(char *)sunaddr; req->addr.len=sizeof(*sunaddr); req->addr.maxlen=sizeof(*sunaddr); } if( t_bind(fd, req, NULL) < 0 ) { PRMSG(1, "TLIBindLocal: Unable to bind TLI device to %s\n", port, 0,0 ); return -1; } return 0;}static XtransConnInfoTRANS(TLIOpen)(device)char *device;{ XtransConnInfo ciptr; PRMSG(3,"TLIOpen(%s)\n", device, 0,0 ); if( (ciptr=(XtransConnInfo)xcalloc(1,sizeof(struct _XtransConnInfo))) == NULL ) { PRMSG(1, "TLIOpen: calloc failed\n", 0,0,0 ); return NULL; } if( (ciptr->fd=t_open( device, O_RDWR, NULL )) < 0 ) { PRMSG(1, "TLIOpen: t_open failed for %s\n", device, 0,0 ); return NULL; } return ciptr;}#ifdef TRANS_REOPENstatic XtransConnInfoTRANS(TLIReopen)(device, fd, port)char *device;int fd;char *port;{ XtransConnInfo ciptr; PRMSG(3,"TLIReopen(%s,%d, %s)\n", device, fd, port ); if (t_sync (fd) < 0) { PRMSG(1, "TLIReopen: t_sync failed\n", 0,0,0 ); return NULL; } if( (ciptr=(XtransConnInfo)xcalloc(1,sizeof(struct _XtransConnInfo))) == NULL ) { PRMSG(1, "TLIReopen: calloc failed\n", 0,0,0 ); return NULL; } ciptr->fd = fd; return ciptr;}#endif /* TRANS_REOPEN */static intTRANS(TLIAddrToNetbuf)(tlifamily, host, port, netbufp)int tlifamily;char *host;char *port;struct netbuf *netbufp;{ struct netconfig *netconfigp; struct nd_hostserv nd_hostserv; struct nd_addrlist *nd_addrlistp = NULL; void *handlep; PRMSG(3,"TLIAddrToNetbuf(%d,%s,%s)\n", tlifamily, host, port ); if( (handlep=setnetconfig()) == NULL ) return -1; nd_hostserv.h_host = host; if( port && *port ) { nd_hostserv.h_serv = port; } else { nd_hostserv.h_serv = NULL; } while( (netconfigp=getnetconfig(handlep)) != NULL ) { if( strcmp(netconfigp->nc_protofmly, TLItrans2devtab[tlifamily].protofamily) != 0 ) continue; PRMSG(5,"TLIAddrToNetbuf: Trying to resolve %s.%s for %s\n", host, port, TLItrans2devtab[tlifamily].protofamily ); if( netdir_getbyname(netconfigp,&nd_hostserv, &nd_addrlistp) == 0 ) { /* we have at least one address to use */ PRMSG(5, "TLIAddrToNetbuf: found address for %s.%s\n", host, port, 0 ); PRMSG(5, "TLIAddrToNetbuf: %s\n",taddr2uaddr(netconfigp,nd_addrlistp->n_addrs), 0,0 ); memcpy(netbufp->buf,nd_addrlistp->n_addrs->buf, nd_addrlistp->n_addrs->len); netbufp->len=nd_addrlistp->n_addrs->len; endnetconfig(handlep); return 0; } } endnetconfig(handlep); return -1;}/* * These functions are the interface supplied in the Xtransport structure */#ifdef TRANS_CLIENTstatic XtransConnInfoTRANS(TLIOpenCOTSClient)(thistrans, protocol, host, port)Xtransport *thistrans;char *protocol;char *host;char *port;{ XtransConnInfo ciptr; int i; PRMSG(2,"TLIOpenCOTSClient(%s,%s,%s)\n", protocol, host, port ); if( (i=TRANS(TLISelectFamily)(thistrans->TransName)) < 0 ) { PRMSG(1,"TLIOpenCOTSClient: Unable to determine device for %s\n", thistrans->TransName, 0,0 ); return NULL; } if( (ciptr=TRANS(TLIOpen)(TLItrans2devtab[i].devcotsname)) == NULL ) { PRMSG(1,"TLIOpenCOTSClient: Unable to open device for %s\n", thistrans->TransName, 0,0 ); return NULL; } if( TRANS(TLITLIBindLocal)(ciptr->fd,TLItrans2devtab[i].family,port) < 0 ) { PRMSG(1, "TLIOpenCOTSClient: ...TLITLIBindLocal() failed: %d\n", errno, 0,0 ); t_close(ciptr->fd); xfree(ciptr); return NULL; } if( TRANS(TLIGetAddr)(ciptr) < 0 ) { PRMSG(1, "TLIOpenCOTSClient: ...TLIGetAddr() failed: %d\n", errno, 0,0 ); t_close(ciptr->fd); xfree(ciptr); return NULL; } /* Save the TLIFamily for later use in TLIAddrToNetbuf() lookups */ ciptr->index = i; return ciptr;}#endif /* TRANS_CLIENT */#ifdef TRANS_SERVERstatic XtransConnInfo
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -