diff options
-rw-r--r-- | doc/TODO | 3 | ||||
-rw-r--r-- | src/or/control.c | 2 | ||||
-rw-r--r-- | src/or/geoip.c | 29 | ||||
-rw-r--r-- | src/or/or.h | 2 | ||||
-rw-r--r-- | src/or/router.c | 1 |
5 files changed, 32 insertions, 5 deletions
@@ -38,11 +38,12 @@ R . spec o Code to store a GEOIP file in memory. o Code to remember client IPs. o Code to generate history lines - - Controller interface + o Controller interface - Track consecutive time up, not time since last-forgotten IP. - Add log lines. - Tests - Mention in dir-spec.txt + - Mention in control-spec.txt d let Vidalia use the geoip data too rather than doing its own anonymized queries o bridge address disbursal strategies diff --git a/src/or/control.c b/src/or/control.c index 680973b999..e404a4beb7 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -1783,7 +1783,7 @@ static const getinfo_item_t getinfo_items[] = { PREFIX("dir/status/", dir,"Networkstatus docs as retrieved from a DirPort."), PREFIX("exit-policy/default", policies, "The default value appended to the configured exit policy."), - + PREFIX("ip-to-country/", geoip, "Perform a GEOIP lookup"), { NULL, NULL, NULL, 0 } }; diff --git a/src/or/geoip.c b/src/or/geoip.c index c9fb91cdfb..e21f02a41b 100644 --- a/src/or/geoip.c +++ b/src/or/geoip.c @@ -31,11 +31,12 @@ geoip_add_entry(uint32_t low, uint32_t high, const char *country) char *c = tor_strdup(country); tor_strlower(c); smartlist_add(geoip_countries, c); - idx = smartlist_len(geoip_countries) + 1; + idx = smartlist_len(geoip_countries) - 1; strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1)); } else { idx = ((uintptr_t)_idxplus1)-1; } + tor_assert(!strcasecmp(smartlist_get(geoip_countries, idx), country)); ent = tor_malloc_zero(sizeof(geoip_entry_t)); ent->ip_low = low; ent->ip_high = high; @@ -80,19 +81,24 @@ geoip_load_file(const char *filename) geoip_countries = smartlist_create(); geoip_entries = smartlist_create(); country_idxplus1_by_lc_code = strmap_new(); + log_info(LD_GENERAL, "Parsing GEOIP file."); while (!feof(f)) { char buf[512]; unsigned int low, high; char b[3]; if (fgets(buf, sizeof(buf), f) == NULL) break; + /* XXXX020 track full country name. */ if (sscanf(buf,"%u,%u,%2s", &low, &high, b) == 3) { geoip_add_entry(low, high, b); } else if (sscanf(buf,"\"%u\",\"%u\",\"%2s\",", &low, &high, b) == 3) { geoip_add_entry(low, high, b); + } else { + log_warn(LD_GENERAL, "Unable to parse line from GEOIP file: %s", + escaped(buf)); } } - /*XXXX020 abort and return -1 if */ + /*XXXX020 abort and return -1 if no entries/illformed?*/ fclose(f); smartlist_sort(geoip_entries, _geoip_compare_entries); @@ -257,6 +263,24 @@ geoip_get_client_history(time_t now) return result; } +int +getinfo_helper_geoip(control_connection_t *control_conn, + const char *question, char **answer) +{ + (void)control_conn; + if (geoip_is_loaded() && !strcmpstart(question, "ip-to-country/")) { + int c; + uint32_t ip; + struct in_addr in; + question += strlen("ip-to-country/"); + if (tor_inet_aton(question, &in) != 0) { + ip = ntohl(in.s_addr); + c = geoip_get_country_by_ip(ip); + *answer = tor_strdup(geoip_get_country_name(c)); + } + } + return 0; +} void geoip_free_all(void) @@ -284,3 +308,4 @@ geoip_free_all(void) country_idxplus1_by_lc_code = NULL; geoip_entries = NULL; } + diff --git a/src/or/or.h b/src/or/or.h index 758e9faf0c..fd7e949087 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -3210,6 +3210,8 @@ void geoip_note_client_seen(uint32_t addr, time_t now); void geoip_remove_old_clients(time_t cutoff); time_t geoip_get_history_start(void); char *geoip_get_client_history(time_t now); +int getinfo_helper_geoip(control_connection_t *control_conn, + const char *question, char **answer); void geoip_free_all(void); /********************************* hibernate.c **********************/ diff --git a/src/or/router.c b/src/or/router.c index 30b307dc40..e7af2d9da2 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -1731,7 +1731,6 @@ extrainfo_dump_to_string(char *s, size_t maxlen, extrainfo_t *extrainfo, } } - len = strlen(s); strlcat(s+len, "router-signature\n", maxlen-len); len += strlen(s+len); |