[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [bridgedb/master] Rejection-sample randomly-generated v4 addresses.



commit 60ed840f3882f80b81a984d419cc7f78d1c96cfb
Author: Philipp Winter <phw@xxxxxxxxx>
Date:   Tue Feb 11 16:28:33 2020 -0800

    Rejection-sample randomly-generated v4 addresses.
    
    So far, we generated completely random IPv4 addresses which would
    sometimes contain multicast addresses.  This patch adds rejection
    sampling, as we're already doing it for IPv6 addresses.
---
 scripts/create_descriptors | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/scripts/create_descriptors b/scripts/create_descriptors
index 0b9d26d..60022db 100755
--- a/scripts/create_descriptors
+++ b/scripts/create_descriptors
@@ -117,10 +117,13 @@ def get_hex_string(size):
     return hexstr
 
 
-def get_random_ipv6_addr():
+def get_random_addr(ip_version=4):
     valid_addr = None
     while not valid_addr:
-        maybe = ipaddress.IPv6Address(random.getrandbits(128))
+        if ip_version == 4:
+            maybe = ipaddress.IPv4Address(random.getrandbits(32))
+        else:
+            maybe = ipaddress.IPv6Address(random.getrandbits(128))
         valid = check_ip_validity(maybe)
         if valid:
             valid_addr = maybe
@@ -128,13 +131,6 @@ def get_random_ipv6_addr():
     return str(valid_addr)
 
 
-def get_random_ipv4_addr():
-    return "%i.%i.%i.%i" % (random.randint(0, 255),
-                            random.randint(0, 255),
-                            random.randint(0, 255),
-                            random.randint(0, 255))
-
-
 def get_protocol(tor_version):
     line = ""
     if tor_version is not None:
@@ -198,8 +194,8 @@ def create_server_desc(signing_key):
     timestamp = make_timestamp(variation=True, period=36)
 
     server_desc = RelayDescriptor.create({
-        "router": "%s %s %s 0 0" % (nickname, get_random_ipv4_addr(), port),
-        "or-address": "[%s]:%s" % (get_random_ipv6_addr(), port-1),
+        "router": "%s %s %s 0 0" % (nickname, get_random_addr(ip_version=4), port),
+        "or-address": "[%s]:%s" % (get_random_addr(ip_version=6), port-1),
         "platform": "Tor %s on Linux" % tor_version,
         get_protocol(tor_version): "",
         "published": timestamp,



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits