tor  0.4.2.1-alpha-dev
btrack_orconn.c
Go to the documentation of this file.
1 /* Copyright (c) 2007-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
36 #include <stdbool.h>
37 
38 #include "core/or/or.h"
39 
40 #define BTRACK_ORCONN_PRIVATE
41 
42 #include "core/or/ocirc_event.h"
43 #include "core/or/orconn_event.h"
47 #include "lib/log/log.h"
48 #include "lib/pubsub/pubsub.h"
49 
50 DECLARE_SUBSCRIBE(orconn_state, bto_state_rcvr);
51 DECLARE_SUBSCRIBE(orconn_status, bto_status_rcvr);
52 DECLARE_SUBSCRIBE(ocirc_chan, bto_chan_rcvr);
53 
55 typedef struct bto_best_t {
56  uint64_t gid;
57  int state;
58 } bto_best_t;
59 
61 static bto_best_t best_any = { 0, -1 };
63 static bto_best_t best_ap = { 0, -1 };
64 
70 static bool
71 bto_update_best(const bt_orconn_t *bto, bto_best_t *best, const char *type)
72 {
73  if (bto->state < best->state)
74  return false;
75  /* Update even if we won't change best->state, because it's more
76  * recent information that a particular connection transitioned to
77  * that state. */
78  best->gid = bto->gid;
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;
83  return true;
84  }
85  return false;
86 }
87 
94 static void
95 bto_update_bests(const bt_orconn_t *bto)
96 {
97  tor_assert(bto->is_orig);
98 
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);
103 }
104 
106 static void
108 {
109  best_any.gid = best_ap.gid = 0;
110  best_any.state = best_ap.state = -1;
111 }
112 
117 static void
118 bto_state_rcvr(const msg_t *msg, const orconn_state_msg_t *arg)
119 {
120  bt_orconn_t *bto;
121 
122  (void)msg;
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;
129  if (bto->is_orig)
130  bto_update_bests(bto);
131 }
132 
138 static void
140 {
141  (void)msg;
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);
148  default:
149  break;
150  }
151 }
152 
158 static void
159 bto_chan_rcvr(const msg_t *msg, const ocirc_chan_msg_t *arg)
160 {
161  bt_orconn_t *bto;
162 
163  (void)msg;
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);
168  }
169  bto->is_orig = true;
170  if (!arg->onehop)
171  bto->is_onehop = false;
172  bto_update_bests(bto);
173 }
174 
179 int
181 {
182  bto_init_maps();
183 
184  return 0;
185 }
186 
187 int
188 btrack_orconn_add_pubsub(pubsub_connector_t *connector)
189 {
190  if (DISPATCH_ADD_SUB(connector, orconn, orconn_state))
191  return -1;
192  if (DISPATCH_ADD_SUB(connector, orconn, orconn_status))
193  return -1;
194  if (DISPATCH_ADD_SUB(connector, ocirc, ocirc_chan))
195  return -1;
196  return 0;
197 }
198 
200 void
202 {
203  bto_clear_maps();
204  bto_reset_bests();
206 }
#define DECLARE_SUBSCRIBE(messagename, hookfn)
Header file for btrack_orconn_cevent.c.
struct pubsub_connector_t pubsub_connector_t
static void bto_chan_rcvr(const msg_t *msg, const ocirc_chan_msg_t *arg)
int btrack_orconn_init(void)
static bool bto_update_best(const bt_orconn_t *bto, bto_best_t *best, const char *type)
Definition: btrack_orconn.c:71
void bto_init_maps(void)
void bto_cevent_apconn(const bt_orconn_t *bto)
Header file for orconn_event.c.
static void bto_state_rcvr(const msg_t *msg, const orconn_state_msg_t *arg)
static void bto_reset_bests(void)
tor_assert(buffer)
Master header file for Tor-specific functionality.
Header file for btrack_orconn.c.
void bto_cevent_reset(void)
Definition: msgtypes.h:50
static void bto_update_bests(const bt_orconn_t *bto)
Definition: btrack_orconn.c:95
void btrack_orconn_fini(void)
Header file for btrack_orconn_maps.c.
void bto_cevent_anyconn(const bt_orconn_t *bto)
#define LD_BTRACK
Definition: log.h:117
void bto_clear_maps(void)
static void bto_status_rcvr(const msg_t *msg, const orconn_status_msg_t *arg)
static bto_best_t best_ap
Definition: btrack_orconn.c:63
Header for OO publish-subscribe functionality.
struct bto_best_t bto_best_t
Header file for ocirc_event.c.
Headers for log.c.
static bto_best_t best_any
Definition: btrack_orconn.c:61
void bto_delete(uint64_t gid)
bt_orconn_t * bto_find_or_new(uint64_t gid, uint64_t chan)