From: Daniel Stenberg Date: Mon, 9 Jun 2025 07:39:40 +0000 (+0200) Subject: lib: make curlx_inet_ntop() X-Git-Tag: curl-8_15_0~312 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1886260a95f442d947b4d1fc01be95ee55e5f5a1;p=thirdparty%2Fcurl.git lib: make curlx_inet_ntop() move function to curlx/, change all callers Closes #17560 --- diff --git a/lib/Makefile.inc b/lib/Makefile.inc index b384e09623..ecf4ce1115 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -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 \ diff --git a/lib/asyn-thrdd.c b/lib/asyn-thrdd.c index 9cd25dcd20..593ef37175 100644 --- a/lib/asyn-thrdd.c +++ b/lib/asyn-thrdd.c @@ -61,7 +61,6 @@ #include "share.h" #include "url.h" #include "multiif.h" -#include "inet_ntop.h" #include "curl_threads.h" #include "strdup.h" diff --git a/lib/cf-socket.c b/lib/cf-socket.c index e31977201c..f389504707 100644 --- a/lib/cf-socket.c +++ b/lib/cf-socket.c @@ -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" diff --git a/lib/connect.c b/lib/connect.c index 1dcdde3fc5..abf30a641f 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -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; diff --git a/lib/curlx/curlx.h b/lib/curlx/curlx.h index 983c7b5c75..e3598c23a0 100644 --- a/lib/curlx/curlx.h +++ b/lib/curlx/curlx.h @@ -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 */ diff --git a/lib/inet_ntop.c b/lib/curlx/inet_ntop.c similarity index 86% rename from lib/inet_ntop.c rename to lib/curlx/inet_ntop.c index bb2ec57872..e5ff45cb1b 100644 --- a/lib/inet_ntop.c +++ b/lib/curlx/inet_ntop.c @@ -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: diff --git a/lib/inet_ntop.h b/lib/curlx/inet_ntop.h similarity index 87% rename from lib/inet_ntop.h rename to lib/curlx/inet_ntop.h index 9923daaed1..41ec909718 100644 --- a/lib/inet_ntop.h +++ b/lib/curlx/inet_ntop.h @@ -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 #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 diff --git a/lib/ftp.c b/lib/ftp.c index 3dd7fed045..576100be46 100644 --- 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) { diff --git a/lib/hostip.c b/lib/hostip.c index ca6724ed55..6c82ec4218 100644 --- a/lib/hostip.c +++ b/lib/hostip.c @@ -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 diff --git a/lib/if2ip.c b/lib/if2ip.c index 6da68efd50..bc420c98f2 100644 --- a/lib/if2ip.c +++ b/lib/if2ip.c @@ -52,7 +52,7 @@ # include #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) diff --git a/lib/url.c b/lib/url.c index 10e37ec67f..7c6aa929d1 100644 --- a/lib/url.c +++ b/lib/url.c @@ -107,7 +107,6 @@ #include "imap.h" #include "url.h" #include "connect.h" -#include "inet_ntop.h" #include "http_ntlm.h" #include "curl_rtmp.h" #include "gopher.h" diff --git a/lib/urlapi.c b/lib/urlapi.c index 6a7ab1a575..3f67431e57 100644 --- a/lib/urlapi.c +++ b/lib/urlapi.c @@ -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; } diff --git a/lib/vssh/libssh.c b/lib/vssh/libssh.c index 86f1e1a568..b6e35bd79d 100644 --- a/lib/vssh/libssh.c +++ b/lib/vssh/libssh.c @@ -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" diff --git a/lib/vssh/libssh2.c b/lib/vssh/libssh2.c index c16b3ac388..bdfc2689c6 100644 --- a/lib/vssh/libssh2.c +++ b/lib/vssh/libssh2.c @@ -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"