tor  0.4.1.0-alpha-dev
routerset.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2 n * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
28 #define ROUTERSET_PRIVATE
29 
30 #include "core/or/or.h"
31 #include "core/or/policies.h"
32 #include "feature/client/bridges.h"
36 #include "feature/nodelist/routerset.h"
37 #include "lib/geoip/geoip.h"
38 
39 #include "core/or/addr_policy_st.h"
40 #include "core/or/extend_info_st.h"
41 #include "feature/nodelist/node_st.h"
42 #include "feature/nodelist/routerinfo_st.h"
43 #include "feature/nodelist/routerstatus_st.h"
44 
46 routerset_t *
48 {
49  routerset_t *result = tor_malloc_zero(sizeof(routerset_t));
50  result->list = smartlist_new();
51  result->names = strmap_new();
52  result->digests = digestmap_new();
53  result->policies = smartlist_new();
54  result->country_names = smartlist_new();
55  return result;
56 }
57 
60 STATIC char *
62 {
63  char *country;
64 
65  if (strlen(c) < 4 || c[0] !='{' || c[3] !='}')
66  return NULL;
67 
68  country = tor_strndup(c+1, 2);
69  tor_strlower(country);
70  return country;
71 }
72 
76 void
77 routerset_refresh_countries(routerset_t *target)
78 {
79  int cc;
80  bitarray_free(target->countries);
81 
82  if (!geoip_is_loaded(AF_INET)) {
83  target->countries = NULL;
84  target->n_countries = 0;
85  return;
86  }
87  target->n_countries = geoip_get_n_countries();
88  target->countries = bitarray_init_zero(target->n_countries);
89  SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) {
90  cc = geoip_get_country(country);
91  if (cc >= 0) {
92  tor_assert(cc < target->n_countries);
93  bitarray_set(target->countries, cc);
94  } else {
95  log_warn(LD_CONFIG, "Country code '%s' is not recognized.",
96  country);
97  }
98  } SMARTLIST_FOREACH_END(country);
99 }
100 
109 int
110 routerset_parse(routerset_t *target, const char *s, const char *description)
111 {
112  int r = 0;
113  int added_countries = 0;
114  char *countryname;
115  smartlist_t *list = smartlist_new();
116  int malformed_list;
117  smartlist_split_string(list, s, ",",
118  SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0);
119  SMARTLIST_FOREACH_BEGIN(list, char *, nick) {
120  addr_policy_t *p;
121  /* if it doesn't pass our validation, assume it's malformed */
122  malformed_list = 1;
123  if (is_legal_hexdigest(nick)) {
124  char d[DIGEST_LEN];
125  if (*nick == '$')
126  ++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,
135  description);
136  smartlist_add(target->country_names, countryname);
137  added_countries = 1;
138  } else if ((strchr(nick,'.') || strchr(nick, ':') || strchr(nick, '*'))
139  && (p = router_parse_addr_policy_item_from_string(
140  nick, ADDR_POLICY_REJECT,
141  &malformed_list))) {
142  /* IPv4 addresses contain '.', IPv6 addresses contain ':',
143  * and wildcard addresses contain '*'. */
144  log_debug(LD_CONFIG, "Adding address %s to %s", nick, description);
145  smartlist_add(target->policies, p);
146  } else if (malformed_list) {
147  log_warn(LD_CONFIG, "Entry '%s' in %s is malformed. Discarding entire"
148  " list.", nick, description);
149  r = -1;
150  tor_free(nick);
151  SMARTLIST_DEL_CURRENT(list, nick);
152  } else {
153  log_notice(LD_CONFIG, "Entry '%s' in %s is ignored. Using the"
154  " remainder of the list.", nick, description);
155  tor_free(nick);
156  SMARTLIST_DEL_CURRENT(list, nick);
157  }
158  } SMARTLIST_FOREACH_END(nick);
159  policy_expand_unspec(&target->policies);
160  smartlist_add_all(target->list, list);
161  smartlist_free(list);
162  if (added_countries)
164  return r;
165 }
166 
168 void
169 routerset_union(routerset_t *target, const routerset_t *source)
170 {
171  char *s;
172  tor_assert(target);
173  if (!source || !source->list)
174  return;
175  s = routerset_to_string(source);
176  routerset_parse(target, s, "other routerset");
177  tor_free(s);
178 }
179 
182 int
183 routerset_is_list(const routerset_t *set)
184 {
185  return smartlist_len(set->country_names) == 0 &&
186  smartlist_len(set->policies) == 0;
187 }
188 
191 int
192 routerset_needs_geoip(const routerset_t *set)
193 {
194  return set && smartlist_len(set->country_names);
195 }
196 
198 int
199 routerset_is_empty(const routerset_t *set)
200 {
201  return !set || smartlist_len(set->list) == 0;
202 }
203 
206 int
207 routerset_len(const routerset_t *set)
208 {
209  if (!set) {
210  return 0;
211  }
212  return smartlist_len(set->list);
213 }
214 
222 STATIC int
223 routerset_contains(const routerset_t *set, const tor_addr_t *addr,
224  uint16_t orport,
225  const char *nickname, const char *id_digest,
226  country_t country)
227 {
228  if (!set || !set->list)
229  return 0;
230  if (nickname && strmap_get_lc(set->names, nickname))
231  return 4;
232  if (id_digest && digestmap_get(set->digests, id_digest))
233  return 4;
234  if (addr && compare_tor_addr_to_addr_policy(addr, orport, set->policies)
236  return 3;
237  if (set->countries) {
238  if (country < 0 && addr)
239  country = geoip_get_country_by_addr(addr);
240 
241  if (country >= 0 && country < set->n_countries &&
242  bitarray_is_set(set->countries, country))
243  return 2;
244  }
245  return 0;
246 }
247 
252 int
253 routerset_add_unknown_ccs(routerset_t **setp, int only_if_some_cc_set)
254 {
255  routerset_t *set;
256  int add_unknown, add_a1;
257  if (only_if_some_cc_set) {
258  if (!*setp || smartlist_len((*setp)->country_names) == 0)
259  return 0;
260  }
261  if (!*setp)
262  *setp = routerset_new();
263 
264  set = *setp;
265 
266  add_unknown = ! smartlist_contains_string_case(set->country_names, "??") &&
267  geoip_get_country("??") >= 0;
268  add_a1 = ! smartlist_contains_string_case(set->country_names, "a1") &&
269  geoip_get_country("A1") >= 0;
270 
271  if (add_unknown) {
272  smartlist_add_strdup(set->country_names, "??");
273  smartlist_add_strdup(set->list, "{??}");
274  }
275  if (add_a1) {
276  smartlist_add_strdup(set->country_names, "a1");
277  smartlist_add_strdup(set->list, "{a1}");
278  }
279 
280  if (add_unknown || add_a1) {
282  return 1;
283  }
284  return 0;
285 }
286 
288 int
289 routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
290 {
291  return routerset_contains(set,
292  &ei->addr,
293  ei->port,
294  ei->nickname,
295  ei->identity_digest,
296  -1 /*country*/);
297 }
298 
301 int
302 routerset_contains_router(const routerset_t *set, const routerinfo_t *ri,
303  country_t country)
304 {
305  tor_addr_t addr;
306  tor_addr_from_ipv4h(&addr, ri->addr);
307  return routerset_contains(set,
308  &addr,
309  ri->or_port,
310  ri->nickname,
311  ri->cache_info.identity_digest,
312  country);
313 }
314 
317 int
318 routerset_contains_routerstatus(const routerset_t *set,
319  const routerstatus_t *rs,
320  country_t country)
321 {
322  tor_addr_t addr;
323  tor_addr_from_ipv4h(&addr, rs->addr);
324  return routerset_contains(set,
325  &addr,
326  rs->or_port,
327  rs->nickname,
328  rs->identity_digest,
329  country);
330 }
331 
333 int
334 routerset_contains_node(const routerset_t *set, const node_t *node)
335 {
336  if (node->rs)
337  return routerset_contains_routerstatus(set, node->rs, node->country);
338  else if (node->ri)
339  return routerset_contains_router(set, node->ri, node->country);
340  else
341  return 0;
342 }
343 
345 int
346 routerset_contains_bridge(const routerset_t *set, const bridge_info_t *bridge)
347 {
348  const char *id = (const char*)bridge_get_rsa_id_digest(bridge);
349  const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
350 
351  tor_assert(addrport);
352  return routerset_contains(set, &addrport->addr, addrport->port,
353  NULL, id, -1);
354 }
355 
359 void
360 routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset,
361  const routerset_t *excludeset, int running_only)
362 {
363  tor_assert(out);
364  if (!routerset || !routerset->list)
365  return;
366 
367  if (routerset_is_list(routerset)) {
368  /* No routers are specified by type; all are given by name or digest.
369  * we can do a lookup in O(len(routerset)). */
370  SMARTLIST_FOREACH(routerset->list, const char *, name, {
371  const node_t *node = node_get_by_nickname(name, 0);
372  if (node) {
373  if (!running_only || node->is_running)
374  if (!routerset_contains_node(excludeset, node))
375  smartlist_add(out, (void*)node);
376  }
377  });
378  } else {
379  /* We need to iterate over the routerlist to get all the ones of the
380  * right kind. */
381  const smartlist_t *nodes = nodelist_get_list();
382  SMARTLIST_FOREACH(nodes, const node_t *, node, {
383  if (running_only && !node->is_running)
384  continue;
385  if (routerset_contains_node(routerset, node) &&
386  !routerset_contains_node(excludeset, node))
387  smartlist_add(out, (void*)node);
388  });
389  }
390 }
391 
393 void
394 routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
395 {
396  tor_assert(lst);
397  if (!routerset)
398  return;
399  SMARTLIST_FOREACH(lst, const node_t *, node, {
400  if (routerset_contains_node(routerset, node)) {
401  //log_debug(LD_DIR, "Subtracting %s",r->nickname);
402  SMARTLIST_DEL_CURRENT(lst, node);
403  }
404  });
405 }
406 
409 char *
410 routerset_to_string(const routerset_t *set)
411 {
412  if (!set || !set->list)
413  return tor_strdup("");
414  return smartlist_join_strings(set->list, ",", 0, NULL);
415 }
416 
419 int
420 routerset_equal(const routerset_t *old, const routerset_t *new)
421 {
422  if (routerset_is_empty(old) && routerset_is_empty(new)) {
423  /* Two empty sets are equal */
424  return 1;
425  } else if (routerset_is_empty(old) || routerset_is_empty(new)) {
426  /* An empty set is equal to nothing else. */
427  return 0;
428  }
429  tor_assert(old != NULL);
430  tor_assert(new != NULL);
431 
432  if (smartlist_len(old->list) != smartlist_len(new->list))
433  return 0;
434 
435  SMARTLIST_FOREACH(old->list, const char *, cp1, {
436  const char *cp2 = smartlist_get(new->list, cp1_sl_idx);
437  if (strcmp(cp1, cp2))
438  return 0;
439  });
440 
441  return 1;
442 }
443 
445 void
446 routerset_free_(routerset_t *routerset)
447 {
448  if (!routerset)
449  return;
450 
451  SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
452  smartlist_free(routerset->list);
453  SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
454  addr_policy_free(p));
455  smartlist_free(routerset->policies);
456  SMARTLIST_FOREACH(routerset->country_names, char *, cp, tor_free(cp));
457  smartlist_free(routerset->country_names);
458 
459  strmap_free(routerset->names, NULL);
460  digestmap_free(routerset->digests, NULL);
461  bitarray_free(routerset->countries);
462  tor_free(routerset);
463 }
void routerset_union(routerset_t *target, const routerset_t *source)
Definition: routerset.c:169
int routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
Definition: routerset.c:289
void * strmap_get_lc(const strmap_t *map, const char *key)
Definition: map.c:393
Definition: node_st.h:28
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
int routerset_contains_node(const routerset_t *set, const node_t *node)
Definition: routerset.c:334
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
char identity_digest[DIGEST_LEN]
Header file for nodelist.c.
const uint8_t * bridge_get_rsa_id_digest(const bridge_info_t *bridge)
Definition: bridges.c:147
void smartlist_add(smartlist_t *sl, void *element)
Header file for nickname.c.
int routerset_is_list(const routerset_t *set)
Definition: routerset.c:183
#define tor_free(p)
Definition: malloc.h:52
#define SMARTLIST_DEL_CURRENT(sl, var)
void * strmap_set_lc(strmap_t *map, const char *key, void *val)
Definition: map.c:379
void routerset_refresh_countries(routerset_t *target)
Definition: routerset.c:77
Header file for geoip.c.
static void bitarray_set(bitarray_t *b, int bit)
Definition: bitarray.h:68
void policy_expand_unspec(smartlist_t **policy)
Definition: policies.c:145
void routerset_free_(routerset_t *routerset)
Definition: routerset.c:446
Header file for policies.c.
STATIC int routerset_contains(const routerset_t *set, const tor_addr_t *addr, uint16_t orport, const char *nickname, const char *id_digest, country_t country)
Definition: routerset.c:223
char * routerset_to_string(const routerset_t *set)
Definition: routerset.c:410
int is_legal_hexdigest(const char *s)
Definition: nickname.c:45
tor_assert(buffer)
#define tor_addr_from_ipv4h(dest, v4addr)
Definition: address.h:287
Header file for policy_parse.c.
char nickname[MAX_HEX_NICKNAME_LEN+1]
#define DIGEST_LEN
Definition: digest_sizes.h:20
int smartlist_contains_string_case(const smartlist_t *sl, const char *element)
Definition: smartlist.c:133
int routerset_contains_router(const routerset_t *set, const routerinfo_t *ri, country_t country)
Definition: routerset.c:302
Header file for circuitbuild.c.
Master header file for Tor-specific functionality.
static bitarray_t * bitarray_init_zero(unsigned int n_bits)
Definition: bitarray.h:33
char nickname[MAX_NICKNAME_LEN+1]
void tor_strlower(char *s)
Definition: util_string.c:127
int routerset_add_unknown_ccs(routerset_t **setp, int only_if_some_cc_set)
Definition: routerset.c:253
int routerset_needs_geoip(const routerset_t *set)
Definition: routerset.c:192
int routerset_parse(routerset_t *target, const char *s, const char *description)
Definition: routerset.c:110
int is_legal_nickname(const char *s)
Definition: nickname.c:19
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
country_t country
Definition: node_st.h:87
char identity_digest[DIGEST_LEN]
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
char identity_digest[DIGEST_LEN]
#define SMARTLIST_FOREACH(sl, type, var, cmd)
int routerset_len(const routerset_t *set)
Definition: routerset.c:207
void routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
Definition: routerset.c:394
char * nickname
Definition: routerinfo_st.h:17
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
routerset_t * routerset_new(void)
Definition: routerset.c:47
int routerset_equal(const routerset_t *old, const routerset_t *new)
Definition: routerset.c:420
uint32_t addr
Definition: routerinfo_st.h:19
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:506
const tor_addr_port_t * bridge_get_addr_port(const bridge_info_t *bridge)
Definition: bridges.c:161
void routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset, const routerset_t *excludeset, int running_only)
Definition: routerset.c:360
int routerset_contains_routerstatus(const routerset_t *set, const routerstatus_t *rs, country_t country)
Definition: routerset.c:318
tor_addr_t addr
STATIC char * routerset_get_countryname(const char *c)
Definition: routerset.c:61
static unsigned int bitarray_is_set(bitarray_t *b, int bit)
Definition: bitarray.h:81
int routerset_contains_bridge(const routerset_t *set, const bridge_info_t *bridge)
Definition: routerset.c:346
#define LD_CONFIG
Definition: log.h:65
int routerset_is_empty(const routerset_t *set)
Definition: routerset.c:199
uint16_t or_port
Definition: routerinfo_st.h:20
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)