routerset.c 13 KB

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