xref: /netbsd/external/bsd/ntp/dist/lib/isc/netaddr.c (revision 6550d01e)
1 /*	$NetBSD: netaddr.c,v 1.1.1.1 2009/12/13 16:54:13 kardel Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-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.c,v 1.38 2007/06/18 23:47:44 tbox Exp */
21 
22 /*! \file */
23 
24 #include <config.h>
25 
26 #include <stdio.h>
27 
28 #include <isc/buffer.h>
29 #include <isc/msgs.h>
30 #include <isc/net.h>
31 #include <isc/netaddr.h>
32 #include <isc/print.h>
33 #include <isc/sockaddr.h>
34 #include <isc/string.h>
35 #include <isc/util.h>
36 
37 isc_boolean_t
38 isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b) {
39 	REQUIRE(a != NULL && b != NULL);
40 
41 	if (a->family != b->family)
42 		return (ISC_FALSE);
43 
44 	if (a->zone != b->zone)
45 		return (ISC_FALSE);
46 
47 	switch (a->family) {
48 	case AF_INET:
49 		if (a->type.in.s_addr != b->type.in.s_addr)
50 			return (ISC_FALSE);
51 		break;
52 	case AF_INET6:
53 		if (memcmp(&a->type.in6, &b->type.in6,
54 			   sizeof(a->type.in6)) != 0 ||
55 		    a->zone != b->zone)
56 			return (ISC_FALSE);
57 		break;
58 #ifdef ISC_PLATFORM_HAVESYSUNH
59 	case AF_UNIX:
60 		if (strcmp(a->type.un, b->type.un) != 0)
61 			return (ISC_FALSE);
62 		break;
63 #endif
64 	default:
65 		return (ISC_FALSE);
66 	}
67 	return (ISC_TRUE);
68 }
69 
70 isc_boolean_t
71 isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
72 		     unsigned int prefixlen)
73 {
74 	const unsigned char *pa, *pb;
75 	unsigned int ipabytes; /* Length of whole IP address in bytes */
76 	unsigned int nbytes;   /* Number of significant whole bytes */
77 	unsigned int nbits;    /* Number of significant leftover bits */
78 
79 	REQUIRE(a != NULL && b != NULL);
80 
81 	if (a->family != b->family)
82 		return (ISC_FALSE);
83 
84 	if (a->zone != b->zone && b->zone != 0)
85 		return (ISC_FALSE);
86 
87 	switch (a->family) {
88 	case AF_INET:
89 		pa = (const unsigned char *) &a->type.in;
90 		pb = (const unsigned char *) &b->type.in;
91 		ipabytes = 4;
92 		break;
93 	case AF_INET6:
94 		pa = (const unsigned char *) &a->type.in6;
95 		pb = (const unsigned char *) &b->type.in6;
96 		ipabytes = 16;
97 		break;
98 	default:
99 		pa = pb = NULL; /* Avoid silly compiler warning. */
100 		ipabytes = 0; /* Ditto. */
101 		return (ISC_FALSE);
102 	}
103 
104 	/*
105 	 * Don't crash if we get a pattern like 10.0.0.1/9999999.
106 	 */
107 	if (prefixlen > ipabytes * 8)
108 		prefixlen = ipabytes * 8;
109 
110 	nbytes = prefixlen / 8;
111 	nbits = prefixlen % 8;
112 
113 	if (nbytes > 0) {
114 		if (memcmp(pa, pb, nbytes) != 0)
115 			return (ISC_FALSE);
116 	}
117 	if (nbits > 0) {
118 		unsigned int bytea, byteb, mask;
119 		INSIST(nbytes < ipabytes);
120 		INSIST(nbits < 8);
121 		bytea = pa[nbytes];
122 		byteb = pb[nbytes];
123 		mask = (0xFF << (8-nbits)) & 0xFF;
124 		if ((bytea & mask) != (byteb & mask))
125 			return (ISC_FALSE);
126 	}
127 	return (ISC_TRUE);
128 }
129 
130 isc_result_t
131 isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) {
132 	char abuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
133 	char zbuf[sizeof("%4294967295")];
134 	unsigned int alen;
135 	int zlen;
136 	const char *r;
137 	const void *type;
138 
139 	REQUIRE(netaddr != NULL);
140 
141 	switch (netaddr->family) {
142 	case AF_INET:
143 		type = &netaddr->type.in;
144 		break;
145 	case AF_INET6:
146 		type = &netaddr->type.in6;
147 		break;
148 #ifdef ISC_PLATFORM_HAVESYSUNH
149 	case AF_UNIX:
150 		alen = strlen(netaddr->type.un);
151 		if (alen > isc_buffer_availablelength(target))
152 			return (ISC_R_NOSPACE);
153 		isc_buffer_putmem(target,
154 				  (const unsigned char *)(netaddr->type.un),
155 				  alen);
156 		return (ISC_R_SUCCESS);
157 #endif
158 	default:
159 		return (ISC_R_FAILURE);
160 	}
161 	r = inet_ntop(netaddr->family, type, abuf, sizeof(abuf));
162 	if (r == NULL)
163 		return (ISC_R_FAILURE);
164 
165 	alen = strlen(abuf);
166 	INSIST(alen < sizeof(abuf));
167 
168 	zlen = 0;
169 	if (netaddr->family == AF_INET6 && netaddr->zone != 0) {
170 		zlen = snprintf(zbuf, sizeof(zbuf), "%%%u", netaddr->zone);
171 		if (zlen < 0)
172 			return (ISC_R_FAILURE);
173 		INSIST((unsigned int)zlen < sizeof(zbuf));
174 	}
175 
176 	if (alen + zlen > isc_buffer_availablelength(target))
177 		return (ISC_R_NOSPACE);
178 
179 	isc_buffer_putmem(target, (unsigned char *)abuf, alen);
180 	isc_buffer_putmem(target, (unsigned char *)zbuf, zlen);
181 
182 	return (ISC_R_SUCCESS);
183 }
184 
185 void
186 isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
187 	isc_result_t result;
188 	isc_buffer_t buf;
189 
190 	isc_buffer_init(&buf, array, size);
191 	result = isc_netaddr_totext(na, &buf);
192 
193 	/*
194 	 * Null terminate.
195 	 */
196 	if (result == ISC_R_SUCCESS) {
197 		if (isc_buffer_availablelength(&buf) >= 1)
198 			isc_buffer_putuint8(&buf, 0);
199 		else
200 			result = ISC_R_NOSPACE;
201 	}
202 
203 	if (result != ISC_R_SUCCESS) {
204 		snprintf(array, size,
205 			 isc_msgcat_get(isc_msgcat, ISC_MSGSET_NETADDR,
206 					ISC_MSG_UNKNOWNADDR,
207 					"<unknown address, family %u>"),
208 			 na->family);
209 		array[size - 1] = '\0';
210 	}
211 }
212 
213 
214 isc_result_t
215 isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen) {
216 	static const unsigned char zeros[16];
217 	unsigned int nbits, nbytes, ipbytes;
218 	const unsigned char *p;
219 
220 	switch (na->family) {
221 	case AF_INET:
222 		p = (const unsigned char *) &na->type.in;
223 		ipbytes = 4;
224 		if (prefixlen > 32)
225 			return (ISC_R_RANGE);
226 		break;
227 	case AF_INET6:
228 		p = (const unsigned char *) &na->type.in6;
229 		ipbytes = 16;
230 		if (prefixlen > 128)
231 			return (ISC_R_RANGE);
232 		break;
233 	default:
234 		ipbytes = 0;
235 		return (ISC_R_NOTIMPLEMENTED);
236 	}
237 	nbytes = prefixlen / 8;
238 	nbits = prefixlen % 8;
239 	if (nbits != 0) {
240 		if ((p[nbytes] & (0xff>>nbits)) != 0U)
241 			return (ISC_R_FAILURE);
242 		nbytes++;
243 	}
244 	if (memcmp(p + nbytes, zeros, ipbytes - nbytes) != 0)
245 		return (ISC_R_FAILURE);
246 	return (ISC_R_SUCCESS);
247 }
248 
249 isc_result_t
250 isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {
251 	unsigned int nbits, nbytes, ipbytes, i;
252 	const unsigned char *p;
253 
254 	switch (s->family) {
255 	case AF_INET:
256 		p = (const unsigned char *) &s->type.in;
257 		ipbytes = 4;
258 		break;
259 	case AF_INET6:
260 		p = (const unsigned char *) &s->type.in6;
261 		ipbytes = 16;
262 		break;
263 	default:
264 		ipbytes = 0;
265 		return (ISC_R_NOTIMPLEMENTED);
266 	}
267 	nbytes = nbits = 0;
268 	for (i = 0; i < ipbytes; i++) {
269 		if (p[i] != 0xFF)
270 			break;
271 	}
272 	nbytes = i;
273 	if (i < ipbytes) {
274 		unsigned int c = p[nbytes];
275 		while ((c & 0x80) != 0 && nbits < 8) {
276 			c <<= 1; nbits++;
277 		}
278 		if ((c & 0xFF) != 0)
279 			return (ISC_R_MASKNONCONTIG);
280 		i++;
281 	}
282 	for (; i < ipbytes; i++) {
283 		if (p[i] != 0)
284 			return (ISC_R_MASKNONCONTIG);
285 		i++;
286 	}
287 	*lenp = nbytes * 8 + nbits;
288 	return (ISC_R_SUCCESS);
289 }
290 
291 void
292 isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina) {
293 	memset(netaddr, 0, sizeof(*netaddr));
294 	netaddr->family = AF_INET;
295 	netaddr->type.in = *ina;
296 }
297 
298 void
299 isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6) {
300 	memset(netaddr, 0, sizeof(*netaddr));
301 	netaddr->family = AF_INET6;
302 	netaddr->type.in6 = *ina6;
303 }
304 
305 isc_result_t
306 isc_netaddr_frompath(isc_netaddr_t *netaddr, const char *path) {
307 #ifdef ISC_PLATFORM_HAVESYSUNH
308         if (strlen(path) > sizeof(netaddr->type.un) - 1)
309                 return (ISC_R_NOSPACE);
310 
311         memset(netaddr, 0, sizeof(*netaddr));
312         netaddr->family = AF_UNIX;
313         strcpy(netaddr->type.un, path);
314         netaddr->zone = 0;
315         return (ISC_R_SUCCESS);
316 #else
317 	UNUSED(netaddr);
318 	UNUSED(path);
319         return (ISC_R_NOTIMPLEMENTED);
320 #endif
321 }
322 
323 
324 void
325 isc_netaddr_setzone(isc_netaddr_t *netaddr, isc_uint32_t zone) {
326 	/* we currently only support AF_INET6. */
327 	REQUIRE(netaddr->family == AF_INET6);
328 
329 	netaddr->zone = zone;
330 }
331 
332 isc_uint32_t
333 isc_netaddr_getzone(const isc_netaddr_t *netaddr) {
334 	return (netaddr->zone);
335 }
336 
337 void
338 isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
339 	int family = s->type.sa.sa_family;
340 	t->family = family;
341 	switch (family) {
342 	case AF_INET:
343 		t->type.in = s->type.sin.sin_addr;
344 		t->zone = 0;
345 		break;
346 	case AF_INET6:
347 		memcpy(&t->type.in6, &s->type.sin6.sin6_addr, 16);
348 #ifdef ISC_PLATFORM_HAVESCOPEID
349 		t->zone = s->type.sin6.sin6_scope_id;
350 #else
351 		t->zone = 0;
352 #endif
353 		break;
354 #ifdef ISC_PLATFORM_HAVESYSUNH
355 	case AF_UNIX:
356 		memcpy(t->type.un, s->type.sunix.sun_path, sizeof(t->type.un));
357 		t->zone = 0;
358 		break;
359 #endif
360 	default:
361 		INSIST(0);
362 	}
363 }
364 
365 void
366 isc_netaddr_any(isc_netaddr_t *netaddr) {
367 	memset(netaddr, 0, sizeof(*netaddr));
368 	netaddr->family = AF_INET;
369 	netaddr->type.in.s_addr = INADDR_ANY;
370 }
371 
372 void
373 isc_netaddr_any6(isc_netaddr_t *netaddr) {
374 	memset(netaddr, 0, sizeof(*netaddr));
375 	netaddr->family = AF_INET6;
376 	netaddr->type.in6 = in6addr_any;
377 }
378 
379 isc_boolean_t
380 isc_netaddr_ismulticast(isc_netaddr_t *na) {
381 	switch (na->family) {
382 	case AF_INET:
383 		return (ISC_TF(ISC_IPADDR_ISMULTICAST(na->type.in.s_addr)));
384 	case AF_INET6:
385 		return (ISC_TF(IN6_IS_ADDR_MULTICAST(&na->type.in6)));
386 	default:
387 		return (ISC_FALSE);  /* XXXMLG ? */
388 	}
389 }
390 
391 isc_boolean_t
392 isc_netaddr_isexperimental(isc_netaddr_t *na) {
393 	switch (na->family) {
394 	case AF_INET:
395 		return (ISC_TF(ISC_IPADDR_ISEXPERIMENTAL(na->type.in.s_addr)));
396 	default:
397 		return (ISC_FALSE);  /* XXXMLG ? */
398 	}
399 }
400 
401 isc_boolean_t
402 isc_netaddr_islinklocal(isc_netaddr_t *na) {
403 	switch (na->family) {
404 	case AF_INET:
405 		return (ISC_FALSE);
406 	case AF_INET6:
407 		return (ISC_TF(IN6_IS_ADDR_LINKLOCAL(&na->type.in6)));
408 	default:
409 		return (ISC_FALSE);
410 	}
411 }
412 
413 isc_boolean_t
414 isc_netaddr_issitelocal(isc_netaddr_t *na) {
415 	switch (na->family) {
416 	case AF_INET:
417 		return (ISC_FALSE);
418 	case AF_INET6:
419 		return (ISC_TF(IN6_IS_ADDR_SITELOCAL(&na->type.in6)));
420 	default:
421 		return (ISC_FALSE);
422 	}
423 }
424 
425 void
426 isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s) {
427 	isc_netaddr_t *src;
428 
429 	DE_CONST(s, src);	/* Must come before IN6_IS_ADDR_V4MAPPED. */
430 
431 	REQUIRE(s->family == AF_INET6);
432 	REQUIRE(IN6_IS_ADDR_V4MAPPED(&src->type.in6));
433 
434 	memset(t, 0, sizeof(*t));
435 	t->family = AF_INET;
436 	memcpy(&t->type.in, (char *)&src->type.in6 + 12, 4);
437 	return;
438 }
439