1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
2 * Copyright (c) 2007-2008, The Tor Project, Inc.
4 /* See LICENSE for licensing information */
20 #ifdef HAVE_NETINET_IN_H
21 #include <netinet/in.h>
23 #ifdef HAVE_ARPA_INET_H
24 #include <arpa/inet.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
37 #if defined(_MSC_VER) && (_MSC_VER <= 1300)
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. */
56 build_socks_resolve_request(char **out
,
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) {
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!");
85 addrlen
= reverse
? 4 : 1 + strlen(hostname
);
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;
93 set_uint32((*out
)+4, in
.s_addr
);
95 (*out
)[4] = (char)(uint8_t)(addrlen
- 1);
96 memcpy((*out
)+5, hostname
, addrlen
- 1);
98 set_uint16((*out
)+4+addrlen
, 0); /* port */
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.
111 parse_socks4a_resolve_response(const char *response
, size_t len
,
115 tor_assert(response
);
116 tor_assert(addr_out
);
118 if (len
< RESPONSE_LEN_4
) {
119 log_warn(LD_PROTOCOL
,"Truncated socks response.");
122 if (((uint8_t)response
[0])!=0) { /* version: 0 */
123 log_warn(LD_PROTOCOL
,"Nonzero version in socks response: bad format.");
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.");
132 log_warn(LD_NET
,"Got status response '%d': socks request failed.", status
);
136 *addr_out
= ntohl(get_uint32(response
+4));
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>.
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
)
150 struct sockaddr_in socksaddr
;
154 tor_assert(hostname
);
155 tor_assert(result_addr
);
156 tor_assert(version
== 4 || version
== 5);
159 *result_hostname
= NULL
;
161 s
= tor_open_socket(PF_INET
,SOCK_STREAM
,IPPROTO_TCP
);
163 log_sock_error("creating_socket", -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
);
178 if (write_all(s
, "\x05\x01\x00", 3, 1) != 3) {
179 log_err(LD_NET
, "Error sending SOCKS5 method list.");
182 if (read_all(s
, method_buf
, 2, 1) != 2) {
183 log_err(LD_NET
, "Error reading SOCKS5 methods.");
186 if (method_buf
[0] != '\x05') {
187 log_err(LD_NET
, "Unrecognized socks version: %u",
188 (unsigned)method_buf
[0]);
191 if (method_buf
[1] != '\x00') {
192 log_err(LD_NET
, "Unrecognized socks authentication method: %u",
193 (unsigned)method_buf
[1]);
198 if ((len
= build_socks_resolve_request(&req
, "", hostname
, reverse
,
200 log_err(LD_BUG
,"Error generating SOCKS request");
204 if (write_all(s
, req
, len
, 1) != len
) {
205 log_sock_error("sending SOCKS request", s
);
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.");
217 if (parse_socks4a_resolve_response(reply_buf
, RESPONSE_LEN_4
,
223 if (read_all(s
, reply_buf
, 4, 1) != 4) {
224 log_err(LD_NET
, "Error reading SOCKS5 response.");
227 if (reply_buf
[0] != 5) {
228 log_err(LD_NET
, "Bad SOCKS5 reply version.");
231 if (reply_buf
[1] != 0) {
232 log_warn(LD_NET
,"Got status response '%u': SOCKS5 request failed.",
233 (unsigned)reply_buf
[1]);
236 if (reply_buf
[3] == 1) {
238 if (read_all(s
, reply_buf
, 4, 1) != 4) {
239 log_err(LD_NET
, "Error reading address in socks5 response.");
242 *result_addr
= ntohl(get_uint32(reply_buf
));
243 } else if (reply_buf
[3] == 3) {
245 if (read_all(s
, reply_buf
, 1, 1) != 1) {
246 log_err(LD_NET
, "Error reading address_length in socks5 response.");
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.");
255 (*result_hostname
)[result_len
] = '\0';
262 /** Print a usage message and exit. */
266 puts("Syntax: tor-resolve [-4] [-v] [-x] [-F] "
267 "hostname [sockshost:socksport]");
271 /** Entry point to tor-resolve */
273 main(int argc
, char **argv
)
277 int isSocks4
= 0, isVerbose
= 0, isReverse
= 0, force
= 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
));
294 if (!strcmp(arg
[0],"--version")) {
295 printf("Tor version %s.\n",VERSION
);
298 while (n_args
&& *arg
[0] == '-') {
299 if (!strcmp("-v", arg
[0]))
301 else if (!strcmp("-4", arg
[0]))
303 else if (!strcmp("-5", arg
[0]))
305 else if (!strcmp("-x", arg
[0]))
307 else if (!strcmp("-F", arg
[0]))
310 fprintf(stderr
, "Unrecognized flag '%s'\n", arg
[0]);
317 if (isSocks4
&& isReverse
) {
318 fprintf(stderr
, "Reverse lookups not supported with SOCKS4a\n");
323 set_log_severity_config(LOG_DEBUG
, LOG_ERR
, s
);
325 set_log_severity_config(LOG_WARN
, LOG_ERR
, s
);
326 add_stream_log(s
, "<stderr>", stderr
);
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]);
337 if (socksport
== 0) {
338 log_debug(LD_CONFIG
, "defaulting to port 9050");
345 if (!strcasecmpend(arg
[0], ".onion") && !force
) {
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]);
353 if (network_init()<0) {
354 log_err(LD_BUG
,"Error initializing network; exiting.");
358 if (do_resolve(arg
[0], sockshost
, socksport
, isReverse
,
359 isSocks4
? 4 : 5, &result
,
363 if (result_hostname
) {
364 printf("%s\n", result_hostname
);
366 a
.s_addr
= htonl(result
);
367 tor_inet_ntoa(&a
, buf
, sizeof(buf
));