geoip.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. /* Did we recently switch from bridge to relay or back? */
  279. if (client_history_starts > now)
  280. return;
  281. } else {
  282. #ifndef ENABLE_GEOIP_STATS
  283. return;
  284. #else
  285. if (options->BridgeRelay || options->BridgeAuthoritativeDir ||
  286. !options->DirRecordUsageByCountry)
  287. return;
  288. #endif
  289. }
  290. /* Rotate the current request period. */
  291. while (current_request_period_starts + REQUEST_HIST_PERIOD < now) {
  292. if (!geoip_countries)
  293. geoip_countries = smartlist_create();
  294. if (!current_request_period_starts) {
  295. current_request_period_starts = now;
  296. break;
  297. }
  298. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
  299. memmove(&c->n_v2_ns_requests[0], &c->n_v2_ns_requests[1],
  300. sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
  301. memmove(&c->n_v3_ns_requests[0], &c->n_v3_ns_requests[1],
  302. sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
  303. c->n_v2_ns_requests[REQUEST_HIST_LEN-1] = 0;
  304. c->n_v3_ns_requests[REQUEST_HIST_LEN-1] = 0;
  305. });
  306. current_request_period_starts += REQUEST_HIST_PERIOD;
  307. if (n_old_request_periods < REQUEST_HIST_LEN-1)
  308. ++n_old_request_periods;
  309. }
  310. /* We use the low 3 bits of the time to encode the action. Since we're
  311. * potentially remembering tons of clients, we don't want to make
  312. * clientmap_entry_t larger than it has to be. */
  313. now = (now & ~ACTION_MASK) | (((int)action) & ACTION_MASK);
  314. lookup.ipaddr = addr;
  315. ent = HT_FIND(clientmap, &client_history, &lookup);
  316. if (ent) {
  317. ent->last_seen = now;
  318. } else {
  319. ent = tor_malloc_zero(sizeof(clientmap_entry_t));
  320. ent->ipaddr = addr;
  321. ent->last_seen = now;
  322. HT_INSERT(clientmap, &client_history, ent);
  323. }
  324. if (action == GEOIP_CLIENT_NETWORKSTATUS ||
  325. action == GEOIP_CLIENT_NETWORKSTATUS_V2) {
  326. int country_idx = geoip_get_country_by_ip(addr);
  327. if (country_idx >= 0 && country_idx < smartlist_len(geoip_countries)) {
  328. geoip_country_t *country = smartlist_get(geoip_countries, country_idx);
  329. if (action == GEOIP_CLIENT_NETWORKSTATUS)
  330. ++country->n_v3_ns_requests[REQUEST_HIST_LEN-1];
  331. else
  332. ++country->n_v2_ns_requests[REQUEST_HIST_LEN-1];
  333. }
  334. }
  335. if (!client_history_starts) {
  336. client_history_starts = now;
  337. current_request_period_starts = now;
  338. }
  339. }
  340. /** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
  341. * older than a certain time. */
  342. static int
  343. _remove_old_client_helper(struct clientmap_entry_t *ent, void *_cutoff)
  344. {
  345. time_t cutoff = *(time_t*)_cutoff;
  346. if (ent->last_seen < cutoff) {
  347. tor_free(ent);
  348. return 1;
  349. } else {
  350. return 0;
  351. }
  352. }
  353. /** Forget about all clients that haven't connected since <b>cutoff</b>. */
  354. void
  355. geoip_remove_old_clients(time_t cutoff)
  356. {
  357. clientmap_HT_FOREACH_FN(&client_history,
  358. _remove_old_client_helper,
  359. &cutoff);
  360. if (client_history_starts < cutoff)
  361. client_history_starts = cutoff;
  362. }
  363. /** Do not mention any country from which fewer than this number of IPs have
  364. * connected. This conceivably avoids reporting information that could
  365. * deanonymize users, though analysis is lacking. */
  366. #define MIN_IPS_TO_NOTE_COUNTRY 1
  367. /** Do not report any geoip data at all if we have fewer than this number of
  368. * IPs to report about. */
  369. #define MIN_IPS_TO_NOTE_ANYTHING 1
  370. /** When reporting geoip data about countries, round up to the nearest
  371. * multiple of this value. */
  372. #define IP_GRANULARITY 8
  373. /** Return the time at which we started recording geoip data. */
  374. time_t
  375. geoip_get_history_start(void)
  376. {
  377. return client_history_starts;
  378. }
  379. /** Helper type: used to sort per-country totals by value. */
  380. typedef struct c_hist_t {
  381. char country[3]; /**< Two-letter country code. */
  382. unsigned total; /**< Total IP addresses seen in this country. */
  383. } c_hist_t;
  384. /** Sorting helper: return -1, 1, or 0 based on comparison of two
  385. * geoip_entry_t. Sort in descending order of total, and then by country
  386. * code. */
  387. static int
  388. _c_hist_compare(const void **_a, const void **_b)
  389. {
  390. const c_hist_t *a = *_a, *b = *_b;
  391. if (a->total > b->total)
  392. return -1;
  393. else if (a->total < b->total)
  394. return 1;
  395. else
  396. return strcmp(a->country, b->country);
  397. }
  398. /** How long do we have to have observed per-country request history before we
  399. * are willing to talk about it? */
  400. #define GEOIP_MIN_OBSERVATION_TIME (12*60*60)
  401. /** Return the lowest x such that x is at least <b>number</b>, and x modulo
  402. * <b>divisor</b> == 0. */
  403. static INLINE unsigned
  404. round_to_next_multiple_of(unsigned number, unsigned divisor)
  405. {
  406. number += divisor - 1;
  407. number -= number % divisor;
  408. return number;
  409. }
  410. /** Return a newly allocated comma-separated string containing entries for all
  411. * the countries from which we've seen enough clients connect. The entry
  412. * format is cc=num where num is the number of IPs we've seen connecting from
  413. * that country, and cc is a lowercased country code. Returns NULL if we don't
  414. * want to export geoip data yet. */
  415. char *
  416. geoip_get_client_history(time_t now, geoip_client_action_t action)
  417. {
  418. char *result = NULL;
  419. if (!geoip_is_loaded())
  420. return NULL;
  421. if (client_history_starts < (now - GEOIP_MIN_OBSERVATION_TIME)) {
  422. char buf[32];
  423. smartlist_t *chunks = NULL;
  424. smartlist_t *entries = NULL;
  425. int n_countries = geoip_get_n_countries();
  426. int i;
  427. clientmap_entry_t **ent;
  428. unsigned *counts = tor_malloc_zero(sizeof(unsigned)*n_countries);
  429. unsigned total = 0;
  430. unsigned granularity = IP_GRANULARITY;
  431. #ifdef ENABLE_GEOIP_STATS
  432. if (get_options()->DirRecordUsageByCountry)
  433. granularity = get_options()->DirRecordUsageGranularity;
  434. #endif
  435. HT_FOREACH(ent, clientmap, &client_history) {
  436. int country;
  437. if (((*ent)->last_seen & ACTION_MASK) != (int)action)
  438. continue;
  439. country = geoip_get_country_by_ip((*ent)->ipaddr);
  440. if (country < 0)
  441. continue;
  442. tor_assert(0 <= country && country < n_countries);
  443. ++counts[country];
  444. ++total;
  445. }
  446. /* Don't record anything if we haven't seen enough IPs. */
  447. if (total < MIN_IPS_TO_NOTE_ANYTHING)
  448. goto done;
  449. /* Make a list of c_hist_t */
  450. entries = smartlist_create();
  451. for (i = 0; i < n_countries; ++i) {
  452. unsigned c = counts[i];
  453. const char *countrycode;
  454. c_hist_t *ent;
  455. /* Only report a country if it has a minimum number of IPs. */
  456. if (c >= MIN_IPS_TO_NOTE_COUNTRY) {
  457. c = round_to_next_multiple_of(c, granularity);
  458. countrycode = geoip_get_country_name(i);
  459. ent = tor_malloc(sizeof(c_hist_t));
  460. strlcpy(ent->country, countrycode, sizeof(ent->country));
  461. ent->total = c;
  462. smartlist_add(entries, ent);
  463. }
  464. }
  465. /* Sort entries. Note that we must do this _AFTER_ rounding, or else
  466. * the sort order could leak info. */
  467. smartlist_sort(entries, _c_hist_compare);
  468. /* Build the result. */
  469. chunks = smartlist_create();
  470. SMARTLIST_FOREACH(entries, c_hist_t *, ch, {
  471. tor_snprintf(buf, sizeof(buf), "%s=%u", ch->country, ch->total);
  472. smartlist_add(chunks, tor_strdup(buf));
  473. });
  474. result = smartlist_join_strings(chunks, ",", 0, NULL);
  475. done:
  476. tor_free(counts);
  477. if (chunks) {
  478. SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
  479. smartlist_free(chunks);
  480. }
  481. if (entries) {
  482. SMARTLIST_FOREACH(entries, c_hist_t *, c, tor_free(c));
  483. smartlist_free(entries);
  484. }
  485. }
  486. return result;
  487. }
  488. /** Return a newly allocated string holding the per-country request history
  489. * for <b>action</b> in a format suitable for an extra-info document, or NULL
  490. * on failure. */
  491. char *
  492. geoip_get_request_history(time_t now, geoip_client_action_t action)
  493. {
  494. smartlist_t *entries, *strings;
  495. char *result;
  496. unsigned granularity = IP_GRANULARITY;
  497. #ifdef ENABLE_GEOIP_STATS
  498. if (get_options()->DirRecordUsageByCountry)
  499. granularity = get_options()->DirRecordUsageGranularity;
  500. #endif
  501. if (client_history_starts >= (now - GEOIP_MIN_OBSERVATION_TIME))
  502. return NULL;
  503. if (action != GEOIP_CLIENT_NETWORKSTATUS &&
  504. action != GEOIP_CLIENT_NETWORKSTATUS_V2)
  505. return NULL;
  506. if (!geoip_countries)
  507. return NULL;
  508. entries = smartlist_create();
  509. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
  510. uint32_t *n = (action == GEOIP_CLIENT_NETWORKSTATUS)
  511. ? c->n_v3_ns_requests : c->n_v2_ns_requests;
  512. uint32_t tot = 0;
  513. int i;
  514. c_hist_t *ent;
  515. for (i=0; i < REQUEST_HIST_LEN; ++i)
  516. tot += n[i];
  517. if (!tot)
  518. continue;
  519. ent = tor_malloc_zero(sizeof(c_hist_t));
  520. strlcpy(ent->country, c->countrycode, sizeof(ent->country));
  521. ent->total = round_to_next_multiple_of(tot, granularity);
  522. smartlist_add(entries, ent);
  523. });
  524. smartlist_sort(entries, _c_hist_compare);
  525. strings = smartlist_create();
  526. SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
  527. char buf[32];
  528. tor_snprintf(buf, sizeof(buf), "%s=%u", ent->country, ent->total);
  529. smartlist_add(strings, tor_strdup(buf));
  530. });
  531. result = smartlist_join_strings(strings, ",", 0, NULL);
  532. SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));
  533. SMARTLIST_FOREACH(entries, c_hist_t *, ent, tor_free(ent));
  534. smartlist_free(strings);
  535. smartlist_free(entries);
  536. return result;
  537. }
  538. /** Store all our geoip statistics into $DATADIR/geoip-stats. */
  539. void
  540. dump_geoip_stats(void)
  541. {
  542. #ifdef ENABLE_GEOIP_STATS
  543. time_t now = time(NULL);
  544. time_t request_start;
  545. char *filename = get_datadir_fname("geoip-stats");
  546. char *data_v2 = NULL, *data_v3 = NULL;
  547. char since[ISO_TIME_LEN+1], written[ISO_TIME_LEN+1];
  548. open_file_t *open_file = NULL;
  549. double v2_share = 0.0, v3_share = 0.0;
  550. FILE *out;
  551. data_v2 = geoip_get_client_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
  552. data_v3 = geoip_get_client_history(now, GEOIP_CLIENT_NETWORKSTATUS);
  553. format_iso_time(since, geoip_get_history_start());
  554. format_iso_time(written, now);
  555. out = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE,
  556. 0600, &open_file);
  557. if (!out)
  558. goto done;
  559. if (fprintf(out, "written %s\nstarted-at %s\nns-ips %s\nns-v2-ips %s\n",
  560. written, since,
  561. data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
  562. goto done;
  563. tor_free(data_v2);
  564. tor_free(data_v3);
  565. request_start = current_request_period_starts -
  566. (n_old_request_periods * REQUEST_HIST_PERIOD);
  567. format_iso_time(since, request_start);
  568. data_v2 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
  569. data_v3 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS);
  570. if (fprintf(out, "requests-start %s\nn-ns-reqs %s\nn-v2-ns-reqs %s\n",
  571. since,
  572. data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
  573. goto done;
  574. if (!router_get_my_share_of_directory_requests(&v2_share, &v3_share)) {
  575. if (fprintf(out, "v2-ns-share %0.2lf%%\n", v2_share*100) < 0)
  576. goto done;
  577. if (fprintf(out, "v3-ns-share %0.2lf%%\n", v3_share*100) < 0)
  578. goto done;
  579. }
  580. finish_writing_to_file(open_file);
  581. open_file = NULL;
  582. done:
  583. if (open_file)
  584. abort_writing_to_file(open_file);
  585. tor_free(filename);
  586. tor_free(data_v2);
  587. tor_free(data_v3);
  588. #endif
  589. }
  590. /** Helper used to implement GETINFO ip-to-country/... controller command. */
  591. int
  592. getinfo_helper_geoip(control_connection_t *control_conn,
  593. const char *question, char **answer)
  594. {
  595. (void)control_conn;
  596. if (geoip_is_loaded() && !strcmpstart(question, "ip-to-country/")) {
  597. int c;
  598. uint32_t ip;
  599. struct in_addr in;
  600. question += strlen("ip-to-country/");
  601. if (tor_inet_aton(question, &in) != 0) {
  602. ip = ntohl(in.s_addr);
  603. c = geoip_get_country_by_ip(ip);
  604. *answer = tor_strdup(geoip_get_country_name(c));
  605. }
  606. }
  607. return 0;
  608. }
  609. /** Release all storage held by the GeoIP database. */
  610. static void
  611. clear_geoip_db(void)
  612. {
  613. if (geoip_countries) {
  614. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, tor_free(c));
  615. smartlist_free(geoip_countries);
  616. }
  617. if (country_idxplus1_by_lc_code)
  618. strmap_free(country_idxplus1_by_lc_code, NULL);
  619. if (geoip_entries) {
  620. SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));
  621. smartlist_free(geoip_entries);
  622. }
  623. geoip_countries = NULL;
  624. country_idxplus1_by_lc_code = NULL;
  625. geoip_entries = NULL;
  626. }
  627. /** Release all storage held in this file. */
  628. void
  629. geoip_free_all(void)
  630. {
  631. clientmap_entry_t **ent, **next, *this;
  632. for (ent = HT_START(clientmap, &client_history); ent != NULL; ent = next) {
  633. this = *ent;
  634. next = HT_NEXT_RMV(clientmap, &client_history, ent);
  635. tor_free(this);
  636. }
  637. HT_CLEAR(clientmap, &client_history);
  638. clear_geoip_db();
  639. }