Blob Blame History Raw
From 7ffbbae3a046dbb4c8d5089c41b143eafabed709 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes@redhat.com>
Date: Thu, 16 Jan 2014 17:29:08 +0100
Subject: [PATCH 1/2] libnm-glib; fix introspection annotations so that
 <ipv6>.get_address() worked
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Without the correct annotation, the functions didn't work correctly in Python
(causing segmentation fault).

Signed-off-by: Jiří Klimeš <jklimes@redhat.com>
---
 libnm-util/nm-setting-ip6-config.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/libnm-util/nm-setting-ip6-config.c b/libnm-util/nm-setting-ip6-config.c
index 7798db3..b02e231 100644
--- a/libnm-util/nm-setting-ip6-config.c
+++ b/libnm-util/nm-setting-ip6-config.c
@@ -1285,7 +1285,8 @@ nm_ip6_address_compare (NMIP6Address *address, NMIP6Address *other)
  *
  * Gets the IPv6 address property of this address object.
  *
- * Returns: (transfer none): the IPv6 address
+ * Returns: (array fixed-size=16) (element-type guint8) (transfer none):
+ *          the IPv6 address
  **/
 const struct in6_addr *
 nm_ip6_address_get_address (NMIP6Address *address)
@@ -1354,7 +1355,9 @@ nm_ip6_address_set_prefix (NMIP6Address *address, guint32 prefix)
  *
  * Gets the IPv6 default gateway property of this address object.
  *
- * Returns: (transfer none): the IPv6 gateway address
+ * Returns: (transfer none): 
+ * Returns: (array fixed-size=16) (element-type guint8) (transfer none):
+ *          the IPv6 gateway address
  **/
 const struct in6_addr *
 nm_ip6_address_get_gateway (NMIP6Address *address)
@@ -1502,7 +1505,8 @@ nm_ip6_route_compare (NMIP6Route *route, NMIP6Route *other)
  *
  * Gets the IPv6 destination address property of this route object.
  *
- * Returns: the IPv6 address
+ * Returns: (array fixed-size=16) (element-type guint8) (transfer none):
+ *          the IPv6 address of destination
  **/
 const struct in6_addr *
 nm_ip6_route_get_dest (NMIP6Route *route)
@@ -1571,7 +1575,8 @@ nm_ip6_route_set_prefix (NMIP6Route *route, guint32 prefix)
  *
  * Gets the IPv6 address of the next hop of this route.
  *
- * Returns: the IPv6 address
+ * Returns: (array fixed-size=16) (element-type guint8) (transfer none):
+ *          the IPv6 address of next hop
  **/
 const struct in6_addr *
 nm_ip6_route_get_next_hop (NMIP6Route *route)
-- 
1.7.11.7


From 68fe50ff3a4774c0abff1d6f8c2f270b73105351 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes@redhat.com>
Date: Thu, 16 Jan 2014 17:27:05 +0100
Subject: [PATCH 2/2] examples: add an python example (using GI) getting
 device IPs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit


Signed-off-by: Jiří Klimeš <jklimes@redhat.com>
---
 examples/python/gi/Makefile.am |   3 +-
 examples/python/gi/get_ips.py  | 127 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100755 examples/python/gi/get_ips.py

diff --git a/examples/python/gi/Makefile.am b/examples/python/gi/Makefile.am
index 78569b3..7f122a4 100644
--- a/examples/python/gi/Makefile.am
+++ b/examples/python/gi/Makefile.am
@@ -2,4 +2,5 @@ EXTRA_DIST = \
 	list-connections.py \
 	device-state-ip4config.py \
 	firewall-zone.py \
-	show-wifi-networks.py
+	show-wifi-networks.py \
+	get_ips.py
diff --git a/examples/python/gi/get_ips.py b/examples/python/gi/get_ips.py
new file mode 100755
index 0000000..b1e59e2
--- /dev/null
+++ b/examples/python/gi/get_ips.py
@@ -0,0 +1,127 @@
+#!/usr/bin/env python
+#
+# vim: ft=python ts=4 sts=4 sw=4 et ai
+# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright (C) 2014 Red Hat, Inc.
+#
+
+import sys, socket, struct
+from gi.repository import GLib, NetworkManager, NMClient
+
+#
+#  This example shows how to get get addresses and routes from NMIP4Config and NMIP6Config
+#  (got out of NMDevice)
+#
+
+def show_addresses(self, family):
+    if (family == socket.AF_INET):
+       ip_cfg = self.get_ip4_config()
+    else:
+       ip_cfg = self.get_ip6_config()
+
+    if ip_cfg is None:
+        print("None")
+        return
+
+    nm_addresses = ip_cfg.get_addresses()
+    if len(nm_addresses) == 0:
+        print("None")
+        return
+
+    for nm_address in nm_addresses:
+        addr = nm_address.get_address()
+        prefix = nm_address.get_prefix()
+        gateway = nm_address.get_gateway()
+
+        if (family == socket.AF_INET):
+            addr_struct = struct.pack("=I", addr)
+            gateway_struct = struct.pack("=I", gateway)
+        else:
+            addr_struct = addr
+            gateway_struct = gateway
+        print("%s/%d  %s") % (socket.inet_ntop(family, addr_struct),
+                              prefix,
+                              socket.inet_ntop(family, gateway_struct))
+
+
+
+def show_routes(self, family):
+    if (family == socket.AF_INET):
+       ip_cfg = self.get_ip4_config()
+    else:
+       ip_cfg = self.get_ip6_config()
+
+    if ip_cfg is None:
+        print("None")
+        return
+
+    nm_routes = ip_cfg.get_routes() 
+    if len(nm_routes) == 0:
+        print("None")
+        return
+
+    for nm_route in nm_routes:
+        dest = nm_route.get_dest()
+        prefix = nm_route.get_prefix()
+        next_hop = nm_route.get_next_hop()
+        metric = nm_route.get_metric()
+
+        if (family == socket.AF_INET):
+            dest_struct = struct.pack("=I", dest)
+            next_hop_struct = struct.pack("=I", next_hop)
+        else:
+            dest_struct = dest
+            next_hop_struct = next_hop
+        print("%s/%d  %s  %d") % (socket.inet_ntop(family, dest_struct),
+                                  prefix,
+                                  socket.inet_ntop(family, next_hop_struct),
+                                  metric)
+
+
+if __name__ == "__main__":
+    if len(sys.argv) != 2:
+        sys.exit('Usage: %s <interface>' % sys.argv[0])
+    dev_iface = sys.argv[1]
+
+    c = NMClient.Client.new()
+    dev = c.get_device_by_iface(dev_iface)
+    if dev is None:
+        sys.exit('Device \'%s\' not found' % dev_iface)
+    print "Device: %s - %s" % (dev_iface, dev.get_device_type().value_name)
+    print "---------------------------------------"
+
+    print("IPv4 addresses:")
+    print("---------------")
+    show_addresses(dev, socket.AF_INET)
+    print
+
+    print("IPv4 routes:")
+    print("------------")
+    show_routes(dev, socket.AF_INET)
+    print
+
+    print "IPv6 addresses:"
+    print("---------------")
+    show_addresses(dev, socket.AF_INET6)
+    print
+
+    print "IPv6 routes:"
+    print("------------")
+    show_routes(dev, socket.AF_INET6)
+    print
+
-- 
1.7.11.7