src/socket_wrapper.c: pretty print ip addresses in tracing output.
authorStefan Metzmacher <metze@samba.org>
Mon, 16 Jan 2023 15:50:32 +0000 (16:50 +0100)
committerAndreas Schneider <asn@samba.org>
Tue, 17 Jan 2023 15:19:47 +0000 (16:19 +0100)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
src/socket_wrapper.c

index bedda07a72b939cb8a302b40bca174d048925009..4c03e1cdd4984430c8d62681ed4b80d6035e74d6 100644 (file)
@@ -1429,6 +1429,55 @@ static size_t socket_length(int family)
        return 0;
 }
 
+struct swrap_sockaddr_buf {
+       char str[128];
+};
+
+static const char *swrap_sockaddr_string(struct swrap_sockaddr_buf *buf,
+                                        const struct sockaddr *saddr)
+{
+       unsigned int port = 0;
+       char addr[64] = {0,};
+
+       switch (saddr->sa_family) {
+       case AF_INET: {
+               const struct sockaddr_in *in =
+                   (const struct sockaddr_in *)(const void *)saddr;
+
+               port = ntohs(in->sin_port);
+
+               inet_ntop(saddr->sa_family,
+                         &in->sin_addr,
+                         addr, sizeof(addr));
+               break;
+       }
+#ifdef HAVE_IPV6
+       case AF_INET6: {
+               const struct sockaddr_in6 *in6 =
+                   (const struct sockaddr_in6 *)(const void *)saddr;
+
+               port = ntohs(in6->sin6_port);
+
+               inet_ntop(saddr->sa_family,
+                         &in6->sin6_addr,
+                         addr, sizeof(addr));
+               break;
+       }
+#endif
+       default:
+               snprintf(addr, sizeof(addr),
+                        "<Unknown address family %u>",
+                        saddr->sa_family);
+               break;
+       }
+
+       snprintf(buf->str, sizeof(buf->str),
+                "addr[%s]/port[%u]",
+                addr, port);
+
+       return buf->str;
+}
+
 static struct socket_info *swrap_get_socket_info(int si_index)
 {
        return (struct socket_info *)(&(sockets[si_index].info));
@@ -2064,13 +2113,10 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
                        type = u_type;
                        iface = (addr & 0x000000FF);
                } else {
-                       char str[256] = {0,};
-                       inet_ntop(inaddr->sa_family,
-                                 &in->sin_addr,
-                                 str, sizeof(str));
+                       struct swrap_sockaddr_buf buf = {};
                        SWRAP_LOG(SWRAP_LOG_WARN,
-                                 "str[%s] prt[%u]",
-                                 str, (unsigned)prt);
+                                 "%s",
+                                 swrap_sockaddr_string(&buf, inaddr));
                        errno = ENETUNREACH;
                        return -1;
                }
@@ -2106,13 +2152,10 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
                if (IN6_ARE_ADDR_EQUAL(&cmp1, &cmp2)) {
                        iface = in->sin6_addr.s6_addr[15];
                } else {
-                       char str[256] = {0,};
-                       inet_ntop(inaddr->sa_family,
-                                 &in->sin6_addr,
-                                 str, sizeof(str));
+                       struct swrap_sockaddr_buf buf = {};
                        SWRAP_LOG(SWRAP_LOG_WARN,
-                                 "str[%s] prt[%u]",
-                                 str, (unsigned)prt);
+                                 "%s",
+                                 swrap_sockaddr_string(&buf, inaddr));
                        errno = ENETUNREACH;
                        return -1;
                }
@@ -3984,6 +4027,7 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr,
                .sa_socklen = sizeof(struct sockaddr_un),
        };
        struct socket_info *si = find_socket_info(s);
+       struct swrap_sockaddr_buf buf = {};
        int bcast = 0;
 
        if (!si) {
@@ -4032,7 +4076,8 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr,
        }
 
        SWRAP_LOG(SWRAP_LOG_TRACE,
-                 "connect() path=%s, fd=%d",
+                 "connect(%s) path=%s, fd=%d",
+                 swrap_sockaddr_string(&buf, serv_addr),
                  un_addr.sa.un.sun_path, s);
 
 
@@ -4098,6 +4143,7 @@ static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
                .sa_socklen = sizeof(struct sockaddr_un),
        };
        struct socket_info *si = find_socket_info(s);
+       struct swrap_sockaddr_buf buf = {};
        int bind_error = 0;
 #if 0 /* FIXME */
        bool in_use;
@@ -4187,7 +4233,8 @@ static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
        ret = libc_bind(s, &un_addr.sa.s, un_addr.sa_socklen);
 
        SWRAP_LOG(SWRAP_LOG_TRACE,
-                 "bind() path=%s, fd=%d",
+                 "bind(%s) path=%s, fd=%d",
+                 swrap_sockaddr_string(&buf, myaddr),
                  un_addr.sa.un.sun_path, s);
 
        if (ret == 0) {