]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
lib: make curlx_inet_ntop()
authorDaniel Stenberg <daniel@haxx.se>
Mon, 9 Jun 2025 07:39:40 +0000 (09:39 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 9 Jun 2025 11:16:01 +0000 (13:16 +0200)
move function to curlx/, change all callers

Closes #17560

14 files changed:
lib/Makefile.inc
lib/asyn-thrdd.c
lib/cf-socket.c
lib/connect.c
lib/curlx/curlx.h
lib/curlx/inet_ntop.c [moved from lib/inet_ntop.c with 86% similarity]
lib/curlx/inet_ntop.h [moved from lib/inet_ntop.h with 87% similarity]
lib/ftp.c
lib/hostip.c
lib/if2ip.c
lib/url.c
lib/urlapi.c
lib/vssh/libssh.c
lib/vssh/libssh2.c

index b384e09623d937b90e6504d4dfeab2cf67792a10..ecf4ce11154658688bd906321cf75d1b984d15f8 100644 (file)
@@ -25,6 +25,7 @@
 LIB_CURLX_CFILES = \
   curlx/base64.c   \
   curlx/dynbuf.c   \
+  curlx/inet_ntop.c \
   curlx/inet_pton.c \
   curlx/multibyte.c \
   curlx/nonblock.c \
@@ -39,6 +40,7 @@ LIB_CURLX_HFILES = \
   curlx/base64.h   \
   curlx/curlx.h    \
   curlx/dynbuf.h   \
+  curlx/inet_ntop.h \
   curlx/inet_pton.h \
   curlx/multibyte.h \
   curlx/nonblock.h \
@@ -208,7 +210,6 @@ LIB_CFILES =         \
   idn.c              \
   if2ip.c            \
   imap.c             \
-  inet_ntop.c        \
   krb5.c             \
   ldap.c             \
   llist.c            \
@@ -345,7 +346,6 @@ LIB_HFILES =         \
   idn.h              \
   if2ip.h            \
   imap.h             \
-  inet_ntop.h        \
   llist.h            \
   macos.h            \
   memdebug.h         \
index 9cd25dcd209cb548925924378c6453d0fbb65357..593ef3717565d2752be45990d2347e755645df74 100644 (file)
@@ -61,7 +61,6 @@
 #include "share.h"
 #include "url.h"
 #include "multiif.h"
-#include "inet_ntop.h"
 #include "curl_threads.h"
 #include "strdup.h"
 
index e31977201c6812d5ee00eb16e85c1487155061d7..f3895047073d5066a0e7951c7c0676814733130f 100644 (file)
@@ -73,7 +73,6 @@
 #include "url.h" /* for Curl_safefree() */
 #include "multiif.h"
 #include "sockaddr.h" /* required for Curl_sockaddr_storage */
-#include "inet_ntop.h"
 #include "curlx/inet_pton.h"
 #include "progress.h"
 #include "curlx/warnless.h"
index 1dcdde3fc5a8c224e39ee995a55099bddf7bc190..abf30a641f423ec3b94f77b0bb898a0e03d00da2 100644 (file)
@@ -66,7 +66,7 @@
 #include "url.h" /* for Curl_safefree() */
 #include "multiif.h"
 #include "sockaddr.h" /* required for Curl_sockaddr_storage */
-#include "inet_ntop.h"
+#include "curlx/inet_ntop.h"
 #include "curlx/inet_pton.h"
 #include "vtls/vtls.h" /* for vtsl cfilters */
 #include "progress.h"
@@ -254,8 +254,8 @@ addr_next_match(const struct Curl_addrinfo *addr, int family)
   return NULL;
 }
 
