tor-fw-helper-natpmp.c 6.7 KB

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