-# 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()
- 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_C ONNECTION_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]]
if self.ip == "127.0.0.1" or self.ip == "::1":
self.ip = None
--
1.6.0.6
_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
06-18-2010, 05:30 PM
Chris Lumens
Account for ipv6 addresses too (#594090)
> -# 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
>
You return a dict of two lists, but in the two places getIPAddresses is
called, you only use the concatenation of those two lists. It looks to
me like it makes more sense to always return a single list. We can
always figure out if an address is IPv6 or not later, should we care.
This could perhaps be abstracted out a bit, but I think it would make
for more confusing code.
- Chris
_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
06-21-2010, 10:51 AM
Radek Vykydal
Account for ipv6 addresses too (#594090)
Chris Lumens wrote:
-# 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
You return a dict of two lists, but in the two places getIPAddresses is
called, you only use the concatenation of those two lists. It looks to
me like it makes more sense to always return a single list. We can
always figure out if an address is IPv6 or not later, should we care.
In one of the places, the order is important and the caller
code seems more readable to me with getIPAddresses returning
the dict because suggested list would require to detect type and sort
addresses in the list (I don't like the idea of having another
function/code doing this), or it would have to rely on assumption
of order addresses returned in the list.
How about having a parameter version='ipv4' to return ipv4 addresses
only, 'ipv6' to return ipv6 addresses and None to return list as
you suggested?
... of course we could rely on implicit order ipv4, ipv6, but I like
to be more explicit for code reader.
Radek
_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
06-21-2010, 06:47 PM
David Cantrell
Account for ipv6 addresses too (#594090)
On Mon, 21 Jun 2010, Radek Vykydal wrote:
Chris Lumens wrote:
-# 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
You return a dict of two lists, but in the two places getIPAddresses is
called, you only use the concatenation of those two lists. It looks to
me like it makes more sense to always return a single list. We can
always figure out if an address is IPv6 or not later, should we care.
In one of the places, the order is important and the caller
code seems more readable to me with getIPAddresses returning
the dict because suggested list would require to detect type and sort
addresses in the list (I don't like the idea of having another
function/code doing this), or it would have to rely on assumption
of order addresses returned in the list.
How about having a parameter version='ipv4' to return ipv4 addresses
only, 'ipv6' to return ipv6 addresses and None to return list as
you suggested?