tor-fw-helper-natpmp.c 6.7 KB

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