Files
NetworkManager/examples/C/glib/add-connection-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

115 lines
3.4 KiB
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2011 Red Hat, Inc.
*/
/*
* The example shows how to add a new connection using libnm. Contrast this
* example with add-connection-gdbus.c, which is a bit lower level and talks
* directly to NM using GDBus. This example is simpler because libnm handles
* much of the low-level stuff for you.
*
* Compile with:
* gcc -Wall add-connection-libnm.c -o add-connection-libnm `pkg-config --libs --cflags libnm`
*/
#include <glib.h>
#include <NetworkManager.h>
static void
added_cb(GObject *client, GAsyncResult *result, gpointer user_data)
{
GMainLoop *loop = user_data;
NMRemoteConnection *remote;
GError *error = NULL;
/* NM responded to our request; either handle the resulting error or
* print out the object path of the connection we just added.
*/
remote = nm_client_add_connection_finish(NM_CLIENT(client), result, &error);
if (error) {
g_print("Error adding connection: %s", error->message);
g_error_free(error);
} else {
g_print("Added: %s\n", nm_connection_get_path(NM_CONNECTION(remote)));
g_object_unref(remote);
}
/* Tell the mainloop we're done and we can quit now */
g_main_loop_quit(loop);
}
static void
add_connection(NMClient *client, GMainLoop *loop, const char *con_name)
{
NMConnection *connection;
NMSettingConnection *s_con;
NMSettingWired *s_wired;
NMSettingIP4Config *s_ip4;
char *uuid;
/* Create a new connection object */
connection = nm_simple_connection_new();
/* Build up the 'connection' Setting */
s_con = (NMSettingConnection *) nm_setting_connection_new();
uuid = nm_utils_uuid_generate();
g_object_set(G_OBJECT(s_con),
NM_SETTING_CONNECTION_UUID,
uuid,
NM_SETTING_CONNECTION_ID,
con_name,
NM_SETTING_CONNECTION_TYPE,
"802-3-ethernet",
NULL);
g_free(uuid);
nm_connection_add_setting(connection, NM_SETTING(s_con));
/* Build up the 'wired' Setting */
s_wired = (NMSettingWired *) nm_setting_wired_new();
nm_connection_add_setting(connection, NM_SETTING(s_wired));
/* Build up the 'ipv4' Setting */
s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new();
g_object_set(G_OBJECT(s_ip4),
NM_SETTING_IP_CONFIG_METHOD,
NM_SETTING_IP4_CONFIG_METHOD_AUTO,
NULL);
nm_connection_add_setting(connection, NM_SETTING(s_ip4));
/* Ask the settings service to add the new connection; we'll quit the
* mainloop and exit when the callback is called.
*/
nm_client_add_connection_async(client, connection, TRUE, NULL, added_cb, loop);
g_object_unref(connection);
}
int
main(int argc, char *argv[])
{
NMClient *client;
GMainLoop *loop;
GError *error = NULL;
loop = g_main_loop_new(NULL, FALSE);
/* Connect to NetworkManager */
client = nm_client_new(NULL, &error);
if (!client) {
g_message("Error: Could not connect to NetworkManager: %s.", error->message);
g_error_free(error);
return 1;
}
/* Ask NM to add the new connection */
add_connection(client, loop, "__Test connection__");
/* Wait for the connection to be added */
g_main_loop_run(loop);
/* Clean up */
g_object_unref(client);
return 0;
}