geoip.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /* Copyright (c) 2007-2008, 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. return 0;
  190. }
  191. /** Given an IP address in host order, return a number representing the
  192. * country to which that address belongs, or -1 for unknown. The return value
  193. * will always be less than geoip_get_n_countries(). To decode it,
  194. * call geoip_get_country_name().
  195. */
  196. int
  197. geoip_get_country_by_ip(uint32_t ipaddr)
  198. {
  199. geoip_entry_t *ent;
  200. if (!geoip_entries)
  201. return -1;
  202. ent = smartlist_bsearch(geoip_entries, &ipaddr, _geoip_compare_key_to_entry);
  203. return ent ? (int)ent->country : -1;
  204. }
  205. /** Return the number of countries recognized by the GeoIP database. */
  206. int
  207. geoip_get_n_countries(void)
  208. {
  209. return (int) smartlist_len(geoip_countries);
  210. }
  211. /** Return the two-letter country code associated with the number <b>num</b>,
  212. * or "??" for an unknown value. */
  213. const char *
  214. geoip_get_country_name(country_t num)
  215. {
  216. if (geoip_countries && num >= 0 && num < smartlist_len(geoip_countries)) {
  217. geoip_country_t *c = smartlist_get(geoip_countries, num);
  218. return c->countrycode;
  219. } else
  220. return "??";
  221. }
  222. /** Return true iff we have loaded a GeoIP database.*/
  223. int
  224. geoip_is_loaded(void)
  225. {
  226. return geoip_countries != NULL && geoip_entries != NULL;
  227. }
  228. /** Entry in a map from IP address to the last time we've seen an incoming
  229. * connection from that IP address. Used by bridges only, to track which
  230. * countries have them blocked. */
  231. typedef struct clientmap_entry_t {
  232. HT_ENTRY(clientmap_entry_t) node;
  233. uint32_t ipaddr;
  234. time_t last_seen; /* The last 2 bits of this value hold the client
  235. * operation. */
  236. } clientmap_entry_t;
  237. #define ACTION_MASK 3
  238. /** Map from client IP address to last time seen. */
  239. static HT_HEAD(clientmap, clientmap_entry_t) client_history =
  240. HT_INITIALIZER();
  241. /** Time at which we started tracking client IP history. */
  242. static time_t client_history_starts = 0;
  243. /** When did the current period of checking per-country request history
  244. * start? */
  245. static time_t current_request_period_starts = 0;
  246. /** How many older request periods are we remembering? */
  247. static int n_old_request_periods = 0;
  248. /** Hashtable helper: compute a hash of a clientmap_entry_t. */
  249. static INLINE unsigned
  250. clientmap_entry_hash(const clientmap_entry_t *a)
  251. {
  252. return ht_improve_hash((unsigned) a->ipaddr);
  253. }
  254. /** Hashtable helper: compare two clientmap_entry_t values for equality. */
  255. static INLINE int
  256. clientmap_entries_eq(const clientmap_entry_t *a, const clientmap_entry_t *b)
  257. {
  258. return a->ipaddr == b->ipaddr;
  259. }
  260. HT_PROTOTYPE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
  261. clientmap_entries_eq);
  262. HT_GENERATE(clientmap, clientmap_entry_t, node, clientmap_entry_hash,
  263. clientmap_entries_eq, 0.6, malloc, realloc, free);
  264. /** Note that we've seen a client connect from the IP <b>addr</b> (host order)
  265. * at time <b>now</b>. Ignored by all but bridges. */
  266. void
  267. geoip_note_client_seen(geoip_client_action_t action,
  268. uint32_t addr, time_t now)
  269. {
  270. or_options_t *options = get_options();
  271. clientmap_entry_t lookup, *ent;
  272. if (action == GEOIP_CLIENT_CONNECT) {
  273. if (!(options->BridgeRelay && options->BridgeRecordUsageByCountry))
  274. return;
  275. } else {
  276. #ifndef ENABLE_GEOIP_STATS
  277. return;
  278. #else
  279. if (options->BridgeRelay || options->BridgeAuthoritativeDir ||
  280. !options->DirRecordUsageByCountry)
  281. return;
  282. #endif
  283. }
  284. /* Rotate the current request period. */
  285. while (current_request_period_starts + REQUEST_HIST_PERIOD < now) {
  286. if (!geoip_countries)
  287. geoip_countries = smartlist_create();
  288. if (!current_request_period_starts) {
  289. current_request_period_starts = now;
  290. break;
  291. }
  292. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
  293. memmove(&c->n_v2_ns_requests[0], &c->n_v2_ns_requests[1],
  294. sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
  295. memmove(&c->n_v3_ns_requests[0], &c->n_v3_ns_requests[1],
  296. sizeof(uint32_t)*(REQUEST_HIST_LEN-1));
  297. c->n_v2_ns_requests[REQUEST_HIST_LEN-1] = 0;
  298. c->n_v3_ns_requests[REQUEST_HIST_LEN-1] = 0;
  299. });
  300. current_request_period_starts += REQUEST_HIST_PERIOD;
  301. if (n_old_request_periods < REQUEST_HIST_LEN-1)
  302. ++n_old_request_periods;
  303. }
  304. /* We use the low 3 bits of the time to encode the action. Since we're
  305. * potentially remembering tons of clients, we don't want to make
  306. * clientmap_entry_t larger than it has to be. */
  307. now = (now & ~ACTION_MASK) | (((int)action) & ACTION_MASK);
  308. lookup.ipaddr = addr;
  309. ent = HT_FIND(clientmap, &client_history, &lookup);
  310. if (ent) {
  311. ent->last_seen = now;
  312. } else {
  313. ent = tor_malloc_zero(sizeof(clientmap_entry_t));
  314. ent->ipaddr = addr;
  315. ent->last_seen = now;
  316. HT_INSERT(clientmap, &client_history, ent);
  317. }
  318. if (action == GEOIP_CLIENT_NETWORKSTATUS ||
  319. action == GEOIP_CLIENT_NETWORKSTATUS_V2) {
  320. int country_idx = geoip_get_country_by_ip(addr);
  321. if (country_idx >= 0 && country_idx < smartlist_len(geoip_countries)) {
  322. geoip_country_t *country = smartlist_get(geoip_countries, country_idx);
  323. if (action == GEOIP_CLIENT_NETWORKSTATUS)
  324. ++country->n_v3_ns_requests[REQUEST_HIST_LEN-1];
  325. else
  326. ++country->n_v2_ns_requests[REQUEST_HIST_LEN-1];
  327. }
  328. }
  329. if (!client_history_starts) {
  330. client_history_starts = now;
  331. current_request_period_starts = now;
  332. }
  333. }
  334. /** HT_FOREACH helper: remove a clientmap_entry_t from the hashtable if it's
  335. * older than a certain time. */
  336. static int
  337. _remove_old_client_helper(struct clientmap_entry_t *ent, void *_cutoff)
  338. {
  339. time_t cutoff = *(time_t*)_cutoff;
  340. if (ent->last_seen < cutoff) {
  341. tor_free(ent);
  342. return 1;
  343. } else {
  344. return 0;
  345. }
  346. }
  347. /** Forget about all clients that haven't connected since <b>cutoff</b>. */
  348. void
  349. geoip_remove_old_clients(time_t cutoff)
  350. {
  351. clientmap_HT_FOREACH_FN(&client_history,
  352. _remove_old_client_helper,
  353. &cutoff);
  354. if (client_history_starts < cutoff)
  355. client_history_starts = cutoff;
  356. }
  357. /** Do not mention any country from which fewer than this number of IPs have
  358. * connected. This conceivably avoids reporting information that could
  359. * deanonymize users, though analysis is lacking. */
  360. #define MIN_IPS_TO_NOTE_COUNTRY 0
  361. /** Do not report any geoip data at all if we have fewer than this number of
  362. * IPs to report about. */
  363. #define MIN_IPS_TO_NOTE_ANYTHING 0
  364. /** When reporting geoip data about countries, round up to the nearest
  365. * multiple of this value. */
  366. #define IP_GRANULARITY 8
  367. /** Return the time at which we started recording geoip data. */
  368. time_t
  369. geoip_get_history_start(void)
  370. {
  371. return client_history_starts;
  372. }
  373. /** Helper type: used to sort per-country totals by value. */
  374. typedef struct c_hist_t {
  375. char country[3]; /**< Two-letter country code. */
  376. unsigned total; /**< Total IP addresses seen in this country. */
  377. } c_hist_t;
  378. /** Sorting helper: return -1, 1, or 0 based on comparison of two
  379. * geoip_entry_t. Sort in descending order of total, and then by country
  380. * code. */
  381. static int
  382. _c_hist_compare(const void **_a, const void **_b)
  383. {
  384. const c_hist_t *a = *_a, *b = *_b;
  385. if (a->total > b->total)
  386. return -1;
  387. else if (a->total < b->total)
  388. return 1;
  389. else
  390. return strcmp(a->country, b->country);
  391. }
  392. /** How long do we have to have observed per-country request history before we
  393. * are willing to talk about it? */
  394. #define GEOIP_MIN_OBSERVATION_TIME (12*60*60)
  395. /** Return the lowest x such that x is at least <b>number</b>, and x modulo
  396. * <b>divisor</b> == 0. */
  397. static INLINE unsigned
  398. round_to_next_multiple_of(unsigned number, unsigned divisor)
  399. {
  400. number += divisor - 1;
  401. number -= number % divisor;
  402. return number;
  403. }
  404. /** Return a newly allocated comma-separated string containing entries for all
  405. * the countries from which we've seen enough clients connect. The entry
  406. * format is cc=num where num is the number of IPs we've seen connecting from
  407. * that country, and cc is a lowercased country code. Returns NULL if we don't
  408. * want to export geoip data yet. */
  409. char *
  410. geoip_get_client_history(time_t now, geoip_client_action_t action)
  411. {
  412. char *result = NULL;
  413. if (!geoip_is_loaded())
  414. return NULL;
  415. if (client_history_starts < (now - GEOIP_MIN_OBSERVATION_TIME)) {
  416. char buf[32];
  417. smartlist_t *chunks = NULL;
  418. smartlist_t *entries = NULL;
  419. int n_countries = geoip_get_n_countries();
  420. int i;
  421. clientmap_entry_t **ent;
  422. unsigned *counts = tor_malloc_zero(sizeof(unsigned)*n_countries);
  423. unsigned total = 0;
  424. unsigned granularity = IP_GRANULARITY;
  425. #ifdef ENABLE_GEOIP_STATS
  426. if (get_options()->DirRecordUsageByCountry)
  427. granularity = get_options()->DirRecordUsageGranularity;
  428. #endif
  429. HT_FOREACH(ent, clientmap, &client_history) {
  430. int country;
  431. if (((*ent)->last_seen & ACTION_MASK) != (int)action)
  432. continue;
  433. country = geoip_get_country_by_ip((*ent)->ipaddr);
  434. if (country < 0)
  435. continue;
  436. tor_assert(0 <= country && country < n_countries);
  437. ++counts[country];
  438. ++total;
  439. }
  440. /* Don't record anything if we haven't seen enough IPs. */
  441. #if (MIN_IPS_TO_NOTE_ANYTHING > 0)
  442. if (total < MIN_IPS_TO_NOTE_ANYTHING)
  443. goto done;
  444. #endif
  445. /* Make a list of c_hist_t */
  446. entries = smartlist_create();
  447. for (i = 0; i < n_countries; ++i) {
  448. unsigned c = counts[i];
  449. const char *countrycode;
  450. c_hist_t *ent;
  451. /* Only report a country if it has a minimum number of IPs. */
  452. #if (MIN_IPS_TO_NOTE_COUNTRY > 0)
  453. if (c >= MIN_IPS_TO_NOTE_COUNTRY) {
  454. #else
  455. if (c > 0) {
  456. #endif
  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. #if (MIN_IPS_TO_NOTE_ANYTHING > 0)
  476. done:
  477. #endif
  478. tor_free(counts);
  479. if (chunks) {
  480. SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
  481. smartlist_free(chunks);
  482. }
  483. if (entries) {
  484. SMARTLIST_FOREACH(entries, c_hist_t *, c, tor_free(c));
  485. smartlist_free(entries);
  486. }
  487. }
  488. return result;
  489. }
  490. /** Return a newly allocated string holding the per-country request history
  491. * for <b>action</b> in a format suitable for an extra-info document, or NULL
  492. * on failure. */
  493. char *
  494. geoip_get_request_history(time_t now, geoip_client_action_t action)
  495. {
  496. smartlist_t *entries, *strings;
  497. char *result;
  498. unsigned granularity = IP_GRANULARITY;
  499. #ifdef ENABLE_GEOIP_STATS
  500. if (get_options()->DirRecordUsageByCountry)
  501. granularity = get_options()->DirRecordUsageGranularity;
  502. #endif
  503. if (client_history_starts >= (now - GEOIP_MIN_OBSERVATION_TIME))
  504. return NULL;
  505. if (action != GEOIP_CLIENT_NETWORKSTATUS &&
  506. action != GEOIP_CLIENT_NETWORKSTATUS_V2)
  507. return NULL;
  508. if (!geoip_countries)
  509. return NULL;
  510. entries = smartlist_create();
  511. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, {
  512. uint32_t *n = (action == GEOIP_CLIENT_NETWORKSTATUS)
  513. ? c->n_v3_ns_requests : c->n_v2_ns_requests;
  514. uint32_t tot = 0;
  515. int i;
  516. c_hist_t *ent;
  517. for (i=0; i < REQUEST_HIST_LEN; ++i)
  518. tot += n[i];
  519. if (!tot)
  520. continue;
  521. ent = tor_malloc_zero(sizeof(c_hist_t));
  522. strlcpy(ent->country, c->countrycode, sizeof(ent->country));
  523. ent->total = round_to_next_multiple_of(tot, granularity);
  524. smartlist_add(entries, ent);
  525. });
  526. smartlist_sort(entries, _c_hist_compare);
  527. strings = smartlist_create();
  528. SMARTLIST_FOREACH(entries, c_hist_t *, ent, {
  529. char buf[32];
  530. tor_snprintf(buf, sizeof(buf), "%s=%u", ent->country, ent->total);
  531. smartlist_add(strings, tor_strdup(buf));
  532. });
  533. result = smartlist_join_strings(strings, ",", 0, NULL);
  534. SMARTLIST_FOREACH(strings, char *, cp, tor_free(cp));
  535. SMARTLIST_FOREACH(entries, c_hist_t *, ent, tor_free(ent));
  536. smartlist_free(strings);
  537. smartlist_free(entries);
  538. return result;
  539. }
  540. /** Store all our geoip statistics into $DATADIR/geoip-stats. */
  541. void
  542. dump_geoip_stats(void)
  543. {
  544. #ifdef ENABLE_GEOIP_STATS
  545. time_t now = time(NULL);
  546. time_t request_start;
  547. char *filename = get_datadir_fname("geoip-stats");
  548. char *data_v2 = NULL, *data_v3 = NULL;
  549. char since[ISO_TIME_LEN+1], written[ISO_TIME_LEN+1];
  550. open_file_t *open_file = NULL;
  551. double v2_share = 0.0, v3_share = 0.0;
  552. FILE *out;
  553. data_v2 = geoip_get_client_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
  554. data_v3 = geoip_get_client_history(now, GEOIP_CLIENT_NETWORKSTATUS);
  555. format_iso_time(since, geoip_get_history_start());
  556. format_iso_time(written, now);
  557. out = start_writing_to_stdio_file(filename, OPEN_FLAGS_REPLACE,
  558. 0600, &open_file);
  559. if (!out)
  560. goto done;
  561. if (fprintf(out, "written %s\nstarted-at %s\nns-ips %s\nns-v2-ips %s\n",
  562. written, since,
  563. data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
  564. goto done;
  565. tor_free(data_v2);
  566. tor_free(data_v3);
  567. request_start = current_request_period_starts -
  568. (n_old_request_periods * REQUEST_HIST_PERIOD);
  569. format_iso_time(since, request_start);
  570. data_v2 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS_V2);
  571. data_v3 = geoip_get_request_history(now, GEOIP_CLIENT_NETWORKSTATUS);
  572. if (fprintf(out, "requests-start %s\nn-ns-reqs %s\nn-v2-ns-reqs %s\n",
  573. since,
  574. data_v3 ? data_v3 : "", data_v2 ? data_v2 : "") < 0)
  575. goto done;
  576. if (!router_get_my_share_of_directory_requests(&v2_share, &v3_share)) {
  577. if (fprintf(out, "v2-ns-share %0.2lf%%\n", v2_share*100) < 0)
  578. goto done;
  579. if (fprintf(out, "v3-ns-share %0.2lf%%\n", v3_share*100) < 0)
  580. goto done;
  581. }
  582. finish_writing_to_file(open_file);
  583. open_file = NULL;
  584. done:
  585. if (open_file)
  586. abort_writing_to_file(open_file);
  587. tor_free(filename);
  588. tor_free(data_v2);
  589. tor_free(data_v3);
  590. #endif
  591. }
  592. /** Helper used to implement GETINFO ip-to-country/... controller command. */
  593. int
  594. getinfo_helper_geoip(control_connection_t *control_conn,
  595. const char *question, char **answer)
  596. {
  597. (void)control_conn;
  598. if (geoip_is_loaded() && !strcmpstart(question, "ip-to-country/")) {
  599. int c;
  600. uint32_t ip;
  601. struct in_addr in;
  602. question += strlen("ip-to-country/");
  603. if (tor_inet_aton(question, &in) != 0) {
  604. ip = ntohl(in.s_addr);
  605. c = geoip_get_country_by_ip(ip);
  606. *answer = tor_strdup(geoip_get_country_name(c));
  607. }
  608. }
  609. return 0;
  610. }
  611. /** Release all storage held by the GeoIP database. */
  612. static void
  613. clear_geoip_db(void)
  614. {
  615. if (geoip_countries) {
  616. SMARTLIST_FOREACH(geoip_countries, geoip_country_t *, c, tor_free(c));
  617. smartlist_free(geoip_countries);
  618. }
  619. if (country_idxplus1_by_lc_code)
  620. strmap_free(country_idxplus1_by_lc_code, NULL);
  621. if (geoip_entries) {
  622. SMARTLIST_FOREACH(geoip_entries, geoip_entry_t *, ent, tor_free(ent));
  623. smartlist_free(geoip_entries);
  624. }
  625. geoip_countries = NULL;
  626. country_idxplus1_by_lc_code = NULL;
  627. geoip_entries = NULL;
  628. }
  629. /** Release all storage held in this file. */
  630. void
  631. geoip_free_all(void)
  632. {
  633. clientmap_entry_t **ent, **next, *this;
  634. for (ent = HT_START(clientmap, &client_history); ent != NULL; ent = next) {
  635. this = *ent;
  636. next = HT_NEXT_RMV(clientmap, &client_history, ent);
  637. tor_free(this);
  638. }
  639. HT_CLEAR(clientmap, &client_history);
  640. clear_geoip_db();
  641. }