1/*	$NetBSD$	*/
2
3/*
4 * Copyright (C) 2004-2007, 2009  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/* Id: sockaddr.h,v 1.57 2009/01/18 23:48:14 tbox Exp  */
21
22#ifndef ISC_SOCKADDR_H
23#define ISC_SOCKADDR_H 1
24
25/*! \file isc/sockaddr.h */
26
27#include <isc/lang.h>
28#include <isc/net.h>
29#include <isc/types.h>
30#ifdef ISC_PLATFORM_HAVESYSUNH
31#include <sys/un.h>
32#endif
33
34struct isc_sockaddr {
35	union {
36		struct sockaddr		sa;
37		struct sockaddr_in	sin;
38		struct sockaddr_in6	sin6;
39#ifdef ISC_PLATFORM_HAVESYSUNH
40		struct sockaddr_un	sunix;
41#endif
42	}				type;
43	unsigned int			length;		/* XXXRTH beginning? */
44	ISC_LINK(struct isc_sockaddr)	link;
45};
46
47typedef ISC_LIST(struct isc_sockaddr)	isc_sockaddrlist_t;
48
49#define ISC_SOCKADDR_CMPADDR	  0x0001	/*%< compare the address
50						 *   sin_addr/sin6_addr */
51#define ISC_SOCKADDR_CMPPORT 	  0x0002	/*%< compare the port
52						 *   sin_port/sin6_port */
53#define ISC_SOCKADDR_CMPSCOPE     0x0004	/*%< compare the scope
54						 *   sin6_scope */
55#define ISC_SOCKADDR_CMPSCOPEZERO 0x0008	/*%< when comparing scopes
56						 *   zero scopes always match */
57
58ISC_LANG_BEGINDECLS
59
60isc_boolean_t
61isc_sockaddr_compare(const isc_sockaddr_t *a, const isc_sockaddr_t *b,
62		     unsigned int flags);
63/*%<
64 * Compare the elements of the two address ('a' and 'b') as specified
65 * by 'flags' and report if they are equal or not.
66 *
67 * 'flags' is set from ISC_SOCKADDR_CMP*.
68 */
69
70isc_boolean_t
71isc_sockaddr_equal(const isc_sockaddr_t *a, const isc_sockaddr_t *b);
72/*%<
73 * Return ISC_TRUE iff the socket addresses 'a' and 'b' are equal.
74 */
75
76isc_boolean_t
77isc_sockaddr_eqaddr(const isc_sockaddr_t *a, const isc_sockaddr_t *b);
78/*%<
79 * Return ISC_TRUE iff the address parts of the socket addresses
80 * 'a' and 'b' are equal, ignoring the ports.
81 */
82
83isc_boolean_t
84isc_sockaddr_eqaddrprefix(const isc_sockaddr_t *a, const isc_sockaddr_t *b,
85			  unsigned int prefixlen);
86/*%<
87 * Return ISC_TRUE iff the most significant 'prefixlen' bits of the
88 * socket addresses 'a' and 'b' are equal, ignoring the ports.
89 * If 'b''s scope is zero then 'a''s scope will be ignored.
90 */
91
92unsigned int
93isc_sockaddr_hash(const isc_sockaddr_t *sockaddr, isc_boolean_t address_only);
94/*%<
95 * Return a hash value for the socket address 'sockaddr'.  If 'address_only'
96 * is ISC_TRUE, the hash value will not depend on the port.
97 *
98 * IPv6 addresses containing mapped IPv4 addresses generate the same hash
99 * value as the equivalent IPv4 address.
100 */
101
102void
103isc_sockaddr_any(isc_sockaddr_t *sockaddr);
104/*%<
105 * Return the IPv4 wildcard address.
106 */
107
108void
109isc_sockaddr_any6(isc_sockaddr_t *sockaddr);
110/*%<
111 * Return the IPv6 wildcard address.
112 */
113
114void
115isc_sockaddr_anyofpf(isc_sockaddr_t *sockaddr, int family);
116/*%<
117 * Set '*sockaddr' to the wildcard address of protocol family
118 * 'family'.
119 *
120 * Requires:
121 * \li	'family' is AF_INET or AF_INET6.
122 */
123
124void
125isc_sockaddr_fromin(isc_sockaddr_t *sockaddr, const struct in_addr *ina,
126		    in_port_t port);
127/*%<
128 * Construct an isc_sockaddr_t from an IPv4 address and port.
129 */
130
131void
132isc_sockaddr_fromin6(isc_sockaddr_t *sockaddr, const struct in6_addr *ina6,
133		     in_port_t port);
134/*%<
135 * Construct an isc_sockaddr_t from an IPv6 address and port.
136 */
137
138void
139isc_sockaddr_v6fromin(isc_sockaddr_t *sockaddr, const struct in_addr *ina,
140		      in_port_t port);
141/*%<
142 * Construct an IPv6 isc_sockaddr_t representing a mapped IPv4 address.
143 */
144
145void
146isc_sockaddr_fromnetaddr(isc_sockaddr_t *sockaddr, const isc_netaddr_t *na,
147			 in_port_t port);
148/*%<
149 * Construct an isc_sockaddr_t from an isc_netaddr_t and port.
150 */
151
152int
153isc_sockaddr_pf(const isc_sockaddr_t *sockaddr);
154/*%<
155 * Get the protocol family of 'sockaddr'.
156 *
157 * Requires:
158 *
159 *\li	'sockaddr' is a valid sockaddr with an address family of AF_INET
160 *	or AF_INET6.
161 *
162 * Returns:
163 *
164 *\li	The protocol family of 'sockaddr', e.g. PF_INET or PF_INET6.
165 */
166
167void
168isc_sockaddr_setport(isc_sockaddr_t *sockaddr, in_port_t port);
169/*%<
170 * Set the port of 'sockaddr' to 'port'.
171 */
172
173in_port_t
174isc_sockaddr_getport(const isc_sockaddr_t *sockaddr);
175/*%<
176 * Get the port stored in 'sockaddr'.
177 */
178
179isc_result_t
180isc_sockaddr_totext(const isc_sockaddr_t *sockaddr, isc_buffer_t *target);
181/*%<
182 * Append a text representation of 'sockaddr' to the buffer 'target'.
183 * The text will include both the IP address (v4 or v6) and the port.
184 * The text is null terminated, but the terminating null is not
185 * part of the buffer's used region.
186 *
187 * Returns:
188 * \li	ISC_R_SUCCESS
189 * \li	ISC_R_NOSPACE	The text or the null termination did not fit.
190 */
191
192void
193isc_sockaddr_format(const isc_sockaddr_t *sa, char *array, unsigned int size);
194/*%<
195 * Format a human-readable representation of the socket address '*sa'
196 * into the character array 'array', which is of size 'size'.
197 * The resulting string is guaranteed to be null-terminated.
198 */
199
200isc_boolean_t
201isc_sockaddr_ismulticast(const isc_sockaddr_t *sa);
202/*%<
203 * Returns #ISC_TRUE if the address is a multicast address.
204 */
205
206isc_boolean_t
207isc_sockaddr_isexperimental(const isc_sockaddr_t *sa);
208/*
209 * Returns ISC_TRUE if the address is a experimental (CLASS E) address.
210 */
211
212isc_boolean_t
213isc_sockaddr_islinklocal(const isc_sockaddr_t *sa);
214/*%<
215 * Returns ISC_TRUE if the address is a link local address.
216 */
217
218isc_boolean_t
219isc_sockaddr_issitelocal(const isc_sockaddr_t *sa);
220/*%<
221 * Returns ISC_TRUE if the address is a sitelocal address.
222 */
223
224isc_result_t
225isc_sockaddr_frompath(isc_sockaddr_t *sockaddr, const char *path);
226/*
227 *  Create a UNIX domain sockaddr that refers to path.
228 *
229 * Returns:
230 * \li	ISC_R_NOSPACE
231 * \li	ISC_R_NOTIMPLEMENTED
232 * \li	ISC_R_SUCCESS
233 */
234
235#define ISC_SOCKADDR_FORMATSIZE \
236	sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX%SSSSSSSSSS#YYYYY")
237/*%<
238 * Minimum size of array to pass to isc_sockaddr_format().
239 */
240
241ISC_LANG_ENDDECLS
242
243#endif /* ISC_SOCKADDR_H */
244