tor-fw-helper-natpmp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Copyright (c) 2010, Jacob Appelbaum, Steven J. Murdoch.
  2. * Copyright (c) 2010-2012, 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(stdout, "V: natpmp init...\n");
  54. r = initnatpmp(&(state->natpmp), 0, 0);
  55. if (r == 0) {
  56. state->init = 1;
  57. fprintf(stdout, "tor-fw-helper: natpmp initialized...\n");
  58. return r;
  59. } else {
  60. fprintf(stderr, "tor-fw-helper: 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(stdout, "V: natpmp cleanup...\n");
  72. r = closenatpmp(&(state->natpmp));
  73. if (tor_fw_options->verbose)
  74. fprintf(stdout, "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. if (fd >= FD_SETSIZE) {
  84. fprintf(stderr, "E: NAT-PMP FD_SETSIZE error %d\n", fd);
  85. return -1;
  86. }
  87. FD_ZERO(&fds);
  88. FD_SET(fd, &fds);
  89. r = select(fd+1, &fds, NULL, NULL, timeout);
  90. if (r == -1) {
  91. fprintf(stdout, "V: select failed in wait_until_fd_readable: %s\n",
  92. strerror(errno));
  93. return -1;
  94. }
  95. /* XXXX we should really check to see whether fd was readable, or we timed
  96. out. */
  97. return 0;
  98. }
  99. /** Add a TCP port mapping for a single port stored in <b>tor_fw_options</b>
  100. * using the <b>natpmp_t</b> stored in <b>backend_state</b>. */
  101. int
  102. tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
  103. void *backend_state)
  104. {
  105. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  106. int r = 0;
  107. int x = 0;
  108. int sav_errno;
  109. struct timeval timeout;
  110. if (tor_fw_options->verbose)
  111. fprintf(stdout, "V: sending natpmp portmapping request...\n");
  112. r = sendnewportmappingrequest(&(state->natpmp), state->protocol,
  113. tor_fw_options->internal_port,
  114. tor_fw_options->external_port,
  115. state->lease);
  116. if (tor_fw_options->verbose)
  117. fprintf(stdout, "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 (tor_fw_options->verbose)
  125. fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n");
  126. r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
  127. sav_errno = errno;
  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. 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(stdout, "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. tor_fw_options->nat_pmp_status = 1;
  151. return r;
  152. }
  153. /** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device.
  154. * Use the connection context stored in <b>backend_state</b>. */
  155. int
  156. tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options,
  157. void *backend_state)
  158. {
  159. int r = 0;
  160. int x = 0;
  161. int sav_errno;
  162. natpmp_state_t *state = (natpmp_state_t *) backend_state;
  163. struct timeval timeout;
  164. r = sendpublicaddressrequest(&(state->natpmp));
  165. fprintf(stdout, "tor-fw-helper: NAT-PMP sendpublicaddressrequest returned"
  166. " %d (%s)\n", r, r==2?"SUCCESS":"FAILED");
  167. do {
  168. getnatpmprequesttimeout(&(state->natpmp), &timeout);
  169. x = wait_until_fd_readable(state->natpmp.s, &timeout);
  170. if (x == -1)
  171. return -1;
  172. if (tor_fw_options->verbose)
  173. fprintf(stdout, "V: NAT-PMP attempting to read reponse...\n");
  174. r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
  175. sav_errno = errno;
  176. if (tor_fw_options->verbose)
  177. fprintf(stdout, "V: NAT-PMP readnatpmpresponseorretry returned"
  178. " %d\n", r);
  179. if ( r < 0 && r != NATPMP_TRYAGAIN) {
  180. fprintf(stderr, "E: NAT-PMP readnatpmpresponseorretry failed %d\n",
  181. r);
  182. fprintf(stderr, "E: NAT-PMP errno=%d '%s'\n", sav_errno,
  183. strerror(sav_errno));
  184. }
  185. } while (r == NATPMP_TRYAGAIN );
  186. if (r != 0) {
  187. fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
  188. " %d\n", r);
  189. return r;
  190. }
  191. fprintf(stdout, "tor-fw-helper: ExternalIPAddress = %s\n",
  192. inet_ntoa((state->response).pnu.publicaddress.addr));
  193. tor_fw_options->public_ip_status = 1;
  194. if (tor_fw_options->verbose) {
  195. fprintf(stdout, "V: result = %u\n", r);
  196. fprintf(stdout, "V: type = %u\n", (state->response).type);
  197. fprintf(stdout, "V: resultcode = %u\n", (state->response).resultcode);
  198. fprintf(stdout, "V: epoch = %u\n", (state->response).epoch);
  199. }
  200. return r;
  201. }
  202. #endif