tor-fw-helper-natpmp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Copyright (c) 2010, Jacob Appelbaum, Steven J. Murdoch.
  2. * Copyright (c) 2010-2013, The Tor Project, Inc. */
  3. /* See LICENSE for licensing information */
  4. /**
  5. * \file tor-fw-helper-natpmp.c
  6. * \brief The implementation of our NAT-PMP firewall helper.
  7. **/
  8. #include "orconfig.h"
  9. #ifdef NAT_PMP
  10. #ifdef _WIN32
  11. #define STATICLIB
  12. #endif
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #ifndef _WIN32
  18. #include <arpa/inet.h>
  19. #endif
  20. // debugging stuff
  21. #include <assert.h>
  22. #include "compat.h"
  23. #include "tor-fw-helper.h"
  24. #include "tor-fw-helper-natpmp.h"
  25. /** This hooks NAT-PMP into our multi-backend API. */
  26. static tor_fw_backend_t tor_natpmp_backend = {
  27. "natpmp",
  28. sizeof(struct natpmp_state_t),
  29. tor_natpmp_init,
  30. tor_natpmp_cleanup,
  31. tor_natpmp_fetch_public_ip,
  32. tor_natpmp_add_tcp_mapping
  33. };
  34. /** Return the backend for NAT-PMP. */
  35. const tor_fw_backend_t *
  36. tor_fw_get_natpmp_backend(void)
  37. {
  38. return &tor_natpmp_backend;
  39. }
  40. /** Initialize the NAT-PMP backend and store the results in
  41. * <b>backend_state</b>.*/
  42. int
  43. tor_natpmp_init(tor_fw_options_t *tor_fw_options, void *backend_state)
  44. {
  45. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  46. int r = 0;
  47. memset(&(state->natpmp), 0, sizeof(natpmp_t));
  48. memset(&(state->response), 0, sizeof(natpmpresp_t));
  49. state->init = 0;
  50. state->protocol = NATPMP_PROTOCOL_TCP;
  51. state->lease = NATPMP_DEFAULT_LEASE;
  52. if (tor_fw_options->verbose)
  53. fprintf(stderr, "V: natpmp init...\n");
  54. r = initnatpmp(&(state->natpmp), 0, 0);
  55. if (r == 0) {
  56. state->init = 1;
  57. fprintf(stderr, "V: natpmp initialized...\n");
  58. return r;
  59. } else {
  60. fprintf(stderr, "V: natpmp failed to initialize...\n");
  61. return r;
  62. }
  63. }
  64. /** Tear down the NAT-PMP connection stored in <b>backend_state</b>.*/
  65. int
  66. tor_natpmp_cleanup(tor_fw_options_t *tor_fw_options, void *backend_state)
  67. {
  68. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  69. int r = 0;
  70. if (tor_fw_options->verbose)
  71. fprintf(stderr, "V: natpmp cleanup...\n");
  72. r = closenatpmp(&(state->natpmp));
  73. if (tor_fw_options->verbose)
  74. fprintf(stderr, "V: closing natpmp socket: %d\n", r);
  75. return r;
  76. }
  77. /** Use select() to wait until we can read on fd. */
  78. static int
  79. wait_until_fd_readable(tor_socket_t fd, struct timeval *timeout)
  80. {
  81. int r;
  82. fd_set fds;
  83. #ifndef WIN32
  84. if (fd >= FD_SETSIZE) {
  85. fprintf(stderr, "E: NAT-PMP FD_SETSIZE error %d\n", fd);
  86. return -1;
  87. }
  88. #endif
  89. FD_ZERO(&fds);
  90. FD_SET(fd, &fds);
  91. r = select(fd+1, &fds, NULL, NULL, timeout);
  92. if (r == -1) {
  93. fprintf(stderr, "V: select failed in wait_until_fd_readable: %s\n",
  94. tor_socket_strerror(tor_socket_errno(fd)));
  95. return -1;
  96. }
  97. /* XXXX we should really check to see whether fd was readable, or we timed
  98. out. */
  99. return 0;
  100. }
  101. int
  102. tor_natpmp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
  103. int is_verbose, void *backend_state)
  104. {
  105. int r = 0;
  106. int x = 0;
  107. int sav_errno;
  108. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  109. struct timeval timeout;
  110. if (is_verbose)
  111. fprintf(stderr, "V: sending natpmp portmapping request...\n");
  112. r = sendnewportmappingrequest(&(state->natpmp), state->protocol,
  113. internal_port,
  114. external_port,
  115. state->lease);
  116. if (is_verbose)
  117. fprintf(stderr, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
  118. "returned %d (%s)\n", r, r==12?"SUCCESS":"FAILED");
  119. do {
  120. getnatpmprequesttimeout(&(state->natpmp), &timeout);
  121. x = wait_until_fd_readable(state->natpmp.s, &timeout);
  122. if (x == -1)
  123. return -1;
  124. if (is_verbose)
  125. fprintf(stderr, "V: attempting to readnatpmpreponseorretry...\n");
  126. r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
  127. sav_errno = tor_socket_errno(state->natpmp.s);
  128. if (r<0 && r!=NATPMP_TRYAGAIN) {
  129. fprintf(stderr, "E: readnatpmpresponseorretry failed %d\n", r);
  130. fprintf(stderr, "E: errno=%d '%s'\n", sav_errno,
  131. tor_socket_strerror(sav_errno));
  132. }
  133. } while (r == NATPMP_TRYAGAIN);
  134. if (r != 0) {
  135. /* XXX TODO: NATPMP_* should be formatted into useful error strings */
  136. fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
  137. " %d\n", r);
  138. if (r == -51)
  139. fprintf(stderr, "E: NAT-PMP It appears that the request was "
  140. "unauthorized\n");
  141. return r;
  142. }
  143. if (r == NATPMP_SUCCESS) {
  144. fprintf(stderr, "tor-fw-helper: NAT-PMP mapped public port %hu to"
  145. " localport %hu liftime %u\n",
  146. (state->response).pnu.newportmapping.mappedpublicport,
  147. (state->response).pnu.newportmapping.privateport,
  148. (state->response).pnu.newportmapping.lifetime);
  149. }
  150. return (r == NATPMP_SUCCESS) ? 0 : -1;
  151. }
  152. /** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device.
  153. * Use the connection context stored in <b>backend_state</b>. */
  154. int
  155. tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options,
  156. void *backend_state)
  157. {
  158. int r = 0;
  159. int x = 0;
  160. int sav_errno;
  161. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  162. struct timeval timeout;
  163. r = sendpublicaddressrequest(&(state->natpmp));
  164. fprintf(stderr, "tor-fw-helper: NAT-PMP sendpublicaddressrequest returned"
  165. " %d (%s)\n", r, r==2?"SUCCESS":"FAILED");
  166. do {
  167. getnatpmprequesttimeout(&(state->natpmp), &timeout);
  168. x = wait_until_fd_readable(state->natpmp.s, &timeout);
  169. if (x == -1)
  170. return -1;
  171. if (tor_fw_options->verbose)
  172. fprintf(stderr, "V: NAT-PMP attempting to read reponse...\n");
  173. r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
  174. sav_errno = tor_socket_errno(state->natpmp.s);
  175. if (tor_fw_options->verbose)
  176. fprintf(stderr, "V: NAT-PMP readnatpmpresponseorretry returned"
  177. " %d\n", r);
  178. if ( r < 0 && r != NATPMP_TRYAGAIN) {
  179. fprintf(stderr, "E: NAT-PMP readnatpmpresponseorretry failed %d\n",
  180. r);
  181. fprintf(stderr, "E: NAT-PMP errno=%d '%s'\n", sav_errno,
  182. tor_socket_strerror(sav_errno));
  183. }
  184. } while (r == NATPMP_TRYAGAIN );
  185. if (r != 0) {
  186. fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
  187. " %d\n", r);
  188. return r;
  189. }
  190. fprintf(stderr, "tor-fw-helper: ExternalIPAddress = %s\n",
  191. inet_ntoa((state->response).pnu.publicaddress.addr));
  192. tor_fw_options->public_ip_status = 1;
  193. if (tor_fw_options->verbose) {
  194. fprintf(stderr, "V: result = %u\n", r);
  195. fprintf(stderr, "V: type = %u\n", (state->response).type);
  196. fprintf(stderr, "V: resultcode = %u\n", (state->response).resultcode);
  197. fprintf(stderr, "V: epoch = %u\n", (state->response).epoch);
  198. }
  199. return r;
  200. }
  201. #endif