routerset.c 13 KB

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