node_select.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. /**
  7. * \file node_select.c
  8. * \brief Code to choose nodes randomly based on restrictions and
  9. * weighted probabilities.
  10. **/
  11. #define NODE_SELECT_PRIVATE
  12. #include "core/or/or.h"
  13. #include "app/config/config.h"
  14. #include "core/mainloop/connection.h"
  15. #include "core/or/policies.h"
  16. #include "core/or/reasons.h"
  17. #include "feature/client/entrynodes.h"
  18. #include "feature/dirclient/dirclient.h"
  19. #include "feature/dircommon/directory.h"
  20. #include "feature/nodelist/describe.h"
  21. #include "feature/nodelist/dirlist.h"
  22. #include "feature/nodelist/microdesc.h"
  23. #include "feature/nodelist/networkstatus.h"
  24. #include "feature/nodelist/node_select.h"
  25. #include "feature/nodelist/nodelist.h"
  26. #include "feature/nodelist/routerlist.h"
  27. #include "feature/nodelist/routerset.h"
  28. #include "feature/relay/router.h"
  29. #include "feature/relay/routermode.h"
  30. #include "lib/container/bitarray.h"
  31. #include "lib/crypt_ops/crypto_rand.h"
  32. #include "lib/math/fp.h"
  33. #include "feature/dirclient/dir_server_st.h"
  34. #include "feature/nodelist/networkstatus_st.h"
  35. #include "feature/nodelist/node_st.h"
  36. #include "feature/nodelist/routerinfo_st.h"
  37. #include "feature/nodelist/routerstatus_st.h"
  38. static int compute_weighted_bandwidths(const smartlist_t *sl,
  39. bandwidth_weight_rule_t rule,
  40. double **bandwidths_out,
  41. double *total_bandwidth_out);
  42. static const routerstatus_t *router_pick_trusteddirserver_impl(
  43. const smartlist_t *sourcelist, dirinfo_type_t auth,
  44. int flags, int *n_busy_out);
  45. static const routerstatus_t *router_pick_dirserver_generic(
  46. smartlist_t *sourcelist,
  47. dirinfo_type_t type, int flags);
  48. /** Try to find a running dirserver that supports operations of <b>type</b>.
  49. *
  50. * If there are no running dirservers in our routerlist and the
  51. * <b>PDS_RETRY_IF_NO_SERVERS</b> flag is set, set all the fallback ones
  52. * (including authorities) as running again, and pick one.
  53. *
  54. * If the <b>PDS_IGNORE_FASCISTFIREWALL</b> flag is set, then include
  55. * dirservers that we can't reach.
  56. *
  57. * If the <b>PDS_ALLOW_SELF</b> flag is not set, then don't include ourself
  58. * (if we're a dirserver).
  59. *
  60. * Don't pick a fallback directory mirror if any non-fallback is viable;
  61. * (the fallback directory mirrors include the authorities)
  62. * try to avoid using servers that have returned 503 recently.
  63. */
  64. const routerstatus_t *
  65. router_pick_directory_server(dirinfo_type_t type, int flags)
  66. {
  67. int busy = 0;
  68. const routerstatus_t *choice;
  69. choice = router_pick_directory_server_impl(type, flags, &busy);
  70. if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
  71. return choice;
  72. if (busy) {
  73. /* If the reason that we got no server is that servers are "busy",
  74. * we must be excluding good servers because we already have serverdesc
  75. * fetches with them. Do not mark down servers up because of this. */
  76. tor_assert((flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
  77. PDS_NO_EXISTING_MICRODESC_FETCH)));
  78. return NULL;
  79. }
  80. log_info(LD_DIR,
  81. "No reachable router entries for dirservers. "
  82. "Trying them all again.");
  83. /* mark all fallback directory mirrors as up again */
  84. router_reset_status_download_failures();
  85. /* try again */
  86. choice = router_pick_directory_server_impl(type, flags, NULL);
  87. return choice;
  88. }
  89. /** Try to find a running fallback directory. Flags are as for
  90. * router_pick_directory_server.
  91. */
  92. const routerstatus_t *
  93. router_pick_dirserver_generic(smartlist_t *sourcelist,
  94. dirinfo_type_t type, int flags)
  95. {
  96. const routerstatus_t *choice;
  97. int busy = 0;
  98. if (smartlist_len(sourcelist) == 1) {
  99. /* If there's only one choice, then we should disable the logic that
  100. * would otherwise prevent us from choosing ourself. */
  101. flags |= PDS_ALLOW_SELF;
  102. }
  103. choice = router_pick_trusteddirserver_impl(sourcelist, type, flags, &busy);
  104. if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
  105. return choice;
  106. if (busy) {
  107. /* If the reason that we got no server is that servers are "busy",
  108. * we must be excluding good servers because we already have serverdesc
  109. * fetches with them. Do not mark down servers up because of this. */
  110. tor_assert((flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
  111. PDS_NO_EXISTING_MICRODESC_FETCH)));
  112. return NULL;
  113. }
  114. log_info(LD_DIR,
  115. "No dirservers are reachable. Trying them all again.");
  116. mark_all_dirservers_up(sourcelist);
  117. return router_pick_trusteddirserver_impl(sourcelist, type, flags, NULL);
  118. }
  119. /* Common retry code for router_pick_directory_server_impl and
  120. * router_pick_trusteddirserver_impl. Retry with the non-preferred IP version.
  121. * Must be called before RETRY_WITHOUT_EXCLUDE().
  122. *
  123. * If we got no result, and we are applying IP preferences, and we are a
  124. * client that could use an alternate IP version, try again with the
  125. * opposite preferences. */
  126. #define RETRY_ALTERNATE_IP_VERSION(retry_label) \
  127. STMT_BEGIN \
  128. if (result == NULL && try_ip_pref && options->ClientUseIPv4 \
  129. && fascist_firewall_use_ipv6(options) && !server_mode(options) \
  130. && !n_busy) { \
  131. n_excluded = 0; \
  132. n_busy = 0; \
  133. try_ip_pref = 0; \
  134. goto retry_label; \
  135. } \
  136. STMT_END \
  137. /* Common retry code for router_pick_directory_server_impl and
  138. * router_pick_trusteddirserver_impl. Retry without excluding nodes, but with
  139. * the preferred IP version. Must be called after RETRY_ALTERNATE_IP_VERSION().
  140. *
  141. * If we got no result, and we are excluding nodes, and StrictNodes is
  142. * not set, try again without excluding nodes. */
  143. #define RETRY_WITHOUT_EXCLUDE(retry_label) \
  144. STMT_BEGIN \
  145. if (result == NULL && try_excluding && !options->StrictNodes \
  146. && n_excluded && !n_busy) { \
  147. try_excluding = 0; \
  148. n_excluded = 0; \
  149. n_busy = 0; \
  150. try_ip_pref = 1; \
  151. goto retry_label; \
  152. } \
  153. STMT_END
  154. /* Common code used in the loop within router_pick_directory_server_impl and
  155. * router_pick_trusteddirserver_impl.
  156. *
  157. * Check if the given <b>identity</b> supports extrainfo. If not, skip further
  158. * checks.
  159. */
  160. #define SKIP_MISSING_TRUSTED_EXTRAINFO(type, identity) \
  161. STMT_BEGIN \
  162. int is_trusted_extrainfo = router_digest_is_trusted_dir_type( \
  163. (identity), EXTRAINFO_DIRINFO); \
  164. if (((type) & EXTRAINFO_DIRINFO) && \
  165. !router_supports_extrainfo((identity), is_trusted_extrainfo)) \
  166. continue; \
  167. STMT_END
  168. #ifndef LOG_FALSE_POSITIVES_DURING_BOOTSTRAP
  169. #define LOG_FALSE_POSITIVES_DURING_BOOTSTRAP 0
  170. #endif
  171. /* Log a message if rs is not found or not a preferred address */
  172. static void
  173. router_picked_poor_directory_log(const routerstatus_t *rs)
  174. {
  175. const networkstatus_t *usable_consensus;
  176. usable_consensus = networkstatus_get_reasonably_live_consensus(time(NULL),
  177. usable_consensus_flavor());
  178. #if !LOG_FALSE_POSITIVES_DURING_BOOTSTRAP
  179. /* Don't log early in the bootstrap process, it's normal to pick from a
  180. * small pool of nodes. Of course, this won't help if we're trying to
  181. * diagnose bootstrap issues. */
  182. if (!smartlist_len(nodelist_get_list()) || !usable_consensus
  183. || !router_have_minimum_dir_info()) {
  184. return;
  185. }
  186. #endif /* !LOG_FALSE_POSITIVES_DURING_BOOTSTRAP */
  187. /* We couldn't find a node, or the one we have doesn't fit our preferences.
  188. * Sometimes this is normal, sometimes it can be a reachability issue. */
  189. if (!rs) {
  190. /* This happens a lot, so it's at debug level */
  191. log_debug(LD_DIR, "Wanted to make an outgoing directory connection, but "
  192. "we couldn't find a directory that fit our criteria. "
  193. "Perhaps we will succeed next time with less strict criteria.");
  194. } else if (!fascist_firewall_allows_rs(rs, FIREWALL_OR_CONNECTION, 1)
  195. && !fascist_firewall_allows_rs(rs, FIREWALL_DIR_CONNECTION, 1)
  196. ) {
  197. /* This is rare, and might be interesting to users trying to diagnose
  198. * connection issues on dual-stack machines. */
  199. log_info(LD_DIR, "Selected a directory %s with non-preferred OR and Dir "
  200. "addresses for launching an outgoing connection: "
  201. "IPv4 %s OR %d Dir %d IPv6 %s OR %d Dir %d",
  202. routerstatus_describe(rs),
  203. fmt_addr32(rs->addr), rs->or_port,
  204. rs->dir_port, fmt_addr(&rs->ipv6_addr),
  205. rs->ipv6_orport, rs->dir_port);
  206. }
  207. }
  208. #undef LOG_FALSE_POSITIVES_DURING_BOOTSTRAP
  209. /* Check if we already have a directory fetch from ap, for serverdesc
  210. * (including extrainfo) or microdesc documents.
  211. * If so, return 1, if not, return 0.
  212. * Also returns 0 if addr is NULL, tor_addr_is_null(addr), or dir_port is 0.
  213. */
  214. STATIC int
  215. router_is_already_dir_fetching(const tor_addr_port_t *ap, int serverdesc,
  216. int microdesc)
  217. {
  218. if (!ap || tor_addr_is_null(&ap->addr) || !ap->port) {
  219. return 0;
  220. }
  221. /* XX/teor - we're not checking tunnel connections here, see #17848
  222. */
  223. if (serverdesc && (
  224. connection_get_by_type_addr_port_purpose(
  225. CONN_TYPE_DIR, &ap->addr, ap->port, DIR_PURPOSE_FETCH_SERVERDESC)
  226. || connection_get_by_type_addr_port_purpose(
  227. CONN_TYPE_DIR, &ap->addr, ap->port, DIR_PURPOSE_FETCH_EXTRAINFO))) {
  228. return 1;
  229. }
  230. if (microdesc && (
  231. connection_get_by_type_addr_port_purpose(
  232. CONN_TYPE_DIR, &ap->addr, ap->port, DIR_PURPOSE_FETCH_MICRODESC))) {
  233. return 1;
  234. }
  235. return 0;
  236. }
  237. /* Check if we already have a directory fetch from the ipv4 or ipv6
  238. * router, for serverdesc (including extrainfo) or microdesc documents.
  239. * If so, return 1, if not, return 0.
  240. */
  241. static int
  242. router_is_already_dir_fetching_(uint32_t ipv4_addr,
  243. const tor_addr_t *ipv6_addr,
  244. uint16_t dir_port,
  245. int serverdesc,
  246. int microdesc)
  247. {
  248. tor_addr_port_t ipv4_dir_ap, ipv6_dir_ap;
  249. /* Assume IPv6 DirPort is the same as IPv4 DirPort */
  250. tor_addr_from_ipv4h(&ipv4_dir_ap.addr, ipv4_addr);
  251. ipv4_dir_ap.port = dir_port;
  252. tor_addr_copy(&ipv6_dir_ap.addr, ipv6_addr);
  253. ipv6_dir_ap.port = dir_port;
  254. return (router_is_already_dir_fetching(&ipv4_dir_ap, serverdesc, microdesc)
  255. || router_is_already_dir_fetching(&ipv6_dir_ap, serverdesc, microdesc));
  256. }
  257. /** Pick a random running valid directory server/mirror from our
  258. * routerlist. Arguments are as for router_pick_directory_server(), except:
  259. *
  260. * If <b>n_busy_out</b> is provided, set *<b>n_busy_out</b> to the number of
  261. * directories that we excluded for no other reason than
  262. * PDS_NO_EXISTING_SERVERDESC_FETCH or PDS_NO_EXISTING_MICRODESC_FETCH.
  263. */
  264. STATIC const routerstatus_t *
  265. router_pick_directory_server_impl(dirinfo_type_t type, int flags,
  266. int *n_busy_out)
  267. {
  268. const or_options_t *options = get_options();
  269. const node_t *result;
  270. smartlist_t *direct, *tunnel;
  271. smartlist_t *trusted_direct, *trusted_tunnel;
  272. smartlist_t *overloaded_direct, *overloaded_tunnel;
  273. time_t now = time(NULL);
  274. const networkstatus_t *consensus = networkstatus_get_latest_consensus();
  275. const int requireother = ! (flags & PDS_ALLOW_SELF);
  276. const int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
  277. const int no_serverdesc_fetching =(flags & PDS_NO_EXISTING_SERVERDESC_FETCH);
  278. const int no_microdesc_fetching = (flags & PDS_NO_EXISTING_MICRODESC_FETCH);
  279. int try_excluding = 1, n_excluded = 0, n_busy = 0;
  280. int try_ip_pref = 1;
  281. if (!consensus)
  282. return NULL;
  283. retry_search:
  284. direct = smartlist_new();
  285. tunnel = smartlist_new();
  286. trusted_direct = smartlist_new();
  287. trusted_tunnel = smartlist_new();
  288. overloaded_direct = smartlist_new();
  289. overloaded_tunnel = smartlist_new();
  290. const int skip_or_fw = router_skip_or_reachability(options, try_ip_pref);
  291. const int skip_dir_fw = router_skip_dir_reachability(options, try_ip_pref);
  292. const int must_have_or = directory_must_use_begindir(options);
  293. /* Find all the running dirservers we know about. */
  294. SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
  295. int is_trusted;
  296. int is_overloaded;
  297. const routerstatus_t *status = node->rs;
  298. const country_t country = node->country;
  299. if (!status)
  300. continue;
  301. if (!node->is_running || !node_is_dir(node) || !node->is_valid)
  302. continue;
  303. if (requireother && router_digest_is_me(node->identity))
  304. continue;
  305. SKIP_MISSING_TRUSTED_EXTRAINFO(type, node->identity);
  306. if (try_excluding &&
  307. routerset_contains_routerstatus(options->ExcludeNodes, status,
  308. country)) {
  309. ++n_excluded;
  310. continue;
  311. }
  312. if (router_is_already_dir_fetching_(status->addr,
  313. &status->ipv6_addr,
  314. status->dir_port,
  315. no_serverdesc_fetching,
  316. no_microdesc_fetching)) {
  317. ++n_busy;
  318. continue;
  319. }
  320. is_overloaded = status->last_dir_503_at + DIR_503_TIMEOUT > now;
  321. is_trusted = router_digest_is_trusted_dir(node->identity);
  322. /* Clients use IPv6 addresses if the server has one and the client
  323. * prefers IPv6.
  324. * Add the router if its preferred address and port are reachable.
  325. * If we don't get any routers, we'll try again with the non-preferred
  326. * address for each router (if any). (To ensure correct load-balancing
  327. * we try routers that only have one address both times.)
  328. */
  329. if (!fascistfirewall || skip_or_fw ||
  330. fascist_firewall_allows_node(node, FIREWALL_OR_CONNECTION,
  331. try_ip_pref))
  332. smartlist_add(is_trusted ? trusted_tunnel :
  333. is_overloaded ? overloaded_tunnel : tunnel, (void*)node);
  334. else if (!must_have_or && (skip_dir_fw ||
  335. fascist_firewall_allows_node(node, FIREWALL_DIR_CONNECTION,
  336. try_ip_pref)))
  337. smartlist_add(is_trusted ? trusted_direct :
  338. is_overloaded ? overloaded_direct : direct, (void*)node);
  339. } SMARTLIST_FOREACH_END(node);
  340. if (smartlist_len(tunnel)) {
  341. result = node_sl_choose_by_bandwidth(tunnel, WEIGHT_FOR_DIR);
  342. } else if (smartlist_len(overloaded_tunnel)) {
  343. result = node_sl_choose_by_bandwidth(overloaded_tunnel,
  344. WEIGHT_FOR_DIR);
  345. } else if (smartlist_len(trusted_tunnel)) {
  346. /* FFFF We don't distinguish between trusteds and overloaded trusteds
  347. * yet. Maybe one day we should. */
  348. /* FFFF We also don't load balance over authorities yet. I think this
  349. * is a feature, but it could easily be a bug. -RD */
  350. result = smartlist_choose(trusted_tunnel);
  351. } else if (smartlist_len(direct)) {
  352. result = node_sl_choose_by_bandwidth(direct, WEIGHT_FOR_DIR);
  353. } else if (smartlist_len(overloaded_direct)) {
  354. result = node_sl_choose_by_bandwidth(overloaded_direct,
  355. WEIGHT_FOR_DIR);
  356. } else {
  357. result = smartlist_choose(trusted_direct);
  358. }
  359. smartlist_free(direct);
  360. smartlist_free(tunnel);
  361. smartlist_free(trusted_direct);
  362. smartlist_free(trusted_tunnel);
  363. smartlist_free(overloaded_direct);
  364. smartlist_free(overloaded_tunnel);
  365. RETRY_ALTERNATE_IP_VERSION(retry_search);
  366. RETRY_WITHOUT_EXCLUDE(retry_search);
  367. if (n_busy_out)
  368. *n_busy_out = n_busy;
  369. router_picked_poor_directory_log(result ? result->rs : NULL);
  370. return result ? result->rs : NULL;
  371. }
  372. /** Given an array of double/uint64_t unions that are currently being used as
  373. * doubles, convert them to uint64_t, and try to scale them linearly so as to
  374. * much of the range of uint64_t. If <b>total_out</b> is provided, set it to
  375. * the sum of all elements in the array _before_ scaling. */
  376. STATIC void
  377. scale_array_elements_to_u64(uint64_t *entries_out, const double *entries_in,
  378. int n_entries,
  379. uint64_t *total_out)
  380. {
  381. double total = 0.0;
  382. double scale_factor = 0.0;
  383. int i;
  384. for (i = 0; i < n_entries; ++i)
  385. total += entries_in[i];
  386. if (total > 0.0) {
  387. scale_factor = ((double)INT64_MAX) / total;
  388. scale_factor /= 4.0; /* make sure we're very far away from overflowing */
  389. }
  390. for (i = 0; i < n_entries; ++i)
  391. entries_out[i] = tor_llround(entries_in[i] * scale_factor);
  392. if (total_out)
  393. *total_out = (uint64_t) total;
  394. }
  395. /** Pick a random element of <b>n_entries</b>-element array <b>entries</b>,
  396. * choosing each element with a probability proportional to its (uint64_t)
  397. * value, and return the index of that element. If all elements are 0, choose
  398. * an index at random. Return -1 on error.
  399. */
  400. STATIC int
  401. choose_array_element_by_weight(const uint64_t *entries, int n_entries)
  402. {
  403. int i;
  404. uint64_t rand_val;
  405. uint64_t total = 0;
  406. for (i = 0; i < n_entries; ++i)
  407. total += entries[i];
  408. if (n_entries < 1)
  409. return -1;
  410. if (total == 0)
  411. return crypto_rand_int(n_entries);
  412. tor_assert(total < INT64_MAX);
  413. rand_val = crypto_rand_uint64(total);
  414. return select_array_member_cumulative_timei(
  415. entries, n_entries, total, rand_val);
  416. }
  417. /** Return bw*1000, unless bw*1000 would overflow, in which case return
  418. * INT32_MAX. */
  419. static inline int32_t
  420. kb_to_bytes(uint32_t bw)
  421. {
  422. return (bw > (INT32_MAX/1000)) ? INT32_MAX : bw*1000;
  423. }
  424. /** Helper function:
  425. * choose a random element of smartlist <b>sl</b> of nodes, weighted by
  426. * the advertised bandwidth of each element using the consensus
  427. * bandwidth weights.
  428. *
  429. * If <b>rule</b>==WEIGHT_FOR_EXIT. we're picking an exit node: consider all
  430. * nodes' bandwidth equally regardless of their Exit status, since there may
  431. * be some in the list because they exit to obscure ports. If
  432. * <b>rule</b>==NO_WEIGHTING, we're picking a non-exit node: weight
  433. * exit-node's bandwidth less depending on the smallness of the fraction of
  434. * Exit-to-total bandwidth. If <b>rule</b>==WEIGHT_FOR_GUARD, we're picking a
  435. * guard node: consider all guard's bandwidth equally. Otherwise, weight
  436. * guards proportionally less.
  437. */
  438. static const node_t *
  439. smartlist_choose_node_by_bandwidth_weights(const smartlist_t *sl,
  440. bandwidth_weight_rule_t rule)
  441. {
  442. double *bandwidths_dbl=NULL;
  443. uint64_t *bandwidths_u64=NULL;
  444. if (compute_weighted_bandwidths(sl, rule, &bandwidths_dbl, NULL) < 0)
  445. return NULL;
  446. bandwidths_u64 = tor_calloc(smartlist_len(sl), sizeof(uint64_t));
  447. scale_array_elements_to_u64(bandwidths_u64, bandwidths_dbl,
  448. smartlist_len(sl), NULL);
  449. {
  450. int idx = choose_array_element_by_weight(bandwidths_u64,
  451. smartlist_len(sl));
  452. tor_free(bandwidths_dbl);
  453. tor_free(bandwidths_u64);
  454. return idx < 0 ? NULL : smartlist_get(sl, idx);
  455. }
  456. }
  457. /** When weighting bridges, enforce these values as lower and upper
  458. * bound for believable bandwidth, because there is no way for us
  459. * to verify a bridge's bandwidth currently. */
  460. #define BRIDGE_MIN_BELIEVABLE_BANDWIDTH 20000 /* 20 kB/sec */
  461. #define BRIDGE_MAX_BELIEVABLE_BANDWIDTH 100000 /* 100 kB/sec */
  462. /** Return the smaller of the router's configured BandwidthRate
  463. * and its advertised capacity, making sure to stay within the
  464. * interval between bridge-min-believe-bw and
  465. * bridge-max-believe-bw. */
  466. static uint32_t
  467. bridge_get_advertised_bandwidth_bounded(routerinfo_t *router)
  468. {
  469. uint32_t result = router->bandwidthcapacity;
  470. if (result > router->bandwidthrate)
  471. result = router->bandwidthrate;
  472. if (result > BRIDGE_MAX_BELIEVABLE_BANDWIDTH)
  473. result = BRIDGE_MAX_BELIEVABLE_BANDWIDTH;
  474. else if (result < BRIDGE_MIN_BELIEVABLE_BANDWIDTH)
  475. result = BRIDGE_MIN_BELIEVABLE_BANDWIDTH;
  476. return result;
  477. }
  478. /** Given a list of routers and a weighting rule as in
  479. * smartlist_choose_node_by_bandwidth_weights, compute weighted bandwidth
  480. * values for each node and store them in a freshly allocated
  481. * *<b>bandwidths_out</b> of the same length as <b>sl</b>, and holding results
  482. * as doubles. If <b>total_bandwidth_out</b> is non-NULL, set it to the total
  483. * of all the bandwidths.
  484. * Return 0 on success, -1 on failure. */
  485. static int
  486. compute_weighted_bandwidths(const smartlist_t *sl,
  487. bandwidth_weight_rule_t rule,
  488. double **bandwidths_out,
  489. double *total_bandwidth_out)
  490. {
  491. int64_t weight_scale;
  492. double Wg = -1, Wm = -1, We = -1, Wd = -1;
  493. double Wgb = -1, Wmb = -1, Web = -1, Wdb = -1;
  494. guardfraction_bandwidth_t guardfraction_bw;
  495. double *bandwidths = NULL;
  496. double total_bandwidth = 0.0;
  497. tor_assert(sl);
  498. tor_assert(bandwidths_out);
  499. /* Can't choose exit and guard at same time */
  500. tor_assert(rule == NO_WEIGHTING ||
  501. rule == WEIGHT_FOR_EXIT ||
  502. rule == WEIGHT_FOR_GUARD ||
  503. rule == WEIGHT_FOR_MID ||
  504. rule == WEIGHT_FOR_DIR);
  505. *bandwidths_out = NULL;
  506. if (total_bandwidth_out) {
  507. *total_bandwidth_out = 0.0;
  508. }
  509. if (smartlist_len(sl) == 0) {
  510. log_info(LD_CIRC,
  511. "Empty routerlist passed in to consensus weight node "
  512. "selection for rule %s",
  513. bandwidth_weight_rule_to_string(rule));
  514. return -1;
  515. }
  516. weight_scale = networkstatus_get_weight_scale_param(NULL);
  517. tor_assert(weight_scale >= 1);
  518. if (rule == WEIGHT_FOR_GUARD) {
  519. Wg = networkstatus_get_bw_weight(NULL, "Wgg", -1);
  520. Wm = networkstatus_get_bw_weight(NULL, "Wgm", -1); /* Bridges */
  521. We = 0;
  522. Wd = networkstatus_get_bw_weight(NULL, "Wgd", -1);
  523. Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
  524. Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
  525. Web = networkstatus_get_bw_weight(NULL, "Web", -1);
  526. Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
  527. } else if (rule == WEIGHT_FOR_MID) {
  528. Wg = networkstatus_get_bw_weight(NULL, "Wmg", -1);
  529. Wm = networkstatus_get_bw_weight(NULL, "Wmm", -1);
  530. We = networkstatus_get_bw_weight(NULL, "Wme", -1);
  531. Wd = networkstatus_get_bw_weight(NULL, "Wmd", -1);
  532. Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
  533. Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
  534. Web = networkstatus_get_bw_weight(NULL, "Web", -1);
  535. Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
  536. } else if (rule == WEIGHT_FOR_EXIT) {
  537. // Guards CAN be exits if they have weird exit policies
  538. // They are d then I guess...
  539. We = networkstatus_get_bw_weight(NULL, "Wee", -1);
  540. Wm = networkstatus_get_bw_weight(NULL, "Wem", -1); /* Odd exit policies */
  541. Wd = networkstatus_get_bw_weight(NULL, "Wed", -1);
  542. Wg = networkstatus_get_bw_weight(NULL, "Weg", -1); /* Odd exit policies */
  543. Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
  544. Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
  545. Web = networkstatus_get_bw_weight(NULL, "Web", -1);
  546. Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
  547. } else if (rule == WEIGHT_FOR_DIR) {
  548. We = networkstatus_get_bw_weight(NULL, "Wbe", -1);
  549. Wm = networkstatus_get_bw_weight(NULL, "Wbm", -1);
  550. Wd = networkstatus_get_bw_weight(NULL, "Wbd", -1);
  551. Wg = networkstatus_get_bw_weight(NULL, "Wbg", -1);
  552. Wgb = Wmb = Web = Wdb = weight_scale;
  553. } else if (rule == NO_WEIGHTING) {
  554. Wg = Wm = We = Wd = weight_scale;
  555. Wgb = Wmb = Web = Wdb = weight_scale;
  556. }
  557. if (Wg < 0 || Wm < 0 || We < 0 || Wd < 0 || Wgb < 0 || Wmb < 0 || Wdb < 0
  558. || Web < 0) {
  559. log_debug(LD_CIRC,
  560. "Got negative bandwidth weights. Defaulting to naive selection"
  561. " algorithm.");
  562. Wg = Wm = We = Wd = weight_scale;
  563. Wgb = Wmb = Web = Wdb = weight_scale;
  564. }
  565. Wg /= weight_scale;
  566. Wm /= weight_scale;
  567. We /= weight_scale;
  568. Wd /= weight_scale;
  569. Wgb /= weight_scale;
  570. Wmb /= weight_scale;
  571. Web /= weight_scale;
  572. Wdb /= weight_scale;
  573. bandwidths = tor_calloc(smartlist_len(sl), sizeof(double));
  574. // Cycle through smartlist and total the bandwidth.
  575. static int warned_missing_bw = 0;
  576. SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
  577. int is_exit = 0, is_guard = 0, is_dir = 0, this_bw = 0;
  578. double weight = 1;
  579. double weight_without_guard_flag = 0; /* Used for guardfraction */
  580. double final_weight = 0;
  581. is_exit = node->is_exit && ! node->is_bad_exit;
  582. is_guard = node->is_possible_guard;
  583. is_dir = node_is_dir(node);
  584. if (node->rs) {
  585. if (!node->rs->has_bandwidth) {
  586. /* This should never happen, unless all the authorities downgrade
  587. * to 0.2.0 or rogue routerstatuses get inserted into our consensus. */
  588. if (! warned_missing_bw) {
  589. log_warn(LD_BUG,
  590. "Consensus is missing some bandwidths. Using a naive "
  591. "router selection algorithm");
  592. warned_missing_bw = 1;
  593. }
  594. this_bw = 30000; /* Chosen arbitrarily */
  595. } else {
  596. this_bw = kb_to_bytes(node->rs->bandwidth_kb);
  597. }
  598. } else if (node->ri) {
  599. /* bridge or other descriptor not in our consensus */
  600. this_bw = bridge_get_advertised_bandwidth_bounded(node->ri);
  601. } else {
  602. /* We can't use this one. */
  603. continue;
  604. }
  605. if (is_guard && is_exit) {
  606. weight = (is_dir ? Wdb*Wd : Wd);
  607. weight_without_guard_flag = (is_dir ? Web*We : We);
  608. } else if (is_guard) {
  609. weight = (is_dir ? Wgb*Wg : Wg);
  610. weight_without_guard_flag = (is_dir ? Wmb*Wm : Wm);
  611. } else if (is_exit) {
  612. weight = (is_dir ? Web*We : We);
  613. } else { // middle
  614. weight = (is_dir ? Wmb*Wm : Wm);
  615. }
  616. /* These should be impossible; but overflows here would be bad, so let's
  617. * make sure. */
  618. if (this_bw < 0)
  619. this_bw = 0;
  620. if (weight < 0.0)
  621. weight = 0.0;
  622. if (weight_without_guard_flag < 0.0)
  623. weight_without_guard_flag = 0.0;
  624. /* If guardfraction information is available in the consensus, we
  625. * want to calculate this router's bandwidth according to its
  626. * guardfraction. Quoting from proposal236:
  627. *
  628. * Let Wpf denote the weight from the 'bandwidth-weights' line a
  629. * client would apply to N for position p if it had the guard
  630. * flag, Wpn the weight if it did not have the guard flag, and B the
  631. * measured bandwidth of N in the consensus. Then instead of choosing
  632. * N for position p proportionally to Wpf*B or Wpn*B, clients should
  633. * choose N proportionally to F*Wpf*B + (1-F)*Wpn*B.
  634. */
  635. if (node->rs && node->rs->has_guardfraction && rule != WEIGHT_FOR_GUARD) {
  636. /* XXX The assert should actually check for is_guard. However,
  637. * that crashes dirauths because of #13297. This should be
  638. * equivalent: */
  639. tor_assert(node->rs->is_possible_guard);
  640. guard_get_guardfraction_bandwidth(&guardfraction_bw,
  641. this_bw,
  642. node->rs->guardfraction_percentage);
  643. /* Calculate final_weight = F*Wpf*B + (1-F)*Wpn*B */
  644. final_weight =
  645. guardfraction_bw.guard_bw * weight +
  646. guardfraction_bw.non_guard_bw * weight_without_guard_flag;
  647. log_debug(LD_GENERAL, "%s: Guardfraction weight %f instead of %f (%s)",
  648. node->rs->nickname, final_weight, weight*this_bw,
  649. bandwidth_weight_rule_to_string(rule));
  650. } else { /* no guardfraction information. calculate the weight normally. */
  651. final_weight = weight*this_bw;
  652. }
  653. bandwidths[node_sl_idx] = final_weight;
  654. total_bandwidth += final_weight;
  655. } SMARTLIST_FOREACH_END(node);
  656. log_debug(LD_CIRC, "Generated weighted bandwidths for rule %s based "
  657. "on weights "
  658. "Wg=%f Wm=%f We=%f Wd=%f with total bw %f",
  659. bandwidth_weight_rule_to_string(rule),
  660. Wg, Wm, We, Wd, total_bandwidth);
  661. *bandwidths_out = bandwidths;
  662. if (total_bandwidth_out) {
  663. *total_bandwidth_out = total_bandwidth;
  664. }
  665. return 0;
  666. }
  667. /** For all nodes in <b>sl</b>, return the fraction of those nodes, weighted
  668. * by their weighted bandwidths with rule <b>rule</b>, for which we have
  669. * descriptors.
  670. *
  671. * If <b>for_direct_connect</b> is true, we intend to connect to the node
  672. * directly, as the first hop of a circuit; otherwise, we intend to connect
  673. * to it indirectly, or use it as if we were connecting to it indirectly. */
  674. double
  675. frac_nodes_with_descriptors(const smartlist_t *sl,
  676. bandwidth_weight_rule_t rule,
  677. int for_direct_conn)
  678. {
  679. double *bandwidths = NULL;
  680. double total, present;
  681. if (smartlist_len(sl) == 0)
  682. return 0.0;
  683. if (compute_weighted_bandwidths(sl, rule, &bandwidths, &total) < 0 ||
  684. total <= 0.0) {
  685. int n_with_descs = 0;
  686. SMARTLIST_FOREACH(sl, const node_t *, node, {
  687. if (node_has_preferred_descriptor(node, for_direct_conn))
  688. n_with_descs++;
  689. });
  690. tor_free(bandwidths);
  691. return ((double)n_with_descs) / smartlist_len(sl);
  692. }
  693. present = 0.0;
  694. SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
  695. if (node_has_preferred_descriptor(node, for_direct_conn))
  696. present += bandwidths[node_sl_idx];
  697. } SMARTLIST_FOREACH_END(node);
  698. tor_free(bandwidths);
  699. return present / total;
  700. }
  701. /** Choose a random element of status list <b>sl</b>, weighted by
  702. * the advertised bandwidth of each node */
  703. const node_t *
  704. node_sl_choose_by_bandwidth(const smartlist_t *sl,
  705. bandwidth_weight_rule_t rule)
  706. { /*XXXX MOVE */
  707. return smartlist_choose_node_by_bandwidth_weights(sl, rule);
  708. }
  709. /** Given a <b>router</b>, add every node_t in its family (including the
  710. * node itself!) to <b>sl</b>.
  711. *
  712. * Note the type mismatch: This function takes a routerinfo, but adds nodes
  713. * to the smartlist!
  714. */
  715. static void
  716. routerlist_add_node_and_family(smartlist_t *sl, const routerinfo_t *router)
  717. {
  718. /* XXXX MOVE ? */
  719. node_t fake_node;
  720. const node_t *node = node_get_by_id(router->cache_info.identity_digest);
  721. if (node == NULL) {
  722. memset(&fake_node, 0, sizeof(fake_node));
  723. fake_node.ri = (routerinfo_t *)router;
  724. memcpy(fake_node.identity, router->cache_info.identity_digest, DIGEST_LEN);
  725. node = &fake_node;
  726. }
  727. nodelist_add_node_and_family(sl, node);
  728. }
  729. /**
  730. * Remove every node_t that appears in <b>excluded</b> from <b>sl</b>.
  731. *
  732. * Behaves like smartlist_subtract, but uses nodelist_idx values to deliver
  733. * linear performance when smartlist_subtract would be quadratic.
  734. **/
  735. static void
  736. nodelist_subtract(smartlist_t *sl, const smartlist_t *excluded)
  737. {
  738. const smartlist_t *nodelist = nodelist_get_list();
  739. const int nodelist_len = smartlist_len(nodelist);
  740. bitarray_t *excluded_idx = bitarray_init_zero(nodelist_len);
  741. /* We haven't used nodelist_idx in this way previously, so I'm going to be
  742. * paranoid in this code, and check that nodelist_idx is correct for every
  743. * node before we use it. If we fail, we fall back to smartlist_subtract().
  744. */
  745. /* Set the excluded_idx bit corresponding to every excluded node...
  746. */
  747. SMARTLIST_FOREACH_BEGIN(excluded, const node_t *, node) {
  748. const int idx = node->nodelist_idx;
  749. if (BUG(idx < 0) || BUG(idx >= nodelist_len) ||
  750. BUG(node != smartlist_get(nodelist, idx))) {
  751. goto internal_error;
  752. }
  753. bitarray_set(excluded_idx, idx);
  754. } SMARTLIST_FOREACH_END(node);
  755. /* Then remove them from sl.
  756. */
  757. SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
  758. const int idx = node->nodelist_idx;
  759. if (BUG(idx < 0) || BUG(idx >= nodelist_len) ||
  760. BUG(node != smartlist_get(nodelist, idx))) {
  761. goto internal_error;
  762. }
  763. if (bitarray_is_set(excluded_idx, idx)) {
  764. SMARTLIST_DEL_CURRENT(sl, node);
  765. }
  766. } SMARTLIST_FOREACH_END(node);
  767. bitarray_free(excluded_idx);
  768. return;
  769. internal_error:
  770. log_warn(LD_BUG, "Internal error prevented us from using the fast method "
  771. "for subtracting nodelists. Falling back to the quadratic way.");
  772. smartlist_subtract(sl, excluded);
  773. bitarray_free(excluded_idx);
  774. }
  775. /** Return a random running node from the nodelist. Never
  776. * pick a node that is in
  777. * <b>excludedsmartlist</b>, or which matches <b>excludedset</b>,
  778. * even if they are the only nodes available.
  779. * If <b>CRN_NEED_UPTIME</b> is set in flags and any router has more than
  780. * a minimum uptime, return one of those.
  781. * If <b>CRN_NEED_CAPACITY</b> is set in flags, weight your choice by the
  782. * advertised capacity of each router.
  783. * If <b>CRN_NEED_GUARD</b> is set in flags, consider only Guard routers.
  784. * If <b>CRN_WEIGHT_AS_EXIT</b> is set in flags, we weight bandwidths as if
  785. * picking an exit node, otherwise we weight bandwidths for picking a relay
  786. * node (that is, possibly discounting exit nodes).
  787. * If <b>CRN_NEED_DESC</b> is set in flags, we only consider nodes that
  788. * have a routerinfo or microdescriptor -- that is, enough info to be
  789. * used to build a circuit.
  790. * If <b>CRN_PREF_ADDR</b> is set in flags, we only consider nodes that
  791. * have an address that is preferred by the ClientPreferIPv6ORPort setting
  792. * (regardless of this flag, we exclude nodes that aren't allowed by the
  793. * firewall, including ClientUseIPv4 0 and fascist_firewall_use_ipv6() == 0).
  794. */
  795. const node_t *
  796. router_choose_random_node(smartlist_t *excludedsmartlist,
  797. routerset_t *excludedset,
  798. router_crn_flags_t flags)
  799. { /* XXXX MOVE */
  800. const int need_uptime = (flags & CRN_NEED_UPTIME) != 0;
  801. const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
  802. const int need_guard = (flags & CRN_NEED_GUARD) != 0;
  803. const int weight_for_exit = (flags & CRN_WEIGHT_AS_EXIT) != 0;
  804. const int need_desc = (flags & CRN_NEED_DESC) != 0;
  805. const int pref_addr = (flags & CRN_PREF_ADDR) != 0;
  806. const int direct_conn = (flags & CRN_DIRECT_CONN) != 0;
  807. const int rendezvous_v3 = (flags & CRN_RENDEZVOUS_V3) != 0;
  808. const smartlist_t *node_list = nodelist_get_list();
  809. smartlist_t *sl=smartlist_new(),
  810. *excludednodes=smartlist_new();
  811. const node_t *choice = NULL;
  812. const routerinfo_t *r;
  813. bandwidth_weight_rule_t rule;
  814. tor_assert(!(weight_for_exit && need_guard));
  815. rule = weight_for_exit ? WEIGHT_FOR_EXIT :
  816. (need_guard ? WEIGHT_FOR_GUARD : WEIGHT_FOR_MID);
  817. SMARTLIST_FOREACH_BEGIN(node_list, const node_t *, node) {
  818. if (node_allows_single_hop_exits(node)) {
  819. /* Exclude relays that allow single hop exit circuits. This is an
  820. * obsolete option since 0.2.9.2-alpha and done by default in
  821. * 0.3.1.0-alpha. */
  822. smartlist_add(excludednodes, (node_t*)node);
  823. } else if (rendezvous_v3 &&
  824. !node_supports_v3_rendezvous_point(node)) {
  825. /* Exclude relays that do not support to rendezvous for a hidden service
  826. * version 3. */
  827. smartlist_add(excludednodes, (node_t*)node);
  828. }
  829. } SMARTLIST_FOREACH_END(node);
  830. /* If the node_t is not found we won't be to exclude ourself but we
  831. * won't be able to pick ourself in router_choose_random_node() so
  832. * this is fine to at least try with our routerinfo_t object. */
  833. if ((r = router_get_my_routerinfo()))
  834. routerlist_add_node_and_family(excludednodes, r);
  835. router_add_running_nodes_to_smartlist(sl, need_uptime, need_capacity,
  836. need_guard, need_desc, pref_addr,
  837. direct_conn);
  838. log_debug(LD_CIRC,
  839. "We found %d running nodes.",
  840. smartlist_len(sl));
  841. if (excludedsmartlist) {
  842. smartlist_add_all(excludednodes, excludedsmartlist);
  843. }
  844. nodelist_subtract(sl, excludednodes);
  845. if (excludedset) {
  846. routerset_subtract_nodes(sl,excludedset);
  847. log_debug(LD_CIRC,
  848. "We removed excludedset, leaving %d nodes.",
  849. smartlist_len(sl));
  850. }
  851. // Always weight by bandwidth
  852. choice = node_sl_choose_by_bandwidth(sl, rule);
  853. smartlist_free(sl);
  854. if (!choice && (need_uptime || need_capacity || need_guard || pref_addr)) {
  855. /* try once more -- recurse but with fewer restrictions. */
  856. log_info(LD_CIRC,
  857. "We couldn't find any live%s%s%s routers; falling back "
  858. "to list of all routers.",
  859. need_capacity?", fast":"",
  860. need_uptime?", stable":"",
  861. need_guard?", guard":"");
  862. flags &= ~ (CRN_NEED_UPTIME|CRN_NEED_CAPACITY|CRN_NEED_GUARD|
  863. CRN_PREF_ADDR);
  864. choice = router_choose_random_node(
  865. excludedsmartlist, excludedset, flags);
  866. }
  867. smartlist_free(excludednodes);
  868. if (!choice) {
  869. log_warn(LD_CIRC,
  870. "No available nodes when trying to choose node. Failing.");
  871. }
  872. return choice;
  873. }
  874. /** Try to find a running directory authority. Flags are as for
  875. * router_pick_directory_server.
  876. */
  877. const routerstatus_t *
  878. router_pick_trusteddirserver(dirinfo_type_t type, int flags)
  879. {
  880. return router_pick_dirserver_generic(
  881. router_get_trusted_dir_servers_mutable(),
  882. type, flags);
  883. }
  884. /** Try to find a running fallback directory. Flags are as for
  885. * router_pick_directory_server.
  886. */
  887. const routerstatus_t *
  888. router_pick_fallback_dirserver(dirinfo_type_t type, int flags)
  889. {
  890. return router_pick_dirserver_generic(
  891. router_get_fallback_dir_servers_mutable(),
  892. type, flags);
  893. }
  894. /** Pick a random element from a list of dir_server_t, weighting by their
  895. * <b>weight</b> field. */
  896. static const dir_server_t *
  897. dirserver_choose_by_weight(const smartlist_t *servers, double authority_weight)
  898. {
  899. int n = smartlist_len(servers);
  900. int i;
  901. double *weights_dbl;
  902. uint64_t *weights_u64;
  903. const dir_server_t *ds;
  904. weights_dbl = tor_calloc(n, sizeof(double));
  905. weights_u64 = tor_calloc(n, sizeof(uint64_t));
  906. for (i = 0; i < n; ++i) {
  907. ds = smartlist_get(servers, i);
  908. weights_dbl[i] = ds->weight;
  909. if (ds->is_authority)
  910. weights_dbl[i] *= authority_weight;
  911. }
  912. scale_array_elements_to_u64(weights_u64, weights_dbl, n, NULL);
  913. i = choose_array_element_by_weight(weights_u64, n);
  914. tor_free(weights_dbl);
  915. tor_free(weights_u64);
  916. return (i < 0) ? NULL : smartlist_get(servers, i);
  917. }
  918. /** Choose randomly from among the dir_server_ts in sourcelist that
  919. * are up. Flags are as for router_pick_directory_server_impl().
  920. */
  921. static const routerstatus_t *
  922. router_pick_trusteddirserver_impl(const smartlist_t *sourcelist,
  923. dirinfo_type_t type, int flags,
  924. int *n_busy_out)
  925. {
  926. const or_options_t *options = get_options();
  927. smartlist_t *direct, *tunnel;
  928. smartlist_t *overloaded_direct, *overloaded_tunnel;
  929. const routerinfo_t *me = router_get_my_routerinfo();
  930. const routerstatus_t *result = NULL;
  931. time_t now = time(NULL);
  932. const int requireother = ! (flags & PDS_ALLOW_SELF);
  933. const int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
  934. const int no_serverdesc_fetching =(flags & PDS_NO_EXISTING_SERVERDESC_FETCH);
  935. const int no_microdesc_fetching =(flags & PDS_NO_EXISTING_MICRODESC_FETCH);
  936. const double auth_weight =
  937. (sourcelist == router_get_fallback_dir_servers()) ?
  938. options->DirAuthorityFallbackRate : 1.0;
  939. smartlist_t *pick_from;
  940. int n_busy = 0;
  941. int try_excluding = 1, n_excluded = 0;
  942. int try_ip_pref = 1;
  943. if (!sourcelist)
  944. return NULL;
  945. retry_search:
  946. direct = smartlist_new();
  947. tunnel = smartlist_new();
  948. overloaded_direct = smartlist_new();
  949. overloaded_tunnel = smartlist_new();
  950. const int skip_or_fw = router_skip_or_reachability(options, try_ip_pref);
  951. const int skip_dir_fw = router_skip_dir_reachability(options, try_ip_pref);
  952. const int must_have_or = directory_must_use_begindir(options);
  953. SMARTLIST_FOREACH_BEGIN(sourcelist, const dir_server_t *, d)
  954. {
  955. int is_overloaded =
  956. d->fake_status.last_dir_503_at + DIR_503_TIMEOUT > now;
  957. if (!d->is_running) continue;
  958. if ((type & d->type) == 0)
  959. continue;
  960. SKIP_MISSING_TRUSTED_EXTRAINFO(type, d->digest);
  961. if (requireother && me && router_digest_is_me(d->digest))
  962. continue;
  963. if (try_excluding &&
  964. routerset_contains_routerstatus(options->ExcludeNodes,
  965. &d->fake_status, -1)) {
  966. ++n_excluded;
  967. continue;
  968. }
  969. if (router_is_already_dir_fetching_(d->addr,
  970. &d->ipv6_addr,
  971. d->dir_port,
  972. no_serverdesc_fetching,
  973. no_microdesc_fetching)) {
  974. ++n_busy;
  975. continue;
  976. }
  977. /* Clients use IPv6 addresses if the server has one and the client
  978. * prefers IPv6.
  979. * Add the router if its preferred address and port are reachable.
  980. * If we don't get any routers, we'll try again with the non-preferred
  981. * address for each router (if any). (To ensure correct load-balancing
  982. * we try routers that only have one address both times.)
  983. */
  984. if (!fascistfirewall || skip_or_fw ||
  985. fascist_firewall_allows_dir_server(d, FIREWALL_OR_CONNECTION,
  986. try_ip_pref))
  987. smartlist_add(is_overloaded ? overloaded_tunnel : tunnel, (void*)d);
  988. else if (!must_have_or && (skip_dir_fw ||
  989. fascist_firewall_allows_dir_server(d, FIREWALL_DIR_CONNECTION,
  990. try_ip_pref)))
  991. smartlist_add(is_overloaded ? overloaded_direct : direct, (void*)d);
  992. }
  993. SMARTLIST_FOREACH_END(d);
  994. if (smartlist_len(tunnel)) {
  995. pick_from = tunnel;
  996. } else if (smartlist_len(overloaded_tunnel)) {
  997. pick_from = overloaded_tunnel;
  998. } else if (smartlist_len(direct)) {
  999. pick_from = direct;
  1000. } else {
  1001. pick_from = overloaded_direct;
  1002. }
  1003. {
  1004. const dir_server_t *selection =
  1005. dirserver_choose_by_weight(pick_from, auth_weight);
  1006. if (selection)
  1007. result = &selection->fake_status;
  1008. }
  1009. smartlist_free(direct);
  1010. smartlist_free(tunnel);
  1011. smartlist_free(overloaded_direct);
  1012. smartlist_free(overloaded_tunnel);
  1013. RETRY_ALTERNATE_IP_VERSION(retry_search);
  1014. RETRY_WITHOUT_EXCLUDE(retry_search);
  1015. router_picked_poor_directory_log(result);
  1016. if (n_busy_out)
  1017. *n_busy_out = n_busy;
  1018. return result;
  1019. }