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 */
12 #include "routerparse.h"
13 #include "routerset.h"
15 /** A routerset specifies constraints on a set of possible routerinfos, based
16 * on their names, identities, or addresses. It is optimized for determining
17 * whether a router is a member or not, in O(1+P) time, where P is the number
18 * of address policy constraints. */
20 /** A list of strings for the elements of the policy. Each string is either
21 * a nickname, a hexadecimal identity fingerprint, or an address policy. A
22 * router belongs to the set if its nickname OR its identity OR its address
23 * matches an entry here. */
25 /** A map from lowercase nicknames of routers in the set to (void*)1 */
27 /** A map from identity digests routers in the set to (void*)1 */
29 /** An address policy for routers in the set. For implementation reasons,
30 * a router belongs to the set if it is _rejected_ by this policy. */
31 smartlist_t
*policies
;
33 /** A human-readable description of what this routerset is for. Used in
37 /** A list of the country codes in this set. */
38 smartlist_t
*country_names
;
39 /** Total number of countries we knew about when we built <b>countries</b>.*/
41 /** Bit array mapping the return value of geoip_get_country() to 1 iff the
42 * country is a member of this routerset. Note that we MUST call
43 * routerset_refresh_countries() whenever the geoip country list is
45 bitarray_t
*countries
;
48 /** Return a new empty routerset. */
52 routerset_t
*result
= tor_malloc_zero(sizeof(routerset_t
));
53 result
->list
= smartlist_new();
54 result
->names
= strmap_new();
55 result
->digests
= digestmap_new();
56 result
->policies
= smartlist_new();
57 result
->country_names
= smartlist_new();
61 /** If <b>c</b> is a country code in the form {cc}, return a newly allocated
62 * string holding the "cc" part. Else, return NULL. */
64 routerset_get_countryname(const char *c
)
68 if (strlen(c
) < 4 || c
[0] !='{' || c
[3] !='}')
71 country
= tor_strndup(c
+1, 2);
72 tor_strlower(country
);
76 /** Update the routerset's <b>countries</b> bitarray_t. Called whenever
77 * the GeoIP IPv4 database is reloaded.
80 routerset_refresh_countries(routerset_t
*target
)
83 bitarray_free(target
->countries
);
85 if (!geoip_is_loaded(AF_INET
)) {
86 target
->countries
= NULL
;
87 target
->n_countries
= 0;
90 target
->n_countries
= geoip_get_n_countries();
91 target
->countries
= bitarray_init_zero(target
->n_countries
);
92 SMARTLIST_FOREACH_BEGIN(target
->country_names
, const char *, country
) {
93 cc
= geoip_get_country(country
);
95 tor_assert(cc
< target
->n_countries
);
96 bitarray_set(target
->countries
, cc
);
98 log_warn(LD_CONFIG
, "Country code '%s' is not recognized.",
101 } SMARTLIST_FOREACH_END(country
);
104 /** Parse the string <b>s</b> to create a set of routerset entries, and add
105 * them to <b>target</b>. In log messages, refer to the string as
106 * <b>description</b>. Return 0 on success, -1 on failure.
108 * Three kinds of elements are allowed in routersets: nicknames, IP address
109 * patterns, and fingerprints. They may be surrounded by optional space, and
110 * must be separated by commas.
113 routerset_parse(routerset_t
*target
, const char *s
, const char *description
)
116 int added_countries
= 0;
118 smartlist_t
*list
= smartlist_new();
119 smartlist_split_string(list
, s
, ",",
120 SPLIT_SKIP_SPACE
| SPLIT_IGNORE_BLANK
, 0);
121 SMARTLIST_FOREACH_BEGIN(list
, char *, nick
) {
123 if (is_legal_hexdigest(nick
)) {
127 log_debug(LD_CONFIG
, "Adding identity %s to %s", nick
, description
);
128 base16_decode(d
, sizeof(d
), nick
, HEX_DIGEST_LEN
);
129 digestmap_set(target
->digests
, d
, (void*)1);
130 } else if (is_legal_nickname(nick
)) {
131 log_debug(LD_CONFIG
, "Adding nickname %s to %s", nick
, description
);
132 strmap_set_lc(target
->names
, nick
, (void*)1);
133 } else if ((countryname
= routerset_get_countryname(nick
)) != NULL
) {
134 log_debug(LD_CONFIG
, "Adding country %s to %s", nick
,
136 smartlist_add(target
->country_names
, countryname
);
138 } else if ((strchr(nick
,'.') || strchr(nick
, '*')) &&
139 (p
= router_parse_addr_policy_item_from_string(
140 nick
, ADDR_POLICY_REJECT
))) {
141 log_debug(LD_CONFIG
, "Adding address %s to %s", nick
, description
);
142 smartlist_add(target
->policies
, p
);
144 log_warn(LD_CONFIG
, "Entry '%s' in %s is malformed.", nick
,
148 SMARTLIST_DEL_CURRENT(list
, nick
);
150 } SMARTLIST_FOREACH_END(nick
);
151 policy_expand_unspec(&target
->policies
);
152 smartlist_add_all(target
->list
, list
);
153 smartlist_free(list
);
155 routerset_refresh_countries(target
);
159 /** Add all members of the set <b>source</b> to <b>target</b>. */
161 routerset_union(routerset_t
*target
, const routerset_t
*source
)
165 if (!source
|| !source
->list
)
167 s
= routerset_to_string(source
);
168 routerset_parse(target
, s
, "other routerset");
172 /** Return true iff <b>set</b> lists only nicknames and digests, and includes
173 * no IP ranges or countries. */
175 routerset_is_list(const routerset_t
*set
)
177 return smartlist_len(set
->country_names
) == 0 &&
178 smartlist_len(set
->policies
) == 0;
181 /** Return true iff we need a GeoIP IP-to-country database to make sense of
184 routerset_needs_geoip(const routerset_t
*set
)
186 return set
&& smartlist_len(set
->country_names
);
189 /** Return true iff there are no entries in <b>set</b>. */
191 routerset_is_empty(const routerset_t
*set
)
193 return !set
|| smartlist_len(set
->list
) == 0;
196 /** Helper. Return true iff <b>set</b> contains a router based on the other
197 * provided fields. Return higher values for more specific subentries: a
198 * single router is more specific than an address range of routers, which is
199 * more specific in turn than a country code.
201 * (If country is -1, then we take the country
204 routerset_contains(const routerset_t
*set
, const tor_addr_t
*addr
,
206 const char *nickname
, const char *id_digest
,
209 if (!set
|| !set
->list
)
211 if (nickname
&& strmap_get_lc(set
->names
, nickname
))
213 if (id_digest
&& digestmap_get(set
->digests
, id_digest
))
215 if (addr
&& compare_tor_addr_to_addr_policy(addr
, orport
, set
->policies
)
216 == ADDR_POLICY_REJECTED
)
218 if (set
->countries
) {
219 if (country
< 0 && addr
)
220 country
= geoip_get_country_by_addr(addr
);
222 if (country
>= 0 && country
< set
->n_countries
&&
223 bitarray_is_set(set
->countries
, country
))
229 /** If *<b>setp</b> includes at least one country code, or if
230 * <b>only_some_cc_set</b> is 0, add the ?? and A1 country codes to
231 * *<b>setp</b>, creating it as needed. Return true iff *<b>setp</b> changed.
234 routerset_add_unknown_ccs(routerset_t
**setp
, int only_if_some_cc_set
)
237 int add_unknown
, add_a1
;
238 if (only_if_some_cc_set
) {
239 if (!*setp
|| smartlist_len((*setp
)->country_names
) == 0)
243 *setp
= routerset_new();
247 add_unknown
= ! smartlist_contains_string_case(set
->country_names
, "??") &&
248 geoip_get_country("??") >= 0;
249 add_a1
= ! smartlist_contains_string_case(set
->country_names
, "a1") &&
250 geoip_get_country("A1") >= 0;
253 smartlist_add(set
->country_names
, tor_strdup("??"));
254 smartlist_add(set
->list
, tor_strdup("{??}"));
257 smartlist_add(set
->country_names
, tor_strdup("a1"));
258 smartlist_add(set
->list
, tor_strdup("{a1}"));
261 if (add_unknown
|| add_a1
) {
262 routerset_refresh_countries(set
);
268 /** Return true iff we can tell that <b>ei</b> is a member of <b>set</b>. */
270 routerset_contains_extendinfo(const routerset_t
*set
, const extend_info_t
*ei
)
272 return routerset_contains(set
,
280 /** Return true iff <b>ri</b> is in <b>set</b>. If country is <b>-1</b>, we
281 * look up the country. */
283 routerset_contains_router(const routerset_t
*set
, const routerinfo_t
*ri
,
287 tor_addr_from_ipv4h(&addr
, ri
->addr
);
288 return routerset_contains(set
,
292 ri
->cache_info
.identity_digest
,
296 /** Return true iff <b>rs</b> is in <b>set</b>. If country is <b>-1</b>, we
297 * look up the country. */
299 routerset_contains_routerstatus(const routerset_t
*set
,
300 const routerstatus_t
*rs
,
304 tor_addr_from_ipv4h(&addr
, rs
->addr
);
305 return routerset_contains(set
,
313 /** Return true iff <b>node</b> is in <b>set</b>. */
315 routerset_contains_node(const routerset_t
*set
, const node_t
*node
)
318 return routerset_contains_routerstatus(set
, node
->rs
, node
->country
);
320 return routerset_contains_router(set
, node
->ri
, node
->country
);
325 /** Add every known node_t that is a member of <b>routerset</b> to
326 * <b>out</b>, but never add any that are part of <b>excludeset</b>.
327 * If <b>running_only</b>, only add the running ones. */
329 routerset_get_all_nodes(smartlist_t
*out
, const routerset_t
*routerset
,
330 const routerset_t
*excludeset
, int running_only
)
333 if (!routerset
|| !routerset
->list
)
336 if (routerset_is_list(routerset
)) {
337 /* No routers are specified by type; all are given by name or digest.
338 * we can do a lookup in O(len(routerset)). */
339 SMARTLIST_FOREACH(routerset
->list
, const char *, name
, {
340 const node_t
*node
= node_get_by_nickname(name
, 1);
342 if (!running_only
|| node
->is_running
)
343 if (!routerset_contains_node(excludeset
, node
))
344 smartlist_add(out
, (void*)node
);
348 /* We need to iterate over the routerlist to get all the ones of the
350 smartlist_t
*nodes
= nodelist_get_list();
351 SMARTLIST_FOREACH(nodes
, const node_t
*, node
, {
352 if (running_only
&& !node
->is_running
)
354 if (routerset_contains_node(routerset
, node
) &&
355 !routerset_contains_node(excludeset
, node
))
356 smartlist_add(out
, (void*)node
);
361 /** Remove every node_t from <b>lst</b> that is in <b>routerset</b>. */
363 routerset_subtract_nodes(smartlist_t
*lst
, const routerset_t
*routerset
)
368 SMARTLIST_FOREACH(lst
, const node_t
*, node
, {
369 if (routerset_contains_node(routerset
, node
)) {
370 //log_debug(LD_DIR, "Subtracting %s",r->nickname);
371 SMARTLIST_DEL_CURRENT(lst
, node
);
376 /** Return a new string that when parsed by routerset_parse_string() will
377 * yield <b>set</b>. */
379 routerset_to_string(const routerset_t
*set
)
381 if (!set
|| !set
->list
)
382 return tor_strdup("");
383 return smartlist_join_strings(set
->list
, ",", 0, NULL
);
386 /** Helper: return true iff old and new are both NULL, or both non-NULL
387 * equal routersets. */
389 routerset_equal(const routerset_t
*old
, const routerset_t
*new)
391 if (routerset_is_empty(old
) && routerset_is_empty(new)) {
392 /* Two empty sets are equal */
394 } else if (routerset_is_empty(old
) || routerset_is_empty(new)) {
395 /* An empty set is equal to nothing else. */
398 tor_assert(old
!= NULL
);
399 tor_assert(new != NULL
);
401 if (smartlist_len(old
->list
) != smartlist_len(new->list
))
404 SMARTLIST_FOREACH(old
->list
, const char *, cp1
, {
405 const char *cp2
= smartlist_get(new->list
, cp1_sl_idx
);
406 if (strcmp(cp1
, cp2
))
413 /** Free all storage held in <b>routerset</b>. */
415 routerset_free(routerset_t
*routerset
)
420 SMARTLIST_FOREACH(routerset
->list
, char *, cp
, tor_free(cp
));
421 smartlist_free(routerset
->list
);
422 SMARTLIST_FOREACH(routerset
->policies
, addr_policy_t
*, p
,
423 addr_policy_free(p
));
424 smartlist_free(routerset
->policies
);
425 SMARTLIST_FOREACH(routerset
->country_names
, char *, cp
, tor_free(cp
));
426 smartlist_free(routerset
->country_names
);
428 strmap_free(routerset
->names
, NULL
);
429 digestmap_free(routerset
->digests
, NULL
);
430 bitarray_free(routerset
->countries
);