[tor-commits] [arm/master] Util for fingerprint -> (ip address, or port) maps

atagar at torproject.org atagar at torproject.org
Wed Sep 7 17:02:50 UTC 2011


commit 6734eaff90dfc1f0fd57544755de1632381fcdee
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Sep 3 14:16:56 2011 -0700

    Util for fingerprint -> (ip address, or port) maps
    
    Moving lookup functionality for the address/or port of relays from the
    connection panel into the utils. This is a generally useful bit of information,
    and needed for the interpretor panel.
---
 src/cli/connections/circEntry.py |   31 ++-------------------------
 src/util/torTools.py             |   41 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 28 deletions(-)

diff --git a/src/cli/connections/circEntry.py b/src/cli/connections/circEntry.py
index 7bf21db..25966df 100644
--- a/src/cli/connections/circEntry.py
+++ b/src/cli/connections/circEntry.py
@@ -13,32 +13,6 @@ import curses
 from cli.connections import entries, connEntry
 from util import torTools, uiTools
 
-# cached fingerprint -> (IP Address, ORPort) results
-RELAY_INFO = {}
-
-def getRelayInfo(fingerprint):
-  """
-  Provides the (IP Address, ORPort) tuple for the given relay. If the lookup
-  fails then this returns ("192.168.0.1", "0").
-  
-  Arguments:
-    fingerprint - relay to look up
-  """
-  
-  if not fingerprint in RELAY_INFO:
-    conn = torTools.getConn()
-    failureResult = ("192.168.0.1", "0")
-    
-    nsEntry = conn.getConsensusEntry(fingerprint)
-    if not nsEntry: return failureResult
-    
-    nsLineComp = nsEntry.split("\n")[0].split(" ")
-    if len(nsLineComp) < 8: return failureResult
-    
-    RELAY_INFO[fingerprint] = (nsLineComp[6], nsLineComp[7])
-  
-  return RELAY_INFO[fingerprint]
-
 class CircEntry(connEntry.ConnectionEntry):
   def __init__(self, circuitID, status, purpose, path):
     connEntry.ConnectionEntry.__init__(self, "127.0.0.1", "0", "127.0.0.1", "0")
@@ -72,14 +46,15 @@ class CircEntry(connEntry.ConnectionEntry):
     
     self.status = status
     self.lines = [self.lines[0]]
+    conn = torTools.getConn()
     
     if status == "BUILT" and not self.lines[0].isBuilt:
-      exitIp, exitORPort = getRelayInfo(path[-1])
+      exitIp, exitORPort = conn.getRelayAddress(path[-1], ("192.168.0.1", "0"))
       self.lines[0].setExit(exitIp, exitORPort, path[-1])
     
     for i in range(len(path)):
       relayFingerprint = path[i]
-      relayIp, relayOrPort = getRelayInfo(relayFingerprint)
+      relayIp, relayOrPort = conn.getRelayAddress(relayFingerprint, ("192.168.0.1", "0"))
       
       if i == len(path) - 1:
         if status == "BUILT": placementType = "Exit"
diff --git a/src/util/torTools.py b/src/util/torTools.py
index 5c1b1ad..adc175a 100644
--- a/src/util/torTools.py
+++ b/src/util/torTools.py
@@ -389,6 +389,7 @@ class Controller(TorCtl.PostEventListener):
     self._fingerprintsAttachedCache = None # cache of relays we're connected to
     self._nicknameLookupCache = {}      # lookup cache with fingerprint -> nickname mappings
     self._nicknameToFpLookupCache = {}  # lookup cache with nickname -> fingerprint mappings
+    self._addressLookupCache = {}       # lookup cache with fingerprint -> (ip address, or port) mappings
     self._consensusLookupCache = {}     # lookup cache with network status entries
     self._descriptorLookupCache = {}    # lookup cache with relay descriptors
     self._isReset = False               # internal flag for tracking resets
@@ -462,6 +463,7 @@ class Controller(TorCtl.PostEventListener):
       self._fingerprintsAttachedCache = None
       self._nicknameLookupCache = {}
       self._nicknameToFpLookupCache = {}
+      self._addressLookupCache = {}
       self._consensusLookupCache = {}
       self._descriptorLookupCache = {}
       
@@ -1248,6 +1250,44 @@ class Controller(TorCtl.PostEventListener):
     
     return result
   
+  def getRelayAddress(self, relayFingerprint, default = None):
+    """
+    Provides the (IP Address, ORPort) tuple for a given relay. If the lookup
+    fails then this returns the default.
+    
+    Arguments:
+      relayFingerprint - fingerprint of the relay
+    """
+    
+    self.connLock.acquire()
+    
+    result = None
+    if self.isAlive():
+      # query the address if it isn't yet cached
+      if not relayFingerprint in self._addressLookupCache:
+        if relayFingerprint == self.getInfo("fingerprint"):
+          # this is us, simply check the config
+          myAddress = self.getInfo("address")
+          myOrPort = self.getOption("ORPort")
+          
+          if myAddress and myOrPort:
+            self._addressLookupCache[relayFingerprint] = (myAddress, myOrPort)
+        else:
+          # check the consensus for the relay
+          nsEntry = self.getConsensusEntry(relayFingerprint)
+          
+          if nsEntry:
+            nsLineComp = nsEntry.split("\n")[0].split(" ")
+            
+            if len(nsLineComp) >= 8:
+              self._addressLookupCache[relayFingerprint] = (nsLineComp[6], nsLineComp[7])
+      
+      result = self._addressLookupCache.get(relayFingerprint, default)
+    
+    self.connLock.release()
+    
+    return result
+  
   def getNicknameFingerprint(self, relayNickname):
     """
     Provides the fingerprint associated with the given relay. This provides
@@ -1590,6 +1630,7 @@ class Controller(TorCtl.PostEventListener):
     self._fingerprintsAttachedCache = None
     self._nicknameLookupCache = {}
     self._nicknameToFpLookupCache = {}
+    self._addressLookupCache = {}
     self._consensusLookupCache = {}
     
     if self._fingerprintMappings != None:





More information about the tor-commits mailing list