[or-cvs] r16414: Fix infinite loop on poor exit choices. (torctl/trunk/python/TorCtl)

mikeperry at seul.org mikeperry at seul.org
Tue Aug 5 04:33:23 UTC 2008


Author: mikeperry
Date: 2008-08-05 00:33:22 -0400 (Tue, 05 Aug 2008)
New Revision: 16414

Modified:
   torctl/trunk/python/TorCtl/PathSupport.py
Log:

Fix infinite loop on poor exit choices.



Modified: torctl/trunk/python/TorCtl/PathSupport.py
===================================================================
--- torctl/trunk/python/TorCtl/PathSupport.py	2008-08-05 04:32:22 UTC (rev 16413)
+++ torctl/trunk/python/TorCtl/PathSupport.py	2008-08-05 04:33:22 UTC (rev 16414)
@@ -67,6 +67,10 @@
 
 #################### Path Support Interfaces #####################
 
+class RestrictionError(Exception):
+  "Error raised for issues with applying restrictions"
+  pass
+
 class NodeRestriction:
   "Interface for node restriction policies"
   def r_is_ok(self, r):
@@ -140,6 +144,7 @@
   def reset_restriction(self, rstr_list):
     "Reset the restriction list to a new list"
     self.rstr_list = rstr_list
+    self.rewind()
 
   def rewind(self):
     "Rewind the generator to the 'beginning'"
@@ -147,6 +152,9 @@
     # during selection, and also some memory overhead (at the cost
     # of a much slower rewind() though..)
     self.routers = filter(lambda r: self.rstr_list.r_is_ok(r), self.sorted_r)
+    if not self.routers:
+      plog("ERROR", "No routers left after restrictions applied!")
+      raise RestrictionError()
     #self.routers = copy.copy(self.sorted_r)
 
   def mark_chosen(self, r):
@@ -512,6 +520,7 @@
     NodeGenerator.__init__(self, sorted_r, rstr_list)
 
   def rewind(self):
+    NodeGenerator.rewind(self)
     if self.to_port not in self.next_exit_by_port or not self.next_exit_by_port[self.to_port]:
       self.next_exit_by_port[self.to_port] = 0
       self.last_idx = len(self.sorted_r)
@@ -745,6 +754,7 @@
   def reconfigure(self, sorted_r):
     """This function is called after a configuration change, 
      to rebuild the RestrictionLists."""
+    plog("DEBUG", "Reconfigure")
     if self.use_all_exits:
       self.path_rstr = PathRestrictionList([UniqueRestriction()])
     else:
@@ -775,7 +785,14 @@
        FlagsRestriction(["Running","Fast"], [])]
 
     )
-    if self.use_all_exits:
+
+    if self.exit_name:
+      plog("DEBUG", "Applying Setexit: "+self.exit_name)
+      if self.exit_name[0] == '$':
+        self.exit_rstr = NodeRestrictionList([IdHexRestriction(self.exit_name)])
+      else:
+        self.exit_rstr = NodeRestrictionList([NickRestriction(self.exit_name)])
+    elif self.use_all_exits:
       self.exit_rstr = NodeRestrictionList(
         [FlagsRestriction(["Valid", "Running","Fast"], ["BadExit"])])
     else:
@@ -783,13 +800,6 @@
         [PercentileRestriction(nonentry_skip, nonentry_fast, sorted_r),
          FlagsRestriction(["Valid", "Running","Fast"], ["BadExit"])])
 
-    if self.exit_name:
-      self.exit_rstr.del_restriction(IdHexRestriction)
-      self.exit_rstr.del_restriction(NickRestriction)
-      if self.exit_name[0] == '$':
-        self.exit_rstr.add_restriction(IdHexRestriction(self.exit_name))
-      else:
-        self.exit_rstr.add_restriction(NickRestriction(self.exit_name))
 
     # GeoIP configuration
     if self.geoip_config:
@@ -840,11 +850,8 @@
         exitgen = self.__ordered_exit_gen = \
           OrderedExitGenerator(80, sorted_r, self.exit_rstr)
     elif self.uniform:
-      # 'real' exits should also be chosen when not using 'order_exits'
-      self.exit_rstr.add_restriction(ExitPolicyRestriction("255.255.255.255", 80))
       exitgen = UniformGenerator(sorted_r, self.exit_rstr)
     else:
-      self.exit_rstr.add_restriction(ExitPolicyRestriction("255.255.255.255", 80))
       exitgen = BwWeightedGenerator(sorted_r, self.exit_rstr, self.pathlen, exit=True)
 
     if self.uniform:



More information about the tor-commits mailing list