about summary refs log tree commit diff
path: root/examples
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2017-05-11 14:55:55 +0200
committerMichael Biebl <biebl@debian.org>2017-05-11 14:55:55 +0200
commitc333f062ddcba9b35330647bf6cbd0a07f2d786e (patch)
tree257c3a0c74c09f4ad2328eab5b932806405f0c1c /examples
parenta222e56e103f949b148a6942e385ccca2c26d9f3 (diff)
New upstream version 1.8.0 upstream/1.8.0
Diffstat (limited to 'examples')
-rw-r--r--examples/C/glib/add-connection-gdbus.c5
-rw-r--r--examples/C/glib/get-active-connections-gdbus.c5
-rw-r--r--examples/C/glib/list-connections-gdbus.c3
-rw-r--r--examples/C/qt/add-connection-wired.cpp3
-rw-r--r--examples/C/qt/change-ipv4-addresses.cpp4
-rw-r--r--examples/C/qt/list-connections.cpp3
-rw-r--r--examples/nm-conf.d/31-mac-addr-change.conf11
-rwxr-xr-xexamples/python/gi/setting-user-data.py246
8 files changed, 273 insertions, 7 deletions
diff --git a/examples/C/glib/add-connection-gdbus.c b/examples/C/glib/add-connection-gdbus.c
index 7a4d5939..f3b64a02 100644
--- a/examples/C/glib/add-connection-gdbus.c
+++ b/examples/C/glib/add-connection-gdbus.c
@@ -29,6 +29,11 @@
 
 #include <gio/gio.h>
 #include <uuid/uuid.h>
+
+#include <nm-dbus-interface.h>
+
+/* include NetworkManager.h for the defines, but we don't link against
+ * libnm. */
 #include <NetworkManager.h>
 
 /* copied from libnm-core/nm-utils.c */
diff --git a/examples/C/glib/get-active-connections-gdbus.c b/examples/C/glib/get-active-connections-gdbus.c
index 6a19bf89..70bd6747 100644
--- a/examples/C/glib/get-active-connections-gdbus.c
+++ b/examples/C/glib/get-active-connections-gdbus.c
@@ -28,6 +28,11 @@
 
 #include <string.h>
 #include <gio/gio.h>
+
+#include <nm-dbus-interface.h>
+
+/* include NetworkManager.h for the defines, but we don't link against
+ * libnm. */
 #include <NetworkManager.h>
 
 static void
diff --git a/examples/C/glib/list-connections-gdbus.c b/examples/C/glib/list-connections-gdbus.c
index f5922582..14a633f7 100644
--- a/examples/C/glib/list-connections-gdbus.c
+++ b/examples/C/glib/list-connections-gdbus.c
@@ -28,7 +28,8 @@
  */
 
 #include <gio/gio.h>
-#include <NetworkManager.h>
+
+#include <nm-dbus-interface.h>
 
 static void
 list_connections (GDBusProxy *proxy)
diff --git a/examples/C/qt/add-connection-wired.cpp b/examples/C/qt/add-connection-wired.cpp
index fe790ecd..c06ed71f 100644
--- a/examples/C/qt/add-connection-wired.cpp
+++ b/examples/C/qt/add-connection-wired.cpp
@@ -28,10 +28,9 @@
 #include <QtDBus/QDBusInterface>

 #include <QtDBus/QDBusMetaType>

 #include <QtDBus/QDBusReply>

-

 #include <QtCore/QDebug>

 

-#include "nm-dbus-interface.h"

+#include <nm-dbus-interface.h>

 

 typedef QMap<QString, QMap<QString, QVariant> > Connection;

 Q_DECLARE_METATYPE(Connection)

diff --git a/examples/C/qt/change-ipv4-addresses.cpp b/examples/C/qt/change-ipv4-addresses.cpp
index 8ca3dde2..d403900a 100644
--- a/examples/C/qt/change-ipv4-addresses.cpp
+++ b/examples/C/qt/change-ipv4-addresses.cpp
@@ -38,9 +38,9 @@
 #include <QtCore/QDebug>
 #include <QtNetwork/QHostAddress>
 
-#include "arpa/inet.h"
+#include <arpa/inet.h>
 
-#include "nm-dbus-interface.h"
+#include <nm-dbus-interface.h>
 
 typedef QMap<QString, QMap<QString, QVariant> > Connection;
 Q_DECLARE_METATYPE(Connection)
diff --git a/examples/C/qt/list-connections.cpp b/examples/C/qt/list-connections.cpp
index 66f309c8..48dd685e 100644
--- a/examples/C/qt/list-connections.cpp
+++ b/examples/C/qt/list-connections.cpp
@@ -25,10 +25,9 @@
 #include <QtDBus/QDBusConnection>
 #include <QtDBus/QDBusInterface>
 #include <QtDBus/QDBusReply>
