Split the socketpair replacement code into its own function for testing

This commit is contained in:
Nick Mathewson 2013-08-02 10:36:01 -04:00
parent b8d9c84037
commit d6adf05582
2 changed files with 24 additions and 2 deletions

View File

@ -23,6 +23,7 @@
* we can also take out the configure check. */ * we can also take out the configure check. */
#define _GNU_SOURCE #define _GNU_SOURCE
#define COMPAT_PRIVATE
#include "compat.h" #include "compat.h"
#ifdef _WIN32 #ifdef _WIN32
@ -1291,6 +1292,18 @@ tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
return 0; return 0;
#else #else
return tor_ersatz_socketpair(family, type, protocol, fd);
#endif
}
#ifdef NEED_ERSATZ_SOCKETPAIR
/**
* Helper used to implement socketpair on systems that lack it, by
* making a direct connection to localhost.
*/
STATIC int
tor_ersatz_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
{
/* 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
* for now, and really, when localhost is down sometimes, we * for now, and really, when localhost is down sometimes, we
@ -1301,7 +1314,7 @@ tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
tor_socket_t acceptor = -1; tor_socket_t acceptor = -1;
struct sockaddr_in listen_addr; struct sockaddr_in listen_addr;
struct sockaddr_in connect_addr; struct sockaddr_in connect_addr;
int size; socklen_t size;
int saved_errno = -1; int saved_errno = -1;
if (protocol if (protocol
@ -1384,8 +1397,8 @@ tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
if (acceptor != -1) if (acceptor != -1)
tor_close_socket(acceptor); tor_close_socket(acceptor);
return -saved_errno; return -saved_errno;
#endif
} }
#endif
/** Number of extra file descriptors to keep in reserve beyond those that we /** Number of extra file descriptors to keep in reserve beyond those that we
* tell Tor it's allowed to use. */ * tell Tor it's allowed to use. */

View File

@ -8,6 +8,7 @@
#include "orconfig.h" #include "orconfig.h"
#include "torint.h" #include "torint.h"
#include "testsupport.h"
#ifdef _WIN32 #ifdef _WIN32
#ifndef _WIN32_WINNT #ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501 #define _WIN32_WINNT 0x0501
@ -738,5 +739,13 @@ char *format_win32_error(DWORD err);
#endif #endif
#ifdef COMPAT_PRIVATE
#if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
#define NEED_ERSATZ_SOCKETPAIR
STATIC int tor_ersatz_socketpair(int family, int type, int protocol,
tor_socket_t fd[2]);
#endif
#endif
#endif #endif