1 /*
2  * Copyright (C) 2013 - David Goulet <dgoulet@ev0ke.net>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License, version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 51
15  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef TORSOCKS_CONNECTION_H
19 #define TORSOCKS_CONNECTION_H
20 
21 #include <netinet/in.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 
25 #include "defaults.h"
26 #include "ht.h"
27 #include "macros.h"
28 #include "ref.h"
29 
30 enum connection_domain {
31 	CONNECTION_DOMAIN_INET	= 1,
32 	CONNECTION_DOMAIN_INET6	= 2,
33 	CONNECTION_DOMAIN_NAME  = 3,
34 };
35 
36 /*
37  * Connection address which both supports IPv4 and IPv6.
38  */
39 struct connection_addr {
40 	enum connection_domain domain;
41 
42 	struct {
43 		char *addr;
44 		uint16_t port;
45 	} hostname;
46 
47 	union {
48 		struct sockaddr_in sin;
49 		struct sockaddr_in6 sin6;
50 	} u;
51 };
52 
53 /*
54  * Connection object representing a connect we did to the Tor network from a
55  * connect(2) hijacked call.
56  */
57 struct connection {
58 	/* Socket fd and also unique ID. */
59 	int fd;
60 
61 	/* Remote destination that passes through Tor. */
62 	struct connection_addr dest_addr;
63 
64 	/*
65 	 * Object refcount needed to access this object outside the registry lock.
66 	 * This is always initialized to 1 so only the destroy process can bring
67 	 * the refcount to 0 so to delete it.
68 	 */
69 	struct ref refcount;
70 
71 	/* Hash table node. */
72 	HT_ENTRY(connection) node;
73 };
74 
75 int connection_addr_set(enum connection_domain domain, const char *ip,
76 		in_port_t port, struct connection_addr *addr);
77 
78 struct connection *connection_create(int fd, const struct sockaddr *dest);
79 struct connection *connection_find(int key);
80 void connection_destroy(struct connection *conn);
81 void connection_remove(struct connection *conn);
82 void connection_insert(struct connection *conn);
83 
84 void connection_registry_lock(void);
85 void connection_registry_unlock(void);
86 
87 void connection_get_ref(struct connection *c);
88 void connection_put_ref(struct connection *c);
89 
90 #endif /* TORSOCKS_CONNECTION_H */
91