1 /* Copyright (c) 2007-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
5 * \file btrack_orconn.c
6 * \brief Bootstrap tracker for OR connections
8 * Track state changes of OR connections, as published by the
9 * connection subsystem. Also track circuit launch events, because
10 * they're one of the few ways to discover the association between a
11 * channel (and OR connection) and a circuit.
13 * We track all OR connections that we receive events for, whether or
14 * not they're carrying origin circuits. (An OR connection might
15 * carry origin circuits only after we first find out about that
18 * All origin ORCONN events update the "any" state variables, while
19 * only application ORCONN events update the "ap" state variables (and
20 * also update the "any") variables.
22 * We do this because we want to report the first increments of
23 * connection progress as the earliest bootstrap phases. This results
24 * in a better user experience because failures here translate into
25 * zero or very small amounts of displayed progress, instead of
26 * progress stuck near completion. The first connection to a relay
27 * might be a one-hop circuit for directory lookups, or it might be a
28 * connection for an application circuit because we already have
29 * enough directory info to build an application circuit.
31 * We call functions in btrack_orconn_cevent.c to generate the actual
32 * controller events, because some of the state decoding we need to do
38 #include "core/or/or.h"
40 #define BTRACK_ORCONN_PRIVATE
42 #include "core/or/ocirc_event.h"
43 #include "core/or/orconn_event.h"
44 #include "feature/control/btrack_orconn.h"
45 #include "feature/control/btrack_orconn_cevent.h"
46 #include "feature/control/btrack_orconn_maps.h"
47 #include "lib/log/log.h"
48 #include "lib/pubsub/pubsub.h"
50 DECLARE_SUBSCRIBE(orconn_state
, bto_state_rcvr
);
51 DECLARE_SUBSCRIBE(orconn_status
, bto_status_rcvr
);
52 DECLARE_SUBSCRIBE(ocirc_chan
, bto_chan_rcvr
);
54 /** Pair of a best ORCONN GID and with its state */
55 typedef struct bto_best_t
{
60 /** GID and state of the best ORCONN we've seen so far */
61 static bto_best_t best_any
= { 0, -1 };
62 /** GID and state of the best application circuit ORCONN we've seen so far */
63 static bto_best_t best_ap
= { 0, -1 };
66 * Update a cached state of a best ORCONN progress we've seen so far.
68 * Return true if the new state is better than the old.
71 bto_update_best(const bt_orconn_t
*bto
, bto_best_t
*best
, const char *type
)
73 if (bto
->state
< best
->state
)
75 /* Update even if we won't change best->state, because it's more
76 * recent information that a particular connection transitioned to
79 if (bto
->state
> best
->state
) {
80 log_info(LD_BTRACK
, "ORCONN BEST_%s state %d->%d gid=%"PRIu64
, type
,
81 best
->state
, bto
->state
, bto
->gid
);
82 best
->state
= bto
->state
;
89 * Update cached states of best ORCONN progress we've seen
91 * Only update the application ORCONN state if we know it's carrying
92 * an application circuit.
95 bto_update_bests(const bt_orconn_t
*bto
)
97 tor_assert(bto
->is_orig
);
99 if (bto_update_best(bto
, &best_any
, "ANY"))
100 bto_cevent_anyconn(bto
);
101 if (!bto
->is_onehop
&& bto_update_best(bto
, &best_ap
, "AP"))
102 bto_cevent_apconn(bto
);
105 /** Reset cached "best" values */
107 bto_reset_bests(void)
109 best_any
.gid
= best_ap
.gid
= 0;
110 best_any
.state
= best_ap
.state
= -1;
114 * Update cached states of ORCONNs from the incoming message. This
115 * message comes from code in connection_or.c.
118 bto_state_rcvr(const msg_t
*msg
, const orconn_state_msg_t
*arg
)
123 bto
= bto_find_or_new(arg
->gid
, arg
->chan
);
124 log_debug(LD_BTRACK
, "ORCONN gid=%"PRIu64
" chan=%"PRIu64
125 " proxy_type=%d state=%d",
126 arg
->gid
, arg
->chan
, arg
->proxy_type
, arg
->state
);
127 bto
->proxy_type
= arg
->proxy_type
;
128 bto
->state
= arg
->state
;
130 bto_update_bests(bto
);
134 * Delete a cached ORCONN state if we get an incoming message saying
135 * the ORCONN is failed or closed. This message comes from code in
139 bto_status_rcvr(const msg_t
*msg
, const orconn_status_msg_t
*arg
)
142 switch (arg
->status
) {
143 case OR_CONN_EVENT_FAILED
:
144 case OR_CONN_EVENT_CLOSED
:
145 log_info(LD_BTRACK
, "ORCONN DELETE gid=%"PRIu64
" status=%d reason=%d",
146 arg
->gid
, arg
->status
, arg
->reason
);
147 return bto_delete(arg
->gid
);
154 * Create or update a cached ORCONN state for a newly launched
155 * connection, including whether it's launched by an origin circuit
156 * and whether it's a one-hop circuit.
159 bto_chan_rcvr(const msg_t
*msg
, const ocirc_chan_msg_t
*arg
)
164 bto
= bto_find_or_new(0, arg
->chan
);
165 if (!bto
->is_orig
|| (bto
->is_onehop
&& !arg
->onehop
)) {
166 log_debug(LD_BTRACK
, "ORCONN LAUNCH chan=%"PRIu64
" onehop=%d",
167 arg
->chan
, arg
->onehop
);
171 bto
->is_onehop
= false;
172 bto_update_bests(bto
);
176 * Initialize the hash maps and subscribe to ORCONN and origin
180 btrack_orconn_init(void)
188 btrack_orconn_add_pubsub(pubsub_connector_t
*connector
)
190 if (DISPATCH_ADD_SUB(connector
, orconn
, orconn_state
))
192 if (DISPATCH_ADD_SUB(connector
, orconn
, orconn_status
))
194 if (DISPATCH_ADD_SUB(connector
, ocirc
, ocirc_chan
))
199 /** Clear the hash maps and reset the "best" states */
201 btrack_orconn_fini(void)