1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
2 * Copyright (c) 2007-2008, The Tor Project, Inc.
4 /* See LICENSE for licensing information */
19 #ifdef HAVE_NETINET_IN_H
20 #include <netinet/in.h>
22 #ifdef HAVE_ARPA_INET_H
23 #include <arpa/inet.h>
25 #ifdef HAVE_SYS_SOCKET_H
26 #include <sys/socket.h>
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
36 #if defined(_MSC_VER) && (_MSC_VER <= 1300)
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. */
55 build_socks_resolve_request(char **out
,
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) {
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!");
84 addrlen
= reverse
? 4 : 1 + strlen(hostname
);
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;
92 set_uint32((*out
)+4, in
.s_addr
);
94 (*out
)[4] = (char)(uint8_t)(addrlen
- 1);
95 memcpy((*out
)+5, hostname
, addrlen
- 1);
97 set_uint16((*out
)+4+addrlen
, 0); /* port */
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.
110 parse_socks4a_resolve_response(const char *response
, size_t len
,
114 tor_assert(response
);
115 tor_assert(addr_out
);
117 if (len
< RESPONSE_LEN_4
) {
118 log_warn(LD_PROTOCOL
,"Truncated socks response.");
121 if (((uint8_t)response
[0])!=0) { /* version: 0 */
122 log_warn(LD_PROTOCOL
,"Nonzero version in socks response: bad format.");
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.");
131 log_warn(LD_NET
,"Got status response '%d': socks request failed.", status
);
135 *addr_out
= ntohl(get_uint32(response
+4));
139 /* It would be nice to let someone know what SOCKS5 issue a user may have */
141 socks5_reason_to_string(char reason
)
144 case SOCKS5_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";
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>.
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
)
177 struct sockaddr_in socksaddr
;
181 tor_assert(hostname
);
182 tor_assert(result_addr
);
183 tor_assert(version
== 4 || version
== 5);
186 *result_hostname
= NULL
;
188 s
= tor_open_socket(PF_INET
,SOCK_STREAM
,IPPROTO_TCP
);
190 log_sock_error("creating_socket", -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
);
205 if (write_all(s
, "\x05\x01\x00", 3, 1) != 3) {
206 log_err(LD_NET
, "Error sending SOCKS5 method list.");
209 if (read_all(s
, method_buf
, 2, 1) != 2) {
210 log_err(LD_NET
, "Error reading SOCKS5 methods.");
213 if (method_buf
[0] != '\x05') {
214 log_err(LD_NET
, "Unrecognized socks version: %u",
215 (unsigned)method_buf
[0]);
218 if (method_buf
[1] != '\x00') {
219 log_err(LD_NET
, "Unrecognized socks authentication method: %u",
220 (unsigned)method_buf
[1]);
225 if ((len
= build_socks_resolve_request(&req
, "", hostname
, reverse
,
227 log_err(LD_BUG
,"Error generating SOCKS request");
231 if (write_all(s
, req
, len
, 1) != len
) {
232 log_sock_error("sending SOCKS request", s
);
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.");
244 if (parse_socks4a_resolve_response(reply_buf
, RESPONSE_LEN_4
,
250 if (read_all(s
, reply_buf
, 4, 1) != 4) {
251 log_err(LD_NET
, "Error reading SOCKS5 response.");
254 if (reply_buf
[0] != 5) {
255 log_err(LD_NET
, "Bad SOCKS5 reply version.");
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]));
265 if (reply_buf
[3] == 1) {
267 if (read_all(s
, reply_buf
, 4, 1) != 4) {
268 log_err(LD_NET
, "Error reading address in socks5 response.");
271 *result_addr
= ntohl(get_uint32(reply_buf
));
272 } else if (reply_buf
[3] == 3) {
274 if (read_all(s
, reply_buf
, 1, 1) != 1) {
275 log_err(LD_NET
, "Error reading address_length in socks5 response.");
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.");
284 (*result_hostname
)[result_len
] = '\0';
291 /** Print a usage message and exit. */
295 puts("Syntax: tor-resolve [-4] [-v] [-x] [-F] [-p port] "
296 "hostname [sockshost:socksport]");
300 /** Entry point to tor-resolve */
302 main(int argc
, char **argv
)
305 uint16_t socksport
= 0, port_option
= 0;
306 int isSocks4
= 0, isVerbose
= 0, isReverse
= 0, force
= 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
));
323 if (!strcmp(arg
[0],"--version")) {
324 printf("Tor version %s.\n",VERSION
);
327 while (n_args
&& *arg
[0] == '-') {
328 if (!strcmp("-v", arg
[0]))
330 else if (!strcmp("-4", arg
[0]))
332 else if (!strcmp("-5", arg
[0]))
334 else if (!strcmp("-x", arg
[0]))
336 else if (!strcmp("-F", arg
[0]))
338 else if (!strcmp("-p", arg
[0])) {
341 fprintf(stderr
, "No arguments given to -p\n");
345 if (p
<1 || p
> 65535) {
346 fprintf(stderr
, "-p requires a number between 1 and 65535\n");
349 port_option
= (uint16_t) p
;
350 ++arg
; /* skip the port */
353 fprintf(stderr
, "Unrecognized flag '%s'\n", arg
[0]);
360 if (isSocks4
&& isReverse
) {
361 fprintf(stderr
, "Reverse lookups not supported with SOCKS4a\n");
366 set_log_severity_config(LOG_DEBUG
, LOG_ERR
, s
);
368 set_log_severity_config(LOG_WARN
, LOG_ERR
, s
);
369 add_stream_log(s
, "<stderr>", fileno(stderr
));
372 log_debug(LD_CONFIG
, "defaulting to localhost");
373 sockshost
= 0x7f000001u
; /* localhost */
375 log_debug(LD_CONFIG
, "Using port %d", (int)port_option
);
376 socksport
= port_option
;
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]);
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
;
392 log_debug(LD_CONFIG
, "defaulting to port 9050");
399 if (!strcasecmpend(arg
[0], ".onion") && !force
) {
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]);
407 if (network_init()<0) {
408 log_err(LD_BUG
,"Error initializing network; exiting.");
412 if (do_resolve(arg
[0], sockshost
, socksport
, isReverse
,
413 isSocks4
? 4 : 5, &result
,
417 if (result_hostname
) {
418 printf("%s\n", result_hostname
);
420 a
.s_addr
= htonl(result
);
421 tor_inet_ntoa(&a
, buf
, sizeof(buf
));