[tor-commits] [torsocks/master] Add basic connection object interface

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


commit edc557f83905eb2940f315e31d1841971357aa6a
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Thu Jun 6 20:39:56 2013 -0400

    Add basic connection object interface
    
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 src/common/connection.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++
 src/common/connection.h |   38 ++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+)

diff --git a/src/common/connection.c b/src/common/connection.c
new file mode 100644
index 0000000..0831340
--- /dev/null
+++ b/src/common/connection.c
@@ -0,0 +1,60 @@
+/*
+ * 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 "connection.h"
+
+/*
+ * Create a new connection with the given fd and destination address.
+ *
+ * Return a newly allocated connection object or else NULL.
+ */
+struct connection *connection_create(int fd, struct sockaddr_in *dest)
+{
+	struct connection *conn;
+
+	assert(dest);
+
+	conn = zmalloc(sizeof(*conn));
+	if (!conn) {
+		PERROR("zmalloc connection");
+		goto error;
+	}
+
+	conn->fd = fd;
+	memcpy(conn->dest_addr, dest, sizeof(conn->dest_addr));
+
+	return conn;
+
+error:
+	return NULL;
+}
+
+/*
+ * Destroy a connection by freeing its memory.
+ */
+void connection_destroy(struct connection *conn)
+{
+	if (!conn) {
+		return;
+	}
+
+	/* Remove from the double linked list. */
+	conn->prev->next = conn->next;
+	conn->next->prev = conn->prev;
+
+	free(conn);
+}
diff --git a/src/common/connection.h b/src/common/connection.h
new file mode 100644
index 0000000..c8711ad
--- /dev/null
+++ b/src/common/connection.h
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+#ifndef TORSOCKS_CONNECTION_H
+#define TORSOCKS_CONNECTION_H
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+struct connection {
+	/* Socket fd and also unique ID. */
+	int fd;
+
+	/* Location of the SOCKS5 server. */
+	struct sockaddr_in socks5_addr;
+
+	/* Remove destination that passes through Tor. */
+	struct sockaddr_in dest_addr;
+
+	/* Next connection of the linked list. */
+	struct connection *next, *prev;
+};
+
+#endif /* TORSOCKS_CONNECTION_H */





More information about the tor-commits mailing list