?? xtrans.c
字號:
/* $XConsortium: Xtrans.c,v 1.31 95/03/28 19:49:02 mor Exp $ *//* $XFree86: xc/lib/xtrans/Xtrans.c,v 3.15.2.2 1997/07/19 04:59:16 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 <ctype.h>/* * The transport table contains a definition for every transport (protocol) * family. All operations that can be made on the transport go through this * table. * * Each transport is assigned a unique transport id. * * New transports can be added by adding an entry in this table. * For compatiblity, the transport ids should never be renumbered. * Always add to the end of the list. */#define TRANS_TLI_INET_INDEX 1#define TRANS_TLI_TCP_INDEX 2#define TRANS_TLI_TLI_INDEX 3#define TRANS_SOCKET_UNIX_INDEX 4#define TRANS_SOCKET_LOCAL_INDEX 5#define TRANS_SOCKET_INET_INDEX 6#define TRANS_SOCKET_TCP_INDEX 7#define TRANS_DNET_INDEX 8#define TRANS_LOCAL_LOCAL_INDEX 9#define TRANS_LOCAL_PTS_INDEX 10#define TRANS_LOCAL_NAMED_INDEX 11#define TRANS_LOCAL_ISC_INDEX 12#define TRANS_LOCAL_SCO_INDEX 13#define TRANS_AMOEBA_INDEX 14#define TRANS_MNX_INET_INDEX 15#define TRANS_MNX_TCP_INDEX 16staticXtransport_table Xtransports[] = {#if defined(STREAMSCONN) &TRANS(TLITCPFuncs), TRANS_TLI_TCP_INDEX, &TRANS(TLIINETFuncs), TRANS_TLI_INET_INDEX, &TRANS(TLITLIFuncs), TRANS_TLI_TLI_INDEX,#endif /* STREAMSCONN */#if defined(TCPCONN) &TRANS(SocketTCPFuncs), TRANS_SOCKET_TCP_INDEX, &TRANS(SocketINETFuncs), TRANS_SOCKET_INET_INDEX,#endif /* TCPCONN */#if defined(DNETCONN) &TRANS(DNETFuncs), TRANS_DNET_INDEX,#endif /* DNETCONN */#if defined(UNIXCONN)#if !defined(LOCALCONN) &TRANS(SocketLocalFuncs), TRANS_SOCKET_LOCAL_INDEX,#endif /* !LOCALCONN */ &TRANS(SocketUNIXFuncs), TRANS_SOCKET_UNIX_INDEX,#endif /* UNIXCONN */#if defined(OS2PIPECONN) &TRANS(OS2LocalFuncs), TRANS_LOCAL_LOCAL_INDEX,#endif /* OS2PIPECONN */#if defined(LOCALCONN) &TRANS(LocalFuncs), TRANS_LOCAL_LOCAL_INDEX, &TRANS(PTSFuncs), TRANS_LOCAL_PTS_INDEX,#ifdef SVR4 &TRANS(NAMEDFuncs), TRANS_LOCAL_NAMED_INDEX,#endif &TRANS(ISCFuncs), TRANS_LOCAL_ISC_INDEX, &TRANS(SCOFuncs), TRANS_LOCAL_SCO_INDEX,#endif /* LOCALCONN */#if defined(AMRPCCONN) || defined(AMTCPCONN) &TRANS(AmConnFuncs), TRANS_AMOEBA_INDEX,#endif /* AMRPCCONN || AMTCPCONN */#if defined(MNX_TCPCONN) &TRANS(MnxINETFuncs), TRANS_MNX_INET_INDEX, &TRANS(MnxTCPFuncs), TRANS_MNX_TCP_INDEX,#endif /* MNX_TCPCONN */};#define NUMTRANS (sizeof(Xtransports)/sizeof(Xtransport_table))#ifdef WIN32#define ioctl ioctlsocket#endif/* * These are a few utility function used by the public interface functions. */voidTRANS(FreeConnInfo) (ciptr)XtransConnInfo ciptr;{ PRMSG (3,"FreeConnInfo(%x)\n", ciptr, 0, 0); if (ciptr->addr) xfree (ciptr->addr); if (ciptr->peeraddr) xfree (ciptr->peeraddr); if (ciptr->port) xfree (ciptr->port); xfree ((char *) ciptr);}#define PROTOBUFSIZE 20static Xtransport *TRANS(SelectTransport) (protocol)char *protocol;{ char protobuf[PROTOBUFSIZE]; int i; PRMSG (3,"SelectTransport(%s)\n", protocol, 0, 0); /* * Force Protocol to be lowercase as a way of doing * a case insensitive match. */ strncpy (protobuf, protocol, PROTOBUFSIZE); for (i = 0; i < PROTOBUFSIZE && protobuf[i] != '\0'; i++) if (isupper (protobuf[i])) protobuf[i] = tolower (protobuf[i]); /* Look at all of the configured protocols */ for (i = 0; i < NUMTRANS; i++) { if (!strcmp (protobuf, Xtransports[i].transport->TransName)) return Xtransports[i].transport; } return NULL;}#ifndef TEST_tstatic#endif /* TEST_t */intTRANS(ParseAddress) (address, protocol, host, port)char *address;char **protocol;char **host;char **port;{ /* * For the font library, the address is a string formatted * as "protocol/host:port[/catalogue]". Note that the catologue * is optional. At this time, the catologue info is ignored, but * we have to parse it anyways. * * Other than fontlib, the address is a string formatted * as "protocol/host:port". * * If the protocol part is missing, then assume INET. * If the protocol part and host part are missing, then assume local. * If a "::" is found then assume DNET. */ char *mybuf, *tmpptr; char *_protocol, *_host, *_port; char hostnamebuf[256]; PRMSG (3,"ParseAddress(%s)\n", address, 0, 0); /* Copy the string so it can be changed */ tmpptr = mybuf = (char *) xalloc (strlen (address) + 1); strcpy (mybuf, address); /* Parse the string to get each component */ /* Get the protocol part */ _protocol = mybuf; if ((mybuf = strpbrk (mybuf,"/:")) == NULL) { /* adress is in a bad format */ *protocol = NULL; *host = NULL; *port = NULL; xfree (tmpptr); return 0; } if (*mybuf == ':') { /* * If there is a hostname, then assume inet, otherwise * it must be local. */ if (mybuf == tmpptr) { /* There is neither a protocol or host specified */ _protocol = "local"; } else { /* Ther is a hostname specified */ _protocol = "inet"; mybuf = tmpptr; /* reset to the begining of the host ptr */ } } else { /* *mybuf == '/' */ *mybuf ++= '\0'; /* put a null at the end of the protocol */ if (strlen(_protocol) == 0) { /* * If there is a hostname, then assume inet, otherwise * it must be local. */ if (*mybuf != ':') _protocol = "inet"; else _protocol = "local"; } } /* Get the host part */ _host = mybuf; if ((mybuf = strchr (mybuf,':')) == NULL) { *protocol = NULL; *host = NULL; *port = NULL; xfree (tmpptr); return 0; } *mybuf ++= '\0'; if (strlen(_host) == 0) { TRANS(GetHostname) (hostnamebuf, sizeof (hostnamebuf)); _host = hostnamebuf; } /* Check for DECnet */ if (*mybuf == ':') { _protocol = "dnet"; mybuf++; } /* Get the port */get_port: _port = mybuf;#if defined(FONT_t) || defined(FS_t) /* * Is there an optional catalogue list? */ if ((mybuf = strchr (mybuf,'/')) != NULL) *mybuf ++= '\0'; /* * The rest, if any, is the (currently unused) catalogue list. * * _catalogue = mybuf; */#endif /* * Now that we have all of the components, allocate new * string space for them. */ if ((*protocol = (char *) xalloc(strlen (_protocol) + 1)) == NULL) { /* Malloc failed */ *port = NULL; *host = NULL; *protocol = NULL; xfree (tmpptr); return 0; } else strcpy (*protocol, _protocol); if ((*host = (char *) xalloc (strlen (_host) + 1)) == NULL) { /* Malloc failed */ *port = NULL; *host = NULL; xfree (*protocol); *protocol = NULL; xfree (tmpptr); return 0; } else strcpy (*host, _host); if ((*port = (char *) xalloc (strlen (_port) + 1)) == NULL) { /* Malloc failed */ *port = NULL; xfree (*host); *host = NULL; xfree (*protocol); *protocol = NULL; xfree (tmpptr); return 0; } else strcpy (*port, _port); xfree (tmpptr); return 1;}/* * TRANS(Open) does all of the real work opening a connection. The only * funny part about this is the type parameter which is used to decide which * type of open to perform. */static XtransConnInfoTRANS(Open) (type, address)int type;char *address;{ char *protocol = NULL, *host = NULL, *port = NULL; XtransConnInfo ciptr = NULL; Xtransport *thistrans; PRMSG (2,"Open(%d,%s)\n", type, address, 0);#if defined(WIN32) && (defined(TCPCONN) || defined(DNETCONN)) if (TRANS(WSAStartup)()) { PRMSG (1,"Open: WSAStartup failed\n", 0, 0, 0); return NULL; }#endif /* Parse the Address */ if (TRANS(ParseAddress) (address, &protocol, &host, &port) == 0) { PRMSG (1,"Open: Unable to Parse address %s\n", address, 0, 0); return NULL; } /* Determine the transport type */ if ((thistrans = TRANS(SelectTransport) (protocol)) == NULL) { PRMSG (1,"Open: Unable to find transport for %s\n", protocol, 0, 0); xfree (protocol); xfree (host); xfree (port); return NULL; } /* Open the transport */ switch (type) { case XTRANS_OPEN_COTS_CLIENT:#ifdef TRANS_CLIENT ciptr = thistrans->OpenCOTSClient(thistrans, protocol, host, port);#endif /* TRANS_CLIENT */ break; case XTRANS_OPEN_COTS_SERVER:#ifdef TRANS_SERVER ciptr = thistrans->OpenCOTSServer(thistrans, protocol, host, port);#endif /* TRANS_SERVER */ break; case XTRANS_OPEN_CLTS_CLIENT:#ifdef TRANS_CLIENT ciptr = thistrans->OpenCLTSClient(thistrans, protocol, host, port);#endif /* TRANS_CLIENT */ break; case XTRANS_OPEN_CLTS_SERVER:#ifdef TRANS_SERVER ciptr = thistrans->OpenCLTSServer(thistrans, protocol, host, port);#endif /* TRANS_SERVER */ break; default: PRMSG (1,"Open: Unknown Open type %d\n", type, 0, 0); } if (ciptr == NULL) { if (!(thistrans->flags & TRANS_DISABLED)) PRMSG (1,"Open: transport open failed for %s/%s:%s\n", protocol, host, port); xfree (protocol); xfree (host); xfree (port); return NULL; } ciptr->transptr = thistrans; ciptr->port = port; /* We need this for TRANS(Reopen) */ xfree (protocol); xfree (host); return ciptr;}#ifdef TRANS_REOPEN/* * We might want to create an XtransConnInfo object based on a previously * opened connection. For example, the font server may clone itself and * pass file descriptors to the parent. */static XtransConnInfoTRANS(Reopen) (type, trans_id, fd, port)int type;int trans_id;int fd;char *port;{ XtransConnInfo ciptr = NULL; Xtransport *thistrans = NULL; char *save_port; int i; PRMSG (2,"Reopen(%d,%d,%s)\n", trans_id, fd, port); /* Determine the transport type */ for (i = 0; i < NUMTRANS; i++) if (Xtransports[i].transport_id == trans_id) { thistrans = Xtransports[i].transport; break; } if (thistrans == NULL) { PRMSG (1,"Reopen: Unable to find transport id %d\n", trans_id, 0, 0); return NULL; } if ((save_port = (char *) xalloc (strlen (port) + 1)) == NULL) { PRMSG (1,"Reopen: Unable to malloc port string\n", 0, 0, 0); return NULL; } strcpy (save_port, port); /* Get a new XtransConnInfo object */ switch (type) { case XTRANS_OPEN_COTS_SERVER: ciptr = thistrans->ReopenCOTSServer(thistrans, fd, port); break; case XTRANS_OPEN_CLTS_SERVER: ciptr = thistrans->ReopenCLTSServer(thistrans, fd, port); break; default: PRMSG (1,"Reopen: Bad Open type %d\n", type, 0, 0); } if (ciptr == NULL) { PRMSG (1,"Reopen: transport open failed\n", 0, 0, 0); return NULL; } ciptr->transptr = thistrans; ciptr->port = save_port; return ciptr;}#endif /* TRANS_REOPEN *//* * These are the public interfaces to this Transport interface. * These are the only functions that should have knowledge of the transport * table. */#ifdef TRANS_CLIENTXtransConnInfoTRANS(OpenCOTSClient) (address)char *address;{ PRMSG (2,"OpenCOTSClient(%s)\n", address, 0, 0); return TRANS(Open) (XTRANS_OPEN_COTS_CLIENT, address);}#endif /* TRANS_CLIENT */#ifdef TRANS_SERVERXtransConnInfoTRANS(OpenCOTSServer) (address)char *address;{ PRMSG (2,"OpenCOTSServer(%s)\n", address, 0, 0); return TRANS(Open) (XTRANS_OPEN_COTS_SERVER, address);}#endif /* TRANS_SERVER */#ifdef TRANS_CLIENTXtransConnInfoTRANS(OpenCLTSClient) (address)char *address;{ PRMSG (2,"OpenCLTSClient(%s)\n", address, 0, 0); return TRANS(Open) (XTRANS_OPEN_CLTS_CLIENT, address);}#endif /* TRANS_CLIENT */#ifdef TRANS_SERVERXtransConnInfoTRANS(OpenCLTSServer) (address)char *address;{ PRMSG (2,"OpenCLTSServer(%s)\n", address, 0, 0); return TRANS(Open) (XTRANS_OPEN_CLTS_SERVER, address);}#endif /* TRANS_SERVER */#ifdef TRANS_REOPENXtransConnInfoTRANS(ReopenCOTSServer) (trans_id, fd, port)int trans_id;int fd;char *port;{ PRMSG (2,"ReopenCOTSServer(%d, %d, %s)\n", trans_id, fd, port); return TRANS(Reopen) (XTRANS_OPEN_COTS_SERVER, trans_id, fd, port);}XtransConnInfoTRANS(ReopenCLTSServer) (trans_id, fd, port)int trans_id;int fd;char *port;{ PRMSG (2,"ReopenCLTSServer(%d, %d, %s)\n", trans_id, fd, port); return TRANS(Reopen) (XTRANS_OPEN_CLTS_SERVER, trans_id, fd, port);}intTRANS(GetReopenInfo) (ciptr, trans_id, fd, port)XtransConnInfo ciptr;int *trans_id;int *fd;char **port;{ int i; for (i = 0; i < NUMTRANS; i++) if (Xtransports[i].transport == ciptr->transptr) { *trans_id = Xtransports[i].transport_id; *fd = ciptr->fd; if ((*port = (char *) xalloc (strlen (ciptr->port) + 1)) == NULL) return 0; else { strcpy (*port, ciptr->port); return 1; } } return 0;}#endif /* TRANS_REOPEN */intTRANS(SetOption) (ciptr, option, arg)XtransConnInfo ciptr;int option;int arg;{ int fd = ciptr->fd; int ret = 0; PRMSG (2,"SetOption(%d,%d,%d)\n", fd, option, arg); /* * For now, all transport type use the same stuff for setting options. * As long as this is true, we can put the common code here. Once a more * complicated transport such as shared memory or an OSI implementation * that uses the session and application libraries is implemented, this * code may have to move to a transport dependent function. * * ret = ciptr->transptr->SetOption (ciptr, option, arg); */#ifdef MINIX return ciptr->transptr->SetOption(ciptr, option, arg);#else /* !MINIX */ switch (option) { case TRANS_NONBLOCKING: switch (arg) { case 0: /* Set to blocking mode */ break; case 1: /* Set to non-blocking mode */#if defined(O_NONBLOCK) && (!defined(ultrix) && !defined(hpux) && !defined(AIXV3) && !defined(uniosu) && !defined(__EMX__) && !defined(SCO)) ret = fcntl (fd, F_SETFL, O_NONBLOCK);#else#ifdef FIOSNBIO {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -