diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/C/glib/meson.build | 23 | ||||
| -rw-r--r-- | examples/C/glib/vpn-import-libnm.c | 163 | ||||
| -rw-r--r-- | examples/C/qt/meson.build | 16 | ||||
| -rwxr-xr-x | examples/python/dbus/create-bond.py | 18 | ||||
| -rwxr-xr-x | examples/python/dbus/vpn.py | 11 | ||||
| -rwxr-xr-x | examples/python/gi/ovs-external-ids.py | 7 |
6 files changed, 200 insertions, 38 deletions
diff --git a/examples/C/glib/meson.build b/examples/C/glib/meson.build index 41c46ca8..8899a90f 100644 --- a/examples/C/glib/meson.build +++ b/examples/C/glib/meson.build @@ -1,20 +1,23 @@ # SPDX-License-Identifier: LGPL-2.1-or-later examples = [ - ['add-connection-gdbus', [libnm_enum_sources[1]], [uuid_dep]], - ['add-connection-libnm', [], [libnm_dep]], - ['get-active-connections-gdbus', [libnm_enum_sources[1]], []], - ['get-ap-info-libnm', [], [libnm_dep]], - ['list-connections-gdbus', [], []], - ['list-connections-libnm', [], [libnm_dep]], - ['monitor-nm-running-gdbus', [], []], - ['monitor-nm-state-gdbus', [], []], + ['add-connection-gdbus', [uuid_dep]], + ['add-connection-libnm', []], + ['get-active-connections-gdbus', []], + ['get-ap-info-libnm', []], + ['list-connections-gdbus', []], + ['list-connections-libnm', []], + ['monitor-nm-running-gdbus', []], + ['monitor-nm-state-gdbus', []], + ['vpn-import-libnm', []], ] foreach example: examples executable( example[0], - [example[0] + '.c'] + example[1], - dependencies: [libnm_nm_default_dep] + example[2], + [example[0] + '.c'], + dependencies: [ + libnm_dep, + ] + example[1], ) endforeach diff --git a/examples/C/glib/vpn-import-libnm.c b/examples/C/glib/vpn-import-libnm.c new file mode 100644 index 00000000..55ed5175 --- /dev/null +++ b/examples/C/glib/vpn-import-libnm.c @@ -0,0 +1,163 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * The example shows how to import VPN connection from a file. + * + * @author: Jagadeesh Kotra <jagadeesh@stdin.top> + * + * Compile with: + * gcc -Wall vpn-import-libnm.c -o vpn-import-libnm `pkg-config --cflags --libs libnm` + */ + +#include <glib.h> +#include <NetworkManager.h> + +/*****************************************************************************/ + +static NMConnection * +vpn_connection_import(const char *filename) +{ + NMConnection *conn = NULL; + GSList * plugins; + GSList * iter; + + g_print("Try to import file \"%s\"...\n", filename); + + plugins = nm_vpn_plugin_info_list_load(); + + for (iter = plugins; iter; iter = iter->next) { + GError * error = NULL; + NMVpnPluginInfo * plugin = iter->data; + NMVpnEditorPlugin *editor; + const char * plugin_name = nm_vpn_plugin_info_get_name(plugin); + + g_print("plugin[%s]: trying import...\n", plugin_name); + + editor = nm_vpn_plugin_info_load_editor_plugin(plugin, &error); + if (error) { + g_print("plugin[%s]: error loading plugin: %s\n", plugin_name, error->message); + g_clear_error(&error); + continue; + } + + conn = nm_vpn_editor_plugin_import(editor, filename, &error); + if (error) { + g_print("plugin[%s]: error importing file: %s\n", plugin_name, error->message); + g_clear_error(&error); + continue; + } + + if (!nm_connection_normalize(conn, NULL, NULL, &error)) { + g_print("plugin[%s]: imported connection invalid: %s\n", plugin_name, error->message); + g_clear_error(&error); + g_clear_object(&conn); + continue; + } + + g_print("plugin[%s]: imported connection \"%s\" (%s)\n", + plugin_name, + nm_connection_get_id(conn), + nm_connection_get_uuid(conn)); + break; + } + g_slist_free_full(plugins, g_object_unref); + + if (!conn) { + g_print("Failure to import the file with any plugin\n"); + return NULL; + } + + return conn; +} + +/*****************************************************************************/ + +typedef struct { + GMainLoop * loop; + GError * error; + NMRemoteConnection *rconn; +} RequestData; + +static void +add_cb(GObject *source, GAsyncResult *result, gpointer user_data) +{ + RequestData *rdata = user_data; + + rdata->rconn = nm_client_add_connection_finish(NM_CLIENT(source), result, &rdata->error); + g_main_loop_quit(rdata->loop); +} + +static NMRemoteConnection * +connection_add(NMConnection *conn) +{ + GError * error = NULL; + NMClient * client; + RequestData rdata; + + g_print("Adding connection \"%s\" (%s)\n", + nm_connection_get_id(conn), + nm_connection_get_uuid(conn)); + + client = nm_client_new(NULL, &error); + if (!client) { + g_print("Failure to connect with NetworkManager: %s\n", error->message); + return NULL; + } + + g_print("Adding connection \"%s\" (%s)\n", + nm_connection_get_id(conn), + nm_connection_get_uuid(conn)); + + rdata = (RequestData){ + .loop = g_main_loop_new(NULL, FALSE), + .rconn = NULL, + .error = NULL, + }; + + nm_client_add_connection_async(client, conn, TRUE, NULL, add_cb, &rdata); + + g_main_loop_run(rdata.loop); + + g_clear_pointer(&rdata.loop, g_main_loop_unref); + + if (rdata.error != NULL) { + g_print("Error: %s\n", rdata.error->message); + g_clear_error(&rdata.error); + } else { + g_print("Connection successfully added: %s\n", nm_object_get_path(NM_OBJECT(rdata.rconn))); + } + + g_clear_object(&client); + + return rdata.rconn; +} + +/*****************************************************************************/ + +int +main(int argc, char **argv) +{ + NMRemoteConnection *rconn; + NMConnection * conn; + const char * filename; + gboolean success; + + if (argc < 2) { + g_print("program takes exactly one(1) argument.\n"); + return 1; + } + + filename = argv[1]; + + conn = vpn_connection_import(filename); + if (!conn) + return 1; + + rconn = connection_add(conn); + + success = (rconn != NULL); + + g_clear_object(&conn); + g_clear_object(&rconn); + + return success ? 0 : 1; +} diff --git a/examples/C/qt/meson.build b/examples/C/qt/meson.build index 7e4e1274..8b905bd6 100644 --- a/examples/C/qt/meson.build +++ b/examples/C/qt/meson.build @@ -6,13 +6,6 @@ examples = [ ['change-ipv4-addresses', []], ] -deps = [ - dbus_dep, - qt_core_dep, - qt_dbus_dep, - qt_network_dep, -] - moc = find_program('moc-qt4', required: false) if not moc.found() moc = qt_core_dep.get_pkgconfig_variable('moc_location') @@ -34,8 +27,13 @@ foreach example: examples executable( example[0], example[0] + '.cpp', - include_directories: libnm_core_inc, - dependencies: deps, + include_directories: libnm_core_public_inc, + dependencies: [ + dbus_dep, + qt_core_dep, + qt_dbus_dep, + qt_network_dep, + ], link_depends: example[1], ) endforeach diff --git a/examples/python/dbus/create-bond.py b/examples/python/dbus/create-bond.py index 4ebec24a..fe35fcc0 100755 --- a/examples/python/dbus/create-bond.py +++ b/examples/python/dbus/create-bond.py @@ -40,8 +40,8 @@ def create_bond(bond_name): "autoconnect-slaves": 1, } ) - s_ip4 = dbus.Dictionary({"method": "auto"}) - s_ip6 = dbus.Dictionary({"method": "ignore"}) + s_ip4 = dbus.Dictionary({"method": "disabled"}) + s_ip6 = dbus.Dictionary({"method": "disabled"}) con = dbus.Dictionary( {"bond": s_bond, "connection": s_con, "ipv4": s_ip4, "ipv6": s_ip6} @@ -97,18 +97,22 @@ print("Activating bond: %s (%s)" % (bond_name, ac)) loop = GLib.MainLoop() -def properties_changed(props): - if "State" in props: - if props["State"] == 2: +def properties_changed(interface_name, changed_properties, invalidated_properties): + if ( + interface_name == "org.freedesktop.NetworkManager.Connection.Active" + and "State" in changed_properties + ): + state = changed_properties["State"] + if state == 2: print("Successfully connected") loop.quit() - if props["State"] == 3 or props["State"] == 4: + if state == 3 or state == 4: print("Bond activation failed") loop.quit() obj = bus.get_object("org.freedesktop.NetworkManager", ac) -iface = dbus.Interface(obj, "org.freedesktop.NetworkManager.Connection.Active") +iface = dbus.Interface(obj, "org.freedesktop.DBus.Properties") iface.connect_to_signal("PropertiesChanged", properties_changed) loop.run() diff --git a/examples/python/dbus/vpn.py b/examples/python/dbus/vpn.py index f86bf1ad..794fb022 100755 --- a/examples/python/dbus/vpn.py +++ b/examples/python/dbus/vpn.py @@ -10,11 +10,8 @@ # The uuid of the connection to activate CONNECTION_UUID = "c08142a4-00d9-45bd-a3b1-7610fe146374" -# UID to use. Note that NM only allows the owner of the connection to activate it. -# UID=1000 -UID = 0 - -import sys, os, dbus +import dbus +import sys from dbus.mainloop.glib import DBusGMainLoop from gi.repository import GLib @@ -127,10 +124,6 @@ def activate_connection(connection_path, device_path): ) -# Change the UID first if required -if UID != 0: - os.setuid(UID) - # Are we configured? if not len(CONNECTION_UUID): print("missing connection UUID") diff --git a/examples/python/gi/ovs-external-ids.py b/examples/python/gi/ovs-external-ids.py index 63f9695e..3bc9de8f 100755 --- a/examples/python/gi/ovs-external-ids.py +++ b/examples/python/gi/ovs-external-ids.py @@ -68,7 +68,7 @@ def can_sudo(): ).returncode == 0 ) - except: + except Exception: return False @@ -222,7 +222,6 @@ def die_usage(msg): def parse_args(argv): - had_dash_dash = False args = { "mode": MODE_GET, "select_arg": None, @@ -615,7 +614,9 @@ def do_apply(nmc, device, ids_arg, do_test): die("FAILURE to get applied connection after reapply") _print() - connection_print(connection, MODE_APPLY, [], device.get_path(), prefix="AFTER: ") + connection_print( + connection_after, MODE_APPLY, [], device.get_path(), prefix="AFTER: " + ) _print() ovs_print_external_ids("AFTER-OVS-VSCTL: ") |