Add a new tor_addr_from_getsockname()

We use this pattern all over, and this should simplify matters a
bit.  Part of 18105.
This commit is contained in:
Nick Mathewson 2018-01-26 11:48:45 -05:00
parent 7a74b3663f
commit 2a7bfec364
2 changed files with 20 additions and 0 deletions

View File

@ -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.
*/

View File

@ -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)