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
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.
38 #include "dm_socket.h"
41 /* given an char *, returns the socket protocol family used,
45 netaddrFamily( char *netaddrp
)
48 short family
= *(short *)netaddrp
;
51 return ((struct sockaddr
*)netaddrp
)->sa_family
;
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. */
61 netaddrPort( char *netaddrp
, int *lenp
)
67 switch (netaddrFamily( netaddrp
))
71 return (CARD8
*)&(((struct sockaddr_in
*)netaddrp
)->sin_port
);
72 #if defined(IPv6) && defined(AF_INET6)
75 return (CARD8
*)&(((struct sockaddr_in6
*)netaddrp
)->sin6_port
);
85 /* given an char *, returns a pointer to the network address
86 and sets *lenp to the length of the address */
89 netaddrAddress( char *netaddrp
, int *lenp
)
95 switch (netaddrFamily( netaddrp
)) {
98 *lenp
= strlen( ((struct sockaddr_un
*)netaddrp
)->sun_path
);
99 return (CARD8
*)(((struct sockaddr_un
*)netaddrp
)->sun_path
);
103 *lenp
= sizeof(struct in_addr
);
104 return (CARD8
*)&(((struct sockaddr_in
*)netaddrp
)->sin_addr
);
105 #if defined(IPv6) && defined(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;
113 *lenp
= sizeof(struct in6_addr
);
114 return (CARD8
*)&(a
->s6_addr
);
121 *lenp
= sizeof(struct dn_naddr
);
122 return (CARD8
*)&(((struct sockaddr_dn
*)netaddrp
)->sdn_add
);
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
)
146 *addr
= netaddrAddress( saddr
, len
);
149 if (netaddrFamily( saddr
) == 2)
150 retval
= FamilyInternet
;
152 switch (netaddrFamily( saddr
)) {
155 retval
= FamilyLocal
;
161 retval
= FamilyLocal
;
167 retval
= FamilyInternet
;
169 #if defined(IPv6) && defined(AF_INET6)
171 if (*len
== sizeof(struct in_addr
))
172 retval
= FamilyInternet
;
174 retval
= FamilyInternet6
;
180 retval
= FamilyDECnet
;
185 retval
= FamilyChaos
;
192 #endif /* STREAMSCONN else */
193 debug( "convertAddr returning %d for family %d\n", retval
,
194 netaddrFamily( saddr
) );
200 addressEqual( char *a1
, int len1
, char *a2
, int len2
)
202 int partlen1
, partlen2
;
203 CARD8
*part1
, *part2
;
207 if (netaddrFamily( a1
) != netaddrFamily( a2
))
209 part1
= netaddrPort( a1
, &partlen1
);
210 part2
= netaddrPort( a2
, &partlen2
);
211 if (partlen1
!= partlen2
|| memcmp( part1
, part2
, partlen1
) != 0)
213 part1
= netaddrAddress( a1
, &partlen1
);
214 part2
= netaddrAddress( a2
, &partlen2
);
215 if (partlen1
!= partlen2
|| memcmp( part1
, part2
, partlen1
) != 0)