From af175fa7e4ee3ffd1d91d463ba77a3c10d95c654 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Sat, 20 Oct 2012 19:35:00 +0200 Subject: [PATCH] Duplicate less code. --- src/or/config.c | 76 ++++++++++++++++++++++++------------------------- src/or/geoip.c | 4 +-- src/or/geoip.h | 3 +- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/or/config.c b/src/or/config.c index 5b1f1eed6f..b83b6f7222 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -482,6 +482,8 @@ static void init_libevent(const or_options_t *options); static int opt_streq(const char *s1, const char *s2); static int parse_outbound_addresses(or_options_t *options, int validate_only, char **msg); +static void config_maybe_load_geoip_files_(const or_options_t *options, + const or_options_t *old_options); /** Magic value for or_options_t. */ #define OR_OPTIONS_MAGIC 9090909 @@ -1512,44 +1514,7 @@ options_act(const or_options_t *old_options) connection_or_update_token_buckets(get_connection_array(), options); } - /* Maybe load IPv4 geoip file */ - if (options->GeoIPFile && - ((!old_options || !opt_streq(old_options->GeoIPFile, options->GeoIPFile)) - || !geoip_is_loaded(AF_INET))) { - /* XXXX Don't use this "" junk; make our filename options - * understand prefixes somehow. -NM */ - /* XXXX024 Reload GeoIPFile on SIGHUP. -NM */ - char *actual_fname = tor_strdup(options->GeoIPFile); -#ifdef _WIN32 - if (!strcmp(actual_fname, "")) { - const char *conf_root = get_windows_conf_root(); - tor_free(actual_fname); - tor_asprintf(&actual_fname, "%s\\geoip", conf_root); - } -#endif - geoip_load_file(AF_INET, actual_fname, options); - tor_free(actual_fname); - } - /* And maybe load IPv6 geoip file */ - if (options->GeoIPv6File && - ((!old_options || !opt_streq(old_options->GeoIPv6File, - options->GeoIPv6File)) - || !geoip_is_loaded(AF_INET6))) { - /* XXXX Don't use this "" junk; make our filename options - * understand prefixes somehow. See also comment for GeoIPFile. */ - /* XXXX024 Reload GeoIPV6File on SIGHUP. See also comment for - * GeoIPFile. */ - char *actual_fname = tor_strdup(options->GeoIPv6File); -#ifdef _WIN32 - if (!strcmp(actual_fname, "")) { - const char *conf_root = get_windows_conf_root(); - tor_free(actual_fname); - tor_asprintf(&actual_fname, "%s\\geoip6", conf_root); - } -#endif - geoip_load_file(AF_INET6, actual_fname, options); - tor_free(actual_fname); - } + config_maybe_load_geoip_files_(options, old_options); if (options->CellStatistics || options->DirReqStatistics || options->EntryStatistics || options->ExitPortStatistics || @@ -5599,3 +5564,38 @@ parse_outbound_addresses(or_options_t *options, int validate_only, char **msg) return 0; } +static void +config_load_geoip_file_(sa_family_t family, + char *fname, /* will be freed */ + const char *default_fname) +{ + (void)default_fname; + /* XXXX Don't use this "" junk; make our filename options + * understand prefixes somehow. -NM */ + /* XXXX024 Reload GeoIPFile on SIGHUP. -NM */ +#ifdef _WIN32 + if (!strcmp(fname, "")) { + const char *conf_root = get_windows_conf_root(); + tor_free(fname); + tor_asprintf(&fname, "%s\\%s", conf_root, default_fname); + } +#endif + geoip_load_file(family, fname); + tor_free(fname); +} + +static void +config_maybe_load_geoip_files_(const or_options_t *options, + const or_options_t *old_options) +{ + if (options->GeoIPFile && + ((!old_options || !opt_streq(old_options->GeoIPFile, + options->GeoIPFile)) + || !geoip_is_loaded(AF_INET))) + config_load_geoip_file_(AF_INET, tor_strdup(options->GeoIPFile), "geoip"); + if (options->GeoIPv6File && + ((!old_options || !opt_streq(old_options->GeoIPv6File, + options->GeoIPv6File)) + || !geoip_is_loaded(AF_INET6))) + config_load_geoip_file_(AF_INET6, tor_strdup(options->GeoIPv6File), "geoip6"); +} diff --git a/src/or/geoip.c b/src/or/geoip.c index e1c0009cc3..5459b5504b 100644 --- a/src/or/geoip.c +++ b/src/or/geoip.c @@ -326,11 +326,11 @@ init_geoip_countries(void) * with '#' (comments). */ int -geoip_load_file(sa_family_t family, const char *filename, - const or_options_t *options) +geoip_load_file(sa_family_t family, const char *filename) { FILE *f; const char *msg = ""; + const or_options_t *options = get_options(); int severity = options_need_geoip_info(options, &msg) ? LOG_WARN : LOG_INFO; crypto_digest_t *geoip_digest_env = NULL; diff --git a/src/or/geoip.h b/src/or/geoip.h index 1d0012f857..3c91617c6e 100644 --- a/src/or/geoip.h +++ b/src/or/geoip.h @@ -19,8 +19,7 @@ int geoip_get_country_by_ipv4(uint32_t ipaddr); int geoip_get_country_by_ipv6(const struct in6_addr *addr); #endif int should_record_bridge_info(const or_options_t *options); -int geoip_load_file(sa_family_t family, const char *filename, - const or_options_t *options); +int geoip_load_file(sa_family_t family, const char *filename); int geoip_get_country_by_addr(const tor_addr_t *addr); int geoip_get_n_countries(void); const char *geoip_get_country_name(country_t num);