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