summaryrefslogtreecommitdiff
path: root/src/common/compat.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-01-26 11:48:45 -0500
committerNick Mathewson <nickm@torproject.org>2018-01-26 12:07:37 -0500
commit2a7bfec364a30a92c3babe658ece2db1a8a40d03 (patch)
treef83363c3e6a6551d671f39caa8450197f90e439a /src/common/compat.c
parent7a74b3663fdaa40fc84e48990d15953a8f46a2bf (diff)
downloadtor-2a7bfec364a30a92c3babe658ece2db1a8a40d03.tar.gz
tor-2a7bfec364a30a92c3babe658ece2db1a8a40d03.zip
Add a new tor_addr_from_getsockname()
We use this pattern all over, and this should simplify matters a bit. Part of 18105.
Diffstat (limited to 'src/common/compat.c')
-rw-r--r--src/common/compat.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 6a61c6c919..c457e6383f 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.
*/