tor-resolve.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, "Error while %s: %s", act, \
  39. tor_socket_strerror(tor_socket_errno(_s))); } while (0)
  40. /** Set *out to a newly allocated SOCKS4a resolve request with
  41. * username and hostname as provided. Return the number of bytes in
  42. * 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 len-byte SOCKS4a response in <b>response</b>, set *addr_out to the
  63. * address it contains (in host order). Return 0 on success, -1 on error.
  64. */
  65. static int
  66. parse_socks4a_resolve_response(const char *response, size_t len,
  67. uint32_t *addr_out)
  68. {
  69. uint8_t status;
  70. tor_assert(response);
  71. tor_assert(addr_out);
  72. if (len < RESPONSE_LEN) {
  73. log_fn(LOG_WARN,"Truncated socks response.");
  74. return -1;
  75. }
  76. if (((uint8_t)response[0])!=0) { /* version: 0 */
  77. log_fn(LOG_WARN,"Nonzero version in socks response: bad format.");
  78. return -1;
  79. }
  80. status = (uint8_t)response[1];
  81. if (get_uint16(response+2)!=0) { /* port: 0 */
  82. log_fn(LOG_WARN,"Nonzero port in socks response: bad format.");
  83. return -1;
  84. }
  85. if (status != 90) {
  86. log_fn(LOG_WARN,"Got status response '%d': socks request failed.", status);
  87. return -1;
  88. }
  89. *addr_out = ntohl(get_uint32(response+4));
  90. return 0;
  91. }
  92. /** Send a resolve request for <b>hostname</b> to the Tor listening on
  93. * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
  94. * address (in host order) into <b>result_addr</b>.
  95. */
  96. static int
  97. do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
  98. uint32_t *result_addr)
  99. {
  100. int s;
  101. struct sockaddr_in socksaddr;
  102. char *req, *cp;
  103. int r, len;
  104. char response_buf[RESPONSE_LEN];
  105. tor_assert(hostname);
  106. tor_assert(result_addr);
  107. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  108. if (s<0) {
  109. log_sock_error("creating_socket", -1);
  110. return -1;
  111. }
  112. memset(&socksaddr, 0, sizeof(socksaddr));
  113. socksaddr.sin_family = AF_INET;
  114. socksaddr.sin_port = htons(socksport);
  115. socksaddr.sin_addr.s_addr = htonl(sockshost);
  116. if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
  117. log_sock_error("connecting to SOCKS host", s);
  118. return -1;
  119. }
  120. if ((len = build_socks4a_resolve_request(&req, "", hostname))<0) {
  121. log_fn(LOG_ERR, "Error generating SOCKS request");
  122. return -1;
  123. }
  124. cp = req;
  125. while (len) {
  126. r = send(s, cp, len, 0);
  127. if (r<0) {
  128. log_sock_error("sending SOCKS request", s);
  129. tor_free(req);
  130. return -1;
  131. }
  132. len -= r;
  133. cp += r;
  134. }
  135. tor_free(req);
  136. len = 0;
  137. while (len < RESPONSE_LEN) {
  138. r = recv(s, response_buf+len, RESPONSE_LEN-len, 0);
  139. if (r==0) {
  140. log_fn(LOG_WARN,"EOF while reading SOCKS response");
  141. return -1;
  142. }
  143. if (r<0) {
  144. log_sock_error("reading SOCKS response", s);
  145. return -1;
  146. }
  147. len += r;
  148. }
  149. if (parse_socks4a_resolve_response(response_buf, RESPONSE_LEN,result_addr)<0){
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. /** Print a usage message and exit. */
  155. static void
  156. usage(void)
  157. {
  158. puts("Syntax: tor-resolve [-v] hostname [sockshost:socksport]");
  159. exit(1);
  160. }
  161. /** Entry point to tor-resolve */
  162. int
  163. main(int argc, char **argv)
  164. {
  165. uint32_t sockshost;
  166. uint16_t socksport;
  167. char **arg;
  168. int n_args;
  169. struct in_addr a;
  170. uint32_t result;
  171. char buf[INET_NTOA_BUF_LEN];
  172. arg = &argv[1];
  173. n_args = argc-1;
  174. if (!n_args)
  175. usage();
  176. if (!strcmp(arg[0],"--version")) {
  177. printf("Tor version %s.\n",VERSION);
  178. return 0;
  179. }
  180. if (!strcmp("-v", arg[0])) {
  181. add_stream_log(LOG_DEBUG, LOG_ERR, "<stderr>", stderr);
  182. ++arg; --n_args;
  183. } else {
  184. add_stream_log(LOG_WARN, LOG_ERR, "<stderr>", stderr);
  185. }
  186. if (n_args == 1) {
  187. log_fn(LOG_DEBUG, "defaulting to localhost:9050");
  188. sockshost = 0x7f000001u; /* localhost */
  189. socksport = 9050; /* 9050 */
  190. } else if (n_args == 2) {
  191. if (parse_addr_port(arg[1], NULL, &sockshost, &socksport)<0) {
  192. fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
  193. return 1;
  194. }
  195. if (socksport == 0) {
  196. log_fn(LOG_DEBUG, "defaulting to port 9050");
  197. socksport = 9050;
  198. }
  199. } else {
  200. usage();
  201. }
  202. if (!strcasecmpend(arg[0], ".onion")) {
  203. fprintf(stderr, "%s is a hidden service; those don't have IP addresses.\n\n"
  204. "To connect to a hidden service, you need to send the hostname to Tor;\n"
  205. "we suggest SOCKS 4a.\n", arg[0]);
  206. return 1;
  207. }
  208. if (network_init()<0) {
  209. log_fn(LOG_ERR,"Error initializing network; exiting.");
  210. return 1;
  211. }
  212. if (do_resolve(arg[0], sockshost, socksport, &result))
  213. return 1;
  214. a.s_addr = htonl(result);
  215. tor_inet_ntoa(&a, buf, sizeof(buf));
  216. printf("%s\n", buf);
  217. return 0;
  218. }