[tor-commits] [torsocks/master] Add close(2) support

dgoulet at torproject.org dgoulet at torproject.org
Fri Apr 4 22:40:25 UTC 2014


commit 22fda9910920d7465d1558bd6cf2741177b3b908
Author: David Goulet <dgoulet at efficios.com>
Date:   Sat Jul 6 22:33:14 2013 -0400

    Add close(2) support
    
    Signed-off-by: David Goulet <dgoulet at efficios.com>
---
 src/lib/Makefile.am |    2 +-
 src/lib/close.c     |   64 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/torsocks.c  |    2 ++
 src/lib/torsocks.h  |   14 +++++++++++
 4 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index bf9237e..ec9ef0f 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -11,7 +11,7 @@ EXTRA_DIST = torsocks.in usewithtor.in
 lib_LTLIBRARIES = libtorsocks.la
 
 libtorsocks_la_SOURCES = torsocks.c torsocks.h \
-                         connect.c gethostbyname.c getaddrinfo.c
+                         connect.c gethostbyname.c getaddrinfo.c close.c
 
 libtorsocks_la_LIBADD = \
 		$(top_builddir)/src/common/libcommon.la \
diff --git a/src/lib/close.c b/src/lib/close.c
new file mode 100644
index 0000000..699b93c
--- /dev/null
+++ b/src/lib/close.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2013 - David Goulet <dgoulet at ev0ke.net>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License, version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <common/connection.h>
+#include <common/log.h>
+
+#include "torsocks.h"
+
+/*
+ * Torsocks call for close(2).
+ */
+LIBC_CLOSE_RET_TYPE tsocks_close(LIBC_CLOSE_SIG)
+{
+	struct connection *conn;
+
+	DBG("Close catched for fd %d", __fd);
+
+	connection_registry_lock();
+	conn = connection_find(__fd);
+	if (conn) {
+		/*
+		 * Remove from the registry so it's not visible anymore and thus using
+		 * it without lock.
+		 */
+		connection_remove(conn);
+	}
+	connection_registry_unlock();
+
+	/*
+	 * Put back the connection reference. If the refcount get to 0, the
+	 * connection pointer is destroyed.
+	 */
+	if (conn) {
+		DBG("Close connection putting back ref");
+		connection_put_ref(conn);
+	}
+
+	/* Return the original libc close. */
+	return tsocks_libc_close(__fd);
+}
+
+/*
+ * Libc hijacked symbol close(2).
+ */
+LIBC_CLOSE_DECL
+{
+	tsocks_libc_close = tsocks_find_libc_symbol(LIBC_CLOSE_NAME_STR,
+			TSOCKS_SYM_EXIT_NOT_FOUND);
+	return tsocks_close(LIBC_CLOSE_ARGS);
+}
diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c
index f40de3c..cf287d9 100644
--- a/src/lib/torsocks.c
+++ b/src/lib/torsocks.c
@@ -121,6 +121,8 @@ static void init_libc_symbols(void)
 {
 	tsocks_libc_connect = tsocks_find_libc_symbol(LIBC_CONNECT_NAME_STR,
 			TSOCKS_SYM_EXIT_NOT_FOUND);
+	tsocks_libc_close = tsocks_find_libc_symbol(LIBC_CLOSE_NAME_STR,
+			TSOCKS_SYM_EXIT_NOT_FOUND);
 }
 
 /*
diff --git a/src/lib/torsocks.h b/src/lib/torsocks.h
index 2d40470..4acacff 100644
--- a/src/lib/torsocks.h
+++ b/src/lib/torsocks.h
@@ -45,6 +45,15 @@
 #define LIBC_CONNECT_ARGS \
 	__sockfd, __addr, __addrlen
 
+/* close(2) */
+#include <unistd.h>
+
+#define LIBC_CLOSE_NAME close
+#define LIBC_CLOSE_NAME_STR XSTR(LIBC_CLOSE_NAME)
+#define LIBC_CLOSE_RET_TYPE int
+#define LIBC_CLOSE_SIG int __fd
+#define LIBC_CLOSE_ARGS __fd
+
 /* gethostbyname(3) - DEPRECATED in glibc. */
 #include <netdb.h>
 
@@ -106,6 +115,11 @@ TSOCKS_LIBC_DECL(connect, LIBC_CONNECT_RET_TYPE, LIBC_CONNECT_SIG)
 #define LIBC_CONNECT_DECL \
 	LIBC_CONNECT_RET_TYPE LIBC_CONNECT_NAME(LIBC_CONNECT_SIG)
 
+/* close(2) */
+TSOCKS_LIBC_DECL(close, LIBC_CLOSE_RET_TYPE, LIBC_CLOSE_SIG)
+#define LIBC_CLOSE_DECL \
+		LIBC_CLOSE_RET_TYPE LIBC_CLOSE_NAME(LIBC_CLOSE_SIG)
+
 /* gethostbyname(3) */
 TSOCKS_LIBC_DECL(gethostbyname, LIBC_GETHOSTBYNAME_RET_TYPE,
 		LIBC_GETHOSTBYNAME_SIG)





More information about the tor-commits mailing list