From a6a4717d39d219e24f5a885c2878674024de478d Mon Sep 17 00:00:00 2001 From: Shreyas Mahangade Date: Fri, 17 Jan 2025 23:50:30 +0530 Subject: [PATCH] Print Source address and port details in verbose logging - Helpful for debugging when isolating ssh related issues and where high amount of ssh traffic is observed - Before this source port can only be seen either via strace or simultaneous netstat --- sshconnect.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/sshconnect.c b/sshconnect.c index c86182d1367..2f3679c529e 100644 --- a/sshconnect.c +++ b/sshconnect.c @@ -453,6 +453,8 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop, int oerrno, sock = -1, attempt; char ntop[NI_MAXHOST], strport[NI_MAXSERV]; struct addrinfo *ai; + struct sockaddr_storage local_addr; + socklen_t addr_len = sizeof(local_addr); debug3_f("entering"); memset(ntop, 0, sizeof(ntop)); @@ -529,6 +531,30 @@ ssh_connect_direct(struct ssh *ssh, const char *host, struct addrinfo *aitop, debug("Connection established."); + /*print Local Address and port once connection is established*/ + if (getsockname(sock, (struct sockaddr *)&local_addr, &addr_len) == 0) { + char addr_str[INET6_ADDRSTRLEN]; + int source_port = 0; + switch(local_addr.ss_family) { + case AF_INET: + struct sockaddr_in *local_addr_in = (struct sockaddr_in *)&local_addr; + inet_ntop(AF_INET, &(local_addr_in->sin_addr), addr_str, sizeof(addr_str)); + source_port = ntohs(local_addr_in->sin_port); + break; + case AF_INET6: + struct sockaddr_in6 *local_addr_in6 = (struct sockaddr_in6 *)&local_addr; + inet_ntop(AF_INET6, &(local_addr_in6->sin6_addr), addr_str, sizeof(addr_str)); + source_port = ntohs(local_addr_in6->sin6_port); + break; + default: + strncpy(addr_str, "unknown", sizeof(addr_str)); + } + + debug3("Connection from [%s] port %d", addr_str, source_port); + } else { + debug("Unable to retrieve source address and port: %s", strerror(errno)); + } + /* Set SO_KEEPALIVE if requested. */ if (want_keepalive && setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,