from twisted.application import service
import os.path
from foolscap import Tub
-from allmydata.util.iputil import get_local_ip_for
+from foolscap.eventual import fireEventually
+from allmydata.util.iputil import get_local_addresses
from allmydata.util import idlib
from twisted.python import log
def log(self, msg):
log.msg(self.short_nodeid + ": " + msg)
- def _setup_tub(self, local_ip):
+ def _setup_tub(self, local_addresses):
# we can't get a dynamically-assigned portnum until our Tub is
# running, which means after startService.
l = self.tub.getListeners()[0]
f = open(local_ip_filename, "r")
local_ip = f.read()
f.close()
- self.tub.setLocation("%s:%d" % (local_ip, portnum))
+ if local_ip not in local_addresses:
+ local_addresses.append(local_ip)
if not os.path.exists(self._portnumfile):
# record which port we're listening on, so we can grab the same
# one next time
f = open(self._portnumfile, "w")
f.write("%d\n" % portnum)
f.close()
- self.tub.setLocation("%s:%d" % (local_ip, l.getPortnum()))
+ location = ",".join(["%s:%d" % (ip, portnum)
+ for ip in local_addresses])
+ self.log("Tub location set to %s" % location)
+ self.tub.setLocation(location)
return self.tub
def tub_ready(self):
def startService(self):
# note: this class can only be started and stopped once.
service.MultiService.startService(self)
- local_ip = get_local_ip_for()
- self._setup_tub(local_ip)
+ local_addresses = get_local_addresses()
+ self._setup_tub(local_addresses)
self.tub_ready()
self.log("%s running" % self.NODETYPE)
+
from twisted.trial import unittest
-from allmydata.util.iputil import get_local_addresses
+from allmydata.util import iputil
class ListAddresses(unittest.TestCase):
- def test_list(self):
- d = get_local_addresses()
+ def test_list_async(self):
+ d = iputil.get_local_addresses_async()
def _check(addresses):
self.failUnless(len(addresses) >= 1) # always have localhost
self.failUnless("127.0.0.1" in addresses)
- print addresses
d.addCallbacks(_check)
return d
+ def test_list(self):
+ addresses = iputil.get_local_addresses()
+ self.failUnless(len(addresses) >= 1) # always have localhost
+ self.failUnless("127.0.0.1" in addresses)
+
# adapted from nattraverso.ipdiscover
+import os
from cStringIO import StringIO
import re
import socket
# eventually I want to use somebody else's cross-platform library for
# this. For right now, I'm running ifconfig and grepping for the 'inet '
# lines.
+
+ cmd = "ifconfig"
+ p = os.popen("ifconfig")
+ addresses = []
+ for line in p.readlines():
+ # linux shows: " inet addr:1.2.3.4 Bcast:1.2.3.255..."
+ # OS-X shows: " inet 1.2.3.4 ..."
+ m = re.match("^\s+inet\s+[a-z:]*([\d\.]+)\s", line)
+ if m:
+ addresses.append(m.group(1))
+ return addresses
+
+def get_local_addresses_async():
+ """Return a Deferred that fires with a list of IPv4 addresses (as
+ dotted-quad strings) that are currently configured on this host.
+ """
+ # eventually I want to use somebody else's cross-platform library for
+ # this. For right now, I'm running ifconfig and grepping for the 'inet '
+ # lines.
+
+ # I'd love to do this synchronously.
cmd = "ifconfig"
d = getProcessOutput("ifconfig")
def _parse(output):