tor-resolve.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright 2004 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. static int
  41. build_socks4a_resolve_request(char **out,
  42. const char *username,
  43. const char *hostname)
  44. {
  45. size_t len;
  46. tor_assert(out);
  47. tor_assert(username);
  48. tor_assert(hostname);
  49. len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
  50. *out = tor_malloc(len);
  51. (*out)[0] = 4; /* SOCKS version 4 */
  52. (*out)[1] = 0xF0; /* Command: resolve. */
  53. set_uint16((*out)+2, htons(0)); /* port: 0. */
  54. set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
  55. strcpy((*out)+8, username);
  56. strcpy((*out)+8+strlen(username)+1, hostname);
  57. return len;
  58. }
  59. static int
  60. parse_socks4a_resolve_response(const char *response, size_t len,
  61. uint32_t *addr_out)
  62. {
  63. uint8_t status;
  64. tor_assert(response);
  65. tor_assert(addr_out);
  66. if (len < RESPONSE_LEN)
  67. return -1;
  68. if (((uint8_t)response[0])!=0) /* version: 0 */
  69. return -1;
  70. status = (uint8_t)response[1];
  71. if (get_uint16(response+2)!=0) /* port: 0 */
  72. return -1;
  73. if (status != 90)
  74. return -1;
  75. *addr_out = ntohl(get_uint32(response+4));
  76. return 0;
  77. }
  78. static int
  79. do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
  80. uint32_t *result_addr)
  81. {
  82. int s;
  83. struct sockaddr_in socksaddr;
  84. char *req, *cp;
  85. int r, len;
  86. char response_buf[RESPONSE_LEN];
  87. tor_assert(hostname);
  88. tor_assert(result_addr);
  89. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  90. if (s<0) {
  91. log_sock_error("creating_socket", -1);
  92. return -1;
  93. }
  94. memset(&socksaddr, 0, sizeof(socksaddr));
  95. socksaddr.sin_family = AF_INET;
  96. socksaddr.sin_port = htons(socksport);
  97. socksaddr.sin_addr.s_addr = htonl(sockshost);
  98. if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
  99. log_sock_error("connecting to SOCKS host", s);
  100. return -1;
  101. }
  102. if ((len = build_socks4a_resolve_request(&req, "", hostname))<0) {
  103. log_fn(LOG_ERR, "Error generating SOCKS request");
  104. return -1;
  105. }
  106. cp = req;
  107. while (len) {
  108. r = send(s, cp, len, 0);
  109. if (r<0) {
  110. log_sock_error("sending SOCKS request", s);
  111. tor_free(req);
  112. return -1;
  113. }
  114. len -= r;
  115. cp += r;
  116. }
  117. tor_free(req);
  118. len = 0;
  119. while (len < RESPONSE_LEN) {
  120. r = recv(s, response_buf+len, RESPONSE_LEN-len, 0);
  121. if (r<0) {
  122. log_sock_error("reading SOCKS response", s);
  123. return -1;
  124. }
  125. len += r;
  126. }
  127. if (parse_socks4a_resolve_response(response_buf, RESPONSE_LEN,result_addr)<0){
  128. log_fn(LOG_ERR, "Error parsing SOCKS response");
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. static void
  134. usage(void)
  135. {
  136. puts("Syntax: tor-resolve [-v] hostname [sockshost:socksport]");
  137. exit(1);
  138. }
  139. int
  140. main(int argc, char **argv)
  141. {
  142. uint32_t sockshost;
  143. uint16_t socksport;
  144. char **arg;
  145. int n_args;
  146. struct in_addr a;
  147. uint32_t result;
  148. arg = &argv[1];
  149. n_args = argc-1;
  150. if (argc < 2)
  151. usage();
  152. if (!strcmp("-v", arg[0])) {
  153. add_stream_log(LOG_DEBUG, LOG_ERR, "<stderr>", stderr);
  154. ++arg; --n_args;
  155. } else {
  156. add_stream_log(LOG_WARN, LOG_ERR, "<stderr>", stderr);
  157. }
  158. if (n_args == 1) {
  159. log_fn(LOG_DEBUG, "defaulting to localhost:9050");
  160. sockshost = 0x7f000001u; /* localhost */
  161. socksport = 9050; /* 9050 */
  162. } else if (n_args == 2) {
  163. if (parse_addr_port(arg[1], NULL, &sockshost, &socksport)<0) {
  164. fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
  165. return 1;
  166. }
  167. if (socksport == 0) {
  168. log_fn(LOG_DEBUG, "defaulting to port 9050");
  169. socksport = 9050;
  170. }
  171. } else {
  172. usage();
  173. }
  174. if (do_resolve(arg[0], sockshost, socksport, &result))
  175. return 1;
  176. a.s_addr = htonl(result);
  177. printf("%s\n", inet_ntoa(a));
  178. return 0;
  179. }