r17358@pc-10-8-1-079: nickm | 2008-07-25 16:41:03 +0200
[tor.git] / src / tools / tor-resolve.c
bloba73fe1503f076fe1631012d8aa88962cec13d36a
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 */
5 /* $Id$ */
7 #include "orconfig.h"
9 #include "compat.h"
10 #include "util.h"
11 #include "address.h"
12 #include "log.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <string.h>
18 #include <assert.h>
20 #ifdef HAVE_NETINET_IN_H
21 #include <netinet/in.h>
22 #endif
23 #ifdef HAVE_ARPA_INET_H
24 #include <arpa/inet.h>
25 #endif
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
28 #endif
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
36 #ifdef MS_WINDOWS
37 #if defined(_MSC_VER) && (_MSC_VER <= 1300)
38 #include <winsock.h>
39 #else
40 #include <winsock2.h>
41 #include <ws2tcpip.h>
42 #endif
43 #endif
45 #define RESPONSE_LEN_4 8
46 #define log_sock_error(act, _s) \
47 STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
48 tor_socket_strerror(tor_socket_errno(_s))); STMT_END
50 static void usage(void) ATTR_NORETURN;
52 /** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
53 * <b>username</b> and <b>hostname</b> as provided. Return the number
54 * of bytes in the request. */
55 static int
56 build_socks_resolve_request(char **out,
57 const char *username,
58 const char *hostname,
59 int reverse,
60 int version)
62 size_t len = 0;
63 tor_assert(out);
64 tor_assert(username);
65 tor_assert(hostname);
67 if (version == 4) {
68 len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
69 *out = tor_malloc(len);
70 (*out)[0] = 4; /* SOCKS version 4 */
71 (*out)[1] = '\xF0'; /* Command: resolve. */
72 set_uint16((*out)+2, htons(0)); /* port: 0. */
73 set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
74 memcpy((*out)+8, username, strlen(username)+1);
75 memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
76 } else if (version == 5) {
77 int is_ip_address;
78 struct in_addr in;
79 size_t addrlen;
80 is_ip_address = tor_inet_aton(hostname, &in);
81 if (!is_ip_address && reverse) {
82 log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
83 return -1;
85 addrlen = reverse ? 4 : 1 + strlen(hostname);
86 len = 6 + addrlen;
87 *out = tor_malloc(len);
88 (*out)[0] = 5; /* SOCKS version 5 */
89 (*out)[1] = reverse ? '\xF1' : '\xF0'; /* RESOLVE_PTR or RESOLVE */
90 (*out)[2] = 0; /* reserved. */
91 (*out)[3] = reverse ? 1 : 3;
92 if (reverse) {
93 set_uint32((*out)+4, in.s_addr);
94 } else {
95 (*out)[4] = (char)(uint8_t)(addrlen - 1);
96 memcpy((*out)+5, hostname, addrlen - 1);
98 set_uint16((*out)+4+addrlen, 0); /* port */
99 } else {
100 tor_assert(0);
103 return len;
106 /** Given a <b>len</b>-byte SOCKS4a response in <b>response</b>, set
107 * *<b>addr_out</b> to the address it contains (in host order).
108 * Return 0 on success, -1 on error.
110 static int
111 parse_socks4a_resolve_response(const char *response, size_t len,
112 uint32_t *addr_out)
114 uint8_t status;
115 tor_assert(response);
116 tor_assert(addr_out);
118 if (len < RESPONSE_LEN_4) {
119 log_warn(LD_PROTOCOL,"Truncated socks response.");
120 return -1;
122 if (((uint8_t)response[0])!=0) { /* version: 0 */
123 log_warn(LD_PROTOCOL,"Nonzero version in socks response: bad format.");
124 return -1;
126 status = (uint8_t)response[1];
127 if (get_uint16(response+2)!=0) { /* port: 0 */
128 log_warn(LD_PROTOCOL,"Nonzero port in socks response: bad format.");
129 return -1;
131 if (status != 90) {
132 log_warn(LD_NET,"Got status response '%d': socks request failed.", status);
133 return -1;
136 *addr_out = ntohl(get_uint32(response+4));
137 return 0;
140 /** Send a resolve request for <b>hostname</b> to the Tor listening on
141 * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
142 * address (in host order) into *<b>result_addr</b>.
144 static int
145 do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
146 int reverse, int version,
147 uint32_t *result_addr, char **result_hostname)
149 int s;
150 struct sockaddr_in socksaddr;
151 char *req = NULL;
152 int len = 0;
154 tor_assert(hostname);
155 tor_assert(result_addr);
156 tor_assert(version == 4 || version == 5);
158 *result_addr = 0;
159 *result_hostname = NULL;
161 s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
162 if (s<0) {
163 log_sock_error("creating_socket", -1);
164 return -1;
167 memset(&socksaddr, 0, sizeof(socksaddr));
168 socksaddr.sin_family = AF_INET;
169 socksaddr.sin_port = htons(socksport);
170 socksaddr.sin_addr.s_addr = htonl(sockshost);
171 if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
172 log_sock_error("connecting to SOCKS host", s);
173 return -1;
176 if (version == 5) {
177 char method_buf[2];
178 if (write_all(s, "\x05\x01\x00", 3, 1) != 3) {
179 log_err(LD_NET, "Error sending SOCKS5 method list.");
180 return -1;
182 if (read_all(s, method_buf, 2, 1) != 2) {
183 log_err(LD_NET, "Error reading SOCKS5 methods.");
184 return -1;
186 if (method_buf[0] != '\x05') {
187 log_err(LD_NET, "Unrecognized socks version: %u",
188 (unsigned)method_buf[0]);
189 return -1;
191 if (method_buf[1] != '\x00') {
192 log_err(LD_NET, "Unrecognized socks authentication method: %u",
193 (unsigned)method_buf[1]);
194 return -1;
198 if ((len = build_socks_resolve_request(&req, "", hostname, reverse,
199 version))<0) {
200 log_err(LD_BUG,"Error generating SOCKS request");
201 tor_assert(!req);
202 return -1;
204 if (write_all(s, req, len, 1) != len) {
205 log_sock_error("sending SOCKS request", s);
206 tor_free(req);
207 return -1;
209 tor_free(req);
211 if (version == 4) {
212 char reply_buf[RESPONSE_LEN_4];
213 if (read_all(s, reply_buf, RESPONSE_LEN_4, 1) != RESPONSE_LEN_4) {
214 log_err(LD_NET, "Error reading SOCKS4 response.");
215 return -1;
217 if (parse_socks4a_resolve_response(reply_buf, RESPONSE_LEN_4,
218 result_addr)<0){
219 return -1;
221 } else {
222 char reply_buf[4];
223 if (read_all(s, reply_buf, 4, 1) != 4) {
224 log_err(LD_NET, "Error reading SOCKS5 response.");
225 return -1;
227 if (reply_buf[0] != 5) {
228 log_err(LD_NET, "Bad SOCKS5 reply version.");
229 return -1;
231 if (reply_buf[1] != 0) {
232 log_warn(LD_NET,"Got status response '%u': SOCKS5 request failed.",
233 (unsigned)reply_buf[1]);
234 return -1;
236 if (reply_buf[3] == 1) {
237 /* IPv4 address */
238 if (read_all(s, reply_buf, 4, 1) != 4) {
239 log_err(LD_NET, "Error reading address in socks5 response.");
240 return -1;
242 *result_addr = ntohl(get_uint32(reply_buf));
243 } else if (reply_buf[3] == 3) {
244 size_t result_len;
245 if (read_all(s, reply_buf, 1, 1) != 1) {
246 log_err(LD_NET, "Error reading address_length in socks5 response.");
247 return -1;
249 result_len = *(uint8_t*)(reply_buf);
250 *result_hostname = tor_malloc(result_len+1);
251 if (read_all(s, *result_hostname, result_len, 1) != (int) result_len) {
252 log_err(LD_NET, "Error reading hostname in socks5 response.");
253 return -1;
255 (*result_hostname)[result_len] = '\0';
259 return 0;
262 /** Print a usage message and exit. */
263 static void
264 usage(void)
266 puts("Syntax: tor-resolve [-4] [-v] [-x] [-F] "
267 "hostname [sockshost:socksport]");
268 exit(1);
271 /** Entry point to tor-resolve */
273 main(int argc, char **argv)
275 uint32_t sockshost;
276 uint16_t socksport;
277 int isSocks4 = 0, isVerbose = 0, isReverse = 0, force = 0;
278 char **arg;
279 int n_args;
280 struct in_addr a;
281 uint32_t result = 0;
282 char *result_hostname = NULL;
283 char buf[INET_NTOA_BUF_LEN];
284 log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
286 init_logging();
288 arg = &argv[1];
289 n_args = argc-1;
291 if (!n_args)
292 usage();
294 if (!strcmp(arg[0],"--version")) {
295 printf("Tor version %s.\n",VERSION);
296 return 0;
298 while (n_args && *arg[0] == '-') {
299 if (!strcmp("-v", arg[0]))
300 isVerbose = 1;
301 else if (!strcmp("-4", arg[0]))
302 isSocks4 = 1;
303 else if (!strcmp("-5", arg[0]))
304 isSocks4 = 0;
305 else if (!strcmp("-x", arg[0]))
306 isReverse = 1;
307 else if (!strcmp("-F", arg[0]))
308 force = 1;
309 else {
310 fprintf(stderr, "Unrecognized flag '%s'\n", arg[0]);
311 usage();
313 ++arg;
314 --n_args;
317 if (isSocks4 && isReverse) {
318 fprintf(stderr, "Reverse lookups not supported with SOCKS4a\n");
319 usage();
322 if (isVerbose)
323 set_log_severity_config(LOG_DEBUG, LOG_ERR, s);
324 else
325 set_log_severity_config(LOG_WARN, LOG_ERR, s);
326 add_stream_log(s, "<stderr>", stderr);
328 if (n_args == 1) {
329 log_debug(LD_CONFIG, "defaulting to localhost:9050");
330 sockshost = 0x7f000001u; /* localhost */
331 socksport = 9050; /* 9050 */
332 } else if (n_args == 2) {
333 if (parse_addr_port(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) {
334 fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
335 return 1;
337 if (socksport == 0) {
338 log_debug(LD_CONFIG, "defaulting to port 9050");
339 socksport = 9050;
341 } else {
342 usage();
345 if (!strcasecmpend(arg[0], ".onion") && !force) {
346 fprintf(stderr,
347 "%s is a hidden service; those don't have IP addresses.\n\n"
348 "To connect to a hidden service, you need to send the hostname\n"
349 "to Tor; we suggest an application that uses SOCKS 4a.\n", arg[0]);
350 return 1;
353 if (network_init()<0) {
354 log_err(LD_BUG,"Error initializing network; exiting.");
355 return 1;
358 if (do_resolve(arg[0], sockshost, socksport, isReverse,
359 isSocks4 ? 4 : 5, &result,
360 &result_hostname))
361 return 1;
363 if (result_hostname) {
364 printf("%s\n", result_hostname);
365 } else {
366 a.s_addr = htonl(result);
367 tor_inet_ntoa(&a, buf, sizeof(buf));
368 printf("%s\n", buf);
370 return 0;