From 763effb0709c8cd9578393f0a6f20decde675df1 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 12 Jul 2021 21:47:07 +0200 Subject: [PATCH] Convert addresses to text using inet_ntop(). The ESP32 SDK doesn't support getnameinfo(). --- src/netutl.c | 66 +++++++++++++++++++++++++++++++--------------------- src/netutl.h | 1 - 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/netutl.c b/src/netutl.c index 79dadea..40236d7 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -42,7 +42,7 @@ struct addrinfo *str2addrinfo(const char *address, const char *service, int sock err = getaddrinfo(address, service, &hint, &ai); if(err) { - logger(NULL, MESHLINK_WARNING, "Error looking up %s port %s: %s", address, service, err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err)); + logger(NULL, MESHLINK_WARNING, "Error looking up %s port %s: %s", address, service, strerror(errno)); return NULL; } @@ -117,10 +117,9 @@ sockaddr_t str2sockaddr_random(struct meshlink_handle *mesh, const char *address } void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) { - char address[NI_MAXHOST]; - char port[NI_MAXSERV]; - char *scopeid; - int err; + char address[48]; + char port[16]; + bool success; if(sa->sa.sa_family == AF_UNKNOWN) { if(addrstr) { @@ -134,17 +133,25 @@ void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) { return; } - err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); + switch(sa->sa.sa_family) { + case AF_INET: + snprintf(port, sizeof port, "%d", sa->in.sin_port); + success = inet_ntop(AF_INET, &sa->in.sin_addr, address, sizeof(address)); + break; - if(err) { - logger(NULL, MESHLINK_ERROR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err)); - abort(); - } + case AF_INET6: + snprintf(port, sizeof port, "%d", sa->in6.sin6_port); + success = inet_ntop(AF_INET6, &sa->in6.sin6_addr, address, sizeof(address)); + break; - scopeid = strchr(address, '%'); + default: + success = false; + break; + } - if(scopeid) { - *scopeid = '\0'; /* Descope. */ + if(!success) { + logger(NULL, MESHLINK_ERROR, "Error while translating addresses: %s", strerror(errno)); + abort(); } if(addrstr) { @@ -158,19 +165,33 @@ void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) { char *sockaddr2hostname(const sockaddr_t *sa) { char *str; - char address[NI_MAXHOST] = "unknown"; - char port[NI_MAXSERV] = "unknown"; - int err; + char address[48] = "unknown"; + char port[16] = "unknown"; + bool success; if(sa->sa.sa_family == AF_UNKNOWN) { xasprintf(&str, "%s port %s", sa->unknown.address, sa->unknown.port); return str; } - err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); + switch(sa->sa.sa_family) { + case AF_INET: + snprintf(port, sizeof port, "%d", sa->in.sin_port); + success = inet_ntop(AF_INET, &sa->in.sin_addr, address, sizeof(address)); + break; - if(err) { - logger(NULL, MESHLINK_ERROR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err)); + case AF_INET6: + snprintf(port, sizeof port, "%d", sa->in6.sin6_port); + success = inet_ntop(AF_INET6, &sa->in6.sin6_addr, address, sizeof(address)); + break; + + default: + success = false; + break; + } + + if(!success) { + logger(NULL, MESHLINK_ERROR, "Error while resolving address to hostname: %s", strerror(errno)); abort(); } @@ -289,13 +310,6 @@ void sockaddrfree(sockaddr_t *a) { } } -void sockaddrunmap(sockaddr_t *sa) { - if(sa->sa.sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->in6.sin6_addr)) { - sa->in.sin_addr.s_addr = ((uint32_t *) & sa->in6.sin6_addr)[3]; - sa->in.sin_family = AF_INET; - } -} - void packmsg_add_sockaddr(packmsg_output_t *out, const sockaddr_t *sa) { switch(sa->sa.sa_family) { case AF_INET: { diff --git a/src/netutl.h b/src/netutl.h index 69346bc..edb080a 100644 --- a/src/netutl.h +++ b/src/netutl.h @@ -30,7 +30,6 @@ void sockaddr2str(const sockaddr_t *, char **, char **); char *sockaddr2hostname(const sockaddr_t *) __attribute__((__malloc__)); int sockaddrcmp(const sockaddr_t *, const sockaddr_t *) __attribute__((__warn_unused_result__)); int sockaddrcmp_noport(const sockaddr_t *, const sockaddr_t *) __attribute__((__warn_unused_result__)); -void sockaddrunmap(sockaddr_t *); void sockaddrfree(sockaddr_t *); void sockaddrcpy(sockaddr_t *, const sockaddr_t *); void sockaddrcpy_setport(sockaddr_t *, const sockaddr_t *, uint16_t port); -- 2.39.5