Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtlogin / netaddr.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $TOG: netaddr.c /main/5 1997/03/14 13:44:57 barstow $ */
24 /* (c) Copyright 1997 The Open Group */
25 /*                                                                      *
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.                                *
30  */
31 /*
32  * @DEC_COPYRIGHT@
33  */
34 /*
35  * HISTORY
36  * $Log$
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]
40  *
41  *      R6 version used for XDMCP improvements
42  *      [1995/04/12  18:32:12  Peter_Derr]
43  *
44  * $EndLog$
45  */
46 /*
47
48 Copyright (c) 1991  X Consortium
49
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:
57
58 The above copyright notice and this permission notice shall be included
59 in all copies or substantial portions of the Software.
60
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.
68
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.
73
74 */
75
76 /*
77  * xdm - X display manager
78  *
79  * netaddr.c - Interpretation of XdmcpNetaddr object.
80  */
81
82 #include "dm.h"
83
84 #include <X11/X.h>              /* FamilyInternet, etc. */
85
86 #include <sys/socket.h>         /* struct sockaddr */
87 #include <netinet/in.h>         /* struct sockaddr_in */
88
89 #ifdef UNIXCONN
90 #include <sys/un.h>             /* struct sockaddr_un */
91 #endif
92 #ifdef DNETCONN
93 #include <netdnet/dn.h>         /* struct sockaddr_dn */
94 #endif
95
96 /* given an XdmcpNetaddr, returns the socket protocol family used,
97    e.g., AF_INET */
98
99 int NetaddrFamily(netaddrp)
100     XdmcpNetaddr netaddrp;
101 {
102 #ifdef STREAMSCONN
103     short family = *(short *)netaddrp;
104     return family;
105 #else
106     return ((struct sockaddr *)netaddrp)->sa_family;
107 #endif
108 }
109
110
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. */
114
115 char * NetaddrPort(netaddrp, lenp)
116     XdmcpNetaddr netaddrp;
117     int *lenp;                  /* return */
118 {
119 #ifdef STREAMSCONN
120     *lenp = 2;
121     return netaddrp+2;
122 #else
123     switch (NetaddrFamily(netaddrp))
124     {
125     case AF_INET:
126         *lenp = 2;
127         return (char *)&(((struct sockaddr_in *)netaddrp)->sin_port);
128     default:
129         *lenp = 0;
130         return NULL;
131     }
132 #endif
133 }
134
135
136 /* given an XdmcpNetaddr, returns a pointer to the network address
137    and sets *lenp to the length of the address */
138
139 char * NetaddrAddress(netaddrp, lenp)
140     XdmcpNetaddr netaddrp;
141     int *lenp;                  /* return */
142 {
143 #ifdef STREAMSCONN
144     *lenp = 4;
145     return netaddrp+4;
146 #else
147     switch (NetaddrFamily(netaddrp)) {
148 #ifdef UNIXCONN
149     case AF_UNIX:
150         *lenp = strlen(((struct sockaddr_un *)netaddrp)->sun_path);
151         return (char *) (((struct sockaddr_un *)netaddrp)->sun_path);
152 #endif
153 #ifdef TCPCONN
154     case AF_INET:
155         *lenp = sizeof (struct in_addr);
156         return (char *) &(((struct sockaddr_in *)netaddrp)->sin_addr);
157 #endif
158 #ifdef DNETCONN
159     case AF_DECnet:
160         *lenp = sizeof (struct dn_naddr);
161         return (char *) &(((struct sockaddr_dn *)netaddrp)->sdn_add);
162 #endif
163 #ifdef AF_CHAOS
164     case AF_CHAOS:
165 #endif
166     default:
167         *lenp = 0;
168         return NULL;
169     }
170 #endif /* STREAMSCONN else */
171 }
172
173
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 */
177
178 int ConvertAddr (saddr, len, addr)
179     XdmcpNetaddr saddr;
180     int *len;                   /* return */
181     char **addr;                /* return */
182 {
183     int retval;
184
185     if (len == NULL)
186         return -1;
187     *addr = NetaddrAddress(saddr, len);
188 #ifdef STREAMSCONN
189     /* kludge */
190     if (NetaddrFamily(saddr) == 2)
191         retval = FamilyInternet;
192 #else
193     switch (NetaddrFamily(saddr))
194     {
195 #ifdef AF_UNSPEC
196       case AF_UNSPEC:
197         retval = FamilyLocal;
198         break;
199 #endif
200 #ifdef AF_UNIX
201 #ifndef hpux
202       case AF_UNIX:
203         retval = FamilyLocal;
204         break;
205 #endif
206 #endif
207 #ifdef TCPCONN
208       case AF_INET:
209         retval = FamilyInternet;
210         break;
211 #endif
212 #ifdef DNETCONN
213       case AF_DECnet:
214         retval = FamilyDECnet;
215         break;
216 #endif
217 #ifdef AF_CHAOS
218     case AF_CHAOS:
219         retval = FamilyChaos;
220         break;
221 #endif
222       default:
223         retval = -1;
224         break;
225     }
226 #endif /* STREAMSCONN else */
227     Debug ("ConvertAddr returning %d for family %d\n", retval,
228            NetaddrFamily(saddr));
229     return retval;
230 }
231
232 addressEqual (a1, len1, a2, len2)
233     XdmcpNetaddr a1, a2;
234     int          len1, len2;
235 {
236     int partlen1, partlen2;
237     char *part1, *part2;
238
239     if (len1 != len2)
240     {
241         return FALSE;
242     }
243     if (NetaddrFamily(a1) != NetaddrFamily(a2))
244     {
245         return FALSE;
246     }
247     part1 = NetaddrPort(a1, &partlen1);
248     part2 = NetaddrPort(a2, &partlen2);
249     if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
250     {
251         return FALSE;
252     }
253     part1 = NetaddrAddress(a1, &partlen1);
254     part2 = NetaddrAddress(a2, &partlen2);
255     if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
256     {
257         return FALSE;
258     }
259     return TRUE;
260 }
261
262 #ifdef DEBUG
263 /*ARGSUSED*/
264 PrintSockAddr (a, len)          /* Debugging routine */
265     struct sockaddr *a;
266     int             len;
267 {
268     unsigned char    *t, *p;
269
270     Debug ("family %d, ", a->sa_family);
271     switch (a->sa_family) {
272 #ifdef AF_INET
273     case AF_INET:
274
275         p = (unsigned char *) &((struct sockaddr_in *) a)->sin_port;
276         t = (unsigned char *) &((struct sockaddr_in *) a)->sin_addr;
277
278         Debug ("port %d, host %d.%d.%d.%d\n",
279                 (p[0] << 8) + p[1], t[0], t[1], t[2], t[3]);
280         break;
281     }
282 #endif
283 }
284 #endif