sockaddr.h 5.87 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2 3
 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
 * Copyright (C) 1998-2003  Internet Software Consortium.
4
 *
Bob Halley's avatar
Bob Halley committed
5 6 7
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
Bob Halley's avatar
Bob Halley committed
16 17
 */

18
/* $Id: sockaddr.h,v 1.43 2004/11/22 23:29:10 marka Exp $ */
David Lawrence's avatar
David Lawrence committed
19

Bob Halley's avatar
Bob Halley committed
20 21 22
#ifndef ISC_SOCKADDR_H
#define ISC_SOCKADDR_H 1

Bob Halley's avatar
Bob Halley committed
23
#include <isc/lang.h>
24
#include <isc/net.h>
25
#include <isc/types.h>
Bob Halley's avatar
Bob Halley committed
26

27
struct isc_sockaddr {
Bob Halley's avatar
Bob Halley committed
28
	union {
Bob Halley's avatar
Bob Halley committed
29 30 31
		struct sockaddr		sa;
		struct sockaddr_in	sin;
		struct sockaddr_in6	sin6;
Bob Halley's avatar
Bob Halley committed
32
	}				type;
Bob Halley's avatar
Bob Halley committed
33
	unsigned int			length;		/* XXXRTH beginning? */
Bob Halley's avatar
Bob Halley committed
34
	ISC_LINK(struct isc_sockaddr)	link;
35
};
Bob Halley's avatar
Bob Halley committed
36

Bob Halley's avatar
Bob Halley committed
37 38
typedef ISC_LIST(struct isc_sockaddr)	isc_sockaddrlist_t;

39 40 41 42 43 44 45 46 47
#define ISC_SOCKADDR_CMPADDR	  0x0001	/* compare the address
						 * sin_addr/sin6_addr */
#define ISC_SOCKADDR_CMPPORT 	  0x0002	/* compare the port
						 * sin_port/sin6_port */
#define ISC_SOCKADDR_CMPSCOPE     0x0004	/* compare the scope
						 * sin6_scope */
#define ISC_SOCKADDR_CMPSCOPEZERO 0x0008	/* when comparing scopes
						 * zero scopes always match */

48 49
ISC_LANG_BEGINDECLS

50 51 52 53 54 55 56 57 58 59
isc_boolean_t
isc_sockaddr_compare(const isc_sockaddr_t *a, const isc_sockaddr_t *b,
		     unsigned int flags);
/*
 * Compare the elements of the two address ('a' and 'b') as specified
 * by 'flags' and report if they are equal or not.
 *
 * 'flags' is set from ISC_SOCKADDR_CMP*.
 */

Michael Graff's avatar
Michael Graff committed
60
isc_boolean_t
61
isc_sockaddr_equal(const isc_sockaddr_t *a, const isc_sockaddr_t *b);
62 63 64
/*
 * Return ISC_TRUE iff the socket addresses 'a' and 'b' are equal.
 */
Bob Halley's avatar
Bob Halley committed
65

66
isc_boolean_t
67
isc_sockaddr_eqaddr(const isc_sockaddr_t *a, const isc_sockaddr_t *b);
68 69 70 71
/*
 * Return ISC_TRUE iff the address parts of the socket addresses
 * 'a' and 'b' are equal, ignoring the ports.
 */
72

73 74 75
isc_boolean_t
isc_sockaddr_eqaddrprefix(const isc_sockaddr_t *a, const isc_sockaddr_t *b,
			  unsigned int prefixlen);
76 77 78 79
/*
 * Return ISC_TRUE iff the most significant 'prefixlen' bits of the
 * socket addresses 'a' and 'b' are equal, ignoring the ports.
 */
80

Bob Halley's avatar
Bob Halley committed
81
unsigned int
82
isc_sockaddr_hash(const isc_sockaddr_t *sockaddr, isc_boolean_t address_only);
83 84 85
/*
 * Return a hash value for the socket address 'sockaddr'.  If 'address_only'
 * is ISC_TRUE, the hash value will not depend on the port.
86 87 88
 *
 * IPv6 addresses containing mapped IPv4 addresses generate the same hash
 * value as the equivalent IPv4 address.
89
 */
Michael Graff's avatar
Michael Graff committed
90

91 92
void
isc_sockaddr_any(isc_sockaddr_t *sockaddr);
93 94 95
/*
 * Return the IPv4 wildcard address.
 */
96 97 98

void
isc_sockaddr_any6(isc_sockaddr_t *sockaddr);
99 100 101
/*
 * Return the IPv6 wildcard address.
 */
102

