geoip.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* Copyright (c) 2007-2009, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file geoip.c
  5. * \brief Functions related to maintaining an IP-to-country database and to
  6. * summarizing client connections by country.
  7. */
  8. #define GEOIP_PRIVATE
  9. #include "or.h"
  10. #include "ht.h"
  11. static void clear_geoip_db(void);
  12. /** An entry from the GeoIP file: maps an IP range to a country. */
  13. typedef struct geoip_entry_t {
  14. uint32_t ip_low; /**< The lowest IP in the range, in host order */
  15. uint32_t ip_high; /**< The highest IP in the range, in host order */
  16. intptr_t country; /**< An index into geoip_countries */
  17. } geoip_entry_t;
  18. /** For how many periods should we remember per-country request history? */
  19. #define REQUEST_HIST_LEN 3
  20. /** How long are the periods for which we should remember request history? */
  21. #define REQUEST_HIST_PERIOD (8*60*60)
  22. /** A per-country record for GeoIP request history. */
  23. typedef struct geoip_country_t {
  24. char countrycode[3];
  25. uint32_t n_v2_ns_requests[REQUEST_HIST_LEN];
  26. uint32_t n_v3_ns_requests[REQUEST_HIST_LEN];
  27. } geoip_country_t;
  28. /** A list of geoip_country_t */
  29. static smartlist_t *geoip_countries = NULL;
  30. /** A map from lowercased country codes to their position in geoip_countries.
  31. * The index is encoded in the pointer, and 1 is added so that NULL can mean
  32. * not found. */
  33. static strmap_t *country_idxplus1_by_lc_code = NULL;
  34. /** A list of all known geoip_entry_t, sorted by ip_low. */
  35. static smartlist_t *geoip_entries = NULL;
  36. /** Return the index of the <b>country</b>'s entry in the GeoIP DB
  37. * if it is a valid 2-letter country code, otherwise return zero.
  38. */
  39. country_t
  40. geoip_get_country(const char *country)
  41. {
  42. void *_idxplus1;
  43. intptr_t idx;
  44. _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
  45. if (!_idxplus1)
  46. return -1;
  47. idx = ((uintptr_t)_idxplus1)-1;
  48. return (country_t)idx;
  49. }
  50. /** Add an entry to the GeoIP table, mapping all IPs between <b>low</b> and
  51. * <b>high</b>, inclusive, to the 2-letter country code <b>country</b>.
  52. */
  53. static void
  54. geoip_add_entry(uint32_t low, uint32_t high, const char *country)
  55. {
  56. intptr_t idx;
  57. geoip_entry_t *ent;
  58. void *_idxplus1;
  59. if (high < low)
  60. return;
  61. _idxplus1 = strmap_get_lc(country_idxplus1_by_lc_code, country);
  62. if (!_idxplus1) {
  63. geoip_country_t *c = tor_malloc_zero(sizeof(geoip_country_t));
  64. strlcpy(c->countrycode, country, sizeof(c->countrycode));
  65. tor_strlower(c->countrycode);
  66. smartlist_add(geoip_countries, c);
  67. idx = smartlist_len(geoip_countries) - 1;
  68. strmap_set_lc(country_idxplus1_by_lc_code, country, (void*)(idx+1));
  69. } else {
  70. idx = ((uintptr_t)_idxplus1)-1;
  71. }
  72. {
  73. geoip_country_t *c = smartlist_get(geoip_countries, idx);
  74. tor_assert(!strcasecmp(c->countrycode, country));
  75. }
  76. ent = tor_malloc_zero(sizeof(geoip_entry_t));
  77. ent->ip_low = low;
  78. ent->ip_high = high;
  79. ent->country = idx;
  80. smartlist_add(geoip_entries, ent);
  81. }
  82. /** Add an entry to the GeoIP table, parsing it from <b>line</b>. The
  83. * format is as for geoip_load_file(). */
  84. /*private*/ int
  85. geoip_parse_entry(const char *line)
  86. {
  87. unsigned int low, high;
  88. char b[3];
  89. if (!geoip_countries) {
  90. geoip_countries = smartlist_create();
  91. geoip_entries = smartlist_create();
  92. country_idxplus1_by_lc_code = strmap_new();
  93. }
  94. while (TOR_ISSPACE(*line))
  95. ++line;
  96. if (*line == '#')
  97. return 0;
  98. if (sscanf(line,"%u,%u,%2s", &low, &high, b) == 3) {
  99. geoip_add_entry(low, high, b);
  100. return 0;
  101. } else if (sscanf(line,"\"%u\",\"%u\",\"%2s\",", &low, &high, b) == 3) {
  102. geoip_add_entry(low, high, b);
  103. return 0;
  104. } else {
  105. log_warn(LD_GENERAL, "Unable to parse line from GEOIP file: %s",
  106. escaped(line));
  107. return -1;
  108. }
  109. }
  110. /** Sorting helper: return -1, 1, or 0 based on comparison of two
  111. * geoip_entry_t */
  112. static int
  113. _geoip_compare_entries(const void **_a, const void **_b)
  114. {
  115. const geoip_entry_t *a = *_a, *b = *_b;
  116. if (a->ip_low < b->ip_low)
  117. return -1;
  118. else if (a->ip_low > b->ip_low)
  119. return 1;
  120. else
  121. return 0;
  122. }
  123. /** bsearch helper: return -1, 1, or 0 based on comparison of an IP (a pointer
  124. * to a uint32_t in host order) to a geoip_entry_t */
  125. static int
  126. _geoip_compare_key_to_entry(const void *_key, const void **_member)
  127. {
  128. const uint32_t addr = *(uint32_t *)_key;
  129. const geoip_entry_t *entry = *_member;
  130. if (addr < entry->ip_low)
  131. return -1;
  132. else if (addr > entry->ip_high)
  133. return 1;
  134. else
  135. return 0;
  136. }
  137. /** Return 1 if we should collect geoip stats on bridge users, and
  138. * include them in our extrainfo descriptor. Else return 0. */
  139. int
  140. should_record_bridge_info(or_options_t *options)
  141. {
  142. return options->BridgeRelay && options->BridgeRecordUsageByCountry;
  143. }
  144. /** Clear the GeoIP database and reload it from the file
  145. * <b>filename</b>. Return 0 on success, -1 on failure.
  146. *
  147. * Recognized line formats are:
  148. * INTIPLOW,INTIPHIGH,CC
  149. * and
  150. * "INTIPLOW","INTIPHIGH","CC","CC3","COUNTRY NAME"
  151. * where INTIPLOW and INTIPHIGH are IPv4 addresses encoded as 4-byte unsigned
  152. * integers, and CC is a country code.
  153. *
  154. * It also recognizes, and skips over, blank lines and lines that start
  155. * with '#' (comments).
  156. */
  157. int
  158. geoip_load_file(const char *filename, or_options_t *options)
  159. {
  160. FILE *f;
  161. const char *msg = "";
  162. int severity = options_need_geoip_info(options, &msg) ? LOG_WARN : LOG_INFO;
  163. clear_geoip_db();
  164. if (!(f = fopen(filename, "r"))) {
  165. log_fn(severity, LD_GENERAL, "Failed to open GEOIP file %s. %s",
  166. filename, msg);
  167. return -1;
  168. }
  169. if (!geoip_countries) {
  170. geoip_countries = smartlist_create();
  171. country_idxplus1_by_lc_code = strmap_new();
  172. }
  173. if (geoip_entries) {
  174. SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, e, tor_free(e));
  175. smartlist_free(geoip_entries);
  176. }
  177. geoip_entries = smartlist_create();
  178. log_notice(LD_GENERAL, "Parsing GEOIP file.");
  179. while (!feof(f)) {
  180. char buf[512];
  181. if (fgets(buf, (int)sizeof(buf), f) == NULL)
  182. break;
  183. /* FFFF track full country name. */
  184. geoip_parse_entry(buf);
  185. }
  186. /*XXXX abort and return -1 if no entries/illformed?*/
  187. fclose(f);
  188. smartlist_sort(geoip_entries, _geoip_compare_entries);
  189. /* Okay, now we need to maybe change our mind about what is in which
  190. * country. */
  191. refresh_all_country_info();
  192. return 0;
  193. }
  194. /** Given an IP address in host order, return a number representing the
  195. * country to which that address belongs, or -1 for unknown. The return value
  196. * will always be less than geoip_get_n_countries(). To decode it,
  197. * call geoip_get_country_name().
  198. */
  199. int
  200. geoip_get_country_by_ip(uint32_t ipaddr)
  201. {
  202. geoip_entry_t *ent;
  203. if (!geoip_entries)
  204. return -1;
  205. ent = smartlist_bsearch(geoip_entries, &ipaddr, _geoip_compare_key_to_entry);
  206. return ent ? (int)ent->country : -1;
  207. }
  208. /** Return the number of countries recognized by the GeoIP database. */
  209. int
  210. geoip_get_n_countries(void)
  211. {
  212. return (int) smartlist_len(geoip_countries);
  213. }
  214. /** Return the two-letter country code associated with the number <b>num</b>,
  215. * or "??" for an unknown value. */
  216. const char *
  217. geoip_get_country_name(country_t num)
  218. {
  219. if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) {
  220. geoip_country_t *c = smartlist_get(geoip_countries, num);
  221. return c->countrycode;
  222. } else
  223. return "??";
  224. }
  225. /** Return true iff we have loaded a GeoIP database.*/
  226. int
  227. geoip_is_loaded(void)
  228. {
  229. return geoip_countries != NULL && geoip_entries != NULL;
  230. }
  231. /** Entry in a map from IP address to the last time we've seen an incoming
  232. * connection from that IP address. Used by bridges only, to track which
  233. * countries have them blocked. */
  234. typedef struct clientmap_entry_t {
  235. HT_ENTRY(clientmap_entry_t) node;
  236. uint32_t ipaddr;
  237. time_t last_seen; /* The last 2 bits of this value hold the client
  238. * operation. */
  239. } clientmap_entry_t;
  240. #define ACTION_MASK 3
  241. /** Map from client IP address to last time seen. */
  242. static HT_HEAD(clientmap, clientmap_entry_t) client_history =
  243. HT_INITIALIZER();
  244. /** Time at which we started tracking client IP history. */
  245. static time_t client_history_starts = 0;
  246. /** When did the current period of checking per-country request history
  247. * start? */
  248. static time_t current_request_period_starts = 0;
  249. /** How many older request periods are we remembering? */
  250. static int n_old_request_periods = 0;
  251. /** Hashtable helper: compute a hash of a clientmap_entry_t. */
  252. static INLINE unsigned
  253. clientmap_entry_hash(const clientmap_entry_t *a)
  254. {
  255. return ht_improve_hash((unsigned) a->ipaddr);
  256. }
  257. /** Hashtable helper: compare two clientmap_entry_t values for equality. */
  258. static INLINE int
  259. clientmap_entries_eq(const clientmap_entry_t *a, const clientmap_entry_t *b)
  260. {
  261. return a->ipaddr == b->ipaddr;
  262. }
  263. HT_PROTOTYPE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
  264. clientmap_entries_eq);
  265. HT_GENERATE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
  266. clientmap_entries_eq, 0.6, malloc, realloc, free);
  267. /** Note that we've seen a client connect from the IP <b>addr</b> (host order)
  268. * at time <b>now</b>. Ignored by all but bridges. */
  269. void
  270. geoip_note_client_seen(geoip_client_action_t action,
  271. uint32_t addr, time_t now)
  272. {
  273. or_options_t *options = get_options();
  274. clientmap_entry_t lookup, *ent;
  275. if (action == GEOIP_CLIENT_CONNECT) {
  276. if (!(options->BridgeRelay && options->BridgeRecordUsageByCountry))
  277. return;
  278. } else {
  279. #ifndef ENABLE_GEOIP_STATS
  280. return;
  281. #else
  282. if (options->BridgeRelay || options->BridgeAuthoritativeDir ||
  283. !options->DirRecordUsageByCountry)
  284. return;
  285. #endif
  286. }
  287. /* Rotate the current request period. */
  288. while (current_request_period_starts + REQUEST_HIST_PERIOD < now) {
  289. if (!geoip_countries)
  290. geoip_countries = smartlist_create();
  291. if (!current_request_period_starts) {
  292. current_request_period_starts = now;
  293. break;
  294. }
  295. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
  296. memmove(&c->n_v2_ns_requests[0], &c->n_v2_ns_requests[1],
  297. sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
  298. memmove(&c->n_v3_ns_requests[0], &c->n_v3_ns_requests[1],
  299. sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
  300. c->n_v2_ns_requests[REQUEST_HIST_LEN-1] = 0;
  301. c->n_v3_ns_requests[REQUEST_HIST_LEN-1] = 0;
  302. });
  303. current_request_period_starts += REQUEST_HIST_PERIOD;
  304. if (n_old_request_periods < REQUEST_HIST_LEN-1)
  305. ++n_old_request_periods;
  306. }
  307. /* We use the low 3 bits of the time to encode the action. Since we're
  308. * potentially remembering tons of clients, we don't want to make
  309. * clientmap_entry_t larger than it has to be. */
  310. now = (now & ~ACTION_MASK) | (((int)action) & ACTION_MASK);
  311. lookup.ipaddr = addr;
  312. ent = HT_FIND(clientmap, &client_history, &lookup);
  313. if (ent) {
  314. ent->last_seen = now;
  315. } else {
  316. ent = tor_malloc_zero(sizeof(clientmap_entry_t));
  317. ent->ipaddr = addr;
  318. ent->last_seen = now;
  319. HT_INSERT(clientmap, &client_history, ent);
  320. }
  321. if (action == GEOIP_CLIENT_NETWORKSTATUS ||
  322. action == GEOIP_CLIENT_NETWORKSTATUS_V2) {
  323. int country_idx = geoip_get_country_by_ip(addr);
  324. if (country_idx >= 0 && country_idx < smartlist_len(geoip_countries)) {
  325. geoip_country_t *country = smartlist_get(geoip_countries, country_idx);
  326. if (action == GEOIP_CLIENT_NETWORKSTATUS)
  327. ++country->n_v3_ns_requests[REQUEST_HIST_LEN-1];
  328. else
  329. ++country->n_v2_ns_requests[REQUEST_HIST_LEN-1];
  330. }
  331. }
  332. if (!client_history_starts) {
  333. client_history_starts = now;
  334. current_request_period_starts = now;
  335. }
  336. }
  337. /** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
  338. * older than a certain time. */
  339. static int
  340. _remove_old_client_helper(struct clientmap_entry_t *ent, void *_cutoff)
  341. {
  342. time_t cutoff = *(time_t*)_cutoff;
  343. if (ent->last_seen < cutoff) {
  344. tor_free(ent);
  345. return 1;
  346. } else {
  347. return 0;
  348. }
  349. }
  350. /** Forget about all clients that haven't connected since <b>cutoff</b>. */
  351. void
  352. geoip_remove_old_clients(time_t cutoff)
  353. {
  354. clientmap_HT_FOREACH_FN(&client_history,
  355. _remove_old_client_helper,
  356. &cutoff);
  357. if (client_history_starts < cutoff)
  358. client_history_starts = cutoff;
  359. }
  360. /** Do not mention any country from which fewer than this number of IPs have
  361. * connected. This conceivably avoids reporting information that could
  362. * deanonymize users, though analysis is lacking. */
  363. #define MIN_IPS_TO_NOTE_COUNTRY 1
  364. /** Do not report any geoip data at all if we have fewer than this number of
  365. * IPs to report about. */
  366. #define MIN_IPS_TO_NOTE_ANYTHING 1
  367. /** When reporting geoip data about countries, round up to the nearest
  368. * multiple of this value. */
  369. #define IP_GRANULARITY 8
  370. /** Return the time at which we started recording geoip data. */
  371. time_t
  372. geoip_get_history_start(void)
  373. {
  374. return client_history_starts;
  375. }
  376. /** Helper type: used to sort per-country totals by value. */
  377. typedef struct c_hist_t {
  378. char country[3]; /**< Two-letter country code. */
  379. unsigned total; /**< Total IP addresses seen in this country. */
  380. } c_hist_t;
  381. /** Sorting helper: return -1, 1, or 0 based on comparison of two
  382. * geoip_entry_t. Sort in descending order of total, and then by country
  383. * code. */
  384. static int
  385. _c_hist_compare(const void **_a, const void **_b)
  386. {
  387. const c_hist_t *a = *_a, *b = *_b;
  388. if (a->total > b->total)
  389. return -1;
  390. else if (a->total < b->total)
  391. return 1;
  392. else
  393. return strcmp(a->country, b->country);
  394. }
  395. /** How long do we have to have observed per-country request history before we
  396. * are willing to talk about it? */
  397. #define GEOIP_MIN_OBSERVATION_TIME (12*60*60)
  398. /** Return the lowest x such that x is at least <b>number</b>, and x modulo
  399. * <b>divisor</b> == 0. */
  400. static INLINE unsigned
  401. round_to_next_multiple_of(unsigned number, unsigned divisor)
  402. {
  403. number += divisor - 1;
  404. number -= number % divisor;
  405. return number;
  406. }
  407. /** Return a newly allocated comma-separated string containing entries for all
  408. * the countries from which we've seen enough clients connect. The entry
  409. * format is cc=num where num is the number of IPs we've seen connecting from
  410. * that country, and cc is a lowercased country code. Returns NULL if we don't
  411. * want to export geoip data yet. */
  412. char *
  413. geoip_get_client_history(time_t now, geoip_client_action_t action)
  414. {
  415. char *result = NULL;
  416. if (!geoip_is_loaded())
  417. return NULL;
  418. if (client_history_starts < (now - GEOIP_MIN_OBSERVATION_TIME)) {
  419. char buf[32];
  420. smartlist_t *chunks = NULL;
  421. smartlist_t *entries = NULL;
  422. int n_countries = geoip_get_n_countries();
  423. int i;
  424. clientmap_entry_t **ent;
  425. unsigned *counts = tor_malloc_zero(sizeof(unsigned)*n_countries);
  426. unsigned total = 0;
  427. unsigned granularity = IP_GRANULARITY;
  428. #ifdef ENABLE_GEOIP_STATS
  429. if (get_options()->DirRecordUsageByCountry)
  430. granularity = get_options()->DirRecordUsageGranularity;
  431. #endif
  432. HT_FOREACH(ent, clientmap, &client_history) {
  433. int country;
  434. if (((*ent)->last_seen & ACTION_MASK) != (int)action)
  435. continue;
  436. country = geoip_get_country_by_ip((*ent)->ipaddr);
  437. if (country < 0)
  438. continue;
  439. tor_assert(0 <= country && country < n_countries);
  440. ++counts[country];
  441. ++total;
  442. }
  443. /* Don't record anything if we haven't seen enough IPs. */
  444. if (total < MIN_IPS_TO_NOTE_ANYTHING)
  445. goto done;
  446. /* Make a list of c_hist_t */
  447. entries = smartlist_create();
  448. for (i = 0; i < n_countries; ++i) {
  449. unsigned c = counts[i];
  450. const char *countrycode;
  451. c_hist_t *ent;
  452. /* Only report a country if it has a minimum number of IPs. */
  453. if (c >= MIN_IPS_TO_NOTE_COUNTRY) {
  454. c = round_to_next_multiple_of(c, granularity);
  455. countrycode = geoip_get_country_name(i);
  456. ent = tor_malloc(sizeof(c_hist_t));
  457. strlcpy(ent->country, countrycode, sizeof(ent->country));
  458. ent->total = c;
  459. smartlist_add(entries, ent);
  460. }
  461. }
  462. /* Sort entries. Note that we must do this _AFTER_ rounding, or else
  463. * the sort order could leak info. */
  464. smartlist_sort(entries, _c_hist_compare);
  465. /* Build the result. */
  466. chunks = smartlist_create();
  467. SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
  468. tor_snprintf(buf, sizeof(buf), "%s=%u", ch->country, ch->total);
  469. smartlist_add(chunks, tor_strdup(buf));
  470. });
  471. result = smartlist_join_strings(chunks, ",", 0, NULL);
  472. done:
  473. tor_free(counts);
  474. if (chunks) {
  475. SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
  476. smartlist_free(chunks);
  477. }
  478. if (entries) {
  479. SMARTLIST_FOREACH(entries, c_hist_t *, c, tor_free(c));
  480. smartlist_free(entries);
  481. }
  482. }
  483. return result;
  484. }
  485. /** Return a newly allocated string holding the per-country request history
  486. * for <b>action</b> in a format suitable for an extra-info document, or NULL
  487. * on failure. */
  488. char *
  489. geoip_get_request_history(time_t now, geoip_client_action_t action)
  490. {
  491. smartlist_t *entries, *strings;
  492. char *result;
  493. unsigned granularity = IP_GRANULARITY;
  494. #ifdef ENABLE_GEOIP_STATS
  495. if (get_options()->DirRecordUsageByCountry)
  496. granularity = get_options()->DirRecordUsageGranularity;
  497. #endif
  498. if (client_history_starts >= (now - GEOIP_MIN_OBSERVATION_TIME))
  499. return NULL;
  500. if (action != GEOIP_CLIENT_NETWORKSTATUS &&
  501. action != GEOIP_CLIENT_NETWORKSTATUS_V2)
  502. return NULL;
  503. if (!geoip_countries)
  504. return NULL;
  505. entries = smartlist_create();
  506. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
  507. uint32_t *n = (action == GEOIP_CLIENT_NETWORKSTATUS)
  508. ? c->n_v3_ns_requests : c->n_v2_ns_requests;
  509. uint32_t tot = 0;
  510. int i;
  511. c_hist_t *ent;
  512. for (i=0; i < REQUEST_HIST_LEN; ++i)
  513. tot += n[i];
  514. if (!tot)
  515. continue;
  516. ent = tor_malloc_zero(sizeof(c_hist_t));
  517. strlcpy(ent->country, c->countrycode, sizeof(ent->country));
  518. ent->total = round_to_next_multiple_of(tot, granularity);
  519. smartlist_add(entries, ent);
  520. });
  521. smartlist_sort(entries, _c_hist_compare);
  522. strings = smartlist_create();
  523. SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
  524. char buf[32];
  525. tor_snprintf(buf, sizeof(buf), "%s=%u", ent->country, ent->total);
  526. smartlist_add(strings, tor_strdup(buf));
  527. });
  528. result = smartlist_join_strings(strings, ",", 0, NULL);
  529. SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));
  530. SMARTLIST_FOREACH(entries, c_hist_t *, ent, tor_free(ent));
  531. smartlist_free(strings);
  532. smartlist_free(entries);
  533. return result;
  534. }
  535. /** Store all our geoip statistics into $DATADIR/geoip-stats. */
  536. void
  537. dump_geoip_stats(void)
  538. {
  539. #ifdef ENABLE_GEOIP_STATS
  540. time_t now = time(NULL);
  541. time_t request_start;
  542. char *filename = get_datadir_fname("geoip-stats");
  543. char *data_v2 = NULL, *data_v3 = NULL;
  544. char since[ISO_TIME_LEN+1], written[ISO_TIME_LEN+1];
  545. open_file_t *open_file = NULL;
  546. double v2_share = 0.0, v3_share = 0.0;
  547. FILE *out;
  548. data_v2 = geoip_get_client_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
  549. data_v3 = geoip_get_client_history(now, GEOIP_CLIENT_NETWORKSTATUS);
  550. format_iso_time(since, geoip_get_history_start());
  551. format_iso_time(written, now);
  552. out = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE,
  553. 0600, &open_file);
  554. if (!out)
  555. goto done;
  556. if (fprintf(out, "written %s\nstarted-at %s\nns-ips %s\nns-v2-ips %s\n",
  557. written, since,
  558. data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
  559. goto done;
  560. tor_free(data_v2);
  561. tor_free(data_v3);
  562. request_start = current_request_period_starts -
  563. (n_old_request_periods * REQUEST_HIST_PERIOD);
  564. format_iso_time(since, request_start);
  565. data_v2 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
  566. data_v3 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS);
  567. if (fprintf(out, "requests-start %s\nn-ns-reqs %s\nn-v2-ns-reqs %s\n",
  568. since,
  569. data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
  570. goto done;
  571. if (!router_get_my_share_of_directory_requests(&v2_share, &v3_share)) {
  572. if (fprintf(out, "v2-ns-share %0.2lf%%\n", v2_share*100) < 0)
  573. goto done;
  574. if (fprintf(out, "v3-ns-share %0.2lf%%\n", v3_share*100) < 0)
  575. goto done;
  576. }
  577. finish_writing_to_file(open_file);
  578. open_file = NULL;
  579. done:
  580. if (open_file)
  581. abort_writing_to_file(open_file);
  582. tor_free(filename);
  583. tor_free(data_v2);
  584. tor_free(data_v3);
  585. #endif
  586. }
  587. /** Helper used to implement GETINFO ip-to-country/... controller command. */
  588. int
  589. getinfo_helper_geoip(control_connection_t *control_conn,
  590. const char *question, char **answer)
  591. {
  592. (void)control_conn;
  593. if (geoip_is_loaded() && !strcmpstart(question, "ip-to-country/")) {
  594. int c;
  595. uint32_t ip;
  596. struct in_addr in;
  597. question += strlen("ip-to-country/");
  598. if (tor_inet_aton(question, &in) != 0) {
  599. ip = ntohl(in.s_addr);
  600. c = geoip_get_country_by_ip(ip);
  601. *answer = tor_strdup(geoip_get_country_name(c));
  602. }
  603. }
  604. return 0;
  605. }
  606. /** Release all storage held by the GeoIP database. */
  607. static void
  608. clear_geoip_db(void)
  609. {
  610. if (geoip_countries) {
  611. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, tor_free(c));
  612. smartlist_free(geoip_countries);
  613. }
  614. if (country_idxplus1_by_lc_code)
  615. strmap_free(country_idxplus1_by_lc_code, NULL);
  616. if (geoip_entries) {
  617. SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));
  618. smartlist_free(geoip_entries);
  619. }
  620. geoip_countries = NULL;
  621. country_idxplus1_by_lc_code = NULL;
  622. geoip_entries = NULL;
  623. }
  624. /** Release all storage held in this file. */
  625. void
  626. geoip_free_all(void)
  627. {
  628. clientmap_entry_t **ent, **next, *this;
  629. for (ent = HT_START(clientmap, &client_history); ent != NULL; ent = next) {
  630. this = *ent;
  631. next = HT_NEXT_RMV(clientmap, &client_history, ent);
  632. tor_free(this);
  633. }
  634. HT_CLEAR(clientmap, &client_history);
  635. clear_geoip_db();
  636. }