not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kdm / backend / netaddr.c
blobc62862d5595fae526a22345426006fe9bceffead
1 /*
3 Copyright 1991, 1998 The Open Group
4 Copyright 2002 Sun Microsystems, Inc. All rights reserved.
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of a copyright holder shall
24 not be used in advertising or otherwise to promote the sale, use or
25 other dealings in this Software without prior written authorization
26 from the copyright holder.
31 * xdm - display manager daemon
32 * Author: Keith Packard, MIT X Consortium
34 * netaddr.c - Interpretation of XdmcpNetaddr object.
37 #include "dm.h"
38 #include "dm_socket.h"
39 #include "dm_error.h"
41 /* given an char *, returns the socket protocol family used,
42 e.g., AF_INET */
44 int
45 netaddrFamily( char *netaddrp )
47 #ifdef STREAMSCONN
48 short family = *(short *)netaddrp;
49 return family;
50 #else
51 return ((struct sockaddr *)netaddrp)->sa_family;
52 #endif
56 /* given an char *, returns a pointer to the TCP/UDP port used
57 and sets *lenp to the length of the address
58 or 0 if not using TCP or UDP. */
60 CARD8 *
61 netaddrPort( char *netaddrp, int *lenp )
63 #ifdef STREAMSCONN
64 *lenp = 2;
65 return netaddrp+2;
66 #else
67 switch (netaddrFamily( netaddrp ))
69 case AF_INET:
70 *lenp = 2;
71 return (CARD8 *)&(((struct sockaddr_in *)netaddrp)->sin_port);
72 #if defined(IPv6) && defined(AF_INET6)
73 case AF_INET6:
74 *lenp = 2;
75 return (CARD8 *)&(((struct sockaddr_in6 *)netaddrp)->sin6_port);
76 #endif
77 default:
78 *lenp = 0;
79 return NULL;
81 #endif
85 /* given an char *, returns a pointer to the network address
86 and sets *lenp to the length of the address */
88 CARD8 *
89 netaddrAddress( char *netaddrp, int *lenp )
91 #ifdef STREAMSCONN
92 *lenp = 4;
93 return netaddrp+4;
94 #else
95 switch (netaddrFamily( netaddrp )) {
96 #ifdef UNIXCONN
97 case AF_UNIX:
98 *lenp = strlen( ((struct sockaddr_un *)netaddrp)->sun_path );
99 return (CARD8 *)(((struct sockaddr_un *)netaddrp)->sun_path);
100 #endif
101 #ifdef TCPCONN
102 case AF_INET:
103 *lenp = sizeof(struct in_addr);
104 return (CARD8 *)&(((struct sockaddr_in *)netaddrp)->sin_addr);
105 #if defined(IPv6) && defined(AF_INET6)
106 case AF_INET6:
108 struct in6_addr *a = &(((struct sockaddr_in6 *)netaddrp)->sin6_addr);
109 if (IN6_IS_ADDR_V4MAPPED( a )) {
110 *lenp = sizeof(struct in_addr);
111 return ((CARD8 *)&(a->s6_addr))+12;
112 } else {
113 *lenp = sizeof(struct in6_addr);
114 return (CARD8 *)&(a->s6_addr);
117 #endif
118 #endif
119 #ifdef DNETCONN
120 case AF_DECnet:
121 *lenp = sizeof(struct dn_naddr);
122 return (CARD8 *)&(((struct sockaddr_dn *)netaddrp)->sdn_add);
123 #endif
124 #ifdef AF_CHAOS
125 case AF_CHAOS:
126 #endif
127 default:
128 *lenp = 0;
129 return NULL;
131 #endif /* STREAMSCONN else */
135 /* given an char *, sets *addr to the network address used and
136 sets *len to the number of bytes in addr.
137 Returns the X protocol family used, e.g., FamilyInternet */
140 convertAddr( char *saddr, int *len, CARD8 **addr )
142 int retval;
144 if (len == NULL)
145 return -1;
146 *addr = netaddrAddress( saddr, len );
147 #ifdef STREAMSCONN
148 /* kludge */
149 if (netaddrFamily( saddr ) == 2)
150 retval = FamilyInternet;
151 #else
152 switch (netaddrFamily( saddr )) {
153 #ifdef AF_UNSPEC
154 case AF_UNSPEC:
155 retval = FamilyLocal;
156 break;
157 #endif
158 #ifdef AF_UNIX
159 #ifndef __hpux
160 case AF_UNIX:
161 retval = FamilyLocal;
162 break;
163 #endif
164 #endif
165 #ifdef TCPCONN
166 case AF_INET:
167 retval = FamilyInternet;
168 break;
169 #if defined(IPv6) && defined(AF_INET6)
170 case AF_INET6:
171 if (*len == sizeof(struct in_addr))
172 retval = FamilyInternet;
173 else
174 retval = FamilyInternet6;
175 break;
176 #endif
177 #endif
178 #ifdef DNETCONN
179 case AF_DECnet:
180 retval = FamilyDECnet;
181 break;
182 #endif
183 #ifdef AF_CHAOS
184 case AF_CHAOS:
185 retval = FamilyChaos;
186 break;
187 #endif
188 default:
189 retval = -1;
190 break;
192 #endif /* STREAMSCONN else */
193 debug( "convertAddr returning %d for family %d\n", retval,
194 netaddrFamily( saddr ) );
195 return retval;
198 #ifdef XDMCP
200 addressEqual( char *a1, int len1, char *a2, int len2 )
202 int partlen1, partlen2;
203 CARD8 *part1, *part2;
205 if (len1 != len2)
206 return False;
207 if (netaddrFamily( a1 ) != netaddrFamily( a2 ))
208 return False;
209 part1 = netaddrPort( a1, &partlen1 );
210 part2 = netaddrPort( a2, &partlen2 );
211 if (partlen1 != partlen2 || memcmp( part1, part2, partlen1 ) != 0)
212 return False;
213 part1 = netaddrAddress( a1, &partlen1 );
214 part2 = netaddrAddress( a2, &partlen2 );
215 if (partlen1 != partlen2 || memcmp( part1, part2, partlen1 ) != 0)
216 return False;
217 return True;
219 #endif