summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bind/lib/isc/netaddr.c
diff options
context:
space:
mode:
authorderaadt <deraadt@openbsd.org>2019-12-16 16:16:22 +0000
committerderaadt <deraadt@openbsd.org>2019-12-16 16:16:22 +0000
commit2fc5abb04c862f358e234358caea4444e15db068 (patch)
tree1c77d5b577fef0399be14245b2108ca9c1a8819d /usr.sbin/bind/lib/isc/netaddr.c
parentNeed to include message size in the maximum buffer calculation. (diff)
downloadwireguard-openbsd-2fc5abb04c862f358e234358caea4444e15db068.tar.xz
wireguard-openbsd-2fc5abb04c862f358e234358caea4444e15db068.zip
Update to bind-9.10.5-P3, which appears to have been the last ISC version.
We only use this tree to build dig and nslookup. Our previous version predated edns0 support in those tools, and we want that. This is the worst code I've looked at in years, with layers and layers of spaghetti abstraction clearly unfit for reuse, but then reused anyways, and the old ones remain behind. So this is a 8MB diff. florian, sthen, and otto tried this merge before but failed.
Diffstat (limited to 'usr.sbin/bind/lib/isc/netaddr.c')
-rw-r--r--usr.sbin/bind/lib/isc/netaddr.c74
1 files changed, 44 insertions, 30 deletions
diff --git a/usr.sbin/bind/lib/isc/netaddr.c b/usr.sbin/bind/lib/isc/netaddr.c
index ba0bd4e6569..9f4d49eec21 100644
--- a/usr.sbin/bind/lib/isc/netaddr.c
+++ b/usr.sbin/bind/lib/isc/netaddr.c
@@ -1,8 +1,8 @@
/*
- * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (C) 2004, 2005, 2007, 2010-2012, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2002 Internet Software Consortium.
*
- * Permission to use, copy, modify, and distribute this software for any
+ * 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.
*
@@ -15,7 +15,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $ISC: netaddr.c,v 1.27.18.8 2005/04/27 05:02:03 sra Exp $ */
+/* $Id: netaddr.c,v 1.3 2019/12/16 16:16:26 deraadt Exp $ */
/*! \file */
@@ -69,17 +69,17 @@ isc_boolean_t
isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
unsigned int prefixlen)
{
- const unsigned char *pa, *pb;
- unsigned int ipabytes; /* Length of whole IP address in bytes */
- unsigned int nbytes; /* Number of significant whole bytes */
- unsigned int nbits; /* Number of significant leftover bits */
+ 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)
+ if (a->zone != b->zone && b->zone != 0)
return (ISC_FALSE);
switch (a->family) {
@@ -94,8 +94,6 @@ isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b,
ipabytes = 16;
break;
default:
- pa = pb = NULL; /* Avoid silly compiler warning. */
- ipabytes = 0; /* Ditto. */
return (ISC_FALSE);
}
@@ -188,6 +186,9 @@ isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
isc_buffer_init(&buf, array, size);
result = isc_netaddr_totext(na, &buf);
+ if (size == 0)
+ return;
+
/*
* Null terminate.
*/
@@ -212,7 +213,7 @@ isc_netaddr_format(const isc_netaddr_t *na, char *array, unsigned int size) {
isc_result_t
isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen) {
static const unsigned char zeros[16];
- unsigned int nbits, nbytes, ipbytes;
+ unsigned int nbits, nbytes, ipbytes = 0;
const unsigned char *p;
switch (na->family) {
@@ -229,24 +230,24 @@ isc_netaddr_prefixok(const isc_netaddr_t *na, unsigned int prefixlen) {
return (ISC_R_RANGE);
break;
default:
- ipbytes = 0;
return (ISC_R_NOTIMPLEMENTED);
}
nbytes = prefixlen / 8;
nbits = prefixlen % 8;
if (nbits != 0) {
+ INSIST(nbytes < ipbytes);
if ((p[nbytes] & (0xff>>nbits)) != 0U)
return (ISC_R_FAILURE);
nbytes++;
}
- if (memcmp(p + nbytes, zeros, ipbytes - nbytes) != 0)
+ if (nbytes < ipbytes && memcmp(p + nbytes, zeros, ipbytes - nbytes) != 0)
return (ISC_R_FAILURE);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {
- unsigned int nbits, nbytes, ipbytes, i;
+ unsigned int nbits = 0, nbytes = 0, ipbytes = 0, i;
const unsigned char *p;
switch (s->family) {
@@ -259,10 +260,8 @@ isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {
ipbytes = 16;
break;
default:
- ipbytes = 0;
return (ISC_R_NOTIMPLEMENTED);
}
- nbytes = nbits = 0;
for (i = 0; i < ipbytes; i++) {
if (p[i] != 0xFF)
break;
@@ -280,7 +279,6 @@ isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {
for (; i < ipbytes; i++) {
if (p[i] != 0)
return (ISC_R_MASKNONCONTIG);
- i++;
}
*lenp = nbytes * 8 + nbits;
return (ISC_R_SUCCESS);
@@ -303,18 +301,18 @@ isc_netaddr_fromin6(isc_netaddr_t *netaddr, const struct in6_addr *ina6) {
isc_result_t
isc_netaddr_frompath(isc_netaddr_t *netaddr, const char *path) {
#ifdef ISC_PLATFORM_HAVESYSUNH
- if (strlen(path) > sizeof(netaddr->type.un) - 1)
- return (ISC_R_NOSPACE);
-
- memset(netaddr, 0, sizeof(*netaddr));
- netaddr->family = AF_UNIX;
- strlcpy(netaddr->type.un, path, sizeof(netaddr->type.un));
- netaddr->zone = 0;
- return (ISC_R_SUCCESS);
-#else
+ if (strlen(path) > sizeof(netaddr->type.un) - 1)
+ return (ISC_R_NOSPACE);
+
+ memset(netaddr, 0, sizeof(*netaddr));
+ netaddr->family = AF_UNIX;
+ strcpy(netaddr->type.un, path);
+ netaddr->zone = 0;
+ return (ISC_R_SUCCESS);
+#else
UNUSED(netaddr);
UNUSED(path);
- return (ISC_R_NOTIMPLEMENTED);
+ return (ISC_R_NOTIMPLEMENTED);
#endif
}
@@ -342,7 +340,7 @@ isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
t->zone = 0;
break;
case AF_INET6:
- memcpy(&t->type.in6, &s->type.sin6.sin6_addr, 16);
+ memmove(&t->type.in6, &s->type.sin6.sin6_addr, 16);
#ifdef ISC_PLATFORM_HAVESCOPEID
t->zone = s->type.sin6.sin6_scope_id;
#else
@@ -351,7 +349,7 @@ isc_netaddr_fromsockaddr(isc_netaddr_t *t, const isc_sockaddr_t *s) {
break;
#ifdef ISC_PLATFORM_HAVESYSUNH
case AF_UNIX:
- memcpy(t->type.un, s->type.sunix.sun_path, sizeof(t->type.un));
+ memmove(t->type.un, s->type.sunix.sun_path, sizeof(t->type.un));
t->zone = 0;
break;
#endif
@@ -420,6 +418,22 @@ isc_netaddr_issitelocal(isc_netaddr_t *na) {
}
}
+#define ISC_IPADDR_ISNETZERO(i) \
+ (((isc_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);
+ }
+}
+
void
isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s) {
isc_netaddr_t *src;
@@ -431,6 +445,6 @@ isc_netaddr_fromv4mapped(isc_netaddr_t *t, const isc_netaddr_t *s) {
memset(t, 0, sizeof(*t));
t->family = AF_INET;
- memcpy(&t->type.in, (char *)&src->type.in6 + 12, 4);
+ memmove(&t->type.in, (char *)&src->type.in6 + 12, 4);
return;
}