diff options
author | Dan Winship <danw@gnome.org> | 2014-09-04 18:20:33 -0400 |
---|---|---|
committer | Dan Winship <danw@gnome.org> | 2014-09-04 18:20:33 -0400 |
commit | 00695f1939fdfe3a25d23b3022ae9006e63429af (patch) | |
tree | 08d2a70319396fe04014e23b0ae70d3255e8c8a1 | |
parent | 4e530adf60d65e76252ad9e32ee1788c028abb27 (diff) | |
parent | 9e5ddb58305c3f0b3041606e2a790b3cc75c92c4 (diff) | |
download | NetworkManager-00695f1939fdfe3a25d23b3022ae9006e63429af.tar.gz |
Port non-libnm*-based code to GDBus (bgo #622927)
18 files changed, 761 insertions, 986 deletions
diff --git a/.gitignore b/.gitignore index bd177a1f35..fb475a2b72 100644 --- a/.gitignore +++ b/.gitignore @@ -116,15 +116,14 @@ valgrind-*.log /docs/libnm/libnm.types /docs/libnm/version.xml -/examples/C/glib/add-connection-dbus-glib +/examples/C/glib/add-connection-gdbus /examples/C/glib/add-connection-libnm -/examples/C/glib/get-active-connections-dbus-glib +/examples/C/glib/get-active-connections-gdbus /examples/C/glib/get-ap-info-libnm -/examples/C/glib/list-connections-dbus-glib +/examples/C/glib/list-connections-gdbus /examples/C/glib/list-connections-libnm -/examples/C/glib/monitor-nm-running-GDBus -/examples/C/glib/monitor-nm-running-dbus-glib -/examples/C/glib/monitor-nm-state-GDBus +/examples/C/glib/monitor-nm-running-gdbus +/examples/C/glib/monitor-nm-state-gdbus /examples/C/qt/add-connection-wired /examples/C/qt/list-connections /examples/C/qt/change-ipv4-addresses diff --git a/examples/C/glib/Makefile.am b/examples/C/glib/Makefile.am index ffbc98783d..c3e9be0660 100644 --- a/examples/C/glib/Makefile.am +++ b/examples/C/glib/Makefile.am @@ -8,21 +8,19 @@ AM_CPPFLAGS = \ $(GLIB_CFLAGS) noinst_PROGRAMS = \ - add-connection-dbus-glib \ + add-connection-gdbus \ add-connection-libnm \ - get-active-connections-dbus-glib \ - list-connections-dbus-glib \ - list-connections-libnm \ + get-active-connections-gdbus \ get-ap-info-libnm \ - monitor-nm-running-dbus-glib \ - monitor-nm-running-GDBus \ - monitor-nm-state-GDBus + list-connections-gdbus \ + list-connections-libnm \ + monitor-nm-running-gdbus \ + monitor-nm-state-gdbus -add_connection_dbus_glib_SOURCES = add-connection-dbus-glib.c -add_connection_dbus_glib_LDADD = \ - $(top_builddir)/libnm/libnm.la \ - $(DBUS_LIBS) \ - $(GLIB_LIBS) +add_connection_gdbus_SOURCES = add-connection-gdbus.c +add_connection_gdbus_LDADD = \ + $(GLIB_LIBS) \ + $(UUID_LIBS) add_connection_libnm_SOURCES = add-connection-libnm.c add_connection_libnm_LDADD = \ @@ -30,51 +28,30 @@ add_connection_libnm_LDADD = \ $(DBUS_LIBS) \ $(GLIB_LIBS) -get_active_connections_dbus_glib_SOURCES = get-active-connections-dbus-glib.c -get_active_connections_dbus_glib_LDADD = \ - $(top_builddir)/libnm/libnm.la \ - $(DBUS_LIBS) \ +get_active_connections_gdbus_SOURCES = get-active-connections-gdbus.c +get_active_connections_gdbus_LDADD = \ $(GLIB_LIBS) -list_connections_dbus_glib_SOURCES = list-connections-dbus-glib.c -list_connections_dbus_glib_LDADD = \ +get_ap_info_libnm_SOURCES = get-ap-info-libnm.c +get_ap_info_libnm_LDADD = \ $(top_builddir)/libnm/libnm.la \ $(DBUS_LIBS) \ $(GLIB_LIBS) -list_connections_libnm_SOURCES = list-connections-libnm.c -list_connections_libnm_LDADD = \ - $(top_builddir)/libnm/libnm.la \ - $(DBUS_LIBS) \ +list_connections_gdbus_SOURCES = list-connections-gdbus.c +list_connections_gdbus_LDADD = \ $(GLIB_LIBS) -get_ap_info_libnm_SOURCES = get-ap-info-libnm.c -get_ap_info_libnm_LDADD = \ +list_connections_libnm_SOURCES = list-connections-libnm.c +list_connections_libnm_LDADD = \ $(top_builddir)/libnm/libnm.la \ $(DBUS_LIBS) \ $(GLIB_LIBS) -monitor_nm_running_dbus_glib_SOURCES = monitor-nm-running-dbus-glib.c -monitor_nm_running_dbus_glib_LDADD = \ - $(DBUS_LIBS) \ - $(GLIB_LIBS) - -monitor_nm_running_GDBus_SOURCES = monitor-nm-running-GDBus.c -monitor_nm_running_GDBus_LDADD = \ +monitor_nm_running_gdbus_SOURCES = monitor-nm-running-gdbus.c +monitor_nm_running_gdbus_LDADD = \ $(GLIB_LIBS) -monitor_nm_state_GDBus_SOURCES = monitor-nm-state-GDBus.c -monitor_nm_state_GDBus_LDADD = \ +monitor_nm_state_gdbus_SOURCES = monitor-nm-state-gdbus.c +monitor_nm_state_gdbus_LDADD = \ $(GLIB_LIBS) - -EXTRA_DIST = \ - add-connection-dbus-glib.c \ - add-connection-libnm.c \ - get-active-connections-dbus-glib.c \ - list-connections-dbus-glib.c \ - list-connections-libnm.c \ - get-ap-info-libnm.c \ - monitor-nm-running-dbus-glib.c \ - monitor-nm-running-GDBus.c \ - monitor-nm-state-GDBus.c - diff --git a/examples/C/glib/add-connection-dbus-glib.c b/examples/C/glib/add-connection-dbus-glib.c deleted file mode 100644 index d4ac7be096..0000000000 --- a/examples/C/glib/add-connection-dbus-glib.c +++ /dev/null @@ -1,121 +0,0 @@ -/* -*- 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 2011 Red Hat, Inc. - */ - -/* - * The example shows how to call AddConnection() D-Bus method to add - * a connection to system settings service. It uses dbus-glib and libnm - * libraries. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm` add-connection-glib.c -o add-connection-glib - */ - -#include <glib.h> -#include <dbus/dbus-glib.h> - -#include <NetworkManager.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)) - -static void -add_connection (DBusGProxy *proxy, const char *con_name) -{ - NMConnection *connection; - NMSettingConnection *s_con; - NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - char *uuid, *new_con_path = NULL; - GHashTable *hash; - GError *error = NULL; - - /* Create a new connection object */ - connection = (NMConnection *) 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_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NULL); - nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - - hash = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL); - - /* Call AddConnection with the hash as argument */ - if (!dbus_g_proxy_call (proxy, "AddConnection", &error, - DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash, - G_TYPE_INVALID, - DBUS_TYPE_G_OBJECT_PATH, &new_con_path, - G_TYPE_INVALID)) { - g_print ("Error adding connection: %s %s", - dbus_g_error_get_name (error), - error->message); - g_clear_error (&error); - } else { - g_print ("Added: %s\n", new_con_path); - g_free (new_con_path); - } - - g_hash_table_destroy (hash); - g_object_unref (connection); -} - - -int main (int argc, char *argv[]) -{ - DBusGConnection *bus; - DBusGProxy *proxy; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* Get system bus */ - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); - - /* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */ - proxy = dbus_g_proxy_new_for_name (bus, - NM_DBUS_SERVICE, - NM_DBUS_PATH_SETTINGS, - NM_DBUS_INTERFACE_SETTINGS); - - /* Add a connection */ - add_connection (proxy, "__Test connection__"); - - g_object_unref (proxy); - dbus_g_connection_unref (bus); - - return 0; -} diff --git a/examples/C/glib/add-connection-gdbus.c b/examples/C/glib/add-connection-gdbus.c new file mode 100644 index 0000000000..a7874fc51f --- /dev/null +++ b/examples/C/glib/add-connection-gdbus.c @@ -0,0 +1,148 @@ +/* -*- 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, 2014 Red Hat, Inc. + */ + +/* + * The example shows how to call the AddConnection() D-Bus method to add a + * connection to the system settings service. It uses GDBus, plus a few defines + * from the NetworkManager headers. Contrast add-connection-libnm, which is + * higher level because it uses libnm. + * + * Compile with: + * gcc -Wall `pkg-config --cflags libnm` `pkg-config --cflags --libs gio-2.0` -luuid add-connection-gdbus.c -o add-connection-gdbus + */ + +#include <gio/gio.h> +#include <uuid/uuid.h> +#include <NetworkManager.h> + +/* copied from libnm-core/nm-utils.c */ +char * +nm_utils_uuid_generate (void) +{ + uuid_t uuid; + char *buf; + + buf = g_malloc0 (37); + uuid_generate_random (uuid); + uuid_unparse_lower (uuid, &buf[0]); + return buf; +} + +static void +add_connection (GDBusProxy *proxy, const char *con_name) +{ + GVariantBuilder connection_builder; + GVariantBuilder setting_builder; + char *uuid; + const char *new_con_path; + GVariant *ret; + GError *error = NULL; + + /* Initialize connection GVariantBuilder */ + g_variant_builder_init (&connection_builder, G_VARIANT_TYPE ("a{sa{sv}}")); + + /* Build up the 'connection' Setting */ + g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}")); + + uuid = nm_utils_uuid_generate (); + g_variant_builder_add (&setting_builder, "{sv}", + NM_SETTING_CONNECTION_UUID, + g_variant_new_string (uuid)); + g_free (uuid); + + g_variant_builder_add (&setting_builder, "{sv}", + NM_SETTING_CONNECTION_ID, + g_variant_new_string (con_name)); + g_variant_builder_add (&setting_builder, "{sv}", + NM_SETTING_CONNECTION_TYPE, + g_variant_new_string (NM_SETTING_WIRED_SETTING_NAME)); + + g_variant_builder_add (&connection_builder, "{sa{sv}}", + NM_SETTING_CONNECTION_SETTING_NAME, + &setting_builder); + + /* Add the (empty) 'wired' Setting */ + g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&connection_builder, "{sa{sv}}", + NM_SETTING_WIRED_SETTING_NAME, + &setting_builder); + + /* Build up the 'ipv4' Setting */ + g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&setting_builder, "{sv}", + NM_SETTING_IP4_CONFIG_METHOD, + g_variant_new_string (NM_SETTING_IP4_CONFIG_METHOD_AUTO)); + g_variant_builder_add (&connection_builder, "{sa{sv}}", + NM_SETTING_IP4_CONFIG_SETTING_NAME, + &setting_builder); + + /* Call AddConnection with the connection dictionary as argument. + * (g_variant_new() will consume the floating GVariant returned from + * &connection_builder, and g_dbus_proxy_call_sync() will consume the + * floating variant returned from g_variant_new(), so no cleanup is needed. + */ + ret = g_dbus_proxy_call_sync (proxy, + "AddConnection", + g_variant_new ("(a{sa{sv}})", &connection_builder), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (ret) { + g_variant_get (ret, "(&o)", &new_con_path); + g_print ("Added: %s\n", new_con_path); + g_variant_unref (ret); + } else { + g_dbus_error_strip_remote_error (error); + g_print ("Error adding connection: %s\n", error->message); + g_clear_error (&error); + } +} + +int +main (int argc, char *argv[]) +{ + GDBusProxy *proxy; + GError *error = NULL; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + /* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */ + proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + NM_DBUS_PATH_SETTINGS, + NM_DBUS_INTERFACE_SETTINGS, + NULL, &error); + if (!proxy) { + g_dbus_error_strip_remote_error (error); + g_print ("Could not create NetworkManager D-Bus proxy: %s\n", error->message); + g_error_free (error); + return 1; + } + + /* Add a connection */ + add_connection (proxy, "__Test connection__"); + + g_object_unref (proxy); + + return 0; +} diff --git a/examples/C/glib/add-connection-libnm.c b/examples/C/glib/add-connection-libnm.c index f390bfa159..2b0143d35a 100644 --- a/examples/C/glib/add-connection-libnm.c +++ b/examples/C/glib/add-connection-libnm.c @@ -14,14 +14,14 @@ * 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. + * Copyright 2011 Red Hat, Inc. */ /* - * The example shows how to add a new connection using libnm. - * Contrast this example with add-connection-dbus-glib.c, which is a bit lower - * level and talks directly to NM using dbus-glib. This example is simpler - * because libnm handles much of the low-level stuff for you. + * 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 @@ -98,7 +98,8 @@ add_connection (NMRemoteSettings *settings, GMainLoop *loop, const char *con_nam } -int main (int argc, char *argv[]) +int +main (int argc, char *argv[]) { NMRemoteSettings *settings; GMainLoop *loop; diff --git a/examples/C/glib/get-active-connections-dbus-glib.c b/examples/C/glib/get-active-connections-dbus-glib.c deleted file mode 100644 index e7aaff7bb3..0000000000 --- a/examples/C/glib/get-active-connections-dbus-glib.c +++ /dev/null @@ -1,218 +0,0 @@ -/* -*- 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 -2014 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 libraries. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1 libnm` get-active-connections-dbus-glib.c -o get-active-connections-dbus-glib - */ - -#include <stdio.h> -#include <glib.h> -#include <dbus/dbus-glib.h> - -#include <NetworkManager.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 *path) -{ - DBusGProxy *proxy; - GError *error = NULL; - GHashTable *hash = NULL; - NMConnection *connection = NULL; - - /* This function asks NetworkManager for the details of the connection */ - - /* Create the D-Bus proxy so we can ask it for the connection configuration details. */ - proxy = dbus_g_proxy_new_for_name (bus, - NM_DBUS_SERVICE, - path, - NM_DBUS_INTERFACE_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 NetworkManager are valid. */ - connection = nm_simple_connection_new_from_dbus (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 */ - printf ("%s <=> %s\n", nm_connection_get_id (connection), 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 = G_VALUE_INIT; - GError *error = NULL; - const char *path = 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; - } - - /* Print out the actual connection details */ - print_connection (bus, path); - -out: - g_value_unset (&path_value); - g_object_unref (props_proxy); -} - -static void -get_active_connections (DBusGConnection *bus, DBusGProxy *proxy) -{ - GError *error = NULL; - GValue value = G_VALUE_INIT; - GPtrArray *paths = NULL; - const char *a_path; - 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 for each active connection */ - for (i = 0; i < paths->len; i++) { - a_path = g_ptr_array_index (paths, i); - printf ("Active connection path: %s\n", a_path); - get_active_connection_details (bus, a_path); - } - -out: - g_value_unset (&value); -} - - -int main (int argc, char *argv[]) -{ - DBusGConnection *bus; - DBusGProxy *props_proxy; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* 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 nm-dbus-interface.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/glib/get-active-connections-gdbus.c b/examples/C/glib/get-active-connections-gdbus.c new file mode 100644 index 0000000000..6a19bf895e --- /dev/null +++ b/examples/C/glib/get-active-connections-gdbus.c @@ -0,0 +1,272 @@ +/* -*- 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 -2014 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 GDBus, plus + * a few defines from the NetworkManager headers. + * + * Compile with: + * gcc -Wall `pkg-config --cflags libnm` `pkg-config --cflags --libs gio-2.0` get-active-connections-gdbus.c -o get-active-connections-gdbus + */ + +#include <string.h> +#include <gio/gio.h> +#include <NetworkManager.h> + +static void +print_setting (const char *setting_name, GVariant *setting) +{ + GVariantIter iter; + const char *property_name; + GVariant *value; + char *printed_value; + + g_print (" %s:\n", setting_name); + g_variant_iter_init (&iter, setting); + while (g_variant_iter_next (&iter, "{&sv}", &property_name, &value)) { + printed_value = g_variant_print (value, FALSE); + if (strcmp (printed_value, "[]") != 0) + g_print (" %s: %s\n", property_name, printed_value); + g_free (printed_value); + g_variant_unref (value); + } +} + +static void +print_connection (const char *path) +{ + GDBusProxy *proxy; + GError *error = NULL; + GVariant *ret, *connection = NULL, *s_con = NULL; + const char *id, *type; + gboolean found; + GVariantIter iter; + const char *setting_name; + GVariant *setting; + + /* This function asks NetworkManager for the details of the connection */ + + /* Create the D-Bus proxy so we can ask it for the connection configuration details. */ + proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + path, + NM_DBUS_INTERFACE_SETTINGS_CONNECTION, + NULL, NULL); + g_assert (proxy); + + /* Request the all the configuration of the Connection */ + ret = g_dbus_proxy_call_sync (proxy, + "GetSettings", + NULL, + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (!ret) { + g_dbus_error_strip_remote_error (error); + g_warning ("Failed to get connection settings: %s\n", error->message); + g_error_free (error); + goto out; + } + + g_variant_get (ret, "(@a{sa{sv}})", &connection); + + s_con = g_variant_lookup_value (connection, NM_SETTING_CONNECTION_SETTING_NAME, NULL); + g_assert (s_con != NULL); + found = g_variant_lookup (s_con, NM_SETTING_CONNECTION_ID, "&s", &id); + g_assert (found); + found = g_variant_lookup (s_con, NM_SETTING_CONNECTION_TYPE, "&s", &type); + g_assert (found); + + /* Dump the configuration to stdout */ + g_print ("%s <=> %s\n", id, path); + + /* Connection setting first */ + print_setting (NM_SETTING_CONNECTION_SETTING_NAME, s_con); + + /* Then the type-specific setting */ + setting = g_variant_lookup_value (connection, type, NULL); + if (setting) { + print_setting (type, setting); + g_variant_unref (setting); + } + + g_variant_iter_init (&iter, connection); + while (g_variant_iter_next (&iter, "{&s@a{sv}}", &setting_name, &setting)) { + if ( strcmp (setting_name, NM_SETTING_CONNECTION_SETTING_NAME) != 0 + && strcmp (setting_name, type) != 0) + print_setting (setting_name, setting); + g_variant_unref (setting); + } + g_print ("\n"); + +out: + if (s_con) + g_variant_unref (s_con); + if (connection) + g_variant_unref (connection); + if (ret) + g_variant_unref (ret); + g_object_unref (proxy); +} + +static void +get_active_connection_details (const char *obj_path) +{ + GDBusProxy *props_proxy; + GVariant *ret = NULL, *path_value = NULL; + const char *path = NULL; + GError *error = 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 = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + obj_path, + "org.freedesktop.DBus.Properties", + NULL, NULL); + g_assert (props_proxy); + + /* Get the object path of the Connection details */ + ret = g_dbus_proxy_call_sync (props_proxy, + "Get", + g_variant_new ("(ss)", + NM_DBUS_INTERFACE_ACTIVE_CONNECTION, + "Connection"), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (!ret) { + g_dbus_error_strip_remote_error (error); + g_warning ("Failed to get active connection Connection property: %s\n", + error->message); + g_error_free (error); + goto out; + } + + g_variant_get (ret, "(v)", &path_value); + if (!g_variant_is_of_type (path_value, G_VARIANT_TYPE_OBJECT_PATH)) { + g_warning ("Unexpected type returned getting Connection property: %s", + g_variant_get_type_string (path_value)); + goto out; + } + + path = g_variant_get_string (path_value, NULL); + + /* Print out the actual connection details */ + print_connection (path); + +out: + if (path_value) + g_variant_unref (path_value); + if (ret) + g_variant_unref (ret); + g_object_unref (props_proxy); +} + +static void +get_active_connections (GDBusProxy *proxy) +{ + GError *error = NULL; + GVariant *ret = NULL, *value = NULL; + char **paths; + int i; + + /* Get the ActiveConnections property from the NM Manager object */ + ret = g_dbus_proxy_call_sync (proxy, + "Get", + g_variant_new ("(ss)", + NM_DBUS_INTERFACE, + "ActiveConnections"), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (!ret) { + g_dbus_error_strip_remote_error (error); + g_warning ("Failed to get ActiveConnections property: %s\n", error->message); + g_error_free (error); + return; + } + + g_variant_get (ret, "(v)", &value); + + /* Make sure the ActiveConnections property is the type we expect it to be */ + if (!g_variant_is_of_type (value, G_VARIANT_TYPE ("ao"))) { + g_warning ("Unexpected type returned getting ActiveConnections: %s", + g_variant_get_type_string (value)); + goto out; + } + + /* Extract the active connections array from the GValue */ + paths = g_variant_dup_objv (value, NULL); + if (!paths) { + g_warning ("Could not retrieve active connections property"); + goto out; + } + + /* And print out the details for each active connection */ + for (i = 0; paths[i]; i++) { + g_print ("Active connection path: %s\n", paths[i]); + get_active_connection_details (paths[i]); + } + g_strfreev (paths); + +out: + if (value) + g_variant_unref (value); + if (ret) + g_variant_unref (ret); +} + + +int +main (int argc, char *argv[]) +{ + GDBusProxy *props_proxy; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + /* Create a D-Bus proxy to get the object properties from the NM Manager + * object. NM_DBUS_* defines are from nm-dbus-interface.h. + */ + props_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + NM_DBUS_PATH, + "org.freedesktop.DBus.Properties", + NULL, NULL); + g_assert (props_proxy); + + /* Get active connections */ + get_active_connections (props_proxy); + + g_object_unref (props_proxy); + + return 0; +} diff --git a/examples/C/glib/get-ap-info-libnm.c b/examples/C/glib/get-ap-info-libnm.c index 044cec9c04..efb70bf4b8 100644 --- a/examples/C/glib/get-ap-info-libnm.c +++ b/examples/C/glib/get-ap-info-libnm.c @@ -1,3 +1,4 @@ +/* -*- 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 @@ -13,19 +14,18 @@ * 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. + * Copyright 2010 Red Hat, Inc. */ /* - * The example shows how to get info about APs visible by Wi-Fi devices - * using libnm (that wraps direct D-Bus calls). + * The example shows how to get info about APs visible by Wi-Fi devices using + * libnm. * * Compile with: * gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` get-ap-info-libnm.c -o get-ap-info-libnm */ #include <glib.h> -#include <dbus/dbus-glib.h> #include <stdio.h> #include <stdlib.h> @@ -188,7 +188,8 @@ show_wifi_device_info (NMDevice *device) } } -int main (int argc, char *argv[]) +int +main (int argc, char *argv[]) { NMClient *client; const GPtrArray *devices; diff --git a/examples/C/glib/list-connections-dbus-glib.c b/examples/C/glib/list-connections-dbus-glib.c deleted file mode 100644 index 608c666603..0000000000 --- a/examples/C/glib/list-connections-dbus-glib.c +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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 libraries and libnm headers. - * - * Compile with: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1` `pkg-config --cflags libnm` list-connections-dbus.c -o list-connections-dbus - */ - -#include <glib.h> -#include <dbus/dbus-glib.h> -#include <stdio.h> - -#include <NetworkManager.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; - gboolean success; - - /* Call ListConnections D-Bus method */ - success = 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); - if (!success) { - printf ("ListConnections failed: %s", error->message); - g_error_free (error); - return; - } - - 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; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - /* Get system bus */ - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); - - /* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */ - proxy = dbus_g_proxy_new_for_name (bus, - NM_DBUS_SERVICE, - NM_DBUS_PATH_SETTINGS, - NM_DBUS_INTERFACE_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/glib/list-connections-gdbus.c b/examples/C/glib/list-connections-gdbus.c new file mode 100644 index 0000000000..f5922582ea --- /dev/null +++ b/examples/C/glib/list-connections-gdbus.c @@ -0,0 +1,88 @@ +/* -*- 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, 2014 Red Hat, Inc. + */ + +/* + * The example shows how to list connections from the System Settings service + * using direct D-Bus calls. The example uses GDBus, plus a few defines from + * the NetworkManager headers. Contrast list-connections-libnm, which is higher + * level because it uses libnm. + * + * Compile with: + * gcc -Wall `pkg-config --cflags libnm` `pkg-config --cflags --libs gio-2.0` list-connections-gdbus.c -o list-connections-gdbus + */ + +#include <gio/gio.h> +#include <NetworkManager.h> + +static void +list_connections (GDBusProxy *proxy) +{ + int i; + GError *error = NULL; + GVariant *ret; + char **paths; + + /* Call ListConnections D-Bus method */ + ret = g_dbus_proxy_call_sync (proxy, + "ListConnections", + NULL, + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); + if (!ret) { + g_dbus_error_strip_remote_error (error); + g_print ("ListConnections failed: %s\n", error->message); + g_error_free (error); + return; + } + + g_variant_get (ret, "(^ao)", &paths); + g_variant_unref (ret); + + for (i = 0; paths[i]; i++) + g_print ("%s\n", paths[i]); + g_strfreev (paths); +} + +int +main (int argc, char *argv[]) +{ + GDBusProxy *proxy; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + /* Initialize GType system */ + g_type_init (); +#endif + + /* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */ + proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + NM_DBUS_PATH_SETTINGS, + NM_DBUS_INTERFACE_SETTINGS, + NULL, NULL); + g_assert (proxy != NULL); + + /* List connections of system settings service */ + list_connections (proxy); + + g_object_unref (proxy); + + return 0; +} diff --git a/examples/C/glib/list-connections-libnm.c b/examples/C/glib/list-connections-libnm.c index 1827835948..bebeeb8a23 100644 --- a/examples/C/glib/list-connections-libnm.c +++ b/examples/C/glib/list-connections-libnm.c @@ -1,3 +1,4 @@ +/* -*- 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 @@ -13,12 +14,13 @@ * 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. + * Copyright 2011 Red Hat, Inc. */ /* - * The example shows how to list connections from System Settings service using libnm - * (that wraps direct D-Bus calls). + * The example shows how to list connections from the System Settings service + * using libnm. Contrast this example with list-connections-gdbus.c, which is a + * bit lower level and talks directly to NM using GDBus. * * Compile with: * gcc -Wall `pkg-config --libs --cflags glib-2.0 libnm` list-connections-libnm.c -o list-connections-libnm diff --git a/examples/C/glib/monitor-nm-running-dbus-glib.c b/examples/C/glib/monitor-nm-running-dbus-glib.c deleted file mode 100644 index 0b01a57f06..0000000000 --- a/examples/C/glib/monitor-nm-running-dbus-glib.c +++ /dev/null @@ -1,127 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */ -/* - * 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 2012 Red Hat, Inc. - */ - -/* - * This example monitors whether NM is running by checking D-Bus - * NameOwnerChanged signal. - * It uses dbus-glib library. - * - * Standalone compilation: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 dbus-glib-1` monitor-nm-running.c -o monitor-nm-running - */ - -#include <glib.h> -#include <dbus/dbus-glib.h> -#include <dbus/dbus-glib-bindings.h> -#include <string.h> - -#define NM_DBUS_SERVICE "org.freedesktop.NetworkManager" - -static void -proxy_name_owner_changed (DBusGProxy *proxy, - const char *name, - const char *old_owner, - const char *new_owner, - gpointer user_data) -{ - gboolean *nm_running = (gboolean *) user_data; - gboolean old_good = (old_owner && strlen (old_owner)); - gboolean new_good = (new_owner && strlen (new_owner)); - gboolean new_running = FALSE; - - /* We are only interested in NetworkManager */ - if (!name || strcmp (name, NM_DBUS_SERVICE) != 0) - return; - - if (!old_good && new_good) - new_running = TRUE; - else if (old_good && !new_good) - new_running = FALSE; - - *nm_running = new_running; - - g_print ("name: '%s', old_owner: '%s', new_owner: '%s'", name, old_owner, new_owner); - g_print (" => NM is %s\n", *nm_running ? "running" : "not running"); -} - - -int -main (int argc, char *argv[]) -{ - DBusGConnection *bus; - DBusGProxy *bus_proxy; - GMainLoop *loop = NULL; - GError *err = NULL; - gboolean nm_running; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - /* Initialize GType system */ - g_type_init (); -#endif - - g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n"); - g_print ("===================================================\n"); - - /* Get system bus */ - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); - - /* Create a D-Bus proxy to D-Bus daemon */ - bus_proxy = dbus_g_proxy_new_for_name (bus, - "org.freedesktop.DBus", - "/org/freedesktop/DBus", - "org.freedesktop.DBus"); - - if (!bus_proxy) { - g_message ("Error: Couldn't create D-Bus object proxy for org.freedesktop.DBus."); - dbus_g_connection_unref (bus); - return -1; - } - - /* Call NameHasOwner method to find out if NM is running. When NM runs it claims - * 'org.freedesktop.NetworkManager' service name on D-Bus */ - if (!org_freedesktop_DBus_name_has_owner (bus_proxy, NM_DBUS_SERVICE, &nm_running, &err)) { - g_message ("Error: NameHasOwner request failed: %s", - (err && err->message) ? err->message : "(unknown)"); - g_clear_error (&err); - g_object_unref (bus_proxy); - dbus_g_connection_unref (bus); - return -1; - } - g_print ("NM is %s\n", nm_running ? "running" : "not running"); - - - /* Connect to NameOwnerChanged signal to monitor NM running state */ - dbus_g_proxy_add_signal (bus_proxy, "NameOwnerChanged", - G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, - G_TYPE_INVALID); - dbus_g_proxy_connect_signal (bus_proxy, - "NameOwnerChanged", - G_CALLBACK (proxy_name_owner_changed), - &nm_running, NULL); - - loop = g_main_loop_new (NULL, FALSE); /* Create main loop */ - g_main_loop_run (loop); /* Run main loop */ - - g_object_unref (bus_proxy); - dbus_g_connection_unref (bus); - - return 0; -} - diff --git a/examples/C/glib/monitor-nm-running-GDBus.c b/examples/C/glib/monitor-nm-running-gdbus.c index 9e38d4c70d..2e1a11c0c8 100644 --- a/examples/C/glib/monitor-nm-running-GDBus.c +++ b/examples/C/glib/monitor-nm-running-gdbus.c @@ -1,5 +1,4 @@ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */ /* * 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 @@ -15,7 +14,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 2012 Red Hat, Inc. + * Copyright 2012 Red Hat, Inc. */ /* @@ -26,7 +25,7 @@ * See also http://developer.gnome.org/gio/stable/gio-Watching-Bus-Names.html * * Standalone compilation: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 gio-2.0` monitor-nm-running-GDBus.c -o monitor-nm-running-GDBus + * gcc -Wall `pkg-config --libs --cflags glib-2.0 gio-2.0` monitor-nm-running-gdbus.c -o monitor-nm-running-gdbus */ #include <gio/gio.h> diff --git a/examples/C/glib/monitor-nm-state-GDBus.c b/examples/C/glib/monitor-nm-state-gdbus.c index 5131b8c867..05596399d9 100644 --- a/examples/C/glib/monitor-nm-state-GDBus.c +++ b/examples/C/glib/monitor-nm-state-gdbus.c @@ -1,5 +1,4 @@ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* vim: set ft=c ts=4 sts=4 sw=4 noexpandtab smartindent: */ /* * 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 @@ -15,7 +14,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 2012 Red Hat, Inc. + * Copyright 2012 Red Hat, Inc. */ /* @@ -23,11 +22,11 @@ * "org.freedesktop.NetworkManager" object. * It uses GDBus and the libnm headers. - * You don't need to have NetworkManager devel package installed. You can just + * You don't need to have the NetworkManager devel packages installed. You can just * grab nm-dbus-interface.h and put it in the path. * * Standalone compilation: - * gcc -Wall `pkg-config --libs --cflags glib-2.0 gio-2.0` `pkg-config --cflags libnm` monitor-nm-state-GDBus.c -o monitor-nm-state-GDBus + * gcc -Wall `pkg-config --libs --cflags glib-2.0 gio-2.0` `pkg-config --cflags libnm` monitor-nm-state-gdbus.c -o monitor-nm-state-gdbus */ #include <gio/gio.h> @@ -112,6 +111,7 @@ main (int argc, char *argv[]) if (proxy == NULL) { + g_dbus_error_strip_remote_error (error); g_printerr ("Error creating D-Bus proxy: %s\n", error->message); g_error_free (error); return -1; diff --git a/src/dhcp-manager/Makefile.am b/src/dhcp-manager/Makefile.am index 63eac9d56f..27945f55c2 100644 --- a/src/dhcp-manager/Makefile.am +++ b/src/dhcp-manager/Makefile.am @@ -3,15 +3,15 @@ libexec_PROGRAMS = nm-dhcp-helper nm_dhcp_helper_SOURCES = nm-dhcp-helper.c nm_dhcp_helper_CPPFLAGS = \ - $(DBUS_CFLAGS) \ + $(GLIB_CFLAGS) \ -DG_LOG_DOMAIN=\""nm-dhcp-helper"\" \ -DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \ -DNMRUNDIR=\"$(nmrundir)\" -nm_dhcp_helper_LDADD = $(DBUS_LIBS) +nm_dhcp_helper_LDADD = $(GLIB_LIBS) -# FIXME: remove when dbus-glib >= 0.100 is required +# FIXME: remove when dbus-glib >= 0.100 or GDBus is required dhcp_helper_conf = nm-dhcp-helper.conf if !HAVE_DBUS_GLIB_100 diff --git a/src/dhcp-manager/nm-dhcp-helper.c b/src/dhcp-manager/nm-dhcp-helper.c index 4ddfafe173..ff591d8297 100644 --- a/src/dhcp-manager/nm-dhcp-helper.c +++ b/src/dhcp-manager/nm-dhcp-helper.c @@ -18,184 +18,37 @@ * Copyright (C) 2007 - 2013 Red Hat, Inc. */ -/* for environ */ -#define _GNU_SOURCE +#include <config.h> -#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <signal.h> -#include <config.h> - -#include <dbus/dbus.h> +#include <gio/gio.h> #define NM_DHCP_CLIENT_DBUS_IFACE "org.freedesktop.nm_dhcp_client" -/** - * _dbus_dict_open_write: - * @iter: A valid dbus message iterator - * @iter_dict: on return, a dict iterator to pass to further dict functions - * - * Start a dict in a dbus message. Should be paired with a call to - * _dbus_dict_close_write(). - * - * Returns: %TRUE on success, %FALSE on failure - */ -static dbus_bool_t -_dbus_dict_open_write (DBusMessageIter *iter, DBusMessageIter *iter_dict) -{ - if (!iter || !iter_dict) - return FALSE; - - return dbus_message_iter_open_container (iter, - DBUS_TYPE_ARRAY, - DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING - DBUS_TYPE_STRING_AS_STRING - DBUS_TYPE_VARIANT_AS_STRING - DBUS_DICT_ENTRY_END_CHAR_AS_STRING, - iter_dict); -} - -/** - * _dbus_dict_close_write: - * @iter: valid dbus message iterator, same as passed to _dbus_dict_open_write() - * @iter_dict: a dbus dict iterator returned from _dbus_dict_open_write() - * - * End a dict element in a dbus message. Should be paired with a call to - * _dbus_dict_open_write(). - * - * Returns: %TRUE on success, %FALSE on failure - */ -static dbus_bool_t -_dbus_dict_close_write (DBusMessageIter *iter, DBusMessageIter *iter_dict) -{ - if (!iter || !iter_dict) - return FALSE; - - return dbus_message_iter_close_container (iter, iter_dict); -} - -static dbus_bool_t -_dbus_add_dict_entry_start (DBusMessageIter *iter_dict, - DBusMessageIter *iter_dict_entry, - const char *key, - const int value_type) -{ - if (!dbus_message_iter_open_container (iter_dict, DBUS_TYPE_DICT_ENTRY, NULL, iter_dict_entry)) - return FALSE; - - if (!dbus_message_iter_append_basic (iter_dict_entry, DBUS_TYPE_STRING, &key)) - return FALSE; - - return TRUE; -} - - -static dbus_bool_t -_dbus_add_dict_entry_end (DBusMessageIter *iter_dict, - DBusMessageIter *iter_dict_entry, - DBusMessageIter *iter_dict_val) -{ - if (!dbus_message_iter_close_container (iter_dict_entry, iter_dict_val)) - return FALSE; - if (!dbus_message_iter_close_container (iter_dict, iter_dict_entry)) - return FALSE; - - return TRUE; -} - -static dbus_bool_t -_dbus_add_dict_entry_byte_array (DBusMessageIter *iter_dict, - const char *key, - const char *value, - const dbus_uint32_t value_len) -{ - DBusMessageIter iter_dict_entry, iter_dict_val, iter_array; - dbus_uint32_t i; - - if (!_dbus_add_dict_entry_start (iter_dict, &iter_dict_entry, key, DBUS_TYPE_ARRAY)) - return FALSE; - - if (!dbus_message_iter_open_container (&iter_dict_entry, - DBUS_TYPE_VARIANT, - DBUS_TYPE_ARRAY_AS_STRING - DBUS_TYPE_BYTE_AS_STRING, - &iter_dict_val)) - return FALSE; - - if (!dbus_message_iter_open_container (&iter_dict_val, - DBUS_TYPE_ARRAY, - DBUS_TYPE_BYTE_AS_STRING, - &iter_array)) - return FALSE; - - for (i = 0; i < value_len; i++) { - if (!dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_BYTE, &(value[i]))) - return FALSE; - } - - if (!dbus_message_iter_close_container (&iter_dict_val, &iter_array)) - return FALSE; - - if (!_dbus_add_dict_entry_end (iter_dict, &iter_dict_entry, &iter_dict_val)) - return FALSE; - - return TRUE; -} - -/** - * _dbus_dict_append_byte_array: - * @iter_dict: A valid %DBusMessageIter returned from _dbus_dict_open_write() - * @key: The key of the dict item - * @value: The byte array - * @value_len: The length of the byte array, in bytes - * - * Add a byte array entry to the dict. - * - * Returns: %TRUE on success, %FALSE on failure - * - */ -static dbus_bool_t -_dbus_dict_append_byte_array (DBusMessageIter *iter_dict, - const char *key, - const char *value, - const dbus_uint32_t value_len) -{ - if (!key) - return FALSE; - if (!value && (value_len != 0)) - return FALSE; - return _dbus_add_dict_entry_byte_array (iter_dict, key, value, value_len); -} - - static const char * ignore[] = {"PATH", "SHLVL", "_", "PWD", "dhc_dbus", NULL}; -static dbus_bool_t -build_message (DBusMessage * message) +static GVariant * +build_signal_parameters (void) { char **item; - dbus_bool_t success = FALSE; - DBusMessageIter iter, iter_dict; + GVariantBuilder builder; - dbus_message_iter_init_append (message, &iter); - if (!_dbus_dict_open_write (&iter, &iter_dict)) - goto out; + g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); /* List environment and format for dbus dict */ for (item = environ; *item; item++) { char *name, *val, **p; /* Split on the = */ - name = strdup (*item); + name = g_strdup (*item); val = strchr (name, '='); - if (!val) + if (!val || val == name) goto next; *val++ = '\0'; - if (!strlen (val)) - val = NULL; /* Ignore non-DCHP-related environment variables */ for (p = (char **) ignore; *p; p++) { @@ -206,72 +59,82 @@ build_message (DBusMessage * message) /* Value passed as a byte array rather than a string, because there are * no character encoding guarantees with DHCP, and D-Bus requires * strings to be UTF-8. + * + * Note that we can't use g_variant_new_bytestring() here, because that + * includes the trailing '\0'. (??!?) */ - if (!_dbus_dict_append_byte_array (&iter_dict, - name, - val ? val : "\0", - val ? strlen (val) : 1)) { - fprintf (stderr, "Error: failed to add item '%s' to signal\n", name); - } + g_variant_builder_add (&builder, "{sv}", + name, + g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, + val, strlen (val), 1)); next: - free (name); + g_free (name); } - if (!_dbus_dict_close_write (&iter, &iter_dict)) - goto out; - - success = TRUE; - -out: - return success; + return g_variant_new ("(a{sv})", &builder); } #if !HAVE_DBUS_GLIB_100 -static DBusConnection * -shared_connection_init (void) +/* It doesn't matter that nm-dhcp-helper doesn't use dbus-glib itself; the + * workaround code is for if the daemon is built with old dbus-glib. + */ + +static gboolean ever_acquired = FALSE; + +static void +on_name_acquired (GDBusConnection *connection, + const gchar *name, + gpointer user_data) { - DBusConnection * connection; - DBusError error; - int ret; + GMainLoop *loop = user_data; - dbus_connection_set_change_sigpipe (TRUE); + ever_acquired = TRUE; + g_main_loop_quit (loop); +} - dbus_error_init (&error); - connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); - if (dbus_error_is_set (&error)) { - fprintf (stderr, "Error: could not get the system bus. Make sure " - "the message bus daemon is running! Message: (%s) %s\n", - error.name, - error.message); - goto error; +static void +on_name_lost (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + if (ever_acquired) { + g_print ("Lost D-Bus name: exiting\n"); + exit (0); + } else { + g_printerr ("Error: Could not acquire the NM DHCP client service.\n"); + exit (1); } +} - dbus_error_init (&error); - ret = dbus_bus_request_name (connection, "org.freedesktop.nm_dhcp_client", 0, &error); - if (dbus_error_is_set (&error)) { - fprintf (stderr, "Error: Could not acquire the NM DHCP client service. " - "Message: (%s) %s\n", - error.name, - error.message); - goto error; - } +static GDBusConnection * +shared_connection_init (void) +{ + GDBusConnection *connection; + GError *error = NULL; + GMainLoop *loop; - if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { - fprintf (stderr, "Error: Could not acquire the NM DHCP client service " - "as it is already taken. Return: %d\n", - ret); - goto error; + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (!connection) { + g_dbus_error_strip_remote_error (error); + g_printerr ("Error: could not get the system bus. Make sure " + "the message bus daemon is running! Message: %s\n", + error->message); + g_error_free (&error); + return NULL; } - return connection; + loop = g_main_loop_new (NULL, FALSE); + g_bus_own_name_on_connection (connection, + "org.freedesktop.nm_dhcp_client", + 0, + on_name_acquired, + on_name_lost, + loop, NULL); + g_main_loop_run (loop); + g_main_loop_unref (loop); -error: - if (dbus_error_is_set (&error)) - dbus_error_free (&error); - if (connection) - dbus_connection_unref (connection); - return NULL; + return connection; } #endif @@ -284,7 +147,7 @@ fatal_error (void) if (pid_str) pid = strtol (pid_str, NULL, 10); if (pid) { - fprintf (stderr, "Fatal error occured, killing dhclient instance with pid %d.\n", pid); + g_printerr ("Fatal error occured, killing dhclient instance with pid %d.\n", pid); kill (pid, SIGTERM); } @@ -294,52 +157,46 @@ fatal_error (void) int main (int argc, char *argv[]) { - DBusConnection *connection; - DBusMessage *message; - dbus_bool_t result; - DBusError error; - - dbus_connection_set_change_sigpipe (TRUE); + GDBusConnection *connection; + GError *error = NULL; - dbus_error_init (&error); - connection = dbus_connection_open_private ("unix:path=" NMRUNDIR "/private-dhcp", &error); + connection = g_dbus_connection_new_for_address_sync ("unix:path=" NMRUNDIR "/private-dhcp", + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, + NULL, NULL, &error); if (!connection) { #if !HAVE_DBUS_GLIB_100 connection = shared_connection_init (); #endif if (!connection) { - fprintf (stderr, "Error: could not connect to NetworkManager DBus socket: (%s) %s\n", - error.name, error.message); - dbus_error_free (&error); + g_dbus_error_strip_remote_error (error); + g_printerr ("Error: could not connect to NetworkManager D-Bus socket: %s\n", + error->message); + g_error_free (error); fatal_error (); } } - dbus_connection_set_exit_on_disconnect (connection, FALSE); - message = dbus_message_new_signal ("/", NM_DHCP_CLIENT_DBUS_IFACE, "Event"); - if (message == NULL) { - fprintf (stderr, "Error: Not enough memory to send DHCP Event signal.\n"); + if (!g_dbus_connection_emit_signal (connection, + NULL, + "/", + NM_DHCP_CLIENT_DBUS_IFACE, + "Event", + build_signal_parameters (), + &error)) { + g_dbus_error_strip_remote_error (error); + g_printerr ("Error: Could not send DHCP Event signal: %s\n", error->message); + g_error_free (error); fatal_error (); } - /* Dump environment variables into the message */ - result = build_message (message); - if (result == FALSE) { - fprintf (stderr, "Error: Not enough memory to send DHCP Event signal.\n"); + if (!g_dbus_connection_flush_sync (connection, NULL, &error)) { + g_dbus_error_strip_remote_error (error); + g_printerr ("Error: Could not flush D-Bus connection: %s\n", error->message); + g_error_free (error); fatal_error (); } - /* queue the message */ - result = dbus_connection_send (connection, message, NULL); - if (!result) { - fprintf (stderr, "Error: Could not send send DHCP Event signal.\n"); - fatal_error (); - } - dbus_message_unref (message); - - /* Send out the message */ - dbus_connection_flush (connection); - + g_object_unref (connection); return 0; } diff --git a/src/ppp-manager/Makefile.am b/src/ppp-manager/Makefile.am index aef3f418ae..fd2fe76bd8 100644 --- a/src/ppp-manager/Makefile.am +++ b/src/ppp-manager/Makefile.am @@ -7,7 +7,6 @@ AM_CPPFLAGS = \ -DG_LOG_DOMAIN=\""nm-pppd-plugin"\" \ -DNETWORKMANAGER_COMPILATION \ -DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \ - $(DBUS_CFLAGS) \ $(GLIB_CFLAGS) pppd_plugindir = $(PPPD_PLUGIN_DIR) @@ -21,7 +20,6 @@ nm_pppd_plugin_la_SOURCES = \ nm_pppd_plugin_la_LDFLAGS = -module -avoid-version nm_pppd_plugin_la_LIBADD = \ - $(DBUS_LIBS) \ $(GLIB_LIBS) \ -ldl diff --git a/src/ppp-manager/nm-pppd-plugin.c b/src/ppp-manager/nm-pppd-plugin.c index 64de4b5c9f..183f5dbe00 100644 --- a/src/ppp-manager/nm-pppd-plugin.c +++ b/src/ppp-manager/nm-pppd-plugin.c @@ -28,9 +28,7 @@ #include <netinet/in.h> #include <arpa/inet.h> #include <dlfcn.h> -#include <glib.h> -#include <glib-object.h> -#include <dbus/dbus-glib.h> +#include <gio/gio.h> #define INET6 #include <pppd/eui64.h> @@ -39,13 +37,12 @@ #include "nm-dbus-interface.h" #include "nm-pppd-plugin.h" #include "nm-ppp-status.h" -#include "nm-dbus-glib-types.h" int plugin_init (void); char pppd_version[] = VERSION; -static DBusGProxy *proxy = NULL; +static GDBusProxy *proxy = NULL; static void nm_phasechange (void *data, int arg) @@ -53,7 +50,7 @@ nm_phasechange (void *data, int arg) NMPPPStatus ppp_status = NM_PPP_STATUS_UNKNOWN; char *ppp_phase; - g_return_if_fail (DBUS_IS_G_PROXY (proxy)); + g_return_if_fail (G_IS_DBUS_PROXY (proxy)); switch (arg) { case PHASE_DEAD: @@ -120,54 +117,24 @@ nm_phasechange (void *data, int arg) ppp_phase); if (ppp_status != NM_PPP_STATUS_UNKNOWN) { - dbus_g_proxy_call_no_reply (proxy, "SetState", - G_TYPE_UINT, ppp_status, G_TYPE_INVALID, - G_TYPE_INVALID); + g_dbus_proxy_call (proxy, + "SetState", + g_variant_new ("(u)", ppp_status), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, + NULL, NULL); } } -static GValue * -str_to_gvalue (const char *str) -{ - GValue *val; - - val = g_slice_new0 (GValue); - g_value_init (val, G_TYPE_STRING); - g_value_set_string (val, str); - return val; -} - -static GValue * -uint_to_gvalue (guint32 i) -{ - GValue *val; - - val = g_slice_new0 (GValue); - g_value_init (val, G_TYPE_UINT); - g_value_set_uint (val, i); - return val; -} - -static void -value_destroy (gpointer data) -{ - GValue *val = (GValue *) data; - - g_value_unset (val); - g_slice_free (GValue, val); -} - static void nm_ip_up (void *data, int arg) { ipcp_options opts = ipcp_gotoptions[0]; ipcp_options peer_opts = ipcp_hisoptions[0]; - GHashTable *hash; - GArray *array; - GValue *val; + GVariantBuilder builder; guint32 pppd_made_up_address = htonl (0x0a404040 + ifunit); - g_return_if_fail (DBUS_IS_G_PROXY (proxy)); + g_return_if_fail (G_IS_DBUS_PROXY (proxy)); g_message ("nm-ppp-plugin: (%s): ip-up event", __func__); @@ -177,85 +144,88 @@ nm_ip_up (void *data, int arg) return; } - hash = g_hash_table_new_full (g_str_hash, g_str_equal, - NULL, value_destroy); + g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); - g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_INTERFACE, - str_to_gvalue (ifname)); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP4_CONFIG_INTERFACE, + g_variant_new_string (ifname)); - g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_ADDRESS, - uint_to_gvalue (opts.ouraddr)); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP4_CONFIG_ADDRESS, + g_variant_new_uint32 (opts.ouraddr)); /* Prefer the peer options remote address first, _unless_ pppd made the * address up, at which point prefer the local options remote address, * and if that's not right, use the made-up address as a last resort. */ if (peer_opts.hisaddr && (peer_opts.hisaddr != pppd_made_up_address)) { - g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, - uint_to_gvalue (peer_opts.hisaddr)); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP4_CONFIG_GATEWAY, + g_variant_new_uint32 (peer_opts.hisaddr)); } else if (opts.hisaddr) { - g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, - uint_to_gvalue (opts.hisaddr)); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP4_CONFIG_GATEWAY, + g_variant_new_uint32 (opts.hisaddr)); } else if (peer_opts.hisaddr == pppd_made_up_address) { /* As a last resort, use the made-up address */ - g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_GATEWAY, - uint_to_gvalue (peer_opts.hisaddr)); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP4_CONFIG_GATEWAY, + g_variant_new_uint32 (peer_opts.ouraddr)); } - g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_PREFIX, uint_to_gvalue (32)); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP4_CONFIG_PREFIX, + g_variant_new_uint32 (32)); if (opts.dnsaddr[0] || opts.dnsaddr[1]) { - array = g_array_new (FALSE, FALSE, sizeof (guint32)); + guint32 dns[2]; + int len = 0; if (opts.dnsaddr[0]) - g_array_append_val (array, opts.dnsaddr[0]); + dns[len++] = opts.dnsaddr[0]; if (opts.dnsaddr[1]) - g_array_append_val (array, opts.dnsaddr[1]); - - val = g_slice_new0 (GValue); - g_value_init (val, DBUS_TYPE_G_UINT_ARRAY); - g_value_set_boxed (val, array); + dns[len++] = opts.dnsaddr[1]; - g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_DNS, val); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP4_CONFIG_DNS, + g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, + dns, len, sizeof (guint32))); } if (opts.winsaddr[0] || opts.winsaddr[1]) { - array = g_array_new (FALSE, FALSE, sizeof (guint32)); + guint32 wins[2]; + int len = 0; if (opts.winsaddr[0]) - g_array_append_val (array, opts.winsaddr[0]); + wins[len++] = opts.winsaddr[0]; if (opts.winsaddr[1]) - g_array_append_val (array, opts.winsaddr[1]); + wins[len++] = opts.winsaddr[1]; - val = g_slice_new0 (GValue); - g_value_init (val, DBUS_TYPE_G_UINT_ARRAY); - g_value_set_boxed (val, array); - - g_hash_table_insert (hash, NM_PPP_IP4_CONFIG_WINS, val); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP4_CONFIG_WINS, + g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, + wins, len, sizeof (guint32))); } g_message ("nm-ppp-plugin: (%s): sending IPv4 config to NetworkManager...", __func__); - dbus_g_proxy_call_no_reply (proxy, "SetIp4Config", - DBUS_TYPE_G_MAP_OF_VARIANT, hash, G_TYPE_INVALID, - G_TYPE_INVALID); - - g_hash_table_destroy (hash); + g_dbus_proxy_call (proxy, + "SetIp4Config", + g_variant_new ("(a{sv})", &builder), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, + NULL, NULL); } -static GValue * -eui64_to_gvalue (eui64_t eui) +static GVariant * +eui64_to_variant (eui64_t eui) { - GValue *val; guint64 iid; G_STATIC_ASSERT (sizeof (iid) == sizeof (eui)); - val = g_slice_new0 (GValue); - g_value_init (val, G_TYPE_UINT64); memcpy (&iid, &eui, sizeof (eui)); - g_value_set_uint64 (val, iid); - return val; + return g_variant_new_uint64 (iid); } static void @@ -263,26 +233,33 @@ nm_ip6_up (void *data, int arg) { ipv6cp_options *ho = &ipv6cp_hisoptions[0]; ipv6cp_options *go = &ipv6cp_gotoptions[0]; - GHashTable *hash; + GVariantBuilder builder; - g_return_if_fail (DBUS_IS_G_PROXY (proxy)); + g_return_if_fail (G_IS_DBUS_PROXY (proxy)); g_message ("nm-ppp-plugin: (%s): ip6-up event", __func__); - hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, value_destroy); - g_hash_table_insert (hash, NM_PPP_IP6_CONFIG_INTERFACE, str_to_gvalue (ifname)); - g_hash_table_insert (hash, NM_PPP_IP6_CONFIG_OUR_IID, eui64_to_gvalue (go->ourid)); - g_hash_table_insert (hash, NM_PPP_IP6_CONFIG_PEER_IID, eui64_to_gvalue (ho->hisid)); + g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP6_CONFIG_INTERFACE, + g_variant_new_string (ifname)); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP6_CONFIG_OUR_IID, + eui64_to_variant (go->ourid)); + g_variant_builder_add (&builder, "{sv}", + NM_PPP_IP6_CONFIG_PEER_IID, + eui64_to_variant (ho->hisid)); /* DNS is done via DHCPv6 or router advertisements */ g_message ("nm-ppp-plugin: (%s): sending IPv6 config to NetworkManager...", __func__); - dbus_g_proxy_call_no_reply (proxy, "SetIp6Config", - DBUS_TYPE_G_MAP_OF_VARIANT, hash, G_TYPE_INVALID, - G_TYPE_INVALID); - - g_hash_table_destroy (hash); + g_dbus_proxy_call (proxy, + "SetIp6Config", + g_variant_new ("(a{sv})", &builder), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, + NULL, NULL); } static int @@ -300,9 +277,10 @@ get_pap_check (void) static int get_credentials (char *username, char *password) { - char *my_username = NULL; - char *my_password = NULL; + const char *my_username = NULL; + const char *my_password = NULL; size_t len; + GVariant *ret; GError *err = NULL; if (!password) { @@ -312,15 +290,16 @@ get_credentials (char *username, char *password) } g_return_val_if_fail (username, -1); - g_return_val_if_fail (DBUS_IS_G_PROXY (proxy), -1); + g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), -1); g_message ("nm-ppp-plugin: (%s): passwd-hook, requesting credentials...", __func__); - if (!dbus_g_proxy_call (proxy, "NeedSecrets", &err, - G_TYPE_INVALID, - G_TYPE_STRING, &my_username, - G_TYPE_STRING, &my_password, - G_TYPE_INVALID)) { + ret = g_dbus_proxy_call_sync (proxy, + "NeedSecrets", + NULL, + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &err); + if (!ret) { g_warning ("nm-ppp-plugin: (%s): could not get secrets: (%d) %s", __func__, err ? err->code : -1, @@ -331,14 +310,14 @@ get_credentials (char *username, char *password) g_message ("nm-ppp-plugin: (%s): got credentials from NetworkManager", __func__); + g_variant_get (ret, "(&s&s)", &my_username, &my_password); + if (my_username) { len = strlen (my_username) + 1; len = len < MAXNAMELEN ? len : MAXNAMELEN; strncpy (username, my_username, len); username[len - 1] = '\0'; - - g_free (my_username); } if (my_password) { @@ -347,17 +326,17 @@ get_credentials (char *username, char *password) strncpy (password, my_password, len); password[len - 1] = '\0'; - - g_free (my_password); } + g_variant_unref (ret); + return 1; } static void nm_exit_notify (void *data, int arg) { - g_return_if_fail (DBUS_IS_G_PROXY (proxy)); + g_return_if_fail (G_IS_DBUS_PROXY (proxy)); g_message ("nm-ppp-plugin: (%s): cleaning up", __func__); @@ -389,7 +368,7 @@ add_ip6_notifier (void) int plugin_init (void) { - DBusGConnection *bus; + GDBusConnection *bus; GError *err = NULL; #if !GLIB_CHECK_VERSION (2, 35, 0) @@ -398,12 +377,10 @@ plugin_init (void) g_message ("nm-ppp-plugin: (%s): initializing", __func__); - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err); + bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &err); if (!bus) { - g_warning ("nm-pppd-plugin: (%s): couldn't connect to system bus: (%d) %s", - __func__, - err ? err->code : -1, - err && err->message ? err->message : "(unknown)"); + g_warning ("nm-pppd-plugin: (%s): couldn't connect to system bus: %s", + __func__, err->message); g_error_free (err); return -1; } @@ -411,9 +388,21 @@ plugin_init (void) /* NM passes in the object path of the corresponding PPPManager * object as the 'ipparam' argument to pppd. */ - proxy = dbus_g_proxy_new_for_name (bus, NM_DBUS_SERVICE, ipparam, NM_DBUS_INTERFACE_PPP); - - dbus_g_connection_unref (bus); + proxy = g_dbus_proxy_new_sync (bus, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + NM_DBUS_SERVICE, + ipparam, + NM_DBUS_INTERFACE_PPP, + NULL, &err); + g_object_unref (bus); + + if (!proxy) { + g_warning ("nm-pppd-plugin: (%s): couldn't create D-Bus proxy: %s", + __func__, err->message); + g_error_free (err); + return -1; + } chap_passwd_hook = get_credentials; chap_check_hook = get_chap_check; |