[tor-commits] [stem/master] Using a lightweight rule counterpart for microdescriptor policies

atagar at torproject.org atagar at torproject.org
Mon Dec 31 05:37:27 UTC 2012


commit d35b9c737e320500fa6cfdfa874534cfbe6f1b3a
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Dec 30 21:26:34 2012 -0800

    Using a lightweight rule counterpart for microdescriptor policies
    
    The exit policies used in microdescriptors are wonderfully simple. There's no
    point in storing full ExitPolicyRule instances for them, so making an alternate
    lightweight subclass.
    
    Advantages are faster for parsing and less memory usage. Disadvantage is that
    this doesn't use the ExitPolicyRule constructor, so we'll need to take care
    that ExitPolicyRule changes are reflected in this subclass.
    
    This dropped the runtime of the network status integ tests on my system from
    9.4 to 8.3 seconds.
---
 stem/exit_policy.py |   43 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index 462a0b5..d7e091f 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -308,19 +308,20 @@ class MicrodescriptorExitPolicy(ExitPolicy):
     
     policy = policy[1:]
     
-    # convert our port list into ExitPolicyRules
+    # convert our port list into MicroExitPolicyRule
     rules = []
-    rule_format = "accept *:%s" if self.is_accept else "reject *:%s"
     
     for port_entry in policy.split(","):
-      rule_str = rule_format % port_entry
+      if '-' in port_entry:
+        min_port, max_port = port_entry.split('-', 1)
+      else:
+        min_port = max_port = port_entry
+      
+      if not stem.util.connection.is_valid_port(min_port) or \
+         not stem.util.connection.is_valid_port(max_port):
+        raise ValueError("'%s' is an invalid port range" % port_entry)
       
-      try:
-        rule = ExitPolicyRule(rule_str)
-        rules.append(rule)
-      except ValueError, exc:
-        exc_msg = "Policy '%s' is malformed. %s" % (self._policy, str(exc).replace(rule_str, port_entry))
-        raise ValueError(exc_msg)
+      rules.append(MicroExitPolicyRule(self.is_accept, int(min_port), int(max_port)))
     
     super(MicrodescriptorExitPolicy, self).__init__(*rules)
     self._set_default_allowed(not self.is_accept)
@@ -694,3 +695,27 @@ def _address_type_to_int(address_type):
 def _int_to_address_type(address_type_int):
   return AddressType[AddressType.keys()[address_type_int]]
 
+class MicroExitPolicyRule(ExitPolicyRule):
+  """
+  Lighter weight ExitPolicyRule derivative for microdescriptors.
+  """
+  
+  def __init__(self, is_accept, min_port, max_port):
+    self.is_accept = is_accept
+    self.address = None # wildcard address
+    self.min_port = min_port
+    self.max_port = max_port
+    self._str_representation = None
+  
+  def is_address_wildcard(self):
+    return True
+    
+  def get_address_type(self):
+    return AddressType.WILDCARD
+  
+  def get_mask(self, cache = True):
+    return None
+  
+  def get_masked_bits(self):
+    return None
+



More information about the tor-commits mailing list