summaryrefslogtreecommitdiff
path: root/examples/C
diff options
context:
space:
mode:
authorJagadeesh Kotra <jagadeesh@stdin.top>2021-03-19 16:54:00 +0530
committerThomas Haller <thaller@redhat.com>2021-03-22 15:54:07 +0100
commit905f9975d2868e6544d4759a90be85facbfd59b1 (patch)
tree8cd89e3da985f3d66b961ed221a710ca2dd1c9c8 /examples/C
parent91bf576a43ca013a66a55f679102046a9d95e716 (diff)
downloadNetworkManager-905f9975d2868e6544d4759a90be85facbfd59b1.tar.gz
example: importing vpn with libnm
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/789
Diffstat (limited to 'examples/C')
-rw-r--r--examples/C/glib/meson.build1
-rw-r--r--examples/C/glib/vpn-import-libnm.c72
2 files changed, 73 insertions, 0 deletions
diff --git a/examples/C/glib/meson.build b/examples/C/glib/meson.build
index 5bee13c1dc..8899a90fa4 100644
--- a/examples/C/glib/meson.build
+++ b/examples/C/glib/meson.build
@@ -9,6 +9,7 @@ examples = [
['list-connections-libnm', []],
['monitor-nm-running-gdbus', []],
['monitor-nm-state-gdbus', []],
+ ['vpn-import-libnm', []],
]
foreach example: examples
diff --git a/examples/C/glib/vpn-import-libnm.c b/examples/C/glib/vpn-import-libnm.c
new file mode 100644
index 0000000000..ccdbe92310
--- /dev/null
+++ b/examples/C/glib/vpn-import-libnm.c
@@ -0,0 +1,72 @@
+/* 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>
+#include <stdlib.h>
+
+static void
+add_cb(NMClient *client, GAsyncResult *result, GMainLoop *loop)
+{
+ GError *err = NULL;
+ nm_client_add_connection_finish(client, result, &err);
+ if (err != NULL) {
+ g_print("Error: %s\n", err->message);
+ } else {
+ g_print("Connection Added.\n");
+ }
+
+ g_main_loop_quit(loop);
+}
+
+int
+main(int argc, char **argv)
+{
+ GMainLoop * loop = g_main_loop_new(NULL, FALSE);
+ GSList * plugins;
+ GSList * iter;
+ NMVpnEditorPlugin *editor;
+ NMClient * client;
+ GError * err = NULL;
+ NMConnection * conn = NULL;
+
+ if (argc < 2) {
+ g_print("program takes exactly one(1) argument.\n");
+ exit(1);
+ }
+
+ plugins = nm_vpn_plugin_info_list_load();
+ g_assert(plugins != NULL);
+
+ for (iter = plugins; iter; iter = iter->next) {
+ const char *plugin_name = nm_vpn_plugin_info_get_name(iter->data);
+ g_print("Trying Plugin: %s\n", plugin_name);
+
+ //try to load plugin
+ editor = nm_vpn_plugin_info_load_editor_plugin(iter->data, NULL);
+
+ conn = nm_vpn_editor_plugin_import(editor, argv[1], &err);
+ if (err != NULL) {
+ g_print("Error: %s\n", err->message);
+ g_error_free(err);
+ err = NULL;
+ } else {
+ g_print("%s imported with %s plugin.\n", argv[1], plugin_name);
+ break;
+ }
+ }
+
+ g_slist_free_full(plugins, g_object_unref);
+ g_assert(conn != NULL);
+
+ client = nm_client_new(NULL, NULL);
+
+ nm_client_add_connection_async(client, conn, TRUE, NULL, (GAsyncReadyCallback) add_cb, loop);
+ g_main_loop_run(loop);
+}