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

[minion-cvs] Print useful server names for incoming connections.



Update of /home/minion/cvsroot/src/minion/lib/mixminion/server
In directory moria.mit.edu:/tmp/cvs-serv11868/lib/mixminion/server

Modified Files:
	DNSFarm.py MMTPServer.py ServerMain.py 
Log Message:
Print useful server names for incoming connections.

Index: DNSFarm.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/DNSFarm.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- DNSFarm.py	12 Jan 2004 04:48:16 -0000	1.9
+++ DNSFarm.py	2 Feb 2004 07:05:50 -0000	1.10
@@ -9,8 +9,8 @@
 import threading
 import time
 import sys
+import mixminion.NetUtils
 from mixminion.Common import LOG
-from mixminion.NetUtils import getIP, nameIsStaticIP
 from mixminion.ThreadUtils import TimeoutQueue, QueueEmpty
 
 __all__ = [ 'DNSCache' ]
@@ -32,8 +32,10 @@
 # they're idele for more than MAX_THREAD_IDLE seconds.
 MAX_THREAD_IDLE = 5*60
 # We clear entries from the DNS cache when they're more than MAX_ENTRY_TTL
-# seconds old.
+# seconds old...
 MAX_ENTRY_TTL = 30*60
+# ...and entries from the reverse cache after MAX_RENTRY_TTL seconds.
+MAX_RENTRY_TTL = 24*60*60
 
 class DNSCache:
     """Class to cache answers to DNS requests and manager DNS threads."""
@@ -41,6 +43,7 @@
     # _isShutdown: boolean: are the threads shutting down?  (While the
     #     threads are shutting down, we don't answer any requests.)
     # cache: map from name to PENDING or getIP result.
+    # rCache: map from (family,lowercase IP) to (hostname, time).
     # callbacks: map from name to list of callback functions. (See lookup
     #     for definition of callback.)
     # lock: Lock to control access to this class's shared state.
@@ -52,6 +55,7 @@
     def __init__(self):
         """Create a new DNSCache"""
         self.cache = {}
+        self.rCache = {}
         self.callbacks = {}
         self.lock = threading.RLock()
         self.queue = TimeoutQueue()
@@ -70,6 +74,27 @@
             return self.cache.get(name)
         finally:
             self.lock.release()
+
+    def getNameByAddressNonblocking(self, addr, family=None):
+        """Given an IP address (and optionally a family), if we have gotten
+           the address as a result in the past, return the hostname that
+           most recently resolved to the address, or None if no such hostname
+           is found."""
+        if family is None:
+            if ':' in addr:
+                family = mixminion.NetUtils.AF_INET6
+            else:
+                family = mixminion.NetUtils.AF_INET
+        try:
+            self.lock.acquire()
+            v = self.rCache.get((family,addr.lower()))
+            if v is None:
+                return None
+            else:
+                return v[0]
+        finally:
+            self.lock.release()
+
     def lookup(self,name,cb):
         """Look up the name 'name', and pass the result to the callback
            function 'cb' when we're done.  The result will be of the
@@ -81,7 +106,7 @@
            so it shouldn't be especially time-consuming.
         """
         # Check for a static IP first; no need to resolve that.
-        v = nameIsStaticIP(name)
+        v = mixminion.NetUtils.nameIsStaticIP(name)
         if v is not None:
             cb(name,v)
             return
@@ -127,13 +152,18 @@
         try:
             self.lock.acquire()
 
-            # Purge old entries from the
+            # Purge old entries from the caches.
             cache = self.cache
             for name in cache.keys():
                 v = cache[name]
                 if v is PENDING: continue
                 if now-v[2] > MAX_ENTRY_TTL:
                     del cache[name]
+            rCache = self.rCache
+            for name in rCache.keys():
+                v=rCache[name]
+                if now-v[1] > MAX_RENTRY_TTL:
+                    del rCache[name]
 
             # Remove dead threads from self.threads.
             liveThreads = [ thr for thr in self.threads if thr.isAlive() ]
@@ -174,6 +204,9 @@
             self.lock.acquire()
             # Insert the value in the cache.
             self.cache[name]=val
+            # Insert the value in the reverse cache.
+            if val[0] != 'NOENT':
+                self.rCache[(val[0], val[1].lower())] = (name.lower(),val[2])
             # Get the callbacks for the name, if any.
             cbs = self.callbacks.get(name,[])
             try:
