aboutsummaryrefslogtreecommitdiff
path: root/src/or
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2007-12-17 22:44:18 +0000
committerNick Mathewson <nickm@torproject.org>2007-12-17 22:44:18 +0000
commitb8ac050e85209a5ac56eed63954dda63874364b1 (patch)
treee1d0fa50f4121e4f548160a4fb86790b48391521 /src/or
parent25f78498f91ba9f482bc654d3af8905b600d4f7f (diff)
downloadtor-b8ac050e85209a5ac56eed63954dda63874364b1.tar.gz
tor-b8ac050e85209a5ac56eed63954dda63874364b1.zip
r15532@tombo: nickm | 2007-12-17 17:41:05 -0500
clean up whitesapce and debug a little on geoip stuff. svn:r12847
Diffstat (limited to 'src/or')
-rw-r--r--src/or/control.c2
-rw-r--r--src/or/geoip.c29
-rw-r--r--src/or/or.h2
-rw-r--r--src/or/router.c1
4 files changed, 30 insertions, 4 deletions
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);