From: Volker Lendecke Date: Sun, 10 May 2026 18:10:22 +0000 (+0200) Subject: lib: Convert get_peer_addr() to use ssaddr_buf X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0e83c68abe7fccd90480531a08bc19635e29c040;p=thirdparty%2Fsamba.git lib: Convert get_peer_addr() to use ssaddr_buf While there remove get_peer_addr_internal, returning the sockaddr was not used. Signed-off-by: Volker Lendecke Reviewed-by: Anoop C S --- diff --git a/source3/include/proto.h b/source3/include/proto.h index 208298aa7cb..c6725efa894 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -507,7 +507,7 @@ struct tevent_req *open_socket_out_send(TALLOC_CTX *mem_ctx, void (*after_connect)(int fd, void *private_data), void *private_data); NTSTATUS open_socket_out_recv(struct tevent_req *req, int *pfd); -const char *get_peer_addr(int fd, char *addr, size_t addr_len); +const char *get_peer_addr(int fd, struct ssaddr_buf *buf); struct tsocket_address; diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index 3c09b85261e..ce6d646b9df 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -559,45 +559,6 @@ NTSTATUS open_socket_out(const struct sockaddr_storage *pss, uint16_t port, return status; } -/******************************************************************* - Return the IP addr of the remote end of a socket as a string. - Optionally return the struct sockaddr_storage. - ******************************************************************/ - -static const char *get_peer_addr_internal(int fd, - char *addr_buf, - size_t addr_buf_len, - struct sockaddr *pss, - socklen_t *plength) -{ - struct sockaddr_storage ss; - socklen_t length = sizeof(ss); - - strlcpy(addr_buf,"0.0.0.0",addr_buf_len); - - if (fd == -1) { - return addr_buf; - } - - if (pss == NULL) { - pss = (struct sockaddr *)&ss; - plength = &length; - } - - if (getpeername(fd, (struct sockaddr *)pss, plength) < 0) { - int level = (errno == ENOTCONN) ? 2 : 0; - DEBUG(level, ("getpeername failed. Error was %s\n", - strerror(errno))); - return addr_buf; - } - - print_sockaddr_len(addr_buf, - addr_buf_len, - pss, - *plength); - return addr_buf; -} - /******************************************************************* Matchname - determine if host name matches IP address. Used to confirm a hostname lookup to prevent spoof attacks. @@ -725,9 +686,28 @@ static void store_nc(const struct name_addr_pair *nc) Return the IP addr of the remote end of a socket as a string. ******************************************************************/ -const char *get_peer_addr(int fd, char *addr, size_t addr_len) +const char *get_peer_addr(int fd, struct ssaddr_buf *buf) { - return get_peer_addr_internal(fd, addr, addr_len, NULL, NULL); + struct samba_sockaddr addr = { + .sa_socklen = sizeof(struct sockaddr_storage), + }; + int ret; + + (void)strlcpy(buf->buf, "0.0.0.0", sizeof(buf->buf)); + + if (fd == -1) { + return buf->buf; + } + + ret = getpeername(fd, &addr.u.sa, &addr.sa_socklen); + if (ret == -1) { + int level = (errno == ENOTCONN) ? 2 : 0; + DEBUG(level, ("getpeername failed. Error was %s\n", + strerror(errno))); + return buf->buf; + } + + return ssaddr_str_buf(&addr, buf); } int get_remote_hostname(const struct tsocket_address *remote_address, diff --git a/source3/smbd/smb1_reply.c b/source3/smbd/smb1_reply.c index cd12dbc143b..e506ca2109d 100644 --- a/source3/smbd/smb1_reply.c +++ b/source3/smbd/smb1_reply.c @@ -3833,13 +3833,13 @@ static NTSTATUS read_smb_length(int fd, char *inbuf, unsigned int timeout, status = read_smb_length_return_keepalive(fd, inbuf, timeout, len); if (!NT_STATUS_IS_OK(status)) { - char addr[INET6_ADDRSTRLEN]; + struct ssaddr_buf buf; /* Try and give an error message * saying what client failed. */ - DEBUG(0, ("read_smb_length_return_keepalive failed for " - "client %s read error = %s.\n", - get_peer_addr(fd,addr,sizeof(addr)), - nt_errstr(status))); + DBG_ERR("read_smb_length_return_keepalive failed for " + "client %s read error = %s.\n", + get_peer_addr(fd, &buf), + nt_errstr(status)); return status; } diff --git a/source3/smbd/smb2_process.c b/source3/smbd/smb2_process.c index f16a1496438..de22a5b678a 100644 --- a/source3/smbd/smb2_process.c +++ b/source3/smbd/smb2_process.c @@ -111,11 +111,11 @@ NTSTATUS read_packet_remainder(int fd, char *buffer, status = read_fd_with_timeout(fd, buffer, len, len, timeout, NULL); if (!NT_STATUS_IS_OK(status)) { - char addr[INET6_ADDRSTRLEN]; - DEBUG(0, ("read_fd_with_timeout failed for client %s read " - "error = %s.\n", - get_peer_addr(fd, addr, sizeof(addr)), - nt_errstr(status))); + struct ssaddr_buf buf; + DBG_ERR("read_fd_with_timeout failed for client %s read " + "error = %s.\n", + get_peer_addr(fd, &buf), + nt_errstr(status)); } return status; }