commit e165097d7e719d794f174bc387c50bafa7f575c3 Author: Alexander Færøy ahf@0x90.dk Date: Mon Oct 12 22:51:46 2015 +0200
Expose set_tor_{address,port} as conf_file_set_tor_{address,port}.
This patch exposes set_tor_address and set_tor_port as part of the configuration API as conf_file_set_tor_address and conf_file_set_tor_port.
We also ensure to free() the Tor address if it's set multiple times during execution.
Signed-off-by: David Goulet dgoulet@ev0ke.net --- src/common/config-file.c | 154 +++++++++++++++++++++++++---------------------- src/common/config-file.h | 2 + src/lib/torsocks.c | 4 +- 3 files changed, 85 insertions(+), 75 deletions(-)
diff --git a/src/common/config-file.c b/src/common/config-file.c index 750bfa8..05bd480 100644 --- a/src/common/config-file.c +++ b/src/common/config-file.c @@ -115,75 +115,6 @@ error: }
/* - * Set the given string port in a configuration object. - * - * Return 0 on success or else a negative EINVAL if the port is equal to 0 or - * over 65535. - */ -static int set_tor_port(const char *port, struct configuration *config) -{ - int ret = 0; - char *endptr; - unsigned long _port; - - assert(port); - assert(config); - - /* Let's avoid a integer overflow here ;). */ - _port = strtoul(port, &endptr, 10); - if (_port == 0 || _port > 65535) { - ret = -EINVAL; - ERR("Config file invalid port: %s", port); - goto error; - } - - config->conf_file.tor_port = (in_port_t) _port; - - DBG("Config file setting tor port to %lu", _port); - -error: - return ret; -} - -/* - * Set the given string address in a configuration object. - * - * Return 0 on success or else a negative value. On error, the address was not - * recognized. - */ -static int set_tor_address(const char *addr, struct configuration *config) -{ - int ret; - - assert(addr); - assert(config); - - ret = utils_is_address_ipv4(addr); - if (ret == 1 ) { - config->conf_file.tor_domain = CONNECTION_DOMAIN_INET; - } else { - ret = utils_is_address_ipv6(addr); - if (ret != 1) { - /* At this point, the addr is either v4 nor v6 so error. */ - ERR("Config file unknown tor address: %s", addr); - goto error; - } - config->conf_file.tor_domain = CONNECTION_DOMAIN_INET6; - } - config->conf_file.tor_address = strdup(addr); - if (!config->conf_file.tor_address) { - ret = -ENOMEM; - goto error; - } - - DBG("Config file setting tor address to %s", addr); - ret = 0; - -error: - return ret; -} - -/* * Parse a single line of from a configuration file and set the value found in * the configuration object. * @@ -209,12 +140,12 @@ static int parse_config_line(const char *line, struct configuration *config) }
if (!strcmp(tokens[0], conf_toraddr_str)) { - ret = set_tor_address(tokens[1], config); + ret = conf_file_set_tor_address(tokens[1], config); if (ret < 0) { goto error; } } else if (!strcmp(tokens[0], conf_torport_str)) { - ret = set_tor_port(tokens[1], config); + ret = conf_file_set_tor_port(tokens[1], config); if (ret < 0) { goto error; } @@ -295,6 +226,83 @@ error: }
/* + * Set the given string port in a configuration object. + * + * Return 0 on success or else a negative EINVAL if the port is equal to 0 or + * over 65535. + */ +ATTR_HIDDEN +int conf_file_set_tor_port(const char *port, struct configuration *config) +{ + int ret = 0; + char *endptr; + unsigned long _port; + + assert(port); + assert(config); + + /* Let's avoid a integer overflow here ;). */ + _port = strtoul(port, &endptr, 10); + if (_port == 0 || _port > 65535) { + ret = -EINVAL; + ERR("Config file invalid port: %s", port); + goto error; + } + + config->conf_file.tor_port = (in_port_t) _port; + + DBG("Config file setting tor port to %lu", _port); + +error: + return ret; +} + +/* + * Set the given string address in a configuration object. + * + * Return 0 on success or else a negative value. On error, the address was not + * recognized. + */ +ATTR_HIDDEN +int conf_file_set_tor_address(const char *addr, struct configuration *config) +{ + int ret; + + assert(addr); + assert(config); + + ret = utils_is_address_ipv4(addr); + if (ret == 1 ) { + config->conf_file.tor_domain = CONNECTION_DOMAIN_INET; + } else { + ret = utils_is_address_ipv6(addr); + if (ret != 1) { + /* At this point, the addr is either v4 nor v6 so error. */ + ERR("Config file unknown tor address: %s", addr); + goto error; + } + config->conf_file.tor_domain = CONNECTION_DOMAIN_INET6; + } + + if (config->conf_file.tor_address == NULL) { + free(config->conf_file.tor_address); + config->conf_file.tor_address = NULL; + } + + config->conf_file.tor_address = strdup(addr); + if (!config->conf_file.tor_address) { + ret = -ENOMEM; + goto error; + } + + DBG("Config file setting tor address to %s", addr); + ret = 0; + +error: + return ret; +} + +/* * Set the SOCKS5 username to the given configuration. * * Return 0 on success else a negative value. @@ -538,12 +546,12 @@ int config_file_read(const char *filename, struct configuration *config) fp = fopen(filename, "r"); if (!fp) { WARN("Config file not found: %s. Using default for Tor", filename); - (void) set_tor_address(DEFAULT_TOR_ADDRESS, config); + (void) conf_file_set_tor_address(DEFAULT_TOR_ADDRESS, config); /* * We stringify the default value here so we can print the debug * statement in the function call to set port. */ - (void) set_tor_port(XSTR(DEFAULT_TOR_PORT), config); + (void) conf_file_set_tor_port(XSTR(DEFAULT_TOR_PORT), config);
ret = set_onion_info( DEFAULT_ONION_ADDR_RANGE "/" DEFAULT_ONION_ADDR_MASK, config); diff --git a/src/common/config-file.h b/src/common/config-file.h index 48be392..154c7be 100644 --- a/src/common/config-file.h +++ b/src/common/config-file.h @@ -94,6 +94,8 @@ struct configuration {
int config_file_read(const char *filename, struct configuration *config); void config_file_destroy(struct config_file *conf); +int conf_file_set_tor_address(const char *addr, struct configuration *config); +int conf_file_set_tor_port(const char *port, struct configuration *config); int conf_file_set_socks5_pass(const char *password, struct configuration *config); int conf_file_set_socks5_user(const char *username, diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c index 305ad22..60d586d 100644 --- a/src/lib/torsocks.c +++ b/src/lib/torsocks.c @@ -158,8 +158,8 @@ static void init_config(void) * are missing. */ if (!tsocks_config.conf_file.tor_address) { - tsocks_config.conf_file.tor_address = strdup(DEFAULT_TOR_ADDRESS); - if (!tsocks_config.conf_file.tor_address) { + ret = conf_file_set_tor_address(DEFAULT_TOR_ADDRESS, &tsocks_config); + if (ret < 0) { /* Most likely ENOMEM thus we can't continue. */ clean_exit(EXIT_FAILURE); }
tor-commits@lists.torproject.org