aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tools/tor-fw-helper/tor-fw-helper-natpmp.c30
-rw-r--r--src/tools/tor-fw-helper/tor-fw-helper-natpmp.h4
-rw-r--r--src/tools/tor-fw-helper/tor-fw-helper-upnp.c40
-rw-r--r--src/tools/tor-fw-helper/tor-fw-helper-upnp.h3
-rw-r--r--src/tools/tor-fw-helper/tor-fw-helper.h4
5 files changed, 37 insertions, 44 deletions
diff --git a/src/tools/tor-fw-helper/tor-fw-helper-natpmp.c b/src/tools/tor-fw-helper/tor-fw-helper-natpmp.c
index 0e0b385f9..257ef0dd3 100644
--- a/src/tools/tor-fw-helper/tor-fw-helper-natpmp.c
+++ b/src/tools/tor-fw-helper/tor-fw-helper-natpmp.c
@@ -110,27 +110,25 @@ wait_until_fd_readable(tor_socket_t fd, struct timeval *timeout)
return 0;
}
-/** Add a TCP port mapping for a single port stored in <b>tor_fw_options</b>
- * using the <b>natpmp_t</b> stored in <b>backend_state</b>. */
int
-tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
- void *backend_state)
+tor_natpmp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
+ int is_verbose, void *backend_state)
{
- natpmp_state_t *state = (natpmp_state_t *) backend_state;
int r = 0;
int x = 0;
int sav_errno;
+ natpmp_state_t *state = (natpmp_state_t *) backend_state;
struct timeval timeout;
- if (tor_fw_options->verbose)
- fprintf(stdout, "V: sending natpmp portmapping request...\n");
+ if (is_verbose)
+ fprintf(stderr, "V: sending natpmp portmapping request...\n");
r = sendnewportmappingrequest(&(state->natpmp), state->protocol,
- tor_fw_options->internal_port,
- tor_fw_options->external_port,
+ internal_port,
+ external_port,
state->lease);
- if (tor_fw_options->verbose)
- fprintf(stdout, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
+ if (is_verbose)
+ fprintf(stderr, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
"returned %d (%s)\n", r, r==12?"SUCCESS":"FAILED");
do {
@@ -139,8 +137,8 @@ tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
if (x == -1)
return -1;
- if (tor_fw_options->verbose)
- fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n");
+ if (is_verbose)
+ fprintf(stderr, "V: attempting to readnatpmpreponseorretry...\n");
r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
sav_errno = errno;
@@ -163,16 +161,14 @@ tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
}
if (r == NATPMP_SUCCESS) {
- fprintf(stdout, "tor-fw-helper: NAT-PMP mapped public port %hu to"
+ 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);
}
- tor_fw_options->nat_pmp_status = 1;
-
- return r;
+ return (r == NATPMP_SUCCESS) ? 0 : -1;
}
/** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device.
diff --git a/src/tools/tor-fw-helper/tor-fw-helper-natpmp.h b/src/tools/tor-fw-helper/tor-fw-helper-natpmp.h
index 54f541bcf..0f97236af 100644
--- a/src/tools/tor-fw-helper/tor-fw-helper-natpmp.h
+++ b/src/tools/tor-fw-helper/tor-fw-helper-natpmp.h
@@ -36,8 +36,8 @@ int tor_natpmp_init(tor_fw_options_t *tor_fw_options, void *backend_state);
int tor_natpmp_cleanup(tor_fw_options_t *tor_fw_options, void *backend_state);
-int tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
- void *backend_state);
+int tor_natpmp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
+ int is_verbose, void *backend_state);
int tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options,
void *backend_state);
diff --git a/src/tools/tor-fw-helper/tor-fw-helper-upnp.c b/src/tools/tor-fw-helper/tor-fw-helper-upnp.c
index 7c104f11c..0977e0d5a 100644
--- a/src/tools/tor-fw-helper/tor-fw-helper-upnp.c
+++ b/src/tools/tor-fw-helper/tor-fw-helper-upnp.c
@@ -154,44 +154,40 @@ tor_upnp_fetch_public_ip(tor_fw_options_t *options, void *backend_state)
return UPNP_ERR_GETEXTERNALIP;
}
-/** Add a TCP port mapping for a single port stored in <b>tor_fw_options</b>
- * and store the results in <b>backend_state</b>. */
int
-tor_upnp_add_tcp_mapping(tor_fw_options_t *options, void *backend_state)
+tor_upnp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
+ int is_verbose, void *backend_state)
{
- miniupnpc_state_t *state = (miniupnpc_state_t *) backend_state;
- int r;
+ int retval;
char internal_port_str[6];
char external_port_str[6];
+ miniupnpc_state_t *state = (miniupnpc_state_t *) backend_state;
if (!state->init) {
- r = tor_upnp_init(options, state);
- if (r != UPNP_ERR_SUCCESS)
- return r;
+ fprintf(stderr, "E: %s but state is not initialized.\n", __func__);
+ return -1;
}
- if (options->verbose)
- fprintf(stdout, "V: internal port: %d, external port: %d\n",
- (int)options->internal_port, (int)options->external_port);
+ if (is_verbose)
+ fprintf(stderr, "V: UPnP: internal port: %u, external port: %u\n",
+ internal_port, external_port);
tor_snprintf(internal_port_str, sizeof(internal_port_str),
- "%d", (int)options->internal_port);
+ "%u", internal_port);
tor_snprintf(external_port_str, sizeof(external_port_str),
- "%d", (int)options->external_port);
+ "%u", external_port);
- r = UPNP_AddPortMapping(state->urls.controlURL,
- state->data.first.servicetype,
- external_port_str, internal_port_str,
+ retval = UPNP_AddPortMapping(state->urls.controlURL,
+ state->data.first.servicetype,
+ external_port_str, internal_port_str,
#ifdef MINIUPNPC15
- state->lanaddr, UPNP_DESC, "TCP", 0);
+ state->lanaddr, UPNP_DESC, "TCP", 0);
#else
- state->lanaddr, UPNP_DESC, "TCP", 0, 0);
+ state->lanaddr, UPNP_DESC, "TCP", 0, 0);
#endif
- if (r != UPNPCOMMAND_SUCCESS)
- return UPNP_ERR_ADDPORTMAPPING;
- options->upnp_status = 1;
- return UPNP_ERR_SUCCESS;
+ return (retval == UPNP_ERR_SUCCESS) ? 0 : -1;
}
+
#endif
diff --git a/src/tools/tor-fw-helper/tor-fw-helper-upnp.h b/src/tools/tor-fw-helper/tor-fw-helper-upnp.h
index f037c75ba..3a061981d 100644
--- a/src/tools/tor-fw-helper/tor-fw-helper-upnp.h
+++ b/src/tools/tor-fw-helper/tor-fw-helper-upnp.h
@@ -36,7 +36,8 @@ int tor_upnp_cleanup(tor_fw_options_t *options, void *backend_state);
int tor_upnp_fetch_public_ip(tor_fw_options_t *options, void *backend_state);
-int tor_upnp_add_tcp_mapping(tor_fw_options_t *options, void *backend_state);
+int tor_upnp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
+ int is_verbose, void *backend_state);
#endif
#endif
diff --git a/src/tools/tor-fw-helper/tor-fw-helper.h b/src/tools/tor-fw-helper/tor-fw-helper.h
index 64d27a540..0981501c9 100644
--- a/src/tools/tor-fw-helper/tor-fw-helper.h
+++ b/src/tools/tor-fw-helper/tor-fw-helper.h
@@ -52,8 +52,8 @@ typedef struct tor_fw_backend_t {
int (*init)(tor_fw_options_t *options, void *backend_state);
int (*cleanup)(tor_fw_options_t *options, void *backend_state);
int (*fetch_public_ip)(tor_fw_options_t *options, void *backend_state);
- int (*add_tcp_mapping)(tor_fw_options_t *options, void *backend_state);
+ int (*add_tcp_mapping)(uint16_t internal_port, uint16_t external_port,
+ int is_verbose, void *backend_state);
} tor_fw_backend_t;
-
#endif