routerset.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "or.h"
  7. #include "geoip.h"
  8. #include "nodelist.h"
  9. #include "policies.h"
  10. #include "router.h"
  11. #include "routerparse.h"
  12. #include "routerset.h"
  13. /** A routerset specifies constraints on a set of possible routerinfos, based
  14. * on their names, identities, or addresses. It is optimized for determining
  15. * whether a router is a member or not, in O(1+P) time, where P is the number
  16. * of address policy constraints. */
  17. struct routerset_t {
  18. /** A list of strings for the elements of the policy. Each string is either
  19. * a nickname, a hexadecimal identity fingerprint, or an address policy. A
  20. * router belongs to the set if its nickname OR its identity OR its address
  21. * matches an entry here. */
  22. smartlist_t *list;
  23. /** A map from lowercase nicknames of routers in the set to (void*)1 */
  24. strmap_t *names;
  25. /** A map from identity digests routers in the set to (void*)1 */
  26. digestmap_t *digests;
  27. /** An address policy for routers in the set. For implementation reasons,
  28. * a router belongs to the set if it is _rejected_ by this policy. */
  29. smartlist_t *policies;
  30. /** A human-readable description of what this routerset is for. Used in
  31. * log messages. */
  32. char *description;
  33. /** A list of the country codes in this set. */
  34. smartlist_t *country_names;
  35. /** Total number of countries we knew about when we built <b>countries</b>.*/
  36. int n_countries;
  37. /** Bit array mapping the return value of geoip_get_country() to 1 iff the
  38. * country is a member of this routerset. Note that we MUST call
  39. * routerset_refresh_countries() whenever the geoip country list is
  40. * reloaded. */
  41. bitarray_t *countries;
  42. };
  43. /** Return a new empty routerset. */
  44. routerset_t *
  45. routerset_new(void)
  46. {
  47. routerset_t *result = tor_malloc_zero(sizeof(routerset_t));
  48. result->list = smartlist_new();
  49. result->names = strmap_new();
  50. result->digests = digestmap_new();
  51. result->policies = smartlist_new();
  52. result->country_names = smartlist_new();
  53. return result;
  54. }
  55. /** If <b>c</b> is a country code in the form {cc}, return a newly allocated
  56. * string holding the "cc" part. Else, return NULL. */
  57. static char *
  58. routerset_get_countryname(const char *c)
  59. {
  60. char *country;
  61. if (strlen(c) < 4 || c[0] !='{' || c[3] !='}')
  62. return NULL;
  63. country = tor_strndup(c+1, 2);
  64. tor_strlower(country);
  65. return country;
  66. }
  67. /** Update the routerset's <b>countries</b> bitarray_t. Called whenever
  68. * the GeoIP IPv4 database is reloaded.
  69. */
  70. void
  71. routerset_refresh_countries(routerset_t *target)
  72. {
  73. int cc;
  74. bitarray_free(target->countries);
  75. if (!geoip_is_loaded(AF_INET)) {
  76. target->countries = NULL;
  77. target->n_countries = 0;
  78. return;
  79. }
  80. target->n_countries = geoip_get_n_countries();
  81. target->countries = bitarray_init_zero(target->n_countries);
  82. SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) {
  83. cc = geoip_get_country(country);
  84. if (cc >= 0) {
  85. tor_assert(cc < target->n_countries);
  86. bitarray_set(target->countries, cc);
  87. } else {
  88. log_warn(LD_CONFIG, "Country code '%s' is not recognized.",
  89. country);
  90. }
  91. } SMARTLIST_FOREACH_END(country);
  92. }
  93. /** Parse the string <b>s</b> to create a set of routerset entries, and add
  94. * them to <b>target</b>. In log messages, refer to the string as
  95. * <b>description</b>. Return 0 on success, -1 on failure.
  96. *
  97. * Three kinds of elements are allowed in routersets: nicknames, IP address
  98. * patterns, and fingerprints. They may be surrounded by optional space, and
  99. * must be separated by commas.
  100. */
  101. int
  102. routerset_parse(routerset_t *target, const char *s, const char *description)
  103. {
  104. int r = 0;
  105. int added_countries = 0;
  106. char *countryname;
  107. smartlist_t *list = smartlist_new();
  108. smartlist_split_string(list, s, ",",
  109. SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0);
  110. SMARTLIST_FOREACH_BEGIN(list, char *, nick) {
  111. addr_policy_t *p;
  112. if (is_legal_hexdigest(nick)) {
  113. char d[DIGEST_LEN];
  114. if (*nick == '$')
  115. ++nick;
  116. log_debug(LD_CONFIG, "Adding identity %s to %s", nick, description);
  117. base16_decode(d, sizeof(d), nick, HEX_DIGEST_LEN);
  118. digestmap_set(target->digests, d, (void*)1);
  119. } else if (is_legal_nickname(nick)) {
  120. log_debug(LD_CONFIG, "Adding nickname %s to %s", nick, description);
  121. strmap_set_lc(target->names, nick, (void*)1);
  122. } else if ((countryname = routerset_get_countryname(nick)) != NULL) {
  123. log_debug(LD_CONFIG, "Adding country %s to %s", nick,
  124. description);
  125. smartlist_add(target->country_names, countryname);
  126. added_countries = 1;
  127. } else if ((strchr(nick,'.') || strchr(nick, '*')) &&
  128. (p = router_parse_addr_policy_item_from_string(
  129. nick, ADDR_POLICY_REJECT))) {
  130. log_debug(LD_CONFIG, "Adding address %s to %s", nick, description);
  131. smartlist_add(target->policies, p);
  132. } else {
  133. log_warn(LD_CONFIG, "Entry '%s' in %s is malformed.", nick,
  134. description);
  135. r = -1;
  136. tor_free(nick);
  137. SMARTLIST_DEL_CURRENT(list, nick);
  138. }
  139. } SMARTLIST_FOREACH_END(nick);
  140. policy_expand_unspec(&target->policies);
  141. smartlist_add_all(target->list, list);
  142. smartlist_free(list);
  143. if (added_countries)
  144. routerset_refresh_countries(target);
  145. return r;
  146. }
  147. /** Add all members of the set <b>source</b> to <b>target</b>. */
  148. void
  149. routerset_union(routerset_t *target, const routerset_t *source)
  150. {
  151. char *s;
  152. tor_assert(target);
  153. if (!source || !source->list)
  154. return;
  155. s = routerset_to_string(source);
  156. routerset_parse(target, s, "other routerset");
  157. tor_free(s);
  158. }
  159. /** Return true iff <b>set</b> lists only nicknames and digests, and includes
  160. * no IP ranges or countries. */
  161. int
  162. routerset_is_list(const routerset_t *set)
  163. {
  164. return smartlist_len(set->country_names) == 0 &&
  165. smartlist_len(set->policies) == 0;
  166. }
  167. /** Return true iff we need a GeoIP IP-to-country database to make sense of
  168. * <b>set</b>. */
  169. int
  170. routerset_needs_geoip(const routerset_t *set)
  171. {
  172. return set && smartlist_len(set->country_names);
  173. }
  174. /** Return true iff there are no entries in <b>set</b>. */
  175. int
  176. routerset_is_empty(const routerset_t *set)
  177. {
  178. return !set || smartlist_len(set->list) == 0;
  179. }
  180. /** Helper. Return true iff <b>set</b> contains a router based on the other
  181. * provided fields. Return higher values for more specific subentries: a
  182. * single router is more specific than an address range of routers, which is
  183. * more specific in turn than a country code.
  184. *
  185. * (If country is -1, then we take the country
  186. * from addr.) */
  187. static int
  188. routerset_contains(const routerset_t *set, const tor_addr_t *addr,
  189. uint16_t orport,
  190. const char *nickname, const char *id_digest,
  191. country_t country)
  192. {
  193. if (!set || !set->list)
  194. return 0;
  195. if (nickname && strmap_get_lc(set->names, nickname))
  196. return 4;
  197. if (id_digest && digestmap_get(set->digests, id_digest))
  198. return 4;
  199. if (addr && compare_tor_addr_to_addr_policy(addr, orport, set->policies)
  200. == ADDR_POLICY_REJECTED)
  201. return 3;
  202. if (set->countries) {
  203. if (country < 0 && addr)
  204. country = geoip_get_country_by_addr(addr);
  205. if (country >= 0 && country < set->n_countries &&
  206. bitarray_is_set(set->countries, country))
  207. return 2;
  208. }
  209. return 0;
  210. }
  211. /** If *<b>setp</b> includes at least one country code, or if
  212. * <b>only_some_cc_set</b> is 0, add the ?? and A1 country codes to
  213. * *<b>setp</b>, creating it as needed. Return true iff *<b>setp</b> changed.
  214. */
  215. int
  216. routerset_add_unknown_ccs(routerset_t **setp, int only_if_some_cc_set)
  217. {
  218. routerset_t *set;
  219. int add_unknown, add_a1;
  220. if (only_if_some_cc_set) {
  221. if (!*setp || smartlist_len((*setp)->country_names) == 0)
  222. return 0;
  223. }
  224. if (!*setp)
  225. *setp = routerset_new();
  226. set = *setp;
  227. add_unknown = ! smartlist_contains_string_case(set->country_names, "??") &&
  228. geoip_get_country("??") >= 0;
  229. add_a1 = ! smartlist_contains_string_case(set->country_names, "a1") &&
  230. geoip_get_country("A1") >= 0;
  231. if (add_unknown) {
  232. smartlist_add(set->country_names, tor_strdup("??"));
  233. smartlist_add(set->list, tor_strdup("{??}"));
  234. }
  235. if (add_a1) {
  236. smartlist_add(set->country_names, tor_strdup("a1"));
  237. smartlist_add(set->list, tor_strdup("{a1}"));
  238. }
  239. if (add_unknown || add_a1) {
  240. routerset_refresh_countries(set);
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. /** Return true iff we can tell that <b>ei</b> is a member of <b>set</b>. */
  246. int
  247. routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
  248. {
  249. return routerset_contains(set,
  250. &ei->addr,
  251. ei->port,
  252. ei->nickname,
  253. ei->identity_digest,
  254. -1 /*country*/);
  255. }
  256. /** Return true iff <b>ri</b> is in <b>set</b>. If country is <b>-1</b>, we
  257. * look up the country. */
  258. int
  259. routerset_contains_router(const routerset_t *set, const routerinfo_t *ri,
  260. country_t country)
  261. {
  262. tor_addr_t addr;
  263. tor_addr_from_ipv4h(&addr, ri->addr);
  264. return routerset_contains(set,
  265. &addr,
  266. ri->or_port,
  267. ri->nickname,
  268. ri->cache_info.identity_digest,
  269. country);
  270. }
  271. /** Return true iff <b>rs</b> is in <b>set</b>. If country is <b>-1</b>, we
  272. * look up the country. */
  273. int
  274. routerset_contains_routerstatus(const routerset_t *set,
  275. const routerstatus_t *rs,
  276. country_t country)
  277. {
  278. tor_addr_t addr;
  279. tor_addr_from_ipv4h(&addr, rs->addr);
  280. return routerset_contains(set,
  281. &addr,
  282. rs->or_port,
  283. rs->nickname,
  284. rs->identity_digest,
  285. country);
  286. }
  287. /** Return true iff <b>node</b> is in <b>set</b>. */
  288. int
  289. routerset_contains_node(const routerset_t *set, const node_t *node)
  290. {
  291. if (node->rs)
  292. return routerset_contains_routerstatus(set, node->rs, node->country);
  293. else if (node->ri)
  294. return routerset_contains_router(set, node->ri, node->country);
  295. else
  296. return 0;
  297. }
  298. /** Add every known node_t that is a member of <b>routerset</b> to
  299. * <b>out</b>, but never add any that are part of <b>excludeset</b>.
  300. * If <b>running_only</b>, only add the running ones. */
  301. void
  302. routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset,
  303. const routerset_t *excludeset, int running_only)
  304. {
  305. tor_assert(out);
  306. if (!routerset || !routerset->list)
  307. return;
  308. if (routerset_is_list(routerset)) {
  309. /* No routers are specified by type; all are given by name or digest.
  310. * we can do a lookup in O(len(routerset)). */
  311. SMARTLIST_FOREACH(routerset->list, const char *, name, {
  312. const node_t *node = node_get_by_nickname(name, 1);
  313. if (node) {
  314. if (!running_only || node->is_running)
  315. if (!routerset_contains_node(excludeset, node))
  316. smartlist_add(out, (void*)node);
  317. }
  318. });
  319. } else {
  320. /* We need to iterate over the routerlist to get all the ones of the
  321. * right kind. */
  322. smartlist_t *nodes = nodelist_get_list();
  323. SMARTLIST_FOREACH(nodes, const node_t *, node, {
  324. if (running_only && !node->is_running)
  325. continue;
  326. if (routerset_contains_node(routerset, node) &&
  327. !routerset_contains_node(excludeset, node))
  328. smartlist_add(out, (void*)node);
  329. });
  330. }
  331. }
  332. /** Remove every node_t from <b>lst</b> that is in <b>routerset</b>. */
  333. void
  334. routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
  335. {
  336. tor_assert(lst);
  337. if (!routerset)
  338. return;
  339. SMARTLIST_FOREACH(lst, const node_t *, node, {
  340. if (routerset_contains_node(routerset, node)) {
  341. //log_debug(LD_DIR, "Subtracting %s",r->nickname);
  342. SMARTLIST_DEL_CURRENT(lst, node);
  343. }
  344. });
  345. }
  346. /** Return a new string that when parsed by routerset_parse_string() will
  347. * yield <b>set</b>. */
  348. char *
  349. routerset_to_string(const routerset_t *set)
  350. {
  351. if (!set || !set->list)
  352. return tor_strdup("");
  353. return smartlist_join_strings(set->list, ",", 0, NULL);
  354. }
  355. /** Helper: return true iff old and new are both NULL, or both non-NULL
  356. * equal routersets. */
  357. int
  358. routerset_equal(const routerset_t *old, const routerset_t *new)
  359. {
  360. if (routerset_is_empty(old) && routerset_is_empty(new)) {
  361. /* Two empty sets are equal */
  362. return 1;
  363. } else if (routerset_is_empty(old) || routerset_is_empty(new)) {
  364. /* An empty set is equal to nothing else. */
  365. return 0;
  366. }
  367. tor_assert(old != NULL);
  368. tor_assert(new != NULL);
  369. if (smartlist_len(old->list) != smartlist_len(new->list))
  370. return 0;
  371. SMARTLIST_FOREACH(old->list, const char *, cp1, {
  372. const char *cp2 = smartlist_get(new->list, cp1_sl_idx);
  373. if (strcmp(cp1, cp2))
  374. return 0;
  375. });
  376. return 1;
  377. }
  378. /** Free all storage held in <b>routerset</b>. */
  379. void
  380. routerset_free(routerset_t *routerset)
  381. {
  382. if (!routerset)
  383. return;
  384. SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
  385. smartlist_free(routerset->list);
  386. SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
  387. addr_policy_free(p));
  388. smartlist_free(routerset->policies);
  389. SMARTLIST_FOREACH(routerset->country_names, char *, cp, tor_free(cp));
  390. smartlist_free(routerset->country_names);
  391. strmap_free(routerset->names, NULL);
  392. digestmap_free(routerset->digests, NULL);
  393. bitarray_free(routerset->countries);
  394. tor_free(routerset);
  395. }