summaryrefslogtreecommitdiff
path: root/usr.bin/dig
diff options
context:
space:
mode:
authorFlorian Obser <florian@cvs.openbsd.org>2020-12-21 11:41:10 +0000
committerFlorian Obser <florian@cvs.openbsd.org>2020-12-21 11:41:10 +0000
commit4fc31e61ba5daec3aeb64e838fc61333a99367a1 (patch)
tree321eac4b59d555a88fb3a0a918805eb020d5b05e /usr.bin/dig
parent0fea1445927afbb614bcce94154ec4f16c3a6290 (diff)
Rewrite parse_netprefix to no longer use isc_sockaddr_fromin{,6}.
Since this was the last user of those functions we can delete them. OK otto
Diffstat (limited to 'usr.bin/dig')
-rw-r--r--usr.bin/dig/dighost.c70
-rw-r--r--usr.bin/dig/lib/isc/include/isc/sockaddr.h16
-rw-r--r--usr.bin/dig/lib/isc/sockaddr.c26
3 files changed, 20 insertions, 92 deletions
diff --git a/usr.bin/dig/dighost.c b/usr.bin/dig/dighost.c
index 3f4fac976c9..2d2a52c86e2 100644
--- a/usr.bin/dig/dighost.c
+++ b/usr.bin/dig/dighost.c
@@ -14,7 +14,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: dighost.c,v 1.36 2020/12/20 12:02:00 florian Exp $ */
+/* $Id: dighost.c,v 1.37 2020/12/21 11:41:08 florian Exp $ */
/*! \file
* \note
@@ -933,68 +933,34 @@ parse_bits(char *arg, uint32_t max) {
isc_result_t
parse_netprefix(struct sockaddr_storage **sap, int *plen, const char *value) {
struct sockaddr_storage *sa = NULL;
- struct in_addr in4;
- struct in6_addr in6;
- uint32_t prefix_length = 0xffffffff;
- char *slash = NULL;
- int parsed = 0;
- int prefix_parsed = 0;
- char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX/128")];
- const char *errstr;
+ struct in_addr *in4;
+ struct in6_addr *in6;
+ int prefix_length;
REQUIRE(sap != NULL && *sap == NULL);
- if (strlcpy(buf, value, sizeof(buf)) >= sizeof(buf))
- fatal("invalid prefix '%s'\n", value);
-
- sa = malloc(sizeof(*sa));
+ sa = calloc(1, sizeof(*sa));
if (sa == NULL)
fatal("out of memory");
- memset(sa, 0, sizeof(*sa));
- if (strcmp(buf, "0") == 0) {
+ in4 = &((struct sockaddr_in *)sa)->sin_addr;
+ in6 = &((struct sockaddr_in6 *)sa)->sin6_addr;
+
+ if (strcmp(value, "0") == 0) {
sa->ss_family = AF_UNSPEC;
prefix_length = 0;
goto done;
}
- slash = strchr(buf, '/');
- if (slash != NULL) {
- *slash = '\0';
- prefix_length = strtonum(slash + 1, 0, 128, &errstr);
- if (errstr != NULL) {
- fatal("prefix length is %s: '%s'", errstr, value);
- }
- prefix_parsed = 1;
- }
-
- if (inet_pton(AF_INET6, buf, &in6) == 1) {
- parsed = 1;
- isc_sockaddr_fromin6(sa, &in6, 0);
- if (prefix_length > 128)
- prefix_length = 128;
- } else if (inet_pton(AF_INET, buf, &in4) == 1) {
- parsed = 1;
- isc_sockaddr_fromin(sa, &in4, 0);
- if (prefix_length > 32)
- prefix_length = 32;
- } else if (prefix_parsed) {
- int i;
-
- for (i = 0; i < 3 && strlen(buf) < sizeof(buf) - 2; i++) {
- strlcat(buf, ".0", sizeof(buf));
- if (inet_pton(AF_INET, buf, &in4) == 1) {
- parsed = 1;
- isc_sockaddr_fromin(sa, &in4, 0);
- break;
- }
- }
-
- if (prefix_length > 32)
- prefix_length = 32;
- }
-
- if (!parsed)
+ if ((prefix_length = inet_net_pton(AF_INET6, value, in6, sizeof(*in6)))
+ != -1) {
+ sa->ss_len = sizeof(struct sockaddr_in6);
+ sa->ss_family = AF_INET6;
+ } else if ((prefix_length = inet_net_pton(AF_INET, value, in4,
+ sizeof(*in4))) != -1) {
+ sa->ss_len = sizeof(struct sockaddr_in);
+ sa->ss_family = AF_INET;
+ } else
fatal("invalid address '%s'", value);
done:
diff --git a/usr.bin/dig/lib/isc/include/isc/sockaddr.h b/usr.bin/dig/lib/isc/include/isc/sockaddr.h
index c29bbd30760..e31e5cfb4bc 100644
--- a/usr.bin/dig/lib/isc/include/isc/sockaddr.h
+++ b/usr.bin/dig/lib/isc/include/isc/sockaddr.h
@@ -14,7 +14,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: sockaddr.h,v 1.7 2020/09/15 11:47:42 florian Exp $ */
+/* $Id: sockaddr.h,v 1.8 2020/12/21 11:41:09 florian Exp $ */
#ifndef ISC_SOCKADDR_H
#define ISC_SOCKADDR_H 1
@@ -82,20 +82,6 @@ isc_sockaddr_anyofpf(struct sockaddr_storage *sockaddr, int family);
* \li 'family' is AF_INET or AF_INET6.
*/
-void
-isc_sockaddr_fromin(struct sockaddr_storage *sockaddr, const struct in_addr *ina,
- in_port_t port);
-/*%<
- * Construct an struct sockaddr_storage from an IPv4 address and port.
- */
-
-void
-isc_sockaddr_fromin6(struct sockaddr_storage *sockaddr, const struct in6_addr *ina6,
- in_port_t port);
-/*%<
- * Construct an struct sockaddr_storage from an IPv6 address and port.
- */
-
int
isc_sockaddr_pf(const struct sockaddr_storage *sockaddr);
/*%<
diff --git a/usr.bin/dig/lib/isc/sockaddr.c b/usr.bin/dig/lib/isc/sockaddr.c
index 42ee201fb35..6e83848f452 100644
--- a/usr.bin/dig/lib/isc/sockaddr.c
+++ b/usr.bin/dig/lib/isc/sockaddr.c
@@ -14,7 +14,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: sockaddr.c,v 1.14 2020/11/28 06:33:55 jmatthew Exp $ */
+/* $Id: sockaddr.c,v 1.15 2020/12/21 11:41:09 florian Exp $ */
/*! \file */
#include <sys/types.h>
@@ -196,18 +196,6 @@ isc_sockaddr_any6(struct sockaddr_storage *sockaddr)
}
void
-isc_sockaddr_fromin(struct sockaddr_storage *sockaddr, const struct in_addr *ina,
- in_port_t port)
-{
- struct sockaddr_in *sin = (struct sockaddr_in *) sockaddr;
- memset(sockaddr, 0, sizeof(*sockaddr));
- sin->sin_family = AF_INET;
- sin->sin_len = sizeof(*sin);
- sin->sin_addr = *ina;
- sin->sin_port = htons(port);
-}
-
-void
isc_sockaddr_anyofpf(struct sockaddr_storage *sockaddr, int pf) {
switch (pf) {
case AF_INET:
@@ -221,18 +209,6 @@ isc_sockaddr_anyofpf(struct sockaddr_storage *sockaddr, int pf) {
}
}
-void
-isc_sockaddr_fromin6(struct sockaddr_storage *sockaddr, const struct in6_addr *ina6,
- in_port_t port)
-{
- struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sockaddr;
- memset(sockaddr, 0, sizeof(*sockaddr));
- sin6->sin6_family = AF_INET6;
- sin6->sin6_len = sizeof(*sin6);
- sin6->sin6_addr = *ina6;
- sin6->sin6_port = htons(port);
-}
-
int
isc_sockaddr_pf(const struct sockaddr_storage *sockaddr) {