[tor-commits] [tor/release-0.4.2] nodelist: Helper to add an address to the nodelist address set

nickm at torproject.org nickm at torproject.org
Fri Mar 13 20:45:30 UTC 2020


commit 4152c349b4b9f7b2336a9d97356a28471e8d8f27
Author: David Goulet <dgoulet at torproject.org>
Date:   Tue Jan 28 09:17:34 2020 -0500

    nodelist: Helper to add an address to the nodelist address set
    
    We separate v4 and v6 because we often use an IPv4 address represented with
    a uint32_t instead of a tor_addr_t.
    
    This will be used to also add the trusted directory addresses taken from the
    configuration.
    
    The trusted directories from the consensus are already added to the address
    set from their descriptor.
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/feature/nodelist/nodelist.c | 31 ++++++++++++++++++++++++++-----
 src/feature/nodelist/nodelist.h |  2 ++
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/src/feature/nodelist/nodelist.c b/src/feature/nodelist/nodelist.c
index bd80fc1b4..90c655d12 100644
--- a/src/feature/nodelist/nodelist.c
+++ b/src/feature/nodelist/nodelist.c
@@ -455,22 +455,43 @@ node_add_to_address_set(const node_t *node)
 
   if (node->rs) {
     if (node->rs->addr)
-      address_set_add_ipv4h(the_nodelist->node_addrs, node->rs->addr);
+      nodelist_add_addr4_to_address_set(node->rs->addr);
     if (!tor_addr_is_null(&node->rs->ipv6_addr))
-      address_set_add(the_nodelist->node_addrs, &node->rs->ipv6_addr);
+      nodelist_add_addr6_to_address_set(&node->rs->ipv6_addr);
   }
   if (node->ri) {
     if (node->ri->addr)
-      address_set_add_ipv4h(the_nodelist->node_addrs, node->ri->addr);
+      nodelist_add_addr4_to_address_set(node->ri->addr);
     if (!tor_addr_is_null(&node->ri->ipv6_addr))
-      address_set_add(the_nodelist->node_addrs, &node->ri->ipv6_addr);
+      nodelist_add_addr6_to_address_set(&node->ri->ipv6_addr);
   }
   if (node->md) {
     if (!tor_addr_is_null(&node->md->ipv6_addr))
-      address_set_add(the_nodelist->node_addrs, &node->md->ipv6_addr);
+      nodelist_add_addr6_to_address_set(&node->md->ipv6_addr);
   }
 }
 
+/** Add the given v4 address into the nodelist address set. */
+void
+nodelist_add_addr4_to_address_set(const uint32_t addr)
+{
+  if (!the_nodelist || !the_nodelist->node_addrs || addr == 0) {
+    return;
+  }
+  address_set_add_ipv4h(the_nodelist->node_addrs, addr);
+}
+
+/** Add the given v6 address into the nodelist address set. */
+void
+nodelist_add_addr6_to_address_set(const tor_addr_t *addr)
+{
+  if (BUG(!addr) || tor_addr_is_null(addr) || tor_addr_is_v4(addr) ||
+      !the_nodelist || !the_nodelist->node_addrs) {
+    return;
+  }
+  address_set_add(the_nodelist->node_addrs, addr);
+}
+
 /** Return true if <b>addr</b> is the address of some node in the nodelist.
  * If not, probably return false. */
 int
diff --git a/src/feature/nodelist/nodelist.h b/src/feature/nodelist/nodelist.h
index af144c197..87cfa48e2 100644
--- a/src/feature/nodelist/nodelist.h
+++ b/src/feature/nodelist/nodelist.h
@@ -35,6 +35,8 @@ node_t *nodelist_add_microdesc(microdesc_t *md);
 void nodelist_set_consensus(networkstatus_t *ns);
 void nodelist_ensure_freshness(networkstatus_t *ns);
 int nodelist_probably_contains_address(const tor_addr_t *addr);
+void nodelist_add_addr4_to_address_set(const uint32_t addr);
+void nodelist_add_addr6_to_address_set(const tor_addr_t *addr);
 
 void nodelist_remove_microdesc(const char *identity_digest, microdesc_t *md);
 void nodelist_remove_routerinfo(routerinfo_t *ri);





More information about the tor-commits mailing list