Commit 1b11745b55863e0639826e3948807978cdc567b1

Authored by Stéphane Raimbault
1 parent 5a6efecf

Replace inet_ntoa by inet_ptop

- check conversion
- display IP in in IPv6
Showing 1 changed file with 13 additions and 3 deletions
src/modbus-tcp.c
@@ -709,7 +709,12 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) @@ -709,7 +709,12 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
709 } 709 }
710 710
711 if (ctx->debug) { 711 if (ctx->debug) {
712 - printf("The client connection from %s is accepted\n", inet_ntoa(addr.sin_addr)); 712 + char buf[INET_ADDRSTRLEN];
  713 + if (inet_ntop(AF_INET, &(addr.sin_addr), buf, INET_ADDRSTRLEN) == NULL) {
  714 + fprintf(stderr, "Client connection accepted from unparsable IP.\n");
  715 + } else {
  716 + printf("Client connection accepted from %s.\n", buf);
  717 + }
713 } 718 }
714 719
715 return ctx->s; 720 return ctx->s;
@@ -717,7 +722,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s) @@ -717,7 +722,7 @@ int modbus_tcp_accept(modbus_t *ctx, int *s)
717 722
718 int modbus_tcp_pi_accept(modbus_t *ctx, int *s) 723 int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
719 { 724 {
720 - struct sockaddr_storage addr; 725 + struct sockaddr_in6 addr;
721 socklen_t addrlen; 726 socklen_t addrlen;
722 727
723 if (ctx == NULL) { 728 if (ctx == NULL) {
@@ -738,7 +743,12 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s) @@ -738,7 +743,12 @@ int modbus_tcp_pi_accept(modbus_t *ctx, int *s)
738 } 743 }
739 744
740 if (ctx->debug) { 745 if (ctx->debug) {
741 - printf("The client connection is accepted.\n"); 746 + char buf[INET6_ADDRSTRLEN];
  747 + if (inet_ntop(AF_INET6, &(addr.sin6_addr), buf, INET6_ADDRSTRLEN) == NULL) {
  748 + fprintf(stderr, "Client connection accepted from unparsable IP.\n");
  749 + } else {
  750 + printf("Client connection accepted from %s.\n", buf);
  751 + }
742 } 752 }
743 753
744 return ctx->s; 754 return ctx->s;