Files
NetworkManager/examples/C/glib/vpn-import-libnm.c
Thomas Haller 615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00

164 lines
4.3 KiB
C

/* 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;
}