From 10925520c49874b06d4e342908f092c634b84cdf Mon Sep 17 00:00:00 2001 From: Brian Muller Date: Mon, 13 Jan 2014 22:25:51 -0500 Subject: [PATCH] added stun rpc method --- kademlia/network.py | 18 +++++++++++++++++- kademlia/protocol.py | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/kademlia/network.py b/kademlia/network.py index 0907e1f..6f6d9e5 100644 --- a/kademlia/network.py +++ b/kademlia/network.py @@ -172,7 +172,7 @@ class Server(object): are acceptable - hostnames will cause an error. """ # if the transport hasn't been initialized yet, wait a second - if self.protocol.transport == None: + if self.protocol.transport is None: reactor.callLater(1, self.bootstrap, addrs) return @@ -189,6 +189,22 @@ class Server(object): ds[addr] = self.protocol.ping(addr, self.node.id) return deferredDict(ds).addCallback(initTable) + def inetVisibleIP(self): + """ + Get the internet visible IP's of this node as other nodes see it. + + @return: An C{list} of IP's. If no one can be contacted, then the + C{list} will be empty. + """ + def handle(results): + ips = [ result[0] for result in results if result is not None ] + self.log.debug("other nodes think our ip is %s" % str(ips)) + return ips + ds = [] + for neighbor in self.bootstrappableNeighbors(): + ds.append(self.protocol.router.stun(neighbor)) + return defer.gatherResults(ds).addCallback(handle) + def get(self, key): """ Get a key if the network has it. diff --git a/kademlia/protocol.py b/kademlia/protocol.py index 82e111c..7e3def1 100644 --- a/kademlia/protocol.py +++ b/kademlia/protocol.py @@ -23,6 +23,9 @@ class KademliaProtocol(RPCProtocol): ids.append(random.randint(*bucket.range)) return ids + def rpc_stun(self, sender): + return sender + def rpc_ping(self, sender, nodeid): source = Node(nodeid, sender[0], sender[1]) self.router.addContact(source)