-
 #include <QtCore/QDebug>
 
-#include "nm-dbus-interface.h"
+#include <nm-dbus-interface.h>
 
 
 void listConnections(QDBusInterface& interface) {
diff --git a/examples/nm-conf.d/31-mac-addr-change.conf b/examples/nm-conf.d/31-mac-addr-change.conf
new file mode 100644
index 00000000..1ae2e0dc
--- /dev/null
+++ b/examples/nm-conf.d/31-mac-addr-change.conf
@@ -0,0 +1,11 @@
+# Certain drivers are known not to support changing the MAC address.
+# Disable touching the MAC address on such devices.
+#
+# See man NetworkManager.conf
+#
+# https://bugzilla.gnome.org/show_bug.cgi?id=777523
+[device-mac-addr-change-wifi]
+match-device=driver:rtl8723bs,driver:rtl8189es,driver:r8188eu,driver:eagle_sdio
+wifi.scan-rand-mac-address=no
+wifi.cloned-mac-address=preserve
+ethernet.cloned-mac-address=preserve
diff --git a/examples/python/gi/setting-user-data.py b/examples/python/gi/setting-user-data.py
new file mode 100755
index 00000000..79fb98cd
--- /dev/null
+++ b/examples/python/gi/setting-user-data.py
@@ -0,0 +1,246 @@
+#!/usr/bin/env python
+# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+# vim: ft=python ts=4 sts=4 sw=4 et ai
+
+#
+# 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 2017 Red Hat, Inc.
+
+#
+# set and show user-data for a connection:
+#
+# - Show all user data for all connections:
+#   $ ./examples/python/gi/setting-user-data.py
+# - Filter to show only connections with matching id or uuid
+#   $ ./examples/python/gi/setting-user-data.py id my-connection
+#   $ ./examples/python/gi/setting-user-data.py uuid 123e4567-e89b-12d3-a456-426655440000
+# - id and uuid can be repeated to select multiple connections
+#   $ ./examples/python/gi/setting-user-data.py id my-connection1 id my-other-connection
+#
+# - Specify the user-data keys you want to see
+#   $ ./examples/python/gi/setting-user-data.py id my-connection my.user.field.1
+#   $ ./examples/python/gi/setting-user-data.py id my-connection my.user.field.1 my.other.userfield
+# - Prefix the field name with ~ to use a regex
+#   $ ./examples/python/gi/setting-user-data.py '~^my\.user\.'
+#
+# - set the fields, you need to select exactly one connection
+#   $ ./examples/python/gi/setting-user-data.py set id "$NAME" my.field.1 my-value1
+# - delete a user-setting
+#   $ ./examples/python/gi/setting-user-data.py set id "$NAME" -d my.field.1
+# - set/delete multiple user data values at once
+#   $ ./examples/python/gi/setting-user-data.py set id "$NAME" my.field.1 my-value1 -d my.other.field
+#
+# - libnm already client side rejects invalid values, like
+#   $ ./examples/python/gi/setting-user-data.py set id "$NAME" invalid_name 'has-no-dot'
+# - to allow client side to specify invalid values and send them to the
+#   server, pass --set-gobject
+#   $ ./examples/python/gi/setting-user-data.py set id "$NAME" invalid_name 'has-no-dot' --set-gobject
+#
+
+import sys
+import re
+
+import gi
+gi.require_version('NM', '1.0')
+from gi.repository import NM
+
+def pr(v):
+    import pprint
+    pprint.pprint(v, indent=4, depth=5, width=60)
+
+def parse_args():
+    args = {
+        'set': False,
+        'set-gobject': False,
+        'filter': [],
+        'data': []
+    }
+    i = 1
+    while i < len(sys.argv):
+        a = sys.argv[i]
+        if i == 1:
+            if a in ['s', 'set']:
+                args['set'] = True
+                i += 1
+                continue
+            elif a in ['g', 'get']:
+                args['set'] = False
+                i += 1
+                continue
+        if a in ['id', 'uuid']:
+            args['filter'].append((a, sys.argv[i+1]))
+            i += 2
+            continue
+
+        if a in ['--set-gobject']:
+            args['set-gobject'] = True
+            i += 1
+            continue
+
+        if a == 'data':
+            i += 1
+            a = sys.argv[i]
+        if args['set']:
+            if a == '-d':
+                args['data'].append((sys.argv[i+1], None))
+            else:
+                args['data'].append((a, sys.argv[i+1]))
+            i += 2
+        else:
+            args['data'].append(a)
+            i += 1
+
+    return args
+
+def connection_to_str(connection):
+    return '%s (%s)' % (connection.get_id(), connection.get_uuid())
+
+def connections_filter(connections, filter_data):
+    connections = list(sorted(connections, key=connection_to_str))
+    if not filter_data:
+        return connections
+    # we preserve the order of the selected connections. And
+    # if connections are selected multiple times, we return
+    # them multiple times.
+    l = []
+    for f in filter_data:
+        if f[0] == 'id':
+            for c in connections:
+                if f[1] == c.get_id():
+                    l.append(c)
+        else:
+            assert(f[0] == 'uuid')
+            for c in connections:
+                if f[1] == c.get_uuid():
+                    l.append(c)
+    return l
+
+def print_user_data(connection, data_allow_regex, data, prefix=''):
+    s_u = connection.get_setting(NM.SettingUser)
+    n = 'none'
+    keys_len = 0
+    keys = []
+    if s_u is not None:
+        all_keys = s_u.get_keys()
+        keys_len = len(all_keys)
+        if data:
+            for d in data:
+                if data_allow_regex and len(d) > 0 and d[0] == '~':
+                    r = re.compile(d[1:])
+                    keys.extend([k for k in all_keys if r.match(k)])
+                else:
+                    keys.append (d)
+        else:
+            keys.extend(all_keys)
+        n = '%s' % (keys_len)
+
+    print('%s%s [%s]' % (prefix, connection_to_str(connection), n))
+    dd = { }
+    if s_u is not None:
+        dd = s_u.get_property(NM.SETTING_USER_DATA)
+    for k in keys:
+        if s_u is not None:
+            v = s_u.get_data(k)
+            if v is None:
+                if k in dd:
+                    print('%s   INVALID:   "%s" = "%s"' % (prefix, k, dd[k]))
+                else:
+                    print('%s   MISSING:   "%s"' % (prefix, k))
+            else:
+                assert(v == dd.get(k, None))
+                print('%s   SET:       "%s" = "%s"' % (prefix, k, v))
+        else:
+            print('%s   MISSING:  "%s"' % (prefix, k))
+
+
+def do_get(connections, data):
+    first_line = True
+    connections = list(connections)
+    if not connections:
+        print('no matching connections (use id|uuid argument)')
+        sys.exit(1)
+    for c in connections:
+        if first_line:
+            first_line = False
+        else:
+            print('')
+        print_user_data(c, True, data)
+
+def do_set(connection, data, set_gobject):
+    print_user_data(connection, False,
+                    [d[0] for d in data],
+                    prefix = 'BEFORE: ')
+    print('')
+    s_u = connection.get_setting(NM.SettingUser)
+    if s_u is None:
+        connection.add_setting(NM.SettingUser())
+        s_u = connection.get_setting(NM.SettingUser)
+    for d in data:
+        key = d[0]
+        val = d[1]
+        if val is None:
+            print(' DEL: "%s"' % (key))
+        else:
+            print(' SET: "%s" = "%s"' % (key, val))
+        if set_gobject:
+            d = s_u.get_property(NM.SETTING_USER_DATA)
+            if val is None:
+                d.pop(key, None)
+            else:
+                d[key] = val
+            s_u.set_property(NM.SETTING_USER_DATA, d)
+        else:
+            try:
+                s_u.set_data(key, val)
+            except Exception as e:
+                if val is None:
+                    print('error deleting key "%s": %s' % (key, e))
+                else:
+                    print('error setting key "%s" = "%s": %s' % (key, val, e))
+                sys.exit(1)
+
+
+    try:
+        connection.commit_changes(True, None)
+    except Exception as e:
+        print('failure to commit connection: %s' % (e))
+        sys.exit(1)
+
+    print('')
+    print_user_data(connection, False,
+                    [d[0] for d in data],
+                    prefix = 'AFTER:  ')
+
+###############################################################################
+
+if __name__ == '__main__':
+    args = parse_args()
+    nm_client = NM.Client.new(None)
+
+    connections = connections_filter(nm_client.get_connections(), args['filter'])
+
+    if args['set']:
+        if not args['data']:
+            print('Requires one or more arguments to set or delete')
+            sys.exit(1)
+        if len(connections) != 1:
+            print('To set the user-data of a connection, exactly one connection must be selected via id|uuid. Instead, %s connection matched ([%s])' %
+                  (len(connections), ', '.join([connection_to_str(c) for c in connections])))
+            sys.exit(1)
+        do_set(connections[0], args['data'], args['set-gobject'])
+    else:
+        do_get(connections, args['data'])
+