tor  0.4.2.1-alpha-dev
routerset.c
Go to the documentation of this file.
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-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/conf/conftypes.h"
38 #include "lib/confmgt/typedvar.h"
39 #include "lib/encoding/confline.h"
40 #include "lib/geoip/geoip.h"
41 
42 #include "core/or/addr_policy_st.h"
43 #include "core/or/extend_info_st.h"
44 #include "feature/nodelist/node_st.h"
45 #include "feature/nodelist/routerinfo_st.h"
46 #include "feature/nodelist/routerstatus_st.h"
48 
50 routerset_t *
52 {
53  routerset_t *result = tor_malloc_zero(sizeof(routerset_t));
54  result->list = smartlist_new();
55  result->names = strmap_new();
56  result->digests = digestmap_new();
57  result->policies = smartlist_new();
58  result->country_names = smartlist_new();
59  return result;
60 }
61 
64 STATIC char *
66 {
67  char *country;
68 
69  if (strlen(c) < 4 || c[0] !='{' || c[3] !='}')
70  return NULL;
71 
72  country = tor_strndup(c+1, 2);
73  tor_strlower(country);
74  return country;
75 }
76 
80 void
81 routerset_refresh_countries(routerset_t *target)
82 {
83  int cc;
84  bitarray_free(target->countries);
85 
86  if (!geoip_is_loaded(AF_INET)) {
87  target->countries = NULL;
88  target->n_countries = 0;
89  return;
90  }
91  target->n_countries = geoip_get_n_countries();
92  target->countries = bitarray_init_zero(target->n_countries);
93  SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) {
94  cc = geoip_get_country(country);
95  if (cc >= 0) {
96  tor_assert(cc < target->n_countries);
97  bitarray_set(target->countries, cc);
98  } else {
99  log_warn(LD_CONFIG, "Country code '%s' is not recognized.",
100  country);
101  }
102  } SMARTLIST_FOREACH_END(country);
103 }
104 
113 int
114 routerset_parse(routerset_t *target, const char *s, const char *description)
115 {
116  int r = 0;
117  int added_countries = 0;
118  char *countryname;
119  smartlist_t *list = smartlist_new();
120  int malformed_list;
121  smartlist_split_string(list, s, ",",
122  SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0);
123  SMARTLIST_FOREACH_BEGIN(list, char *, nick) {
124  addr_policy_t *p;
125  /* if it doesn't pass our validation, assume it's malformed */
126  malformed_list = 1;
127  if (is_legal_hexdigest(nick)) {
128  char d[DIGEST_LEN];
129  if (*nick == '$')
130  ++nick;
131  log_debug(LD_CONFIG, "Adding identity %s to %s", nick, description);
132  base16_decode(d, sizeof(d), nick, HEX_DIGEST_LEN);
133  digestmap_set(target->digests, d, (void*)1);
134  } else if (is_legal_nickname(nick)) {
135  log_debug(LD_CONFIG, "Adding nickname %s to %s", nick, description);
136  strmap_set_lc(target->names, nick, (void*)1);
137  } else if ((countryname = routerset_get_countryname(nick)) != NULL) {
138  log_debug(LD_CONFIG, "Adding country %s to %s", nick,
139  description);
140  smartlist_add(target->country_names, countryname);
141  added_countries = 1;
142  } else if ((strchr(nick,'.') || strchr(nick, ':') || strchr(nick, '*'))
143  && (p = router_parse_addr_policy_item_from_string(
144  nick, ADDR_POLICY_REJECT,
145  &malformed_list))) {
146  /* IPv4 addresses contain '.', IPv6 addresses contain ':',
147  * and wildcard addresses contain '*'. */
148  log_debug(LD_CONFIG, "Adding address %s to %s", nick, description);
149  smartlist_add(target->policies, p);
150  } else if (malformed_list) {
151  log_warn(LD_CONFIG, "Entry '%s' in %s is malformed. Discarding entire"
152  " list.", nick, description);
153  r = -1;
154  tor_free(nick);
155  SMARTLIST_DEL_CURRENT(list, nick);
156  } else {
157  log_notice(LD_CONFIG, "Entry '%s' in %s is ignored. Using the"
158  " remainder of the list.", nick, description);
159  tor_free(nick);
160  SMARTLIST_DEL_CURRENT(list, nick);
161  }
162  } SMARTLIST_FOREACH_END(nick);
163  policy_expand_unspec(&target->policies);
164  smartlist_add_all(target->list, list);
165  smartlist_free(list);
166  if (added_countries)
168  return r;
169 }
170 
172 void
173 routerset_union(routerset_t *target, const routerset_t *source)
174 {
175  char *s;
176  tor_assert(target);
177  if (!source || !source->list)
178  return;
179  s = routerset_to_string(source);
180  routerset_parse(target, s, "other routerset");
181  tor_free(s);
182 }
183 
186 int
187 routerset_is_list(const routerset_t *set)
188 {
189  return smartlist_len(set->country_names) == 0 &&
190  smartlist_len(set->policies) == 0;
191 }
192 
195 int
196 routerset_needs_geoip(const routerset_t *set)
197 {
198  return set && smartlist_len(set->country_names);
199 }
200 
202 int
203 routerset_is_empty(const routerset_t *set)
204 {
205  return !set || smartlist_len(set->list) == 0;
206 }
207 
210 int
211 routerset_len(const routerset_t *set)
212 {
213  if (!set) {
214  return 0;
215  }
216  return smartlist_len(set->list);
217 }
218 
226 STATIC int
227 routerset_contains(const routerset_t *set, const tor_addr_t *addr,
228  uint16_t orport,
229  const char *nickname, const char *id_digest,
230  country_t country)
231 {
232  if (!set || !set->list)
233  return 0;
234  if (nickname && strmap_get_lc(set->names, nickname))
235  return 4;
236  if (id_digest && digestmap_get(set->digests, id_digest))
237  return 4;
238  if (addr && compare_tor_addr_to_addr_policy(addr, orport, set->policies)
240  return 3;
241  if (set->countries) {
242  if (country < 0 && addr)
243  country = geoip_get_country_by_addr(addr);
244 
245  if (country >= 0 && country < set->n_countries &&
246  bitarray_is_set(set->countries, country))
247  return 2;
248  }
249  return 0;
250 }
251 
256 int
257 routerset_add_unknown_ccs(routerset_t **setp, int only_if_some_cc_set)
258 {
259  routerset_t *set;
260  int add_unknown, add_a1;
261  if (only_if_some_cc_set) {
262  if (!*setp || smartlist_len((*setp)->country_names) == 0)
263  return 0;
264  }
265  if (!*setp)
266  *setp = routerset_new();
267 
268  set = *setp;
269 
270  add_unknown = ! smartlist_contains_string_case(set->country_names, "??") &&
271  geoip_get_country("??") >= 0;
272  add_a1 = ! smartlist_contains_string_case(set->country_names, "a1") &&
273  geoip_get_country("A1") >= 0;
274 
275  if (add_unknown) {
276  smartlist_add_strdup(set->country_names, "??");
277  smartlist_add_strdup(set->list, "{??}");
278  }
279  if (add_a1) {
280  smartlist_add_strdup(set->country_names, "a1");
281  smartlist_add_strdup(set->list, "{a1}");
282  }
283 
284  if (add_unknown || add_a1) {
286  return 1;
287  }
288  return 0;
289 }
290 
292 int
293 routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
294 {
295  return routerset_contains(set,
296  &ei->addr,
297  ei->port,
298  ei->nickname,
299  ei->identity_digest,
300  -1 /*country*/);
301 }
302 
305 int
306 routerset_contains_router(const routerset_t *set, const routerinfo_t *ri,
307  country_t country)
308 {
309  tor_addr_t addr;
310  tor_addr_from_ipv4h(&addr, ri->addr);
311  return routerset_contains(set,
312  &addr,
313  ri->or_port,
314  ri->nickname,
315  ri->cache_info.identity_digest,
316  country);
317 }
318 
321 int
322 routerset_contains_routerstatus(const routerset_t *set,
323  const routerstatus_t *rs,
324  country_t country)
325 {
326  tor_addr_t addr;
327  tor_addr_from_ipv4h(&addr, rs->addr);
328  return routerset_contains(set,
329  &addr,
330  rs->or_port,
331  rs->nickname,
332  rs->identity_digest,
333  country);
334 }
335 
337 int
338 routerset_contains_node(const routerset_t *set, const node_t *node)
339 {
340  if (node->rs)
341  return routerset_contains_routerstatus(set, node->rs, node->country);
342  else if (node->ri)
343  return routerset_contains_router(set, node->ri, node->country);
344  else
345  return 0;
346 }
347 
349 int
350 routerset_contains_bridge(const routerset_t *set, const bridge_info_t *bridge)
351 {
352  const char *id = (const char*)bridge_get_rsa_id_digest(bridge);
353  const tor_addr_port_t *addrport = bridge_get_addr_port(bridge);
354 
355  tor_assert(addrport);
356  return routerset_contains(set, &addrport->addr, addrport->port,
357  NULL, id, -1);
358 }
359 
363 void
364 routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset,
365  const routerset_t *excludeset, int running_only)
366 {
367  tor_assert(out);
368  if (!routerset || !routerset->list)
369  return;
370 
371  if (routerset_is_list(routerset)) {
372  /* No routers are specified by type; all are given by name or digest.
373  * we can do a lookup in O(len(routerset)). */
374  SMARTLIST_FOREACH(routerset->list, const char *, name, {
375  const node_t *node = node_get_by_nickname(name, 0);
376  if (node) {
377  if (!running_only || node->is_running)
378  if (!routerset_contains_node(excludeset, node))
379  smartlist_add(out, (void*)node);
380  }
381  });
382  } else {
383  /* We need to iterate over the routerlist to get all the ones of the
384  * right kind. */
385  const smartlist_t *nodes = nodelist_get_list();
386  SMARTLIST_FOREACH(nodes, const node_t *, node, {
387  if (running_only && !node->is_running)
388  continue;
389  if (routerset_contains_node(routerset, node) &&
390  !routerset_contains_node(excludeset, node))
391  smartlist_add(out, (void*)node);
392  });
393  }
394 }
395 
397 void
398 routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
399 {
400  tor_assert(lst);
401  if (!routerset)
402  return;
403  SMARTLIST_FOREACH(lst, const node_t *, node, {
404  if (routerset_contains_node(routerset, node)) {
405  //log_debug(LD_DIR, "Subtracting %s",r->nickname);
406  SMARTLIST_DEL_CURRENT(lst, node);
407  }
408  });
409 }
410 
413 char *
414 routerset_to_string(const routerset_t *set)
415 {
416  if (!set || !set->list)
417  return tor_strdup("");
418  return smartlist_join_strings(set->list, ",", 0, NULL);
419 }
420 
423 int
424 routerset_equal(const routerset_t *old, const routerset_t *new)
425 {
426  if (routerset_is_empty(old) && routerset_is_empty(new)) {
427  /* Two empty sets are equal */
428  return 1;
429  } else if (routerset_is_empty(old) || routerset_is_empty(new)) {
430  /* An empty set is equal to nothing else. */
431  return 0;
432  }
433  tor_assert(old != NULL);
434  tor_assert(new != NULL);
435 
436  if (smartlist_len(old->list) != smartlist_len(new->list))
437  return 0;
438 
439  SMARTLIST_FOREACH(old->list, const char *, cp1, {
440  const char *cp2 = smartlist_get(new->list, cp1_sl_idx);
441  if (strcmp(cp1, cp2))
442  return 0;
443  });
444 
445  return 1;
446 }
447 
449 void
450 routerset_free_(routerset_t *routerset)
451 {
452  if (!routerset)
453  return;
454 
455  SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
456  smartlist_free(routerset->list);
457  SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
458  addr_policy_free(p));
459  smartlist_free(routerset->policies);
460  SMARTLIST_FOREACH(routerset->country_names, char *, cp, tor_free(cp));
461  smartlist_free(routerset->country_names);
462 
463  strmap_free(routerset->names, NULL);
464  digestmap_free(routerset->digests, NULL);
465  bitarray_free(routerset->countries);
466  tor_free(routerset);
467 }
468 
476 static int
477 routerset_kv_parse(void *target, const config_line_t *line, char **errmsg,
478  const void *params)
479 {
480  (void)params;
481  routerset_t **p = (routerset_t**)target;
482  routerset_free(*p); // clear the old value, if any.
483  routerset_t *rs = routerset_new();
484  if (routerset_parse(rs, line->value, line->key) < 0) {
485  routerset_free(rs);
486  *errmsg = tor_strdup("Invalid router list.");
487  return -1;
488  } else {
489  if (routerset_is_empty(rs)) {
490  /* Represent empty sets as NULL. */
491  routerset_free(rs);
492  }
493  *p = rs;
494  return 0;
495  }
496 }
497 
504 static char *
505 routerset_encode(const void *value, const void *params)
506 {
507  (void)params;
508  const routerset_t **p = (const routerset_t**)value;
509  return routerset_to_string(*p);
510 }
511 
517 static void
518 routerset_clear(void *value, const void *params)
519 {
520  (void)params;
521  routerset_t **p = (routerset_t**)value;
522  routerset_free(*p); // sets *p to NULL.
523 }
524 
532 static int
533 routerset_copy(void *dest, const void *src, const void *params)
534 {
535  (void)params;
536  routerset_t **output = (routerset_t**)dest;
537  const routerset_t *input = *(routerset_t**)src;
538  routerset_free(*output); // sets *output to NULL
539  if (! routerset_is_empty(input)) {
540  *output = routerset_new();
541  routerset_union(*output, input);
542  }
543  return 0;
544 }
545 
551  .encode = routerset_encode,
552  .clear = routerset_clear,
553  .copy = routerset_copy
554 };
555 
566  .name = "RouterList",
567  .fns = &routerset_type_fns
568 };
Header for confline.c.
int(* kv_parse)(void *target, const struct config_line_t *line, char **errmsg, const void *params)
void routerset_union(routerset_t *target, const routerset_t *source)
Definition: routerset.c:173
int routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
Definition: routerset.c:293
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:338
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
const var_type_def_t ROUTERSET_type_defn
Definition: routerset.c:565
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.
Structure declarations for typedvar type definitions.
int routerset_is_list(const routerset_t *set)
Definition: routerset.c:187
#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
static char * routerset_encode(const void *value, const void *params)
Definition: routerset.c:505
void routerset_refresh_countries(routerset_t *target)
Definition: routerset.c:81
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:450
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:227
char * routerset_to_string(const routerset_t *set)
Definition: routerset.c:414
int is_legal_hexdigest(const char *s)
Definition: nickname.c:45
tor_assert(buffer)
static void routerset_clear(void *value, const void *params)
Definition: routerset.c:518
#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
static int routerset_kv_parse(void *target, const config_line_t *line, char **errmsg, const void *params)
Definition: routerset.c:477
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:306
Header file for circuitbuild.c.
Header for lib/confmgt/typedvar.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:257
int routerset_needs_geoip(const routerset_t *set)
Definition: routerset.c:196
int routerset_parse(routerset_t *target, const char *s, const char *description)
Definition: routerset.c:114
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]
static int routerset_copy(void *dest, const void *src, const void *params)
Definition: routerset.c:533
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
Types used to specify configurable options.
char identity_digest[DIGEST_LEN]
#define SMARTLIST_FOREACH(sl, type, var, cmd)
static const var_type_fns_t routerset_type_fns
Definition: routerset.c:549
int routerset_len(const routerset_t *set)
Definition: routerset.c:211
void routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
Definition: routerset.c:398
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:51
int routerset_equal(const routerset_t *old, const routerset_t *new)
Definition: routerset.c:424
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
const char * name
void routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset, const routerset_t *excludeset, int running_only)
Definition: routerset.c:364
int routerset_contains_routerstatus(const routerset_t *set, const routerstatus_t *rs, country_t country)
Definition: routerset.c:322
tor_addr_t addr
STATIC char * routerset_get_countryname(const char *c)
Definition: routerset.c:65
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:350
#define LD_CONFIG
Definition: log.h:66
int routerset_is_empty(const routerset_t *set)
Definition: routerset.c:203
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)