btrack_orconn.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright (c) 2007-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file btrack_orconn.c
  5. * \brief Bootstrap tracker for OR connections
  6. *
  7. * Track state changes of OR connections, as published by the
  8. * connection subsystem. Also track circuit launch events, because
  9. * they're one of the few ways to discover the association between a
  10. * channel (and OR connection) and a circuit.
  11. *
  12. * We track all OR connections that we receive events for, whether or
  13. * not they're carrying origin circuits. (An OR connection might
  14. * carry origin circuits only after we first find out about that
  15. * connection.)
  16. *
  17. * All origin ORCONN events update the "any" state variables, while
  18. * only application ORCONN events update the "ap" state variables (and
  19. * also update the "any") variables.
  20. *
  21. * We do this because we want to report the first increments of
  22. * connection progress as the earliest bootstrap phases. This results
  23. * in a better user experience because failures here translate into
  24. * zero or very small amounts of displayed progress, instead of
  25. * progress stuck near completion. The first connection to a relay
  26. * might be a one-hop circuit for directory lookups, or it might be a
  27. * connection for an application circuit because we already have
  28. * enough directory info to build an application circuit.
  29. *
  30. * We call functions in btrack_orconn_cevent.c to generate the actual
  31. * controller events, because some of the state decoding we need to do
  32. * is complicated.
  33. **/
  34. #include <stdbool.h>
  35. #include "core/or/or.h"
  36. #define BTRACK_ORCONN_PRIVATE
  37. #include "core/or/ocirc_event.h"
  38. #include "core/or/orconn_event.h"
  39. #include "feature/control/btrack_orconn.h"
  40. #include "feature/control/btrack_orconn_cevent.h"
  41. #include "feature/control/btrack_orconn_maps.h"
  42. #include "lib/log/log.h"
  43. #include "lib/pubsub/pubsub.h"
  44. DECLARE_SUBSCRIBE(orconn_state, bto_state_rcvr);
  45. DECLARE_SUBSCRIBE(orconn_status, bto_status_rcvr);
  46. DECLARE_SUBSCRIBE(ocirc_chan, bto_chan_rcvr);
  47. /** Pair of a best ORCONN GID and with its state */
  48. typedef struct bto_best_t {
  49. uint64_t gid;
  50. int state;
  51. } bto_best_t;
  52. /** GID and state of the best ORCONN we've seen so far */
  53. static bto_best_t best_any = { 0, -1 };
  54. /** GID and state of the best application circuit ORCONN we've seen so far */
  55. static bto_best_t best_ap = { 0, -1 };
  56. /**
  57. * Update a cached state of a best ORCONN progress we've seen so far.
  58. *
  59. * Return true if the new state is better than the old.
  60. **/
  61. static bool
  62. bto_update_best(const bt_orconn_t *bto, bto_best_t *best, const char *type)
  63. {
  64. if (bto->state < best->state)
  65. return false;
  66. /* Update even if we won't change best->state, because it's more
  67. * recent information that a particular connection transitioned to
  68. * that state. */
  69. best->gid = bto->gid;
  70. if (bto->state > best->state) {
  71. log_info(LD_BTRACK, "ORCONN BEST_%s state %d->%d gid=%"PRIu64, type,
  72. best->state, bto->state, bto->gid);
  73. best->state = bto->state;
  74. return true;
  75. }
  76. return false;
  77. }
  78. /**
  79. * Update cached states of best ORCONN progress we've seen
  80. *
  81. * Only update the application ORCONN state if we know it's carrying
  82. * an application circuit.
  83. **/
  84. static void
  85. bto_update_bests(const bt_orconn_t *bto)
  86. {
  87. tor_assert(bto->is_orig);
  88. if (bto_update_best(bto, &best_any, "ANY"))
  89. bto_cevent_anyconn(bto);
  90. if (!bto->is_onehop && bto_update_best(bto, &best_ap, "AP"))
  91. bto_cevent_apconn(bto);
  92. }
  93. /** Reset cached "best" values */
  94. static void
  95. bto_reset_bests(void)
  96. {
  97. best_any.gid = best_ap.gid = 0;
  98. best_any.state = best_ap.state = -1;
  99. }
  100. /**
  101. * Update cached states of ORCONNs from the incoming message. This
  102. * message comes from code in connection_or.c.
  103. **/
  104. static void
  105. bto_state_rcvr(const msg_t *msg, const orconn_state_msg_t *arg)
  106. {
  107. bt_orconn_t *bto;
  108. (void)msg;
  109. bto = bto_find_or_new(arg->gid, arg->chan);
  110. log_debug(LD_BTRACK, "ORCONN gid=%"PRIu64" chan=%"PRIu64
  111. " proxy_type=%d state=%d",
  112. arg->gid, arg->chan, arg->proxy_type, arg->state);
  113. bto->proxy_type = arg->proxy_type;
  114. bto->state = arg->state;
  115. if (bto->is_orig)
  116. bto_update_bests(bto);
  117. }
  118. /**
  119. * Delete a cached ORCONN state if we get an incoming message saying
  120. * the ORCONN is failed or closed. This message comes from code in
  121. * control.c.
  122. **/
  123. static void
  124. bto_status_rcvr(const msg_t *msg, const orconn_status_msg_t *arg)
  125. {
  126. (void)msg;
  127. switch (arg->status) {
  128. case OR_CONN_EVENT_FAILED:
  129. case OR_CONN_EVENT_CLOSED:
  130. log_info(LD_BTRACK, "ORCONN DELETE gid=%"PRIu64" status=%d reason=%d",
  131. arg->gid, arg->status, arg->reason);
  132. return bto_delete(arg->gid);
  133. default:
  134. break;
  135. }
  136. }
  137. /**
  138. * Create or update a cached ORCONN state for a newly launched
  139. * connection, including whether it's launched by an origin circuit
  140. * and whether it's a one-hop circuit.
  141. **/
  142. static void
  143. bto_chan_rcvr(const msg_t *msg, const ocirc_chan_msg_t *arg)
  144. {
  145. bt_orconn_t *bto;
  146. (void)msg;
  147. bto = bto_find_or_new(0, arg->chan);
  148. if (!bto->is_orig || (bto->is_onehop && !arg->onehop)) {
  149. log_debug(LD_BTRACK, "ORCONN LAUNCH chan=%"PRIu64" onehop=%d",
  150. arg->chan, arg->onehop);
  151. }
  152. bto->is_orig = true;
  153. if (!arg->onehop)
  154. bto->is_onehop = false;
  155. bto_update_bests(bto);
  156. }
  157. /**
  158. * Initialize the hash maps and subscribe to ORCONN and origin
  159. * circuit events.
  160. **/
  161. int
  162. btrack_orconn_init(void)
  163. {
  164. bto_init_maps();
  165. return 0;
  166. }
  167. int
  168. btrack_orconn_add_pubsub(pubsub_connector_t *connector)
  169. {
  170. if (DISPATCH_ADD_SUB(connector, orconn, orconn_state))
  171. return -1;
  172. if (DISPATCH_ADD_SUB(connector, orconn, orconn_status))
  173. return -1;
  174. if (DISPATCH_ADD_SUB(connector, ocirc, ocirc_chan))
  175. return -1;
  176. return 0;
  177. }
  178. /** Clear the hash maps and reset the "best" states */
  179. void
  180. btrack_orconn_fini(void)
  181. {
  182. bto_clear_maps();
  183. bto_reset_bests();
  184. bto_cevent_reset();
  185. }