Use tor_addr_from_getsockname() in several places

I'm leaving the getsockname code in transproxy alone, since it is
comparatively isolated, rather platform-specific, and hard to test.

Implements 18105.
This commit is contained in:
Nick Mathewson 2018-01-26 11:59:01 -05:00
parent 2a7bfec364
commit 6ed384b827
4 changed files with 15 additions and 19 deletions

4
changes/18105 Normal file
View File

@ -0,0 +1,4 @@
o Code simplification and refactoring:
- Introduce a function to call getsockname() and return
tor_addr_t, to save a little complexity throughout the codebase.
Closes ticket 18105.

View File

@ -1672,7 +1672,7 @@ get_interface_address6_via_udp_socket_hack,(int severity,
sa_family_t family,
tor_addr_t *addr))
{
struct sockaddr_storage my_addr, target_addr;
struct sockaddr_storage target_addr;
int sock=-1, r=-1;
socklen_t addr_len;
@ -1715,21 +1715,19 @@ get_interface_address6_via_udp_socket_hack,(int severity,
goto err;
}
if (tor_getsockname(sock,(struct sockaddr*)&my_addr, &addr_len)) {
if (tor_addr_from_getsockname(addr, sock) < 0) {
int e = tor_socket_errno(sock);
log_fn(severity, LD_NET, "getsockname() to determine interface failed: %s",
tor_socket_strerror(e));
goto err;
}
if (tor_addr_from_sockaddr(addr, (struct sockaddr*)&my_addr, NULL) == 0) {
if (tor_addr_is_loopback(addr) || tor_addr_is_multicast(addr)) {
log_fn(severity, LD_NET, "Address that we determined via UDP socket"
" magic is unsuitable for public comms.");
} else {
r=0;
}
}
if (tor_addr_is_loopback(addr) || tor_addr_is_multicast(addr)) {
log_fn(severity, LD_NET, "Address that we determined via UDP socket"
" magic is unsuitable for public comms.");
} else {
r=0;
}
err:
if (sock >= 0)

View File

@ -1402,7 +1402,7 @@ 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));
memset(&ss, 0, sizeof(ss));
if (tor_getsockname(sock, (struct sockaddr *) &ss, &ss_len) < 0)
return -1;

View File

@ -1253,15 +1253,12 @@ connection_listener_new(const struct sockaddr *listensockaddr,
gotPort = usePort;
} else {
tor_addr_t addr2;
struct sockaddr_storage ss;
socklen_t ss_len=sizeof(ss);
if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
if (tor_addr_from_getsockname(&addr2, s)<0) {
log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
conn_type_to_string(type),
tor_socket_strerror(tor_socket_errno(s)));
gotPort = 0;
}
tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
}
#ifdef HAVE_SYS_UN_H
/*
@ -4514,8 +4511,6 @@ alloc_http_authenticator(const char *authenticator)
static void
client_check_address_changed(tor_socket_t sock)
{
struct sockaddr_storage out_sockaddr;
socklen_t out_addr_len = (socklen_t) sizeof(out_sockaddr);
tor_addr_t out_addr, iface_addr;
tor_addr_t **last_interface_ip_ptr;
sa_family_t family;
@ -4523,13 +4518,12 @@ client_check_address_changed(tor_socket_t sock)
if (!outgoing_addrs)
outgoing_addrs = smartlist_new();
if (getsockname(sock, (struct sockaddr*)&out_sockaddr, &out_addr_len)<0) {
if (tor_addr_from_getsockname(&out_addr, sock) < 0) {
int e = tor_socket_errno(sock);
log_warn(LD_NET, "getsockname() to check for address change failed: %s",
tor_socket_strerror(e));
return;
}
tor_addr_from_sockaddr(&out_addr, (struct sockaddr*)&out_sockaddr, NULL);
family = tor_addr_family(&out_addr);
if (family == AF_INET)