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 */
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
35 #include "routerparse.h"
36 #include "routerset.h"
38 /** Return a new empty routerset. */
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();
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. */
54 routerset_get_countryname(const char *c
)
58 if (strlen(c
) < 4 || c
[0] !='{' || c
[3] !='}')
61 country
= tor_strndup(c
+1, 2);
62 tor_strlower(country
);
66 /** Update the routerset's <b>countries</b> bitarray_t. Called whenever
67 * the GeoIP IPv4 database is reloaded.
70 routerset_refresh_countries(routerset_t
*target
)
73 bitarray_free(target
->countries
);
75 if (!geoip_is_loaded(AF_INET
)) {
76 target
->countries
= NULL
;
77 target
->n_countries
= 0;
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
);
85 tor_assert(cc
< target
->n_countries
);
86 bitarray_set(target
->countries
, cc
);
88 log_warn(LD_CONFIG
, "Country code '%s' is not recognized.",
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
)
106 int added_countries
= 0;
108 smartlist_t
*list
= smartlist_new();
110 smartlist_split_string(list
, s
, ",",
111 SPLIT_SKIP_SPACE
| SPLIT_IGNORE_BLANK
, 0);
112 SMARTLIST_FOREACH_BEGIN(list
, char *, nick
) {
114 /* if it doesn't pass our validation, assume it's malformed */
116 if (is_legal_hexdigest(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
,
129 smartlist_add(target
->country_names
, countryname
);
131 } else if ((strchr(nick
,'.') || strchr(nick
, ':') || strchr(nick
, '*'))
132 && (p
= router_parse_addr_policy_item_from_string(
133 nick
, ADDR_POLICY_REJECT
,
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
);
144 SMARTLIST_DEL_CURRENT(list
, nick
);
146 log_notice(LD_CONFIG
, "Entry '%s' in %s is ignored. Using the"
147 " remainder of the list.", nick
, description
);
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
);
156 routerset_refresh_countries(target
);
160 /** Add all members of the set <b>source</b> to <b>target</b>. */
162 routerset_union(routerset_t
*target
, const routerset_t
*source
)
166 if (!source
|| !source
->list
)
168 s
= routerset_to_string(source
);
169 routerset_parse(target
, s
, "other routerset");
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
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
200 routerset_len(const routerset_t
*set
)
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
216 routerset_contains(const routerset_t
*set
, const tor_addr_t
*addr
,
218 const char *nickname
, const char *id_digest
,
221 if (!set
|| !set
->list
)
223 if (nickname
&& strmap_get_lc(set
->names
, nickname
))
225 if (id_digest
&& digestmap_get(set
->digests
, id_digest
))
227 if (addr
&& compare_tor_addr_to_addr_policy(addr
, orport
, set
->policies
)
228 == ADDR_POLICY_REJECTED
)
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
))
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
)
249 int add_unknown
, add_a1
;
250 if (only_if_some_cc_set
) {
251 if (!*setp
|| smartlist_len((*setp
)->country_names
) == 0)
255 *setp
= routerset_new();
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;
265 smartlist_add(set
->country_names
, tor_strdup("??"));
266 smartlist_add(set
->list
, tor_strdup("{??}"));
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
);
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
,
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
,
299 tor_addr_from_ipv4h(&addr
, ri
->addr
);
300 return routerset_contains(set
,
304 ri
->cache_info
.identity_digest
,
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
,
316 tor_addr_from_ipv4h(&addr
, rs
->addr
);
317 return routerset_contains(set
,
325 /** Return true iff <b>node</b> is in <b>set</b>. */
327 routerset_contains_node(const routerset_t
*set
, const node_t
*node
)
330 return routerset_contains_routerstatus(set
, node
->rs
, node
->country
);
332 return routerset_contains_router(set
, node
->ri
, node
->country
);
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. */
341 routerset_get_all_nodes(smartlist_t
*out
, const routerset_t
*routerset
,
342 const routerset_t
*excludeset
, int running_only
)
345 if (!routerset
|| !routerset
->list
)
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);
354 if (!running_only
|| node
->is_running
)
355 if (!routerset_contains_node(excludeset
, node
))
356 smartlist_add(out
, (void*)node
);
360 /* We need to iterate over the routerlist to get all the ones of the
362 smartlist_t
*nodes
= nodelist_get_list();
363 SMARTLIST_FOREACH(nodes
, const node_t
*, node
, {
364 if (running_only
&& !node
->is_running
)
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>. */
375 routerset_subtract_nodes(smartlist_t
*lst
, const routerset_t
*routerset
)
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>. */
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 */
406 } else if (routerset_is_empty(old
) || routerset_is_empty(new)) {
407 /* An empty set is equal to nothing else. */
410 tor_assert(old
!= NULL
);
411 tor_assert(new != NULL
);
413 if (smartlist_len(old
->list
) != smartlist_len(new->list
))
416 SMARTLIST_FOREACH(old
->list
, const char *, cp1
, {
417 const char *cp2
= smartlist_get(new->list
, cp1_sl_idx
);
418 if (strcmp(cp1
, cp2
))
425 /** Free all storage held in <b>routerset</b>. */
427 routerset_free(routerset_t
*routerset
)
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
);