-/* retrieves ip address and port from a sockaddr structure.
-   note it calls Curl_inet_ntop which sets errno on fail, not SOCKERRNO. */
+/* retrieves ip address and port from a sockaddr structure. note it calls
+   curlx_inet_ntop which sets errno on fail, not SOCKERRNO. */
 bool Curl_addr2string(struct sockaddr *sa, curl_socklen_t salen,
                       char *addr, int *port)
 {
@@ -272,7 +272,7 @@ bool Curl_addr2string(struct sockaddr *sa, curl_socklen_t salen,
   switch(sa->sa_family) {
     case AF_INET:
       si = (struct sockaddr_in *)(void *) sa;
-      if(Curl_inet_ntop(sa->sa_family, &si->sin_addr, addr, MAX_IPADR_LEN)) {
+      if(curlx_inet_ntop(sa->sa_family, &si->sin_addr, addr, MAX_IPADR_LEN)) {
         unsigned short us_port = ntohs(si->sin_port);
         *port = us_port;
         return TRUE;
@@ -281,7 +281,8 @@ bool Curl_addr2string(struct sockaddr *sa, curl_socklen_t salen,
 #ifdef USE_IPV6
     case AF_INET6:
       si6 = (struct sockaddr_in6 *)(void *) sa;
-      if(Curl_inet_ntop(sa->sa_family, &si6->sin6_addr, addr, MAX_IPADR_LEN)) {
+      if(curlx_inet_ntop(sa->sa_family, &si6->sin6_addr, addr,
+                         MAX_IPADR_LEN)) {
         unsigned short us_port = ntohs(si6->sin6_port);
         *port = us_port;
         return TRUE;
index 983c7b5c75b68151d6fb4cfa0dbcf6be39e50154..e3598c23a02fd977e5af98f8b76c8d9731699041 100644 (file)
@@ -71,4 +71,7 @@
 #include "inet_pton.h"
 /* for curlx_inet_pton */
 
+#include "inet_ntop.h"
+/* for curlx_inet_ntop */
+
 #endif /* HEADER_CURL_CURLX_H */
similarity index 86%
rename from lib/inet_ntop.c
rename to lib/curlx/inet_ntop.c
index bb2ec57872578a6038760b6bead040de020afc51..e5ff45cb1be413716f820242d1201d6a57275d01 100644 (file)
@@ -20,7 +20,7 @@
  * Original code by Paul Vixie. "curlified" by Gisle Vanem.
  */
 
-#include "curl_setup.h"
+#include "../curl_setup.h"
 
 #ifndef HAVE_INET_NTOP
 
@@ -35,7 +35,6 @@
 #endif
 
 #include "inet_ntop.h"
-#include "curl_printf.h"
 
 #define IN6ADDRSZ       16
 /* #define INADDRSZ         4 */
@@ -65,8 +64,9 @@ static char *inet_ntop4(const unsigned char *src, char *dst, size_t size)
 
   DEBUGASSERT(size >= 16);
 
-  tmp[0] = '\0';
-  (void)msnprintf(tmp, sizeof(tmp), "%d.%d.%d.%d",
+  /* this sprintf() does not overflow the buffer. Avoids snprintf to work more
+     widely. Avoids the msnprintf family to work as a curlx function. */
+  (void)(sprintf)(tmp, "%d.%d.%d.%d",
                   ((int)((unsigned char)src[0])) & 0xff,
                   ((int)((unsigned char)src[1])) & 0xff,
                   ((int)((unsigned char)src[2])) & 0xff,
@@ -162,7 +162,21 @@ static char *inet_ntop6(const unsigned char *src, char *dst, size_t size)
       tp += strlen(tp);
       break;
     }
-    tp += msnprintf(tp, 5, "%x", words[i]);
+    else {
+      /* Lower-case digits. Can't use the set from mprintf.c since this
+         needs to work as a curlx function */
+      static const unsigned char ldigits[] = "0123456789abcdef";
+
+      unsigned int w = words[i];
+      /* output lowercase 16bit hex number but ignore leading zeroes */
+      if(w & 0xf000)
+        *tp++ = ldigits[(w & 0xf000) >> 12];
+      if(w & 0xff00)
+        *tp++ = ldigits[(w & 0x0f00) >> 8];
+      if(w & 0xfff0)
+        *tp++ = ldigits[(w & 0x00f0) >> 4];
+      *tp++ = ldigits[(w & 0x000f)];
+    }
   }
 
   /* Was it a trailing run of 0x00's?
@@ -196,7 +210,7 @@ static char *inet_ntop6(const unsigned char *src, char *dst, size_t size)
  * code. This is to avoid losing the actual last Winsock error. When this
  * function returns NULL, check errno not SOCKERRNO.
  */
-char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
+char *curlx_inet_ntop(int af, const void *src, char *buf, size_t size)
 {
   switch(af) {
   case AF_INET:
similarity index 87%
rename from lib/inet_ntop.h
rename to lib/curlx/inet_ntop.h
index 9923daaed1b91d374caaf54e6b055356ac6c725e..41ec909718f1b7faf742d57de2960fb69ea7fbb5 100644 (file)
@@ -24,9 +24,9 @@
  *
  ***************************************************************************/
 
-#include "curl_setup.h"
+#include "../curl_setup.h"
 
-char *Curl_inet_ntop(int af, const void *addr, char *buf, size_t size);
+char *curlx_inet_ntop(int af, const void *addr, char *buf, size_t size);
 
 #ifdef HAVE_INET_NTOP
 #ifdef HAVE_NETINET_IN_H
@@ -39,11 +39,11 @@ char *Curl_inet_ntop(int af, const void *addr, char *buf, size_t size);
 #include <arpa/inet.h>
 #endif
 #ifdef __AMIGA__
-#define Curl_inet_ntop(af,addr,buf,size)                                \
+#define curlx_inet_ntop(af,addr,buf,size)                               \
   (char *)inet_ntop(af, CURL_UNCONST(addr), (unsigned char *)buf,       \
                     (curl_socklen_t)(size))
 #else
-#define Curl_inet_ntop(af,addr,buf,size)                \
+#define curlx_inet_ntop(af,addr,buf,size)                \
   inet_ntop(af, addr, buf, (curl_socklen_t)(size))
 #endif
 #endif
index 3dd7fed045d618d74d7a00d460700925cd9158f3..576100be46e5bfab1197c35d49980e88aad2629e 100644 (file)
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -60,7 +60,7 @@
 #include "cf-socket.h"
 #include "connect.h"
 #include "strerror.h"
-#include "inet_ntop.h"
+#include "curlx/inet_ntop.h"
 #include "curlx/inet_pton.h"
 #include "select.h"
 #include "parsedate.h" /* for the week day and month names */
@@ -1020,11 +1020,11 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data,
     switch(sa->sa_family) {
 #ifdef USE_IPV6
     case AF_INET6:
-      r = Curl_inet_ntop(sa->sa_family, &sa6->sin6_addr, hbuf, sizeof(hbuf));
+      r = curlx_inet_ntop(sa->sa_family, &sa6->sin6_addr, hbuf, sizeof(hbuf));
       break;
 #endif
     default:
-      r = Curl_inet_ntop(sa->sa_family, &sa4->sin_addr, hbuf, sizeof(hbuf));
+      r = curlx_inet_ntop(sa->sa_family, &sa4->sin_addr, hbuf, sizeof(hbuf));
       break;
     }
     if(!r) {
index ca6724ed55dcf64b215be2489176f4c24559fb8b..6c82ec421856966f22d1832b4c4a8d232c670a20 100644 (file)
@@ -54,7 +54,7 @@
 #include "rand.h"
 #include "share.h"
 #include "url.h"
-#include "inet_ntop.h"
+#include "curlx/inet_ntop.h"
 #include "curlx/inet_pton.h"
 #include "multiif.h"
 #include "doh.h"
@@ -145,14 +145,14 @@ void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf,
   case AF_INET: {
     const struct sockaddr_in *sa4 = (const void *)ai->ai_addr;
     const struct in_addr *ipaddr4 = &sa4->sin_addr;
-    (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
+    (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
     break;
   }
 #ifdef USE_IPV6
   case AF_INET6: {
     const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr;
     const struct in6_addr *ipaddr6 = &sa6->sin6_addr;
-    (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
+    (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
     break;
   }
 #endif
index 6da68efd500d09d49e9653d6578ce061aaa4cc4c..bc420c98f2049f259c309eca51bc27ad3ff7e878 100644 (file)
@@ -52,7 +52,7 @@
 #  include <inet.h>
 #endif
 
-#include "inet_ntop.h"
+#include "curlx/inet_ntop.h"
 #include "strcase.h"
 #include "if2ip.h"
 /* The last 3 #include files should be in this order */
@@ -162,7 +162,7 @@ if2ip_result_t Curl_if2ip(int af,
               addr =
                 &((struct sockaddr_in *)(void *)iface->ifa_addr)->sin_addr;
             res = IF2IP_FOUND;
-            ip = Curl_inet_ntop(af, addr, ipstr, sizeof(ipstr));
+            ip = curlx_inet_ntop(af, addr, ipstr, sizeof(ipstr));
             msnprintf(buf, buf_size, "%s%s", ip, scope);
             break;
           }
@@ -235,7 +235,7 @@ if2ip_result_t Curl_if2ip(int af,
 
   s = (struct sockaddr_in *)(void *)&req.ifr_addr;
   memcpy(&in, &s->sin_addr, sizeof(in));
-  r = Curl_inet_ntop(s->sin_family, &in, buf, buf_size);
+  r = curlx_inet_ntop(s->sin_family, &in, buf, buf_size);
 
   sclose(dummy);
   if(!r)
index 10e37ec67f43151167affca73e6cb1ce4a949c5e..7c6aa929d108b52819a5c4977d5db7f6e5676e14 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
 #include "imap.h"
 #include "url.h"
 #include "connect.h"
-#include "inet_ntop.h"
 #include "http_ntlm.h"
 #include "curl_rtmp.h"
 #include "gopher.h"
index 6a7ab1a575e53d7fb68b273e048f940aaead99fc..3f67431e570d84ec7ea711bf07c429a9342bc294 100644 (file)
@@ -31,7 +31,7 @@
 #include "escape.h"
 #include "curl_ctype.h"
 #include "curlx/inet_pton.h"
-#include "inet_ntop.h"
+#include "curlx/inet_ntop.h"
 #include "strdup.h"
 #include "idn.h"
 #include "curlx/strparse.h"
@@ -513,7 +513,7 @@ static CURLUcode ipv6_parse(struct Curl_URL *u, char *hostname,
     hostname[hlen] = 0; /* end the address there */
     if(1 != curlx_inet_pton(AF_INET6, hostname, dest))
       return CURLUE_BAD_IPV6;
-    if(Curl_inet_ntop(AF_INET6, dest, hostname, hlen)) {
+    if(curlx_inet_ntop(AF_INET6, dest, hostname, hlen)) {
       hlen = strlen(hostname); /* might be shorter now */
       hostname[hlen + 1] = 0;
     }
index 86f1e1a568bfc413304f414ccb0a2521a34acc64..b6e35bd79d1647523ce433ba461ea6eb0a13819b 100644 (file)
@@ -62,7 +62,6 @@
 #include "../vtls/vtls.h"
 #include "../cfilters.h"
 #include "../connect.h"
-#include "../inet_ntop.h"
 #include "../parsedate.h"          /* for the week day and month names */
 #include "../sockaddr.h"           /* required for Curl_sockaddr_storage */
 #include "../curlx/strparse.h"
index c16b3ac3886512a0c9434039d2b76eee65878f21..bdfc2689c6cae91a3a0b55a8a591af2496420817 100644 (file)
@@ -65,7 +65,6 @@
 #include "../vtls/vtls.h"
 #include "../cfilters.h"
 #include "../connect.h"
-#include "../inet_ntop.h"
 #include "../parsedate.h" /* for the week day and month names */
 #include "../sockaddr.h" /* required for Curl_sockaddr_storage */
 #include "../multiif.h"