From 1b11745b55863e0639826e3948807978cdc567b1 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Mon, 28 Nov 2022 00:16:42 +0100 Subject: [PATCH] Replace inet_ntoa by inet_ptop --- src/modbus-tcp.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/modbus-tcp.c b/src/modbus-tcp.c index 410737b..66a5f52 100644 --- a/src/modbus-tcp.c +++ b/src/modbus-tcp.c @@ -709,7 +709,12 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) } if (ctx->debug) { - printf("The client connection from %s is accepted\n", inet_ntoa(addr.sin_addr)); + char buf[INET_ADDRSTRLEN]; + if (inet_ntop(AF_INET, &(addr.sin_addr), buf, INET_ADDRSTRLEN) == NULL) { + fprintf(stderr, "Client connection accepted from unparsable IP.\n"); + } else { + printf("Client connection accepted from %s.\n", buf); + } } return ctx->s; @@ -717,7 +722,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) int modbus_tcp_pi_accept(modbus_t *ctx, int *s) { - struct sockaddr_storage addr; + struct sockaddr_in6 addr; socklen_t addrlen; if (ctx == NULL) { @@ -738,7 +743,12 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s) } if (ctx->debug) { - printf("The client connection is accepted.\n"); + char buf[INET6_ADDRSTRLEN]; + if (inet_ntop(AF_INET6, &(addr.sin6_addr), buf, INET6_ADDRSTRLEN) == NULL) { + fprintf(stderr, "Client connection accepted from unparsable IP.\n"); + } else { + printf("Client connection accepted from %s.\n", buf); + } } return ctx->s; -- libgit2 0.21.4