[tor-commits] [tor/master] Add a new tor_addr_from_getsockname()

nickm at torproject.org nickm at torproject.org
Fri Feb 16 01:19:27 UTC 2018


commit 2a7bfec364a30a92c3babe658ece2db1a8a40d03
Author: Nick Mathewson <nickm at torproject.org>
Date:   Fri Jan 26 11:48:45 2018 -0500

    Add a new tor_addr_from_getsockname()
    
    We use this pattern all over, and this should simplify matters a
    bit.  Part of 18105.
---
 src/common/compat.c | 18 ++++++++++++++++++
 src/common/compat.h |  2 ++
 2 files changed, 20 insertions(+)

diff --git a/src/common/compat.c b/src/common/compat.c
index 6a61c6c91..c457e6383 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1392,6 +1392,24 @@ tor_getsockname,(tor_socket_t sock, struct sockaddr *address,
    return getsockname(sock, address, address_len);
 }
 
+/**
+ * Find the local address associated with the socket <b>sock</b>, and
+ * place it in *<b>addr_out</b>.  Return 0 on success, -1 on failure.
+ *
+ * (As tor_getsockname, but instead places the result in a tor_addr_t.) */
+int
+tor_addr_from_getsockname(tor_addr_t *addr_out, tor_socket_t sock)
+{
+  struct sockaddr_storage ss;
+  socklen_t ss_len = sizeof(ss);
+  memset(ss, 0, sizeof(ss));
+
+  if (tor_getsockname(sock, (struct sockaddr *) &ss, &ss_len) < 0)
+    return -1;
+
+  return tor_addr_from_sockaddr(addr_out, (struct sockaddr *)&ss, NULL);
+}
+
 /** Turn <b>socket</b> into a nonblocking socket. Return 0 on success, -1
  * on failure.
  */
diff --git a/src/common/compat.h b/src/common/compat.h
index 93301feda..f7932c914 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -510,6 +510,8 @@ int get_n_open_sockets(void);
 MOCK_DECL(int,
 tor_getsockname,(tor_socket_t socket, struct sockaddr *address,
                  socklen_t *address_len));
+struct tor_addr_t;
+int tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock);
 
 #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
 #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)





More information about the tor-commits mailing list