summaryrefslogtreecommitdiff
path: root/examples/C
diff options
context:
space:
mode:
authorDan Williams <dcbw@redhat.com>2011-01-12 15:46:37 -0600
committerDan Williams <dcbw@redhat.com>2011-01-12 15:46:37 -0600
commitf9ceafd4a124142617280101eda5464ac67c5e39 (patch)
treecd336e771bf30ba1ee0347037a7f47095fc04c58 /examples/C
parent7de13fa58dca796493b623fcba00d468f3aaa9f6 (diff)
parent5a35862a1d24fb8d515ea09339123646370c2845 (diff)
Merge remote branch 'origin/master' into rm-userset
Diffstat (limited to 'examples/C')
-rw-r--r--examples/C/Makefile.am54
-rw-r--r--examples/C/add-connection-glib.c7
-rw-r--r--examples/C/get-active-connections.c248
-rw-r--r--examples/C/get-ap-info-libnm-glib.c229
-rw-r--r--examples/C/list-connections-dbus.c83
-rw-r--r--examples/C/list-connections-libnm-glib.c176
6 files changed, 793 insertions, 4 deletions
diff --git a/examples/C/Makefile.am b/examples/C/Makefile.am
index 68c0697b..a8b5a640 100644
--- a/examples/C/Makefile.am
+++ b/examples/C/Makefile.am
@@ -1,2 +1,54 @@
+INCLUDES = -I${top_srcdir}/libnm-util \
+ -I${top_srcdir}/libnm-glib \
+ -I${top_srcdir}/include
+
+AM_CPPFLAGS = \
+ $(DBUS_CFLAGS) \
+ $(GLIB_CFLAGS)
+
+noinst_PROGRAMS = \
+ add-connection-glib \
+ get-active-connections \
+ list-connections-dbus \
+ list-connections-libnm-glib \
+ get-ap-info-libnm-glib
+
+add_connection_glib_SOURCES = add-connection-glib.c
+add_connection_glib_LDADD = \
+ $(top_builddir)/libnm-util/libnm-util.la \
+ $(DBUS_LIBS) \
+ $(GLIB_LIBS)
+
+get_active_connections_SOURCES = get-active-connections.c
+get_active_connections_LDADD = \
+ $(top_builddir)/libnm-util/libnm-util.la \
+ $(DBUS_LIBS) \
+ $(GLIB_LIBS)
+
+list_connections_dbus_SOURCES = list-connections-dbus.c
+list_connections_dbus_LDADD = \
+ $(top_builddir)/libnm-util/libnm-util.la \
+ $(DBUS_LIBS) \
+ $(GLIB_LIBS)
+
+list_connections_libnm_glib_SOURCES = list-connections-libnm-glib.c
+list_connections_libnm_glib_LDADD = \
+ $(top_builddir)/libnm-util/libnm-util.la \
+ $(top_builddir)/libnm-glib/libnm-glib.la \
+ $(DBUS_LIBS) \
+ $(GLIB_LIBS)
+
+get_ap_info_libnm_glib_SOURCES = get-ap-info-libnm-glib.c
+get_ap_info_libnm_glib_LDADD = \
+ $(top_builddir)/libnm-util/libnm-util.la \
+ $(top_builddir)/libnm-glib/libnm-glib.la \
+ $(DBUS_LIBS) \
+ $(GLIB_LIBS)
+
EXTRA_DIST = \
- add-connection-glib.c
+ add-connection-glib.c \
+ get-active-connections.c \
+ list-connections-dbus.c \
+ list-connections-libnm-glib.c \
+ get-ap-info-libnm-glib.c
+
diff --git a/examples/C/add-connection-glib.c b/examples/C/add-connection-glib.c
index 0e8dc180..d650dc2f 100644
--- a/examples/C/add-connection-glib.c
+++ b/examples/C/add-connection-glib.c
@@ -13,7 +13,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
- * (C) Copyright 2010 Red Hat, Inc.
+ * (C) Copyright 2011 Red Hat, Inc.
*/
/*
@@ -38,7 +38,8 @@
#define DBUS_TYPE_G_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
#define DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, DBUS_TYPE_G_MAP_OF_VARIANT))
-void add_connection (DBusGProxy *proxy, const char *con_name)
+static void
+add_connection (DBusGProxy *proxy, const char *con_name)
{
NMConnection *connection;
NMSettingConnection *s_con;
@@ -98,7 +99,7 @@ int main (int argc, char *argv[])
/* Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h */
proxy = dbus_g_proxy_new_for_name (bus,
- NM_DBUS_SERVICE_SYSTEM_SETTINGS,
+ NM_DBUS_SERVICE,
NM_DBUS_PATH_SETTINGS,
NM_DBUS_IFACE_SETTINGS);
diff --git a/examples/C/get-active-connections.c b/examples/C/get-active-connections.c
new file mode 100644
index 00000000..36224d29
--- /dev/null
+++ b/examples/C/get-active-connections.c
@@ -0,0 +1,248 @@
+/* -*- 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.
+ *
+ * (C) Copyright 2010 Red Hat, Inc.
+ */
+
+/*
+ * The example shows how to call the D-Bus properties interface to get the
+ * list of currently active connections known to NetworkManager. It uses
+ * dbus-glib and libnm-util libraries.
+ *
+ * Compile with:
+ * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` get-active-connections.c -o get-active-connections
+ */
+
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+
+#include <nm-connection.h>
+#include <nm-setting-connection.h>
+#include <nm-setting-wired.h>
+#include <nm-setting-ip4-config.h>
+#include <NetworkManager.h>
+#include <nm-utils.h>
+
+#define DBUS_TYPE_G_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE))
+#define DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, DBUS_TYPE_G_MAP_OF_VARIANT))
+#define DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH))
+
+static void
+print_connection (DBusGConnection *bus, const char *service, const char *path)
+{
+ DBusGProxy *proxy;
+ GError *error = NULL;
+ GHashTable *hash = NULL;
+ NMConnection *connection = NULL;
+
+ /* This function asks the Settings Service that provides this network
+ * configuration for the details of that configuration.
+ */
+
+ /* Create the D-Bus proxy for the Settings Service so we can ask it for the
+ * connection configuration details.
+ */
+ proxy = dbus_g_proxy_new_for_name (bus,
+ service,
+ path,
+ NM_DBUS_IFACE_SETTINGS_CONNECTION);
+ g_assert (proxy);
+
+ /* Request the all the configuration of the Connection */
+ if (!dbus_g_proxy_call (proxy, "GetSettings", &error,
+ G_TYPE_INVALID,
+ DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, &hash,
+ G_TYPE_INVALID)) {
+ g_warning ("Failed to get active connection Connection property: %s",
+ error->message);
+ g_error_free (error);
+ goto out;
+ }
+
+ /* Using the raw configuration, create an NMConnection object for it. This
+ * step also verifies that the data we got from the settings service is
+ * valid. */
+ connection = nm_connection_new_from_hash (hash, &error);
+ if (!connection) {
+ g_warning ("Received invalid connection data: %s", error->message);
+ g_error_free (error);
+ goto out;
+ }
+
+ /* And finally dump all the configuration to stdout */
+ g_message ("%s => %s", service, path);
+ nm_connection_dump (connection);
+
+out:
+ if (connection)
+ g_object_unref (connection);
+ if (hash)
+ g_hash_table_destroy (hash);
+ g_object_unref (proxy);
+}
+
+static void
+get_active_connection_details (DBusGConnection *bus, const char *obj_path)
+{
+ DBusGProxy *props_proxy;
+ GValue path_value = { 0 };
+ GValue serv_value = { 0 };
+ GError *error = NULL;
+ const char *path = NULL, *service = NULL;
+
+ /* This function gets the backing Connection object that describes the
+ * network configuration that the ActiveConnection object is actually using.
+ * The ActiveConnection object contains the mapping between the configuration
+ * and the actual network interfaces that are using that configuration.
+ */
+
+ /* Create a D-Bus object proxy for the active connection object's properties */
+ props_proxy = dbus_g_proxy_new_for_name (bus,
+ NM_DBUS_SERVICE,
+ obj_path,
+ DBUS_INTERFACE_PROPERTIES);
+ g_assert (props_proxy);
+
+ /* Get the object path of the Connection details */
+ if (!dbus_g_proxy_call (props_proxy, "Get", &error,
+ G_TYPE_STRING, NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
+ G_TYPE_STRING, "Connection",
+ G_TYPE_INVALID,
+ G_TYPE_VALUE, &path_value,
+ G_TYPE_INVALID)) {
+ g_warning ("Failed to get active connection Connection property: %s",
+ error->message);
+ g_error_free (error);
+ goto out;
+ }
+
+ if (!G_VALUE_HOLDS (&path_value, DBUS_TYPE_G_OBJECT_PATH)) {
+ g_warning ("Unexpected type returned getting Connection property: %s",
+ G_VALUE_TYPE_NAME (&path_value));
+ goto out;
+ }
+
+ path = g_value_get_boxed (&path_value);
+ if (!path) {
+ g_warning ("Missing connection path!");
+ goto out;
+ }
+
+ /* Get the service name of the D-Bus service that provides the Connection */
+ if (!dbus_g_proxy_call (props_proxy, "Get", &error,
+ G_TYPE_STRING, NM_DBUS_INTERFACE_ACTIVE_CONNECTION,
+ G_TYPE_STRING, "ServiceName",
+ G_TYPE_INVALID,
+ G_TYPE_VALUE, &serv_value,
+ G_TYPE_INVALID)) {
+ g_warning ("Failed to get active connection ServiceName property: %s",
+ error->message);
+ g_error_free (error);
+ goto out;
+ }
+
+ if (!G_VALUE_HOLDS (&serv_value, G_TYPE_STRING)) {
+ g_warning ("Unexpected type returned getting Connection property: %s",
+ G_VALUE_TYPE_NAME (&serv_value));
+ goto out;
+ }
+
+ service = g_value_get_string (&serv_value);
+ if (!service) {
+ g_warning ("Missing connection service name!");
+ goto out;
+ }
+
+ /* Print out the actual connection details */
+ print_connection (bus, service, path);
+
+out:
+ g_value_unset (&path_value);
+ g_value_unset (&serv_value);
+ g_object_unref (props_proxy);
+}
+
+static void
+get_active_connections (DBusGConnection *bus, DBusGProxy *proxy)
+{
+ GError *error = NULL;
+ GValue value = { 0 };
+ GPtrArray *paths = NULL;
+ int i;
+
+ /* Get the ActiveConnections property from the NM Manager object */
+ if (!dbus_g_proxy_call (proxy, "Get", &error,
+ G_TYPE_STRING, NM_DBUS_INTERFACE,
+ G_TYPE_STRING, "ActiveConnections",
+ G_TYPE_INVALID,
+ G_TYPE_VALUE, &value,
+ G_TYPE_INVALID)) {
+ g_warning ("Failed to get ActiveConnections property: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ /* Make sure the ActiveConnections property is the type we expect it to be */
+ if (!G_VALUE_HOLDS (&value, DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH)) {
+ g_warning ("Unexpected type returned getting ActiveConnections: %s",
+ G_VALUE_TYPE_NAME (&value));
+ goto out;
+ }
+
+ /* Extract the active connections array from the GValue */
+ paths = g_value_get_boxed (&value);
+ if (!paths) {
+ g_warning ("Could not retrieve active connections property");
+ goto out;
+ }
+
+ /* And print out the details of each active connection */
+ for (i = 0; i < paths->len; i++)
+ get_active_connection_details (bus, g_ptr_array_index (paths, i));
+
+out:
+ g_value_unset (&value);
+}
+
+
+int main (int argc, char *argv[])
+{
+ DBusGConnection *bus;
+ DBusGProxy *props_proxy;
+
+ /* Initialize GType system */
+ g_type_init ();
+
+ /* Get system bus */
+ bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
+
+ /* Create a D-Bus proxy to get the object properties from the NM Manager
+ * object. NM_DBUS_* defines are from NetworkManager.h.
+ */
+ props_proxy = dbus_g_proxy_new_for_name (bus,
+ NM_DBUS_SERVICE,
+ NM_DBUS_PATH,
+ DBUS_INTERFACE_PROPERTIES);
+ g_assert (props_proxy);
+
+ /* Get active connections */
+ get_active_connections (bus, props_proxy);
+
+ g_object_unref (props_proxy);
+ dbus_g_connection_unref (bus);
+
+ return 0;
+}
diff --git a/examples/C/get-ap-info-libnm-glib.c b/examples/C/get-ap-info-libnm-glib.c
new file mode 100644
index 00000000..6ff310d8
--- /dev/null
+++ b/examples/C/get-ap-info-libnm-glib.c
@@ -0,0 +1,229 @@
+/*
+ * 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.
+ *
+ * (C) Copyright 2010 Red Hat, Inc.
+ */
+
+/*
+ * The example shows how to get info about APs visible by Wi-Fi devices
+ * using libnm-glib (that wraps direct D-Bus calls).
+ * The example uses dbus-glib, libnm-util and libnm-glib libraries.
+ *
+ * Compile with:
+ * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util libnm-glib` get-ap-info-libnm-glib.c -o get-ap-info-libnm-glib
+ */
+
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <nm-client.h>
+#include <nm-device.h>
+#include <nm-device-wifi.h>
+#include <nm-access-point.h>
+#include <NetworkManager.h>
+#include <nm-utils.h>
+
+/* Convert flags to string */
+static char *
+ap_wpa_rsn_flags_to_string (guint32 flags)
+{
+ char *flags_str[16]; /* Enough space for flags and terminating NULL */
+ char *ret_str;
+ int i = 0;
+
+ if (flags & NM_802_11_AP_SEC_PAIR_WEP40)
+ flags_str[i++] = g_strdup ("pair_wpe40");
+ if (flags & NM_802_11_AP_SEC_PAIR_WEP104)
+ flags_str[i++] = g_strdup ("pair_wpe104");
+ if (flags & NM_802_11_AP_SEC_PAIR_TKIP)
+ flags_str[i++] = g_strdup ("pair_tkip");
+ if (flags & NM_802_11_AP_SEC_PAIR_CCMP)
+ flags_str[i++] = g_strdup ("pair_ccmp");
+ if (flags & NM_802_11_AP_SEC_GROUP_WEP40)
+ flags_str[i++] = g_strdup ("group_wpe40");
+ if (flags & NM_802_11_AP_SEC_GROUP_WEP104)
+ flags_str[i++] = g_strdup ("group_wpe104");
+ if (flags & NM_802_11_AP_SEC_GROUP_TKIP)
+ flags_str[i++] = g_strdup ("group_tkip");
+ if (flags & NM_802_11_AP_SEC_GROUP_CCMP)
+ flags_str[i++] = g_strdup ("group_ccmp");
+ if (flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
+ flags_str[i++] = g_strdup ("psk");
+ if (flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
+ flags_str[i++] = g_strdup ("802.1X");
+
+ if (i == 0)
+ flags_str[i++] = g_strdup ("none");
+
+ flags_str[i] = NULL;
+
+ ret_str = g_strjoinv (" ", flags_str);
+
+ i = 0;
+ while (flags_str[i])
+ g_free (flags_str[i++]);
+
+ return ret_str;
+}
+
+static void
+show_access_point_info (NMAccessPoint *ap)
+{
+ guint32 flags, wpa_flags, rsn_flags, freq, bitrate;
+ guint8 strength;
+ const GByteArray *ssid;
+ const char *hwaddr;
+ NM80211Mode mode;
+ char *freq_str, *ssid_str, *bitrate_str, *strength_str, *wpa_flags_str, *rsn_flags_str;
+ GString *security_str;
+
+ /* Get AP properties */
+ flags = nm_access_point_get_flags (ap);
+ wpa_flags = nm_access_point_get_wpa_flags (ap);
+ rsn_flags = nm_access_point_get_rsn_flags (ap);
+ ssid = nm_access_point_get_ssid (ap);
+ hwaddr = nm_access_point_get_hw_address (ap);
+ freq = nm_access_point_get_frequency (ap);
+ mode = nm_access_point_get_mode (ap);
+ bitrate = nm_access_point_get_max_bitrate (ap);
+ strength = nm_access_point_get_strength (ap);
+
+ /* Convert to strings */
+ ssid_str = nm_utils_ssid_to_utf8 ((const char *) ssid->data, ssid->len);
+ freq_str = g_strdup_printf ("%u MHz", freq);
+ bitrate_str = g_strdup_printf ("%u MB/s", bitrate/1000);
+ strength_str = g_strdup_printf ("%u", strength);
+ wpa_flags_str = ap_wpa_rsn_flags_to_string (wpa_flags);
+ rsn_flags_str = ap_wpa_rsn_flags_to_string (rsn_flags);
+
+ security_str = g_string_new (NULL);
+ if ( !(flags & NM_802_11_AP_FLAGS_PRIVACY)
+ && (wpa_flags != NM_802_11_AP_SEC_NONE)
+ && (rsn_flags != NM_802_11_AP_SEC_NONE))
+ g_string_append (security_str, "Encrypted: ");
+
+ if ( (flags & NM_802_11_AP_FLAGS_PRIVACY)
+ && (wpa_flags == NM_802_11_AP_SEC_NONE)
+ && (rsn_flags == NM_802_11_AP_SEC_NONE))
+ g_string_append (security_str, "WEP ");
+ if (wpa_flags != NM_802_11_AP_SEC_NONE)
+ g_string_append (security_str, "WPA ");
+ if (rsn_flags != NM_802_11_AP_SEC_NONE)
+ g_string_append (security_str, "WPA2 ");
+ if ( (wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
+ || (rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_802_1X))
+ g_string_append (security_str, "Enterprise ");
+
+ if (security_str->len > 0)
+ g_string_truncate (security_str, security_str->len-1); /* Chop off last space */
+
+ printf ("SSID: %s\n", ssid_str);
+ printf ("BSSID: %s\n", hwaddr);
+ printf ("Mode: %s\n", mode == NM_802_11_MODE_ADHOC ? "Ad-Hoc" : mode == NM_802_11_MODE_INFRA ? "Infrastructure" : "Unknown");
+ printf ("Freq: %s\n", freq_str);
+ printf ("Bitrate: %s\n", bitrate_str);
+ printf ("Strength: %s\n", strength_str);
+ printf ("Security: %s\n", security_str->str);
+ printf ("WPA flags: %s\n", wpa_flags_str);
+ printf ("RSN flags: %s\n", rsn_flags_str);
+ printf ("D-Bus path: %s\n\n", nm_object_get_path (NM_OBJECT (ap)));
+
+ g_free (ssid_str);
+ g_free (freq_str);
+ g_free (bitrate_str);
+ g_free (strength_str);
+ g_free (wpa_flags_str);
+ g_free (rsn_flags_str);
+ g_string_free (security_str, TRUE);
+}
+
+static void
+show_wifi_device_info (NMDevice *device)
+{
+ NMAccessPoint *active_ap = NULL;
+ const GPtrArray *aps;
+ const char *iface;
+ const char *driver;
+ guint32 speed;
+ const GByteArray *active_ssid;
+ char *active_ssid_str = NULL;
+ int i;
+
+ /* Get active AP */
+ if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) {
+ if ((active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)))) {
+ active_ssid = nm_access_point_get_ssid (active_ap);
+ active_ssid_str = nm_utils_ssid_to_utf8 ((const char *) active_ssid->data, active_ssid->len);
+ }
+ }
+
+ iface = nm_device_get_iface (device);
+ driver = nm_device_get_driver (device);
+ speed = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device));
+ speed /= 1000;
+
+ printf ("Device: %s ---- Driver: %s ---- Speed: %d MB/s ---- Active AP: %s\n",
+ iface, driver, speed, active_ssid_str ? active_ssid_str : "none");
+ printf ("=================================================================================\n");
+ g_free (active_ssid_str);
+
+ /* Get all APs of the Wi-Fi device */
+ aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device));
+
+ /* Print AP details */
+ for (i = 0; aps && (i < aps->len); i++) {
+ NMAccessPoint *ap = g_ptr_array_index (aps, i);
+ show_access_point_info (ap);
+ }
+}
+
+int main (int argc, char *argv[])
+{
+ DBusGConnection *bus;
+ NMClient *client;
+ const GPtrArray *devices;
+ int i;
+
+ /* Initialize GType system */
+ g_type_init ();
+
+ /* Get system bus */
+ bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
+
+ /* Get NMClient object */
+ client = nm_client_new ();
+ if (!client) {
+ dbus_g_connection_unref (bus);
+ g_message ("Error: Could not create NMClient.");
+ return EXIT_FAILURE;
+ }
+
+ /* Get all devices managed by NetworkManager */
+ devices = nm_client_get_devices (client);
+
+ /* Go through the array and process Wi-Fi devices */
+ for (i = 0; devices && (i < devices->len); i++) {
+ NMDevice *device = g_ptr_array_index (devices, i);
+ if (NM_IS_DEVICE_WIFI (device))
+ show_wifi_device_info (device);
+ }
+
+ g_object_unref (client);
+ dbus_g_connection_unref (bus);
+
+ return EXIT_SUCCESS;
+}
diff --git a/examples/C/list-connections-dbus.c b/examples/C/list-connections-dbus.c
new file mode 100644
index 00000000..2cb584b5
--- /dev/null
+++ b/examples/C/list-connections-dbus.c
@@ -0,0 +1,83 @@
+/*
+ * 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.
+ *
+ * (C) Copyright 2011 Red Hat, Inc.
+ */
+
+/*
+ * The example shows how to list connections from System Settings service using direct
+ * D-Bus call of ListConnections method.
+ * The example uses dbus-glib, libnm-util libraries.
+ *
+ * Compile with:
+ * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util` list-connections-dbus.c -o list-connections-dbus
+ */
+
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+#include <stdio.h>
+
+#include <NetworkManager.h>
+#include <nm-utils.h>
+
+#define DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH))
+
+static void
+list_connections (DBusGProxy *proxy)
+{
+ int i;
+ GError *error = NULL;
+ GPtrArray *con_array;
+
+ /* Call ListConnections D-Bus method */
+ dbus_g_proxy_call (proxy, "ListConnections", &error,
+ /* No input arguments */
+ G_TYPE_INVALID,
+ DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH, &con_array, /* Return values */
+ G_TYPE_INVALID);
+
+ for (i = 0; con_array && i < con_array->len; i++) {
+ char *connection_path = g_ptr_array_index (con_array, i);
+ printf ("%s\n", connection_path);
+ g_free (connection_path);
+ }
+ g_ptr_array_free (con_array, TRUE);
+}
+
+int main (int argc, char *argv[])
+{
+ DBusGConnection *bus;
+ DBusGProxy *proxy;
+
+ /* Initialize GType system */
+ g_type_init ();
+
+ /* Get system bus */
+ bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
+
+ /* Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h */
+ proxy = dbus_g_proxy_new_for_name (bus,
+ NM_DBUS_SERVICE,
+ NM_DBUS_PATH_SETTINGS,
+ NM_DBUS_IFACE_SETTINGS);
+
+ /* List connections of system settings service */
+ list_connections (proxy);
+
+ g_object_unref (proxy);
+ dbus_g_connection_unref (bus);
+
+ return 0;
+}
diff --git a/examples/C/list-connections-libnm-glib.c b/examples/C/list-connections-libnm-glib.c
new file mode 100644
index 00000000..edb5ccc3
--- /dev/null
+++ b/examples/C/list-connections-libnm-glib.c
@@ -0,0 +1,176 @@
+/*
+ * 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.
+ *
+ * (C) Copyright 2011 Red Hat, Inc.
+ */
+
+/*
+ * The example shows how to list connections from System Settings service using libnm-glib
+ * (that wraps direct D-Bus calls).
+ * The example uses dbus-glib, libnm-util and libnm-glib libraries.
+ *
+ * Compile with:
+ * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm-util libnm-glib` list-connections-libnm-glib.c -o list-connections-libnm-glib
+ */
+
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+
+#include <nm-connection.h>
+#include <nm-setting-connection.h>
+#include <NetworkManager.h>
+#include <nm-utils.h>
+#include <nm-remote-settings.h>
+
+
+/* Global variables */
+GMainLoop *loop = NULL; /* Main loop variable - needed for waiting for signal */
+int result = EXIT_SUCCESS;
+
+static void
+signal_handler (int signo)
+{
+ if (signo == SIGINT || signo == SIGTERM) {
+ g_message ("Caught signal %d, shutting down...", signo);
+ g_main_loop_quit (loop);
+ }
+}
+
+static void
+setup_signals (void)
+{
+ struct sigaction action;
+ sigset_t mask;
+
+ sigemptyset (&mask);
+ action.sa_handler = signal_handler;
+ action.sa_mask = mask;
+ action.sa_flags = 0;
+ sigaction (SIGTERM, &action, NULL);
+ sigaction (SIGINT, &action, NULL);
+}
+
+/* Print details of connection */
+static void
+show_connection (NMConnection *data, gpointer user_data)
+{
+ NMConnection *connection = (NMConnection *) data;
+ NMSettingConnection *s_con;
+ guint64 timestamp;
+ char *timestamp_str;
+ char timestamp_real_str[64];
+ const char *val1, *val2, *val3, *val4, *val5;
+
+ s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
+ if (s_con) {
+ /* Get various info from NMSettingConnection and show it */
+ timestamp = nm_setting_connection_get_timestamp (s_con);
+ timestamp_str = g_strdup_printf ("%" G_GUINT64_FORMAT, timestamp);
+ strftime (timestamp_real_str, sizeof (timestamp_real_str), "%c", localtime ((time_t *) &timestamp));
+
+ val1 = nm_setting_connection_get_id (s_con);
+ val2 = nm_setting_connection_get_uuid (s_con);
+ val3 = nm_setting_connection_get_connection_type (s_con);
+ val4 = nm_connection_get_path (connection);
+ val5 = timestamp ? timestamp_real_str : "never";
+
+ printf ("%-25s | %s | %-15s | %-43s | %s\n", val1, val2, val3, val4, val5);
+
+ g_free (timestamp_str);
+ }
+}
+
+/* This callback is called when connections from the settings service are ready.
+ * Now the connections can be listed.
+ */
+static void
+get_connections_cb (NMRemoteSettings *settings, gpointer user_data)
+{
+ GSList *connections;
+
+ connections = nm_remote_settings_list_connections (settings);
+
+ printf ("Connections:\n===================\n");
+
+ g_slist_foreach (connections, (GFunc) show_connection, NULL);
+
+ g_slist_free (connections);
+ g_object_unref (settings);
+
+ /* We are done, exit main loop */
+ g_main_loop_quit (loop);
+}
+
+/* Get system settings and then connect to connections-read signal */
+static gboolean
+list_connections (gpointer data)
+{
+ DBusGConnection *bus = (DBusGConnection *) data;
+ NMRemoteSettings *settings;
+ gboolean settings_running;
+
+ /* Get system settings */
+ if (!(settings = nm_remote_settings_new (bus))) {
+ g_message ("Error: Could not get system settings.");
+ result = EXIT_FAILURE;
+ g_main_loop_quit (loop);
+ return FALSE;
+ }
+
+ /* Find out whether setting service is running */
+ g_object_get (settings, NM_REMOTE_SETTINGS_SERVICE_RUNNING, &settings_running, NULL);
+
+ if (!settings_running) {
+ g_message ("Error: Can't obtain connections: settings service is not running.");
+ result = EXIT_FAILURE;
+ g_main_loop_quit (loop);
+ return FALSE;
+ }
+
+ /* Connect to signal "connections-read" - emitted when connections are fetched and ready */
+ g_signal_connect (settings, NM_REMOTE_SETTINGS_CONNECTIONS_READ,
+ G_CALLBACK (get_connections_cb), NULL);
+
+ return FALSE;
+}
+
+int main (int argc, char *argv[])
+{
+ DBusGConnection *bus;
+
+ /* Initialize GType system */
+ g_type_init ();
+
+ /* Get system bus */
+ bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
+
+ /* Run list_connections from main loop, because we need to wait for "connections-read"
+ * signal to have connections ready. The execution will be finished in get_connections_cb()
+ * callback on the signal.
+ */
+ g_idle_add (list_connections, bus);
+
+ loop = g_main_loop_new (NULL, FALSE); /* Create main loop */
+ setup_signals (); /* Setup UNIX signals */
+ g_main_loop_run (loop); /* Run main loop */
+
+ g_main_loop_unref (loop);
+ dbus_g_connection_unref (bus);
+
+ return result;
+}