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

[PATCH rhel6-branch] Account for ipv6 addresses too (#594090)



---
 isys/isys.py |   50 ++++++++++++++++++++++++++++++++++++++++----------
 network.py   |   37 +++++++++----------------------------
 vnc.py       |    7 +++++--
 3 files changed, 54 insertions(+), 40 deletions(-)

diff --git a/isys/isys.py b/isys/isys.py
index 7ab5988..bc1777a 100755
--- a/isys/isys.py
+++ b/isys/isys.py
@@ -49,6 +49,7 @@ NM_ACTIVE_CONNECTION_IFACE = "org.freedesktop.NetworkManager.Connection.Active"
 NM_CONNECTION_IFACE = "org.freedesktop.NetworkManagerSettings.Connection"
 NM_DEVICE_IFACE = "org.freedesktop.NetworkManager.Device"
 NM_IP4CONFIG_IFACE = "org.freedesktop.NetworkManager.IP4Config"
+NM_IP6CONFIG_IFACE = "org.freedesktop.NetworkManager.IP6Config"
 
 NM_STATE_UNKNOWN = 0
 NM_STATE_ASLEEP = 1
@@ -509,8 +510,9 @@ def isWireless(dev):
     else:
         return False
 
-# Get the IP address for a network device.
-def getIPAddress(dev):
+# Get the IP addresses for a network device.
+# returns dict of lists with keys 'ipv4' and 'ipv6'
+def getIPAddresses(dev):
     if dev == '' or dev is None:
        return None
 
@@ -518,16 +520,44 @@ def getIPAddress(dev):
     if device_props_iface is None:
         return None
 
-    # XXX: add support for IPv6 addresses when NM can do that
-    device_ip4addr = device_props_iface.Get(NM_DEVICE_IFACE, "Ip4Address")
+    bus = dbus.SystemBus()
 
-    try:
-        tmp = struct.pack('I', device_ip4addr)
-        address = socket.inet_ntop(socket.AF_INET, tmp)
-    except ValueError, e:
-        return None
+    addresses = {'ipv4':[], 'ipv6':[]}
 
-    return address
+    ip4_config_path = device_props_iface.Get(NM_DEVICE_IFACE, 'Ip4Config')
+    if ip4_config_path != '/':
+        ip4_config_obj = bus.get_object(NM_SERVICE, ip4_config_path)
+        ip4_config_props = dbus.Interface(ip4_config_obj, DBUS_PROPS_IFACE)
+
+        # addresses (3-element list:  ipaddr, netmask, gateway)
+        addrs = ip4_config_props.Get(NM_IP4CONFIG_IFACE, "Addresses")
+        for addr in addrs:
+            try:
+                tmp = struct.pack('I', addr[0])
+                ipaddr = socket.inet_ntop(socket.AF_INET, tmp)
+                addresses['ipv4'].append(ipaddr)
+            except ValueError as e:
+                log.debug("Exception caught trying to convert IP address %s: %s" %
+                (addr, e))
+
+    ip6_config_path = device_props_iface.Get(NM_DEVICE_IFACE, 'Ip6Config')
+    if ip6_config_path != '/':
+        ip6_config_obj = bus.get_object(NM_SERVICE, ip6_config_path)
+        ip6_config_props = dbus.Interface(ip6_config_obj, DBUS_PROPS_IFACE)
+
+        addrs = ip6_config_props.Get(NM_IP6CONFIG_IFACE, "Addresses")
+        for addr in addrs:
+            try:
+                addrstr = "".join(str(byte) for byte in addr[0])
+                ipaddr = socket.inet_ntop(socket.AF_INET6, addrstr)
+                # XXX - should we prefer Global or Site-Local types?
+                #       does NM prefer them?
+                addresses['ipv6'].append(ipaddr)
+            except ValueError as e:
+                log.debug("Exception caught trying to convert IP address %s: %s" %
+                (addr, e))
+
+    return addresses
 
 ## Get the correct context for a file from loaded policy.
 # @param fn The filename to query.
diff --git a/network.py b/network.py
index 02f1ff2..1f4d393 100644
--- a/network.py
+++ b/network.py
@@ -79,39 +79,20 @@ def getDefaultHostname(anaconda):
     isys.resetResolv()
 
     hn = None
-    bus = dbus.SystemBus()
-    nm = bus.get_object(isys.NM_SERVICE, isys.NM_MANAGER_PATH)
-    nm_props_iface = dbus.Interface(nm, isys.DBUS_PROPS_IFACE)
-
-    active_connections = nm_props_iface.Get(isys.NM_MANAGER_IFACE, "ActiveConnections")
-
-    # XXX: account for Ip6Config objects when NetworkManager supports them
-    for connection in active_connections:
-        active_connection = bus.get_object(isys.NM_SERVICE, connection)
-        active_connection_props_iface = dbus.Interface(active_connection, isys.DBUS_PROPS_IFACE)
-        devices = active_connection_props_iface.Get(isys.NM_ACTIVE_CONNECTION_IFACE, 'Devices')
-
-        for device_path in devices:
-            device = bus.get_object(isys.NM_SERVICE, device_path)
-            device_props_iface = dbus.Interface(device, isys.DBUS_PROPS_IFACE)
 
-            ip4_config_path = device_props_iface.Get(isys.NM_DEVICE_IFACE, 'Ip4Config')
-            ip4_config_obj = bus.get_object(isys.NM_SERVICE, ip4_config_path)
-            ip4_config_props = dbus.Interface(ip4_config_obj, isys.DBUS_PROPS_IFACE)
-
-            # addresses (3-element list:  ipaddr, netmask, gateway)
-            addrs = ip4_config_props.Get(isys.NM_IP4CONFIG_IFACE, "Addresses")[0]
+    # First address (we prefer ipv4) of last device (as it used to be) wins
+    for dev in getActiveNetDevs():
+        addrs = isys.getIPAddresses(dev)
+        for ipaddr in addrs['ipv4'] + addrs['ipv6']:
             try:
-                tmp = struct.pack('I', addrs[0])
-                ipaddr = socket.inet_ntop(socket.AF_INET, tmp)
                 hinfo = socket.gethostbyaddr(ipaddr)
-
+            except Exception as e:
+                log.debug("Exception caught trying to het host name of %s: %s" %
+                          (ipaddr, e))
+            else:
                 if len(hinfo) == 3:
                     hn = hinfo[0]
-                else:
-                    continue
-            except:
-                continue
+                    break
 
     if hn and hn != 'localhost' and hn != 'localhost.localdomain':
         return hn
diff --git a/vnc.py b/vnc.py
index 0a6a049..66bb351 100644
--- a/vnc.py
+++ b/vnc.py
@@ -108,8 +108,11 @@ class VncServer:
             dev = devices[active_devs[0]]
 
             try:
-                self.ip = isys.getIPAddress(dev.get("DEVICE"))
-                log.info("ip of %s is %s" % (dev.get("DEVICE"), self.ip))
+                addrs = isys.getIPAddresses(dev.get("DEVICE"))
+                ips = addrs['ipv4'] + addrs['ipv6']
+                self.ip = ips[0]
+                log.info("IPs (using first) of device %s: %s" % (dev.get("DEVICE"),
+                                                                 ips))
 
                 if self.ip == "127.0.0.1" or self.ip == "::1":
                     self.ip = None
-- 
1.6.0.6


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