1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* Permission to use, copy, modify, and/or 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.
*
* 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.
*/
/* $Id: netaddr.c,v 1.4 2020/02/13 16:55:44 florian Exp $ */
/*! \file */
#include <stdio.h>
#include <isc/buffer.h>
#include <isc/net.h>
#include <isc/netaddr.h>
#include <isc/sockaddr.h>
#include <string.h>
#include <isc/util.h>
isc_boolean_t
isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
unsigned int prefixlen)
{
const unsigned char *pa = NULL, *pb = NULL;
unsigned int ipabytes = 0; /* Length of whole IP address in bytes */
unsigned int nbytes; /* Number of significant whole bytes */
unsigned int nbits; /* Number of significant leftover bits */
REQUIRE(a != NULL && b != NULL);
if (a->family != b->family)
return (ISC_FALSE);
if (a->zone != b->zone && b->zone != 0)
return (ISC_FALSE);
switch (a->family) {
case AF_INET:
pa = (const unsigned char *) &a->type.in;
pb = (const unsigned char *) &b->type.in;
ipabytes = 4;
break;
case AF_INET6:
pa = (const unsigned char *) &a->type.in6;
pb = (const unsigned char *) &b->type.in6;
ipabytes = 16;
break;
default:
return (ISC_FALSE);
}
/*
* Don't crash if we get a pattern like 10.0.0.1/9999999.
*/
if (prefixlen > ipabytes * 8)
prefixlen = ipabytes * 8;
nbytes = prefixlen / 8;
nbits = prefixlen % 8;
if (nbytes > 0) {
if (memcmp(pa, pb, nbytes) != 0)
return (ISC_FALSE);
}
if (nbits > 0) {
unsigned int bytea, byteb, mask;
INSIST(nbytes < ipabytes);
INSIST(nbits < 8);
bytea = pa[nbytes];
byteb = pb[nbytes];
mask = (0xFF << (8-nbits)) & 0xFF;
if ((bytea & mask) != (byteb & mask))
return (ISC_FALSE);
}
return (ISC_TRUE);
}
isc_result_t
isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) {
char abuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
char zbuf[sizeof("%4294967295")];
unsigned int alen;
int zlen;
const char *r;
const void *type;
REQUIRE(netaddr != NULL);
switch (netaddr->family) {
case AF_INET:
type = &netaddr->type.in;
break;
case AF_INET6:
type = &netaddr->type.in6;
break;
default:
return (ISC_R_FAILURE);
}
r = inet_ntop(netaddr->family, type, abuf, sizeof(abuf));
if (r == NULL)
return (ISC_R_FAILURE);
alen = strlen(abuf);
INSIST(alen < sizeof(abuf));
zlen = 0;
if (netaddr->family == AF_INET6 && netaddr->zone != 0) {
zlen = snprintf(zbuf, sizeof(zbuf), "%%%u", netaddr->zone);
if (zlen < 0)
return (ISC_R_FAILURE);
INSIST((unsigned int)zlen < sizeof(zbuf));
}
if (alen + zlen > isc_buffer_availablelength(target))
return (ISC_R_NOSPACE);
isc_buffer_putmem(target, (unsigned char *)abuf, alen);
isc_buffer_putmem(target, (unsigned char *)zbuf, zlen);
return (ISC_R_SUCCESS);
}
void
isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
isc_result_t result;
isc_buffer_t buf;
isc_buffer_init(&buf, array, size);
result = isc_netaddr_totext(na, &buf);
if (size == 0)
return;
/*
* Null terminate.
*/
if (result == ISC_R_SUCCESS) {
if (isc_buffer_availablelength(&buf) >= 1)
isc_buffer_putuint8(&buf, 0);
else
result = ISC_R_NOSPACE;
}
if (result != ISC_R_SUCCESS) {
snprintf(array, size, "<unknown address, family %u>",
na->family);
array[size - 1] = '\0';
}
}
uint32_t
isc_netaddr_getzone(const isc_netaddr_t *netaddr) {
return (netaddr->zone);
}
void
isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
int family = s->type.sa.sa_family;
t->family = family;
switch (family) {
case AF_INET:
t->type.in = s->type.sin.sin_addr;
t->zone = 0;
break;
case AF_INET6:
memmove(&t->type.in6, &s->type.sin6.sin6_addr, 16);
t->zone = s->type.sin6.sin6_scope_id;
break;
default:
INSIST(0);
}
}
isc_boolean_t
isc_netaddr_ismulticast(isc_netaddr_t *na) {
switch (na->family) {
case AF_INET:
return (ISC_TF(ISC_IPADDR_ISMULTICAST(na->type.in.s_addr)));
case AF_INET6:
return (ISC_TF(IN6_IS_ADDR_MULTICAST(&na->type.in6)));
default:
return (ISC_FALSE); /* XXXMLG ? */
}
}
isc_boolean_t
isc_netaddr_isexperimental(isc_netaddr_t *na) {
switch (na->family) {
case AF_INET:
return (ISC_TF(ISC_IPADDR_ISEXPERIMENTAL(na->type.in.s_addr)));
default:
return (ISC_FALSE); /* XXXMLG ? */
}
}
isc_boolean_t
isc_netaddr_islinklocal(isc_netaddr_t *na) {
switch (na->family) {
case AF_INET:
return (ISC_FALSE);
case AF_INET6:
return (ISC_TF(IN6_IS_ADDR_LINKLOCAL(&na->type.in6)));
default:
return (ISC_FALSE);
}
}
isc_boolean_t
isc_netaddr_issitelocal(isc_netaddr_t *na) {
switch (na->family) {
case AF_INET:
return (ISC_FALSE);
case AF_INET6:
return (ISC_TF(IN6_IS_ADDR_SITELOCAL(&na->type.in6)));
default:
return (ISC_FALSE);
}
}
#define ISC_IPADDR_ISNETZERO(i) \
(((uint32_t)(i) & ISC__IPADDR(0xff000000)) \
== ISC__IPADDR(0x00000000))
isc_boolean_t
isc_netaddr_isnetzero(isc_netaddr_t *na) {
switch (na->family) {
case AF_INET:
return (ISC_TF(ISC_IPADDR_ISNETZERO(na->type.in.s_addr)));
case AF_INET6:
return (ISC_FALSE);
default:
return (ISC_FALSE);
}
}
|