lib: make curlx_inet_ntop()

move function to curlx/, change all callers

Closes #17560
This commit is contained in:
Daniel Stenberg 2025-06-09 09:39:40 +02:00
parent c347b43e5c
commit 1886260a95
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
14 changed files with 46 additions and 33 deletions

View 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 \

View 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"

View 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"

View 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;

View 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 */

View 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:

View 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

View file

@ -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) {

View 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

View 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)

View file

@ -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"

View 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;
}

View 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"

View 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"