Exemplo n.º 1
0
int
tor_natpmp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
                           int is_verbose, void *backend_state)
{
  int r = 0;
  int x = 0;
  int sav_errno;
  natpmp_state_t *state = (natpmp_state_t *) backend_state;

  struct timeval timeout;

  if (is_verbose)
    fprintf(stderr, "V: sending natpmp portmapping request...\n");
  r = sendnewportmappingrequest(&(state->natpmp), state->protocol,
                                internal_port,
                                external_port,
                                state->lease);
  if (is_verbose)
    fprintf(stderr, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
            "returned %d (%s)\n", r, r==12?"SUCCESS":"FAILED");

  do {
    getnatpmprequesttimeout(&(state->natpmp), &timeout);
    x = wait_until_fd_readable(state->natpmp.s, &timeout);
    if (x == -1)
      return -1;

    if (is_verbose)
      fprintf(stderr, "V: attempting to readnatpmpreponseorretry...\n");
    r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
    sav_errno = tor_socket_errno(state->natpmp.s);

    if (r<0 && r!=NATPMP_TRYAGAIN) {
      fprintf(stderr, "E: readnatpmpresponseorretry failed %d\n", r);
      fprintf(stderr, "E: errno=%d '%s'\n", sav_errno,
              tor_socket_strerror(sav_errno));
    }

  } while (r == NATPMP_TRYAGAIN);

  if (r != 0) {
    /* XXX TODO: NATPMP_* should be formatted into useful error strings */
    fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
            " %d\n", r);
    if (r == -51)
      fprintf(stderr, "E: NAT-PMP It appears that the request was "
              "unauthorized\n");
    return r;
  }

  if (r == NATPMP_SUCCESS) {
    fprintf(stderr, "tor-fw-helper: NAT-PMP mapped public port %hu to"
            " localport %hu liftime %u\n",
            (state->response).pnu.newportmapping.mappedpublicport,
            (state->response).pnu.newportmapping.privateport,
            (state->response).pnu.newportmapping.lifetime);
  }

  return (r == NATPMP_SUCCESS) ? 0 : -1;
}
/** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device.
 * Use the connection context stored in <b>backend_state</b>. */
int
tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options,
                           void *backend_state)
{
  int r = 0;
  int x = 0;
  int sav_errno;
  natpmp_state_t *state = (natpmp_state_t *) backend_state;

  struct timeval timeout;

  r = sendpublicaddressrequest(&(state->natpmp));
  fprintf(stdout, "tor-fw-helper: NAT-PMP sendpublicaddressrequest returned"
          " %d (%s)\n", r, r==2?"SUCCESS":"FAILED");

  do {
    getnatpmprequesttimeout(&(state->natpmp), &timeout);

    x = wait_until_fd_readable(state->natpmp.s, &timeout);
    if (x == -1)
      return -1;

    if (tor_fw_options->verbose)
      fprintf(stdout, "V: NAT-PMP attempting to read reponse...\n");
    r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
    sav_errno = errno;

    if (tor_fw_options->verbose)
      fprintf(stdout, "V: NAT-PMP readnatpmpresponseorretry returned"
              " %d\n", r);

    if ( r < 0 && r != NATPMP_TRYAGAIN) {
      fprintf(stderr, "E: NAT-PMP readnatpmpresponseorretry failed %d\n",
              r);
      fprintf(stderr, "E: NAT-PMP errno=%d '%s'\n", sav_errno,
              strerror(sav_errno));
    }

  } while (r == NATPMP_TRYAGAIN );

  if (r != 0) {
    fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
            " %d\n", r);
    return r;
  }

  fprintf(stdout, "tor-fw-helper: ExternalIPAddress = %s\n",
          inet_ntoa((state->response).pnu.publicaddress.addr));
  tor_fw_options->public_ip_status = 1;

  if (tor_fw_options->verbose) {
    fprintf(stdout, "V: result = %u\n", r);
    fprintf(stdout, "V: type = %u\n", (state->response).type);
    fprintf(stdout, "V: resultcode = %u\n", (state->response).resultcode);
    fprintf(stdout, "V: epoch = %u\n", (state->response).epoch);
  }

  return r;
}