tor-resolve.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "orconfig.h"
  5. #include "../common/compat.h"
  6. #include "../common/util.h"
  7. #include "../common/log.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #ifdef HAVE_NETINET_IN_H
  14. #include <netinet/in.h>
  15. #endif
  16. #ifdef HAVE_ARPA_INET_H
  17. #include <arpa/inet.h>
  18. #endif
  19. #ifdef HAVE_SYS_SOCKET_H
  20. #include <sys/socket.h>
  21. #endif
  22. #ifdef HAVE_SYS_TYPES_H
  23. #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
  24. #endif
  25. #ifdef HAVE_ERRNO_H
  26. #include <errno.h>
  27. #endif
  28. #ifdef MS_WINDOWS
  29. #if (_MSC_VER <= 1300)
  30. #include <winsock.h>
  31. #else
  32. #include <winsock2.h>
  33. #include <ws2tcpip.h>
  34. #endif
  35. #endif
  36. #define RESPONSE_LEN 8
  37. #define log_sock_error(act, _s) \
  38. do { log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
  39. tor_socket_strerror(tor_socket_errno(_s))); } while (0)
  40. /** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
  41. * <b>username</b> and <b>hostname</b> as provided. Return the number
  42. * of bytes in the request. */
  43. static int
  44. build_socks4a_resolve_request(char **out,
  45. const char *username,
  46. const char *hostname)
  47. {
  48. size_t len;
  49. tor_assert(out);
  50. tor_assert(username);
  51. tor_assert(hostname);
  52. len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
  53. *out = tor_malloc(len);
  54. (*out)[0] = 4; /* SOCKS version 4 */
  55. (*out)[1] = '\xF0'; /* Command: resolve. */
  56. set_uint16((*out)+2, htons(0)); /* port: 0. */
  57. set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
  58. strcpy((*out)+8, username);
  59. strcpy((*out)+8+strlen(username)+1, hostname);
  60. return len;
  61. }
  62. /** Given a <b>len</b>-byte SOCKS4a response in <b>response</b>, set
  63. * *<b>addr_out</b> to the address it contains (in host order).
  64. * Return 0 on success, -1 on error.
  65. */
  66. static int
  67. parse_socks4a_resolve_response(const char *response, size_t len,
  68. uint32_t *addr_out)
  69. {
  70. uint8_t status;
  71. tor_assert(response);
  72. tor_assert(addr_out);
  73. if (len < RESPONSE_LEN) {
  74. log_warn(LD_PROTOCOL,"Truncated socks response.");
  75. return -1;
  76. }
  77. if (((uint8_t)response[0])!=0) { /* version: 0 */
  78. log_warn(LD_PROTOCOL,"Nonzero version in socks response: bad format.");
  79. return -1;
  80. }
  81. status = (uint8_t)response[1];
  82. if (get_uint16(response+2)!=0) { /* port: 0 */
  83. log_warn(LD_PROTOCOL,"Nonzero port in socks response: bad format.");
  84. return -1;
  85. }
  86. if (status != 90) {
  87. log_warn(LD_NET,"Got status response '%d': socks request failed.", status);
  88. return -1;
  89. }
  90. *addr_out = ntohl(get_uint32(response+4));
  91. return 0;
  92. }
  93. /** Send a resolve request for <b>hostname</b> to the Tor listening on
  94. * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
  95. * address (in host order) into *<b>result_addr</b>.
  96. */
  97. static int
  98. do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
  99. uint32_t *result_addr)
  100. {
  101. int s;
  102. struct sockaddr_in socksaddr;
  103. char *req, *cp;
  104. int r, len;
  105. char response_buf[RESPONSE_LEN];
  106. tor_assert(hostname);
  107. tor_assert(result_addr);
  108. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  109. if (s<0) {
  110. log_sock_error("creating_socket", -1);
  111. return -1;
  112. }
  113. memset(&socksaddr, 0, sizeof(socksaddr));
  114. socksaddr.sin_family = AF_INET;
  115. socksaddr.sin_port = htons(socksport);
  116. socksaddr.sin_addr.s_addr = htonl(sockshost);
  117. if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
  118. log_sock_error("connecting to SOCKS host", s);
  119. return -1;
  120. }
  121. if ((len = build_socks4a_resolve_request(&req, "", hostname))<0) {
  122. log_err(LD_BUG,"Error generating SOCKS request");
  123. return -1;
  124. }
  125. cp = req;
  126. while (len) {
  127. r = send(s, cp, len, 0);
  128. if (r<0) {
  129. log_sock_error("sending SOCKS request", s);
  130. tor_free(req);
  131. return -1;
  132. }
  133. len -= r;
  134. cp += r;
  135. }
  136. tor_free(req);
  137. len = 0;
  138. while (len < RESPONSE_LEN) {
  139. r = recv(s, response_buf+len, RESPONSE_LEN-len, 0);
  140. if (r==0) {
  141. log_warn(LD_NET,"EOF while reading SOCKS response");
  142. return -1;
  143. }
  144. if (r<0) {
  145. log_sock_error("reading SOCKS response", s);
  146. return -1;
  147. }
  148. len += r;
  149. }
  150. if (parse_socks4a_resolve_response(response_buf, RESPONSE_LEN,
  151. result_addr)<0){
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. /** Print a usage message and exit. */
  157. static void
  158. usage(void)
  159. {
  160. puts("Syntax: tor-resolve [-v] hostname [sockshost:socksport]");
  161. exit(1);
  162. }
  163. /** Entry point to tor-resolve */
  164. int
  165. main(int argc, char **argv)
  166. {
  167. uint32_t sockshost;
  168. uint16_t socksport;
  169. char **arg;
  170. int n_args;
  171. struct in_addr a;
  172. uint32_t result;
  173. char buf[INET_NTOA_BUF_LEN];
  174. arg = &argv[1];
  175. n_args = argc-1;
  176. if (!n_args)
  177. usage();
  178. if (!strcmp(arg[0],"--version")) {
  179. printf("Tor version %s.\n",VERSION);
  180. return 0;
  181. }
  182. if (!strcmp("-v", arg[0])) {
  183. add_stream_log(LOG_DEBUG, LOG_ERR, "<stderr>", stderr);
  184. ++arg; --n_args;
  185. } else {
  186. add_stream_log(LOG_WARN, LOG_ERR, "<stderr>", stderr);
  187. }
  188. if (n_args == 1) {
  189. log_debug(LD_CONFIG, "defaulting to localhost:9050");
  190. sockshost = 0x7f000001u; /* localhost */
  191. socksport = 9050; /* 9050 */
  192. } else if (n_args == 2) {
  193. if (parse_addr_port(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) {
  194. fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
  195. return 1;
  196. }
  197. if (socksport == 0) {
  198. log_debug(LD_CONFIG, "defaulting to port 9050");
  199. socksport = 9050;
  200. }
  201. } else {
  202. usage();
  203. }
  204. if (!strcasecmpend(arg[0], ".onion")) {
  205. fprintf(stderr,
  206. "%s is a hidden service; those don't have IP addresses.\n\n"
  207. "To connect to a hidden service, you need to send the hostname\n"
  208. "to Tor; we suggest an application that uses SOCKS 4a.\n", arg[0]);
  209. return 1;
  210. }
  211. if (network_init()<0) {
  212. log_err(LD_BUG,"Error initializing network; exiting.");
  213. return 1;
  214. }
  215. if (do_resolve(arg[0], sockshost, socksport, &result))
  216. return 1;
  217. a.s_addr = htonl(result);
  218. tor_inet_ntoa(&a, buf, sizeof(buf));
  219. printf("%s\n", buf);
  220. return 0;
  221. }