From 83e5a18147a0725f08b86a472cf97c996e7f734c Mon Sep 17 00:00:00 2001 From: claudio Date: Wed, 10 Nov 2021 09:15:29 +0000 Subject: [PATCH] Collaps the AFI specific IP print functions into ip_addr_print() their only difference was the buffersize and AF argument to inet_ntop. Use INET6_ADDRSTRLEN as the buffer size and convert the AFI to AF in a switch statement. OK denis@ kn@ deraadt@ --- usr.sbin/rpki-client/ip.c | 59 +++++++++++++-------------------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/usr.sbin/rpki-client/ip.c b/usr.sbin/rpki-client/ip.c index 459285a04cc..05b072ab75e 100644 --- a/usr.sbin/rpki-client/ip.c +++ b/usr.sbin/rpki-client/ip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip.c,v 1.19 2021/11/05 10:50:41 claudio Exp $ */ +/* $OpenBSD: ip.c,v 1.20 2021/11/10 09:15:29 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * @@ -233,41 +233,6 @@ ip_addr_parse(const ASN1_BIT_STRING *p, return 1; } -/* - * Convert the IPv4 address into CIDR notation conforming to RFC 4632. - * Buffer should be able to hold xxx.yyy.zzz.www/nn. - */ -static void -ip4_addr2str(const struct ip_addr *addr, char *b, size_t bsz) -{ - char buf[16]; - int ret; - - if (inet_ntop(AF_INET, addr->addr, buf, sizeof(buf)) == NULL) - err(1, "inet_ntop"); - ret = snprintf(b, bsz, "%s/%hhu", buf, addr->prefixlen); - if (ret < 0 || (size_t)ret >= bsz) - err(1, "malformed IPV4 address"); -} - -/* - * Convert the IPv6 address into CIDR notation conforming to RFC 4291. - * See also RFC 5952. - * Must hold 0000:0000:0000:0000:0000:0000:0000:0000/nn. - */ -static void -ip6_addr2str(const struct ip_addr *addr, char *b, size_t bsz) -{ - char buf[44]; - int ret; - - if (inet_ntop(AF_INET6, addr->addr, buf, sizeof(buf)) == NULL) - err(1, "inet_ntop"); - ret = snprintf(b, bsz, "%s/%hhu", buf, addr->prefixlen); - if (ret < 0 || (size_t)ret >= bsz) - err(1, "malformed IPV6 address"); -} - /* * Convert a ip_addr into a NUL-terminated CIDR notation string * conforming to RFC 4632 or 4291. @@ -277,11 +242,25 @@ void ip_addr_print(const struct ip_addr *addr, enum afi afi, char *buf, size_t bufsz) { + char ipbuf[INET6_ADDRSTRLEN]; + int ret, af; + + switch (afi) { + case AFI_IPV4: + af = AF_INET; + break; + case AFI_IPV6: + af = AF_INET6; + break; + default: + errx(1, "unsupported address family identifier"); + } - if (afi == AFI_IPV4) - ip4_addr2str(addr, buf, bufsz); - else - ip6_addr2str(addr, buf, bufsz); + if (inet_ntop(af, addr->addr, ipbuf, sizeof(ipbuf)) == NULL) + err(1, "inet_ntop"); + ret = snprintf(buf, bufsz, "%s/%hhu", ipbuf, addr->prefixlen); + if (ret < 0 || (size_t)ret >= bufsz) + err(1, "malformed IP address"); } /* -- 2.20.1