From 905f9975d2868e6544d4759a90be85facbfd59b1 Mon Sep 17 00:00:00 2001 From: Jagadeesh Kotra Date: Fri, 19 Mar 2021 16:54:00 +0530 Subject: [PATCH] example: importing vpn with libnm https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/789 --- Makefile.examples | 10 ++++- examples/C/glib/meson.build | 1 + examples/C/glib/vpn-import-libnm.c | 72 ++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 examples/C/glib/vpn-import-libnm.c diff --git a/Makefile.examples b/Makefile.examples index d427397830..437b1e0ed7 100644 --- a/Makefile.examples +++ b/Makefile.examples @@ -20,7 +20,9 @@ check_programs_norun += \ examples/C/glib/list-connections-gdbus \ examples/C/glib/list-connections-libnm \ examples/C/glib/monitor-nm-running-gdbus \ - examples/C/glib/monitor-nm-state-gdbus + examples/C/glib/monitor-nm-state-gdbus \ + examples/C/glib/vpn-import-libnm \ + $(NULL) examples_C_glib_add_connection_gdbus_CPPFLAGS = $(examples_C_glib_cppflags_gdbus) examples_C_glib_add_connection_gdbus_LDADD = \ @@ -63,6 +65,12 @@ examples_C_glib_monitor_nm_state_gdbus_CPPFLAGS = $(examples_C_glib_cppflags_gdb examples_C_glib_monitor_nm_state_gdbus_LDADD = \ $(GLIB_LIBS) +examples_C_glib_vpn_import_libnm_CPPFLAGS = $(examples_C_glib_cppflags_libnm) +examples_C_glib_vpn_import_libnm_LDADD = \ + src/libnm-client-impl/libnm.la \ + $(GLIB_LIBS) +$(examples_C_glib_vpn_import_libnm_OBJECTS): $(src_libnm_core_public_mkenums_h) $(src_libnm_client_public_mkenums_h) + EXTRA_DIST += \ examples/C/glib/meson.build 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 + * + * Compile with: + * gcc -Wall vpn-import-libnm.c -o vpn-import-libnm `pkg-config --cflags --libs libnm` + */ + +#include +#include +#include + +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); +}