commit 4a634b959a84442069b32e830e1110f0e253c8e4 Author: David Goulet dgoulet@ev0ke.net Date: Wed Aug 7 08:09:18 2013 -0400
Add socketpair(2) support
Signed-off-by: David Goulet dgoulet@ev0ke.net --- src/lib/Makefile.am | 2 +- src/lib/socketpair.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib/torsocks.h | 15 +++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-)
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 0f764f1..0fe0233 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -12,7 +12,7 @@ lib_LTLIBRARIES = libtorsocks.la
libtorsocks_la_SOURCES = torsocks.c torsocks.h \ connect.c gethostbyname.c getaddrinfo.c close.c \ - getpeername.c socket.c syscall.c + getpeername.c socket.c syscall.c socketpair.c
libtorsocks_la_LIBADD = \ $(top_builddir)/src/common/libcommon.la \ diff --git a/src/lib/socketpair.c b/src/lib/socketpair.c new file mode 100644 index 0000000..97a5761 --- /dev/null +++ b/src/lib/socketpair.c @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2013 - David Goulet dgoulet@ev0ke.net + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <assert.h> + +#include <common/log.h> + +#include "torsocks.h" + +/* + * Torsocks call for socketpair(2) + */ +LIBC_SOCKETPAIR_RET_TYPE tsocks_socketpair(LIBC_SOCKETPAIR_SIG) +{ + DBG("[socketpair] Creating socket with domain %d, type %d and protocol %d", + __domain, __type, __protocol); + + switch (__type) { + case SOCK_STREAM: + break; + default: + if (__domain == AF_INET || __domain == AF_INET6) { + ERR("Non TCP socketpair denied. Tor network can't handle it. " + "Stopping everything!"); + errno = EINVAL; + return -1; + } + break; + } + + /* Stream socket for INET/INET6 is good so open it. */ + return tsocks_libc_socketpair(__domain, __type, __protocol, __sv); +} + +/* + * Libc hijacked symbol socketpair(2). + */ +LIBC_SOCKETPAIR_DECL +{ + /* Find symbol if not already set. Exit if not found. */ + tsocks_libc_socketpair = tsocks_find_libc_symbol(LIBC_SOCKETPAIR_NAME_STR, + TSOCKS_SYM_EXIT_NOT_FOUND); + return tsocks_socketpair(LIBC_SOCKETPAIR_ARGS); +} diff --git a/src/lib/torsocks.h b/src/lib/torsocks.h index c6191e2..953446b 100644 --- a/src/lib/torsocks.h +++ b/src/lib/torsocks.h @@ -56,6 +56,15 @@ #define LIBC_SOCKET_ARGS \ __domain, __type, __protocol
+/* socketpair(2) */ +#define LIBC_SOCKETPAIR_NAME socketpair +#define LIBC_SOCKETPAIR_NAME_STR XSTR(LIBC_SOCKETPAIR_NAME) +#define LIBC_SOCKETPAIR_RET_TYPE int +#define LIBC_SOCKETPAIR_SIG \ + int __domain, int __type, int __protocol, int __sv[2] +#define LIBC_SOCKETPAIR_ARGS \ + __domain, __type, __protocol, __sv + /* close(2) */ #include <unistd.h>
@@ -188,6 +197,12 @@ TSOCKS_DECL(socket, LIBC_SOCKET_RET_TYPE, LIBC_SOCKET_SIG) #define LIBC_SOCKET_DECL \ LIBC_SOCKET_RET_TYPE LIBC_SOCKET_NAME(LIBC_SOCKET_SIG)
+/* socketpair(2) */ +TSOCKS_LIBC_DECL(socketpair, LIBC_SOCKETPAIR_RET_TYPE, LIBC_SOCKETPAIR_SIG) +TSOCKS_DECL(socketpair, LIBC_SOCKETPAIR_RET_TYPE, LIBC_SOCKETPAIR_SIG) +#define LIBC_SOCKETPAIR_DECL \ + LIBC_SOCKETPAIR_RET_TYPE LIBC_SOCKETPAIR_NAME(LIBC_SOCKETPAIR_SIG) + /* syscall(2) */ TSOCKS_LIBC_DECL(syscall, LIBC_SYSCALL_RET_TYPE, LIBC_SYSCALL_SIG) #define LIBC_SYSCALL_DECL \
tor-commits@lists.torproject.org