Stop checking cached bridge descriptors for usable bridges
[tor/appveyor.git] / src / or / bridges.c
blobac759493ccd9e405be2e1984f156170977ef3b91
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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file bridges.c
9 * \brief Code to manage bridges and bridge selection.
11 * Bridges are fixed entry nodes, used for censorship circumvention.
12 **/
14 #include "or.h"
15 #include "bridges.h"
16 #include "circuitbuild.h"
17 #include "config.h"
18 #include "connection.h"
19 #include "directory.h"
20 #include "entrynodes.h"
21 #include "nodelist.h"
22 #include "policies.h"
23 #include "router.h"
24 #include "routerlist.h"
25 #include "routerset.h"
26 #include "transports.h"
28 /** Information about a configured bridge. Currently this just matches the
29 * ones in the torrc file, but one day we may be able to learn about new
30 * bridges on our own, and remember them in the state file. */
31 struct bridge_info_t {
32 /** Address and port of the bridge, as configured by the user.*/
33 tor_addr_port_t addrport_configured;
34 /** Address of the bridge. */
35 tor_addr_t addr;
36 /** TLS port for the bridge. */
37 uint16_t port;
38 /** Boolean: We are re-parsing our bridge list, and we are going to remove
39 * this one if we don't find it in the list of configured bridges. */
40 unsigned marked_for_removal : 1;
41 /** Expected identity digest, or all zero bytes if we don't know what the
42 * digest should be. */
43 char identity[DIGEST_LEN];
45 /** Name of pluggable transport protocol taken from its config line. */
46 char *transport_name;
48 /** When should we next try to fetch a descriptor for this bridge? */
49 download_status_t fetch_status;
51 /** A smartlist of k=v values to be passed to the SOCKS proxy, if
52 transports are used for this bridge. */
53 smartlist_t *socks_args;
56 static void bridge_free(bridge_info_t *bridge);
57 static void rewrite_node_address_for_bridge(const bridge_info_t *bridge,
58 node_t *node);
60 /** A list of configured bridges. Whenever we actually get a descriptor
61 * for one, we add it as an entry guard. Note that the order of bridges
62 * in this list does not necessarily correspond to the order of bridges
63 * in the torrc. */
64 static smartlist_t *bridge_list = NULL;
66 /** Mark every entry of the bridge list to be removed on our next call to
67 * sweep_bridge_list unless it has first been un-marked. */
68 void
69 mark_bridge_list(void)
71 if (!bridge_list)
72 bridge_list = smartlist_new();
73 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b,
74 b->marked_for_removal = 1);
77 /** Remove every entry of the bridge list that was marked with
78 * mark_bridge_list if it has not subsequently been un-marked. */
79 void
80 sweep_bridge_list(void)
82 if (!bridge_list)
83 bridge_list = smartlist_new();
84 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
85 if (b->marked_for_removal) {
86 SMARTLIST_DEL_CURRENT(bridge_list, b);
87 bridge_free(b);
89 } SMARTLIST_FOREACH_END(b);
92 /** Initialize the bridge list to empty, creating it if needed. */
93 static void
94 clear_bridge_list(void)
96 if (!bridge_list)
97 bridge_list = smartlist_new();
98 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, bridge_free(b));
99 smartlist_clear(bridge_list);
102 /** Free the bridge <b>bridge</b>. */
103 static void
104 bridge_free(bridge_info_t *bridge)
106 if (!bridge)
107 return;
109 tor_free(bridge->transport_name);
110 if (bridge->socks_args) {
111 SMARTLIST_FOREACH(bridge->socks_args, char*, s, tor_free(s));
112 smartlist_free(bridge->socks_args);
115 tor_free(bridge);
118 /** Return a list of all the configured bridges, as bridge_info_t pointers. */
119 const smartlist_t *
120 bridge_list_get(void)
122 if (!bridge_list)
123 bridge_list = smartlist_new();
124 return bridge_list;
128 * Given a <b>bridge</b>, return a pointer to its RSA identity digest, or
129 * NULL if we don't know one for it.
131 const uint8_t *
132 bridge_get_rsa_id_digest(const bridge_info_t *bridge)
134 tor_assert(bridge);
135 if (tor_digest_is_zero(bridge->identity))
136 return NULL;
137 else
138 return (const uint8_t *) bridge->identity;
142 * Given a <b>bridge</b>, return a pointer to its configured addr:port
143 * combination.
145 const tor_addr_port_t *
146 bridge_get_addr_port(const bridge_info_t *bridge)
148 tor_assert(bridge);
149 return &bridge->addrport_configured;
152 /** If we have a bridge configured whose digest matches <b>digest</b>, or a
153 * bridge with no known digest whose address matches any of the
154 * tor_addr_port_t's in <b>orports</b>, return that bridge. Else return
155 * NULL. */
156 static bridge_info_t *
157 get_configured_bridge_by_orports_digest(const char *digest,
158 const smartlist_t *orports)
160 if (!bridge_list)
161 return NULL;
162 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
164 if (tor_digest_is_zero(bridge->identity)) {
165 SMARTLIST_FOREACH_BEGIN(orports, tor_addr_port_t *, ap)
167 if (tor_addr_compare(&bridge->addr, &ap->addr, CMP_EXACT) == 0 &&
168 bridge->port == ap->port)
169 return bridge;
171 SMARTLIST_FOREACH_END(ap);
173 if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
174 return bridge;
176 SMARTLIST_FOREACH_END(bridge);
177 return NULL;
180 /** If we have a bridge configured whose digest matches <b>digest</b>, or a
181 * bridge with no known digest whose address matches <b>addr</b>:<b>port</b>,
182 * return that bridge. Else return NULL. If <b>digest</b> is NULL, check for
183 * address/port matches only. */
184 bridge_info_t *
185 get_configured_bridge_by_addr_port_digest(const tor_addr_t *addr,
186 uint16_t port,
187 const char *digest)
189 if (!bridge_list)
190 return NULL;
191 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
193 if ((tor_digest_is_zero(bridge->identity) || digest == NULL) &&
194 !tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
195 bridge->port == port)
196 return bridge;
197 if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
198 return bridge;
200 SMARTLIST_FOREACH_END(bridge);
201 return NULL;
205 * As get_configured_bridge_by_addr_port, but require that the
206 * address match <b>addr</b>:<b>port</b>, and that the ID digest match
207 * <b>digest</b>. (The other function will ignore the address if the
208 * digest matches.)
210 bridge_info_t *
211 get_configured_bridge_by_exact_addr_port_digest(const tor_addr_t *addr,
212 uint16_t port,
213 const char *digest)
215 if (!bridge_list)
216 return NULL;
217 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
218 if (!tor_addr_compare(&bridge->addr, addr, CMP_EXACT) &&
219 bridge->port == port) {
221 if (digest && tor_memeq(bridge->identity, digest, DIGEST_LEN))
222 return bridge;
223 else if (!digest || tor_digest_is_zero(bridge->identity))
224 return bridge;
227 } SMARTLIST_FOREACH_END(bridge);
228 return NULL;
231 /** If we have a bridge configured whose digest matches <b>digest</b>, or a
232 * bridge with no known digest whose address matches <b>addr</b>:<b>port</b>,
233 * return 1. Else return 0. If <b>digest</b> is NULL, check for
234 * address/port matches only. */
236 addr_is_a_configured_bridge(const tor_addr_t *addr,
237 uint16_t port,
238 const char *digest)
240 tor_assert(addr);
241 return get_configured_bridge_by_addr_port_digest(addr, port, digest) ? 1 : 0;
244 /** If we have a bridge configured whose digest matches
245 * <b>ei->identity_digest</b>, or a bridge with no known digest whose address
246 * matches <b>ei->addr</b>:<b>ei->port</b>, return 1. Else return 0.
247 * If <b>ei->onion_key</b> is NULL, check for address/port matches only. */
249 extend_info_is_a_configured_bridge(const extend_info_t *ei)
251 const char *digest = ei->onion_key ? ei->identity_digest : NULL;
252 return addr_is_a_configured_bridge(&ei->addr, ei->port, digest);
255 /** Wrapper around get_configured_bridge_by_addr_port_digest() to look
256 * it up via router descriptor <b>ri</b>. */
257 static bridge_info_t *
258 get_configured_bridge_by_routerinfo(const routerinfo_t *ri)
260 bridge_info_t *bi = NULL;
261 smartlist_t *orports = router_get_all_orports(ri);
262 bi = get_configured_bridge_by_orports_digest(ri->cache_info.identity_digest,
263 orports);
264 SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
265 smartlist_free(orports);
266 return bi;
269 /** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
271 routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
273 return get_configured_bridge_by_routerinfo(ri) ? 1 : 0;
276 /** Return 1 if <b>node</b> is one of our configured bridges, else 0. */
278 node_is_a_configured_bridge(const node_t *node)
280 int retval = 0;
281 smartlist_t *orports = node_get_all_orports(node);
282 retval = get_configured_bridge_by_orports_digest(node->identity,
283 orports) != NULL;
284 SMARTLIST_FOREACH(orports, tor_addr_port_t *, p, tor_free(p));
285 smartlist_free(orports);
286 return retval;
289 /** We made a connection to a router at <b>addr</b>:<b>port</b>
290 * without knowing its digest. Its digest turned out to be <b>digest</b>.
291 * If it was a bridge, and we still don't know its digest, record it.
293 void
294 learned_router_identity(const tor_addr_t *addr, uint16_t port,
295 const char *digest,
296 const ed25519_public_key_t *ed_id)
298 // XXXX prop220 use ed_id here, once there is some way to specify
299 (void)ed_id;
300 int learned = 0;
301 bridge_info_t *bridge =
302 get_configured_bridge_by_exact_addr_port_digest(addr, port, digest);
303 if (bridge && tor_digest_is_zero(bridge->identity)) {
304 memcpy(bridge->identity, digest, DIGEST_LEN);
305 learned = 1;
307 /* XXXX prop220 remember bridge ed25519 identities -- add a field */
308 #if 0
309 if (bridge && ed_id &&
310 ed25519_public_key_is_zero(&bridge->ed25519_identity) &&
311 !ed25519_public_key_is_zero(ed_id)) {
312 memcpy(&bridge->ed25519_identity, ed_id, sizeof(*ed_id));
313 learned = 1;
315 #endif /* 0 */
316 if (learned) {
317 char *transport_info = NULL;
318 const char *transport_name =
319 find_transport_name_by_bridge_addrport(addr, port);
320 if (transport_name)
321 tor_asprintf(&transport_info, " (with transport '%s')", transport_name);
323 // XXXX prop220 log both fingerprints.
324 log_notice(LD_DIR, "Learned fingerprint %s for bridge %s%s.",
325 hex_str(digest, DIGEST_LEN), fmt_addrport(addr, port),
326 transport_info ? transport_info : "");
327 tor_free(transport_info);
328 entry_guard_learned_bridge_identity(&bridge->addrport_configured,
329 (const uint8_t *)digest);
333 /** Return true if <b>bridge</b> has the same identity digest as
334 * <b>digest</b>. If <b>digest</b> is NULL, it matches
335 * bridges with unspecified identity digests. */
336 static int
337 bridge_has_digest(const bridge_info_t *bridge, const char *digest)
339 if (digest)
340 return tor_memeq(digest, bridge->identity, DIGEST_LEN);
341 else
342 return tor_digest_is_zero(bridge->identity);
345 /** We are about to add a new bridge at <b>addr</b>:<b>port</b>, with optional
346 * <b>digest</b> and <b>transport_name</b>. Mark for removal any previously
347 * existing bridge with the same address and port, and warn the user as
348 * appropriate.
350 static void
351 bridge_resolve_conflicts(const tor_addr_t *addr, uint16_t port,
352 const char *digest, const char *transport_name)
354 /* Iterate the already-registered bridge list:
356 If you find a bridge with the same adress and port, mark it for
357 removal. It doesn't make sense to have two active bridges with
358 the same IP:PORT. If the bridge in question has a different
359 digest or transport than <b>digest</b>/<b>transport_name</b>,
360 it's probably a misconfiguration and we should warn the user.
362 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge) {
363 if (bridge->marked_for_removal)
364 continue;
366 if (tor_addr_eq(&bridge->addr, addr) && (bridge->port == port)) {
368 bridge->marked_for_removal = 1;
370 if (!bridge_has_digest(bridge, digest) ||
371 strcmp_opt(bridge->transport_name, transport_name)) {
372 /* warn the user */
373 char *bridge_description_new, *bridge_description_old;
374 tor_asprintf(&bridge_description_new, "%s:%s:%s",
375 fmt_addrport(addr, port),
376 digest ? hex_str(digest, DIGEST_LEN) : "",
377 transport_name ? transport_name : "");
378 tor_asprintf(&bridge_description_old, "%s:%s:%s",
379 fmt_addrport(&bridge->addr, bridge->port),
380 tor_digest_is_zero(bridge->identity) ?
381 "" : hex_str(bridge->identity,DIGEST_LEN),
382 bridge->transport_name ? bridge->transport_name : "");
384 log_warn(LD_GENERAL,"Tried to add bridge '%s', but we found a conflict"
385 " with the already registered bridge '%s'. We will discard"
386 " the old bridge and keep '%s'. If this is not what you"
387 " wanted, please change your configuration file accordingly.",
388 bridge_description_new, bridge_description_old,
389 bridge_description_new);
391 tor_free(bridge_description_new);
392 tor_free(bridge_description_old);
395 } SMARTLIST_FOREACH_END(bridge);
398 /** Return True if we have a bridge that uses a transport with name
399 * <b>transport_name</b>. */
400 MOCK_IMPL(int,
401 transport_is_needed, (const char *transport_name))
403 if (!bridge_list)
404 return 0;
406 SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
407 if (bridge->transport_name &&
408 !strcmp(bridge->transport_name, transport_name))
409 return 1;
410 } SMARTLIST_FOREACH_END(bridge);
412 return 0;
415 /** Register the bridge information in <b>bridge_line</b> to the
416 * bridge subsystem. Steals reference of <b>bridge_line</b>. */
417 void
418 bridge_add_from_config(bridge_line_t *bridge_line)
420 bridge_info_t *b;
422 // XXXX prop220 add a way to specify ed25519 ID to bridge_line_t.
424 { /* Log the bridge we are about to register: */
425 log_debug(LD_GENERAL, "Registering bridge at %s (transport: %s) (%s)",
426 fmt_addrport(&bridge_line->addr, bridge_line->port),
427 bridge_line->transport_name ?
428 bridge_line->transport_name : "no transport",
429 tor_digest_is_zero(bridge_line->digest) ?
430 "no key listed" : hex_str(bridge_line->digest, DIGEST_LEN));
432 if (bridge_line->socks_args) { /* print socks arguments */
433 int i = 0;
435 tor_assert(smartlist_len(bridge_line->socks_args) > 0);
437 log_debug(LD_GENERAL, "Bridge uses %d SOCKS arguments:",
438 smartlist_len(bridge_line->socks_args));
439 SMARTLIST_FOREACH(bridge_line->socks_args, const char *, arg,
440 log_debug(LD_CONFIG, "%d: %s", ++i, arg));
444 bridge_resolve_conflicts(&bridge_line->addr,
445 bridge_line->port,
446 bridge_line->digest,
447 bridge_line->transport_name);
449 b = tor_malloc_zero(sizeof(bridge_info_t));
450 tor_addr_copy(&b->addrport_configured.addr, &bridge_line->addr);
451 b->addrport_configured.port = bridge_line->port;
452 tor_addr_copy(&b->addr, &bridge_line->addr);
453 b->port = bridge_line->port;
454 memcpy(b->identity, bridge_line->digest, DIGEST_LEN);
455 if (bridge_line->transport_name)
456 b->transport_name = bridge_line->transport_name;
457 b->fetch_status.schedule = DL_SCHED_BRIDGE;
458 b->fetch_status.backoff = DL_SCHED_RANDOM_EXPONENTIAL;
459 b->fetch_status.increment_on = DL_SCHED_INCREMENT_ATTEMPT;
460 /* We can't reset the bridge's download status here, because UseBridges
461 * might be 0 now, and it might be changed to 1 much later. */
462 b->socks_args = bridge_line->socks_args;
463 if (!bridge_list)
464 bridge_list = smartlist_new();
466 tor_free(bridge_line); /* Deallocate bridge_line now. */
468 smartlist_add(bridge_list, b);
471 /** If <b>digest</b> is one of our known bridges, return it. */
472 bridge_info_t *
473 find_bridge_by_digest(const char *digest)
475 if (! bridge_list)
476 return NULL;
477 SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
479 if (tor_memeq(bridge->identity, digest, DIGEST_LEN))
480 return bridge;
482 return NULL;
485 /** Given the <b>addr</b> and <b>port</b> of a bridge, if that bridge
486 * supports a pluggable transport, return its name. Otherwise, return
487 * NULL. */
488 const char *
489 find_transport_name_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
491 if (!bridge_list)
492 return NULL;
494 SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
495 if (tor_addr_eq(&bridge->addr, addr) &&
496 (bridge->port == port))
497 return bridge->transport_name;
498 } SMARTLIST_FOREACH_END(bridge);
500 return NULL;
503 /** If <b>addr</b> and <b>port</b> match the address and port of a
504 * bridge of ours that uses pluggable transports, place its transport
505 * in <b>transport</b>.
507 * Return 0 on success (found a transport, or found a bridge with no
508 * transport, or found no bridge); return -1 if we should be using a
509 * transport, but the transport could not be found.
512 get_transport_by_bridge_addrport(const tor_addr_t *addr, uint16_t port,
513 const transport_t **transport)
515 *transport = NULL;
516 if (!bridge_list)
517 return 0;
519 SMARTLIST_FOREACH_BEGIN(bridge_list, const bridge_info_t *, bridge) {
520 if (tor_addr_eq(&bridge->addr, addr) &&
521 (bridge->port == port)) { /* bridge matched */
522 if (bridge->transport_name) { /* it also uses pluggable transports */
523 *transport = transport_get_by_name(bridge->transport_name);
524 if (*transport == NULL) { /* it uses pluggable transports, but
525 the transport could not be found! */
526 return -1;
528 return 0;
529 } else { /* bridge matched, but it doesn't use transports. */
530 break;
533 } SMARTLIST_FOREACH_END(bridge);
535 *transport = NULL;
536 return 0;
539 /** Return a smartlist containing all the SOCKS arguments that we
540 * should pass to the SOCKS proxy. */
541 const smartlist_t *
542 get_socks_args_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
544 bridge_info_t *bridge = get_configured_bridge_by_addr_port_digest(addr,
545 port,
546 NULL);
547 return bridge ? bridge->socks_args : NULL;
550 /** We need to ask <b>bridge</b> for its server descriptor. */
551 static void
552 launch_direct_bridge_descriptor_fetch(bridge_info_t *bridge)
554 const or_options_t *options = get_options();
555 circuit_guard_state_t *guard_state = NULL;
557 if (connection_get_by_type_addr_port_purpose(
558 CONN_TYPE_DIR, &bridge->addr, bridge->port,
559 DIR_PURPOSE_FETCH_SERVERDESC))
560 return; /* it's already on the way */
562 if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
563 download_status_mark_impossible(&bridge->fetch_status);
564 log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
565 safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
566 return;
569 /* Until we get a descriptor for the bridge, we only know one address for
570 * it. */
571 if (!fascist_firewall_allows_address_addr(&bridge->addr, bridge->port,
572 FIREWALL_OR_CONNECTION, 0, 0)) {
573 log_notice(LD_CONFIG, "Tried to fetch a descriptor directly from a "
574 "bridge, but that bridge is not reachable through our "
575 "firewall.");
576 return;
579 /* If we already have a node_t for this bridge, rewrite its address now. */
580 node_t *node = node_get_mutable_by_id(bridge->identity);
581 if (node) {
582 rewrite_node_address_for_bridge(bridge, node);
585 tor_addr_port_t bridge_addrport;
586 memcpy(&bridge_addrport.addr, &bridge->addr, sizeof(tor_addr_t));
587 bridge_addrport.port = bridge->port;
589 guard_state = get_guard_state_for_bridge_desc_fetch(bridge->identity);
591 directory_request_t *req =
592 directory_request_new(DIR_PURPOSE_FETCH_SERVERDESC);
593 directory_request_set_or_addr_port(req, &bridge_addrport);
594 directory_request_set_directory_id_digest(req, bridge->identity);
595 directory_request_set_router_purpose(req, ROUTER_PURPOSE_BRIDGE);
596 directory_request_set_resource(req, "authority.z");
597 if (guard_state) {
598 directory_request_set_guard_state(req, guard_state);
600 directory_initiate_request(req);
601 directory_request_free(req);
604 /** Fetching the bridge descriptor from the bridge authority returned a
605 * "not found". Fall back to trying a direct fetch. */
606 void
607 retry_bridge_descriptor_fetch_directly(const char *digest)
609 bridge_info_t *bridge = find_bridge_by_digest(digest);
610 if (!bridge)
611 return; /* not found? oh well. */
613 launch_direct_bridge_descriptor_fetch(bridge);
616 /** For each bridge in our list for which we don't currently have a
617 * descriptor, fetch a new copy of its descriptor -- either directly
618 * from the bridge or via a bridge authority. */
619 void
620 fetch_bridge_descriptors(const or_options_t *options, time_t now)
622 int num_bridge_auths = get_n_authorities(BRIDGE_DIRINFO);
623 int ask_bridge_directly;
624 int can_use_bridge_authority;
626 if (!bridge_list)
627 return;
629 /* If we still have unconfigured managed proxies, don't go and
630 connect to a bridge. */
631 if (pt_proxies_configuration_pending())
632 return;
634 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
636 /* This resets the download status on first use */
637 if (!download_status_is_ready(&bridge->fetch_status, now,
638 IMPOSSIBLE_TO_DOWNLOAD))
639 continue; /* don't bother, no need to retry yet */
640 if (routerset_contains_bridge(options->ExcludeNodes, bridge)) {
641 download_status_mark_impossible(&bridge->fetch_status);
642 log_warn(LD_APP, "Not using bridge at %s: it is in ExcludeNodes.",
643 safe_str_client(fmt_and_decorate_addr(&bridge->addr)));
644 continue;
647 /* schedule the next attempt
648 * we can't increment after a failure, because sometimes we use the
649 * bridge authority, and sometimes we use the bridge direct */
650 download_status_increment_attempt(
651 &bridge->fetch_status,
652 safe_str_client(fmt_and_decorate_addr(&bridge->addr)),
653 now);
655 can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
656 num_bridge_auths;
657 ask_bridge_directly = !can_use_bridge_authority ||
658 !options->UpdateBridgesFromAuthority;
659 log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
660 ask_bridge_directly, tor_digest_is_zero(bridge->identity),
661 !options->UpdateBridgesFromAuthority, !num_bridge_auths);
663 if (ask_bridge_directly &&
664 !fascist_firewall_allows_address_addr(&bridge->addr, bridge->port,
665 FIREWALL_OR_CONNECTION, 0,
666 0)) {
667 log_notice(LD_DIR, "Bridge at '%s' isn't reachable by our "
668 "firewall policy. %s.",
669 fmt_addrport(&bridge->addr, bridge->port),
670 can_use_bridge_authority ?
671 "Asking bridge authority instead" : "Skipping");
672 if (can_use_bridge_authority)
673 ask_bridge_directly = 0;
674 else
675 continue;
678 if (ask_bridge_directly) {
679 /* we need to ask the bridge itself for its descriptor. */
680 launch_direct_bridge_descriptor_fetch(bridge);
681 } else {
682 /* We have a digest and we want to ask an authority. We could
683 * combine all the requests into one, but that may give more
684 * hints to the bridge authority than we want to give. */
685 char resource[10 + HEX_DIGEST_LEN];
686 memcpy(resource, "fp/", 3);
687 base16_encode(resource+3, HEX_DIGEST_LEN+1,
688 bridge->identity, DIGEST_LEN);
689 memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
690 log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
691 resource);
692 directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
693 ROUTER_PURPOSE_BRIDGE, resource, 0, DL_WANT_AUTHORITY);
696 SMARTLIST_FOREACH_END(bridge);
699 /** If our <b>bridge</b> is configured to be a different address than
700 * the bridge gives in <b>node</b>, rewrite the routerinfo
701 * we received to use the address we meant to use. Now we handle
702 * multihomed bridges better.
704 static void
705 rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node)
707 /* XXXX move this function. */
708 /* XXXX overridden addresses should really live in the node_t, so that the
709 * routerinfo_t and the microdesc_t can be immutable. But we can only
710 * do that safely if we know that no function that connects to an OR
711 * does so through an address from any source other than node_get_addr().
713 tor_addr_t addr;
714 const or_options_t *options = get_options();
716 if (node->ri) {
717 routerinfo_t *ri = node->ri;
718 tor_addr_from_ipv4h(&addr, ri->addr);
720 if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
721 bridge->port == ri->or_port) ||
722 (!tor_addr_compare(&bridge->addr, &ri->ipv6_addr, CMP_EXACT) &&
723 bridge->port == ri->ipv6_orport)) {
724 /* they match, so no need to do anything */
725 } else {
726 if (tor_addr_family(&bridge->addr) == AF_INET) {
727 ri->addr = tor_addr_to_ipv4h(&bridge->addr);
728 ri->or_port = bridge->port;
729 log_info(LD_DIR,
730 "Adjusted bridge routerinfo for '%s' to match configured "
731 "address %s:%d.",
732 ri->nickname, fmt_addr32(ri->addr), ri->or_port);
733 } else if (tor_addr_family(&bridge->addr) == AF_INET6) {
734 tor_addr_copy(&ri->ipv6_addr, &bridge->addr);
735 ri->ipv6_orport = bridge->port;
736 log_info(LD_DIR,
737 "Adjusted bridge routerinfo for '%s' to match configured "
738 "address %s.",
739 ri->nickname, fmt_addrport(&ri->ipv6_addr, ri->ipv6_orport));
740 } else {
741 log_err(LD_BUG, "Address family not supported: %d.",
742 tor_addr_family(&bridge->addr));
743 return;
747 if (options->ClientPreferIPv6ORPort == -1) {
748 /* Mark which address to use based on which bridge_t we got. */
749 node->ipv6_preferred = (tor_addr_family(&bridge->addr) == AF_INET6 &&
750 !tor_addr_is_null(&node->ri->ipv6_addr));
751 } else {
752 /* Mark which address to use based on user preference */
753 node->ipv6_preferred = (fascist_firewall_prefer_ipv6_orport(options) &&
754 !tor_addr_is_null(&node->ri->ipv6_addr));
757 /* XXXipv6 we lack support for falling back to another address for
758 the same relay, warn the user */
759 if (!tor_addr_is_null(&ri->ipv6_addr)) {
760 tor_addr_port_t ap;
761 node_get_pref_orport(node, &ap);
762 log_notice(LD_CONFIG,
763 "Bridge '%s' has both an IPv4 and an IPv6 address. "
764 "Will prefer using its %s address (%s) based on %s.",
765 ri->nickname,
766 node->ipv6_preferred ? "IPv6" : "IPv4",
767 fmt_addrport(&ap.addr, ap.port),
768 options->ClientPreferIPv6ORPort == -1 ?
769 "the configured Bridge address" :
770 "ClientPreferIPv6ORPort");
773 if (node->rs) {
774 routerstatus_t *rs = node->rs;
775 tor_addr_from_ipv4h(&addr, rs->addr);
777 if (!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
778 bridge->port == rs->or_port) {
779 /* they match, so no need to do anything */
780 } else {
781 rs->addr = tor_addr_to_ipv4h(&bridge->addr);
782 rs->or_port = bridge->port;
783 log_info(LD_DIR,
784 "Adjusted bridge routerstatus for '%s' to match "
785 "configured address %s.",
786 rs->nickname, fmt_addrport(&bridge->addr, rs->or_port));
791 /** We just learned a descriptor for a bridge. See if that
792 * digest is in our entry guard list, and add it if not. */
793 void
794 learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
796 tor_assert(ri);
797 tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
798 if (get_options()->UseBridges) {
799 int first = num_bridges_usable() <= 1;
800 bridge_info_t *bridge = get_configured_bridge_by_routerinfo(ri);
801 time_t now = time(NULL);
802 router_set_status(ri->cache_info.identity_digest, 1);
804 if (bridge) { /* if we actually want to use this one */
805 node_t *node;
806 /* it's here; schedule its re-fetch for a long time from now. */
807 if (!from_cache) {
808 /* This schedules the re-fetch at a constant interval, which produces
809 * a pattern of bridge traffic. But it's better than trying all
810 * configured briges several times in the first few minutes. */
811 download_status_reset(&bridge->fetch_status);
814 node = node_get_mutable_by_id(ri->cache_info.identity_digest);
815 tor_assert(node);
816 rewrite_node_address_for_bridge(bridge, node);
817 if (tor_digest_is_zero(bridge->identity)) {
818 memcpy(bridge->identity,ri->cache_info.identity_digest, DIGEST_LEN);
819 log_notice(LD_DIR, "Learned identity %s for bridge at %s:%d",
820 hex_str(bridge->identity, DIGEST_LEN),
821 fmt_and_decorate_addr(&bridge->addr),
822 (int) bridge->port);
824 entry_guard_learned_bridge_identity(&bridge->addrport_configured,
825 (const uint8_t*)ri->cache_info.identity_digest);
827 log_notice(LD_DIR, "new bridge descriptor '%s' (%s): %s", ri->nickname,
828 from_cache ? "cached" : "fresh", router_describe(ri));
829 /* set entry->made_contact so if it goes down we don't drop it from
830 * our entry node list */
831 if (first) {
832 routerlist_retry_directory_downloads(now);
838 /** Return a smartlist containing all bridge identity digests */
839 MOCK_IMPL(smartlist_t *,
840 list_bridge_identities, (void))
842 smartlist_t *result = NULL;
843 char *digest_tmp;
845 if (get_options()->UseBridges && bridge_list) {
846 result = smartlist_new();
848 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
849 digest_tmp = tor_malloc(DIGEST_LEN);
850 memcpy(digest_tmp, b->identity, DIGEST_LEN);
851 smartlist_add(result, digest_tmp);
852 } SMARTLIST_FOREACH_END(b);
855 return result;
858 /** Get the download status for a bridge descriptor given its identity */
859 MOCK_IMPL(download_status_t *,
860 get_bridge_dl_status_by_id, (const char *digest))
862 download_status_t *dl = NULL;
864 if (digest && get_options()->UseBridges && bridge_list) {
865 SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, b) {
866 if (tor_memeq(digest, b->identity, DIGEST_LEN)) {
867 dl = &(b->fetch_status);
868 break;
870 } SMARTLIST_FOREACH_END(b);
873 return dl;
876 /** Release all storage held in bridges.c */
877 void
878 bridges_free_all(void)
880 clear_bridge_list();
881 smartlist_free(bridge_list);
882 bridge_list = NULL;