Possible fix for broken country settings in ExcludeExitNodes.
[tor/rransom.git] / src / tools / tor-resolve.c
blob100c8107a44a43272ab350f4e4b464413568305d
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
2 * Copyright (c) 2007-2008, The Tor Project, Inc.
3 */
4 /* See LICENSE for licensing information */
6 #include "orconfig.h"
8 #include "compat.h"
9 #include "util.h"
10 #include "address.h"
11 #include "log.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include <assert.h>
19 #ifdef HAVE_NETINET_IN_H
20 #include <netinet/in.h>
21 #endif
22 #ifdef HAVE_ARPA_INET_H
23 #include <arpa/inet.h>
24 #endif
25 #ifdef HAVE_SYS_SOCKET_H
26 #include <sys/socket.h>
27 #endif
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
30 #endif
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
35 #ifdef MS_WINDOWS
36 #if defined(_MSC_VER) && (_MSC_VER <= 1300)
37 #include <winsock.h>
38 #else
39 #include <winsock2.h>
40 #include <ws2tcpip.h>
41 #endif
42 #endif
44 #define RESPONSE_LEN_4 8
45 #define log_sock_error(act, _s) \
46 STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
47 tor_socket_strerror(tor_socket_errno(_s))); STMT_END
49 static void usage(void) ATTR_NORETURN;
51 /** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
52 * <b>username</b> and <b>hostname</b> as provided. Return the number
53 * of bytes in the request. */
54 static int
55 build_socks_resolve_request(char **out,
56 const char *username,
57 const char *hostname,
58 int reverse,
59 int version)
61 size_t len = 0;
62 tor_assert(out);
63 tor_assert(username);
64 tor_assert(hostname);
66 if (version == 4) {
67 len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
68 *out = tor_malloc(len);
69 (*out)[0] = 4; /* SOCKS version 4 */
70 (*out)[1] = '\xF0'; /* Command: resolve. */
71 set_uint16((*out)+2, htons(0)); /* port: 0. */
72 set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
73 memcpy((*out)+8, username, strlen(username)+1);
74 memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
75 } else if (version == 5) {
76 int is_ip_address;
77 struct in_addr in;
78 size_t addrlen;
79 is_ip_address = tor_inet_aton(hostname, &in);
80 if (!is_ip_address && reverse) {
81 log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
82 return -1;
84 addrlen = reverse ? 4 : 1 + strlen(hostname);
85 len = 6 + addrlen;
86 *out = tor_malloc(len);
87 (*out)[0] = 5; /* SOCKS version 5 */
88 (*out)[1] = reverse ? '\xF1' : '\xF0'; /* RESOLVE_PTR or RESOLVE */
89 (*out)[2] = 0; /* reserved. */
90 (*out)[3] = reverse ? 1 : 3;
91 if (reverse) {
92 set_uint32((*out)+4, in.s_addr);
93 } else {
94 (*out)[4] = (char)(uint8_t)(addrlen - 1);
95 memcpy((*out)+5, hostname, addrlen - 1);
97 set_uint16((*out)+4+addrlen, 0); /* port */
98 } else {
99 tor_assert(0);
102 return len;
105 /** Given a <b>len</b>-byte SOCKS4a response in <b>response</b>, set
106 * *<b>addr_out</b> to the address it contains (in host order).
107 * Return 0 on success, -1 on error.
109 static int
110 parse_socks4a_resolve_response(const char *response, size_t len,
111 uint32_t *addr_out)
113 uint8_t status;
114 tor_assert(response);
115 tor_assert(addr_out);
117 if (len < RESPONSE_LEN_4) {
118 log_warn(LD_PROTOCOL,"Truncated socks response.");
119 return -1;
121 if (((uint8_t)response[0])!=0) { /* version: 0 */
122 log_warn(LD_PROTOCOL,"Nonzero version in socks response: bad format.");
123 return -1;
125 status = (uint8_t)response[1];
126 if (get_uint16(response+2)!=0) { /* port: 0 */
127 log_warn(LD_PROTOCOL,"Nonzero port in socks response: bad format.");
128 return -1;
130 if (status != 90) {
131 log_warn(LD_NET,"Got status response '%d': socks request failed.", status);
132 return -1;
135 *addr_out = ntohl(get_uint32(response+4));
136 return 0;
139 /* It would be nice to let someone know what SOCKS5 issue a user may have */
140 static const char *
141 socks5_reason_to_string(char reason)
143 switch(reason) {
144 case SOCKS5_SUCCEEDED:
145 return "succeeded";
146 case SOCKS5_GENERAL_ERROR:
147 return "general error";
148 case SOCKS5_NOT_ALLOWED:
149 return "not allowed";
150 case SOCKS5_NET_UNREACHABLE:
151 return "network is unreachable";
152 case SOCKS5_HOST_UNREACHABLE:
153 return "host is unreachable";
154 case SOCKS5_CONNECTION_REFUSED:
155 return "connection refused";
156 case SOCKS5_TTL_EXPIRED:
157 return "ttl expired";
158 case SOCKS5_COMMAND_NOT_SUPPORTED:
159 return "command not supported";
160 case SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED:
161 return "address type not supported";
162 default:
163 return "unknown SOCKS5 code";
167 /** Send a resolve request for <b>hostname</b> to the Tor listening on
168 * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
169 * address (in host order) into *<b>result_addr</b>.
171 static int
172 do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
173 int reverse, int version,
174 uint32_t *result_addr, char **result_hostname)
176 int s;
177 struct sockaddr_in socksaddr;
178 char *req = NULL;
179 int len = 0;
181 tor_assert(hostname);
182 tor_assert(result_addr);
183 tor_assert(version == 4 || version == 5);
185 *result_addr = 0;
186 *result_hostname = NULL;
188 s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
189 if (s<0) {
190 log_sock_error("creating_socket", -1);
191 return -1;
194 memset(&socksaddr, 0, sizeof(socksaddr));
195 socksaddr.sin_family = AF_INET;
196 socksaddr.sin_port = htons(socksport);
197 socksaddr.sin_addr.s_addr = htonl(sockshost);
198 if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
199 log_sock_error("connecting to SOCKS host", s);
200 return -1;
203 if (version == 5) {
204 char method_buf[2];
205 if (write_all(s, "\x05\x01\x00", 3, 1) != 3) {
206 log_err(LD_NET, "Error sending SOCKS5 method list.");
207 return -1;
209 if (read_all(s, method_buf, 2, 1) != 2) {
210 log_err(LD_NET, "Error reading SOCKS5 methods.");
211 return -1;
213 if (method_buf[0] != '\x05') {
214 log_err(LD_NET, "Unrecognized socks version: %u",
215 (unsigned)method_buf[0]);
216 return -1;
218 if (method_buf[1] != '\x00') {
219 log_err(LD_NET, "Unrecognized socks authentication method: %u",
220 (unsigned)method_buf[1]);
221 return -1;
225 if ((len = build_socks_resolve_request(&req, "", hostname, reverse,
226 version))<0) {
227 log_err(LD_BUG,"Error generating SOCKS request");
228 tor_assert(!req);
229 return -1;
231 if (write_all(s, req, len, 1) != len) {
232 log_sock_error("sending SOCKS request", s);
233 tor_free(req);
234 return -1;
236 tor_free(req);
238 if (version == 4) {
239 char reply_buf[RESPONSE_LEN_4];
240 if (read_all(s, reply_buf, RESPONSE_LEN_4, 1) != RESPONSE_LEN_4) {
241 log_err(LD_NET, "Error reading SOCKS4 response.");
242 return -1;
244 if (parse_socks4a_resolve_response(reply_buf, RESPONSE_LEN_4,
245 result_addr)<0){
246 return -1;
248 } else {
249 char reply_buf[4];
250 if (read_all(s, reply_buf, 4, 1) != 4) {
251 log_err(LD_NET, "Error reading SOCKS5 response.");
252 return -1;
254 if (reply_buf[0] != 5) {
255 log_err(LD_NET, "Bad SOCKS5 reply version.");
256 return -1;
258 /* Give a user some useful feedback about SOCKS5 errors */
259 if (reply_buf[1] != 0) {
260 log_warn(LD_NET,"Got SOCKS5 status response '%u': %s",
261 (unsigned)reply_buf[1],
262 socks5_reason_to_string(reply_buf[1]));
263 return -1;
265 if (reply_buf[3] == 1) {
266 /* IPv4 address */
267 if (read_all(s, reply_buf, 4, 1) != 4) {
268 log_err(LD_NET, "Error reading address in socks5 response.");
269 return -1;
271 *result_addr = ntohl(get_uint32(reply_buf));
272 } else if (reply_buf[3] == 3) {
273 size_t result_len;
274 if (read_all(s, reply_buf, 1, 1) != 1) {
275 log_err(LD_NET, "Error reading address_length in socks5 response.");
276 return -1;
278 result_len = *(uint8_t*)(reply_buf);
279 *result_hostname = tor_malloc(result_len+1);
280 if (read_all(s, *result_hostname, result_len, 1) != (int) result_len) {
281 log_err(LD_NET, "Error reading hostname in socks5 response.");
282 return -1;
284 (*result_hostname)[result_len] = '\0';
288 return 0;
291 /** Print a usage message and exit. */
292 static void
293 usage(void)
295 puts("Syntax: tor-resolve [-4] [-v] [-x] [-F] [-p port] "
296 "hostname [sockshost:socksport]");
297 exit(1);
300 /** Entry point to tor-resolve */
302 main(int argc, char **argv)
304 uint32_t sockshost;
305 uint16_t socksport = 0, port_option = 0;
306 int isSocks4 = 0, isVerbose = 0, isReverse = 0, force = 0;
307 char **arg;
308 int n_args;
309 struct in_addr a;
310 uint32_t result = 0;
311 char *result_hostname = NULL;
312 char buf[INET_NTOA_BUF_LEN];
313 log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
315 init_logging();
317 arg = &argv[1];
318 n_args = argc-1;
320 if (!n_args)
321 usage();
323 if (!strcmp(arg[0],"--version")) {
324 printf("Tor version %s.\n",VERSION);
325 return 0;
327 while (n_args && *arg[0] == '-') {
328 if (!strcmp("-v", arg[0]))
329 isVerbose = 1;
330 else if (!strcmp("-4", arg[0]))
331 isSocks4 = 1;
332 else if (!strcmp("-5", arg[0]))
333 isSocks4 = 0;
334 else if (!strcmp("-x", arg[0]))
335 isReverse = 1;
336 else if (!strcmp("-F", arg[0]))
337 force = 1;
338 else if (!strcmp("-p", arg[0])) {
339 int p;
340 if (n_args < 2) {
341 fprintf(stderr, "No arguments given to -p\n");
342 usage();
344 p = atoi(arg[1]);
345 if (p<1 || p > 65535) {
346 fprintf(stderr, "-p requires a number between 1 and 65535\n");
347 usage();
349 port_option = (uint16_t) p;
350 ++arg; /* skip the port */
351 --n_args;
352 } else {
353 fprintf(stderr, "Unrecognized flag '%s'\n", arg[0]);
354 usage();
356 ++arg;
357 --n_args;
360 if (isSocks4 && isReverse) {
361 fprintf(stderr, "Reverse lookups not supported with SOCKS4a\n");
362 usage();
365 if (isVerbose)
366 set_log_severity_config(LOG_DEBUG, LOG_ERR, s);
367 else
368 set_log_severity_config(LOG_WARN, LOG_ERR, s);
369 add_stream_log(s, "<stderr>", fileno(stderr));
371 if (n_args == 1) {
372 log_debug(LD_CONFIG, "defaulting to localhost");
373 sockshost = 0x7f000001u; /* localhost */
374 if (port_option) {
375 log_debug(LD_CONFIG, "Using port %d", (int)port_option);
376 socksport = port_option;
377 } else {
378 log_debug(LD_CONFIG, "defaulting to port 9050");
379 socksport = 9050; /* 9050 */
381 } else if (n_args == 2) {
382 if (parse_addr_port(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) {
383 fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
384 return 1;
386 if (socksport && port_option && socksport != port_option) {
387 log_warn(LD_CONFIG, "Conflicting ports; using %d, not %d",
388 (int)socksport, (int)port_option);
389 } else if (port_option) {
390 socksport = port_option;
391 } else {
392 log_debug(LD_CONFIG, "defaulting to port 9050");
393 socksport = 9050;
395 } else {
396 usage();
399 if (!strcasecmpend(arg[0], ".onion") && !force) {
400 fprintf(stderr,
401 "%s is a hidden service; those don't have IP addresses.\n\n"
402 "To connect to a hidden service, you need to send the hostname\n"
403 "to Tor; we suggest an application that uses SOCKS 4a.\n", arg[0]);
404 return 1;
407 if (network_init()<0) {
408 log_err(LD_BUG,"Error initializing network; exiting.");
409 return 1;
412 if (do_resolve(arg[0], sockshost, socksport, isReverse,
413 isSocks4 ? 4 : 5, &result,
414 &result_hostname))
415 return 1;
417 if (result_hostname) {
418 printf("%s\n", result_hostname);
419 } else {
420 a.s_addr = htonl(result);
421 tor_inet_ntoa(&a, buf, sizeof(buf));
422 printf("%s\n", buf);
424 return 0;