@@ -224,7 +257,7 @@
                         return
                     # Else, resolve the IP and send the answer to the dnscache
                     _adjBusyThreads(1)
-                    result = getIP(hostname)
+                    result = mixminion.NetUtils.getIP(hostname)
                     _lookupDone(hostname, result)
                     _adjBusyThreads(-1)
             except QueueEmpty:

Index: MMTPServer.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/MMTPServer.py,v
retrieving revision 1.73
retrieving revision 1.74
diff -u -d -r1.73 -r1.74
--- MMTPServer.py	27 Jan 2004 05:56:21 -0000	1.73
+++ MMTPServer.py	2 Feb 2004 07:05:50 -0000	1.74
@@ -27,6 +27,7 @@
 import time
 from types import StringType
 
+import mixminion.ServerInfo
 import mixminion.TLSConnection
 import mixminion._minionlib as _ml
 from mixminion.Common import MixError, MixFatalError, MixProtocolError, \
@@ -37,7 +38,6 @@
 from mixminion.NetUtils import getProtocolSupport, AF_INET, AF_INET6
 import mixminion.server.EventStats as EventStats
 from mixminion.Filestore import CorruptedFile
-from mixminion.ServerInfo import displayServer
 from mixminion.ThreadUtils import MessageQueue, QueueEmpty
 
 __all__ = [ 'AsyncServer', 'ListenConnection', 'MMTPServerConnection',
@@ -232,7 +232,7 @@
     def process(self, r, w, x):
         #XXXX007 do something with x
         con, addr = self.sock.accept()
-        LOG.debug("Accepted connection from %s (fd %s)", addr, con.fileno())
+        LOG.debug("Accepted connection from %s", addr)
         self.connectionFactory(con)
         return self.isOpen,0,self.isOpen
 
@@ -259,10 +259,12 @@
     #   rejectPackets -- flag: do we reject the packets we've received?
     MESSAGE_LEN = 6 + (1<<15) + 20
     PROTOCOL_VERSIONS = ['0.3']
-    def __init__(self, sock, tls, consumer, rejectPackets=0):
-        addr,port=sock.getpeername()
-        serverName = "%s:%s (fd %s)"%(addr,port,sock.fileno())
-
+    def __init__(self, sock, tls, consumer, rejectPackets=0, serverName=None):
+        if serverName is None:
+            addr,port = sock.getpeername()
+            serverName = mixminion.ServerInfo.displayServerByAddress(addr,port)
+        serverName += " (fd %s)"%sock.fileno()
+        
         mixminion.TLSConnection.TLSConnection.__init__(
             self, tls, sock, serverName)
         EventStats.log.receivedConnection()
@@ -502,7 +504,14 @@
         finally:
             self._lock.release()
         sock.setblocking(0)
-        con = MMTPServerConnection(sock, tls, self.onPacketReceived)
+
+        addr, port = sock.getpeername()
+        hostname = self.dnsCache.getNameByAddressNonblocking(addr)
+        name = mixminion.ServerInfo.displayServerByAddress(
+            addr, port, hostname)
+
+        con = MMTPServerConnection(sock, tls, self.onPacketReceived,
+                                   serverName=name)
         self.register(con)
         return con
 
@@ -521,7 +530,7 @@
            corresponding packets to the corresponding sever, doing a DNS
            lookup first if necessary.
         """
-        serverName = displayServer(routing)
+        serverName = mixminion.ServerInfo.displayServerByRouting(routing)
         if isinstance(routing, IPV4Info):
             self._sendPackets(AF_INET, routing.ip, routing.port,
                               routing.keyinfo, deliverable, serverName)

Index: ServerMain.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/server/ServerMain.py,v
retrieving revision 1.115
retrieving revision 1.116
diff -u -d -r1.115 -r1.116
--- ServerMain.py	27 Jan 2004 05:16:29 -0000	1.115
+++ ServerMain.py	2 Feb 2004 07:05:50 -0000	1.116
@@ -366,7 +366,7 @@
                 for pending in packets ]
             LOG.trace("Delivering packets OUT:[%s] to %s",
                       " ".join([p.getHandle() for p in packets]),
-                      mixminion.ServerInfo.displayServer(routing))
+                      mixminion.ServerInfo.displayServerByRouting(routing))
             self.server.sendPacketsByRouting(routing, deliverable)
 
 class _MMTPServer(mixminion.server.MMTPServer.MMTPAsyncServer):