aboutsummaryrefslogtreecommitdiff
path: root/src/feature/control/btrack_orconn_maps.c
blob: a60dffb8c4dd2c05c9db5d14bb74bca68a6e9705 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file btrack_orconn_maps.c
 * \brief Hash map implementation for btrack_orconn.c
 *
 * These functions manipulate the hash maps that contain bt_orconn
 * objects.
 **/

#include <stdbool.h>

#include "core/or/or.h"

#include "ht.h"
#include "siphash.h"

#define BTRACK_ORCONN_PRIVATE

#include "feature/control/btrack_orconn.h"
#include "feature/control/btrack_orconn_maps.h"
#include "lib/log/log.h"

static inline unsigned int
bto_gid_hash_(bt_orconn_t *elm)
{
  return (unsigned)siphash24g(&elm->gid, sizeof(elm->gid));
}

static inline int
bto_gid_eq_(bt_orconn_t *a, bt_orconn_t *b)
{
  return a->gid == b->gid;
}

static inline unsigned int
bto_chan_hash_(bt_orconn_t *elm)
{
  return (unsigned)siphash24g(&elm->chan, sizeof(elm->chan));
}

static inline int
bto_chan_eq_(bt_orconn_t *a, bt_orconn_t *b)
{
  return a->chan == b->chan;
}

HT_HEAD(bto_gid_ht, bt_orconn_t);
HT_PROTOTYPE(bto_gid_ht, bt_orconn_t, node, bto_gid_hash_, bto_gid_eq_);
HT_GENERATE2(bto_gid_ht, bt_orconn_t, node,
             bto_gid_hash_, bto_gid_eq_, 0.6,
             tor_reallocarray_, tor_free_);
static struct bto_gid_ht *bto_gid_map;

HT_HEAD(bto_chan_ht, bt_orconn_t);
HT_PROTOTYPE(bto_chan_ht, bt_orconn_t, chan_node, bto_chan_hash_,
             bto_chan_eq_);
HT_GENERATE2(bto_chan_ht, bt_orconn_t, chan_node,
             bto_chan_hash_, bto_chan_eq_, 0.6,
             tor_reallocarray_, tor_free_);
static struct bto_chan_ht *bto_chan_map;

/** Clear the GID hash map, freeing any bt_orconn_t objects that become
 * unreferenced */
static void
bto_gid_clear_map(void)
{
  bt_orconn_t **elt, **next, *c;

  for (elt = HT_START(bto_gid_ht, bto_gid_map);
       elt;
       elt = next) {
    c = *elt;
    next = HT_NEXT_RMV(bto_gid_ht, bto_gid_map, elt);

    c->gid = 0;
    /* Don't delete if chan ID isn't zero: it's still in the chan hash map */
    if (!c->chan)
      tor_free(c);
  }
  HT_CLEAR(bto_gid_ht, bto_gid_map);
  tor_free(bto_gid_map);
}

/** Clear the chan ID hash map, freeing any bt_orconn_t objects that
 * become unreferenced */
static void
bto_chan_clear_map(void)
{
  bt_orconn_t **elt, **next, *c;

  for (elt = HT_START(bto_chan_ht, bto_chan_map);
       elt;
       elt = next) {
    c = *elt;
    next = HT_NEXT_RMV(bto_chan_ht, bto_chan_map, elt);

    c->chan = 0;
    /* Don't delete if GID isn't zero, it's still in the GID hash map */
    if (!c->gid)
      tor_free(c);
  }
  HT_CLEAR(bto_chan_ht, bto_chan_map);
  tor_free(bto_chan_map);
}

/** Delete a bt_orconn from the hash maps by GID */
void
bto_delete(uint64_t gid)
{
  bt_orconn_t key, *bto;

  key.gid = gid;
  key.chan = 0;
  bto = HT_FIND(bto_gid_ht, bto_gid_map, &key);
  if (!bto) {
    /* The orconn might be unregistered because it's an EXT_OR_CONN? */
    log_debug(LD_BTRACK, "tried to delete unregistered ORCONN gid=%"PRIu64,
              gid);
    return;
  }
  HT_REMOVE(bto_gid_ht, bto_gid_map, &key);
  if (bto->chan) {
    key.chan = bto->chan;
    HT_REMOVE(bto_chan_ht, bto_chan_map, &key);
  }
  tor_free(bto);
}

/**
 * Helper for bto_find_or_new().
 *
 * Update GID and chan ID of an existing bt_orconn object if needed,
 * given a search key previously used within bto_find_or_new().
 **/
static bt_orconn_t *
bto_update(bt_orconn_t *bto, const bt_orconn_t *key)
{
  /* ORCONN GIDs shouldn't change once assigned */
  tor_assert(!bto->gid || !key->gid || bto->gid == key->gid);
  if (!bto->gid && key->gid) {
    /* Got a gid when we didn't already have one; insert into gid map */
    log_debug(LD_BTRACK, "ORCONN chan=%"PRIu64" newgid=%"PRIu64, key->chan,
              key->gid);
    bto->gid = key->gid;
    HT_INSERT(bto_gid_ht, bto_gid_map, bto);
  }
  /* association of ORCONN with channel shouldn't change */
  tor_assert(!bto->chan || !key->chan || bto->chan == key->chan);
  if (!bto->chan && key->chan) {
    /* Got a chan when we didn't already have one; insert into chan map */
    log_debug(LD_BTRACK, "ORCONN gid=%"PRIu64" newchan=%"PRIu64,
              bto->gid, key->chan);
    bto->chan = key->chan;
    HT_INSERT(bto_chan_ht, bto_chan_map, bto);
  }
  return bto;
}

/** Helper for bto_find_or_new() */
static bt_orconn_t *
bto_new(const bt_orconn_t *key)
{
  struct bt_orconn_t *bto = tor_malloc(sizeof(*bto));

  bto->gid = key->gid;
  bto->chan = key->chan;
  bto->state = 0;
  bto->proxy_type = 0;
  bto->is_orig = false;
  bto->is_onehop = true;

  if (bto->gid)
    HT_INSERT(bto_gid_ht, bto_gid_map, bto);
  if (bto->chan)
    HT_INSERT(bto_chan_ht, bto_chan_map, bto);

  return bto;
}

/**
 * Insert a new bt_orconn with the given GID and chan ID, or update
 * the GID and chan ID if one already exists.
 *
 * Return the found or allocated bt_orconn.
 **/
bt_orconn_t *
bto_find_or_new(uint64_t gid, uint64_t chan)
{
  bt_orconn_t key, *bto = NULL;

  tor_assert(gid || chan);
  key.gid = gid;
  key.chan = chan;
  if (key.gid)
    bto = HT_FIND(bto_gid_ht, bto_gid_map, &key);
  if (!bto && key.chan) {
    /* Not found by GID; look up by chan ID */
    bto = HT_FIND(bto_chan_ht, bto_chan_map, &key);
  }
  if (bto)
    return bto_update(bto, &key);
  else
    return bto_new(&key);
}

/** Initialize the hash maps  */
void
bto_init_maps(void)
{
  bto_gid_map = tor_malloc(sizeof(*bto_gid_map));
  HT_INIT(bto_gid_ht, bto_gid_map);
  bto_chan_map = tor_malloc(sizeof(*bto_chan_map));
  HT_INIT(bto_chan_ht, bto_chan_map);
}

/** Clear the hash maps, freeing all associated storage */
void
bto_clear_maps(void)
{
  bto_gid_clear_map();
  bto_chan_clear_map();
}