use sockaddr_storage for stack-allocated sockets in ersatz socketpair

This commit is contained in:
Nick Mathewson 2015-11-27 11:52:59 -05:00
parent f108be7c25
commit eedef41944

View File

@ -1516,19 +1516,20 @@ tor_ersatz_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
tor_socket_t connector = TOR_INVALID_SOCKET;
tor_socket_t acceptor = TOR_INVALID_SOCKET;
tor_addr_t listen_tor_addr;
struct sockaddr listen_addr;
struct sockaddr_storage connect_addr_ss, listen_addr_ss;
struct sockaddr *listen_addr = (struct sockaddr *) &listen_addr_ss;
uint16_t listen_port = 0;
tor_addr_t connect_tor_addr;
uint16_t connect_port = 0;
struct sockaddr connect_addr;
struct sockaddr *connect_addr = (struct sockaddr *) &connect_addr_ss;
socklen_t size;
int saved_errno = -1;
int ersatz_domain = AF_INET;
memset(&connect_tor_addr, 0, sizeof(connect_tor_addr));
memset(&connect_addr, 0, sizeof(connect_addr));
memset(&connect_addr_ss, 0, sizeof(connect_addr_ss));
memset(&listen_tor_addr, 0, sizeof(listen_tor_addr));
memset(&listen_addr, 0, sizeof(listen_addr));
memset(&listen_addr_ss, 0, sizeof(listen_addr_ss));
if (protocol
#ifdef AF_UNIX
@ -1573,11 +1574,11 @@ tor_ersatz_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
tor_addr_parse(&listen_tor_addr, "[::1]");
}
tor_assert(tor_addr_is_loopback(&listen_tor_addr));
tor_addr_to_sockaddr(&listen_tor_addr,
size = tor_addr_to_sockaddr(&listen_tor_addr,
0 /* kernel chooses port. */,
&listen_addr,
sizeof(listen_addr));
if (bind(listener, &listen_addr, sizeof (listen_addr)) == -1)
listen_addr,
sizeof(listen_addr_ss));
if (bind(listener, listen_addr, size) == -1)
goto tidy_up_and_fail;
if (listen(listener, 1) == -1)
goto tidy_up_and_fail;
@ -1586,28 +1587,28 @@ tor_ersatz_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
if (!SOCKET_OK(connector))
goto tidy_up_and_fail;
/* We want to find out the port number to connect to. */
size = sizeof(connect_addr);
if (getsockname(listener, &connect_addr, &size) == -1)
size = sizeof(connect_addr_ss);
if (getsockname(listener, connect_addr, &size) == -1)
goto tidy_up_and_fail;
if (size != SIZEOF_SOCKADDR (connect_addr.sa_family))
if (size != SIZEOF_SOCKADDR (connect_addr->sa_family))
goto abort_tidy_up_and_fail;
if (connect(connector, &connect_addr, sizeof(connect_addr)) == -1)
if (connect(connector, connect_addr, size) == -1)
goto tidy_up_and_fail;
size = sizeof(listen_addr);
acceptor = tor_accept_socket(listener, &listen_addr, &size);
size = sizeof(listen_addr_ss);
acceptor = tor_accept_socket(listener, listen_addr, &size);
if (!SOCKET_OK(acceptor))
goto tidy_up_and_fail;
if (size != SIZEOF_SOCKADDR(listen_addr.sa_family))
if (size != SIZEOF_SOCKADDR(listen_addr->sa_family))
goto abort_tidy_up_and_fail;
/* Now check we are talking to ourself by matching port and host on the
two sockets. */
if (getsockname(connector, &connect_addr, &size) == -1)
if (getsockname(connector, connect_addr, &size) == -1)
goto tidy_up_and_fail;
/* Set *_tor_addr and *_port to the address and port that was used */
tor_addr_from_sockaddr(&listen_tor_addr, &listen_addr, &listen_port);
tor_addr_from_sockaddr(&connect_tor_addr, &connect_addr, &connect_port);
if (size != SIZEOF_SOCKADDR (connect_addr.sa_family)
tor_addr_from_sockaddr(&listen_tor_addr, listen_addr, &listen_port);
tor_addr_from_sockaddr(&connect_tor_addr, connect_addr, &connect_port);
if (size != SIZEOF_SOCKADDR (connect_addr->sa_family)
|| tor_addr_compare(&listen_tor_addr, &connect_tor_addr, CMP_SEMANTIC)
|| listen_port != connect_port) {
goto abort_tidy_up_and_fail;