1 /*	$NetBSD: netaddr.h,v 1.4 2014/12/10 04:38:00 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004-2007, 2009  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1998-2002  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: netaddr.h,v 1.37 2009/01/17 23:47:43 tbox Exp  */
21 
22 #ifndef ISC_NETADDR_H
23 #define ISC_NETADDR_H 1
24 
25 /*! \file isc/netaddr.h */
26 
27 #include <isc/lang.h>
28 #include <isc/net.h>
29 #include <isc/types.h>
30 
31 #ifdef ISC_PLATFORM_HAVESYSUNH
32 #include <sys/types.h>
33 #include <sys/un.h>
34 #endif
35 
36 ISC_LANG_BEGINDECLS
37 
38 struct isc_netaddr {
39 	unsigned int family;
40 	union {
41 		struct in_addr in;
42 		struct in6_addr in6;
43 #ifdef ISC_PLATFORM_HAVESYSUNH
44 		char un[sizeof(((struct sockaddr_un *)0)->sun_path)];
45 #endif
46 	} type;
47 	isc_uint32_t zone;
48 };
49 
50 isc_boolean_t
51 isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b);
52 
53 /*%<
54  * Compare network addresses 'a' and 'b'.  Return #ISC_TRUE if
55  * they are equal, #ISC_FALSE if not.
56  */
57 
58 isc_boolean_t
59 isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
60 		     unsigned int prefixlen);
61 /*%<
62  * Compare the 'prefixlen' most significant bits of the network
63  * addresses 'a' and 'b'.  If 'b''s scope is zero then 'a''s scope is
64  * ignored.  Return #ISC_TRUE if they are equal, #ISC_FALSE if not.
65  */
66 
67 isc_result_t
68 isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp);
69 /*%<
70  * Convert a netmask in 's' into a prefix length in '*lenp'.
71  * The mask should consist of zero or more '1' bits in the most
72  * most significant part of the address, followed by '0' bits.
73  * If this is not the case, #ISC_R_MASKNONCONTIG is returned.
74  *
75  * Returns:
76  *\li	#ISC_R_SUCCESS
77  *\li	#ISC_R_MASKNONCONTIG
78  */
79 
80 isc_result_t
81 isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target);
82 /*%<
83  * Append a text representation of 'sockaddr' to the buffer 'target'.
84  * The text is NOT null terminated.  Handles IPv4 and IPv6 addresses.
85  *
86  * Returns:
87  *\li	#ISC_R_SUCCESS
88  *\li	#ISC_R_NOSPACE	The text or the null termination did not fit.
89  *\li	#ISC_R_FAILURE	Unspecified failure
90  */
91 
92 void
93 isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size);
94 /*%<
95  * Format a human-readable representation of the network address '*na'
96  * into the character array 'array', which is of size 'size'.
97  * The resulting string is guaranteed to be null-terminated.
98  */
99 
100 #define ISC_NETADDR_FORMATSIZE \
101 	sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX%SSSSSSSSSS")
102 /*%<
103  * Minimum size of array to pass to isc_netaddr_format().
104  */
105 
106 void
107 isc_netaddr_fromsockaddr(isc_netaddr_t *netaddr, const isc_sockaddr_t *source);
108 
109 void
110 isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina);
111 
112 void
113 isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6);
114 
115 isc_result_t
116 isc_netaddr_frompath(isc_netaddr_t *netaddr, const char *path);
117 
118 void
119 isc_netaddr_setzone(isc_netaddr_t *netaddr, isc_uint32_t zone);
120 
121 isc_uint32_t
122 isc_netaddr_getzone(const isc_netaddr_t *netaddr);
123 
124 void
125 isc_netaddr_any(isc_netaddr_t *netaddr);
126 /*%<
127  * Return the IPv4 wildcard address.
128  */
129 
130 void
131 isc_netaddr_any6(isc_netaddr_t *netaddr);
132 /*%<
133  * Return the IPv6 wildcard address.
134  */
135 
136 isc_boolean_t
137 isc_netaddr_ismulticast(isc_netaddr_t *na);
138 /*%<
139  * Returns ISC_TRUE if the address is a multicast address.
140  */
141 
142 isc_boolean_t
143 isc_netaddr_isexperimental(isc_netaddr_t *na);
144 /*%<
145  * Returns ISC_TRUE if the address is a experimental (CLASS E) address.
146  */
147 
148 isc_boolean_t
149 isc_netaddr_islinklocal(isc_netaddr_t *na);
150 /*%<
151  * Returns #ISC_TRUE if the address is a link local address.
152  */
153 
154 isc_boolean_t
155 isc_netaddr_issitelocal(isc_netaddr_t *na);
156 /*%<
157  * Returns #ISC_TRUE if the address is a site local address.
158  */
159 
160 void
161 isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s);
162 /*%<
163  * Convert an IPv6 v4mapped address into an IPv4 address.
164  */
165 
166 isc_result_t
167 isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen);
168 /*
169  * Test whether the netaddr 'na' and 'prefixlen' are consistant.
170  * e.g. prefixlen within range.
171  *      na does not have bits set which are not covered by the prefixlen.
172  *
173  * Returns:
174  *	ISC_R_SUCCESS
175  *	ISC_R_RANGE		prefixlen out of range
176  *	ISC_R_NOTIMPLEMENTED	unsupported family
177  *	ISC_R_FAILURE		extra bits.
178  */
179 
180 ISC_LANG_ENDDECLS
181 
182 #endif /* ISC_NETADDR_H */
183