From 2db17df833d4cfa3e194d3154e6033b59e0d507d Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Thu, 7 Jun 2007 19:23:33 -0700
Subject: [PATCH] iputil.get_local_ip_for: tolerate running on a disconnected
 host

---
 src/allmydata/util/iputil.py | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/allmydata/util/iputil.py b/src/allmydata/util/iputil.py
index 99f228d0..7759dd09 100644
--- a/src/allmydata/util/iputil.py
+++ b/src/allmydata/util/iputil.py
@@ -25,7 +25,9 @@ def get_local_addresses_async(target='A.ROOT-SERVERS.NET'):
         reachable to.
     """
     addresses = []
-    addresses.append(get_local_ip_for(target))
+    local_ip = get_local_ip_for(target)
+    if local_ip:
+        addresses.append(local_ip)
 
     if sys.platform == "cygwin":
         d = _cygwin_hack_find_addresses(target)
@@ -44,14 +46,25 @@ def get_local_addresses_async(target='A.ROOT-SERVERS.NET'):
 def get_local_ip_for(target):
     """Find out what our IP address is for use by a given target.
 
-    @returns: the IP address as a dotted-quad string which could be used by
-        'target' to connect to us. It might work for them, it might not
+    @returns:the IP address as a dotted-quad string which could be used by
+              to connect to us. It might work for them, it might not. If
+              there is no suitable address (perhaps we don't currently have an
+              externally-visible interface), this will return None.
     """
-    target_ipaddr = socket.gethostbyname(target)
+
+    try:
+        target_ipaddr = socket.gethostbyname(target)
+    except socket.gaierror:
+        # DNS isn't running
+        return None
     udpprot = DatagramProtocol()
     port = reactor.listenUDP(0, udpprot)
-    udpprot.transport.connect(target_ipaddr, 7)
-    localip = udpprot.transport.getHost().host
+    try:
+        udpprot.transport.connect(target_ipaddr, 7)
+        localip = udpprot.transport.getHost().host
+    except socket.error:
+        # no route to that host
+        localip = None
     port.stopListening() # note, this returns a Deferred
     return localip
 
-- 
2.45.2