]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/util/rrefutil.py
Fix introweb display for mixed V1/V2 clients. Closes #1721.
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / rrefutil.py
index fd2259b65a2bf5b9af6deb5206fe96b2b4ced161..a14d15f13fcb835f3afd09fd46030668c77eb871 100644 (file)
@@ -1,5 +1,7 @@
 
-from foolscap.api import Violation, RemoteException, DeadReferenceError
+from twisted.internet import address
+from foolscap.api import Violation, RemoteException, DeadReferenceError, \
+     SturdyRef
 
 def add_version_to_remote_reference(rref, default):
     """I try to add a .version attribute to the given RemoteReference. I call
@@ -23,3 +25,36 @@ def trap_and_discard(f, *errorTypes):
 
 def trap_deadref(f):
     return trap_and_discard(f, DeadReferenceError)
+
+
+def hosts_for_rref(rref, ignore_localhost=True):
+    # actually, this only returns hostnames
+    advertised = []
+    for hint in rref.getLocationHints():
+        # Foolscap-0.2.5 and earlier used strings in .locationHints, but we
+        # require a newer version that uses tuples of ("ipv4", host, port)
+        assert not isinstance(hint, str), hint
+        if hint[0] == "ipv4":
+            host = hint[1]
+            if ignore_localhost and host == "127.0.0.1":
+                continue
+            advertised.append(host)
+    return advertised
+
+def hosts_for_furl(furl, ignore_localhost=True):
+    advertised = []
+    for hint in SturdyRef(furl).locationHints:
+        assert not isinstance(hint, str), hint
+        if hint[0] == "ipv4":
+            host = hint[1]
+            if ignore_localhost and host == "127.0.0.1":
+                continue
+            advertised.append(host)
+    return advertised
+
+def stringify_remote_address(rref):
+    remote = rref.getPeer()
+    if isinstance(remote, address.IPv4Address):
+        return "%s:%d" % (remote.host, remote.port)
+    # loopback is a non-IPv4Address
+    return str(remote)