Re: rpki-client ip_addr_print cleanup

2021-11-09 Thread Klemens Nanni
On Tue, Nov 09, 2021 at 08:05:10PM +0100, Claudio Jeker wrote:
> On Tue, Nov 09, 2021 at 07:44:41PM +0100, Claudio Jeker wrote:
> > ip_addr_print() can be simplified. ip4_addr2str() and ip6_addr2str() are
> > the same apart from the different AF argument to inet_ntop(). Just collaps
> > all into ip_addr_print().
> 
> This version is using a switch statement and fails hard for unknown AFIs.
> Suggested by Theo.

OK kn, one thing.

> @@ -277,11 +242,25 @@ void
>  ip_addr_print(const struct ip_addr *addr,
>  enum afi afi, char *buf, size_t bufsz)
>  {
> + char ipbuf[44];

Why not 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");
>  }
>  
>  /*
> 



Re: rpki-client ip_addr_print cleanup

2021-11-09 Thread Claudio Jeker
On Tue, Nov 09, 2021 at 07:44:41PM +0100, Claudio Jeker wrote:
> ip_addr_print() can be simplified. ip4_addr2str() and ip6_addr2str() are
> the same apart from the different AF argument to inet_ntop(). Just collaps
> all into ip_addr_print().

This version is using a switch statement and fails hard for unknown AFIs.
Suggested by Theo.

-- 
:wq Claudio

Index: ip.c
===
RCS file: /cvs/src/usr.sbin/rpki-client/ip.c,v
retrieving revision 1.19
diff -u -p -r1.19 ip.c
--- ip.c5 Nov 2021 10:50:41 -   1.19
+++ ip.c9 Nov 2021 18:59:34 -
@@ -234,41 +234,6 @@ ip_addr_parse(const ASN1_BIT_STRING *p,
 }
 
 /*
- * 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 :::::::/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.
  * The size of the buffer must be at least 64 (inclusive).
@@ -277,11 +242,25 @@ void
 ip_addr_print(const struct ip_addr *addr,
 enum afi afi, char *buf, size_t bufsz)
 {
+   char ipbuf[44];
+   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");
 }
 
 /*



Re: rpki-client ip_addr_print cleanup

2021-11-09 Thread Denis Fondras
Le Tue, Nov 09, 2021 at 07:44:41PM +0100, Claudio Jeker a écrit :
> ip_addr_print() can be simplified. ip4_addr2str() and ip6_addr2str() are
> the same apart from the different AF argument to inet_ntop(). Just collaps
> all into ip_addr_print().
> 

OK denis@

> -- 
> :wq Claudio
> 
> Index: ip.c
> ===
> RCS file: /cvs/src/usr.sbin/rpki-client/ip.c,v
> retrieving revision 1.19
> diff -u -p -r1.19 ip.c
> --- ip.c  5 Nov 2021 10:50:41 -   1.19
> +++ ip.c  9 Nov 2021 15:16:57 -
> @@ -234,41 +234,6 @@ ip_addr_parse(const ASN1_BIT_STRING *p,
>  }
>  
>  /*
> - * 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 :::::::/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.
>   * The size of the buffer must be at least 64 (inclusive).
> @@ -277,11 +242,17 @@ void
>  ip_addr_print(const struct ip_addr *addr,
>  enum afi afi, char *buf, size_t bufsz)
>  {
> + char ipbuf[44];
> + int ret, af = AF_INET;
> +
> + if (afi == AFI_IPV6)
> + af = AF_INET6;
>  
> - 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");
>  }
>  
>  /*
> 



rpki-client ip_addr_print cleanup

2021-11-09 Thread Claudio Jeker
ip_addr_print() can be simplified. ip4_addr2str() and ip6_addr2str() are
the same apart from the different AF argument to inet_ntop(). Just collaps
all into ip_addr_print().

-- 
:wq Claudio

Index: ip.c
===
RCS file: /cvs/src/usr.sbin/rpki-client/ip.c,v
retrieving revision 1.19
diff -u -p -r1.19 ip.c
--- ip.c5 Nov 2021 10:50:41 -   1.19
+++ ip.c9 Nov 2021 15:16:57 -
@@ -234,41 +234,6 @@ ip_addr_parse(const ASN1_BIT_STRING *p,
 }
 
 /*
- * 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 :::::::/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.
  * The size of the buffer must be at least 64 (inclusive).
@@ -277,11 +242,17 @@ void
 ip_addr_print(const struct ip_addr *addr,
 enum afi afi, char *buf, size_t bufsz)
 {
+   char ipbuf[44];
+   int ret, af = AF_INET;
+
+   if (afi == AFI_IPV6)
+   af = AF_INET6;
 
-   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");
 }
 
 /*