xref: /freebsd/contrib/ntp/libntp/lib/isc/include/isc/netaddr.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*
2  * Copyright (C) 2004-2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* $Id: netaddr.h,v 1.37 2009/01/17 23:47:43 tbox Exp $ */
19 
20 #ifndef ISC_NETADDR_H
21 #define ISC_NETADDR_H 1
22 
23 /*! \file isc/netaddr.h */
24 
25 #include <isc/lang.h>
26 #include <isc/net.h>
27 #include <isc/types.h>
28 
29 #ifdef ISC_PLATFORM_HAVESYSUNH
30 #include <sys/types.h>
31 #include <sys/un.h>
32 #endif
33 
34 ISC_LANG_BEGINDECLS
35 
36 struct isc_netaddr {
37 	unsigned int family;
38 	union {
39 		struct in_addr in;
40 		struct in6_addr in6;
41 #ifdef ISC_PLATFORM_HAVESYSUNH
42 		char un[sizeof(((struct sockaddr_un *)0)->sun_path)];
43 #endif
44 	} type;
45 	isc_uint32_t zone;
46 };
47 
48 isc_boolean_t
49 isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b);
50 
51 /*%<
52  * Compare network addresses 'a' and 'b'.  Return #ISC_TRUE if
53  * they are equal, #ISC_FALSE if not.
54  */
55 
56 isc_boolean_t
57 isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
58 		     unsigned int prefixlen);
59 /*%<
60  * Compare the 'prefixlen' most significant bits of the network
61  * addresses 'a' and 'b'.  If 'b''s scope is zero then 'a''s scope is
62  * ignored.  Return #ISC_TRUE if they are equal, #ISC_FALSE if not.
63  */
64 
65 isc_result_t
66 isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp);
67 /*%<
68  * Convert a netmask in 's' into a prefix length in '*lenp'.
69  * The mask should consist of zero or more '1' bits in the most
70  * most significant part of the address, followed by '0' bits.
71  * If this is not the case, #ISC_R_MASKNONCONTIG is returned.
72  *
73  * Returns:
74  *\li	#ISC_R_SUCCESS
75  *\li	#ISC_R_MASKNONCONTIG
76  */
77 
78 isc_result_t
79 isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target);
80 /*%<
81  * Append a text representation of 'sockaddr' to the buffer 'target'.
82  * The text is NOT null terminated.  Handles IPv4 and IPv6 addresses.
83  *
84  * Returns:
85  *\li	#ISC_R_SUCCESS
86  *\li	#ISC_R_NOSPACE	The text or the null termination did not fit.
87  *\li	#ISC_R_FAILURE	Unspecified failure
88  */
89 
90 void
91 isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size);
92 /*%<
93  * Format a human-readable representation of the network address '*na'
94  * into the character array 'array', which is of size 'size'.
95  * The resulting string is guaranteed to be null-terminated.
96  */
97 
98 #define ISC_NETADDR_FORMATSIZE \
99 	sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX%SSSSSSSSSS")
100 /*%<
101  * Minimum size of array to pass to isc_netaddr_format().
102  */
103 
104 void
105 isc_netaddr_fromsockaddr(isc_netaddr_t *netaddr, const isc_sockaddr_t *source);
106 
107 void
108 isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina);
109 
110 void
111 isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6);
112 
113 isc_result_t
114 isc_netaddr_frompath(isc_netaddr_t *netaddr, const char *path);
115 
116 void
117 isc_netaddr_setzone(isc_netaddr_t *netaddr, isc_uint32_t zone);
118 
119 isc_uint32_t
120 isc_netaddr_getzone(const isc_netaddr_t *netaddr);
121 
122 void
123 isc_netaddr_any(isc_netaddr_t *netaddr);
124 /*%<
125  * Return the IPv4 wildcard address.
126  */
127 
128 void
129 isc_netaddr_any6(isc_netaddr_t *netaddr);
130 /*%<
131  * Return the IPv6 wildcard address.
132  */
133 
134 isc_boolean_t
135 isc_netaddr_ismulticast(isc_netaddr_t *na);
136 /*%<
137  * Returns ISC_TRUE if the address is a multicast address.
138  */
139 
140 isc_boolean_t
141 isc_netaddr_isexperimental(isc_netaddr_t *na);
142 /*%<
143  * Returns ISC_TRUE if the address is a experimental (CLASS E) address.
144  */
145 
146 isc_boolean_t
147 isc_netaddr_islinklocal(isc_netaddr_t *na);
148 /*%<
149  * Returns #ISC_TRUE if the address is a link local address.
150  */
151 
152 isc_boolean_t
153 isc_netaddr_issitelocal(isc_netaddr_t *na);
154 /*%<
155  * Returns #ISC_TRUE if the address is a site local address.
156  */
157 
158 void
159 isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s);
160 /*%<
161  * Convert an IPv6 v4mapped address into an IPv4 address.
162  */
163 
164 isc_result_t
165 isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen);
166 /*
167  * Test whether the netaddr 'na' and 'prefixlen' are consistant.
168  * e.g. prefixlen within range.
169  *      na does not have bits set which are not covered by the prefixlen.
170  *
171  * Returns:
172  *	ISC_R_SUCCESS
173  *	ISC_R_RANGE		prefixlen out of range
174  *	ISC_R_NOTIMPLEMENTED	unsupported family
175  *	ISC_R_FAILURE		extra bits.
176  */
177 
178 ISC_LANG_ENDDECLS
179 
180 #endif /* ISC_NETADDR_H */
181