]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Convert get_peer_addr() to use ssaddr_buf
authorVolker Lendecke <vl@samba.org>
Sun, 10 May 2026 18:10:22 +0000 (20:10 +0200)
committerAnoop C S <anoopcs@samba.org>
Wed, 17 Jun 2026 08:28:32 +0000 (08:28 +0000)
While there remove get_peer_addr_internal, returning the sockaddr was
not used.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Anoop C S <anoopcs@samba.org>
source3/include/proto.h
source3/lib/util_sock.c
source3/smbd/smb1_reply.c
source3/smbd/smb2_process.c

index 208298aa7cb1065dd9836dfd2609e5729cf2c11c..c6725efa894a913352653e7fdb7824fa7a6e0631 100644 (file)
@@ -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;
 
index 3c09b85261e93b3003d17b168c00841776bcfe23..ce6d646b9df0da072298f2dc3518437e8ff3056c 100644 (file)
@@ -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,
index cd12dbc143b14a1cc67ab1cbd38bbc74ca3cbb7a..e506ca2109d82a6063f23d9c898e75becd62d48f 100644 (file)
@@ -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;
                }
 
index f16a14964386e0e2f84360baf3be20a16183baa3..de22a5b678a3000441ef626b0c4a0dff969d4804 100644 (file)
@@ -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;
 }