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; 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 int
tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options, tor_natpmp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
void *backend_state) int is_verbose, void *backend_state)
{ {
natpmp_state_t *state = (natpmp_state_t *) backend_state;
int r = 0; int r = 0;
int x = 0; int x = 0;
int sav_errno; int sav_errno;
natpmp_state_t *state = (natpmp_state_t *) backend_state;
struct timeval timeout; struct timeval timeout;
if (tor_fw_options->verbose) if (is_verbose)
fprintf(stdout, "V: sending natpmp portmapping request...\n"); fprintf(stderr, "V: sending natpmp portmapping request...\n");
r = sendnewportmappingrequest(&(state->natpmp), state->protocol, r = sendnewportmappingrequest(&(state->natpmp), state->protocol,
tor_fw_options->internal_port, internal_port,
tor_fw_options->external_port, external_port,
state->lease); state->lease);
if (tor_fw_options->verbose) if (is_verbose)
fprintf(stdout, "tor-fw-helper: NAT-PMP sendnewportmappingrequest " fprintf(stderr, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
"returned %d (%s)\n", r, r==12?"SUCCESS":"FAILED"); "returned %d (%s)\n", r, r==12?"SUCCESS":"FAILED");
do { do {
@ -139,8 +137,8 @@ tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
if (x == -1) if (x == -1)
return -1; return -1;
if (tor_fw_options->verbose) if (is_verbose)
fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n"); fprintf(stderr, "V: attempting to readnatpmpreponseorretry...\n");
r = readnatpmpresponseorretry(&(state->natpmp), &(state->response)); r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
sav_errno = errno; sav_errno = errno;
@ -163,16 +161,14 @@ tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
} }
if (r == NATPMP_SUCCESS) { 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", " localport %hu liftime %u\n",
(state->response).pnu.newportmapping.mappedpublicport, (state->response).pnu.newportmapping.mappedpublicport,
(state->response).pnu.newportmapping.privateport, (state->response).pnu.newportmapping.privateport,
(state->response).pnu.newportmapping.lifetime); (state->response).pnu.newportmapping.lifetime);
} }
tor_fw_options->nat_pmp_status = 1; return (r == NATPMP_SUCCESS) ? 0 : -1;
return r;
} }
/** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device. /** 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_cleanup(tor_fw_options_t *tor_fw_options, void *backend_state);
int tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options, int tor_natpmp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
void *backend_state); int is_verbose, void *backend_state);
int tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options, int tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options,
void *backend_state); void *backend_state);

View File

@ -154,32 +154,30 @@ tor_upnp_fetch_public_ip(tor_fw_options_t *options, void *backend_state)
return UPNP_ERR_GETEXTERNALIP; 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 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 retval;
int r;
char internal_port_str[6]; char internal_port_str[6];
char external_port_str[6]; char external_port_str[6];
miniupnpc_state_t *state = (miniupnpc_state_t *) backend_state;
if (!state->init) { if (!state->init) {
r = tor_upnp_init(options, state); fprintf(stderr, "E: %s but state is not initialized.\n", __func__);
if (r != UPNP_ERR_SUCCESS) return -1;
return r;
} }
if (options->verbose) if (is_verbose)
fprintf(stdout, "V: internal port: %d, external port: %d\n", fprintf(stderr, "V: UPnP: internal port: %u, external port: %u\n",
(int)options->internal_port, (int)options->external_port); internal_port, external_port);
tor_snprintf(internal_port_str, sizeof(internal_port_str), 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), tor_snprintf(external_port_str, sizeof(external_port_str),
"%d", (int)options->external_port); "%u", external_port);
r = UPNP_AddPortMapping(state->urls.controlURL, retval = UPNP_AddPortMapping(state->urls.controlURL,
state->data.first.servicetype, state->data.first.servicetype,
external_port_str, internal_port_str, external_port_str, internal_port_str,
#ifdef MINIUPNPC15 #ifdef MINIUPNPC15
@ -187,11 +185,9 @@ tor_upnp_add_tcp_mapping(tor_fw_options_t *options, void *backend_state)
#else #else
state->lanaddr, UPNP_DESC, "TCP", 0, 0); state->lanaddr, UPNP_DESC, "TCP", 0, 0);
#endif #endif
if (r != UPNPCOMMAND_SUCCESS)
return UPNP_ERR_ADDPORTMAPPING;
options->upnp_status = 1; return (retval == UPNP_ERR_SUCCESS) ? 0 : -1;
return UPNP_ERR_SUCCESS;
} }
#endif #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_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
#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 (*init)(tor_fw_options_t *options, void *backend_state);
int (*cleanup)(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 (*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; } tor_fw_backend_t;
#endif #endif