mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-13 06:33:44 +01:00
Make errors retrievable from tor_socketpair; resolve bug 163.
svn:r4509
This commit is contained in:
parent
584a0ae397
commit
d85bfe83bf
@ -294,15 +294,17 @@ set_socket_nonblocking(int socket)
|
|||||||
* be able to read while localhost is down later (the socket pair may
|
* be able to read while localhost is down later (the socket pair may
|
||||||
* even close, depending on OS-specific timeouts).
|
* even close, depending on OS-specific timeouts).
|
||||||
*
|
*
|
||||||
* XXX Bug: this function assumes errno is how you report errors, but
|
* Returns 0 on success and -errno on failure; do not rely on the value
|
||||||
* that isn't the case for Windows, which is where it's most likely
|
* of errno or WSAGetLastSocketError().
|
||||||
* to be called.
|
|
||||||
**/
|
**/
|
||||||
|
/* It would be nicer just to set errno, but that won't work for windows. */
|
||||||
int
|
int
|
||||||
tor_socketpair(int family, int type, int protocol, int fd[2])
|
tor_socketpair(int family, int type, int protocol, int fd[2])
|
||||||
{
|
{
|
||||||
#ifdef HAVE_SOCKETPAIR
|
#ifdef HAVE_SOCKETPAIR
|
||||||
return socketpair(family, type, protocol, fd);
|
int r;
|
||||||
|
r = socketpair(family, type, protocol, fd);
|
||||||
|
return r < 0 ? -errno : r;
|
||||||
#else
|
#else
|
||||||
/* This socketpair does not work when localhost is down. So
|
/* This socketpair does not work when localhost is down. So
|
||||||
* it's really not the same thing at all. But it's close enough
|
* it's really not the same thing at all. But it's close enough
|
||||||
@ -315,6 +317,7 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
|
|||||||
struct sockaddr_in listen_addr;
|
struct sockaddr_in listen_addr;
|
||||||
struct sockaddr_in connect_addr;
|
struct sockaddr_in connect_addr;
|
||||||
int size;
|
int size;
|
||||||
|
int saved_errno = -1;
|
||||||
|
|
||||||
if (protocol
|
if (protocol
|
||||||
#ifdef AF_UNIX
|
#ifdef AF_UNIX
|
||||||
@ -322,24 +325,22 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
|
|||||||
#endif
|
#endif
|
||||||
) {
|
) {
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
errno = WSAEAFNOSUPPORT;
|
return -WSAEAFNOSUPPORT;
|
||||||
#else
|
#else
|
||||||
errno = EAFNOSUPPORT;
|
return -EAFNOSUPPORT;
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
if (!fd) {
|
if (!fd) {
|
||||||
errno = EINVAL;
|
return -EINVAL;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listener = socket(AF_INET, type, 0);
|
listener = socket(AF_INET, type, 0);
|
||||||
if (listener == -1)
|
if (listener == -1)
|
||||||
return -1;
|
return -tor_socket_errno(-1);
|
||||||
if (!SOCKET_IS_POLLABLE(listener)) {
|
if (!SOCKET_IS_POLLABLE(listener)) {
|
||||||
log_fn(LOG_WARN, "Too many connections; can't open socketpair");
|
log_fn(LOG_WARN, "Too many connections; can't open socketpair");
|
||||||
tor_close_socket(listener);
|
tor_close_socket(listener);
|
||||||
return -1;
|
return -ENCONN;
|
||||||
}
|
}
|
||||||
memset(&listen_addr, 0, sizeof(listen_addr));
|
memset(&listen_addr, 0, sizeof(listen_addr));
|
||||||
listen_addr.sin_family = AF_INET;
|
listen_addr.sin_family = AF_INET;
|
||||||
@ -347,13 +348,13 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
|
|||||||
listen_addr.sin_port = 0; /* kernel chooses port. */
|
listen_addr.sin_port = 0; /* kernel chooses port. */
|
||||||
if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
|
if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
|
||||||
== -1)
|
== -1)
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
if (listen(listener, 1) == -1)
|
if (listen(listener, 1) == -1)
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
|
|
||||||
connector = socket(AF_INET, type, 0);
|
connector = socket(AF_INET, type, 0);
|
||||||
if (connector == -1)
|
if (connector == -1)
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
if (!SOCKET_IS_POLLABLE(connector)) {
|
if (!SOCKET_IS_POLLABLE(connector)) {
|
||||||
log_fn(LOG_WARN, "Too many connections; can't open socketpair");
|
log_fn(LOG_WARN, "Too many connections; can't open socketpair");
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
@ -361,33 +362,33 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
|
|||||||
/* We want to find out the port number to connect to. */
|
/* We want to find out the port number to connect to. */
|
||||||
size = sizeof(connect_addr);
|
size = sizeof(connect_addr);
|
||||||
if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
|
if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
if (size != sizeof (connect_addr))
|
if (size != sizeof (connect_addr))
|
||||||
goto abort_tidy_up_and_fail;
|
goto abort_tidy_up_and_fail;
|
||||||
if (connect(connector, (struct sockaddr *) &connect_addr,
|
if (connect(connector, (struct sockaddr *) &connect_addr,
|
||||||
sizeof(connect_addr)) == -1)
|
sizeof(connect_addr)) == -1)
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
|
|
||||||
size = sizeof(listen_addr);
|
size = sizeof(listen_addr);
|
||||||
acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
|
acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
|
||||||
if (acceptor == -1)
|
if (acceptor == -1)
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
if (!SOCKET_IS_POLLABLE(acceptor)) {
|
if (!SOCKET_IS_POLLABLE(acceptor)) {
|
||||||
log_fn(LOG_WARN, "Too many connections; can't open socketpair");
|
log_fn(LOG_WARN, "Too many connections; can't open socketpair");
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
}
|
}
|
||||||
if (size != sizeof(listen_addr))
|
if (size != sizeof(listen_addr))
|
||||||
goto abort_tidy_up_and_fail;
|
goto abort_tidy_up_and_fail;
|
||||||
tor_close_socket(listener);
|
tor_close_socket(listener);
|
||||||
/* Now check we are talking to ourself by matching port and host on the
|
/* Now check we are talking to ourself by matching port and host on the
|
||||||
two sockets. */
|
two sockets. */
|
||||||
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
|
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
|
||||||
goto tidy_up_and_fail;
|
goto tidy_up_and_fail;
|
||||||
if (size != sizeof (connect_addr)
|
if (size != sizeof (connect_addr)
|
||||||
|| listen_addr.sin_family != connect_addr.sin_family
|
|| listen_addr.sin_family != connect_addr.sin_family
|
||||||
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|
||||||
|| listen_addr.sin_port != connect_addr.sin_port) {
|
|| listen_addr.sin_port != connect_addr.sin_port) {
|
||||||
goto abort_tidy_up_and_fail;
|
goto abort_tidy_up_and_fail;
|
||||||
}
|
}
|
||||||
fd[0] = connector;
|
fd[0] = connector;
|
||||||
fd[1] = acceptor;
|
fd[1] = acceptor;
|
||||||
@ -396,22 +397,20 @@ tor_socketpair(int family, int type, int protocol, int fd[2])
|
|||||||
|
|
||||||
abort_tidy_up_and_fail:
|
abort_tidy_up_and_fail:
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
errno = WSAECONNABORTED;
|
saved_errno = WSAECONNABORTED;
|
||||||
#else
|
#else
|
||||||
errno = ECONNABORTED; /* I hope this is portable and appropriate. */
|
saved_errno = ECONNABORTED; /* I hope this is portable and appropriate. */
|
||||||
#endif
|
#endif
|
||||||
tidy_up_and_fail:
|
tidy_up_and_fail:
|
||||||
{
|
if (saved_errno < 0)
|
||||||
int save_errno = errno;
|
saved_errno = errno;
|
||||||
if (listener != -1)
|
if (listener != -1)
|
||||||
tor_close_socket(listener);
|
tor_close_socket(listener);
|
||||||
if (connector != -1)
|
if (connector != -1)
|
||||||
tor_close_socket(connector);
|
tor_close_socket(connector);
|
||||||
if (acceptor != -1)
|
if (acceptor != -1)
|
||||||
tor_close_socket(acceptor);
|
tor_close_socket(acceptor);
|
||||||
errno = save_errno;
|
return -saved_errno;
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1207,12 +1207,13 @@ connection_ap_make_bridge(char *address, uint16_t port)
|
|||||||
{
|
{
|
||||||
int fd[2];
|
int fd[2];
|
||||||
connection_t *conn;
|
connection_t *conn;
|
||||||
|
int err;
|
||||||
|
|
||||||
log_fn(LOG_INFO,"Making AP bridge to %s:%d ...",safe_str(address),port);
|
log_fn(LOG_INFO,"Making AP bridge to %s:%d ...",safe_str(address),port);
|
||||||
|
|
||||||
if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
|
if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) < 0) {
|
||||||
log(LOG_WARN,"Couldn't construct socketpair (%s). Network down? Delaying.",
|
log(LOG_WARN,"Couldn't construct socketpair (%s). Network down? Delaying.",
|
||||||
tor_socket_strerror(tor_socket_errno(-1)));
|
tor_socket_strerror(-err));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,11 +301,11 @@ spawn_cpuworker(void)
|
|||||||
int *fdarray;
|
int *fdarray;
|
||||||
int fd;
|
int fd;
|
||||||
connection_t *conn;
|
connection_t *conn;
|
||||||
|
int err;
|
||||||
|
|
||||||
fdarray = tor_malloc(sizeof(int)*2);
|
fdarray = tor_malloc(sizeof(int)*2);
|
||||||
if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray) < 0) {
|
if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
|
||||||
log(LOG_ERR, "Couldn't construct socketpair: %s",
|
log(LOG_ERR, "Couldn't construct socketpair: %s", tor_socket_strerror(-err));
|
||||||
tor_socket_strerror(tor_socket_errno(-1)));
|
|
||||||
tor_cleanup();
|
tor_cleanup();
|
||||||
tor_free(fdarray);
|
tor_free(fdarray);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -824,11 +824,11 @@ spawn_dnsworker(void)
|
|||||||
int *fdarray;
|
int *fdarray;
|
||||||
int fd;
|
int fd;
|
||||||
connection_t *conn;
|
connection_t *conn;
|
||||||
|
int err;
|
||||||
|
|
||||||
fdarray = tor_malloc(sizeof(int)*2);
|
fdarray = tor_malloc(sizeof(int)*2);
|
||||||
if (tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray) < 0) {
|
if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
|
||||||
log(LOG_ERR, "Couldn't construct socketpair: %s",
|
log(LOG_ERR, "Couldn't construct socketpair: %s", tor_socket_strerror(-err));
|
||||||
tor_socket_strerror(tor_socket_errno(-1)));
|
|
||||||
tor_cleanup();
|
tor_cleanup();
|
||||||
tor_free(fdarray);
|
tor_free(fdarray);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
Loading…
Reference in New Issue
Block a user