[PATCH rhel6-branch] (take II) Account for ipv6 addresses too (#605659)

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

 



Related: #594090

Based on review I only changed getIPAddresses to return a list
instead of dict and to take version parameter.

---
 isys/isys.py |   56 +++++++++++++++++++++++++++++++++++++++++++++-----------
 network.py   |   38 ++++++++++----------------------------
 vnc.py       |    9 ++++++---
 3 files changed, 61 insertions(+), 42 deletions(-)

diff --git a/isys/isys.py b/isys/isys.py
index 7ab5988..55c7165 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,11 @@ def isWireless(dev):
     else:
         return False
 
-# Get the IP address for a network device.
-def getIPAddress(dev):
+# Get IP addresses for a network device.
+# Returns list of ipv4 and ipv6 addresses.
+# With version=4 returns only ipv4 addresses,
+# with version=6 returns only ipv6 addresses.
+def getIPAddresses(dev, version=None):
     if dev == '' or dev is None:
        return None
 
@@ -518,16 +522,46 @@ 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")
-
-    try:
-        tmp = struct.pack('I', device_ip4addr)
-        address = socket.inet_ntop(socket.AF_INET, tmp)
-    except ValueError, e:
-        return None
+    bus = dbus.SystemBus()
 
-    return address
+    addresses = []
+
+    if not version == 6:
+        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.append(ipaddr)
+                except ValueError as e:
+                    log.debug("Exception caught trying to convert IP address %s: %s" %
+                    (addr, e))
+
+    if not version == 4:
+        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.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 c86d6e4..9bebc4b 100644
--- a/network.py
+++ b/network.py
@@ -79,39 +79,21 @@ 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, version=4) +
+                 isys.getIPAddresses(dev, version=6))
+        for ipaddr in addrs:
             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 get 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..6f9f4d3 100644
--- a/vnc.py
+++ b/vnc.py
@@ -108,14 +108,17 @@ 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))
+                devname = dev.get("DEVICE")
+                ips = (isys.getIPAddresses(devname, version=4) +
+                       isys.getIPAddresses(devname, version=6))
+                self.ip = ips[0]
+                log.info("IPs (using first) of device %s: %s" % (devname, ips))
 
                 if self.ip == "127.0.0.1" or self.ip == "::1":
                     self.ip = None
             except Exception, e:
                 log.warning("Got an exception trying to get the self.ip addr "
-                            "of %s: %s" % (dev.get("DEVICE"), e))
+                            "of %s: %s" % (devname, e))
         else:
             self.ip = None
 
-- 
1.6.0.6

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list


[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux