1 /* $XConsortium: Xtransutil.c /main/32 1996/12/04 10:22:57 lehors $ */
2 /* $XFree86: xc/lib/xtrans/Xtransutil.c,v 3.9 1996/12/23 06:04:18 dawes Exp $ */
5 Copyright (c) 1993, 1994 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
26 Except as contained in this notice, the name of the X Consortium shall
27 not be used in advertising or otherwise to promote the sale, use or
28 other dealings in this Software without prior written authorization
29 from the X Consortium.
33 /* Copyright (c) 1993, 1994 NCR Corporation - Dayton, Ohio, USA
37 * Permission to use, copy, modify, and distribute this software and its
38 * documentation for any purpose and without fee is hereby granted, provided
39 * that the above copyright notice appear in all copies and that both that
40 * copyright notice and this permission notice appear in supporting
41 * documentation, and that the name NCR not be used in advertising
42 * or publicity pertaining to distribution of the software without specific,
43 * written prior permission. NCR makes no representations about the
44 * suitability of this software for any purpose. It is provided "as is"
45 * without express or implied warranty.
47 * NCRS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
48 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
49 * NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
50 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
51 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
52 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
53 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
57 * These are some utility functions created for convenience or to provide
58 * an interface that is similar to an existing interface. These are built
59 * only using the Transport Independant API, and have no knowledge of
60 * the internal implementation.
64 #include <X11/Xthreads.h>
70 * These values come from X.h and Xauth.h, and MUST match them. Some
71 * of these values are also defined by the ChangeHost protocol message.
74 #define FamilyInternet 0
75 #define FamilyDECnet 1
77 #define FamilyAmoeba 33
78 #define FamilyLocalHost 252
79 #define FamilyKrb5Principal 253
80 #define FamilyNetname 254
81 #define FamilyLocal 256
82 #define FamilyWild 65535
85 * TRANS(ConvertAddress) converts a sockaddr based address to an
86 * X authorization based address. Some of this is defined as part of
87 * the ChangeHost protocol. The rest is just doen in a consistent manner.
91 TRANS(ConvertAddress)(familyp,addrlenp,addrp)
97 PRMSG(2,"ConvertAddress(%d,%d,%x)\n",*familyp,*addrlenp,*addrp);
101 #if defined(TCPCONN) || defined(STREAMSCONN) || defined(MNX_TCPCONN)
105 * Check for the BSD hack localhost address 127.0.0.1.
106 * In this case, we are really FamilyLocal.
109 struct sockaddr_in saddr;
112 int len = sizeof(saddr.sin_addr);
114 int len = SIZEOF_in_addr;
116 char *cp = (char *) &saddr.sin_addr;
117 #else /* else not CRAY */
118 int len = sizeof(saddr.sin_addr.s_addr);
119 char *cp = (char *) &saddr.sin_addr.s_addr;
122 memcpy (&saddr, *addrp, sizeof (struct sockaddr_in));
124 if ((len == 4) && (cp[0] == 127) && (cp[1] == 0) &&
125 (cp[2] == 0) && (cp[3] == 1))
127 *familyp=FamilyLocal;
131 *familyp=FamilyInternet;
133 memcpy(*addrp,&saddr.sin_addr,len);
137 #endif /* defined(TCPCONN) || defined(STREAMSCONN) || MNX_TCPCONN */
139 #if defined(DNETCONN)
142 struct sockaddr_dn saddr;
144 memcpy (&saddr, *addrp, sizeof (struct sockaddr_dn));
146 *familyp=FamilyDECnet;
147 *addrlenp=sizeof(struct dn_naddr);
148 memcpy(*addrp,&saddr.sdn_add,*addrlenp);
152 #endif /* defined(DNETCONN) */
154 #if defined(UNIXCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
157 *familyp=FamilyLocal;
160 #endif /* defined(UNIXCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)*/
162 #if defined(AMRPCCONN)
165 *familyp=FamilyAmoeba;
169 #if defined(AMTCPCONN) && !(defined(TCPCONN) || defined(STREAMSCONN))
172 *familyp=FamilyInternet;
178 PRMSG(1,"ConvertAddress: Unknown family type %d\n",
184 if (*familyp == FamilyLocal)
187 * In the case of a local connection, we need to get the
188 * host name for authentication.
191 char hostnamebuf[256];
192 int len = TRANS(GetHostname) (hostnamebuf, sizeof hostnamebuf);
195 if (*addrp && *addrlenp < (len + 1))
197 xfree ((char *) *addrp);
201 *addrp = (Xtransaddr *) xalloc (len + 1);
203 strcpy ((char *) *addrp, hostnamebuf);
212 xfree ((char *) *addrp);
228 TRANS(GetMyNetworkId) (ciptr)
230 XtransConnInfo ciptr;
233 int family = ciptr->family;
234 int addrlen = ciptr->addrlen;
235 char *addr = ciptr->addr;
236 char hostnamebuf[256];
237 char *networkId = NULL;
238 char *transName = ciptr->transptr->TransName;
240 if (gethostname (hostnamebuf, sizeof (hostnamebuf)) < 0)
247 #if defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
250 struct sockaddr_un *saddr = (struct sockaddr_un *) addr;
251 networkId = (char *) xalloc (3 + strlen (transName) +
252 strlen (hostnamebuf) + strlen (saddr->sun_path));
253 sprintf (networkId, "%s/%s:%s", transName,
254 hostnamebuf, saddr->sun_path);
257 #endif /* defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
260 #if defined(TCPCONN) || defined(STREAMSCONN) || defined(MNX_TCPCONN)
263 struct sockaddr_in *saddr = (struct sockaddr_in *) addr;
266 sprintf (portnumbuf, "%d", ntohs (saddr->sin_port));
267 networkId = (char *) xalloc (3 + strlen (transName) +
268 strlen (hostnamebuf) + strlen (portnumbuf));
269 sprintf (networkId, "%s/%s:%s", transName, hostnamebuf, portnumbuf);
272 #endif /* defined(TCPCONN) || defined(STREAMSCONN) || MNX_TCPCONN */
274 #if defined(DNETCONN)
277 struct sockaddr_dn *saddr = (struct sockaddr_dn *) addr;
279 networkId = (char *) xalloc (
280 13 + strlen (hostnamebuf) + saddr->sdn_objnamel);
281 sprintf (networkId, "dnet/%s::%s",
282 hostnamebuf, saddr->sdn_objname);
285 #endif /* defined(DNETCONN) */
298 static int nameserver_timedout = 0;
301 #ifdef SIGNALRETURNSINT
308 nameserver_timedout = 1;
311 #ifdef SIGNALRETURNSINT
312 return -1; /* for picky compilers */
315 #endif /* SIGALARM */
319 TRANS(GetPeerNetworkId) (ciptr)
321 XtransConnInfo ciptr;
324 int family = ciptr->family;
325 int peer_addrlen = ciptr->peeraddrlen;
326 char *peer_addr = ciptr->peeraddr;
328 char *networkId = NULL;
335 #if defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
338 if (gethostname (addrbuf, sizeof (addrbuf)) == 0)
342 #endif /* defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
345 #if defined(TCPCONN) || defined(STREAMSCONN) || defined(MNX_TCPCONN)
348 struct sockaddr_in *saddr = (struct sockaddr_in *) peer_addr;
349 _Xgethostbynameparams hparams;
350 struct hostent * hostp;
358 * gethostbyaddr can take a LONG time if the host does not exist.
359 * Assume that if it does not respond in NAMESERVER_TIMEOUT seconds
360 * that something is wrong and do not make the user wait.
361 * gethostbyaddr will continue after a signal, so we have to
365 nameserver_timedout = 0;
366 signal (SIGALRM, nameserver_lost);
368 if (setjmp(env) == 0) {
370 hostp = _XGethostbyaddr ((char *) &saddr->sin_addr,
371 sizeof (saddr->sin_addr), AF_INET, hparams);
377 addr = hostp->h_name;
379 addr = inet_ntoa (saddr->sin_addr);
383 #endif /* defined(TCPCONN) || defined(STREAMSCONN) || MNX_TCPCONN */
385 #if defined(DNETCONN)
388 struct sockaddr_dn *saddr = (struct sockaddr_dn *) peer_addr;
391 if (np = getnodebyaddr(saddr->sdn_add.a_addr,
392 saddr->sdn_add.a_len, AF_DECnet)) {
393 sprintf(addrbuf, "%s:", np->n_name);
395 sprintf(addrbuf, "%s:", dnet_htoa(&saddr->sdn_add));
400 #endif /* defined(DNETCONN) */
402 #if defined(AMRPCCONN)
405 addr = "Amoeba"; /* not really used */
409 #if defined(AMTCPCONN) && !(defined(TCPCONN) || defined(STREAMSCONN))
412 if (gethostname (addrbuf, sizeof (addrbuf)) == 0) {
426 hostname = (char *) xalloc (
427 strlen (ciptr->transptr->TransName) + strlen (addr) + 2);
428 strcpy (hostname, ciptr->transptr->TransName);
429 strcat (hostname, "/");
431 strcat (hostname, addr);
439 #if defined(WIN32) && (defined(TCPCONN) || defined(DNETCONN))
443 static WSADATA wsadata;
445 PRMSG (2,"WSAStartup()\n", 0, 0, 0);
447 if (!wsadata.wVersion && WSAStartup(MAKEWORD(1,1), &wsadata))
462 for (i = 0; i < (int) strlen (str); i++)
463 if (!isdigit (str[i]))