dtlogin: Change to ANSI function definitions
[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 libraries 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(XdmcpNetaddr netaddrp)
100 {
101 #ifdef STREAMSCONN
102     short family = *(short *)netaddrp;
103     return family;
104 #else
105     return ((struct sockaddr *)netaddrp)->sa_family;
106 #endif
107 }
108
109
110 /* given an XdmcpNetaddr, returns a pointer to the TCP/UDP port used
111    and sets *lenp to the length of the address
112    or 0 if not using TCP or UDP. */
113
114 char * NetaddrPort(XdmcpNetaddr netaddrp, int *lenp)
115 {
116 #ifdef STREAMSCONN
117     *lenp = 2;
118     return netaddrp+2;
119 #else
120     switch (NetaddrFamily(netaddrp))
121     {
122     case AF_INET:
123         *lenp = 2;
124         return (char *)&(((struct sockaddr_in *)netaddrp)->sin_port);
125     default:
126         *lenp = 0;
127         return NULL;
128     }
129 #endif
130 }
131
132
133 /* given an XdmcpNetaddr, returns a pointer to the network address
134    and sets *lenp to the length of the address */
135
136 char * NetaddrAddress(XdmcpNetaddr netaddrp, int *lenp)
137 {
138 #ifdef STREAMSCONN
139     *lenp = 4;
140     return netaddrp+4;
141 #else
142     switch (NetaddrFamily(netaddrp)) {
143 #ifdef UNIXCONN
144     case AF_UNIX:
145         *lenp = strlen(((struct sockaddr_un *)netaddrp)->sun_path);
146         return (char *) (((struct sockaddr_un *)netaddrp)->sun_path);
147 #endif
148 #ifdef TCPCONN
149     case AF_INET:
150         *lenp = sizeof (struct in_addr);
151         return (char *) &(((struct sockaddr_in *)netaddrp)->sin_addr);
152 #endif
153 #ifdef DNETCONN
154     case AF_DECnet:
155         *lenp = sizeof (struct dn_naddr);
156         return (char *) &(((struct sockaddr_dn *)netaddrp)->sdn_add);
157 #endif
158 #ifdef AF_CHAOS
159     case AF_CHAOS:
160 #endif
161     default:
162         *lenp = 0;
163         return NULL;
164     }
165 #endif /* STREAMSCONN else */
166 }
167
168
169 /* given an XdmcpNetaddr, sets *addr to the network address used and
170    sets *len to the number of bytes in addr.
171    Returns the X protocol family used, e.g., FamilyInternet */
172
173 int ConvertAddr (XdmcpNetaddr saddr, int *len, char **addr)
174 {
175     int retval;
176
177     if (len == NULL)
178         return -1;
179     *addr = NetaddrAddress(saddr, len);
180 #ifdef STREAMSCONN
181     /* kludge */
182     if (NetaddrFamily(saddr) == 2)
183         retval = FamilyInternet;
184 #else
185     switch (NetaddrFamily(saddr))
186     {
187 #ifdef AF_UNSPEC
188       case AF_UNSPEC:
189         retval = FamilyLocal;
190         break;
191 #endif
192 #ifdef AF_UNIX
193 #ifndef hpux
194       case AF_UNIX:
195         retval = FamilyLocal;
196         break;
197 #endif
198 #endif
199 #ifdef TCPCONN
200       case AF_INET:
201         retval = FamilyInternet;
202         break;
203 #endif
204 #ifdef DNETCONN
205       case AF_DECnet:
206         retval = FamilyDECnet;
207         break;
208 #endif
209 #ifdef AF_CHAOS
210     case AF_CHAOS:
211         retval = FamilyChaos;
212         break;
213 #endif
214       default:
215         retval = -1;
216         break;
217     }
218 #endif /* STREAMSCONN else */
219     Debug ("ConvertAddr returning %d for family %d\n", retval,
220            NetaddrFamily(saddr));
221     return retval;
222 }
223
224 addressEqual (XdmcpNetaddr a1, int len1, XdmcpNetaddr a2, int len2)
225 {
226     int partlen1, partlen2;
227     char *part1, *part2;
228
229     if (len1 != len2)
230     {
231         return FALSE;
232     }
233     if (NetaddrFamily(a1) != NetaddrFamily(a2))
234     {
235         return FALSE;
236     }
237     part1 = NetaddrPort(a1, &partlen1);
238     part2 = NetaddrPort(a2, &partlen2);
239     if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
240     {
241         return FALSE;
242     }
243     part1 = NetaddrAddress(a1, &partlen1);
244     part2 = NetaddrAddress(a2, &partlen2);
245     if (partlen1 != partlen2 || memcmp(part1, part2, partlen1) != 0)
246     {
247         return FALSE;
248     }
249     return TRUE;
250 }
251
252 #ifdef DEBUG
253 /*ARGSUSED*/
254 PrintSockAddr (struct sockaddr *a, int len)
255 {
256     unsigned char    *t, *p;
257
258     Debug ("family %d, ", a->sa_family);
259     switch (a->sa_family) {
260 #ifdef AF_INET
261     case AF_INET:
262
263         p = (unsigned char *) &((struct sockaddr_in *) a)->sin_port;
264         t = (unsigned char *) &((struct sockaddr_in *) a)->sin_addr;
265
266         Debug ("port %d, host %d.%d.%d.%d\n",
267                 (p[0] << 8) + p[1], t[0], t[1], t[2], t[3]);
268         break;
269     }
270 #endif
271 }
272 #endif