btrack_orconn.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright (c) 2007-2018, 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. /** Pair of a best ORCONN GID and with its state */
  44. typedef struct bto_best_t {
  45. uint64_t gid;
  46. int state;
  47. } bto_best_t;
  48. /** GID and state of the best ORCONN we've seen so far */
  49. static bto_best_t best_any = { 0, -1 };
  50. /** GID and state of the best application circuit ORCONN we've seen so far */
  51. static bto_best_t best_ap = { 0, -1 };
  52. /**
  53. * Update a cached state of a best ORCONN progress we've seen so far.
  54. *
  55. * Return true if the new state is better than the old.
  56. **/
  57. static bool
  58. bto_update_best(const bt_orconn_t *bto, bto_best_t *best, const char *type)
  59. {
  60. if (bto->state < best->state)
  61. return false;
  62. /* Update even if we won't change best->state, because it's more
  63. * recent information that a particular connection transitioned to
  64. * that state. */
  65. best->gid = bto->gid;
  66. if (bto->state > best->state) {
  67. log_info(LD_BTRACK, "ORCONN BEST_%s state %d->%d gid=%"PRIu64, type,
  68. best->state, bto->state, bto->gid);
  69. best->state = bto->state;
  70. return true;
  71. }
  72. return false;
  73. }
  74. /**
  75. * Update cached states of best ORCONN progress we've seen
  76. *
  77. * Only update the application ORCONN state if we know it's carrying
  78. * an application circuit.
  79. **/
  80. static void
  81. bto_update_bests(const bt_orconn_t *bto)
  82. {
  83. tor_assert(bto->is_orig);
  84. if (bto_update_best(bto, &best_any, "ANY"))
  85. bto_cevent_anyconn(bto);
  86. if (!bto->is_onehop && bto_update_best(bto, &best_ap, "AP"))
  87. bto_cevent_apconn(bto);
  88. }
  89. /** Reset cached "best" values */
  90. static void
  91. bto_reset_bests(void)
  92. {
  93. best_any.gid = best_ap.gid = 0;
  94. best_any.state = best_ap.state = -1;
  95. }
  96. /**
  97. * Update cached states of ORCONNs from the incoming message. This
  98. * message comes from code in connection_or.c.
  99. **/
  100. static void
  101. bto_state_rcvr(const orconn_state_msg_t *msg)
  102. {
  103. bt_orconn_t *bto;
  104. bto = bto_find_or_new(msg->gid, msg->chan);
  105. log_debug(LD_BTRACK, "ORCONN gid=%"PRIu64" chan=%"PRIu64
  106. " proxy_type=%d state=%d",
  107. msg->gid, msg->chan, msg->proxy_type, msg->state);
  108. bto->proxy_type = msg->proxy_type;
  109. bto->state = msg->state;
  110. if (bto->is_orig)
  111. bto_update_bests(bto);
  112. }
  113. /**
  114. * Delete a cached ORCONN state if we get an incoming message saying
  115. * the ORCONN is failed or closed. This message comes from code in
  116. * control.c.
  117. **/
  118. static void
  119. bto_status_rcvr(const orconn_status_msg_t *msg)
  120. {
  121. switch (msg->status) {
  122. case OR_CONN_EVENT_FAILED:
  123. case OR_CONN_EVENT_CLOSED:
  124. log_info(LD_BTRACK, "ORCONN DELETE gid=%"PRIu64" status=%d reason=%d",
  125. msg->gid, msg->status, msg->reason);
  126. return bto_delete(msg->gid);
  127. default:
  128. break;
  129. }
  130. }
  131. /** Dispatch to individual ORCONN message handlers */
  132. static void
  133. bto_event_rcvr(const orconn_event_msg_t *msg)
  134. {
  135. switch (msg->type) {
  136. case ORCONN_MSGTYPE_STATE:
  137. return bto_state_rcvr(&msg->u.state);
  138. case ORCONN_MSGTYPE_STATUS:
  139. return bto_status_rcvr(&msg->u.status);
  140. default:
  141. tor_assert(false);
  142. }
  143. }
  144. /**
  145. * Create or update a cached ORCONN state for a newly launched
  146. * connection, including whether it's launched by an origin circuit
  147. * and whether it's a one-hop circuit.
  148. **/
  149. static void
  150. bto_chan_rcvr(const ocirc_event_msg_t *msg)
  151. {
  152. bt_orconn_t *bto;
  153. /* Ignore other kinds of origin circuit events; we don't need them */
  154. if (msg->type != OCIRC_MSGTYPE_CHAN)
  155. return;
  156. bto = bto_find_or_new(0, msg->u.chan.chan);
  157. if (!bto->is_orig || (bto->is_onehop && !msg->u.chan.onehop)) {
  158. log_debug(LD_BTRACK, "ORCONN LAUNCH chan=%"PRIu64" onehop=%d",
  159. msg->u.chan.chan, msg->u.chan.onehop);
  160. }
  161. bto->is_orig = true;
  162. if (!msg->u.chan.onehop)
  163. bto->is_onehop = false;
  164. bto_update_bests(bto);
  165. }
  166. /**
  167. * Initialize the hash maps and subscribe to ORCONN and origin
  168. * circuit events.
  169. **/
  170. int
  171. btrack_orconn_init(void)
  172. {
  173. bto_init_maps();
  174. orconn_event_subscribe(bto_event_rcvr);
  175. ocirc_event_subscribe(bto_chan_rcvr);
  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. }