Merge remote-tracking branch 'public/bug23985_029' into maint-0.2.9
[tor.git] / src / or / routerset.c
blob58b66ea777343bb6167a3e9ff114f26e9fc50c65
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-2016, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file routerset.c
10 * \brief Functions and structures to handle set-type selection of routers
11 * by name, ID, address, etc.
13 * This module implements the routerset_t data structure, whose purpose
14 * is to specify a set of relays based on a list of their identities or
15 * properties. Routersets can restrict relays by IP address mask,
16 * identity fingerprint, country codes, and nicknames (deprecated).
18 * Routersets are typically used for user-specified restrictions, and
19 * are created by invoking routerset_new and routerset_parse from
20 * config.c and confparse.c. To use a routerset, invoke one of
21 * routerset_contains_...() functions , or use
22 * routerstatus_get_all_nodes() / routerstatus_subtract_nodes() to
23 * manipulate a smartlist of node_t pointers.
25 * Country-code restrictions are implemented in geoip.c.
28 #define ROUTERSET_PRIVATE
30 #include "or.h"
31 #include "geoip.h"
32 #include "nodelist.h"
33 #include "policies.h"
34 #include "router.h"
35 #include "routerparse.h"
36 #include "routerset.h"
38 /** Return a new empty routerset. */
39 routerset_t *
40 routerset_new(void)
42 routerset_t *result = tor_malloc_zero(sizeof(routerset_t));
43 result->list = smartlist_new();
44 result->names = strmap_new();
45 result->digests = digestmap_new();
46 result->policies = smartlist_new();
47 result->country_names = smartlist_new();
48 return result;
51 /** If <b>c</b> is a country code in the form {cc}, return a newly allocated
52 * string holding the "cc" part. Else, return NULL. */
53 STATIC char *
54 routerset_get_countryname(const char *c)
56 char *country;
58 if (strlen(c) < 4 || c[0] !='{' || c[3] !='}')
59 return NULL;
61 country = tor_strndup(c+1, 2);
62 tor_strlower(country);
63 return country;
66 /** Update the routerset's <b>countries</b> bitarray_t. Called whenever
67 * the GeoIP IPv4 database is reloaded.
69 void
70 routerset_refresh_countries(routerset_t *target)
72 int cc;
73 bitarray_free(target->countries);
75 if (!geoip_is_loaded(AF_INET)) {
76 target->countries = NULL;
77 target->n_countries = 0;
78 return;
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);
91 } SMARTLIST_FOREACH_END(country);
94 /** Parse the string <b>s</b> to create a set of routerset entries, and add
95 * them to <b>target</b>. In log messages, refer to the string as
96 * <b>description</b>. Return 0 on success, -1 on failure.
98 * Three kinds of elements are allowed in routersets: nicknames, IP address
99 * patterns, and fingerprints. They may be surrounded by optional space, and
100 * must be separated by commas.
103 routerset_parse(routerset_t *target, const char *s, const char *description)
105 int r = 0;
106 int added_countries = 0;
107 char *countryname;
108 smartlist_t *list = smartlist_new();
109 int malformed_list;
110 smartlist_split_string(list, s, ",",
111 SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0);
112 SMARTLIST_FOREACH_BEGIN(list, char *, nick) {
113 addr_policy_t *p;
114 /* if it doesn't pass our validation, assume it's malformed */
115 malformed_list = 1;
116 if (is_legal_hexdigest(nick)) {
117 char d[DIGEST_LEN];
118 if (*nick == '$')
119 ++nick;
120 log_debug(LD_CONFIG, "Adding identity %s to %s", nick, description);
121 base16_decode(d, sizeof(d), nick, HEX_DIGEST_LEN);
122 digestmap_set(target->digests, d, (void*)1);
123 } else if (is_legal_nickname(nick)) {
124 log_debug(LD_CONFIG, "Adding nickname %s to %s", nick, description);
125 strmap_set_lc(target->names, nick, (void*)1);
126 } else if ((countryname = routerset_get_countryname(nick)) != NULL) {
127 log_debug(LD_CONFIG, "Adding country %s to %s", nick,
128 description);
129 smartlist_add(target->country_names, countryname);
130 added_countries = 1;
131 } else if ((strchr(nick,'.') || strchr(nick, ':') || strchr(nick, '*'))
132 && (p = router_parse_addr_policy_item_from_string(
133 nick, ADDR_POLICY_REJECT,
134 &malformed_list))) {
135 /* IPv4 addresses contain '.', IPv6 addresses contain ':',
136 * and wildcard addresses contain '*'. */
137 log_debug(LD_CONFIG, "Adding address %s to %s", nick, description);
138 smartlist_add(target->policies, p);
139 } else if (malformed_list) {
140 log_warn(LD_CONFIG, "Entry '%s' in %s is malformed. Discarding entire"
141 " list.", nick, description);
142 r = -1;
143 tor_free(nick);
144 SMARTLIST_DEL_CURRENT(list, nick);
145 } else {
146 log_notice(LD_CONFIG, "Entry '%s' in %s is ignored. Using the"
147 " remainder of the list.", nick, description);
148 tor_free(nick);
149 SMARTLIST_DEL_CURRENT(list, nick);
151 } SMARTLIST_FOREACH_END(nick);
152 policy_expand_unspec(&target->policies);
153 smartlist_add_all(target->list, list);
154 smartlist_free(list);
155 if (added_countries)
156 routerset_refresh_countries(target);
157 return r;
160 /** Add all members of the set <b>source</b> to <b>target</b>. */
161 void
162 routerset_union(routerset_t *target, const routerset_t *source)
164 char *s;
165 tor_assert(target);
166 if (!source || !source->list)
167 return;
168 s = routerset_to_string(source);
169 routerset_parse(target, s, "other routerset");
170 tor_free(s);
173 /** Return true iff <b>set</b> lists only nicknames and digests, and includes
174 * no IP ranges or countries. */
176 routerset_is_list(const routerset_t *set)
178 return smartlist_len(set->country_names) == 0 &&
179 smartlist_len(set->policies) == 0;
182 /** Return true iff we need a GeoIP IP-to-country database to make sense of
183 * <b>set</b>. */
185 routerset_needs_geoip(const routerset_t *set)
187 return set && smartlist_len(set->country_names);
190 /** Return true iff there are no entries in <b>set</b>. */
192 routerset_is_empty(const routerset_t *set)
194 return !set || smartlist_len(set->list) == 0;
197 /** Return the number of entries in <b>set</b>. This does NOT return a
198 * negative value. */
200 routerset_len(const routerset_t *set)
202 if (!set) {
203 return 0;
205 return smartlist_len(set->list);
208 /** Helper. Return true iff <b>set</b> contains a router based on the other
209 * provided fields. Return higher values for more specific subentries: a
210 * single router is more specific than an address range of routers, which is
211 * more specific in turn than a country code.
213 * (If country is -1, then we take the country
214 * from addr.) */
215 STATIC int
216 routerset_contains(const routerset_t *set, const tor_addr_t *addr,
217 uint16_t orport,
218 const char *nickname, const char *id_digest,
219 country_t country)
221 if (!set || !set->list)
222 return 0;
223 if (nickname && strmap_get_lc(set->names, nickname))
224 return 4;
225 if (id_digest && digestmap_get(set->digests, id_digest))
226 return 4;
227 if (addr && compare_tor_addr_to_addr_policy(addr, orport, set->policies)
228 == ADDR_POLICY_REJECTED)
229 return 3;
230 if (set->countries) {
231 if (country < 0 && addr)
232 country = geoip_get_country_by_addr(addr);
234 if (country >= 0 && country < set->n_countries &&
235 bitarray_is_set(set->countries, country))
236 return 2;
238 return 0;
241 /** If *<b>setp</b> includes at least one country code, or if
242 * <b>only_some_cc_set</b> is 0, add the ?? and A1 country codes to
243 * *<b>setp</b>, creating it as needed. Return true iff *<b>setp</b> changed.
246 routerset_add_unknown_ccs(routerset_t **setp, int only_if_some_cc_set)
248 routerset_t *set;
249 int add_unknown, add_a1;
250 if (only_if_some_cc_set) {
251 if (!*setp || smartlist_len((*setp)->country_names) == 0)
252 return 0;
254 if (!*setp)
255 *setp = routerset_new();
257 set = *setp;
259 add_unknown = ! smartlist_contains_string_case(set->country_names, "??") &&
260 geoip_get_country("??") >= 0;
261 add_a1 = ! smartlist_contains_string_case(set->country_names, "a1") &&
262 geoip_get_country("A1") >= 0;
264 if (add_unknown) {
265 smartlist_add(set->country_names, tor_strdup("??"));
266 smartlist_add(set->list, tor_strdup("{??}"));
268 if (add_a1) {
269 smartlist_add(set->country_names, tor_strdup("a1"));
270 smartlist_add(set->list, tor_strdup("{a1}"));
273 if (add_unknown || add_a1) {
274 routerset_refresh_countries(set);
275 return 1;
277 return 0;
280 /** Return true iff we can tell that <b>ei</b> is a member of <b>set</b>. */
282 routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
284 return routerset_contains(set,
285 &ei->addr,
286 ei->port,
287 ei->nickname,
288 ei->identity_digest,
289 -1 /*country*/);
292 /** Return true iff <b>ri</b> is in <b>set</b>. If country is <b>-1</b>, we
293 * look up the country. */
295 routerset_contains_router(const routerset_t *set, const routerinfo_t *ri,
296 country_t country)
298 tor_addr_t addr;
299 tor_addr_from_ipv4h(&addr, ri->addr);
300 return routerset_contains(set,
301 &addr,
302 ri->or_port,
303 ri->nickname,
304 ri->cache_info.identity_digest,
305 country);
308 /** Return true iff <b>rs</b> is in <b>set</b>. If country is <b>-1</b>, we
309 * look up the country. */
311 routerset_contains_routerstatus(const routerset_t *set,
312 const routerstatus_t *rs,
313 country_t country)
315 tor_addr_t addr;
316 tor_addr_from_ipv4h(&addr, rs->addr);
317 return routerset_contains(set,
318 &addr,
319 rs->or_port,
320 rs->nickname,
321 rs->identity_digest,
322 country);
325 /** Return true iff <b>node</b> is in <b>set</b>. */
327 routerset_contains_node(const routerset_t *set, const node_t *node)
329 if (node->rs)
330 return routerset_contains_routerstatus(set, node->rs, node->country);
331 else if (node->ri)
332 return routerset_contains_router(set, node->ri, node->country);
333 else
334 return 0;
337 /** Add every known node_t that is a member of <b>routerset</b> to
338 * <b>out</b>, but never add any that are part of <b>excludeset</b>.
339 * If <b>running_only</b>, only add the running ones. */
340 void
341 routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset,
342 const routerset_t *excludeset, int running_only)
344 tor_assert(out);
345 if (!routerset || !routerset->list)
346 return;
348 if (routerset_is_list(routerset)) {
349 /* No routers are specified by type; all are given by name or digest.
350 * we can do a lookup in O(len(routerset)). */
351 SMARTLIST_FOREACH(routerset->list, const char *, name, {
352 const node_t *node = node_get_by_nickname(name, 1);
353 if (node) {
354 if (!running_only || node->is_running)
355 if (!routerset_contains_node(excludeset, node))
356 smartlist_add(out, (void*)node);
359 } else {
360 /* We need to iterate over the routerlist to get all the ones of the
361 * right kind. */
362 smartlist_t *nodes = nodelist_get_list();
363 SMARTLIST_FOREACH(nodes, const node_t *, node, {
364 if (running_only && !node->is_running)
365 continue;
366 if (routerset_contains_node(routerset, node) &&
367 !routerset_contains_node(excludeset, node))
368 smartlist_add(out, (void*)node);
373 /** Remove every node_t from <b>lst</b> that is in <b>routerset</b>. */
374 void
375 routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
377 tor_assert(lst);
378 if (!routerset)
379 return;
380 SMARTLIST_FOREACH(lst, const node_t *, node, {
381 if (routerset_contains_node(routerset, node)) {
382 //log_debug(LD_DIR, "Subtracting %s",r->nickname);
383 SMARTLIST_DEL_CURRENT(lst, node);
388 /** Return a new string that when parsed by routerset_parse_string() will
389 * yield <b>set</b>. */
390 char *
391 routerset_to_string(const routerset_t *set)
393 if (!set || !set->list)
394 return tor_strdup("");
395 return smartlist_join_strings(set->list, ",", 0, NULL);
398 /** Helper: return true iff old and new are both NULL, or both non-NULL
399 * equal routersets. */
401 routerset_equal(const routerset_t *old, const routerset_t *new)
403 if (routerset_is_empty(old) && routerset_is_empty(new)) {
404 /* Two empty sets are equal */
405 return 1;
406 } else if (routerset_is_empty(old) || routerset_is_empty(new)) {
407 /* An empty set is equal to nothing else. */
408 return 0;
410 tor_assert(old != NULL);
411 tor_assert(new != NULL);
413 if (smartlist_len(old->list) != smartlist_len(new->list))
414 return 0;
416 SMARTLIST_FOREACH(old->list, const char *, cp1, {
417 const char *cp2 = smartlist_get(new->list, cp1_sl_idx);
418 if (strcmp(cp1, cp2))
419 return 0;
422 return 1;
425 /** Free all storage held in <b>routerset</b>. */
426 void
427 routerset_free(routerset_t *routerset)
429 if (!routerset)
430 return;
432 SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
433 smartlist_free(routerset->list);
434 SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
435 addr_policy_free(p));
436 smartlist_free(routerset->policies);
437 SMARTLIST_FOREACH(routerset->country_names, char *, cp, tor_free(cp));
438 smartlist_free(routerset->country_names);
440 strmap_free(routerset->names, NULL);
441 digestmap_free(routerset->digests, NULL);
442 bitarray_free(routerset->countries);
443 tor_free(routerset);