tor-commits
Threads by month
- ----- 2026 -----
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- 1 participants
- 214711 discussions
23 Oct '11
commit 75b9f9880737197b8c00c897e04c1c4116ee3de0
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Mon Feb 14 22:51:40 2011 +0000
Compile and (apparently) work on OpenBSD
Need more testing to be sure, but it looks like it works OK.
---
configure.in | 12 ++++++++++++
src/Makefile.am | 1 -
src/dead_pool.c | 5 +++++
src/torsocks.c | 2 +-
4 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/configure.in b/configure.in
index ca5af2a..cdb99e7 100644
--- a/configure.in
+++ b/configure.in
@@ -518,6 +518,9 @@ SPECIALLIBS=${LIBS}
AC_SUBST(SPECIALLIBS)
LIBS=${SIMPLELIBS}
+AC_SEARCH_LIBS(dlopen, [dl])
+AC_SEARCH_LIBS(inet_aton, [resolv])
+
dnl Linker checks for Mac OSX, which uses DYLD_INSERT_LIBRARIES
dnl instead of LD_PRELOAD
case "$host_os" in
@@ -562,6 +565,15 @@ darwin*)
;;
esac
+dnl OpenBSD needs -lpthread. It also doesn't support AI_V4MAPPED.
+case $host in
+*-*-openbsd*)
+ AC_DEFINE(OPENBSD, 1, "Define to handle OpenBSD")
+ AC_SEARCH_LIBS(pthread_create, [pthread])
+ AC_SEARCH_LIBS(pthread_detach, [pthread])
+ ;;
+esac
+
AC_SUBST(SHLIB_EXT)
AC_SUBST(LD_PRELOAD)
diff --git a/src/Makefile.am b/src/Makefile.am
index 134dc42..cce43f2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,6 +1,5 @@
# Makefile used by configure to create real Makefile
-LIBS = -ldl -lc -lresolv
libdir = @libdir@/torsocks
# Install configuration file
diff --git a/src/dead_pool.c b/src/dead_pool.c
index bba90d3..6f4043a 100644
--- a/src/dead_pool.c
+++ b/src/dead_pool.c
@@ -752,6 +752,10 @@ our_getipnodebyname(dead_pool *pool, const char *name, int af, int flags,
/* Caller has requested an AF_INET6 address, and is not prepared to
accept IPv4-mapped IPV6 addresses. There's nothing we can do to
service their request. */
+#ifdef OPENBSD
+ /* OpenBSD doesn't support the AI_V4MAPPED flag, so just return. */
+ return NULL;
+#else
if((flags & AI_V4MAPPED) == 0) {
show_msg(MSGWARN, "getipnodebyname: asked for V6 addresses only, "
"but torsocks can't handle that\n");
@@ -760,6 +764,7 @@ our_getipnodebyname(dead_pool *pool, const char *name, int af, int flags,
} else {
want_4in6 = 1;
}
+#endif
}
pos = store_pool_entry(pool, (char *)name, &pool_addr);
diff --git a/src/torsocks.c b/src/torsocks.c
index 579751e..4239ced 100644
--- a/src/torsocks.c
+++ b/src/torsocks.c
@@ -147,7 +147,7 @@ static pthread_mutex_t torsocks_init_mutex = PTHREAD_MUTEX_INITIALIZER;
void torsocks_init(void)
{
#define LOAD_ERROR(s,l) { \
- char *error; \
+ const char *error; \
error = dlerror(); \
show_msg(l, "The symbol %s() was not found in any shared " \
"library. The error reported was: %s!\n", s, \
1
0
23 Oct '11
commit cc8e7e0ae59def5da94b576ed19e9412e517ad7b
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Fri Feb 18 22:11:40 2011 +0000
Fix -lresolv on linux builds again
---
configure.in | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/configure.in b/configure.in
index cdb99e7..614f34e 100644
--- a/configure.in
+++ b/configure.in
@@ -519,7 +519,7 @@ AC_SUBST(SPECIALLIBS)
LIBS=${SIMPLELIBS}
AC_SEARCH_LIBS(dlopen, [dl])
-AC_SEARCH_LIBS(inet_aton, [resolv])
+AC_SEARCH_LIBS(res_query, [resolv])
dnl Linker checks for Mac OSX, which uses DYLD_INSERT_LIBRARIES
dnl instead of LD_PRELOAD
1
0
23 Oct '11
commit 91046642c2efa6bed622f524b95919563d2a40a5
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Sat Feb 19 13:20:21 2011 +0000
Split SOCKS functions into separate file
---
src/Makefile.am | 4 +-
src/socks.c | 629 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/socks.h | 116 ++++++++++
src/torsocks.c | 572 +--------------------------------------------------
src/torsocks.h | 104 ---------
5 files changed, 748 insertions(+), 677 deletions(-)
diff --git a/src/Makefile.am b/src/Makefile.am
index cce43f2..cb69714 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -25,11 +25,11 @@ torsocksconfmanpage_DATA = torsocks.conf.5
# Install main library to $(prefix)/lib/tor (must match torsocks.in)
lib_LTLIBRARIES = libtorsocks.la
-libtorsocks_la_SOURCES = torsocks.c common.c parser.c dead_pool.c darwin_warts.c
+libtorsocks_la_SOURCES = torsocks.c common.c parser.c dead_pool.c darwin_warts.c socks.c
libtorsocks_la_LDFLAGS = -version-info 1:0:0
#libtorsocks_la_CFLAGS = -nostartfiles
-DISTCLEANFILES=parser.lo dead_pool.lo common.lo libtorsocks.lo tsocks.lo \
+DISTCLEANFILES=parser.lo dead_pool.lo common.lo libtorsocks.lo torsocks.lo darwin_warts.lo socks.lo\
config.cache config.log config.h Makefile \
aclocal.m4 config.status usewithtor torsocks \
autom4te.cache .libs .deps
diff --git a/src/socks.c b/src/socks.c
new file mode 100644
index 0000000..37b97f5
--- /dev/null
+++ b/src/socks.c
@@ -0,0 +1,629 @@
+/***************************************************************************
+ * *
+ * Copyright (C) 2000-2008 Shaun Clowes <delius(a)progsoc.org> *
+ * Copyright (C) 2008-2011 Robert Hogan <robert(a)roberthogan.net> *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * 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., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+/* PreProcessor Defines */
+#include <config.h>
+
+/*Defining _NONSTD_SOURCE causes library and kernel calls to behave as closely
+to Mac OS X 10.3's library and kernel calls as possible.*/
+#if defined(__APPLE__) || defined(__darwin__)
+/*
+From 'man compat' in OSX:
+64-BIT COMPILATION
+ When compiling for 64-bit architectures, the __LP64__ macro will be defined to 1, and UNIX conformance
+ is always on (the _DARWIN_FEATURE_UNIX_CONFORMANCE macro will also be defined to the SUS conformance
+ level). Defining _NONSTD_SOURCE will cause a compilation error.
+*/
+#if !defined(__LP64__)
+#define _NONSTD_SOURCE 1
+#endif
+#include <sys/socket.h>
+#endif
+
+
+#ifdef USE_GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+/* Header Files */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <dlfcn.h>
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/poll.h>
+#include <sys/time.h>
+#include <pwd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <common.h>
+#include <pthread.h>
+#include <stdarg.h>
+#if !defined(__APPLE__) && !defined(__darwin__)
+#include <sys/socket.h>
+#endif
+#include <resolv.h>
+
+#include "parser.h"
+#include "socks.h"
+#include "dead_pool.h"
+
+static int connect_server(struct connreq *conn);
+static int send_socks_request(struct connreq *conn);
+static int send_socksv4_request(struct connreq *conn);
+static int send_socksv5_method(struct connreq *conn);
+static int send_socksv5_connect(struct connreq *conn);
+static int send_buffer(struct connreq *conn);
+static int recv_buffer(struct connreq *conn);
+static int read_socksv5_method(struct connreq *conn);
+static int read_socksv4_req(struct connreq *conn);
+static int read_socksv5_connect(struct connreq *conn);
+static int read_socksv5_auth(struct connreq *conn);
+static int send_socksv4a_request(struct connreq *conn, const char *onion_host);
+
+struct connreq *new_socks_request(int sockid, struct sockaddr_in *connaddr,
+ struct sockaddr_in *serveraddr,
+ struct serverent *path)
+{
+ struct connreq *newconn;
+
+ if ((newconn = malloc(sizeof(*newconn))) == NULL) {
+ /* Could not malloc, we're stuffed */
+ show_msg(MSGERR, "Could not allocate memory for new socks request\n");
+ return(NULL);
+ }
+
+ /* Add this connection to be proxied to the list */
+ memset(newconn, 0x0, sizeof(*newconn));
+ newconn->sockid = sockid;
+ newconn->state = UNSTARTED;
+ newconn->path = path;
+ memcpy(&(newconn->connaddr), connaddr, sizeof(newconn->connaddr));
+ memcpy(&(newconn->serveraddr), serveraddr, sizeof(newconn->serveraddr));
+ newconn->next = requests;
+ requests = newconn;
+
+ return(newconn);
+}
+
+void kill_socks_request(struct connreq *conn)
+{
+ struct connreq *connnode;
+
+ if (requests == conn)
+ requests = conn->next;
+ else {
+ for (connnode = requests; connnode != NULL; connnode = connnode->next) {
+ if (connnode->next == conn) {
+ connnode->next = conn->next;
+ break;
+ }
+ }
+ }
+
+ free(conn);
+}
+
+struct connreq *find_socks_request(int sockid, int includefinished)
+{
+ struct connreq *connnode;
+
+ for (connnode = requests; connnode != NULL; connnode = connnode->next) {
+ if (connnode->sockid == sockid) {
+ if (((connnode->state == FAILED) || (connnode->state == DONE)) &&
+ !includefinished)
+ break;
+ else
+ return(connnode);
+ }
+ }
+
+ return(NULL);
+}
+
+int handle_request(struct connreq *conn)
+{
+ int rc = 0;
+ int i = 0;
+
+ show_msg(MSGDEBUG, "Beginning handle loop for socket %d\n", conn->sockid);
+
+ while ((rc == 0) &&
+ (conn->state != FAILED) &&
+ (conn->state != DONE) &&
+ (i++ < 20)) {
+ show_msg(MSGDEBUG, "In request handle loop for socket %d, "
+ "current state of request is %d\n", conn->sockid,
+ conn->state);
+ switch(conn->state) {
+ case UNSTARTED:
+ case CONNECTING:
+ rc = connect_server(conn);
+ break;
+ case CONNECTED:
+ rc = send_socks_request(conn);
+ break;
+ case SENDING:
+ rc = send_buffer(conn);
+ break;
+ case RECEIVING:
+ rc = recv_buffer(conn);
+ break;
+ case SENTV4REQ:
+ show_msg(MSGDEBUG, "Receiving reply to SOCKS V4 connect request\n");
+ conn->datalen = sizeof(struct sockrep);
+ conn->datadone = 0;
+ conn->state = RECEIVING;
+ conn->nextstate = GOTV4REQ;
+ break;
+ case GOTV4REQ:
+ rc = read_socksv4_req(conn);
+ break;
+ case SENTV5METHOD:
+ show_msg(MSGDEBUG, "Receiving reply to SOCKS V5 method negotiation\n");
+ conn->datalen = 2;
+ conn->datadone = 0;
+ conn->state = RECEIVING;
+ conn->nextstate = GOTV5METHOD;
+ break;
+ case GOTV5METHOD:
+ rc = read_socksv5_method(conn);
+ break;
+ case SENTV5AUTH:
+ show_msg(MSGDEBUG, "Receiving reply to SOCKS V5 authentication negotiation\n");
+ conn->datalen = 2;
+ conn->datadone = 0;
+ conn->state = RECEIVING;
+ conn->nextstate = GOTV5AUTH;
+ break;
+ case GOTV5AUTH:
+ rc = read_socksv5_auth(conn);
+ break;
+ case SENTV5CONNECT:
+ show_msg(MSGDEBUG, "Receiving reply to SOCKS V5 connect request\n");
+ conn->datalen = 10;
+ conn->datadone = 0;
+ conn->state = RECEIVING;
+ conn->nextstate = GOTV5CONNECT;
+ break;
+ case GOTV5CONNECT:
+ rc = read_socksv5_connect(conn);
+ break;
+ }
+ conn->err = errno;
+ }
+
+ if (i == 20)
+ show_msg(MSGERR, "Ooops, state loop while handling request %d\n",
+ conn->sockid);
+
+ show_msg(MSGDEBUG, "Handle loop completed for socket %d in state %d, "
+ "returning %d\n", conn->sockid, conn->state, rc);
+ return(rc);
+}
+
+static int connect_server(struct connreq *conn)
+{
+ int rc;
+
+ /* Connect this socket to the socks server */
+ show_msg(MSGDEBUG, "Connecting to %s port %d\n",
+ inet_ntoa(conn->serveraddr.sin_addr), ntohs(conn->serveraddr.sin_port));
+
+ rc = realconnect(conn->sockid, (CONNECT_SOCKARG) &(conn->serveraddr),
+ sizeof(conn->serveraddr));
+
+ show_msg(MSGDEBUG, "Connect returned %d, errno is %d\n", rc, errno);
+ if (rc && errno == EISCONN) {
+ rc = 0;
+ show_msg(MSGDEBUG, "Socket %d already connected to SOCKS server\n", conn->sockid);
+ conn->state = CONNECTED;
+ } else if (rc) {
+ if (errno != EINPROGRESS) {
+ show_msg(MSGERR, "Error %d attempting to connect to SOCKS "
+ "server (%s)\n", errno, strerror(errno));
+ conn->state = FAILED;
+ } else {
+ show_msg(MSGDEBUG, "Connection in progress\n");
+ conn->state = CONNECTING;
+ }
+ } else {
+ show_msg(MSGDEBUG, "Socket %d connected to SOCKS server\n", conn->sockid);
+ conn->state = CONNECTED;
+ }
+
+ return((rc ? errno : 0));
+}
+
+static int send_socks_request(struct connreq *conn)
+{
+ int rc = 0;
+
+ if (conn->path->type == 4) {
+ char *name = get_pool_entry(pool, &(conn->connaddr.sin_addr));
+ if(name != NULL)
+ rc = send_socksv4a_request(conn,name);
+ else
+ rc = send_socksv4_request(conn);
+ } else
+ rc = send_socksv5_method(conn);
+ return(rc);
+}
+
+static int send_socksv4a_request(struct connreq *conn,const char *onion_host)
+{
+ struct passwd *user;
+ struct sockreq *thisreq;
+ int endOfUser;
+ /* Determine the current username */
+ user = getpwuid(getuid());
+
+ thisreq = (struct sockreq *) conn->buffer;
+ endOfUser=sizeof(struct sockreq) +
+ (user == NULL ? 0 : strlen(user->pw_name)) + 1;
+
+ /* Check the buffer has enough space for the request */
+ /* and the user name */
+ conn->datalen = endOfUser+
+ (onion_host == NULL ? 0 : strlen(onion_host)) + 1;
+ if (sizeof(conn->buffer) < conn->datalen) {
+ show_msg(MSGERR, "The SOCKS username is too long");
+ conn->state = FAILED;
+ return(ECONNREFUSED);
+ }
+
+ /* Create the request */
+ thisreq->version = 4;
+ thisreq->command = 1;
+ thisreq->dstport = conn->connaddr.sin_port;
+ thisreq->dstip = htonl(1);
+
+ /* Copy the username */
+ strcpy((char *) thisreq + sizeof(struct sockreq),
+ (user == NULL ? "" : user->pw_name));
+
+ /* Copy the onion host */
+ strcpy((char *) thisreq + endOfUser,
+ (onion_host == NULL ? "" : onion_host));
+
+ conn->datadone = 0;
+ conn->state = SENDING;
+ conn->nextstate = SENTV4REQ;
+
+ return(0);
+}
+
+static int send_socksv4_request(struct connreq *conn)
+{
+ struct passwd *user;
+ struct sockreq *thisreq;
+
+ /* Determine the current username */
+ user = getpwuid(getuid());
+
+ thisreq = (struct sockreq *) conn->buffer;
+
+ /* Check the buffer has enough space for the request */
+ /* and the user name */
+ conn->datalen = sizeof(struct sockreq) +
+ (user == NULL ? 0 : strlen(user->pw_name)) + 1;
+ if (sizeof(conn->buffer) < conn->datalen) {
+ show_msg(MSGERR, "The SOCKS username is too long");
+ conn->state = FAILED;
+ return(ECONNREFUSED);
+ }
+
+ /* Create the request */
+ thisreq->version = 4;
+ thisreq->command = 1;
+ thisreq->dstport = conn->connaddr.sin_port;
+ thisreq->dstip = conn->connaddr.sin_addr.s_addr;
+
+ /* Copy the username */
+ strcpy((char *) thisreq + sizeof(struct sockreq),
+ (user == NULL ? "" : user->pw_name));
+
+ conn->datadone = 0;
+ conn->state = SENDING;
+ conn->nextstate = SENTV4REQ;
+
+ return(0);
+}
+
+static int send_socksv5_method(struct connreq *conn)
+{
+ char verstring[] = { 0x05, /* Version 5 SOCKS */
+ 0x02, /* No. Methods */
+ 0x00, /* Null Auth */
+ 0x02 }; /* User/Pass Auth */
+
+ show_msg(MSGDEBUG, "Constructing V5 method negotiation\n");
+ conn->state = SENDING;
+ conn->nextstate = SENTV5METHOD;
+ memcpy(conn->buffer, verstring, sizeof(verstring));
+ conn->datalen = sizeof(verstring);
+ conn->datadone = 0;
+
+ return(0);
+}
+
+static int send_socksv5_connect(struct connreq *conn)
+{
+ int namelen = 0;
+ char *name = NULL;
+ char constring[] = { 0x05, /* Version 5 SOCKS */
+ 0x01, /* Connect request */
+ 0x00, /* Reserved */
+ 0x01 }; /* IP Version 4 */
+
+ show_msg(MSGDEBUG, "Constructing V5 connect request\n");
+ conn->datadone = 0;
+ conn->state = SENDING;
+ conn->nextstate = SENTV5CONNECT;
+ memcpy(conn->buffer, constring, sizeof(constring));
+ conn->datalen = sizeof(constring);
+
+ show_msg(MSGDEBUG, "send_socksv5_connect: looking for: %s\n",
+ inet_ntoa(conn->connaddr.sin_addr));
+
+ name = get_pool_entry(pool, &(conn->connaddr.sin_addr));
+ if(name != NULL) {
+ namelen = strlen(name);
+ if(namelen > 255) /* "Can't happen" */
+ name = NULL;
+ }
+ if(name != NULL) {
+ show_msg(MSGDEBUG, "send_socksv5_connect: found it!\n");
+ /* Substitute the domain name from the pool into the SOCKS request. */
+ conn->buffer[3] = 0x03; /* Change the ATYP field */
+ conn->buffer[4] = namelen; /* Length of name */
+ conn->datalen++;
+ memcpy(&conn->buffer[conn->datalen], name, namelen);
+ conn->datalen += namelen;
+ } else {
+ show_msg(MSGDEBUG, "send_socksv5_connect: ip address not found\n");
+ /* Use the raw IP address */
+ memcpy(&conn->buffer[conn->datalen], &(conn->connaddr.sin_addr.s_addr),
+ sizeof(conn->connaddr.sin_addr.s_addr));
+ conn->datalen += sizeof(conn->connaddr.sin_addr.s_addr);
+ }
+ memcpy(&conn->buffer[conn->datalen], &(conn->connaddr.sin_port),
+ sizeof(conn->connaddr.sin_port));
+ conn->datalen += sizeof(conn->connaddr.sin_port);
+
+ return(0);
+}
+
+static int send_buffer(struct connreq *conn)
+{
+ int rc = 0;
+
+ show_msg(MSGDEBUG, "Writing to server (sending %d bytes)\n", conn->datalen);
+ while ((rc == 0) && (conn->datadone != conn->datalen)) {
+ rc = send(conn->sockid, conn->buffer + conn->datadone,
+ conn->datalen - conn->datadone, 0);
+ if (rc > 0) {
+ conn->datadone += rc;
+ rc = 0;
+ } else {
+ if (errno != EWOULDBLOCK)
+ show_msg(MSGDEBUG, "Write failed, %s\n", strerror(errno));
+ rc = errno;
+ }
+ }
+
+ if (conn->datadone == conn->datalen)
+ conn->state = conn->nextstate;
+
+ show_msg(MSGDEBUG, "Sent %d bytes of %d bytes in buffer, return code is %d\n",
+ conn->datadone, conn->datalen, rc);
+ return(rc);
+}
+
+static int recv_buffer(struct connreq *conn)
+{
+ int rc = 0;
+
+ show_msg(MSGDEBUG, "Reading from server (expecting %d bytes)\n", conn->datalen);
+ while ((rc == 0) && (conn->datadone != conn->datalen)) {
+ rc = recv(conn->sockid, conn->buffer + conn->datadone,
+ conn->datalen - conn->datadone, 0);
+ if (rc > 0) {
+ conn->datadone += rc;
+ rc = 0;
+ } else if (rc == 0) {
+ show_msg(MSGDEBUG, "Peer has shutdown but we only read %d of %d bytes.\n",
+ conn->datadone, conn->datalen);
+ rc = ENOTCONN; /* ENOTCONN seems like the most fitting error message */
+ } else {
+ if (errno != EWOULDBLOCK)
+ show_msg(MSGDEBUG, "Read failed, %s\n", strerror(errno));
+ rc = errno;
+ }
+ }
+
+ if (conn->datadone == conn->datalen)
+ conn->state = conn->nextstate;
+
+ show_msg(MSGDEBUG, "Received %d bytes of %d bytes expected, return code is %d\n",
+ conn->datadone, conn->datalen, rc);
+ return(rc);
+}
+
+static int read_socksv5_method(struct connreq *conn)
+{
+ struct passwd *nixuser;
+ char *uname, *upass;
+
+ /* See if we offered an acceptable method */
+ if (conn->buffer[1] == '\xff') {
+ show_msg(MSGERR, "SOCKS V5 server refused authentication methods\n");
+ conn->state = FAILED;
+ return(ECONNREFUSED);
+ }
+
+ /* If the socks server chose username/password authentication */
+ /* (method 2) then do that */
+ if ((unsigned short int) conn->buffer[1] == 2) {
+ show_msg(MSGDEBUG, "SOCKS V5 server chose username/password authentication\n");
+
+ /* Determine the current *nix username */
+ nixuser = getpwuid(getuid());
+
+ if (((uname = conn->path->defuser) == NULL) &&
+ ((uname = getenv("TORSOCKS_USERNAME")) == NULL) &&
+ ((uname = (nixuser == NULL ? NULL : nixuser->pw_name)) == NULL)) {
+ show_msg(MSGERR, "Could not get SOCKS username from "
+ "local passwd file, torsocks.conf "
+ "or $TORSOCKS_USERNAME to authenticate "
+ "with");
+ conn->state = FAILED;
+ return(ECONNREFUSED);
+ }
+
+ if (((upass = getenv("TORSOCKS_PASSWORD")) == NULL) &&
+ ((upass = conn->path->defpass) == NULL)) {
+ show_msg(MSGERR, "Need a password in torsocks.conf or "
+ "$TORSOCKS_PASSWORD to authenticate with");
+ conn->state = FAILED;
+ return(ECONNREFUSED);
+ }
+
+ /* Check that the username / pass specified will */
+ /* fit into the buffer */
+ if ((3 + strlen(uname) + strlen(upass)) >= sizeof(conn->buffer)) {
+ show_msg(MSGERR, "The supplied socks username or "
+ "password is too long");
+ conn->state = FAILED;
+ return(ECONNREFUSED);
+ }
+
+ conn->datalen = 0;
+ conn->buffer[conn->datalen] = '\x01';
+ conn->datalen++;
+ conn->buffer[conn->datalen] = (int8_t) strlen(uname);
+ conn->datalen++;
+ memcpy(&(conn->buffer[conn->datalen]), uname, strlen(uname));
+ conn->datalen = conn->datalen + strlen(uname);
+ conn->buffer[conn->datalen] = (int8_t) strlen(upass);
+ conn->datalen++;
+ memcpy(&(conn->buffer[conn->datalen]), upass, strlen(upass));
+ conn->datalen = conn->datalen + strlen(upass);
+
+ conn->state = SENDING;
+ conn->nextstate = SENTV5AUTH;
+ conn->datadone = 0;
+ } else
+ return(send_socksv5_connect(conn));
+
+ return(0);
+}
+
+static int read_socksv5_auth(struct connreq *conn)
+{
+
+ if (conn->buffer[1] != '\x00') {
+ show_msg(MSGERR, "SOCKS authentication failed, check username and password\n");
+ conn->state = FAILED;
+ return(ECONNREFUSED);
+ }
+
+ /* Ok, we authenticated ok, send the connection request */
+ return(send_socksv5_connect(conn));
+}
+
+static int read_socksv5_connect(struct connreq *conn)
+{
+
+ /* See if the connection succeeded */
+ if (conn->buffer[1] != '\x00') {
+ show_msg(MSGERR, "SOCKS V5 connect failed: ");
+ conn->state = FAILED;
+ switch ((int8_t) conn->buffer[1]) {
+ case 1:
+ show_msg(MSGERR, "General SOCKS server failure\n");
+ return(ECONNABORTED);
+ case 2:
+ show_msg(MSGERR, "Connection denied by rule\n");
+ return(ECONNABORTED);
+ case 3:
+ show_msg(MSGERR, "Network unreachable\n");
+ return(ENETUNREACH);
+ case 4:
+ show_msg(MSGERR, "Host unreachable\n");
+ return(EHOSTUNREACH);
+ case 5:
+ show_msg(MSGERR, "Connection refused\n");
+ return(ECONNREFUSED);
+ case 6:
+ show_msg(MSGERR, "TTL Expired\n");
+ return(ETIMEDOUT);
+ case 7:
+ show_msg(MSGERR, "Command not supported\n");
+ return(ECONNABORTED);
+ case 8:
+ show_msg(MSGERR, "Address type not supported\n");
+ return(ECONNABORTED);
+ default:
+ show_msg(MSGERR, "Unknown error\n");
+ return(ECONNABORTED);
+ }
+ }
+ conn->state = DONE;
+
+ return(0);
+}
+
+static int read_socksv4_req(struct connreq *conn)
+{
+ struct sockrep *thisrep;
+
+ thisrep = (struct sockrep *) conn->buffer;
+
+ if (thisrep->result != 90) {
+ show_msg(MSGERR, "SOCKS V4 connect rejected:\n");
+ conn->state = FAILED;
+ switch(thisrep->result) {
+ case 91:
+ show_msg(MSGERR, "SOCKS server refused connection\n");
+ return(ECONNREFUSED);
+ case 92:
+ show_msg(MSGERR, "SOCKS server refused connection "
+ "because of failed connect to identd "
+ "on this machine\n");
+ return(ECONNREFUSED);
+ case 93:
+ show_msg(MSGERR, "SOCKS server refused connection "
+ "because identd and this library "
+ "reported different user-ids\n");
+ return(ECONNREFUSED);
+ default:
+ show_msg(MSGERR, "Unknown reason\n");
+ return(ECONNREFUSED);
+ }
+ }
+ conn->state = DONE;
+
+ return(0);
+}
diff --git a/src/socks.h b/src/socks.h
new file mode 100644
index 0000000..af26fa6
--- /dev/null
+++ b/src/socks.h
@@ -0,0 +1,116 @@
+/***************************************************************************
+ * *
+ * Copyright (C) 2000-2008 Shaun Clowes <delius(a)progsoc.org> *
+ * Copyright (C) 2008-2011 Robert Hogan <robert(a)roberthogan.net> *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * 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., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+/* socks.h - Structures used by torsocks to form SOCKS requests */
+
+#ifndef _SOCKS_H
+
+#define _SOCKS_H 1
+
+#include "parser.h"
+#include "dead_pool.h"
+
+/* Structure representing a socks connection request */
+struct sockreq {
+ int8_t version;
+ int8_t command;
+ int16_t dstport;
+ int32_t dstip;
+ /* A null terminated username goes here */
+};
+
+/* Structure representing a socks connection request response */
+struct sockrep {
+ int8_t version;
+ int8_t result;
+ int16_t ignore1;
+ int32_t ignore2;
+};
+
+/* Structure representing a socket which we are currently proxying */
+struct connreq {
+ /* Information about the socket and target */
+ int sockid;
+ struct sockaddr_in connaddr;
+ struct sockaddr_in serveraddr;
+
+ /* Pointer to the config entry for the socks server */
+ struct serverent *path;
+
+ /* Current state of this proxied socket */
+ int state;
+
+ /* Next state to go to when the send or receive is finished */
+ int nextstate;
+
+ /* When connections fail but an error number cannot be reported
+ * because the socket is non blocking we keep the connreq struct until
+ * the status is queried with connect() again, we then return
+ * this value */
+ int err;
+
+ /* Events that were set for this socket upon call to select() or
+ * poll() */
+ int selectevents;
+
+ /* Buffer for sending and receiving on the socket */
+ unsigned int datalen;
+ unsigned int datadone;
+ char buffer[2048];
+
+ struct connreq *next;
+};
+
+/* Connection statuses */
+#define UNSTARTED 0
+#define CONNECTING 1
+#define CONNECTED 2
+#define SENDING 3
+#define RECEIVING 4
+#define SENTV4REQ 5
+#define GOTV4REQ 6
+#define SENTV5METHOD 7
+#define GOTV5METHOD 8
+#define SENTV5AUTH 9
+#define GOTV5AUTH 10
+#define SENTV5CONNECT 11
+#define GOTV5CONNECT 12
+#define DONE 13
+#define FAILED 14
+
+/* Flags to indicate what events a socket was select()ed for */
+#define READ (1<<0)
+#define WRITE (1<<1)
+#define EXCEPT (1<<2)
+#define READWRITE (READ|WRITE)
+#define READWRITEEXCEPT (READ|WRITE|EXCEPT)
+
+/* Global Declarations */
+static dead_pool *pool = NULL;
+static struct connreq *requests = NULL;
+
+struct connreq *new_socks_request(int sockid, struct sockaddr_in *connaddr,
+ struct sockaddr_in *serveraddr,
+ struct serverent *path);
+void kill_socks_request(struct connreq *conn);
+struct connreq *find_socks_request(int sockid, int includefailed);
+int handle_request(struct connreq *conn);
+
+#endif
diff --git a/src/torsocks.c b/src/torsocks.c
index 4239ced..98af4b0 100644
--- a/src/torsocks.c
+++ b/src/torsocks.c
@@ -71,7 +71,7 @@ const char *torsocks_progname = "libtorsocks"; /* Name used in err msgs
#include <resolv.h>
#include "parser.h"
-#include "torsocks.h"
+#include "socks.h"
#include "dead_pool.h"
/* Some function names are macroized on Darwin. Allow those names
@@ -79,9 +79,6 @@ const char *torsocks_progname = "libtorsocks"; /* Name used in err msgs
#define EXPAND_GUTS(x) torsocks_##x##_guts
#define EXPAND_GUTS_NAME(x) EXPAND_GUTS(x)
-/* Global Declarations */
-static dead_pool *pool = NULL;
-
/* Function prototypes for original functions that we patch */
#ifdef SUPPORT_RES_API
int (*realres_init)(void);
@@ -92,7 +89,6 @@ int (*realres_init)(void);
#undef DARWIN_EXPANSION
static struct parsedfile config;
-static struct connreq *requests = NULL;
static int suid = 0;
static char *conffile = NULL;
static volatile int torsocks_init_complete = 0;
@@ -120,27 +116,7 @@ int res_init(void);
static int get_config();
static int get_environment();
-static int connect_server(struct connreq *conn);
-static int send_socks_request(struct connreq *conn);
-static struct connreq *new_socks_request(int sockid, struct sockaddr_in *connaddr,
- struct sockaddr_in *serveraddr,
- struct serverent *path);
-static void kill_socks_request(struct connreq *conn);
-static int handle_request(struct connreq *conn);
-static struct connreq *find_socks_request(int sockid, int includefailed);
-static int connect_server(struct connreq *conn);
-static int send_socks_request(struct connreq *conn);
-static int send_socksv4_request(struct connreq *conn);
-static int send_socksv5_method(struct connreq *conn);
-static int send_socksv5_connect(struct connreq *conn);
-static int send_buffer(struct connreq *conn);
-static int recv_buffer(struct connreq *conn);
-static int read_socksv5_method(struct connreq *conn);
-static int read_socksv4_req(struct connreq *conn);
-static int read_socksv5_connect(struct connreq *conn);
-static int read_socksv5_auth(struct connreq *conn);
static int deadpool_init(void);
-static int send_socksv4a_request(struct connreq *conn, const char *onion_host);
static pthread_mutex_t torsocks_init_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -898,552 +874,6 @@ int torsocks_getpeername_guts(GETPEERNAME_SIGNATURE,
return rc;
}
-static struct connreq *new_socks_request(int sockid, struct sockaddr_in *connaddr,
- struct sockaddr_in *serveraddr,
- struct serverent *path)
-{
- struct connreq *newconn;
-
- if ((newconn = malloc(sizeof(*newconn))) == NULL) {
- /* Could not malloc, we're stuffed */
- show_msg(MSGERR, "Could not allocate memory for new socks request\n");
- return(NULL);
- }
-
- /* Add this connection to be proxied to the list */
- memset(newconn, 0x0, sizeof(*newconn));
- newconn->sockid = sockid;
- newconn->state = UNSTARTED;
- newconn->path = path;
- memcpy(&(newconn->connaddr), connaddr, sizeof(newconn->connaddr));
- memcpy(&(newconn->serveraddr), serveraddr, sizeof(newconn->serveraddr));
- newconn->next = requests;
- requests = newconn;
-
- return(newconn);
-}
-
-static void kill_socks_request(struct connreq *conn)
-{
- struct connreq *connnode;
-
- if (requests == conn)
- requests = conn->next;
- else {
- for (connnode = requests; connnode != NULL; connnode = connnode->next) {
- if (connnode->next == conn) {
- connnode->next = conn->next;
- break;
- }
- }
- }
-
- free(conn);
-}
-
-static struct connreq *find_socks_request(int sockid, int includefinished)
-{
- struct connreq *connnode;
-
- for (connnode = requests; connnode != NULL; connnode = connnode->next) {
- if (connnode->sockid == sockid) {
- if (((connnode->state == FAILED) || (connnode->state == DONE)) &&
- !includefinished)
- break;
- else
- return(connnode);
- }
- }
-
- return(NULL);
-}
-
-static int handle_request(struct connreq *conn)
-{
- int rc = 0;
- int i = 0;
-
- show_msg(MSGDEBUG, "Beginning handle loop for socket %d\n", conn->sockid);
-
- while ((rc == 0) &&
- (conn->state != FAILED) &&
- (conn->state != DONE) &&
- (i++ < 20)) {
- show_msg(MSGDEBUG, "In request handle loop for socket %d, "
- "current state of request is %d\n", conn->sockid,
- conn->state);
- switch(conn->state) {
- case UNSTARTED:
- case CONNECTING:
- rc = connect_server(conn);
- break;
- case CONNECTED:
- rc = send_socks_request(conn);
- break;
- case SENDING:
- rc = send_buffer(conn);
- break;
- case RECEIVING:
- rc = recv_buffer(conn);
- break;
- case SENTV4REQ:
- show_msg(MSGDEBUG, "Receiving reply to SOCKS V4 connect request\n");
- conn->datalen = sizeof(struct sockrep);
- conn->datadone = 0;
- conn->state = RECEIVING;
- conn->nextstate = GOTV4REQ;
- break;
- case GOTV4REQ:
- rc = read_socksv4_req(conn);
- break;
- case SENTV5METHOD:
- show_msg(MSGDEBUG, "Receiving reply to SOCKS V5 method negotiation\n");
- conn->datalen = 2;
- conn->datadone = 0;
- conn->state = RECEIVING;
- conn->nextstate = GOTV5METHOD;
- break;
- case GOTV5METHOD:
- rc = read_socksv5_method(conn);
- break;
- case SENTV5AUTH:
- show_msg(MSGDEBUG, "Receiving reply to SOCKS V5 authentication negotiation\n");
- conn->datalen = 2;
- conn->datadone = 0;
- conn->state = RECEIVING;
- conn->nextstate = GOTV5AUTH;
- break;
- case GOTV5AUTH:
- rc = read_socksv5_auth(conn);
- break;
- case SENTV5CONNECT:
- show_msg(MSGDEBUG, "Receiving reply to SOCKS V5 connect request\n");
- conn->datalen = 10;
- conn->datadone = 0;
- conn->state = RECEIVING;
- conn->nextstate = GOTV5CONNECT;
- break;
- case GOTV5CONNECT:
- rc = read_socksv5_connect(conn);
- break;
- }
- conn->err = errno;
- }
-
- if (i == 20)
- show_msg(MSGERR, "Ooops, state loop while handling request %d\n",
- conn->sockid);
-
- show_msg(MSGDEBUG, "Handle loop completed for socket %d in state %d, "
- "returning %d\n", conn->sockid, conn->state, rc);
- return(rc);
-}
-
-static int connect_server(struct connreq *conn)
-{
- int rc;
-
- /* Connect this socket to the socks server */
- show_msg(MSGDEBUG, "Connecting to %s port %d\n",
- inet_ntoa(conn->serveraddr.sin_addr), ntohs(conn->serveraddr.sin_port));
-
- rc = realconnect(conn->sockid, (CONNECT_SOCKARG) &(conn->serveraddr),
- sizeof(conn->serveraddr));
-
- show_msg(MSGDEBUG, "Connect returned %d, errno is %d\n", rc, errno);
- if (rc && errno == EISCONN) {
- rc = 0;
- show_msg(MSGDEBUG, "Socket %d already connected to SOCKS server\n", conn->sockid);
- conn->state = CONNECTED;
- } else if (rc) {
- if (errno != EINPROGRESS) {
- show_msg(MSGERR, "Error %d attempting to connect to SOCKS "
- "server (%s)\n", errno, strerror(errno));
- conn->state = FAILED;
- } else {
- show_msg(MSGDEBUG, "Connection in progress\n");
- conn->state = CONNECTING;
- }
- } else {
- show_msg(MSGDEBUG, "Socket %d connected to SOCKS server\n", conn->sockid);
- conn->state = CONNECTED;
- }
-
- return((rc ? errno : 0));
-}
-
-static int send_socks_request(struct connreq *conn)
-{
- int rc = 0;
-
- if (conn->path->type == 4) {
- char *name = get_pool_entry(pool, &(conn->connaddr.sin_addr));
- if(name != NULL)
- rc = send_socksv4a_request(conn,name);
- else
- rc = send_socksv4_request(conn);
- } else
- rc = send_socksv5_method(conn);
- return(rc);
-}
-
-static int send_socksv4a_request(struct connreq *conn,const char *onion_host)
-{
- struct passwd *user;
- struct sockreq *thisreq;
- int endOfUser;
- /* Determine the current username */
- user = getpwuid(getuid());
-
- thisreq = (struct sockreq *) conn->buffer;
- endOfUser=sizeof(struct sockreq) +
- (user == NULL ? 0 : strlen(user->pw_name)) + 1;
-
- /* Check the buffer has enough space for the request */
- /* and the user name */
- conn->datalen = endOfUser+
- (onion_host == NULL ? 0 : strlen(onion_host)) + 1;
- if (sizeof(conn->buffer) < conn->datalen) {
- show_msg(MSGERR, "The SOCKS username is too long");
- conn->state = FAILED;
- return(ECONNREFUSED);
- }
-
- /* Create the request */
- thisreq->version = 4;
- thisreq->command = 1;
- thisreq->dstport = conn->connaddr.sin_port;
- thisreq->dstip = htonl(1);
-
- /* Copy the username */
- strcpy((char *) thisreq + sizeof(struct sockreq),
- (user == NULL ? "" : user->pw_name));
-
- /* Copy the onion host */
- strcpy((char *) thisreq + endOfUser,
- (onion_host == NULL ? "" : onion_host));
-
- conn->datadone = 0;
- conn->state = SENDING;
- conn->nextstate = SENTV4REQ;
-
- return(0);
-}
-
-static int send_socksv4_request(struct connreq *conn)
-{
- struct passwd *user;
- struct sockreq *thisreq;
-
- /* Determine the current username */
- user = getpwuid(getuid());
-
- thisreq = (struct sockreq *) conn->buffer;
-
- /* Check the buffer has enough space for the request */
- /* and the user name */
- conn->datalen = sizeof(struct sockreq) +
- (user == NULL ? 0 : strlen(user->pw_name)) + 1;
- if (sizeof(conn->buffer) < conn->datalen) {
- show_msg(MSGERR, "The SOCKS username is too long");
- conn->state = FAILED;
- return(ECONNREFUSED);
- }
-
- /* Create the request */
- thisreq->version = 4;
- thisreq->command = 1;
- thisreq->dstport = conn->connaddr.sin_port;
- thisreq->dstip = conn->connaddr.sin_addr.s_addr;
-
- /* Copy the username */
- strcpy((char *) thisreq + sizeof(struct sockreq),
- (user == NULL ? "" : user->pw_name));
-
- conn->datadone = 0;
- conn->state = SENDING;
- conn->nextstate = SENTV4REQ;
-
- return(0);
-}
-
-static int send_socksv5_method(struct connreq *conn)
-{
- char verstring[] = { 0x05, /* Version 5 SOCKS */
- 0x02, /* No. Methods */
- 0x00, /* Null Auth */
- 0x02 }; /* User/Pass Auth */
-
- show_msg(MSGDEBUG, "Constructing V5 method negotiation\n");
- conn->state = SENDING;
- conn->nextstate = SENTV5METHOD;
- memcpy(conn->buffer, verstring, sizeof(verstring));
- conn->datalen = sizeof(verstring);
- conn->datadone = 0;
-
- return(0);
-}
-
-static int send_socksv5_connect(struct connreq *conn)
-{
- int namelen = 0;
- char *name = NULL;
- char constring[] = { 0x05, /* Version 5 SOCKS */
- 0x01, /* Connect request */
- 0x00, /* Reserved */
- 0x01 }; /* IP Version 4 */
-
- show_msg(MSGDEBUG, "Constructing V5 connect request\n");
- conn->datadone = 0;
- conn->state = SENDING;
- conn->nextstate = SENTV5CONNECT;
- memcpy(conn->buffer, constring, sizeof(constring));
- conn->datalen = sizeof(constring);
-
- show_msg(MSGDEBUG, "send_socksv5_connect: looking for: %s\n",
- inet_ntoa(conn->connaddr.sin_addr));
-
- name = get_pool_entry(pool, &(conn->connaddr.sin_addr));
- if(name != NULL) {
- namelen = strlen(name);
- if(namelen > 255) /* "Can't happen" */
- name = NULL;
- }
- if(name != NULL) {
- show_msg(MSGDEBUG, "send_socksv5_connect: found it!\n");
- /* Substitute the domain name from the pool into the SOCKS request. */
- conn->buffer[3] = 0x03; /* Change the ATYP field */
- conn->buffer[4] = namelen; /* Length of name */
- conn->datalen++;
- memcpy(&conn->buffer[conn->datalen], name, namelen);
- conn->datalen += namelen;
- } else {
- show_msg(MSGDEBUG, "send_socksv5_connect: ip address not found\n");
- /* Use the raw IP address */
- memcpy(&conn->buffer[conn->datalen], &(conn->connaddr.sin_addr.s_addr),
- sizeof(conn->connaddr.sin_addr.s_addr));
- conn->datalen += sizeof(conn->connaddr.sin_addr.s_addr);
- }
- memcpy(&conn->buffer[conn->datalen], &(conn->connaddr.sin_port),
- sizeof(conn->connaddr.sin_port));
- conn->datalen += sizeof(conn->connaddr.sin_port);
-
- return(0);
-}
-
-static int send_buffer(struct connreq *conn)
-{
- int rc = 0;
-
- show_msg(MSGDEBUG, "Writing to server (sending %d bytes)\n", conn->datalen);
- while ((rc == 0) && (conn->datadone != conn->datalen)) {
- rc = send(conn->sockid, conn->buffer + conn->datadone,
- conn->datalen - conn->datadone, 0);
- if (rc > 0) {
- conn->datadone += rc;
- rc = 0;
- } else {
- if (errno != EWOULDBLOCK)
- show_msg(MSGDEBUG, "Write failed, %s\n", strerror(errno));
- rc = errno;
- }
- }
-
- if (conn->datadone == conn->datalen)
- conn->state = conn->nextstate;
-
- show_msg(MSGDEBUG, "Sent %d bytes of %d bytes in buffer, return code is %d\n",
- conn->datadone, conn->datalen, rc);
- return(rc);
-}
-
-static int recv_buffer(struct connreq *conn)
-{
- int rc = 0;
-
- show_msg(MSGDEBUG, "Reading from server (expecting %d bytes)\n", conn->datalen);
- while ((rc == 0) && (conn->datadone != conn->datalen)) {
- rc = recv(conn->sockid, conn->buffer + conn->datadone,
- conn->datalen - conn->datadone, 0);
- if (rc > 0) {
- conn->datadone += rc;
- rc = 0;
- } else if (rc == 0) {
- show_msg(MSGDEBUG, "Peer has shutdown but we only read %d of %d bytes.\n",
- conn->datadone, conn->datalen);
- rc = ENOTCONN; /* ENOTCONN seems like the most fitting error message */
- } else {
- if (errno != EWOULDBLOCK)
- show_msg(MSGDEBUG, "Read failed, %s\n", strerror(errno));
- rc = errno;
- }
- }
-
- if (conn->datadone == conn->datalen)
- conn->state = conn->nextstate;
-
- show_msg(MSGDEBUG, "Received %d bytes of %d bytes expected, return code is %d\n",
- conn->datadone, conn->datalen, rc);
- return(rc);
-}
-
-static int read_socksv5_method(struct connreq *conn)
-{
- struct passwd *nixuser;
- char *uname, *upass;
-
- /* See if we offered an acceptable method */
- if (conn->buffer[1] == '\xff') {
- show_msg(MSGERR, "SOCKS V5 server refused authentication methods\n");
- conn->state = FAILED;
- return(ECONNREFUSED);
- }
-
- /* If the socks server chose username/password authentication */
- /* (method 2) then do that */
- if ((unsigned short int) conn->buffer[1] == 2) {
- show_msg(MSGDEBUG, "SOCKS V5 server chose username/password authentication\n");
-
- /* Determine the current *nix username */
- nixuser = getpwuid(getuid());
-
- if (((uname = conn->path->defuser) == NULL) &&
- ((uname = getenv("TORSOCKS_USERNAME")) == NULL) &&
- ((uname = (nixuser == NULL ? NULL : nixuser->pw_name)) == NULL)) {
- show_msg(MSGERR, "Could not get SOCKS username from "
- "local passwd file, torsocks.conf "
- "or $TORSOCKS_USERNAME to authenticate "
- "with");
- conn->state = FAILED;
- return(ECONNREFUSED);
- }
-
- if (((upass = getenv("TORSOCKS_PASSWORD")) == NULL) &&
- ((upass = conn->path->defpass) == NULL)) {
- show_msg(MSGERR, "Need a password in torsocks.conf or "
- "$TORSOCKS_PASSWORD to authenticate with");
- conn->state = FAILED;
- return(ECONNREFUSED);
- }
-
- /* Check that the username / pass specified will */
- /* fit into the buffer */
- if ((3 + strlen(uname) + strlen(upass)) >= sizeof(conn->buffer)) {
- show_msg(MSGERR, "The supplied socks username or "
- "password is too long");
- conn->state = FAILED;
- return(ECONNREFUSED);
- }
-
- conn->datalen = 0;
- conn->buffer[conn->datalen] = '\x01';
- conn->datalen++;
- conn->buffer[conn->datalen] = (int8_t) strlen(uname);
- conn->datalen++;
- memcpy(&(conn->buffer[conn->datalen]), uname, strlen(uname));
- conn->datalen = conn->datalen + strlen(uname);
- conn->buffer[conn->datalen] = (int8_t) strlen(upass);
- conn->datalen++;
- memcpy(&(conn->buffer[conn->datalen]), upass, strlen(upass));
- conn->datalen = conn->datalen + strlen(upass);
-
- conn->state = SENDING;
- conn->nextstate = SENTV5AUTH;
- conn->datadone = 0;
- } else
- return(send_socksv5_connect(conn));
-
- return(0);
-}
-
-static int read_socksv5_auth(struct connreq *conn)
-{
-
- if (conn->buffer[1] != '\x00') {
- show_msg(MSGERR, "SOCKS authentication failed, check username and password\n");
- conn->state = FAILED;
- return(ECONNREFUSED);
- }
-
- /* Ok, we authenticated ok, send the connection request */
- return(send_socksv5_connect(conn));
-}
-
-static int read_socksv5_connect(struct connreq *conn)
-{
-
- /* See if the connection succeeded */
- if (conn->buffer[1] != '\x00') {
- show_msg(MSGERR, "SOCKS V5 connect failed: ");
- conn->state = FAILED;
- switch ((int8_t) conn->buffer[1]) {
- case 1:
- show_msg(MSGERR, "General SOCKS server failure\n");
- return(ECONNABORTED);
- case 2:
- show_msg(MSGERR, "Connection denied by rule\n");
- return(ECONNABORTED);
- case 3:
- show_msg(MSGERR, "Network unreachable\n");
- return(ENETUNREACH);
- case 4:
- show_msg(MSGERR, "Host unreachable\n");
- return(EHOSTUNREACH);
- case 5:
- show_msg(MSGERR, "Connection refused\n");
- return(ECONNREFUSED);
- case 6:
- show_msg(MSGERR, "TTL Expired\n");
- return(ETIMEDOUT);
- case 7:
- show_msg(MSGERR, "Command not supported\n");
- return(ECONNABORTED);
- case 8:
- show_msg(MSGERR, "Address type not supported\n");
- return(ECONNABORTED);
- default:
- show_msg(MSGERR, "Unknown error\n");
- return(ECONNABORTED);
- }
- }
- conn->state = DONE;
-
- return(0);
-}
-
-static int read_socksv4_req(struct connreq *conn)
-{
- struct sockrep *thisrep;
-
- thisrep = (struct sockrep *) conn->buffer;
-
- if (thisrep->result != 90) {
- show_msg(MSGERR, "SOCKS V4 connect rejected:\n");
- conn->state = FAILED;
- switch(thisrep->result) {
- case 91:
- show_msg(MSGERR, "SOCKS server refused connection\n");
- return(ECONNREFUSED);
- case 92:
- show_msg(MSGERR, "SOCKS server refused connection "
- "because of failed connect to identd "
- "on this machine\n");
- return(ECONNREFUSED);
- case 93:
- show_msg(MSGERR, "SOCKS server refused connection "
- "because identd and this library "
- "reported different user-ids\n");
- return(ECONNREFUSED);
- default:
- show_msg(MSGERR, "Unknown reason\n");
- return(ECONNREFUSED);
- }
- }
- conn->state = DONE;
-
- return(0);
-}
-
#ifdef SUPPORT_RES_API
int res_init(void)
{
diff --git a/src/torsocks.h b/src/torsocks.h
deleted file mode 100644
index 396aa5e..0000000
--- a/src/torsocks.h
+++ /dev/null
@@ -1,104 +0,0 @@
-/***************************************************************************
- * *
- * Copyright (C) 2000-2008 Shaun Clowes <delius(a)progsoc.org> *
- * Copyright (C) 2008-2011 Robert Hogan <robert(a)roberthogan.net> *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- * 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., *
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
- ***************************************************************************/
-/* torsocks.h - Structures used by torsocks to form SOCKS requests */
-
-#ifndef _TORSOCKS_H
-
-#define _TORSOCKS_H 1
-
-#include "parser.h"
-
-/* Structure representing a socks connection request */
-struct sockreq {
- int8_t version;
- int8_t command;
- int16_t dstport;
- int32_t dstip;
- /* A null terminated username goes here */
-};
-
-/* Structure representing a socks connection request response */
-struct sockrep {
- int8_t version;
- int8_t result;
- int16_t ignore1;
- int32_t ignore2;
-};
-
-/* Structure representing a socket which we are currently proxying */
-struct connreq {
- /* Information about the socket and target */
- int sockid;
- struct sockaddr_in connaddr;
- struct sockaddr_in serveraddr;
-
- /* Pointer to the config entry for the socks server */
- struct serverent *path;
-
- /* Current state of this proxied socket */
- int state;
-
- /* Next state to go to when the send or receive is finished */
- int nextstate;
-
- /* When connections fail but an error number cannot be reported
- * because the socket is non blocking we keep the connreq struct until
- * the status is queried with connect() again, we then return
- * this value */
- int err;
-
- /* Events that were set for this socket upon call to select() or
- * poll() */
- int selectevents;
-
- /* Buffer for sending and receiving on the socket */
- unsigned int datalen;
- unsigned int datadone;
- char buffer[2048];
-
- struct connreq *next;
-};
-
-/* Connection statuses */
-#define UNSTARTED 0
-#define CONNECTING 1
-#define CONNECTED 2
-#define SENDING 3
-#define RECEIVING 4
-#define SENTV4REQ 5
-#define GOTV4REQ 6
-#define SENTV5METHOD 7
-#define GOTV5METHOD 8
-#define SENTV5AUTH 9
-#define GOTV5AUTH 10
-#define SENTV5CONNECT 11
-#define GOTV5CONNECT 12
-#define DONE 13
-#define FAILED 14
-
-/* Flags to indicate what events a socket was select()ed for */
-#define READ (1<<0)
-#define WRITE (1<<1)
-#define EXCEPT (1<<2)
-#define READWRITE (READ|WRITE)
-#define READWRITEEXCEPT (READ|WRITE|EXCEPT)
-
-#endif
1
0
commit 0817b3a86108a428c39b2965c579e848ade73199
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Sat Feb 19 18:08:17 2011 +0000
Fix typo in test/Makefile.am
---
test/Makefile.am | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/test/Makefile.am b/test/Makefile.am
index 47b78f3..c03c387 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -2,5 +2,5 @@ noinst_PROGRAMS= test_torsocks
LIBS = -lresolv
-torsocks_test_SOURCES= test_torsocks.c
+test_torsocks_SOURCES= test_torsocks.c
CLEANFILES= test_torsocks
1
0
commit 6d595039efef25fe5bab96c1913fac88e376d8ce
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Sun Feb 20 10:51:44 2011 +0000
Fix copyright notice
---
test/run_tests.sh | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/test/run_tests.sh b/test/run_tests.sh
index 5dc50f7..5582102 100755
--- a/test/run_tests.sh
+++ b/test/run_tests.sh
@@ -1,7 +1,7 @@
#! /bin/sh
# ***************************************************************************
# * *
-# * Copyright (C) 2008-2011 Robert Hogan <robert(a)roberthogan.net> *
+# * Copyright (C) 2011 Robert Hogan <robert(a)roberthogan.net> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License as published by *
1
0
23 Oct '11
commit 34eab39159ba288b370464f54b042f30ccdb470b
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Sat Feb 19 17:47:36 2011 +0000
Set environment variable for test run
---
test/run_tests.sh | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/test/run_tests.sh b/test/run_tests.sh
index 5776bfe..5dc50f7 100755
--- a/test/run_tests.sh
+++ b/test/run_tests.sh
@@ -18,6 +18,7 @@
# * Free Software Foundation, Inc., *
#* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
# ***************************************************************************
+export TORSOCKS_DEBUG=2
TORSOCKS="`which torsocks`"
if [ ! -x "$TORSOCKS" ]; then
@@ -40,3 +41,4 @@ else
echo "Tests failed. Please post this output to http://code.google.com/p/torsocks/issues/entry"
fi
rm -f /tmp/newresults.txt
+export TORSOCKS_DEBUG=
\ No newline at end of file
1
0
commit 6c84bdee0b3da63c1492024b0f267899ae4a4ca0
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Thu Feb 17 20:23:16 2011 +0000
Add a rudimentary test suite
---
Makefile.am | 2 +-
configure.in | 2 +-
src/common.h | 5 +-
src/dead_pool.c | 7 +-
src/parser.c | 2 +-
src/torsocks.c | 26 ++--
test/Makefile.am | 6 +
test/README | 5 +-
test/expectedresults.txt | 120 ++++++++++++
test/gethostbyaddr.c | 21 --
test/resinit.c | 204 ---------------------
test/run_tests.sh | 42 +++++
test/test_torsocks.c | 457 ++++++++++++++++++++++++++++++++++++++++++++++
test/udp.c | 89 ---------
14 files changed, 652 insertions(+), 336 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 02520f2..44a7fbd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,4 +2,4 @@
# have all needed files, that a GNU package needs
AUTOMAKE_OPTIONS = foreign 1.4
-SUBDIRS = src
+SUBDIRS = src test
diff --git a/configure.in b/configure.in
index 614f34e..f238caa 100644
--- a/configure.in
+++ b/configure.in
@@ -612,5 +612,5 @@ AC_ENABLE_STATIC
AC_CONFIG_FILES([src/usewithtor src/torsocks src/torsocks.conf.5 src/torsocks.8 src/usewithtor.1 src/torsocks.1])
-AC_OUTPUT(Makefile src/Makefile)
+AC_OUTPUT(Makefile src/Makefile test/Makefile)
diff --git a/src/common.h b/src/common.h
index 656aefb..1fc4589 100644
--- a/src/common.h
+++ b/src/common.h
@@ -64,8 +64,9 @@ unsigned int resolve_ip(char *, int, int);
#define MSGNONE -1
#define MSGERR 0
#define MSGWARN 1
-#define MSGNOTICE 2
-#define MSGDEBUG 2
+#define MSGTEST 2
+#define MSGNOTICE 3
+#define MSGDEBUG 3
/* Required by some BSDs */
#ifndef MAP_ANONYMOUS
diff --git a/src/dead_pool.c b/src/dead_pool.c
index 6f4043a..0ef0d30 100644
--- a/src/dead_pool.c
+++ b/src/dead_pool.c
@@ -100,7 +100,7 @@ init_pool(unsigned int pool_size, struct in_addr deadrange_base,
return NULL;
}
- show_msg(MSGWARN, "init_pool: sockshost %s \n", sockshost);
+ show_msg(MSGDEBUG, "init_pool: sockshost %s \n", sockshost);
/* Initialize the dead_pool structure */
#ifdef HAVE_INET_ATON
@@ -590,7 +590,7 @@ our_gethostbyaddr(dead_pool *pool, const void *_addr, socklen_t len, int type)
he.h_addrtype = type;
he.h_addr_list = addrs;
- show_msg(MSGDEBUG, "our_gethostbyaddr: resolved '%s' to: '%s'\n",
+ show_msg(MSGTEST, "our_gethostbyaddr: resolved '%s' to: '%s'\n",
inet_ntoa(*((struct in_addr *)he.h_addr)), result_hostname);
return &he;
@@ -605,7 +605,7 @@ our_gethostbyname(dead_pool *pool, const char *name)
static struct hostent he;
static char *addrs[2];
- show_msg(MSGDEBUG, "our_gethostbyname: '%s' requested\n", name);
+ show_msg(MSGTEST, "our_gethostbyname: '%s' requested\n", name);
pos = store_pool_entry(pool,(char *) name, &addr);
if(pos == -1) {
@@ -735,6 +735,7 @@ our_getaddrinfo(dead_pool *pool, const char *node, const char *service,
ret = realgetaddrinfo(node, service, hints, res);
}
+ show_msg(MSGTEST, "our_getaddrinfo: '%s' requested\n", service);
return ret;
}
diff --git a/src/parser.c b/src/parser.c
index 592af18..c7c6290 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -82,7 +82,7 @@ int read_config (char *filename, struct parsedfile *config) {
/* Insure null termination */
line[sizeof(line) - 1] = (char) 0;
filename = line;
- show_msg(MSGWARN, "Configuration file not provided by TORSOCKS_CONF_FILE "
+ show_msg(MSGDEBUG, "Configuration file not provided by TORSOCKS_CONF_FILE "
"environment variable, attempting to use defaults in %s.\n", filename);
}
diff --git a/src/torsocks.c b/src/torsocks.c
index 98af4b0..8099a5e 100644
--- a/src/torsocks.c
+++ b/src/torsocks.c
@@ -140,13 +140,11 @@ void torsocks_init(void)
/* This has been observed on Snow Leopard for instance. */
torsocks_init_complete = 1;
- show_msg(MSGWARN, "In torsocks_init \n");
+ show_msg(MSGDEBUG, "In torsocks_init \n");
get_environment();
get_config();
- show_msg(MSGWARN, "In torsocks_init after env/config\n");
-
#ifdef USE_OLD_DLSYM
void *lib;
#endif
@@ -202,7 +200,7 @@ void torsocks_init(void)
torsocks_init_complete=1;
pthread_mutex_unlock(&torsocks_init_mutex);
- show_msg(MSGWARN, "Exit torsocks_init \n");
+ show_msg(MSGDEBUG, "Exit torsocks_init \n");
}
static int get_environment()
@@ -220,7 +218,7 @@ static int get_environment()
loglevel = atoi(env);
if (((env = getenv("TORSOCKS_DEBUG_FILE"))) && !suid)
logfile = env;
- set_log_options(loglevel, logfile, 1);
+ set_log_options(loglevel, logfile, (loglevel == MSGTEST) ? 0 : 1);
done = 1;
@@ -287,7 +285,7 @@ int torsocks_connect_guts(CONNECT_SIGNATURE, int (*original_connect)(CONNECT_SIG
return(-1);
}
- show_msg(MSGDEBUG, "Got connection request\n");
+ show_msg(MSGTEST, "Got connection request\n");
connaddr = (struct sockaddr_in *) __addr;
@@ -456,6 +454,7 @@ int torsocks_select_guts(SELECT_SIGNATURE, int (*original_select)(SELECT_SIGNATU
if (!torsocks_init_complete)
torsocks_init();
+ show_msg(MSGTEST, "Intercepted call to select\n");
show_msg(MSGDEBUG, "Intercepted call to select with %d fds, "
"0x%08x 0x%08x 0x%08x, timeout %08x\n", n,
readfds, writefds, exceptfds, timeout);
@@ -641,6 +640,7 @@ int torsocks_poll_guts(POLL_SIGNATURE, int (*original_poll)(POLL_SIGNATURE))
if (!torsocks_init_complete)
torsocks_init();
+ show_msg(MSGTEST, "Intercepted call to poll\n");
show_msg(MSGDEBUG, "Intercepted call to poll with %d fds, "
"0x%08x timeout %d\n", nfds, ufds, timeout);
@@ -807,6 +807,7 @@ int torsocks_close_guts(CLOSE_SIGNATURE, int (*original_close)(CLOSE_SIGNATURE))
return(-1);
}
+ show_msg(MSGTEST, "Got call to close()\n");
show_msg(MSGDEBUG, "Call to close(%d)\n", fd);
rc = original_close(fd);
@@ -854,6 +855,7 @@ int torsocks_getpeername_guts(GETPEERNAME_SIGNATURE,
return(-1);
}
+ show_msg(MSGTEST, "Intercepted call to getpeername\n");
show_msg(MSGDEBUG, "Call to getpeername for fd %d\n", __fd);
@@ -882,7 +884,7 @@ int res_init(void)
if (!realres_init && ((realres_init = dlsym(RTLD_NEXT, "res_init")) == NULL))
LOAD_ERROR("res_init", MSGERR);
- show_msg(MSGDEBUG, "Got res_init request\n");
+ show_msg(MSGTEST, "Got res_init request\n");
/* See comment in close() */
if (!torsocks_init_complete)
@@ -907,7 +909,7 @@ int EXPAND_GUTS_NAME(res_query)(RES_QUERY_SIGNATURE, int (*original_res_query)(R
if (!original_res_query && ((original_res_query = dlsym(RTLD_NEXT, "res_query")) == NULL))
LOAD_ERROR("res_query", MSGERR);
- show_msg(MSGDEBUG, "Got res_query request\n");
+ show_msg(MSGTEST, "Got res_query request\n");
/* See comment in close() */
if (!torsocks_init_complete)
@@ -967,7 +969,7 @@ int EXPAND_GUTS_NAME(res_search)(RES_SEARCH_SIGNATURE, int (*original_res_search
((original_res_search = dlsym(RTLD_NEXT, "res_search")) == NULL))
LOAD_ERROR("res_search", MSGERR);
- show_msg(MSGDEBUG, "Got res_search request\n");
+ show_msg(MSGTEST, "Got res_search request\n");
/* See comment in close() */
if (!torsocks_init_complete)
@@ -996,7 +998,7 @@ int EXPAND_GUTS_NAME(res_send)(RES_SEND_SIGNATURE, int (*original_res_send)(RES_
if (!original_res_send && ((original_res_send = dlsym(RTLD_NEXT, "res_send")) == NULL))
LOAD_ERROR("res_send", MSGERR);
- show_msg(MSGDEBUG, "Got res_send request\n");
+ show_msg(MSGTEST, "Got res_send request\n");
/* See comment in close() */
if (!torsocks_init_complete)
@@ -1089,7 +1091,7 @@ ssize_t torsocks_sendto_guts(SENDTO_SIGNATURE, ssize_t (*original_sendto)(SENDTO
return(-1);
}
- show_msg(MSGDEBUG, "Got sendto request\n");
+ show_msg(MSGTEST, "Got sendto request\n");
/* Get the type of the socket */
getsockopt(s, SOL_SOCKET, SO_TYPE,
@@ -1133,7 +1135,7 @@ ssize_t torsocks_sendmsg_guts(SENDMSG_SIGNATURE, ssize_t (*original_sendmsg)(SEN
return(-1);
}
- show_msg(MSGDEBUG, "Got sendmsg request\n");
+ show_msg(MSGTEST, "Got sendmsg request\n");
/* Get the type of the socket */
getsockopt(s, SOL_SOCKET, SO_TYPE,
diff --git a/test/Makefile.am b/test/Makefile.am
new file mode 100644
index 0000000..47b78f3
--- /dev/null
+++ b/test/Makefile.am
@@ -0,0 +1,6 @@
+noinst_PROGRAMS= test_torsocks
+
+LIBS = -lresolv
+
+torsocks_test_SOURCES= test_torsocks.c
+CLEANFILES= test_torsocks
diff --git a/test/README b/test/README
index c746800..4d65948 100644
--- a/test/README
+++ b/test/README
@@ -1,2 +1,3 @@
-This folder contains some simple test utilties for various libc function that
-libtorsocks is expected to hook.
\ No newline at end of file
+To run the tests:
+$./run_tests.sh
+
diff --git a/test/expectedresults.txt b/test/expectedresults.txt
new file mode 100644
index 0000000..53f1105
--- /dev/null
+++ b/test/expectedresults.txt
@@ -0,0 +1,120 @@
+libtorsocks: The symbol getipnodebyname() was not found in any shared library. The error reported was: not found!
+libtorsocks: The symbol getipnodebyname() was not found in any shared library. The error reported was: not found!
+libtorsocks: The symbol getipnodebyname() was not found in any shared library. The error reported was: not found!
+libtorsocks: our_getaddrinfo: 'www.torproject.org' requested
+libtorsocks: Got sendmsg request
+libtorsocks: sendmsg: Connection is a UDP or ICMP stream, may be a DNS request or other form of leak: rejecting.
+libtorsocks: Got sendto request
+libtorsocks: sendto: Connection is a UDP or ICMP stream, may be a DNS request or other form of leak: rejecting.
+libtorsocks: Got sendto request
+libtorsocks: sendto: Connection is a UDP or ICMP stream, may be a DNS request or other form of leak: rejecting.
+libtorsocks: Got connection request
+libtorsocks: connect: Connection is a UDP or ICMP stream, may be a DNS request or other form of leak: rejecting.
+libtorsocks: our_gethostbyaddr: resolved '38.229.70.16' to: 'vescum.torproject.org'
+libtorsocks: our_gethostbyname: 'www.torproject.org' requested
+libtorsocks: Got connection request
+libtorsocks: connect: Connection is to a local address (192.168.1.1), may be a TCP DNS request to a local DNS server so have to reject to be safe. Please report a bug to http://code.google.com/p/torsocks/issues/entry if this is preventing a program from working properly with torsocks.
+libtorsocks: Got connection request
+libtorsocks: Intercepted call to getpeername
+libtorsocks: Got res_init request
+libtorsocks: Got connection request
+libtorsocks: Intercepted call to getpeername
+libtorsocks: Got connection request
+libtorsocks: Intercepted call to getpeername
+libtorsocks: Got connection request
+libtorsocks: Intercepted call to getpeername
+libtorsocks: Got connection request
+libtorsocks: Intercepted call to getpeername
+libtorsocks: Got res_init request
+libtorsocks: Got connection request
+libtorsocks: connect: Connection is to a local address (192.168.1.1), may be a TCP DNS request to a local DNS server so have to reject to be safe. Please report a bug to http://code.google.com/p/torsocks/issues/entry if this is preventing a program from working properly with torsocks.
+libtorsocks: Got connection request
+libtorsocks: connect: Connection is to a local address (192.168.1.1), may be a TCP DNS request to a local DNS server so have to reject to be safe. Please report a bug to http://code.google.com/p/torsocks/issues/entry if this is preventing a program from working properly with torsocks.
+libtorsocks: Got connection request
+libtorsocks: connect: Connection is to a local address (192.168.1.1), may be a TCP DNS request to a local DNS server so have to reject to be safe. Please report a bug to http://code.google.com/p/torsocks/issues/entry if this is preventing a program from working properly with torsocks.
+libtorsocks: Got connection request
+libtorsocks: connect: Connection is to a local address (192.168.1.1), may be a TCP DNS request to a local DNS server so have to reject to be safe. Please report a bug to http://code.google.com/p/torsocks/issues/entry if this is preventing a program from working properly with torsocks.
+libtorsocks: Got connection request
+libtorsocks: connect: Connection is to a local address (192.168.1.1), may be a TCP DNS request to a local DNS server so have to reject to be safe. Please report a bug to http://code.google.com/p/torsocks/issues/entry if this is preventing a program from working properly with torsocks.
+socket: Operation not permitted
+
+----------------------getaddrinfo() TEST-----------------
+
+getaddrinfo: Servname not supported for ai_socktype
+
+----------------------UDP TEST----------------------
+
+
+----------------------udp sendmsg() TEST-------------------
+
+sendmsg() returned ret=-1 wb=0
+
+----------------------udp sendto() TEST--------------------
+
+sendto() returned ret=-1 wb=0
+
+----------------------udp connect() TEST-------------------
+
+Connect returned ret=-1
+
+----------------------udp send() TEST----------------------
+
+Note: no interception by torsocks expected as send() requires a socket in a connected state.
+send() returned ret=-1 wb=0
+
+----------------------gethostbyaddr() TEST-----------------
+
+vescum.torproject.org -> 38.229.70.16
+
+----------------------gethostbyname() TEST-----------------
+
+www.torproject.org -> 38.229.70.16
+
+---------------------- local connect() TEST----------------------
+
+
+---------------------- internet connect() TEST----------------------
+
+
+---------------------- internet res_init() TEST----------------------
+
+nameserver for test: 8.8.8.8
+
+---------------------- internet res_query() TEST----------------------
+
+return code: 102
+
+---------------------- internet res_search() TEST----------------------
+
+return code: 102
+
+--------------- internet res_querydomain() TEST----------------------
+
+return code: -1
+
+---------------------- internet res_send() TEST----------------------
+
+return code: -1
+
+---------------------- local res_init() TEST----------------------
+
+nameserver for test: 192.168.1.1
+
+---------------------- local res_query() TEST----------------------
+
+return code: -1
+
+---------------------- local res_search() TEST----------------------
+
+return code: -1
+
+--------------- local res_querydomain() TEST----------------------
+
+return code: -1
+
+---------------------- local res_send() TEST----------------------
+
+return code: -1
+
+----------------icmp() TEST----------------------------
+
diff --git a/test/gethostbyaddr.c b/test/gethostbyaddr.c
deleted file mode 100644
index c3e719e..0000000
--- a/test/gethostbyaddr.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <netdb.h>
-#include <stdio.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-//gcc -fPIC -g -O2 -Wall -I. -o gethostbyaddr gethostbyaddr.c -lc
-int main() {
- struct in_addr bar;
- struct hostent *foo;
- inet_aton("64.4.33.7",&bar);
- foo=gethostbyaddr(&bar,4,AF_INET);
- if (foo) {
- int i;
- for (i=0; foo->h_addr_list[i]; ++i)
- printf("%s -> %s\n",foo->h_name,inet_ntoa(*(struct in_addr*)foo->h_addr_list[i]));
- for (i=0; foo->h_aliases[i]; ++i)
- printf(" also known as %s\n",foo->h_aliases[i]);
- }
- return 0;
-}
-
diff --git a/test/resinit.c b/test/resinit.c
deleted file mode 100644
index 73a9d57..0000000
--- a/test/resinit.c
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- * Copyright (C) 2004 Tomasz Kojm <tkojm(a)clamav.net>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * 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 <stdio.h>
-
-#include <string.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <arpa/nameser.h>
-#include <resolv.h>
-#include <sys/types.h>
-
-
-#include <netdb.h>
-#include <stdio.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <sysexits.h>
-#include <syslog.h>
-#include <pthread.h>
-
-#include <netdb.h>
-#include <arpa/nameser.h>
-#include <resolv.h>
-
-#ifndef LINUX
-#include <sys/queue.h>
-#else
-#include "queue.h"
-#endif
-
-#ifndef PACKETSZ
-#define PACKETSZ 512
-#endif
-
-static char *txtquery(const char *domain, unsigned int *ttl)
-{
- unsigned char answer[PACKETSZ], *answend, *pt;
- char *txt, host[128];
- int len, type, qtype;
- unsigned int cttl, size, txtlen = 0;
-
-
- *ttl = 0;
- if(res_init() < 0) {
- printf("^res_init failed\n");
- return NULL;
- }
-
- printf("*Querying %s\n", domain);
-
- memset(answer, 0, PACKETSZ);
- qtype = T_TXT;
- if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0 || len > PACKETSZ) {
- /* The DNS server in the SpeedTouch Alcatel 510 modem can't
- * handle a TXT-query, but it can resolve an ANY-query to a
- * TXT-record, so we try an ANY-query now. The thing we try
- * to resolve normally only has a TXT-record anyway.
- */
- memset(answer, 0, PACKETSZ);
- qtype=T_ANY;
- if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0) {
- printf("^Can't query %s\n", domain);
- return NULL;
- }
- }
-
- answend = answer + len;
- pt = answer + sizeof(HEADER);
-
- if((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) {
- printf("^dn_expand failed\n");
- return NULL;
- }
-
- pt += len;
- if(pt > answend-4) {
- printf("^Bad (too short) DNS reply\n");
- return NULL;
- }
-
- GETSHORT(type, pt);
- if(type != qtype) {
- printf("^Broken DNS reply.\n");
- return NULL;
- }
-
- pt += INT16SZ; /* class */
- size = 0;
- do { /* recurse through CNAME rr's */
- pt += size;
- if((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) {
- printf("^second dn_expand failed\n");
- return NULL;
- }
- printf("^Host: %s\n", host);
- pt += len;
- if(pt > answend-10) {
- printf("^Bad (too short) DNS reply\n");
- return NULL;
- }
- GETSHORT(type, pt);
- pt += INT16SZ; /* class */
- GETLONG(cttl, pt);
- GETSHORT(size, pt);
- if(pt + size < answer || pt + size > answend) {
- printf("^DNS rr overflow\n");
- return NULL;
- }
- } while(type == T_CNAME);
-
- if(type != T_TXT) {
- printf("^Not a TXT record\n");
- return NULL;
- }
-
- if(!size || (txtlen = *pt) >= size || !txtlen) {
- printf("^Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size);
- return NULL;
- }
-
- if(!(txt = (char *) malloc(txtlen + 1)))
- return NULL;
-
- memcpy(txt, pt+1, txtlen);
- txt[txtlen] = 0;
- *ttl = cttl;
-
- return txt;
-}
-
-
-//gcc -fPIC -g -O2 -Wall -I. -o resinit resinit.c -lc -lresolv
-int main() {
- unsigned char dnsreply[1024];
- unsigned char host[128];
- char *dnsrep;
- int ret = 0;
- unsigned int ttl=0;
-
- memset( dnsreply, '\0', sizeof( dnsreply ));
-// if (res_init() == -1)
-// {
-// printf("res_init failed\n");
-// return -1;
-// }
-
- snprintf((char *)host, 127, "www.google.com");
- dnsrep = txtquery((const char *)host, &ttl);
- printf("RES_QUERY results: %s.", dnsrep);
- printf("return code: %i\n", ret);
-
- snprintf((char *)host, 127, "www.google.com");
- ret = res_query( (char *) host, C_IN, T_TXT, dnsreply, sizeof( dnsreply ));
- printf("RES_QUERY results: %s.", dnsreply);
- printf("return code: %i\n", ret);
-
- memset( dnsreply, '\0', sizeof( dnsreply ));
- ret = res_search( (char *) host, C_IN, T_TXT, dnsreply, sizeof( dnsreply ));
- printf("RES_SEARCH results: %s.", dnsreply);
- printf("return code: %i\n", ret);
-
- memset( dnsreply, '\0', sizeof( dnsreply ));
- ret = res_querydomain( "www.google.com", "google.com", C_IN, T_TXT, dnsreply, sizeof( dnsreply ));
- printf("RES_QUERYDOMAIN results: %s.", dnsreply);
- printf("return code: %i\n", ret);
-
- memset( dnsreply, '\0', sizeof( dnsreply ));
- ret = res_send( host, 32, dnsreply, sizeof( dnsreply ));
- printf("RES_SEND results: %s.", dnsreply);
- printf("return code: %i\n", ret);
-
- return ret;
-}
-
-
diff --git a/test/run_tests.sh b/test/run_tests.sh
new file mode 100755
index 0000000..5776bfe
--- /dev/null
+++ b/test/run_tests.sh
@@ -0,0 +1,42 @@
+#! /bin/sh
+# ***************************************************************************
+# * *
+# * Copyright (C) 2008-2011 Robert Hogan <robert(a)roberthogan.net> *
+# * *
+# * This program is free software; you can redistribute it and/or modify *
+# * it under the terms of the GNU General Public License as published by *
+# * the Free Software Foundation; either version 2 of the License, or *
+# * (at your option) any later version. *
+# * *
+# * 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., *
+#* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+# ***************************************************************************
+TORSOCKS="`which torsocks`"
+
+if [ ! -x "$TORSOCKS" ]; then
+ echo "torsocks doesn't exist." >&2
+ echo "Perhaps you haven't installed torsocks yet?" >&2
+ exit 1
+fi
+
+if [ ! -f ./test_torsocks ]; then
+ echo "test_torsocks binary doesn't exist in this directory." >&2
+ echo "Perhaps you haven't compiled torsocks yet?" >&2
+ exit 1
+fi
+
+torsocks ./test_torsocks > /tmp/newresults.txt 2>&1
+output=`diff expectedresults.txt /tmp/newresults.txt`
+if ["$output" = ""]; then
+ echo "Tests passed"
+else
+ echo "Tests failed. Please post this output to http://code.google.com/p/torsocks/issues/entry"
+fi
+rm -f /tmp/newresults.txt
diff --git a/test/test_torsocks.c b/test/test_torsocks.c
new file mode 100644
index 0000000..e67de0a
--- /dev/null
+++ b/test/test_torsocks.c
@@ -0,0 +1,457 @@
+/***************************************************************************
+ * *
+ * Copyright (c) 2000 Alessandro Iurlano. *
+ * Copyright (C) 2004 Tomasz Kojm <tkojm(a)clamav.net> *
+ * Copyright (C) 2011 Robert Hogan <robert(a)roberthogan.net> *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * 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., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
+#include <netinet/ip_icmp.h>
+#include <pthread.h>
+#include <resolv.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <sysexits.h>
+#include <syslog.h>
+#include <unistd.h>
+
+#ifndef LINUX
+#include <sys/queue.h>
+#else
+#include "queue.h"
+#endif
+
+#ifndef PACKETSZ
+#define PACKETSZ 512
+#endif
+
+static unsigned short csum (unsigned short *buf, int nwords)
+{
+ unsigned long sum;
+ for (sum = 0; nwords > 0; nwords--)
+ sum += *buf++;
+ sum = (sum >> 16) + (sum & 0xffff);
+ sum += (sum >> 16);
+ return ~sum;
+}
+
+static int icmp_test()
+{
+
+ int sockfd;
+ char datagram[400];
+ struct sockaddr_in dest;
+ struct ip *iphdr=(struct ip *) datagram;
+ struct icmphdr *icmphdr=(struct icmphdr *)(iphdr +1);
+ char *buff=(icmphdr +1);
+ printf("\n----------------icmp() TEST----------------------------\n\n");
+
+ if((sockfd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))<0)
+ {
+ perror("socket");
+ exit(1);
+ }
+
+ memset(datagram,0,400);
+ strcpy(buff,"entzwei");
+ dest.sin_family=AF_INET;
+ dest.sin_addr.s_addr=inet_addr("192.168.1.33");
+
+ iphdr->ip_v=4;
+ iphdr->ip_hl=5;
+ iphdr->ip_len=sizeof(datagram);
+ iphdr->ip_id=htonl(54321);
+ iphdr->ip_off=0;
+ iphdr->ip_ttl=225;
+ iphdr->ip_p=1;
+ iphdr->ip_sum=0;
+ iphdr->ip_tos=0;
+ iphdr->ip_src.s_addr=inet_addr("192.168.1.35");
+ iphdr->ip_dst.s_addr=dest.sin_addr.s_addr;
+ iphdr->ip_sum=csum((unsigned short *)datagram,iphdr->ip_len >> 1);
+
+ icmphdr->type=130;
+ icmphdr->code=0;
+ icmphdr->checksum=htons(0xc3b0);
+ icmphdr->un.echo.sequence=0;
+ icmphdr->un.echo.id=0;
+
+ int one=1;
+ int *val=&one;
+ if(setsockopt(sockfd,IPPROTO_IP,IP_HDRINCL,val,sizeof(one))<0)
+ printf("cannot set HDRINCL!\n");
+
+
+ if(sendto(sockfd,datagram,35,0,(struct sockaddr *)&dest,sizeof(dest))<0)
+ {
+ perror("sendto");
+ exit(1);
+ }
+
+ return(0);
+}
+
+static char *txtquery(const char *domain, unsigned int *ttl)
+{
+ unsigned char answer[PACKETSZ], *answend, *pt;
+ char *txt, host[128];
+ int len, type, qtype;
+ unsigned int cttl, size, txtlen = 0;
+
+
+ *ttl = 0;
+ if(res_init() < 0) {
+ printf("^res_init failed\n");
+ return NULL;
+ }
+
+ printf("*Querying %s\n", domain);
+
+ memset(answer, 0, PACKETSZ);
+ qtype = T_TXT;
+ if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0 || len > PACKETSZ) {
+ /* The DNS server in the SpeedTouch Alcatel 510 modem can't
+ * handle a TXT-query, but it can resolve an ANY-query to a
+ * TXT-record, so we try an ANY-query now. The thing we try
+ * to resolve normally only has a TXT-record anyway.
+ */
+ memset(answer, 0, PACKETSZ);
+ qtype=T_ANY;
+ if((len = res_query(domain, C_IN, qtype, answer, PACKETSZ)) < 0) {
+ printf("^Can't query %s\n", domain);
+ return NULL;
+ }
+ }
+
+ answend = answer + len;
+ pt = answer + sizeof(HEADER);
+
+ if((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) {
+ printf("^dn_expand failed\n");
+ return NULL;
+ }
+
+ pt += len;
+ if(pt > answend-4) {
+ printf("^Bad (too short) DNS reply\n");
+ return NULL;
+ }
+
+ GETSHORT(type, pt);
+ if(type != qtype) {
+ printf("^Broken DNS reply.\n");
+ return NULL;
+ }
+
+ pt += INT16SZ; /* class */
+ size = 0;
+ do { /* recurse through CNAME rr's */
+ pt += size;
+ if((len = dn_expand(answer, answend, pt, host, sizeof(host))) < 0) {
+ printf("^second dn_expand failed\n");
+ return NULL;
+ }
+ printf("^Host: %s\n", host);
+ pt += len;
+ if(pt > answend-10) {
+ printf("^Bad (too short) DNS reply\n");
+ return NULL;
+ }
+ GETSHORT(type, pt);
+ pt += INT16SZ; /* class */
+ GETLONG(cttl, pt);
+ GETSHORT(size, pt);
+ if(pt + size < answer || pt + size > answend) {
+ printf("^DNS rr overflow\n");
+ return NULL;
+ }
+ } while(type == T_CNAME);
+
+ if(type != T_TXT) {
+ printf("^Not a TXT record\n");
+ return NULL;
+ }
+
+ if(!size || (txtlen = *pt) >= size || !txtlen) {
+ printf("^Broken TXT record (txtlen = %d, size = %d)\n", txtlen, size);
+ return NULL;
+ }
+
+ if(!(txt = (char *) malloc(txtlen + 1)))
+ return NULL;
+
+ memcpy(txt, pt+1, txtlen);
+ txt[txtlen] = 0;
+ *ttl = cttl;
+
+ return txt;
+}
+
+static int res_tests(char *ip, char *test) {
+ unsigned char dnsreply[1024];
+ unsigned char host[128];
+ int ret = 0, i;
+ char buf[16];
+ struct sockaddr_in addr;
+
+ memset( dnsreply, '\0', sizeof( dnsreply ));
+
+ printf("\n---------------------- %s res_init() TEST----------------------\n\n", test);
+ if (res_init() == -1) {
+ printf("res_init failed\n");
+ return -1;
+ }
+
+ inet_ntop(AF_INET, &_res.nsaddr_list[0].sin_addr.s_addr, buf, sizeof(buf));
+
+ addr.sin_family=AF_INET;
+ addr.sin_port=htons(53);
+ addr.sin_addr.s_addr=inet_addr(ip);
+
+ for (i = 0; i < _res.nscount; i++)
+ memcpy(&_res.nsaddr_list[i], &addr, sizeof(struct sockaddr_in));
+
+ inet_ntop(AF_INET, &_res.nsaddr_list[0].sin_addr.s_addr, buf, sizeof(buf));
+ printf("nameserver for test: %s\n", buf);
+
+ printf("\n---------------------- %s res_query() TEST----------------------\n\n", test);
+ snprintf((char *)host, 127, "www.google.com");
+ ret = res_nquery( &_res, (char *) host, C_IN, T_TXT, dnsreply, sizeof( dnsreply ));
+// printf("RES_QUERY results: %s.\n", dnsreply);
+ printf("return code: %i\n", ret);
+
+ printf("\n---------------------- %s res_search() TEST----------------------\n\n", test);
+ memset( dnsreply, '\0', sizeof( dnsreply ));
+ ret = res_nsearch( &_res, (char *) host, C_IN, T_TXT, dnsreply, sizeof( dnsreply ));
+// printf("RES_SEARCH results: %s.\n", dnsreply);
+ printf("return code: %i\n", ret);
+
+ printf("\n--------------- %s res_querydomain() TEST----------------------\n\n", test);
+ memset( dnsreply, '\0', sizeof( dnsreply ));
+ ret = res_nquerydomain( &_res, "www.google.com", "google.com", C_IN, T_TXT, dnsreply, sizeof( dnsreply ));
+// printf("RES_QUERYDOMAIN results: %s.\n", dnsreply);
+ printf("return code: %i\n", ret);
+
+ printf("\n---------------------- %s res_send() TEST----------------------\n\n", test);
+ memset( dnsreply, '\0', sizeof( dnsreply ));
+ ret = res_nsend( &_res, host, 32, dnsreply, sizeof( dnsreply ));
+// printf("RES_SEND results: %s.\n", dnsreply);
+ printf("return code: %i\n", ret);
+
+ return ret;
+}
+
+static int res_internet_tests() {
+ char *ip = "8.8.8.8";
+ char *test = "internet";
+ return res_tests(ip, test);
+}
+
+static int res_local_tests() {
+ char *ip = "192.168.1.1";
+ char *test = "local";
+ return res_tests(ip, test);
+}
+
+static int udp_test() {
+ struct sockaddr_in addr;
+ char testtext[]="This message should be sent via udp\nThis is row number 2\nAnd then number three\n";
+ int sock,ret,wb,flags=0;
+ char *ip = "6.6.6.6";
+
+ printf("\n----------------------UDP TEST----------------------\n\n");
+
+ addr.sin_family=AF_INET;
+ addr.sin_port=53;
+ addr.sin_addr.s_addr=inet_addr(ip);
+
+ sock=socket(AF_INET,SOCK_DGRAM,0);
+
+ struct iovec iov;
+ struct msghdr msg;
+
+ iov.iov_base = (void *)testtext;
+ iov.iov_len = strlen(testtext);
+
+ msg.msg_name = (struct sockaddr *)&addr;
+ msg.msg_namelen = sizeof(addr);
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = NULL;
+ msg.msg_controllen = 0;
+
+ printf("\n----------------------udp sendmsg() TEST-------------------\n\n");
+ wb=0;
+ ret=sendmsg(sock, &msg, flags);
+ printf("sendmsg() returned ret=%d wb=%d\n",ret,wb);
+
+ printf("\n----------------------udp sendto() TEST--------------------\n\n");
+ wb=0;
+ ret=sendto(sock,testtext,strlen(testtext)+1,wb, (struct sockaddr*)&addr, sizeof(addr));
+ ret=sendto(sock,"CiaoCiao",strlen("CiaoCiao")+1,wb, (struct sockaddr*)&addr, sizeof(addr));
+ printf("sendto() returned ret=%d wb=%d\n",ret,wb);
+
+ printf("\n----------------------udp connect() TEST-------------------\n\n");
+ ret=connect(sock,(struct sockaddr*)&addr,sizeof(addr));
+ printf("Connect returned ret=%d\n",ret);
+
+ printf("\n----------------------udp send() TEST----------------------\n\n");
+ wb=0;
+ ret=send(sock,testtext,strlen(testtext)+1,wb);
+ ret=send(sock,"CiaoCiao",strlen("CiaoCiao")+1,wb);
+ printf("Note: no interception by torsocks expected as send() requires a socket in a connected state.\n");
+ printf("send() returned ret=%d wb=%d\n",ret,wb);
+
+ return 0;
+}
+
+static int gethostbyname_test() {
+ struct hostent *foo;
+
+ printf("\n----------------------gethostbyname() TEST-----------------\n\n");
+
+ foo=gethostbyname("www.torproject.org");
+ if (foo) {
+ int i;
+ for (i=0; foo->h_addr_list[i]; ++i)
+ printf("%s -> %s\n",foo->h_name,inet_ntoa(*(struct in_addr*)foo->h_addr_list[i]));
+/* for (i=0; foo->h_aliases[i]; ++i)
+ printf(" also known as %s\n",foo->h_aliases[i]);*/
+ }
+ return 0;
+}
+
+static int gethostbyaddr_test() {
+ struct in_addr bar;
+ struct hostent *foo;
+
+ printf("\n----------------------gethostbyaddr() TEST-----------------\n\n");
+
+ inet_aton("38.229.70.16",&bar);
+ foo=gethostbyaddr(&bar,4,AF_INET);
+ if (foo) {
+ int i;
+ for (i=0; foo->h_addr_list[i]; ++i)
+ printf("%s -> %s\n",foo->h_name,inet_ntoa(*(struct in_addr*)foo->h_addr_list[i]));
+ for (i=0; foo->h_aliases[i]; ++i)
+ printf(" also known as %s\n",foo->h_aliases[i]);
+ }
+ return 0;
+}
+
+static int getaddrinfo_test() {
+ struct addrinfo hints;
+ struct addrinfo *result;
+ int s;
+
+ printf("\n----------------------getaddrinfo() TEST-----------------\n\n");
+
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
+ hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
+ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
+ hints.ai_protocol = 0; /* Any protocol */
+ hints.ai_canonname = NULL;
+ hints.ai_addr = NULL;
+ hints.ai_next = NULL;
+
+ s = getaddrinfo(NULL, "www.torproject.org", &hints, &result);
+ if (s != 0) {
+ printf("getaddrinfo: %s\n", gai_strerror(s));
+ }
+
+ return 0;
+}
+
+/* Unavailable in glibc. */
+/*
+static int getipnodebyname_test() {
+ int error;
+
+ printf("\n----------------------getipnodebyname() TEST-----------------\n\n");
+
+ getipnodebyname("www.torproject.org", AF_INET, 0, &error);
+ if (error != 0) {
+ printf("getipnodebyname error: %i\n", error);
+ }
+
+ return 0;
+}
+*/
+
+static int connect_test(const char *name, const char *ip, int port)
+{
+ int sock;
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = inet_addr(ip);
+ addr.sin_port = htons(port);
+
+ printf("\n---------------------- %s connect() TEST----------------------\n\n", name);
+
+ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
+ return 1;
+
+ if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)
+ return 1;
+
+ return 0;
+}
+
+static int connect_local_test()
+{
+ const char *ip = "192.168.1.1";
+ const char *name = "local";
+ int port = 80;
+ return connect_test(name, ip, port);
+}
+
+static int connect_internet_test()
+{
+ const char *ip = "8.8.8.8";
+ int port = 53;
+ const char *name = "internet";
+ return connect_test(name, ip, port);
+}
+
+int main() {
+
+ getaddrinfo_test();
+ udp_test();
+ gethostbyaddr_test();
+ gethostbyname_test();
+ connect_local_test();
+ connect_internet_test();
+ res_internet_tests();
+ res_local_tests();
+ icmp_test();
+
+ return 0;
+}
diff --git a/test/udp.c b/test/udp.c
deleted file mode 100644
index 4926c66..0000000
--- a/test/udp.c
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * OSSO - A Micro Kernel OS
- * Copyright (c) 2000 Alessandro Iurlano.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * 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., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
- */
-
-/******************************* O S S O ***********************************
- * file : $Source: /home/robert/Development/torsockstosvn/torsocks-cvsbackup/torsocks/test/udp.c,v $
- * Description: UDP protocol testing program.
- ***************************************************************************
-
- ***************************************************************************
- * $Id: udp.c,v 1.1 2008-06-23 19:38:34 hoganrobert Exp $
- ***************************************************************************/
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <netdb.h>
-#include <unistd.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-
-//gcc -fPIC -g -O2 -Wall -I. -o udp udp.c -lc
-
-struct sockaddr_in addr;
-char testtext[]="This message should be sent via udp\nThis is row number 2\nAnd then number three\n";
-
-
-int main(int argc, char *argv[]) {
- int sock,ret,wb,flags=0;
-
- printf("\n----------------------UDP TEST----------------------\n\n");
-
- addr.sin_family=AF_INET;
- addr.sin_port=53;
- addr.sin_addr.s_addr=159|(134<<8)|(237<<16)|(6<<24);
-
- sock=socket(AF_INET,SOCK_DGRAM,0);
-
- printf("socket returned %d\n",sock);
-
- struct iovec iov;
- struct msghdr msg;
-
- iov.iov_base = (void *)testtext;
- iov.iov_len = strlen(testtext);
-
- msg.msg_name = (struct sockaddr *)&addr;
- msg.msg_namelen = sizeof(addr);
- msg.msg_iov = &iov;
- msg.msg_iovlen = 1;
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
-
- wb=0;
- ret=sendmsg(sock, &msg, flags);
- printf("sendmsg() returned ret=%d wb=%d\n",ret,wb);
-
- wb=0;
- ret=sendto(sock,testtext,strlen(testtext)+1,wb, (struct sockaddr*)&addr, sizeof(addr));
- ret=sendto(sock,"CiaoCiao",strlen("CiaoCiao")+1,wb, (struct sockaddr*)&addr, sizeof(addr));
- printf("sendto() returned ret=%d wb=%d\n",ret,wb);
-
- ret=connect(sock,(struct sockaddr*)&addr,sizeof(addr));
- printf("Connect returned ret=%d\n",ret);
- wb=0;
- ret=send(sock,testtext,strlen(testtext)+1,wb);
- ret=send(sock,"CiaoCiao",strlen("CiaoCiao")+1,wb);
- printf("send() returned ret=%d wb=%d\n",ret,wb);
-
-
- return 0;
-}
1
0
commit d8c11fa185dbcb4ea448394670b70f51e6cd786c
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Sun Feb 20 13:52:59 2011 +0000
Fix segfault
Occurred when tor not running, torsocks run in debug/test mode, and
gethostbyaddr() fails.
---
src/dead_pool.c | 5 +++--
1 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/dead_pool.c b/src/dead_pool.c
index 0ef0d30..13e5740 100644
--- a/src/dead_pool.c
+++ b/src/dead_pool.c
@@ -590,8 +590,9 @@ our_gethostbyaddr(dead_pool *pool, const void *_addr, socklen_t len, int type)
he.h_addrtype = type;
he.h_addr_list = addrs;
- show_msg(MSGTEST, "our_gethostbyaddr: resolved '%s' to: '%s'\n",
- inet_ntoa(*((struct in_addr *)he.h_addr)), result_hostname);
+ if (result_hostname)
+ show_msg(MSGTEST, "our_gethostbyaddr: resolved '%s' to: '%s'\n",
+ inet_ntoa(*((struct in_addr *)he.h_addr)), result_hostname);
return &he;
1
0
commit da043e7e57a6ad0bd7c2a48292fa0b8ee04188cb
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Mon Feb 14 20:13:09 2011 +0000
Rename tsocks.h to torsocks.h
---
src/torsocks.c | 2 +-
src/torsocks.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/tsocks.h | 104 --------------------------------------------------------
3 files changed, 105 insertions(+), 105 deletions(-)
diff --git a/src/torsocks.c b/src/torsocks.c
index 538a182..6f3bb39 100644
--- a/src/torsocks.c
+++ b/src/torsocks.c
@@ -70,7 +70,7 @@ const char *torsocks_progname = "libtorsocks"; /* Name used in err msgs
#endif
#include <resolv.h>
#include <parser.h>
-#include <tsocks.h>
+#include <torsocks.h>
#include "dead_pool.h"
/* Some function names are macroized on Darwin. Allow those names
diff --git a/src/torsocks.h b/src/torsocks.h
new file mode 100644
index 0000000..ea298f0
--- /dev/null
+++ b/src/torsocks.h
@@ -0,0 +1,104 @@
+/***************************************************************************
+ * *
+ * Copyright (C) 2000-2008 Shaun Clowes <delius(a)progsoc.org> *
+ * Copyright (C) 2008-2011 Robert Hogan <robert(a)roberthogan.net> *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * 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., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+/* torsocks.h - Structures used by tsocks to form SOCKS requests */
+
+#ifndef _TORSOCKS_H
+
+#define _TORSOCKS_H 1
+
+#include <parser.h>
+
+/* Structure representing a socks connection request */
+struct sockreq {
+ int8_t version;
+ int8_t command;
+ int16_t dstport;
+ int32_t dstip;
+ /* A null terminated username goes here */
+};
+
+/* Structure representing a socks connection request response */
+struct sockrep {
+ int8_t version;
+ int8_t result;
+ int16_t ignore1;
+ int32_t ignore2;
+};
+
+/* Structure representing a socket which we are currently proxying */
+struct connreq {
+ /* Information about the socket and target */
+ int sockid;
+ struct sockaddr_in connaddr;
+ struct sockaddr_in serveraddr;
+
+ /* Pointer to the config entry for the socks server */
+ struct serverent *path;
+
+ /* Current state of this proxied socket */
+ int state;
+
+ /* Next state to go to when the send or receive is finished */
+ int nextstate;
+
+ /* When connections fail but an error number cannot be reported
+ * because the socket is non blocking we keep the connreq struct until
+ * the status is queried with connect() again, we then return
+ * this value */
+ int err;
+
+ /* Events that were set for this socket upon call to select() or
+ * poll() */
+ int selectevents;
+
+ /* Buffer for sending and receiving on the socket */
+ unsigned int datalen;
+ unsigned int datadone;
+ char buffer[2048];
+
+ struct connreq *next;
+};
+
+/* Connection statuses */
+#define UNSTARTED 0
+#define CONNECTING 1
+#define CONNECTED 2
+#define SENDING 3
+#define RECEIVING 4
+#define SENTV4REQ 5
+#define GOTV4REQ 6
+#define SENTV5METHOD 7
+#define GOTV5METHOD 8
+#define SENTV5AUTH 9
+#define GOTV5AUTH 10
+#define SENTV5CONNECT 11
+#define GOTV5CONNECT 12
+#define DONE 13
+#define FAILED 14
+
+/* Flags to indicate what events a socket was select()ed for */
+#define READ (1<<0)
+#define WRITE (1<<1)
+#define EXCEPT (1<<2)
+#define READWRITE (READ|WRITE)
+#define READWRITEEXCEPT (READ|WRITE|EXCEPT)
+
+#endif
diff --git a/src/tsocks.h b/src/tsocks.h
deleted file mode 100644
index b637331..0000000
--- a/src/tsocks.h
+++ /dev/null
@@ -1,104 +0,0 @@
-/***************************************************************************
- * *
- * Copyright (C) 2000-2008 Shaun Clowes <delius(a)progsoc.org> *
- * Copyright (C) 2008-2011 Robert Hogan <robert(a)roberthogan.net> *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- * 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., *
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
- ***************************************************************************/
-/* tsocks.h - Structures used by tsocks to form SOCKS requests */
-
-#ifndef _TSOCKS_H
-
-#define _TSOCKS_H 1
-
-#include <parser.h>
-
-/* Structure representing a socks connection request */
-struct sockreq {
- int8_t version;
- int8_t command;
- int16_t dstport;
- int32_t dstip;
- /* A null terminated username goes here */
-};
-
-/* Structure representing a socks connection request response */
-struct sockrep {
- int8_t version;
- int8_t result;
- int16_t ignore1;
- int32_t ignore2;
-};
-
-/* Structure representing a socket which we are currently proxying */
-struct connreq {
- /* Information about the socket and target */
- int sockid;
- struct sockaddr_in connaddr;
- struct sockaddr_in serveraddr;
-
- /* Pointer to the config entry for the socks server */
- struct serverent *path;
-
- /* Current state of this proxied socket */
- int state;
-
- /* Next state to go to when the send or receive is finished */
- int nextstate;
-
- /* When connections fail but an error number cannot be reported
- * because the socket is non blocking we keep the connreq struct until
- * the status is queried with connect() again, we then return
- * this value */
- int err;
-
- /* Events that were set for this socket upon call to select() or
- * poll() */
- int selectevents;
-
- /* Buffer for sending and receiving on the socket */
- unsigned int datalen;
- unsigned int datadone;
- char buffer[2048];
-
- struct connreq *next;
-};
-
-/* Connection statuses */
-#define UNSTARTED 0
-#define CONNECTING 1
-#define CONNECTED 2
-#define SENDING 3
-#define RECEIVING 4
-#define SENTV4REQ 5
-#define GOTV4REQ 6
-#define SENTV5METHOD 7
-#define GOTV5METHOD 8
-#define SENTV5AUTH 9
-#define GOTV5AUTH 10
-#define SENTV5CONNECT 11
-#define GOTV5CONNECT 12
-#define DONE 13
-#define FAILED 14
-
-/* Flags to indicate what events a socket was select()ed for */
-#define READ (1<<0)
-#define WRITE (1<<1)
-#define EXCEPT (1<<2)
-#define READWRITE (READ|WRITE)
-#define READWRITEEXCEPT (READ|WRITE|EXCEPT)
-
-#endif
1
0
23 Oct '11
commit 78b7f1db0a945cffd28c0641b1b99e844738c5c8
Author: Robert Hogan <robert(a)roberthogan.net>
Date: Mon Feb 14 20:58:33 2011 +0000
Rename functions from tsocks* to torsocks*
---
src/darwin_warts.c | 4 +-
src/torsocks.c | 111 ++++++++++++++++++++++++++--------------------------
src/torsocks.h | 2 +-
3 files changed, 59 insertions(+), 58 deletions(-)
diff --git a/src/darwin_warts.c b/src/darwin_warts.c
index 2222cf2..0a1b922 100644
--- a/src/darwin_warts.c
+++ b/src/darwin_warts.c
@@ -52,7 +52,7 @@ typedef struct fd_set fd_set;
struct timeval;
int (*realselect)(SELECT_SIGNATURE);
-int tsocks_select_guts(SELECT_SIGNATURE, int (*original_select)(SELECT_SIGNATURE));
+int torsocks_select_guts(SELECT_SIGNATURE, int (*original_select)(SELECT_SIGNATURE));
int select(SELECT_SIGNATURE) {
if (!realselect) {
@@ -60,7 +60,7 @@ int select(SELECT_SIGNATURE) {
if ((realselect = dlsym(RTLD_NEXT, "select")) == NULL)
LOAD_ERROR("select", MSGERR);
}
- return tsocks_select_guts(SELECT_ARGNAMES, realselect);
+ return torsocks_select_guts(SELECT_ARGNAMES, realselect);
}
#endif /* 10.6 */
diff --git a/src/torsocks.c b/src/torsocks.c
index 46557f3..579751e 100644
--- a/src/torsocks.c
+++ b/src/torsocks.c
@@ -69,13 +69,14 @@ const char *torsocks_progname = "libtorsocks"; /* Name used in err msgs
#include <sys/socket.h>
#endif
#include <resolv.h>
-#include <parser.h>
-#include <torsocks.h>
+
+#include "parser.h"
+#include "torsocks.h"
#include "dead_pool.h"
/* Some function names are macroized on Darwin. Allow those names
to expand accordingly. */
-#define EXPAND_GUTS(x) tsocks_##x##_guts
+#define EXPAND_GUTS(x) torsocks_##x##_guts
#define EXPAND_GUTS_NAME(x) EXPAND_GUTS(x)
/* Global Declarations */
@@ -94,10 +95,10 @@ static struct parsedfile config;
static struct connreq *requests = NULL;
static int suid = 0;
static char *conffile = NULL;
-static volatile int tsocks_init_complete = 0;
+static volatile int torsocks_init_complete = 0;
/* Exported Function Prototypes */
-void __attribute__ ((constructor)) tsocks_init(void);
+void __attribute__ ((constructor)) torsocks_init(void);
/* Function prototypes for our patches */
#ifdef SUPPORT_RES_API
@@ -111,8 +112,8 @@ int res_init(void);
#undef DARWIN_EXPANSION
/* Private Function Prototypes */
-/* no tsocks_res_init_guts */
-#define PATCH_TABLE_EXPANSION(e,r,s,n,b,m) r tsocks_##b##_guts(s##SIGNATURE, r (*original_##b)(s##SIGNATURE));
+/* no torsocks_res_init_guts */
+#define PATCH_TABLE_EXPANSION(e,r,s,n,b,m) r torsocks_##b##_guts(s##SIGNATURE, r (*original_##b)(s##SIGNATURE));
#include "expansion_table.h"
#undef PATCH_TABLE_EXPANSION
@@ -141,9 +142,9 @@ static int read_socksv5_auth(struct connreq *conn);
static int deadpool_init(void);
static int send_socksv4a_request(struct connreq *conn, const char *onion_host);
-static pthread_mutex_t tsocks_init_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t torsocks_init_mutex = PTHREAD_MUTEX_INITIALIZER;
-void tsocks_init(void)
+void torsocks_init(void)
{
#define LOAD_ERROR(s,l) { \
char *error; \
@@ -153,22 +154,22 @@ void tsocks_init(void)
(error)?error:"not found"); \
dlerror(); \
}
- pthread_mutex_lock(&tsocks_init_mutex);
+ pthread_mutex_lock(&torsocks_init_mutex);
/* We only need to be called once */
- if (tsocks_init_complete)
+ if (torsocks_init_complete)
return;
/* Not strictly true yet, but prevents us getting called while still in progress.*/
/* This has been observed on Snow Leopard for instance. */
- tsocks_init_complete = 1;
+ torsocks_init_complete = 1;
- show_msg(MSGWARN, "In tsocks_init \n");
+ show_msg(MSGWARN, "In torsocks_init \n");
get_environment();
get_config();
- show_msg(MSGWARN, "In tsocks_init after env/config\n");
+ show_msg(MSGWARN, "In torsocks_init after env/config\n");
#ifdef USE_OLD_DLSYM
void *lib;
@@ -222,10 +223,10 @@ void tsocks_init(void)
exit(1);
}
- tsocks_init_complete=1;
- pthread_mutex_unlock(&tsocks_init_mutex);
+ torsocks_init_complete=1;
+ pthread_mutex_unlock(&torsocks_init_mutex);
- show_msg(MSGWARN, "Exit tsocks_init \n");
+ show_msg(MSGWARN, "Exit torsocks_init \n");
}
static int get_environment()
@@ -274,7 +275,7 @@ static int get_config ()
}
/* Patch trampoline functions */
-/* no tsocks_res_init_guts */
+/* no torsocks_res_init_guts */
#define PATCH_TABLE_EXPANSION(e,r,s,n,b,m) \
r n(s##SIGNATURE) { \
if (!real##n) { \
@@ -282,12 +283,12 @@ static int get_config ()
if ((real##n = dlsym(RTLD_NEXT, m)) == NULL) \
LOAD_ERROR(m, MSG##e); \
} \
- return tsocks_##b##_guts(s##ARGNAMES, real##n); \
+ return torsocks_##b##_guts(s##ARGNAMES, real##n); \
}
#include "expansion_table.h"
#undef PATCH_TABLE_EXPANSION
-int tsocks_connect_guts(CONNECT_SIGNATURE, int (*original_connect)(CONNECT_SIGNATURE))
+int torsocks_connect_guts(CONNECT_SIGNATURE, int (*original_connect)(CONNECT_SIGNATURE))
{
struct sockaddr_in *connaddr;
struct sockaddr_in peer_address;
@@ -301,8 +302,8 @@ int tsocks_connect_guts(CONNECT_SIGNATURE, int (*original_connect)(CONNECT_SIGNA
struct connreq *newconn;
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
/* If the real connect doesn't exist, we're stuffed */
if (original_connect == NULL) {
@@ -364,7 +365,7 @@ int tsocks_connect_guts(CONNECT_SIGNATURE, int (*original_connect)(CONNECT_SIGNA
* to have the same fd as a request we haven't had the chance
* to delete yet, so we delete it here. */
show_msg(MSGDEBUG, "Call to connect received on old "
- "tsocks request for socket %d but to "
+ "torsocks request for socket %d but to "
"new destination, deleting old request\n",
newconn->sockid);
kill_socks_request(newconn);
@@ -460,7 +461,7 @@ int tsocks_connect_guts(CONNECT_SIGNATURE, int (*original_connect)(CONNECT_SIGNA
}
}
-int tsocks_select_guts(SELECT_SIGNATURE, int (*original_select)(SELECT_SIGNATURE))
+int torsocks_select_guts(SELECT_SIGNATURE, int (*original_select)(SELECT_SIGNATURE))
{
int nevents = 0;
int rc = 0;
@@ -476,8 +477,8 @@ int tsocks_select_guts(SELECT_SIGNATURE, int (*original_select)(SELECT_SIGNATURE
return(original_select(n, readfds, writefds, exceptfds, timeout));
}
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
show_msg(MSGDEBUG, "Intercepted call to select with %d fds, "
"0x%08x 0x%08x 0x%08x, timeout %08x\n", n,
@@ -647,7 +648,7 @@ int tsocks_select_guts(SELECT_SIGNATURE, int (*original_select)(SELECT_SIGNATURE
return(nevents);
}
-int tsocks_poll_guts(POLL_SIGNATURE, int (*original_poll)(POLL_SIGNATURE))
+int torsocks_poll_guts(POLL_SIGNATURE, int (*original_poll)(POLL_SIGNATURE))
{
int nevents = 0;
int rc = 0;
@@ -661,8 +662,8 @@ int tsocks_poll_guts(POLL_SIGNATURE, int (*original_poll)(POLL_SIGNATURE))
if (!requests)
return(original_poll(ufds, nfds, timeout));
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
show_msg(MSGDEBUG, "Intercepted call to poll with %d fds, "
"0x%08x timeout %d\n", nfds, ufds, timeout);
@@ -806,7 +807,7 @@ int tsocks_poll_guts(POLL_SIGNATURE, int (*original_poll)(POLL_SIGNATURE))
return(nevents);
}
-int tsocks_close_guts(CLOSE_SIGNATURE, int (*original_close)(CLOSE_SIGNATURE))
+int torsocks_close_guts(CLOSE_SIGNATURE, int (*original_close)(CLOSE_SIGNATURE))
{
int rc;
struct connreq *conn;
@@ -822,8 +823,8 @@ int tsocks_close_guts(CLOSE_SIGNATURE, int (*original_close)(CLOSE_SIGNATURE))
loading symbols now. This is a workaround for a problem I don't
really understand and have only encountered when using torsocks
with svn on Fedora 10, so definitely a hack. */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
if (original_close == NULL) {
show_msg(MSGERR, "Unresolved symbol: close\n");
@@ -862,15 +863,15 @@ int tsocks_close_guts(CLOSE_SIGNATURE, int (*original_close)(CLOSE_SIGNATURE))
* PP, Sat, 27 Mar 2004 11:30:23 +0100
*/
-int tsocks_getpeername_guts(GETPEERNAME_SIGNATURE,
+int torsocks_getpeername_guts(GETPEERNAME_SIGNATURE,
int (*original_getpeername)(GETPEERNAME_SIGNATURE))
{
struct connreq *conn;
int rc;
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
if (original_getpeername == NULL) {
show_msg(MSGERR, "Unresolved symbol: getpeername\n");
@@ -1454,8 +1455,8 @@ int res_init(void)
show_msg(MSGDEBUG, "Got res_init request\n");
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
if (realres_init == NULL) {
show_msg(MSGERR, "Unresolved symbol: res_init\n");
@@ -1479,8 +1480,8 @@ int EXPAND_GUTS_NAME(res_query)(RES_QUERY_SIGNATURE, int (*original_res_query)(R
show_msg(MSGDEBUG, "Got res_query request\n");
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
if (original_res_query == NULL) {
show_msg(MSGERR, "Unresolved symbol: res_query\n");
@@ -1509,8 +1510,8 @@ int EXPAND_GUTS_NAME(res_querydomain)(RES_QUERYDOMAIN_SIGNATURE, int (*original_
show_msg(MSGDEBUG, "Got res_querydomain request\n");
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
if (original_res_querydomain == NULL) {
show_msg(MSGERR, "Unresolved symbol: res_querydomain\n");
@@ -1539,8 +1540,8 @@ int EXPAND_GUTS_NAME(res_search)(RES_SEARCH_SIGNATURE, int (*original_res_search
show_msg(MSGDEBUG, "Got res_search request\n");
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
if (original_res_search == NULL) {
show_msg(MSGERR, "Unresolved symbol: res_search\n");
@@ -1568,8 +1569,8 @@ int EXPAND_GUTS_NAME(res_send)(RES_SEND_SIGNATURE, int (*original_res_send)(RES_
show_msg(MSGDEBUG, "Got res_send request\n");
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
if (original_res_send == NULL) {
show_msg(MSGERR, "Unresolved symbol: res_send\n");
@@ -1614,43 +1615,43 @@ static int deadpool_init(void)
return 1;
}
-struct hostent *tsocks_gethostbyname_guts(GETHOSTBYNAME_SIGNATURE, struct hostent *(*original_gethostbyname)(GETHOSTBYNAME_SIGNATURE))
+struct hostent *torsocks_gethostbyname_guts(GETHOSTBYNAME_SIGNATURE, struct hostent *(*original_gethostbyname)(GETHOSTBYNAME_SIGNATURE))
{
if (pool)
return our_gethostbyname(pool, name);
return original_gethostbyname(name);
}
-struct hostent *tsocks_gethostbyaddr_guts(GETHOSTBYADDR_SIGNATURE, struct hostent *(*original_gethostbyaddr)(GETHOSTBYADDR_SIGNATURE))
+struct hostent *torsocks_gethostbyaddr_guts(GETHOSTBYADDR_SIGNATURE, struct hostent *(*original_gethostbyaddr)(GETHOSTBYADDR_SIGNATURE))
{
if (pool)
return our_gethostbyaddr(pool, addr, len, type);
return original_gethostbyaddr(addr, len, type);
}
-int tsocks_getaddrinfo_guts(GETADDRINFO_SIGNATURE, int (*original_getaddrinfo)(GETADDRINFO_SIGNATURE))
+int torsocks_getaddrinfo_guts(GETADDRINFO_SIGNATURE, int (*original_getaddrinfo)(GETADDRINFO_SIGNATURE))
{
if (pool)
return our_getaddrinfo(pool, node, service, hints, res);
return original_getaddrinfo(node, service, hints, res);
}
-struct hostent *tsocks_getipnodebyname_guts(GETIPNODEBYNAME_SIGNATURE, struct hostent *(*original_getipnodebyname)(GETIPNODEBYNAME_SIGNATURE))
+struct hostent *torsocks_getipnodebyname_guts(GETIPNODEBYNAME_SIGNATURE, struct hostent *(*original_getipnodebyname)(GETIPNODEBYNAME_SIGNATURE))
{
if (pool)
return our_getipnodebyname(pool, name, af, flags, error_num);
return original_getipnodebyname(name, af, flags, error_num);
}
-ssize_t tsocks_sendto_guts(SENDTO_SIGNATURE, ssize_t (*original_sendto)(SENDTO_SIGNATURE))
+ssize_t torsocks_sendto_guts(SENDTO_SIGNATURE, ssize_t (*original_sendto)(SENDTO_SIGNATURE))
{
int sock_type = -1;
unsigned int sock_type_len = sizeof(sock_type);
struct sockaddr_in *connaddr;
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
/* If the real sendto doesn't exist, we're stuffed */
if (original_sendto == NULL) {
@@ -1686,15 +1687,15 @@ ssize_t tsocks_sendto_guts(SENDTO_SIGNATURE, ssize_t (*original_sendto)(SENDTO_S
return (ssize_t) original_sendto(s, buf, len, flags, to, tolen);
}
-ssize_t tsocks_sendmsg_guts(SENDMSG_SIGNATURE, ssize_t (*original_sendmsg)(SENDMSG_SIGNATURE))
+ssize_t torsocks_sendmsg_guts(SENDMSG_SIGNATURE, ssize_t (*original_sendmsg)(SENDMSG_SIGNATURE))
{
int sock_type = -1;
unsigned int sock_type_len = sizeof(sock_type);
struct sockaddr_in *connaddr;
/* See comment in close() */
- if (!tsocks_init_complete)
- tsocks_init();
+ if (!torsocks_init_complete)
+ torsocks_init();
/* If the real sendmsg doesn't exist, we're stuffed */
if (original_sendmsg == NULL) {
diff --git a/src/torsocks.h b/src/torsocks.h
index ea298f0..92ac35c 100644
--- a/src/torsocks.h
+++ b/src/torsocks.h
@@ -22,7 +22,7 @@
#ifndef _TORSOCKS_H
-#define _TORSOCKS_H 1
+#define _TORSOCKS_H 1
#include <parser.h>
1
0