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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/* Copyright (c) 2001 Matej Pfajfar.
* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2010, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "or.h"
#include "nodelist.h"
#include "microdesc.h"
#include "networkstatus.h"
#include "routerlist.h"
#include <string.h>
static void nodelist_drop_node(node_t *node);
static void node_free(node_t *node);
/** A nodelist_t holds a node_t object for every router we're "willing to use
* for something". Specifically, it should hold a node_t for every node that
* is currently in the routerlist, or currently in the consensus we're using.
*/
typedef struct nodelist_t {
/* A list of all the nodes. */
smartlist_t *nodes;
/* Hash table to map from node ID digest to node. */
HT_HEAD(nodelist_map, node_t) nodes_by_id;
} nodelist_t;
static INLINE unsigned int
node_id_hash(const node_t *node)
{
#if SIZEOF_INT == 4
const uint32_t *p = (const uint32_t*)node->identity;
return p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4];
#elif SIZEOF_INT == 8
const uint64_t *p = (const uint32_t*)node->identity;
const uint32_t *p32 = (const uint32_t*)node->identity;
return p[0] ^ p[1] ^ p32[4];
#endif
}
static INLINE unsigned int
node_id_eq(const node_t *node1, const node_t *node2)
{
return 0 == memcmp(node1->identity, node2->identity, DIGEST_LEN);
}
HT_PROTOTYPE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq);
HT_GENERATE(nodelist_map, node_t, ht_ent, node_id_hash, node_id_eq,
0.6, malloc, realloc, free);
/** The global nodelist. */
static nodelist_t *the_nodelist=NULL;
/** Create an empty nodelist if we haven't done so already. */
static void
init_nodelist(void)
{
if (PREDICT_UNLIKELY(the_nodelist == NULL)) {
the_nodelist = tor_malloc_zero(sizeof(nodelist_t));
HT_INIT(nodelist_map, &the_nodelist->nodes_by_id);
the_nodelist->nodes = smartlist_create();
}
}
/** Return the node_t whose identity is <b>identity_digest</b>, or NULL
* if no such node exists. */
node_t *
node_get_by_id(const char *identity_digest)
{
node_t search, *node;
if (PREDICT_UNLIKELY(the_nodelist == NULL))
return NULL;
memcpy(&search.identity, identity_digest, DIGEST_LEN);
node = HT_FIND(nodelist_map, &the_nodelist->nodes_by_id, &search);
return node;
}
/** Internal: return the node_t whose identity_digest is
* <b>identity_digest</b>. If none exists, create a new one, add it to the
* nodelist, and return it.
*
* Requires that the nodelist be initialized.
*/
static node_t *
node_get_or_create(const char *identity_digest)
{
node_t *node;
if ((node = node_get_by_id(identity_digest)))
return node;
node = tor_malloc_zero(sizeof(node_t));
memcpy(node->identity, identity_digest, DIGEST_LEN);
HT_INSERT(nodelist_map, &the_nodelist->nodes_by_id, node);
smartlist_add(the_nodelist->nodes, node);
node->nodelist_idx = smartlist_len(the_nodelist->nodes) - 1;
return node;
}
/** Add <b>ri</b> to the nodelist. */
node_t *
nodelist_add_routerinfo(routerinfo_t *ri)
{
node_t *node;
init_nodelist();
node = node_get_or_create(ri->cache_info.identity_digest);
node->ri = ri;
return node;
}
/** Set the appropriate node_t to use <b>md</b> as its microdescriptor.
*
* Called when a new microdesc has arrived and the usable consensus flavor
* is "microdesc".
**/
node_t *
nodelist_add_microdesc(microdesc_t *md)
{
networkstatus_t *ns =
networkstatus_get_latest_consensus_by_flavor(FLAV_MICRODESC);
const routerstatus_t *rs;
node_t *node;
if (ns == NULL)
return NULL;
init_nodelist();
/* Microdescriptors don't carry an identity digest, so we need to figure
* it out by looking up the routerstatus. */
rs = router_get_consensus_status_by_descriptor_digest(ns, md->digest);
if (rs == NULL)
return NULL;
node = node_get_by_id(rs->identity_digest);
if (node)
node->md = md;
return node;
}
/** Tell the nodelist that the current usable consensus to <b>ns</b>.
* This makes the nodelist change all of the routerstatus entries for
* the nodes, drop nodes that no longer have enough info to get used,
* and grab microdescriptors into nodes as appropriate.
*/
void
nodelist_set_consensus(networkstatus_t *ns)
{
init_nodelist();
SMARTLIST_FOREACH(the_nodelist->nodes, node_t *, node,
node->rs = NULL);
SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
node_t *node = node_get_or_create(rs->identity_digest);
node->rs = rs;
if (ns->flavor == FLAV_MICRODESC) {
if (node->md == NULL ||
0!=memcmp(node->md->digest,rs->descriptor_digest,DIGEST256_LEN)) {
node->md = microdesc_cache_lookup_by_digest256(NULL,
rs->descriptor_digest);
}
}
} SMARTLIST_FOREACH_END(rs);
nodelist_purge();
}
/** Helper: return true iff a node has a usable amount of information*/
static INLINE int
node_is_usable(const node_t *node)
{
return (node->rs) || (node->ri);
}
/** Tell the nodelist that <b>md</b> is no longer a microdescriptor for the
* node with <b>identity_digest</b>. */
void
nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md)
{
node_t *node = node_get_by_id(identity_digest);
if (node && node->md == md)
node->md = NULL;
}
/** Tell the nodelist that <b>ri</b> is no longer in the routerlist. */
void
nodelist_remove_routerinfo(routerinfo_t *ri)
{
node_t *node = node_get_by_id(ri->cache_info.identity_digest);
if (node && node->ri == ri) {
node->ri = NULL;
if (! node_is_usable(node)) {
nodelist_drop_node(node);
node_free(node);
}
}
}
/** Remove <b>node</b> from the nodelist. (Asserts that it was there to begin
* with.) */
static void
nodelist_drop_node(node_t *node)
{
node_t *tmp;
int idx;
tmp = HT_REMOVE(nodelist_map, &the_nodelist->nodes_by_id, node);
tor_assert(tmp == node);
idx = node->nodelist_idx;
tor_assert(idx >= 0);
tor_assert(node == smartlist_get(the_nodelist->nodes, idx));
smartlist_del(the_nodelist->nodes, idx);
if (idx < smartlist_len(the_nodelist->nodes)) {
tmp = smartlist_get(the_nodelist->nodes, idx);
tmp->nodelist_idx = idx;
}
node->nodelist_idx = -1;
}
/** Release storage held by <b>node</b> */
static void
node_free(node_t *node)
{
if (!node)
return;
tor_assert(node->nodelist_idx == -1);
tor_free(node);
}
/** Remove all entries from the nodelist that don't have enough info to be
* usable for anything. */
void
nodelist_purge(void)
{
node_t **iter;
if (PREDICT_UNLIKELY(the_nodelist == NULL))
return;
for (iter = HT_START(nodelist_map, &the_nodelist->nodes_by_id); iter; ) {
node_t *node = *iter;
if (node_is_usable(node)) {
iter = HT_NEXT(nodelist_map, &the_nodelist->nodes_by_id, iter);
} else {
iter = HT_NEXT_RMV(nodelist_map, &the_nodelist->nodes_by_id, iter);
nodelist_drop_node(node);
node_free(node);
}
}
}
/** Release all storage held by the nodelist. */
void
nodelist_free_all(void)
{
if (PREDICT_UNLIKELY(the_nodelist == NULL))
return;
HT_CLEAR(nodelist_map, &the_nodelist->nodes_by_id);
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
node->nodelist_idx = -1;
node_free(node);
} SMARTLIST_FOREACH_END(node);
smartlist_free(the_nodelist->nodes);
tor_free(the_nodelist);
}
/** Check that the nodelist is internally consistent, and consistent with
* the directory info it's derived from.
*/
void
nodelist_assert_ok(void)
{
routerlist_t *rl = router_get_routerlist();
networkstatus_t *ns = networkstatus_get_latest_consensus();
digestmap_t *dm = digestmap_new();
if (!the_nodelist)
return;
/* every routerinfo in rl->routers should be in the nodelist. */
if (rl) {
SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, ri) {
node_t *node = node_get_by_id(ri->cache_info.identity_digest);
tor_assert(node && node->ri == ri);
tor_assert(0 == memcmp(ri->cache_info.identity_digest,
node->identity, DIGEST_LEN));
tor_assert(! digestmap_get(dm, node->identity));
digestmap_set(dm, node->identity, node);
} SMARTLIST_FOREACH_END(ri);
}
/* every routerstatus in ns should be in the nodelist */
if (ns) {
SMARTLIST_FOREACH_BEGIN(ns->routerstatus_list, routerstatus_t *, rs) {
node_t *node = node_get_by_id(rs->identity_digest);
tor_assert(node && node->rs == rs);
tor_assert(0 == memcmp(rs->identity_digest, node->identity, DIGEST_LEN));
digestmap_set(dm, node->identity, node);
if (ns->flavor == FLAV_MICRODESC) {
/* If it's a microdesc consensus, every entry that has a microdescriptor
* should be in the nodelist.
*/
microdesc_t *md =
microdesc_cache_lookup_by_digest256(NULL, rs->descriptor_digest);
tor_assert(md == node->md);
}
} SMARTLIST_FOREACH_END(rs);
}
/* The nodelist should have no other entries, and its entries should be
* well-formed. */
SMARTLIST_FOREACH_BEGIN(the_nodelist->nodes, node_t *, node) {
tor_assert(digestmap_get(dm, node->identity) != NULL);
tor_assert(node_sl_idx == node->nodelist_idx);
} SMARTLIST_FOREACH_END(node);
tor_assert((long)smartlist_len(the_nodelist->nodes) ==
(long)HT_SIZE(&the_nodelist->nodes_by_id));
digestmap_free(dm, NULL);
}
|