103 104
void
isc_sockaddr_anyofpf(isc_sockaddr_t *sockaddr, int family);
105 106 107 108 109 110 111
/*
 * Set '*sockaddr' to the wildcard address of protocol family
 * 'family'.
 *
 * Requires:
 *	'family' is AF_INET or AF_INET6.
 */
112

Bob Halley's avatar
Bob Halley committed
113
void
114
isc_sockaddr_fromin(isc_sockaddr_t *sockaddr, const struct in_addr *ina,
115
		    in_port_t port);
116 117 118
/*
 * Construct an isc_sockaddr_t from an IPv4 address and port.
 */
Bob Halley's avatar
Bob Halley committed
119 120

void
121
isc_sockaddr_fromin6(isc_sockaddr_t *sockaddr, const struct in6_addr *ina6,
122
		     in_port_t port);
123 124 125
/*
 * Construct an isc_sockaddr_t from an IPv6 address and port.
 */
Bob Halley's avatar
Bob Halley committed
126

127
void
128
isc_sockaddr_v6fromin(isc_sockaddr_t *sockaddr, const struct in_addr *ina,
129
		      in_port_t port);
130 131 132
/*
 * Construct an IPv6 isc_sockaddr_t representing a mapped IPv4 address.
 */
133

134 135 136
void
isc_sockaddr_fromnetaddr(isc_sockaddr_t *sockaddr, const isc_netaddr_t *na,
			 in_port_t port);
137 138 139
/*
 * Construct an isc_sockaddr_t from an isc_netaddr_t and port.
 */
140

141
int
142
isc_sockaddr_pf(const isc_sockaddr_t *sockaddr);
143 144 145 146 147 148 149 150 151 152 153 154 155
/*
 * Get the protocol family of 'sockaddr'.
 *
 * Requires:
 *
 *	'sockaddr' is a valid sockaddr with an address family of AF_INET
 *	or AF_INET6.
 *
 * Returns:
 *
 *	The protocol family of 'sockaddr', e.g. PF_INET or PF_INET6.
 */

Bob Halley's avatar
Bob Halley committed
156 157 158 159 160 161
void
isc_sockaddr_setport(isc_sockaddr_t *sockaddr, in_port_t port);
/*
 * Set the port of 'sockaddr' to 'port'.
 */

David Lawrence's avatar
David Lawrence committed
162 163 164 165 166 167
in_port_t
isc_sockaddr_getport(isc_sockaddr_t *sockaddr);
/*
 * Get the port stored in 'sockaddr'.
 */

168 169 170 171 172
isc_result_t
isc_sockaddr_totext(const isc_sockaddr_t *sockaddr, isc_buffer_t *target);
/*
 * Append a text representation of 'sockaddr' to the buffer 'target'.
 * The text will include both the IP address (v4 or v6) and the port.
173 174
 * The text is null terminated, but the terminating null is not
 * part of the buffer's used region.
175 176 177
 *
 * Returns:
 *	ISC_R_SUCCESS
178
 *	ISC_R_NOSPACE	The text or the null termination did not fit.
179
 */
Mark Andrews's avatar
Mark Andrews committed
180

181
void
Andreas Gustafsson's avatar
Andreas Gustafsson committed
182
isc_sockaddr_format(const isc_sockaddr_t *sa, char *array, unsigned int size);
183 184 185 186 187 188
/*
 * Format a human-readable representation of the socket address '*sa'
 * into the character array 'array', which is of size 'size'.
 * The resulting string is guaranteed to be null-terminated.
 */

189 190 191
isc_boolean_t
isc_sockaddr_ismulticast(isc_sockaddr_t *sa);
/*
192 193 194
 * Returns ISC_TRUE if the address is a multicast address.
 */

195 196 197 198 199 200
isc_boolean_t
isc_sockaddr_isexperimental(isc_sockaddr_t *sa);
/*
 * Returns ISC_TRUE if the address is a experimental (CLASS E) address.
 */

201 202 203 204 205 206 207 208 209 210
isc_boolean_t
isc_sockaddr_islinklocal(isc_sockaddr_t *sa);
/*
 * Returns ISC_TRUE if the address is a link local addresss.
 */

isc_boolean_t
isc_sockaddr_issitelocal(isc_sockaddr_t *sa);
/*
 * Returns ISC_TRUE if the address is a sitelocal address.
211 212
 */

213 214
#define ISC_SOCKADDR_FORMATSIZE \
	sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX#YYYYY")
215 216 217
/*
 * Minimum size of array to pass to isc_sockaddr_format().
 */
218

Bob Halley's avatar
Bob Halley committed
219 220
ISC_LANG_ENDDECLS

Bob Halley's avatar
Bob Halley committed
221
#endif /* ISC_SOCKADDR_H */