Refactor our backends' interface.

This commit is contained in:
George Kadianakis 2012-06-07 00:56:23 +03:00
parent c5778553dc
commit b9e160446a
5 changed files with 37 additions and 44 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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