2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: netaddr.c /main/5 1997/03/14 13:44:57 barstow $ */
24 /* (c) Copyright 1997 The Open Group */
26 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
27 * (c) Copyright 1993, 1994 International Business Machines Corp. *
28 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
29 * (c) Copyright 1993, 1994 Novell, Inc. *
37 * Revision 1.1.2.2 1995/04/21 13:05:31 Peter_Derr
38 * dtlogin auth key fixes from deltacde
39 * [1995/04/12 19:21:13 Peter_Derr]
41 * R6 version used for XDMCP improvements
42 * [1995/04/12 18:32:12 Peter_Derr]
48 Copyright (c) 1991 X Consortium
50 Permission is hereby granted, free of charge, to any person obtaining
51 a copy of this software and associated documentation files (the
52 "Software"), to deal in the Software without restriction, including
53 without limitation the rights to use, copy, modify, merge, publish,
54 distribute, sublicense, and/or sell copies of the Software, and to
55 permit persons to whom the Software is furnished to do so, subject to
56 the following conditions:
58 The above copyright notice and this permission notice shall be included
59 in all copies or substantial portions of the Software.
61 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
62 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
64 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
65 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
66 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
67 OTHER DEALINGS IN THE SOFTWARE.
69 Except as contained in this notice, the name of the X Consortium shall
70 not be used in advertising or otherwise to promote the sale, use or
71 other dealings in this Software without prior written authorization
72 from the X Consortium.
77 * xdm - X display manager
79 * netaddr.c - Interpretation of XdmcpNetaddr object.
84 #include <X11/X.h> /* FamilyInternet, etc. */
86 #include <sys/socket.h> /* struct sockaddr */
87 #include <netinet/in.h> /* struct sockaddr_in */
90 #include <sys/un.h> /* struct sockaddr_un */
93 #include <netdnet/dn.h> /* struct sockaddr_dn */
96 /* given an XdmcpNetaddr, returns the socket protocol family used,
99 int NetaddrFamily(netaddrp)
100 XdmcpNetaddr netaddrp;
103 short family = *(short *)netaddrp;
106 return ((struct sockaddr *)netaddrp)->sa_family;
111 /* given an XdmcpNetaddr, returns a pointer to the TCP/UDP port used
112 and sets *lenp to the length of the address
113 or 0 if not using TCP or UDP. */
115 char * NetaddrPort(netaddrp, lenp)
116 XdmcpNetaddr netaddrp;
117 int *lenp; /* return */
123 switch (NetaddrFamily(netaddrp))
127 return (char *)&(((struct sockaddr_in *)netaddrp)->sin_port);
136 /* given an XdmcpNetaddr, returns a pointer to the network address
137 and sets *lenp to the length of the address */
139 char * NetaddrAddress(netaddrp, lenp)
140 XdmcpNetaddr netaddrp;
141 int *lenp; /* return */
147 switch (NetaddrFamily(netaddrp)) {
150 *lenp = strlen(((struct sockaddr_un *)netaddrp)->sun_path);
151 return (char *) (((struct sockaddr_un *)netaddrp)->sun_path);
155 *lenp = sizeof (struct in_addr);
156 return (char *) &(((struct sockaddr_in *)netaddrp)->sin_addr);
160 *lenp = sizeof (struct dn_naddr);
161 return (char *) &(((struct sockaddr_dn *)netaddrp)->sdn_add);
170 #endif /* STREAMSCONN else */
174 /* given an XdmcpNetaddr, sets *addr to the network address used and
175 sets *len to the number of bytes in addr.
176 Returns the X protocol family used, e.g., FamilyInternet */
178 int ConvertAddr (saddr, len, addr)
180 int *len; /* return */
181 char **addr; /* return */
187 *addr = NetaddrAddress(saddr, len);
190 if (NetaddrFamily(saddr) == 2)
191 retval = FamilyInternet;
193 switch (NetaddrFamily(saddr))
197 retval = FamilyLocal;
203 retval = FamilyLocal;
209 retval = FamilyInternet;
214 retval = FamilyDECnet;
219 retval = FamilyChaos;
226 #endif /* STREAMSCONN else */
227 Debug ("ConvertAddr returning %d for family %d\n", retval,
228 NetaddrFamily(saddr));
232 addressEqual (a1, len1, a2, len2)
236 int partlen1, partlen2;
243 if (NetaddrFamily(a1) != NetaddrFamily(a2))
247 part1 = NetaddrPort(a1, &partlen1);
248 part2 = NetaddrPort(a2, &partlen2);
249 if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
253 part1 = NetaddrAddress(a1, &partlen1);
254 part2 = NetaddrAddress(a2, &partlen2);
255 if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
264 PrintSockAddr (a, len) /* Debugging routine */
268 unsigned char *t, *p;
270 Debug ("family %d, ", a->sa_family);
271 switch (a->sa_family) {
275 p = (unsigned char *) &((struct sockaddr_in *) a)->sin_port;
276 t = (unsigned char *) &((struct sockaddr_in *) a)->sin_addr;
278 Debug ("port %d, host %d.%d.%d.%d\n",
279 (p[0] << 8) + p[1], t[0], t[1], t[2], t[3]);