From 7a023716982431df14deda031f901a9fbc349d40 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 2 Mar 2021 17:28:02 +0100 Subject: [PATCH] util: require inet_pton() Always use inet_pton() for converting IP addresses. It should be available on all currently supported systems. --- sysincl.h | 6 +----- util.c | 15 +++------------ 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/sysincl.h b/sysincl.h index 5c8866b..e26b236 100644 --- a/sysincl.h +++ b/sysincl.h @@ -28,6 +28,7 @@ #ifndef GOT_SYSINCL_H #define GOT_SYSINCL_H +#include #include #include #include @@ -61,11 +62,6 @@ #include #endif -#ifdef FEAT_IPV6 -/* For inet_ntop() */ -#include -#endif - #ifdef HAVE_GETRANDOM #include #endif diff --git a/util.c b/util.c index 31b655b..93d5ecb 100644 --- a/util.c +++ b/util.c @@ -325,9 +325,10 @@ UTI_IPToString(const IPAddr *addr) int UTI_StringToIP(const char *addr, IPAddr *ip) { -#ifdef FEAT_IPV6 struct in_addr in4; +#ifdef FEAT_IPV6 struct in6_addr in6; +#endif if (inet_pton(AF_INET, addr, &in4) > 0) { ip->family = IPADDR_INET4; @@ -336,23 +337,13 @@ UTI_StringToIP(const char *addr, IPAddr *ip) return 1; } +#ifdef FEAT_IPV6 if (inet_pton(AF_INET6, addr, &in6) > 0) { ip->family = IPADDR_INET6; ip->_pad = 0; memcpy(ip->addr.in6, in6.s6_addr, sizeof (ip->addr.in6)); return 1; } -#else - unsigned long a, b, c, d, n; - - n = sscanf(addr, "%lu.%lu.%lu.%lu", &a, &b, &c, &d); - if (n == 4) { - ip->family = IPADDR_INET4; - ip->_pad = 0; - ip->addr.in4 = ((a & 0xff) << 24) | ((b & 0xff) << 16) | - ((c & 0xff) << 8) | (d & 0xff); - return 1; - } #endif return 0;