1 /*
2  * Structure used by torsocks to form SOCKS requests.
3  *
4  * Copyright (C) 2013 - David Goulet <dgoulet@ev0ke.net>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License, version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef TORSOCKS_SOCKS_H
21 #define TORSOCKS_SOCKS_H
22 
23 #include <stdint.h>
24 
25 #include "connection.h"
26 
27 /* For SOCKS version 5. */
28 #define SOCKS5_VERSION			0x05
29 
30 /*
31  * As stated in the SOCKS extension of Tor, for v5 the "NO AUTHENTICATION
32  * METHOD" [00] is supported and should be used.
33  */
34 #define SOCKS5_NO_AUTH_METHOD	0x00
35 #define SOCKS5_USER_PASS_METHOD 0x02
36 #define SOCKS5_NO_ACCPT_METHOD	0xFF
37 
38 /* SOCKS5 rfc1929 username and password authentication. */
39 #define SOCKS5_USER_PASS_VER    0x01
40 
41 /* Request command. */
42 #define SOCKS5_CMD_CONNECT		0x01
43 #define SOCKS5_CMD_RESOLVE		0xF0
44 #define SOCKS5_CMD_RESOLVE_PTR	0xF1
45 
46 /* Address type. */
47 #define SOCKS5_ATYP_IPV4		0x01
48 #define SOCKS5_ATYP_DOMAIN		0x03
49 #define SOCKS5_ATYP_IPV6		0x04
50 
51 /* Replies code. */
52 #define SOCKS5_REPLY_SUCCESS	0x00
53 #define SOCKS5_REPLY_FAIL		0x01
54 #define SOCKS5_REPLY_DENY_RULE	0x02
55 #define SOCKS5_REPLY_NO_NET		0x03
56 #define SOCKS5_REPLY_NO_HOST	0x04
57 #define SOCKS5_REPLY_REFUSED	0x05
58 #define SOCKS5_REPLY_TTL_EXP	0x06
59 #define SOCKS5_REPLY_CMD_NOTSUP	0x07
60 #define SOCKS5_REPLY_ADR_NOTSUP	0x08
61 
62 /* As described in rfc1929. */
63 #define SOCKS5_USERNAME_LEN     255
64 #define SOCKS5_PASSWORD_LEN     255
65 
66 /* Request data structure for the method. */
67 struct socks5_method_req {
68 	uint8_t ver;
69 	uint8_t nmethods;
70 	uint8_t methods;
71 };
72 
73 /* Reply data structure for the method. */
74 struct socks5_method_res {
75 	uint8_t ver;
76 	uint8_t method;
77 };
78 
79 /* First part of a request. */
80 struct socks5_request {
81 	uint8_t ver;
82 	uint8_t cmd;
83 	uint8_t rsv;
84 	uint8_t atyp;
85 };
86 
87 /* IPv4 destination addr for a request. */
88 struct socks5_request_ipv4 {
89 	uint8_t addr[4];
90 	uint16_t port;
91 };
92 
93 /* IPv6 destination addr for a request. */
94 struct socks5_request_ipv6 {
95 	uint8_t addr[16];
96 	uint16_t port;
97 };
98 
99 /* Domain name for a request. */
100 struct socks5_request_domain {
101 	uint8_t len;
102 	/* Maximum size of len above. No NULL byte is needed. */
103 	unsigned char name[UINT8_MAX];
104 	uint16_t port;
105 };
106 
107 /* Use for the Tor resolve command. */
108 struct socks5_request_resolve {
109 	uint8_t len;
110 	unsigned char name[UINT8_MAX];
111 	uint16_t port;
112 };
113 
114 /* Use for the Tor resolve ptr command. */
115 struct socks5_request_resolve_ptr {
116 	union {
117 		uint8_t ipv4[4];
118 		uint8_t ipv6[16];
119 	} addr;
120 	uint16_t port;
121 };
122 
123 /* Non variable part of a reply. */
124 struct socks5_reply {
125 	uint8_t ver;
126 	uint8_t rep;
127 	uint8_t rsv;
128 	uint8_t atyp;
129 };
130 
131 /* Username/password reply message. */
132 struct socks5_user_pass_reply {
133 	uint8_t ver;
134 	uint8_t status;
135 };
136 
137 int socks5_connect(struct connection *conn);
138 
139 /* Method messaging. */
140 int socks5_send_method(struct connection *conn, uint8_t type);
141 int socks5_recv_method(struct connection *conn);
142 
143 /* Username/Password request. */
144 int socks5_send_user_pass_request(struct connection *conn,
145 		const char *user, const char *pass);
146 int socks5_recv_user_pass_reply(struct connection *conn);
147 
148 /* Connect request. */
149 int socks5_send_connect_request(struct connection *conn);
150 int socks5_recv_connect_reply(struct connection *conn);
151 
152 /* Tor DNS resolve. */
153 int socks5_send_resolve_request(const char *hostname, struct connection *conn);
154 int socks5_recv_resolve_reply(struct connection *conn, void *addr,
155 		size_t addrlent);
156 int socks5_recv_resolve_ptr_reply(struct connection *conn, char **_hostname);
157 int socks5_send_resolve_ptr_request(struct connection *conn, const void *ip, int af);
158 
159 void socks5_init(ssize_t (*new_send_data)(int, const void *, size_t),
160 		ssize_t (*new_recv_data)(int, void *, size_t));
161 
162 #endif /* TORSOCKS_SOCKS_H */
163