浏览代码

Replace inet_ntoa by inet_ptop

- check conversion
- display IP in in IPv6
Stéphane Raimbault 2 年之前
父节点
当前提交
1b11745b55
共有 1 个文件被更改,包括 13 次插入3 次删除
  1. 13 3
      src/modbus-tcp.c

+ 13 - 3
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;