]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cli/show_fd: report local and report ports when known
authorWilly Tarreau <w@1wt.eu>
Fri, 5 Feb 2021 09:54:52 +0000 (10:54 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 5 Feb 2021 09:58:03 +0000 (10:58 +0100)
FD dumps are not always easy to match against netstat dumps, and often
require an lsof as a third dump. Let's emit the socket family, and the
local and remore ports when the FD is an IPv4/IPv6 socket, this will
significantly ease the matching.

src/cli.c

index 48146630f18d099d09df2206147a25583a21ba8c..bfce9a60b448e83f5b3a93298030b0379567e0d6 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -1102,6 +1102,27 @@ static int cli_io_handler_show_fd(struct appctx *appctx)
                        if (conn->handle.fd != fd) {
                                chunk_appendf(&trash, " fd=%d(BOGUS)", conn->handle.fd);
                                suspicious = 1;
+                       } else {
+                               struct sockaddr_storage sa;
+                               socklen_t salen;
+
+                               salen = sizeof(sa);
+                               if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
+                                       if (sa.ss_family == AF_INET)
+                                               chunk_appendf(&trash, " fam=ipv4 lport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
+                                       else if (sa.ss_family == AF_INET6)
+                                               chunk_appendf(&trash, " fam=ipv6 lport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
+                                       else if (sa.ss_family == AF_UNIX)
+                                               chunk_appendf(&trash, " fam=unix");
+                               }
+
+                               salen = sizeof(sa);
+                               if (getpeername(fd, (struct sockaddr *)&sa, &salen) != -1) {
+                                       if (sa.ss_family == AF_INET)
+                                               chunk_appendf(&trash, " rport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
+                                       else if (sa.ss_family == AF_INET6)
+                                               chunk_appendf(&trash, " rport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
+                               }
                        }
 
                        if (px)
@@ -1130,9 +1151,22 @@ static int cli_io_handler_show_fd(struct appctx *appctx)
                        }
                }
                else if (fdt.iocb == sock_accept_iocb) {
+                       struct sockaddr_storage sa;
+                       socklen_t salen;
+
                        chunk_appendf(&trash, ") l.st=%s fe=%s",
                                      listener_state_str(li),
                                      li->bind_conf->frontend->id);
+
+                       salen = sizeof(sa);
+                       if (getsockname(fd, (struct sockaddr *)&sa, &salen) != -1) {
+                               if (sa.ss_family == AF_INET)
+                                       chunk_appendf(&trash, " fam=ipv4 lport=%d", ntohs(((const struct sockaddr_in *)&sa)->sin_port));
+                               else if (sa.ss_family == AF_INET6)
+                                       chunk_appendf(&trash, " fam=ipv6 lport=%d", ntohs(((const struct sockaddr_in6 *)&sa)->sin6_port));
+                               else if (sa.ss_family == AF_UNIX)
+                                       chunk_appendf(&trash, " fam=unix");
+                       }
                }
                else
                        chunk_appendf(&trash, ")");