From 6ab7ff64f07c0031f0779edd5a5a8cfccf2582bb Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Tue, 1 Aug 2023 01:07:59 -0400 Subject: [PATCH] iiod: fix the printing of IP addresses inside iiod when a IP client attaches to the current iiod, it prints out: New client connected from 0.0.0.0 This is because we this was coded when we only supported ipv4, but when we got a ipv6 socket (which is all the time now when we have ipv6 enabled), we didn't allocate the right size of structure, and the client/peer address didn't get decoded/printed out properly. So, fix that, and print out where the clients are at. Tested onm x86, ARM (32-bit and 64-bit) Signed-off-by: Robin Getz Signed-off-by: Paul Cercueil --- iiod/iiod.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/iiod/iiod.c b/iiod/iiod.c index 2cdf0356a..dc1bc0bf2 100644 --- a/iiod/iiod.c +++ b/iiod/iiod.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,12 @@ #define _STRINGIFY(x) #x #define STRINGIFY(x) _STRINGIFY(x) +#ifdef HAVE_IPV6 +#define IP_ADDR_LEN (INET6_ADDRSTRLEN + 1 + IF_NAMESIZE) +#else +#define IP_ADDR_LEN (INET_ADDRSTRLEN + 1 + IF_NAMESIZE) +#endif + static int start_iiod(const char *uri, const char *ffs_mountpoint, const char *uart_params, bool debug, bool interactive, bool use_aio, uint16_t port, unsigned int nb_pipes, @@ -265,7 +272,12 @@ static int main_server(struct iio_context *ctx, bool debug, while (true) { struct client_data *cdata; +#ifdef HAVE_IPV6 + struct sockaddr_in6 caddr; +#else struct sockaddr_in caddr; +#endif + socklen_t addr_len = sizeof(caddr); int new; @@ -330,8 +342,43 @@ static int main_server(struct iio_context *ctx, bool debug, cdata->xml_zstd = xml_zstd; cdata->xml_zstd_len = xml_zstd_len; - IIO_INFO("New client connected from %s\n", - inet_ntoa(caddr.sin_addr)); + if (LOG_LEVEL >= Info_L) { + struct sockaddr_in *caddr4 = (struct sockaddr_in *)&caddr; + char ipaddr[IP_ADDR_LEN]; + int zone = 0; + void *addr; + char *ptr; + + if (!ipv6 || caddr4->sin_family == AF_INET) { + addr = &caddr4->sin_addr; +#ifdef HAVE_IPV6 + } else { + addr = &caddr.sin6_addr; + zone = caddr.sin6_scope_id; +#endif + } + + if (!inet_ntop(caddr4->sin_family, addr, ipaddr, sizeof(ipaddr) - 1)) { + iio_strerror(errno, err_str, sizeof(err_str)); + IIO_ERROR("Error during inet_ntop: %s\n", err_str); + } else { + ipaddr[IP_ADDR_LEN - 1] = '\0'; + + if (zone) { + ptr = &ipaddr[strnlen(ipaddr, IP_ADDR_LEN)]; + + if (if_indextoname(zone, ptr + 1)) + *ptr = '%'; + } + + if (!strncmp(ipaddr, "::ffff:", sizeof("::ffff:") - 1)) + ptr = &ipaddr[sizeof("::ffff:") - 1]; + else + ptr = ipaddr; + + IIO_INFO("New client connected from %s\n", ptr); + } + } ret = thread_pool_add_thread(main_thread_pool, client_thd, cdata, "net_client_thd"); if (ret) {