2000-02-09 22:52:37 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2000-02-09 22:52:37 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2000-05-26 00:41:03 +00:00
|
|
|
#include <stdio.h>
|
2000-02-09 22:52:37 +00:00
|
|
|
|
2000-05-26 00:41:03 +00:00
|
|
|
#include <isc/buffer.h>
|
2004-02-20 00:52:46 +00:00
|
|
|
#include <isc/net.h>
|
2000-02-09 22:52:37 +00:00
|
|
|
#include <isc/netaddr.h>
|
2000-05-26 00:41:03 +00:00
|
|
|
#include <isc/print.h>
|
2000-02-09 22:52:37 +00:00
|
|
|
#include <isc/sockaddr.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
2000-02-09 22:52:37 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_equal(const isc_netaddr_t *a, const isc_netaddr_t *b)
|
|
|
|
{
|
2000-02-15 00:28:48 +00:00
|
|
|
REQUIRE(a != NULL && b != NULL);
|
|
|
|
|
|
|
|
if (a->family != b->family)
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2000-02-15 00:28:48 +00:00
|
|
|
|
2002-10-24 03:52:35 +00:00
|
|
|
if (a->zone != b->zone)
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2002-10-24 03:52:35 +00:00
|
|
|
|
2000-02-15 00:28:48 +00:00
|
|
|
switch (a->family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (a->type.in.s_addr != b->type.in.s_addr)
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2000-02-15 00:28:48 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2020-02-12 13:59:18 +01:00
|
|
|
if (memcmp(&a->type.in6, &b->type.in6, sizeof(a->type.in6)) !=
|
|
|
|
0 ||
|
2004-05-15 03:37:34 +00:00
|
|
|
a->zone != b->zone)
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2000-02-15 00:28:48 +00:00
|
|
|
break;
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVESYSUNH
|
|
|
|
case AF_UNIX:
|
|
|
|
if (strcmp(a->type.un, b->type.un) != 0)
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2005-02-23 01:09:23 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2000-02-15 00:28:48 +00:00
|
|
|
default:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2000-02-15 00:28:48 +00:00
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2000-02-15 00:28:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2000-02-09 22:52:37 +00:00
|
|
|
isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
|
|
|
|
unsigned int prefixlen)
|
|
|
|
{
|
2011-03-11 06:11:27 +00:00
|
|
|
const unsigned char *pa = NULL, *pb = NULL;
|
|
|
|
unsigned int ipabytes = 0; /* Length of whole IP address in bytes */
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int nbytes; /* Number of significant whole bytes */
|
|
|
|
unsigned int nbits; /* Number of significant leftover bits */
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-09 22:52:37 +00:00
|
|
|
REQUIRE(a != NULL && b != NULL);
|
|
|
|
|
|
|
|
if (a->family != b->family)
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2007-03-05 04:57:57 +00:00
|
|
|
if (a->zone != b->zone && b->zone != 0)
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2002-10-24 03:52:35 +00:00
|
|
|
|
2000-02-09 22:52:37 +00:00
|
|
|
switch (a->family) {
|
|
|
|
case AF_INET:
|
2020-02-12 13:59:18 +01:00
|
|
|
pa = (const unsigned char *)&a->type.in;
|
|
|
|
pb = (const unsigned char *)&b->type.in;
|
2000-02-09 22:52:37 +00:00
|
|
|
ipabytes = 4;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2020-02-12 13:59:18 +01:00
|
|
|
pa = (const unsigned char *)&a->type.in6;
|
|
|
|
pb = (const unsigned char *)&b->type.in6;
|
2000-02-09 22:52:37 +00:00
|
|
|
ipabytes = 16;
|
|
|
|
break;
|
|
|
|
default:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2000-02-09 22:52:37 +00:00
|
|
|
}
|
|
|
|
|
2000-06-01 17:20:56 +00:00
|
|
|
/*
|
|
|
|
* Don't crash if we get a pattern like 10.0.0.1/9999999.
|
|
|
|
*/
|
2000-02-09 22:52:37 +00:00
|
|
|
if (prefixlen > ipabytes * 8)
|
|
|
|
prefixlen = ipabytes * 8;
|
|
|
|
|
|
|
|
nbytes = prefixlen / 8;
|
|
|
|
nbits = prefixlen % 8;
|
|
|
|
|
|
|
|
if (nbytes > 0) {
|
|
|
|
if (memcmp(pa, pb, nbytes) != 0)
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2000-02-09 22:52:37 +00:00
|
|
|
}
|
|
|
|
if (nbits > 0) {
|
|
|
|
unsigned int bytea, byteb, mask;
|
|
|
|
INSIST(nbytes < ipabytes);
|
|
|
|
INSIST(nbits < 8);
|
|
|
|
bytea = pa[nbytes];
|
|
|
|
byteb = pb[nbytes];
|
2020-02-12 13:59:18 +01:00
|
|
|
mask = (0xFF << (8 - nbits)) & 0xFF;
|
2000-02-09 22:52:37 +00:00
|
|
|
if ((bytea & mask) != (byteb & mask))
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2000-02-09 22:52:37 +00:00
|
|
|
}
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2000-02-09 22:52:37 +00:00
|
|
|
}
|
|
|
|
|
2000-05-26 00:41:03 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target)
|
|
|
|
{
|
2001-11-27 01:56:32 +00:00
|
|
|
char abuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
|
2002-10-24 03:52:35 +00:00
|
|
|
char zbuf[sizeof("%4294967295")];
|
2000-05-26 00:41:03 +00:00
|
|
|
unsigned int alen;
|
2020-02-12 13:59:18 +01:00
|
|
|
int zlen;
|
|
|
|
const char * r;
|
|
|
|
const void * type;
|
2000-05-26 00:41:03 +00:00
|
|
|
|
|
|
|
REQUIRE(netaddr != NULL);
|
|
|
|
|
2002-10-24 03:52:35 +00:00
|
|
|
switch (netaddr->family) {
|
|
|
|
case AF_INET:
|
|
|
|
type = &netaddr->type.in;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
type = &netaddr->type.in6;
|
|
|
|
break;
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVESYSUNH
|
|
|
|
case AF_UNIX:
|
|
|
|
alen = strlen(netaddr->type.un);
|
|
|
|
if (alen > isc_buffer_availablelength(target))
|
|
|
|
return (ISC_R_NOSPACE);
|
2005-03-16 20:15:08 +00:00
|
|
|
isc_buffer_putmem(target,
|
|
|
|
(const unsigned char *)(netaddr->type.un),
|
|
|
|
alen);
|
2005-02-23 01:09:23 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
#endif
|
2002-10-24 03:52:35 +00:00
|
|
|
default:
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
r = inet_ntop(netaddr->family, type, abuf, sizeof(abuf));
|
2000-05-26 00:41:03 +00:00
|
|
|
if (r == NULL)
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
|
|
|
|
alen = strlen(abuf);
|
|
|
|
INSIST(alen < sizeof(abuf));
|
|
|
|
|
2002-10-24 03:52:35 +00:00
|
|
|
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))
|
2000-05-26 00:41:03 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-05-26 00:41:03 +00:00
|
|
|
isc_buffer_putmem(target, (unsigned char *)abuf, alen);
|
2018-05-15 08:18:01 +02:00
|
|
|
isc_buffer_putmem(target, (unsigned char *)zbuf, (unsigned int)zlen);
|
2000-05-26 00:41:03 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size)
|
|
|
|
{
|
2000-05-26 00:41:03 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_buffer_t buf;
|
|
|
|
|
|
|
|
isc_buffer_init(&buf, array, size);
|
|
|
|
result = isc_netaddr_totext(na, &buf);
|
|
|
|
|
2011-02-21 06:30:06 +00:00
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
2000-05-26 00:41:03 +00:00
|
|
|
/*
|
|
|
|
* Null terminate.
|
|
|
|
*/
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
if (isc_buffer_availablelength(&buf) >= 1)
|
|
|
|
isc_buffer_putuint8(&buf, 0);
|
|
|
|
else
|
|
|
|
result = ISC_R_NOSPACE;
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-05-26 00:41:03 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-02-12 13:59:18 +01:00
|
|
|
snprintf(array, size, "<unknown address, family %u>",
|
2000-05-26 00:41:03 +00:00
|
|
|
na->family);
|
|
|
|
array[size - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-17 00:46:05 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen)
|
|
|
|
{
|
2005-01-17 00:46:05 +00:00
|
|
|
static const unsigned char zeros[16];
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int nbits, nbytes, ipbytes = 0;
|
|
|
|
const unsigned char * p;
|
2005-01-17 00:46:05 +00:00
|
|
|
|
|
|
|
switch (na->family) {
|
|
|
|
case AF_INET:
|
2020-02-12 13:59:18 +01:00
|
|
|
p = (const unsigned char *)&na->type.in;
|
2005-01-17 00:46:05 +00:00
|
|
|
ipbytes = 4;
|
|
|
|
if (prefixlen > 32)
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2020-02-12 13:59:18 +01:00
|
|
|
p = (const unsigned char *)&na->type.in6;
|
2005-01-17 00:46:05 +00:00
|
|
|
ipbytes = 16;
|
|
|
|
if (prefixlen > 128)
|
|
|
|
return (ISC_R_RANGE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
nbytes = prefixlen / 8;
|
|
|
|
nbits = prefixlen % 8;
|
|
|
|
if (nbits != 0) {
|
2014-06-19 11:33:22 +10:00
|
|
|
INSIST(nbytes < ipbytes);
|
2020-02-12 13:59:18 +01:00
|
|
|
if ((p[nbytes] & (0xff >> nbits)) != 0U)
|
2005-01-17 00:46:05 +00:00
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
nbytes++;
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
if (nbytes < ipbytes &&
|
|
|
|
memcmp(p + nbytes, zeros, ipbytes - nbytes) != 0)
|
2005-01-17 00:46:05 +00:00
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-02-09 22:52:37 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp)
|
|
|
|
{
|
|
|
|
unsigned int nbits = 0, nbytes = 0, ipbytes = 0, i;
|
2000-06-01 17:20:56 +00:00
|
|
|
const unsigned char *p;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-02-09 22:52:37 +00:00
|
|
|
switch (s->family) {
|
|
|
|
case AF_INET:
|
2020-02-12 13:59:18 +01:00
|
|
|
p = (const unsigned char *)&s->type.in;
|
2000-02-09 22:52:37 +00:00
|
|
|
ipbytes = 4;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2020-02-12 13:59:18 +01:00
|
|
|
p = (const unsigned char *)&s->type.in6;
|
2000-02-09 22:52:37 +00:00
|
|
|
ipbytes = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
for (i = 0; i < ipbytes; i++) {
|
|
|
|
if (p[i] != 0xFF)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nbytes = i;
|
|
|
|
if (i < ipbytes) {
|
|
|
|
unsigned int c = p[nbytes];
|
|
|
|
while ((c & 0x80) != 0 && nbits < 8) {
|
2020-02-12 13:59:18 +01:00
|
|
|
c <<= 1;
|
|
|
|
nbits++;
|
2000-02-09 22:52:37 +00:00
|
|
|
}
|
|
|
|
if ((c & 0xFF) != 0)
|
|
|
|
return (ISC_R_MASKNONCONTIG);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
for (; i < ipbytes; i++) {
|
|
|
|
if (p[i] != 0)
|
|
|
|
return (ISC_R_MASKNONCONTIG);
|
|
|
|
}
|
|
|
|
*lenp = nbytes * 8 + nbits;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_fromin(isc_netaddr_t *netaddr, const struct in_addr *ina)
|
|
|
|
{
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(netaddr, 0, sizeof(*netaddr));
|
2000-02-09 22:52:37 +00:00
|
|
|
netaddr->family = AF_INET;
|
|
|
|
netaddr->type.in = *ina;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6)
|
|
|
|
{
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(netaddr, 0, sizeof(*netaddr));
|
2000-02-09 22:52:37 +00:00
|
|
|
netaddr->family = AF_INET6;
|
|
|
|
netaddr->type.in6 = *ina6;
|
|
|
|
}
|
|
|
|
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_frompath(isc_netaddr_t *netaddr, const char *path)
|
|
|
|
{
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVESYSUNH
|
2010-11-17 23:47:09 +00:00
|
|
|
if (strlen(path) > sizeof(netaddr->type.un) - 1)
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
|
|
|
|
memset(netaddr, 0, sizeof(*netaddr));
|
|
|
|
netaddr->family = AF_UNIX;
|
2017-09-13 00:14:37 -07:00
|
|
|
strlcpy(netaddr->type.un, path, sizeof(netaddr->type.un));
|
2010-11-17 23:47:09 +00:00
|
|
|
netaddr->zone = 0;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
#else
|
2005-02-23 01:09:23 +00:00
|
|
|
UNUSED(netaddr);
|
|
|
|
UNUSED(path);
|
2010-11-17 23:47:09 +00:00
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
2005-02-23 01:09:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-10-24 03:52:35 +00:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_setzone(isc_netaddr_t *netaddr, uint32_t zone)
|
|
|
|
{
|
2002-10-24 03:52:35 +00:00
|
|
|
/* we currently only support AF_INET6. */
|
|
|
|
REQUIRE(netaddr->family == AF_INET6);
|
|
|
|
|
|
|
|
netaddr->zone = zone;
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_getzone(const isc_netaddr_t *netaddr)
|
|
|
|
{
|
2002-10-24 03:52:35 +00:00
|
|
|
return (netaddr->zone);
|
|
|
|
}
|
|
|
|
|
2000-02-09 22:52:37 +00:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s)
|
|
|
|
{
|
2000-02-09 22:52:37 +00:00
|
|
|
int family = s->type.sa.sa_family;
|
|
|
|
t->family = family;
|
|
|
|
switch (family) {
|
2002-10-24 03:52:35 +00:00
|
|
|
case AF_INET:
|
2000-02-09 22:52:37 +00:00
|
|
|
t->type.in = s->type.sin.sin_addr;
|
2002-10-25 02:36:42 +00:00
|
|
|
t->zone = 0;
|
2002-10-24 03:52:35 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(&t->type.in6, &s->type.sin6.sin6_addr, 16);
|
2002-10-24 03:52:35 +00:00
|
|
|
t->zone = s->type.sin6.sin6_scope_id;
|
|
|
|
break;
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVESYSUNH
|
|
|
|
case AF_UNIX:
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(t->type.un, s->type.sunix.sun_path, sizeof(t->type.un));
|
2005-02-23 01:09:23 +00:00
|
|
|
t->zone = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
2002-10-24 03:52:35 +00:00
|
|
|
default:
|
|
|
|
INSIST(0);
|
2018-11-07 15:00:07 +07:00
|
|
|
ISC_UNREACHABLE();
|
2002-10-24 03:52:35 +00:00
|
|
|
}
|
2000-02-09 22:52:37 +00:00
|
|
|
}
|
|
|
|
|
2001-02-02 02:48:47 +00:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_any(isc_netaddr_t *netaddr)
|
|
|
|
{
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(netaddr, 0, sizeof(*netaddr));
|
2001-02-02 02:48:47 +00:00
|
|
|
netaddr->family = AF_INET;
|
|
|
|
netaddr->type.in.s_addr = INADDR_ANY;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_any6(isc_netaddr_t *netaddr)
|
|
|
|
{
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(netaddr, 0, sizeof(*netaddr));
|
2001-02-02 02:48:47 +00:00
|
|
|
netaddr->family = AF_INET6;
|
|
|
|
netaddr->type.in6 = in6addr_any;
|
|
|
|
}
|
|
|
|
|
2017-02-02 11:54:28 -08:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_unspec(isc_netaddr_t *netaddr)
|
|
|
|
{
|
2017-02-02 11:54:28 -08:00
|
|
|
memset(netaddr, 0, sizeof(*netaddr));
|
|
|
|
netaddr->family = AF_UNSPEC;
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_ismulticast(const isc_netaddr_t *na)
|
|
|
|
{
|
2000-06-15 18:23:54 +00:00
|
|
|
switch (na->family) {
|
|
|
|
case AF_INET:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (ISC_IPADDR_ISMULTICAST(na->type.in.s_addr));
|
2000-06-15 18:23:54 +00:00
|
|
|
case AF_INET6:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (IN6_IS_ADDR_MULTICAST(&na->type.in6));
|
2000-06-15 18:23:54 +00:00
|
|
|
default:
|
2020-02-12 13:59:18 +01:00
|
|
|
return (false); /* XXXMLG ? */
|
2000-06-15 18:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
2001-03-26 21:33:07 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_isexperimental(const isc_netaddr_t *na)
|
|
|
|
{
|
2004-02-20 00:52:46 +00:00
|
|
|
switch (na->family) {
|
|
|
|
case AF_INET:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (ISC_IPADDR_ISEXPERIMENTAL(na->type.in.s_addr));
|
2004-02-20 00:52:46 +00:00
|
|
|
default:
|
2020-02-12 13:59:18 +01:00
|
|
|
return (false); /* XXXMLG ? */
|
2004-02-20 00:52:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_islinklocal(const isc_netaddr_t *na)
|
|
|
|
{
|
2002-04-03 06:38:38 +00:00
|
|
|
switch (na->family) {
|
|
|
|
case AF_INET:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2002-04-03 06:38:38 +00:00
|
|
|
case AF_INET6:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (IN6_IS_ADDR_LINKLOCAL(&na->type.in6));
|
2002-04-03 06:38:38 +00:00
|
|
|
default:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2002-04-03 06:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_issitelocal(const isc_netaddr_t *na)
|
|
|
|
{
|
2002-04-03 06:38:38 +00:00
|
|
|
switch (na->family) {
|
|
|
|
case AF_INET:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2002-04-03 06:38:38 +00:00
|
|
|
case AF_INET6:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (IN6_IS_ADDR_SITELOCAL(&na->type.in6));
|
2002-04-03 06:38:38 +00:00
|
|
|
default:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2002-04-03 06:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 10:46:43 +11:00
|
|
|
#define ISC_IPADDR_ISNETZERO(i) \
|
2020-02-12 13:59:18 +01:00
|
|
|
(((uint32_t)(i)&ISC__IPADDR(0xff000000)) == ISC__IPADDR(0x00000000))
|
2015-10-16 08:00:15 +11:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_isnetzero(const isc_netaddr_t *na)
|
|
|
|
{
|
2015-10-16 08:00:15 +11:00
|
|
|
switch (na->family) {
|
|
|
|
case AF_INET:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (ISC_IPADDR_ISNETZERO(na->type.in.s_addr));
|
2015-10-16 08:00:15 +11:00
|
|
|
case AF_INET6:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2015-10-16 08:00:15 +11:00
|
|
|
default:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2015-10-16 08:00:15 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-26 21:33:07 +00:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s)
|
|
|
|
{
|
2001-04-12 21:23:21 +00:00
|
|
|
isc_netaddr_t *src;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
DE_CONST(s, src); /* Must come before IN6_IS_ADDR_V4MAPPED. */
|
2001-04-12 21:23:21 +00:00
|
|
|
|
2001-03-26 21:33:07 +00:00
|
|
|
REQUIRE(s->family == AF_INET6);
|
2001-04-14 00:20:07 +00:00
|
|
|
REQUIRE(IN6_IS_ADDR_V4MAPPED(&src->type.in6));
|
2001-03-26 21:33:07 +00:00
|
|
|
|
2001-04-12 21:23:21 +00:00
|
|
|
memset(t, 0, sizeof(*t));
|
2001-03-26 21:33:07 +00:00
|
|
|
t->family = AF_INET;
|
2014-01-08 16:27:10 -08:00
|
|
|
memmove(&t->type.in, (char *)&src->type.in6 + 12, 4);
|
2001-03-26 21:33:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-06 15:13:27 -07:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_netaddr_isloopback(const isc_netaddr_t *na)
|
|
|
|
{
|
2017-10-06 15:13:27 -07:00
|
|
|
switch (na->family) {
|
|
|
|
case AF_INET:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (((ntohl(na->type.in.s_addr) & 0xff000000U) ==
|
2020-02-12 13:59:18 +01:00
|
|
|
0x7f000000U));
|
2017-10-06 15:13:27 -07:00
|
|
|
case AF_INET6:
|
|
|
|
return (IN6_IS_ADDR_LOOPBACK(&na->type.in6));
|
|
|
|
default:
|
2018-04-17 08:29:14 -07:00
|
|
|
return (false);
|
2017-10-06 15:13:27 -07:00
|
|
|
}
|
|
|
|
}
|