summaryrefslogtreecommitdiff
path: root/src/or/geoip.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-09-25 20:21:35 +0000
committerNick Mathewson <nickm@torproject.org>2008-09-25 20:21:35 +0000
commit8bbbbaf87b5ab3c0a5918d2bc8d1d7f22cf54f53 (patch)
tree9c9019d25cbba426218b1fd95560226e4e127cf1 /src/or/geoip.c
parentb2c7090da6e0d01322d6f1590c46ca03e47f6e26 (diff)
downloadtor-8bbbbaf87b5ab3c0a5918d2bc8d1d7f22cf54f53.tar.gz
tor-8bbbbaf87b5ab3c0a5918d2bc8d1d7f22cf54f53.zip
Add country-code support to configured node lists to implement the ever-popular "no exits in Monaco" feature (ExcludeExitNodes {MC}). Also allow country codes and IP ranges in ExitNodes. (EntryNodes needs more work.) Based on code by Robert Hogan. Needs more testing.
svn:r16966
Diffstat (limited to 'src/or/geoip.c')
-rw-r--r--src/or/geoip.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/or/geoip.c b/src/or/geoip.c
index e15f0ba523..e948d60de4 100644
--- a/src/or/geoip.c
+++ b/src/or/geoip.c
@@ -42,6 +42,23 @@ static strmap_t *country_idxplus1_by_lc_code = NULL;
/** A list of all known geoip_entry_t, sorted by ip_low. */
static smartlist_t *geoip_entries = NULL;
+/** Return the index of the <b>country</b>'s entry in the GeoIP DB
+ * if it is a valid 2-letter country code, otherwise return zero.
+ */
+country_t
+geoip_get_country(const char *country)
+{
+ void *_idxplus1;
+ intptr_t idx;
+
+ _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
+ if (!_idxplus1)
+ return -1;
+
+ idx = ((uintptr_t)_idxplus1)-1;
+ return (country_t)idx;
+}
+
/** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and
* <b>high</b>, inclusive, to the 2-letter country code <b>country</b>.
*/
@@ -167,9 +184,15 @@ geoip_load_file(const char *filename, or_options_t *options)
log_fn(severity, LD_GENERAL, "Failed to open GEOIP file %s.", filename);
return -1;
}
- geoip_countries = smartlist_create();
+ if (!geoip_countries) {
+ geoip_countries = smartlist_create();
+ country_idxplus1_by_lc_code = strmap_new();
+ }
+ if (geoip_entries) {
+ SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, e, tor_free(e));
+ smartlist_free(geoip_entries);
+ }
geoip_entries = smartlist_create();
- country_idxplus1_by_lc_code = strmap_new();
log_info(LD_GENERAL, "Parsing GEOIP file.");
while (!feof(f)) {
char buf[512];
@@ -210,7 +233,7 @@ geoip_get_n_countries(void)
/** Return the two-letter country code associated with the number <b>num</b>,
* or "??" for an unknown value. */
const char *
-geoip_get_country_name(int num)
+geoip_get_country_name(country_t num)
{
if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) {
geoip_country_t *c = smartlist_get(geoip_countries, num);