aboutsummaryrefslogtreecommitdiff
path: root/src/feature/control/btrack_circuit.c
blob: be51b51046006e2c1c9a6c72682d01d55940894e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file btrack_circuit.c
 * \brief Bootstrap tracker for origin circuits
 *
 * Track state changes of origin circuits, as published by the circuit
 * subsystem.
 **/

#include "core/or/or.h"

#include "core/or/ocirc_event.h"

#include "feature/control/btrack_circuit.h"
#include "feature/control/control.h"
#include "lib/log/log.h"

/** Pair of a best origin circuit GID with its state or status */
typedef struct btc_best_t {
  uint32_t gid;
  int val;
} btc_best_t;

/** GID and state of the best origin circuit we've seen so far */
static btc_best_t best_any_state = { 0, -1 };
/** GID and state of the best application circuit we've seen so far */
static btc_best_t best_ap_state = { 0, -1 };
/** GID and status of the best origin circuit we've seen so far */
static btc_best_t best_any_evtype = { 0, -1 };
/** GID and status of the best application circuit we've seen so far */
static btc_best_t best_ap_evtype = { 0, -1 };

/** Reset cached "best" values */
static void
btc_reset_bests(void)
{
  best_any_state.gid = best_ap_state.gid = 0;
  best_any_state.val = best_ap_state.val = -1;
  best_any_evtype.gid = best_ap_state.gid = 0;
  best_any_evtype.val = best_ap_evtype.val = -1;
}

/** True if @a state is a "better" origin circuit state than @a best->val */
static bool
btc_state_better(int state, const btc_best_t *best)
{
  return state > best->val;
}

/**
 * Definine an ordering on circuit status events
 *
 * The CIRC_EVENT_ constants aren't sorted in a useful order, so this
 * array helps to decode them.  This approach depends on the statuses
 * being nonnegative and dense.
 **/
static int circ_event_order[] = {
  [CIRC_EVENT_FAILED] = -1,
  [CIRC_EVENT_CLOSED] = -1,
  [CIRC_EVENT_LAUNCHED] = 1,
  [CIRC_EVENT_EXTENDED] = 2,
  [CIRC_EVENT_BUILT] = 3,
};
#define N_CIRC_EVENT_ORDER \
  (sizeof(circ_event_order) / sizeof(circ_event_order[0]))

/** True if @a state is a "better" origin circuit event status than @a
    best->val */
static bool
btc_evtype_better(int state, const btc_best_t *best)
{
  if (state < 0)
    return false;
  if (best->val < 0)
    return true;

  tor_assert(state >= 0 && (unsigned)state < N_CIRC_EVENT_ORDER);
  tor_assert(best->val >= 0 && (unsigned)best->val < N_CIRC_EVENT_ORDER);
  return circ_event_order[state] > circ_event_order[best->val];
}

static bool
btc_update_state(const ocirc_state_msg_t *msg, btc_best_t *best,
                 const char *type)
{
  if (btc_state_better(msg->state, best)) {
    log_info(LD_BTRACK, "CIRC BEST_%s state %d->%d gid=%"PRIu32, type,
             best->val, msg->state, msg->gid);
    best->gid = msg->gid;
    best->val = msg->state;
    return true;
  }
  return false;
}

static bool
btc_update_evtype(const ocirc_cevent_msg_t *msg, btc_best_t *best,
                  const char *type)
{
  if (btc_evtype_better(msg->evtype, best)) {
    log_info(LD_BTRACK, "CIRC BEST_%s evtype %d->%d gid=%"PRIu32, type,
             best->val, msg->evtype, msg->gid);
    best->gid = msg->gid;
    best->val = msg->evtype;
    return true;
  }
  return false;
}

DECLARE_SUBSCRIBE(ocirc_state, btc_state_rcvr);
DECLARE_SUBSCRIBE(ocirc_cevent, btc_cevent_rcvr);
DECLARE_SUBSCRIBE(ocirc_chan, btc_chan_rcvr);

static void
btc_state_rcvr(const msg_t *msg, const ocirc_state_msg_t *arg)
{
  (void)msg;
  log_debug(LD_BTRACK, "CIRC gid=%"PRIu32" state=%d onehop=%d",
            arg->gid, arg->state, arg->onehop);

  btc_update_state(arg, &best_any_state, "ANY");
  if (arg->onehop)
    return;
  btc_update_state(arg, &best_ap_state, "AP");
}

static void
btc_cevent_rcvr(const msg_t *msg, const ocirc_cevent_msg_t *arg)
{
  (void)msg;
  log_debug(LD_BTRACK, "CIRC gid=%"PRIu32" evtype=%d reason=%d onehop=%d",
            arg->gid, arg->evtype, arg->reason, arg->onehop);

  btc_update_evtype(arg, &best_any_evtype, "ANY");
  if (arg->onehop)
    return;
  btc_update_evtype(arg, &best_ap_evtype, "AP");
}

static void
btc_chan_rcvr(const msg_t *msg, const ocirc_chan_msg_t *arg)
{
  (void)msg;
  log_debug(LD_BTRACK, "CIRC gid=%"PRIu32" chan=%"PRIu64" onehop=%d",
            arg->gid, arg->chan, arg->onehop);
}

int
btrack_circ_add_pubsub(pubsub_connector_t *connector)
{
  if (DISPATCH_ADD_SUB(connector, ocirc, ocirc_chan))
    return -1;
  if (DISPATCH_ADD_SUB(connector, ocirc, ocirc_cevent))
    return -1;
  if (DISPATCH_ADD_SUB(connector, ocirc, ocirc_state))
    return -1;
  return 0;
}

void
btrack_circ_fini(void)
{
  btc_reset_bests();
}