diff options
author | Nick Mathewson <nickm@torproject.org> | 2010-09-30 15:21:37 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2010-09-30 15:55:42 -0400 |
commit | 0e9d969bb2bb4bb9a5f7db1db824c729d7142cbc (patch) | |
tree | 0d4b17893ba8b6d653bcc71f56ceb417cbaa4096 /src/tools | |
parent | 2835dcf69f5bafd634a1b65fa28c08d4f1ce3306 (diff) | |
download | tor-0e9d969bb2bb4bb9a5f7db1db824c729d7142cbc.tar.gz tor-0e9d969bb2bb4bb9a5f7db1db824c729d7142cbc.zip |
Fix space and formatting issues
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/tor-fw-helper/tor-fw-helper-natpmp.c | 275 | ||||
-rw-r--r-- | src/tools/tor-fw-helper/tor-fw-helper-natpmp.h | 14 | ||||
-rw-r--r-- | src/tools/tor-fw-helper/tor-fw-helper-upnp.c | 35 | ||||
-rw-r--r-- | src/tools/tor-fw-helper/tor-fw-helper.c | 496 | ||||
-rw-r--r-- | src/tools/tor-fw-helper/tor-fw-helper.h | 38 |
5 files changed, 419 insertions, 439 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 1b896fa3ba..51b456a8c7 100644 --- a/src/tools/tor-fw-helper/tor-fw-helper-natpmp.c +++ b/src/tools/tor-fw-helper/tor-fw-helper-natpmp.c @@ -23,19 +23,19 @@ /** This hooks NAT-PMP into our multi-backend API. */ static tor_fw_backend_t tor_natpmp_backend = { - "natpmp", - sizeof(struct natpmp_state_t), - tor_natpmp_init, - tor_natpmp_cleanup, - tor_natpmp_fetch_public_ip, - tor_natpmp_add_tcp_mapping + "natpmp", + sizeof(struct natpmp_state_t), + tor_natpmp_init, + tor_natpmp_cleanup, + tor_natpmp_fetch_public_ip, + tor_natpmp_add_tcp_mapping }; /** Return the backend for NAT-PMP. */ const tor_fw_backend_t * tor_fw_get_natpmp_backend(void) { - return &tor_natpmp_backend; + return &tor_natpmp_backend; } /** Initialize the NAT-PMP backend and store the results in @@ -43,41 +43,41 @@ tor_fw_get_natpmp_backend(void) int tor_natpmp_init(tor_fw_options_t *tor_fw_options, void *backend_state) { - natpmp_state_t *state = (natpmp_state_t *) backend_state; - int r = 0; - - memset(&(state->natpmp), 0, sizeof(natpmp_t)); - memset(&(state->response), 0, sizeof(natpmpresp_t)); - state->init = 0; - state->protocol = NATPMP_PROTOCOL_TCP; - state->lease = NATPMP_DEFAULT_LEASE; - - if (tor_fw_options->verbose) - fprintf(stdout, "V: natpmp init...\n"); - - r = initnatpmp(&(state->natpmp)); - if (r == 0) { - state->init = 1; - fprintf(stdout, "tor-fw-helper: natpmp initialized...\n"); - return r; - } else { - fprintf(stderr, "tor-fw-helper: natpmp failed to initialize...\n"); - return r; - } + natpmp_state_t *state = (natpmp_state_t *) backend_state; + int r = 0; + + memset(&(state->natpmp), 0, sizeof(natpmp_t)); + memset(&(state->response), 0, sizeof(natpmpresp_t)); + state->init = 0; + state->protocol = NATPMP_PROTOCOL_TCP; + state->lease = NATPMP_DEFAULT_LEASE; + + if (tor_fw_options->verbose) + fprintf(stdout, "V: natpmp init...\n"); + + r = initnatpmp(&(state->natpmp)); + if (r == 0) { + state->init = 1; + fprintf(stdout, "tor-fw-helper: natpmp initialized...\n"); + return r; + } else { + fprintf(stderr, "tor-fw-helper: natpmp failed to initialize...\n"); + return r; + } } /** Tear down the NAT-PMP connection stored in <b>backend_state</b>.*/ int tor_natpmp_cleanup(tor_fw_options_t *tor_fw_options, void *backend_state) { - natpmp_state_t *state = (natpmp_state_t *) backend_state; - int r = 0; - if (tor_fw_options->verbose) - fprintf(stdout, "V: natpmp cleanup...\n"); - r = closenatpmp(&(state->natpmp)); - if (tor_fw_options->verbose) - fprintf(stdout, "V: closing natpmp socket: %d\n", r); - return r; + natpmp_state_t *state = (natpmp_state_t *) backend_state; + int r = 0; + if (tor_fw_options->verbose) + fprintf(stdout, "V: natpmp cleanup...\n"); + r = closenatpmp(&(state->natpmp)); + if (tor_fw_options->verbose) + fprintf(stdout, "V: closing natpmp socket: %d\n", r); + return r; } /** Use select() to wait until we can read on fd. */ @@ -109,65 +109,63 @@ int tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options, 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; + int r = 0; + int x = 0; + int sav_errno; + + struct timeval timeout; + + if (tor_fw_options->verbose) + fprintf(stdout, "V: sending natpmp portmapping request...\n"); + r = sendnewportmappingrequest(&(state->natpmp), state->protocol, + tor_fw_options->internal_port, + tor_fw_options->external_port, + state->lease); + if (tor_fw_options->verbose) + fprintf(stdout, "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; - struct timeval timeout; - - if (tor_fw_options->verbose) - fprintf(stdout, "V: sending natpmp portmapping request...\n"); - r = sendnewportmappingrequest(&(state->natpmp), state->protocol, - tor_fw_options->internal_port, - tor_fw_options->external_port, - state->lease); if (tor_fw_options->verbose) - fprintf(stdout, "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 (tor_fw_options->verbose) - fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n"); - r = readnatpmpresponseorretry(&(state->natpmp), &(state->response)); - sav_errno = errno; - - if (r<0 && r!=NATPMP_TRYAGAIN) - { - fprintf(stderr, "E: readnatpmpresponseorretry failed %d\n", r); - fprintf(stderr, "E: errno=%d '%s'\n", sav_errno, - 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; + fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n"); + r = readnatpmpresponseorretry(&(state->natpmp), &(state->response)); + sav_errno = errno; + + if (r<0 && r!=NATPMP_TRYAGAIN) { + fprintf(stderr, "E: readnatpmpresponseorretry failed %d\n", r); + fprintf(stderr, "E: errno=%d '%s'\n", sav_errno, + strerror(sav_errno)); } - if (r == NATPMP_SUCCESS) { - fprintf(stdout, "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; + } 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(stdout, "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; } /** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device. @@ -176,63 +174,60 @@ 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; - } + int r = 0; + int x = 0; + int sav_errno; + natpmp_state_t *state = (natpmp_state_t *) backend_state; - fprintf(stdout, "tor-fw-helper: ExternalIPAddress = %s\n", - inet_ntoa((state->response).pnu.publicaddress.addr)); - tor_fw_options->public_ip_status = 1; + 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: 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); + 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; } #endif 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 38448dffa3..5a6540b68f 100644 --- a/src/tools/tor-fw-helper/tor-fw-helper-natpmp.h +++ b/src/tools/tor-fw-helper/tor-fw-helper-natpmp.h @@ -21,13 +21,13 @@ * various NAT-PMP parameters, and of course the status of the motion in the * NAT-PMP ocean. */ typedef struct natpmp_state_t { - natpmp_t natpmp; - natpmpresp_t response; - int fetch_public_ip; - int status; - int init; /**< Have we been initialized? */ - int protocol; /**< This will only be TCP. */ - int lease; + natpmp_t natpmp; + natpmpresp_t response; + int fetch_public_ip; + int status; + int init; /**< Have we been initialized? */ + int protocol; /**< This will only be TCP. */ + int lease; } natpmp_state_t; const tor_fw_backend_t *tor_fw_get_natpmp_backend(void); 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 7cf6be9160..b3b80d6987 100644 --- a/src/tools/tor-fw-helper/tor-fw-helper-upnp.c +++ b/src/tools/tor-fw-helper/tor-fw-helper-upnp.c @@ -40,19 +40,19 @@ /** This hooks miniupnpc into our multi-backend API. */ static tor_fw_backend_t tor_miniupnp_backend = { - "miniupnp", - sizeof(struct miniupnpc_state_t), - tor_upnp_init, - tor_upnp_cleanup, - tor_upnp_fetch_public_ip, - tor_upnp_add_tcp_mapping + "miniupnp", + sizeof(struct miniupnpc_state_t), + tor_upnp_init, + tor_upnp_cleanup, + tor_upnp_fetch_public_ip, + tor_upnp_add_tcp_mapping }; /** Return the backend for miniupnp. */ const tor_fw_backend_t * tor_fw_get_miniupnp_backend(void) { - return &tor_miniupnp_backend; + return &tor_miniupnp_backend; } /** Initialize the UPnP backend and store the results in @@ -64,7 +64,7 @@ tor_upnp_init(tor_fw_options_t *options, void *backend_state) This leaks the user agent from the client to the router - perhaps we don't want to do that? eg: - User-Agent: Ubuntu/10.04, UPnP/1.0, MiniUPnPc/1.4 + User-Agent: Ubuntu/10.04, UPnP/1.0, MiniUPnPc/1.4 */ miniupnpc_state_t *state = (miniupnpc_state_t *) backend_state; @@ -112,7 +112,7 @@ tor_upnp_cleanup(tor_fw_options_t *options, void *backend_state) } /** Fetch our likely public IP from our upstream UPnP IGD enabled NAT device. -* Use the connection context stored in <b>backend_state</b>. */ + * Use the connection context stored in <b>backend_state</b>. */ int tor_upnp_fetch_public_ip(tor_fw_options_t *options, void *backend_state) { @@ -138,12 +138,13 @@ tor_upnp_fetch_public_ip(tor_fw_options_t *options, void *backend_state) externalIPAddress); tor_upnp_cleanup(options, state); options->public_ip_status = 1; return UPNP_ERR_SUCCESS; - } else + } else { goto err; + } - err: - tor_upnp_cleanup(options, state); - return UPNP_ERR_GETEXTERNALIP; + err: + tor_upnp_cleanup(options, state); + return UPNP_ERR_GETEXTERNALIP; } /** Add a TCP port mapping for a single port stored in <b>tor_fw_options</b> @@ -163,13 +164,13 @@ tor_upnp_add_tcp_mapping(tor_fw_options_t *options, void *backend_state) } if (options->verbose) - fprintf(stdout, "V: internal port: %d, external port: %d\n", - (int)options->internal_port, (int)options->external_port); + fprintf(stdout, "V: internal port: %d, external port: %d\n", + (int)options->internal_port, (int)options->external_port); tor_snprintf(internal_port_str, sizeof(internal_port_str), - "%d", (int)options->internal_port); + "%d", (int)options->internal_port); tor_snprintf(external_port_str, sizeof(external_port_str), - "%d", (int)options->external_port); + "%d", (int)options->external_port); r = UPNP_AddPortMapping(state->urls.controlURL, state->data.first.servicetype, diff --git a/src/tools/tor-fw-helper/tor-fw-helper.c b/src/tools/tor-fw-helper/tor-fw-helper.c index 4639f3def8..378672a20a 100644 --- a/src/tools/tor-fw-helper/tor-fw-helper.c +++ b/src/tools/tor-fw-helper/tor-fw-helper.c @@ -33,73 +33,70 @@ including the total number of helper backends, function pointers, and helper state. */ typedef struct backends_t { - /** The total number of backends */ - int n_backends; - /** The backend functions as an array */ - tor_fw_backend_t backend_ops[MAX_BACKENDS]; - /** The internal backend state */ - void *backend_state[MAX_BACKENDS]; + /** The total number of backends */ + int n_backends; + /** The backend functions as an array */ + tor_fw_backend_t backend_ops[MAX_BACKENDS]; + /** The internal backend state */ + void *backend_state[MAX_BACKENDS]; } backends_t; -int -init_backends(tor_fw_options_t *options, backends_t *backends); - /** Initalize each backend helper with the user input stored in <b>options</b> * and put the results in the <b>backends</b> struct. */ -int +static int init_backends(tor_fw_options_t *options, backends_t *backends) { - int n_available = 0; - int i, r, n; - tor_fw_backend_t *backend_ops_list[MAX_BACKENDS]; - void *data = NULL; - /* First, build a list of the working backends. */ - n = 0; + int n_available = 0; + int i, r, n; + tor_fw_backend_t *backend_ops_list[MAX_BACKENDS]; + void *data = NULL; + /* First, build a list of the working backends. */ + n = 0; #ifdef MINIUPNPC - backend_ops_list[n++] = (tor_fw_backend_t *) tor_fw_get_miniupnp_backend(); + backend_ops_list[n++] = (tor_fw_backend_t *) tor_fw_get_miniupnp_backend(); #endif #ifdef NAT_PMP - backend_ops_list[n++] = (tor_fw_backend_t *) tor_fw_get_natpmp_backend(); + backend_ops_list[n++] = (tor_fw_backend_t *) tor_fw_get_natpmp_backend(); #endif - n_available = n; - - /* Now, for each backend that might work, try to initialize it. - * That's how we roll, initialized. - */ - n = 0; - for (i=0; i<n_available; ++i) { - data = calloc(1, backend_ops_list[i]->state_len); - if (!data) { - perror("calloc"); - exit(1); - } - r = backend_ops_list[i]->init(options, data); - if (r == 0) { - backends->backend_ops[n] = *backend_ops_list[i]; - backends->backend_state[n] = data; - n++; - } else { - free(data); - } + n_available = n; + + /* Now, for each backend that might work, try to initialize it. + * That's how we roll, initialized. + */ + n = 0; + for (i=0; i<n_available; ++i) { + data = calloc(1, backend_ops_list[i]->state_len); + if (!data) { + perror("calloc"); + exit(1); + } + r = backend_ops_list[i]->init(options, data); + if (r == 0) { + backends->backend_ops[n] = *backend_ops_list[i]; + backends->backend_state[n] = data; + n++; + } else { + free(data); } - backends->n_backends = n; + } + backends->n_backends = n; - return n; + return n; } /** Return the proper commandline switches when the user needs information. */ static void usage(void) { - fprintf(stderr, "tor-fw-helper usage:\n" - " [-h|--help]\n" - " [-T|--Test]\n" - " [-v|--verbose]\n" - " [-g|--fetch-public-ip]\n" - " -i|--internal-or-port [TCP port]\n" - " [-e|--external-or-port [TCP port]]\n" - " [-d|--internal-dir-port [TCP port]\n" - " [-p|--external-dir-port [TCP port]]]\n"); + fprintf(stderr, "tor-fw-helper usage:\n" + " [-h|--help]\n" + " [-T|--Test]\n" + " [-v|--verbose]\n" + " [-g|--fetch-public-ip]\n" + " -i|--internal-or-port [TCP port]\n" + " [-e|--external-or-port [TCP port]]\n" + " [-d|--internal-dir-port [TCP port]\n" + " [-p|--external-dir-port [TCP port]]]\n"); } /** Log commandline options to a hardcoded file <b>tor-fw-helper.log</b> in the @@ -136,9 +133,9 @@ log_commandline_options(int argc, char **argv) return retval; /* If there was an error during writing */ - error: - fclose(logfile); - return -1; + error: + fclose(logfile); + return -1; } /** Iterate over over each of the supported <b>backends</b> and attempt to @@ -147,26 +144,25 @@ static void tor_fw_fetch_public_ip(tor_fw_options_t *tor_fw_options, backends_t *backends) { - int i; - int r = 0; - - if (tor_fw_options->verbose) - fprintf(stdout, "V: tor_fw_fetch_public_ip\n"); - - for (i=0; i<backends->n_backends; ++i) { - if (tor_fw_options->verbose) - { - fprintf(stdout, "V: running backend_state now: %i\n", i); - fprintf(stdout, "V: size of backend state: %u\n", - (int)(backends->backend_ops)[i].state_len); - fprintf(stdout, "V: backend state name: %s\n", - (char *)(backends->backend_ops)[i].name); - } - r = ((backends->backend_ops)[i].fetch_public_ip(tor_fw_options, - (backends->backend_state)[i])); - fprintf(stdout, "tor-fw-helper: tor_fw_fetch_public_ip backend %s " - " returned: %i\n", (char *)(backends->backend_ops)[i].name, r); - } + int i; + int r = 0; + + if (tor_fw_options->verbose) + fprintf(stdout, "V: tor_fw_fetch_public_ip\n"); + + for (i=0; i<backends->n_backends; ++i) { + if (tor_fw_options->verbose) { + fprintf(stdout, "V: running backend_state now: %i\n", i); + fprintf(stdout, "V: size of backend state: %u\n", + (int)(backends->backend_ops)[i].state_len); + fprintf(stdout, "V: backend state name: %s\n", + (char *)(backends->backend_ops)[i].name); + } + r = backends->backend_ops[i].fetch_public_ip(tor_fw_options, + backends->backend_state[i]); + fprintf(stdout, "tor-fw-helper: tor_fw_fetch_public_ip backend %s " + " returned: %i\n", (char *)(backends->backend_ops)[i].name, r); + } } /** Iterate over each of the supported <b>backends</b> and attempt to add a @@ -175,26 +171,25 @@ static void tor_fw_add_or_port(tor_fw_options_t *tor_fw_options, backends_t *backends) { - int i; - int r = 0; - - if (tor_fw_options->verbose) - fprintf(stdout, "V: tor_fw_add_or_port\n"); - - for (i=0; i<backends->n_backends; ++i) { - if (tor_fw_options->verbose) - { - fprintf(stdout, "V: running backend_state now: %i\n", i); - fprintf(stdout, "V: size of backend state: %u\n", - (int)(backends->backend_ops)[i].state_len); - fprintf(stdout, "V: backend state name: %s\n", - (char *)(backends->backend_ops)[i].name); - } - r = ((backends->backend_ops)[i].add_tcp_mapping(tor_fw_options, - (backends->backend_state)[i])); - fprintf(stdout, "tor-fw-helper: tor_fw_add_or_port backend %s " - "returned: %i\n", (char *)(backends->backend_ops)[i].name, r); + int i; + int r = 0; + + if (tor_fw_options->verbose) + fprintf(stdout, "V: tor_fw_add_or_port\n"); + + for (i=0; i<backends->n_backends; ++i) { + if (tor_fw_options->verbose) { + fprintf(stdout, "V: running backend_state now: %i\n", i); + fprintf(stdout, "V: size of backend state: %u\n", + (int)(backends->backend_ops)[i].state_len); + fprintf(stdout, "V: backend state name: %s\n", + (const char *) backends->backend_ops[i].name); } + r = backends->backend_ops[i].add_tcp_mapping(tor_fw_options, + backends->backend_state[i]); + fprintf(stdout, "tor-fw-helper: tor_fw_add_or_port backend %s " + "returned: %i\n", (const char *) backends->backend_ops[i].name, r); + } } /** Iterate over each of the supported <b>backends</b> and attempt to add a @@ -203,177 +198,166 @@ static void tor_fw_add_dir_port(tor_fw_options_t *tor_fw_options, backends_t *backends) { - int i; - int r = 0; - - if (tor_fw_options->verbose) - fprintf(stdout, "V: tor_fw_add_dir_port\n"); - - for (i=0; i<backends->n_backends; ++i) { - if (tor_fw_options->verbose) - { - fprintf(stdout, "V: running backend_state now: %i\n", i); - fprintf(stdout, "V: size of backend state: %u\n", - (int)(backends->backend_ops)[i].state_len); - fprintf(stdout, "V: backend state name: %s\n", - (char *)(backends->backend_ops)[i].name); - } - r=((backends->backend_ops)[i].add_tcp_mapping(tor_fw_options, - (backends->backend_state)[i])); - fprintf(stdout, "tor-fw-helper: tor_fw_add_dir_port backend %s " - "returned: %i\n", (char *)(backends->backend_ops)[i].name, r); + int i; + int r = 0; + + if (tor_fw_options->verbose) + fprintf(stdout, "V: tor_fw_add_dir_port\n"); + + for (i=0; i<backends->n_backends; ++i) { + if (tor_fw_options->verbose) { + fprintf(stdout, "V: running backend_state now: %i\n", i); + fprintf(stdout, "V: size of backend state: %u\n", + (int)(backends->backend_ops)[i].state_len); + fprintf(stdout, "V: backend state name: %s\n", + (char *)(backends->backend_ops)[i].name); } + r = backends->backend_ops[i].add_tcp_mapping(tor_fw_options, + backends->backend_state[i]); + fprintf(stdout, "tor-fw-helper: tor_fw_add_dir_port backend %s " + "returned: %i\n", (const char *)backends->backend_ops[i].name, r); + } } int main(int argc, char **argv) { - int r = 0; - int c = 0; - - tor_fw_options_t tor_fw_options = {0,0,0,0,0,0,0,0,0,0,0,0,0}; - backends_t backend_state; - - while (1) - { - int option_index = 0; - static struct option long_options[] = - { - {"verbose", 0, 0, 'v'}, - {"help", 0, 0, 'h'}, - {"internal-or-port", 1, 0, 'i'}, - {"external-or-port", 1, 0, 'e'}, - {"internal-dir-port", 1, 0, 'd'}, - {"external-dir-port", 1, 0, 'p'}, - {"fetch-public-ip", 0, 0, 'g'}, - {"test-commandline", 0, 0, 'T'}, - {0, 0, 0, 0} - }; - - c = getopt_long(argc, argv, "vhi:e:d:p:gT", - long_options, &option_index); - if (c == -1) - break; - - switch (c) - { - case 'v': tor_fw_options.verbose = 1; break; - case 'h': tor_fw_options.help = 1; usage(); exit(1); break; - case 'i': sscanf(optarg, "%hu", &tor_fw_options.private_or_port); - break; - case 'e': sscanf(optarg, "%hu", &tor_fw_options.public_or_port); - break; - case 'd': sscanf(optarg, "%hu", &tor_fw_options.private_dir_port); - break; - case 'p': sscanf(optarg, "%hu", &tor_fw_options.public_dir_port); - break; - case 'g': tor_fw_options.fetch_public_ip = 1; break; - case 'T': tor_fw_options.test_commandline = 1; break; - case '?': break; - default : fprintf(stderr, "Unknown option!\n"); usage(); exit(1); - } - } - - if (tor_fw_options.verbose) - { - fprintf(stderr, "V: tor-fw-helper version %s\n" - "V: We were called with the following arguments:\n" - "V: verbose = %d, help = %d, pub or port = %u, " - "priv or port = %u\n" - "V: pub dir port = %u, priv dir port = %u\n" - "V: fetch_public_ip = %u\n", - tor_fw_version, tor_fw_options.verbose, tor_fw_options.help, - tor_fw_options.private_or_port, tor_fw_options.public_or_port, - tor_fw_options.private_dir_port, tor_fw_options.public_dir_port, - tor_fw_options.fetch_public_ip); - } - - if (tor_fw_options.test_commandline) { - return log_commandline_options(argc, argv); - } - - /* At the very least, we require an ORPort; - Given a private ORPort, we can ask for a mapping that matches the port - externally. - */ - if (!tor_fw_options.private_or_port && !tor_fw_options.fetch_public_ip) - { - fprintf(stderr, "E: We require an ORPort or fetch_public_ip" - " request!\n"); - usage(); - exit(1); - } else { - /* When we only have one ORPort, internal/external are - set to be the same.*/ - if (!tor_fw_options.public_or_port && tor_fw_options.private_or_port) - { - if (tor_fw_options.verbose) - fprintf(stdout, "V: We're setting public_or_port = " - "private_or_port.\n"); - tor_fw_options.public_or_port = tor_fw_options.private_or_port; - } - } - if (!tor_fw_options.private_dir_port) - { - if (tor_fw_options.verbose) - fprintf(stdout, "V: We have no DirPort; no hole punching for " - "DirPorts\n"); - - } else { - /* When we only have one DirPort, internal/external are - set to be the same.*/ - if (!tor_fw_options.public_dir_port && tor_fw_options.private_dir_port) - { - if (tor_fw_options.verbose) - fprintf(stdout, "V: We're setting public_or_port = " - "private_or_port.\n"); - - tor_fw_options.public_dir_port = tor_fw_options.private_dir_port; - } - } - - if (tor_fw_options.verbose) - { - fprintf(stdout, "V: pub or port = %u, priv or port = %u\n" - "V: pub dir port = %u, priv dir port = %u\n", - tor_fw_options.private_or_port, tor_fw_options.public_or_port, - tor_fw_options.private_dir_port, - tor_fw_options.public_dir_port); - } - - // Initalize the various fw-helper backend helpers - r = init_backends(&tor_fw_options, &backend_state); - if (r) - printf("tor-fw-helper: %i NAT traversal helper(s) loaded\n", r); - - if (tor_fw_options.fetch_public_ip) - { - tor_fw_fetch_public_ip(&tor_fw_options, &backend_state); - } - - if (tor_fw_options.private_or_port) - { - tor_fw_options.internal_port = tor_fw_options.private_or_port; - tor_fw_options.external_port = tor_fw_options.private_or_port; - tor_fw_add_or_port(&tor_fw_options, &backend_state); - } - - if (tor_fw_options.private_dir_port) - { - tor_fw_options.internal_port = tor_fw_options.private_dir_port; - tor_fw_options.external_port = tor_fw_options.private_dir_port; - tor_fw_add_dir_port(&tor_fw_options, &backend_state); - } - - r = (((tor_fw_options.nat_pmp_status | tor_fw_options.upnp_status) + int r = 0; + int c = 0; + + tor_fw_options_t tor_fw_options; + backends_t backend_state; + + memset(&tor_fw_options, 0, sizeof(tor_fw_options)); + + while (1) { + int option_index = 0; + static struct option long_options[] = + { + {"verbose", 0, 0, 'v'}, + {"help", 0, 0, 'h'}, + {"internal-or-port", 1, 0, 'i'}, + {"external-or-port", 1, 0, 'e'}, + {"internal-dir-port", 1, 0, 'd'}, + {"external-dir-port", 1, 0, 'p'}, + {"fetch-public-ip", 0, 0, 'g'}, + {"test-commandline", 0, 0, 'T'}, + {0, 0, 0, 0} + }; + + c = getopt_long(argc, argv, "vhi:e:d:p:gT", + long_options, &option_index); + if (c == -1) + break; + + switch (c) { + case 'v': tor_fw_options.verbose = 1; break; + case 'h': tor_fw_options.help = 1; usage(); exit(1); break; + case 'i': sscanf(optarg, "%hu", &tor_fw_options.private_or_port); + break; + case 'e': sscanf(optarg, "%hu", &tor_fw_options.public_or_port); + break; + case 'd': sscanf(optarg, "%hu", &tor_fw_options.private_dir_port); + break; + case 'p': sscanf(optarg, "%hu", &tor_fw_options.public_dir_port); + break; + case 'g': tor_fw_options.fetch_public_ip = 1; break; + case 'T': tor_fw_options.test_commandline = 1; break; + case '?': break; + default : fprintf(stderr, "Unknown option!\n"); usage(); exit(1); + } + } + + if (tor_fw_options.verbose) { + fprintf(stderr, "V: tor-fw-helper version %s\n" + "V: We were called with the following arguments:\n" + "V: verbose = %d, help = %d, pub or port = %u, " + "priv or port = %u\n" + "V: pub dir port = %u, priv dir port = %u\n" + "V: fetch_public_ip = %u\n", + tor_fw_version, tor_fw_options.verbose, tor_fw_options.help, + tor_fw_options.private_or_port, tor_fw_options.public_or_port, + tor_fw_options.private_dir_port, tor_fw_options.public_dir_port, + tor_fw_options.fetch_public_ip); + } + + if (tor_fw_options.test_commandline) { + return log_commandline_options(argc, argv); + } + + /* At the very least, we require an ORPort; + Given a private ORPort, we can ask for a mapping that matches the port + externally. + */ + if (!tor_fw_options.private_or_port && !tor_fw_options.fetch_public_ip) { + fprintf(stderr, "E: We require an ORPort or fetch_public_ip" + " request!\n"); + usage(); + exit(1); + } else { + /* When we only have one ORPort, internal/external are + set to be the same.*/ + if (!tor_fw_options.public_or_port && tor_fw_options.private_or_port) { + if (tor_fw_options.verbose) + fprintf(stdout, "V: We're setting public_or_port = " + "private_or_port.\n"); + tor_fw_options.public_or_port = tor_fw_options.private_or_port; + } + } + if (!tor_fw_options.private_dir_port) { + if (tor_fw_options.verbose) + fprintf(stdout, "V: We have no DirPort; no hole punching for " + "DirPorts\n"); + + } else { + /* When we only have one DirPort, internal/external are + set to be the same.*/ + if (!tor_fw_options.public_dir_port && tor_fw_options.private_dir_port) { + if (tor_fw_options.verbose) + fprintf(stdout, "V: We're setting public_or_port = " + "private_or_port.\n"); + + tor_fw_options.public_dir_port = tor_fw_options.private_dir_port; + } + } + + if (tor_fw_options.verbose) { + fprintf(stdout, "V: pub or port = %u, priv or port = %u\n" + "V: pub dir port = %u, priv dir port = %u\n", + tor_fw_options.private_or_port, tor_fw_options.public_or_port, + tor_fw_options.private_dir_port, + tor_fw_options.public_dir_port); + } + + // Initalize the various fw-helper backend helpers + r = init_backends(&tor_fw_options, &backend_state); + if (r) + printf("tor-fw-helper: %i NAT traversal helper(s) loaded\n", r); + + if (tor_fw_options.fetch_public_ip) { + tor_fw_fetch_public_ip(&tor_fw_options, &backend_state); + } + + if (tor_fw_options.private_or_port) { + tor_fw_options.internal_port = tor_fw_options.private_or_port; + tor_fw_options.external_port = tor_fw_options.private_or_port; + tor_fw_add_or_port(&tor_fw_options, &backend_state); + } + + if (tor_fw_options.private_dir_port) { + tor_fw_options.internal_port = tor_fw_options.private_dir_port; + tor_fw_options.external_port = tor_fw_options.private_dir_port; + tor_fw_add_dir_port(&tor_fw_options, &backend_state); + } + + r = (((tor_fw_options.nat_pmp_status | tor_fw_options.upnp_status) |tor_fw_options.public_ip_status)); - if (r > 0) - { - fprintf(stdout, "tor-fw-helper: SUCCESS\n"); - } else { - fprintf(stderr, "tor-fw-helper: FAILURE\n"); - } - - exit(r); + if (r > 0) { + fprintf(stdout, "tor-fw-helper: SUCCESS\n"); + } else { + fprintf(stderr, "tor-fw-helper: FAILURE\n"); + } + + exit(r); } diff --git a/src/tools/tor-fw-helper/tor-fw-helper.h b/src/tools/tor-fw-helper/tor-fw-helper.h index 1af9c0c7aa..b83d4b7d8f 100644 --- a/src/tools/tor-fw-helper/tor-fw-helper.h +++ b/src/tools/tor-fw-helper/tor-fw-helper.h @@ -26,31 +26,31 @@ /** This is where we store parsed commandline options. */ typedef struct { - int verbose; - int help; - int test_commandline; - uint16_t private_dir_port; - uint16_t private_or_port; - uint16_t public_dir_port; - uint16_t public_or_port; - uint16_t internal_port; - uint16_t external_port; - int fetch_public_ip; - int nat_pmp_status; - int upnp_status; - int public_ip_status; + int verbose; + int help; + int test_commandline; + uint16_t private_dir_port; + uint16_t private_or_port; + uint16_t public_dir_port; + uint16_t public_or_port; + uint16_t internal_port; + uint16_t external_port; + int fetch_public_ip; + int nat_pmp_status; + int upnp_status; + int public_ip_status; } tor_fw_options_t; /** This is our main structure that defines our backend helper API; each helper * must conform to these public methods if it expects to be handled in a * non-special way. */ typedef struct tor_fw_backend_t { - const char *name; - size_t state_len; - 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); + const char *name; + size_t state_len; + 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); } tor_fw_backend_t; #endif |