]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
util: require inet_pton()
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 2 Mar 2021 16:28:02 +0000 (17:28 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 4 Mar 2021 11:36:36 +0000 (12:36 +0100)
Always use inet_pton() for converting IP addresses. It should be
available on all currently supported systems.

sysincl.h
util.c

index 5c8866b1527e32ac39e966f464cdf23895d9ecbd..e26b2362bc11d58e4a309431e6a9870434634d6c 100644 (file)
--- a/sysincl.h
+++ b/sysincl.h
@@ -28,6 +28,7 @@
 #ifndef GOT_SYSINCL_H
 #define GOT_SYSINCL_H
 
+#include <arpa/inet.h>
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <sys/timex.h>
 #endif
 
-#ifdef FEAT_IPV6
-/* For inet_ntop() */
-#include <arpa/inet.h>
-#endif
-
 #ifdef HAVE_GETRANDOM
 #include <sys/random.h>
 #endif
diff --git a/util.c b/util.c
index 31b655bc64c27f50c3b7ffe10cc82048a82704fc..93d5ecb57047299ae57368f849d9b9973a03e8fc 100644 (file)
--- 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;