1 /* $TOG: netaddr.c /main/5 1997/03/14 13:44:57 barstow $ */
2 /* (c) Copyright 1997 The Open Group */
4 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
5 * (c) Copyright 1993, 1994 International Business Machines Corp. *
6 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
7 * (c) Copyright 1993, 1994 Novell, Inc. *
15 * Revision 1.1.2.2 1995/04/21 13:05:31 Peter_Derr
16 * dtlogin auth key fixes from deltacde
17 * [1995/04/12 19:21:13 Peter_Derr]
19 * R6 version used for XDMCP improvements
20 * [1995/04/12 18:32:12 Peter_Derr]
26 Copyright (c) 1991 X Consortium
28 Permission is hereby granted, free of charge, to any person obtaining
29 a copy of this software and associated documentation files (the
30 "Software"), to deal in the Software without restriction, including
31 without limitation the rights to use, copy, modify, merge, publish,
32 distribute, sublicense, and/or sell copies of the Software, and to
33 permit persons to whom the Software is furnished to do so, subject to
34 the following conditions:
36 The above copyright notice and this permission notice shall be included
37 in all copies or substantial portions of the Software.
39 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
43 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
44 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
45 OTHER DEALINGS IN THE SOFTWARE.
47 Except as contained in this notice, the name of the X Consortium shall
48 not be used in advertising or otherwise to promote the sale, use or
49 other dealings in this Software without prior written authorization
50 from the X Consortium.
55 * xdm - X display manager
57 * netaddr.c - Interpretation of XdmcpNetaddr object.
62 #include <X11/X.h> /* FamilyInternet, etc. */
64 #include <sys/socket.h> /* struct sockaddr */
65 #include <netinet/in.h> /* struct sockaddr_in */
68 #include <sys/un.h> /* struct sockaddr_un */
71 #include <netdnet/dn.h> /* struct sockaddr_dn */
74 /* given an XdmcpNetaddr, returns the socket protocol family used,
77 int NetaddrFamily(netaddrp)
78 XdmcpNetaddr netaddrp;
81 short family = *(short *)netaddrp;
84 return ((struct sockaddr *)netaddrp)->sa_family;
89 /* given an XdmcpNetaddr, returns a pointer to the TCP/UDP port used
90 and sets *lenp to the length of the address
91 or 0 if not using TCP or UDP. */
93 char * NetaddrPort(netaddrp, lenp)
94 XdmcpNetaddr netaddrp;
95 int *lenp; /* return */
101 switch (NetaddrFamily(netaddrp))
105 return (char *)&(((struct sockaddr_in *)netaddrp)->sin_port);
114 /* given an XdmcpNetaddr, returns a pointer to the network address
115 and sets *lenp to the length of the address */
117 char * NetaddrAddress(netaddrp, lenp)
118 XdmcpNetaddr netaddrp;
119 int *lenp; /* return */
125 switch (NetaddrFamily(netaddrp)) {
128 *lenp = strlen(((struct sockaddr_un *)netaddrp)->sun_path);
129 return (char *) (((struct sockaddr_un *)netaddrp)->sun_path);
133 *lenp = sizeof (struct in_addr);
134 return (char *) &(((struct sockaddr_in *)netaddrp)->sin_addr);
138 *lenp = sizeof (struct dn_naddr);
139 return (char *) &(((struct sockaddr_dn *)netaddrp)->sdn_add);
148 #endif /* STREAMSCONN else */
152 /* given an XdmcpNetaddr, sets *addr to the network address used and
153 sets *len to the number of bytes in addr.
154 Returns the X protocol family used, e.g., FamilyInternet */
156 int ConvertAddr (saddr, len, addr)
158 int *len; /* return */
159 char **addr; /* return */
165 *addr = NetaddrAddress(saddr, len);
168 if (NetaddrFamily(saddr) == 2)
169 retval = FamilyInternet;
171 switch (NetaddrFamily(saddr))
175 retval = FamilyLocal;
181 retval = FamilyLocal;
187 retval = FamilyInternet;
192 retval = FamilyDECnet;
197 retval = FamilyChaos;
204 #endif /* STREAMSCONN else */
205 Debug ("ConvertAddr returning %d for family %d\n", retval,
206 NetaddrFamily(saddr));
210 addressEqual (a1, len1, a2, len2)
214 int partlen1, partlen2;
221 if (NetaddrFamily(a1) != NetaddrFamily(a2))
225 part1 = NetaddrPort(a1, &partlen1);
226 part2 = NetaddrPort(a2, &partlen2);
227 if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
231 part1 = NetaddrAddress(a1, &partlen1);
232 part2 = NetaddrAddress(a2, &partlen2);
233 if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
242 PrintSockAddr (a, len) /* Debugging routine */
246 unsigned char *t, *p;
248 Debug ("family %d, ", a->sa_family);
249 switch (a->sa_family) {
253 p = (unsigned char *) &((struct sockaddr_in *) a)->sin_port;
254 t = (unsigned char *) &((struct sockaddr_in *) a)->sin_addr;
256 Debug ("port %d, host %d.%d.%d.%d\n",
257 (p[0] << 8) + p[1], t[0], t[1], t[2], t[3]);