Files
NetworkManager/examples/C/glib/add-connection-libnm.c
Thomas Haller 8bace23beb all: cleanup includes and let "nm-default.h" include "config.h"
- All internal source files (except "examples", which are not internal)
  should include "config.h" first. As also all internal source
  files should include "nm-default.h", let "config.h" be included
  by "nm-default.h" and include "nm-default.h" as first in every
  source file.
  We already wanted to include "nm-default.h" before other headers
  because it might contains some fixes (like "nm-glib.h" compatibility)
  that is required first.

- After including "nm-default.h", we optinally allow for including the
  corresponding header file for the source file at hand. The idea
  is to ensure that each header file is self contained.

- Don't include "config.h" or "nm-default.h" in any header file
  (except "nm-sd-adapt.h"). Public headers anyway must not include
  these headers, and internal headers are never included after
  "nm-default.h", as of the first previous point.

- Include all internal headers with quotes instead of angle brackets.
  In practice it doesn't matter, because in our public headers we must
  include other headers with angle brackets. As we use our public
  headers also to compile our interal source files, effectively the
  result must be the same. Still do it for consistency.

- Except for <config.h> itself. Include it with angle brackets as suggested by
  https://www.gnu.org/software/autoconf/manual/autoconf.html#Configuration-Headers
2016-02-19 17:53:25 +01:00

133 lines
4.0 KiB
C

/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 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 `pkg-config --libs --cflags glib-2.0 libnm` add-connection-libnm.c -o add-connection-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;
/* 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;
#if !GLIB_CHECK_VERSION (2, 35, 0)
/* Initialize GType system */
g_type_init ();
#endif
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;
}