From 57b30cd7ccf7aca1d89812389a1e320d012f7a9e Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 6 Aug 2010 13:01:32 -0400 Subject: [PATCH] Turn our socketpair() replacement into its own function This patch splits the formerly windows-only case of evutil_socketpair() into an (internal-use-only) function named evutil_ersatz_socketpair(), and makes it build and work right on non-Windows hosts. We need this for convenience to test sendfile on solaris, where socketpair can't give you an AF_INET pair, and sendfile() won't work on AF_UNIX. --- evutil.c | 25 +++++++++++++++++++------ util-internal.h | 2 ++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/evutil.c b/evutil.c index 409a5d84..3b04316c 100644 --- a/evutil.c +++ b/evutil.c @@ -145,6 +145,14 @@ evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2]) #ifndef WIN32 return socketpair(family, type, protocol, fd); #else + return evutil_ersatz_socketpair(family, type, protocol, fd); +#endif +} + +int +evutil_ersatz_socketpair(int family, int type, int protocol, + evutil_socket_t fd[2]) +{ /* This code is originally from Tor. Used with permission. */ /* This socketpair does not work when localhost is down. So @@ -152,12 +160,17 @@ evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2]) * for now, and really, when localhost is down sometimes, we * have other problems too. */ +#ifdef WIN32 +#define ERR(e) WSA##e +#else +#define ERR(e) e +#endif evutil_socket_t listener = -1; evutil_socket_t connector = -1; evutil_socket_t acceptor = -1; struct sockaddr_in listen_addr; struct sockaddr_in connect_addr; - int size; + ev_socklen_t size; int saved_errno = -1; if (protocol @@ -166,11 +179,11 @@ evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2]) && family != AF_UNIX #endif )) { - EVUTIL_SET_SOCKET_ERROR(WSAEAFNOSUPPORT); + EVUTIL_SET_SOCKET_ERROR(ERR(EAFNOSUPPORT)); return -1; } if (!fd) { - EVUTIL_SET_SOCKET_ERROR(WSAEINVAL); + EVUTIL_SET_SOCKET_ERROR(ERR(EINVAL)); return -1; } @@ -222,10 +235,10 @@ evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2]) return 0; abort_tidy_up_and_fail: - saved_errno = WSAECONNABORTED; + saved_errno = ERR(ECONNABORTED); tidy_up_and_fail: if (saved_errno < 0) - saved_errno = WSAGetLastError(); + saved_errno = EVUTIL_SOCKET_ERROR(); if (listener != -1) evutil_closesocket(listener); if (connector != -1) @@ -235,7 +248,7 @@ evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2]) EVUTIL_SET_SOCKET_ERROR(saved_errno); return -1; -#endif +#undef ERR } int diff --git a/util-internal.h b/util-internal.h index 32cd88cb..459a2476 100644 --- a/util-internal.h +++ b/util-internal.h @@ -150,6 +150,8 @@ int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int sock int evutil_socket_finished_connecting(evutil_socket_t fd); +int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]); + int evutil_resolve(int family, const char *hostname, struct sockaddr *sa, ev_socklen_t *socklen, int port);