diff options
Diffstat (limited to 'examples')
26 files changed, 851 insertions, 886 deletions
diff --git a/examples/C/glib/add-connection-gdbus.c b/examples/C/glib/add-connection-gdbus.c index 3ffb2929..3e3f67ce 100644 --- a/examples/C/glib/add-connection-gdbus.c +++ b/examples/C/glib/add-connection-gdbus.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2011, 2014 Red Hat, Inc. */ @@ -24,111 +24,121 @@ /* copied from libnm-core/nm-utils.c */ char * -nm_utils_uuid_generate (void) +nm_utils_uuid_generate(void) { - uuid_t uuid; - char *buf; + uuid_t uuid; + char * buf; - buf = g_malloc0 (37); - uuid_generate_random (uuid); - uuid_unparse_lower (uuid, &buf[0]); - return 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) +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_IP_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); - } + 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_IP_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[]) +main(int argc, char *argv[]) { - GDBusProxy *proxy; - GError *error = NULL; - - /* 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; + GDBusProxy *proxy; + GError * error = NULL; + + /* 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 4859b907..510ea4a2 100644 --- a/examples/C/glib/add-connection-libnm.c +++ b/examples/C/glib/add-connection-libnm.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2011 Red Hat, Inc. */ @@ -17,96 +17,98 @@ #include <NetworkManager.h> static void -added_cb (GObject *client, - GAsyncResult *result, - gpointer user_data) +added_cb(GObject *client, GAsyncResult *result, gpointer user_data) { - GMainLoop *loop = user_data; - NMRemoteConnection *remote; - GError *error = NULL; - - /* NM responded to our request; either handle the resulting error or - * print out the object path of the connection we just added. - */ - remote = nm_client_add_connection_finish (NM_CLIENT (client), result, &error); - - if (error) { - g_print ("Error adding connection: %s", error->message); - g_error_free (error); - } else { - g_print ("Added: %s\n", nm_connection_get_path (NM_CONNECTION (remote))); - g_object_unref (remote); - } - - /* Tell the mainloop we're done and we can quit now */ - g_main_loop_quit (loop); + GMainLoop * loop = user_data; + NMRemoteConnection *remote; + GError * error = NULL; + + /* NM responded to our request; either handle the resulting error or + * print out the object path of the connection we just added. + */ + remote = nm_client_add_connection_finish(NM_CLIENT(client), result, &error); + + if (error) { + g_print("Error adding connection: %s", error->message); + g_error_free(error); + } else { + g_print("Added: %s\n", nm_connection_get_path(NM_CONNECTION(remote))); + g_object_unref(remote); + } + + /* Tell the mainloop we're done and we can quit now */ + g_main_loop_quit(loop); } static void -add_connection (NMClient *client, GMainLoop *loop, const char *con_name) +add_connection(NMClient *client, GMainLoop *loop, const char *con_name) { - NMConnection *connection; - NMSettingConnection *s_con; - NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - char *uuid; - - /* Create a new connection object */ - connection = nm_simple_connection_new (); - - /* Build up the 'connection' Setting */ - s_con = (NMSettingConnection *) nm_setting_connection_new (); - uuid = nm_utils_uuid_generate (); - g_object_set (G_OBJECT (s_con), - NM_SETTING_CONNECTION_UUID, uuid, - NM_SETTING_CONNECTION_ID, con_name, - NM_SETTING_CONNECTION_TYPE, "802-3-ethernet", - NULL); - g_free (uuid); - nm_connection_add_setting (connection, NM_SETTING (s_con)); - - /* Build up the 'wired' Setting */ - s_wired = (NMSettingWired *) nm_setting_wired_new (); - nm_connection_add_setting (connection, NM_SETTING (s_wired)); - - /* Build up the 'ipv4' Setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); - g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NULL); - nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - - /* Ask the settings service to add the new connection; we'll quit the - * mainloop and exit when the callback is called. - */ - nm_client_add_connection_async (client, connection, TRUE, NULL, added_cb, loop); - g_object_unref (connection); + NMConnection * connection; + NMSettingConnection *s_con; + NMSettingWired * s_wired; + NMSettingIP4Config * s_ip4; + char * uuid; + + /* Create a new connection object */ + connection = nm_simple_connection_new(); + + /* Build up the 'connection' Setting */ + s_con = (NMSettingConnection *) nm_setting_connection_new(); + uuid = nm_utils_uuid_generate(); + g_object_set(G_OBJECT(s_con), + NM_SETTING_CONNECTION_UUID, + uuid, + NM_SETTING_CONNECTION_ID, + con_name, + NM_SETTING_CONNECTION_TYPE, + "802-3-ethernet", + NULL); + g_free(uuid); + nm_connection_add_setting(connection, NM_SETTING(s_con)); + + /* Build up the 'wired' Setting */ + s_wired = (NMSettingWired *) nm_setting_wired_new(); + nm_connection_add_setting(connection, NM_SETTING(s_wired)); + + /* Build up the 'ipv4' Setting */ + s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new(); + g_object_set(G_OBJECT(s_ip4), + NM_SETTING_IP_CONFIG_METHOD, + NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NULL); + nm_connection_add_setting(connection, NM_SETTING(s_ip4)); + + /* Ask the settings service to add the new connection; we'll quit the + * mainloop and exit when the callback is called. + */ + nm_client_add_connection_async(client, connection, TRUE, NULL, added_cb, loop); + g_object_unref(connection); } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - NMClient *client; - GMainLoop *loop; - GError *error = NULL; + NMClient * client; + GMainLoop *loop; + GError * error = NULL; - loop = g_main_loop_new (NULL, FALSE); + loop = g_main_loop_new(NULL, FALSE); - /* Connect to NetworkManager */ - client = nm_client_new (NULL, &error); - if (!client) { - g_message ("Error: Could not connect to NetworkManager: %s.", error->message); - g_error_free (error); - return 1; - } + /* Connect to NetworkManager */ + client = nm_client_new(NULL, &error); + if (!client) { + g_message("Error: Could not connect to NetworkManager: %s.", error->message); + g_error_free(error); + return 1; + } - /* Ask NM to add the new connection */ - add_connection (client, loop, "__Test connection__"); - /* Wait for the connection to be added */ - g_main_loop_run (loop); + /* Ask NM to add the new connection */ + add_connection(client, loop, "__Test connection__"); + /* Wait for the connection to be added */ + g_main_loop_run(loop); - /* Clean up */ - g_object_unref (client); + /* Clean up */ + g_object_unref(client); - return 0; + return 0; } diff --git a/examples/C/glib/get-active-connections-gdbus.c b/examples/C/glib/get-active-connections-gdbus.c index e4218ae5..e3edb429 100644 --- a/examples/C/glib/get-active-connections-gdbus.c +++ b/examples/C/glib/get-active-connections-gdbus.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2010 - 2014 Red Hat, Inc. */ @@ -22,236 +22,241 @@ #include <NetworkManager.h> static void -print_setting (const char *setting_name, GVariant *setting) +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); - } + 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) +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"); + 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); + 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) +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); + 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); + 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) +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); + 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); + if (value) + g_variant_unref(value); + if (ret) + g_variant_unref(ret); } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - GDBusProxy *props_proxy; - - /* 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; + GDBusProxy *props_proxy; + + /* 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 85242b0e..f2bcdf9d 100644 --- a/examples/C/glib/get-ap-info-libnm.c +++ b/examples/C/glib/get-ap-info-libnm.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2010 Red Hat, Inc. */ @@ -19,194 +19,196 @@ /* Convert flags to string */ static char * -ap_wpa_rsn_flags_to_string (guint32 flags) +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; + 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) +show_access_point_info(NMAccessPoint *ap) { - guint32 flags, wpa_flags, rsn_flags, freq, bitrate; - guint8 strength; - GBytes *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_bssid (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 */ - if (ssid) - ssid_str = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid)); - else - ssid_str = g_strdup ("--"); - freq_str = g_strdup_printf ("%u MHz", freq); - bitrate_str = g_strdup_printf ("%u Mbit/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); + guint32 flags, wpa_flags, rsn_flags, freq, bitrate; + guint8 strength; + GBytes * 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_bssid(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 */ + if (ssid) + ssid_str = nm_utils_ssid_to_utf8(g_bytes_get_data(ssid, NULL), g_bytes_get_size(ssid)); + else + ssid_str = g_strdup("--"); + freq_str = g_strdup_printf("%u MHz", freq); + bitrate_str = g_strdup_printf("%u Mbit/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) +show_wifi_device_info(NMDevice *device) { - NMAccessPoint *active_ap = NULL; - const GPtrArray *aps; - const char *iface; - const char *driver; - guint32 speed; - GBytes *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); - if (active_ssid) - active_ssid_str = nm_utils_ssid_to_utf8 (g_bytes_get_data (active_ssid, NULL), - g_bytes_get_size (active_ssid)); - else - active_ssid_str = g_strdup ("--"); - } - } - - 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 Mbit/s ---- Active AP: %s\n", - iface, driver, speed, 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; i < aps->len; i++) { - NMAccessPoint *ap = g_ptr_array_index (aps, i); - show_access_point_info (ap); - } + NMAccessPoint * active_ap = NULL; + const GPtrArray *aps; + const char * iface; + const char * driver; + guint32 speed; + GBytes * 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); + if (active_ssid) + active_ssid_str = nm_utils_ssid_to_utf8(g_bytes_get_data(active_ssid, NULL), + g_bytes_get_size(active_ssid)); + else + active_ssid_str = g_strdup("--"); + } + } + + 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 Mbit/s ---- Active AP: %s\n", + iface, + driver, + speed, + 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; i < aps->len; i++) { + NMAccessPoint *ap = g_ptr_array_index(aps, i); + show_access_point_info(ap); + } } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - NMClient *client; - const GPtrArray *devices; - int i; - GError *error = NULL; - - /* Get NMClient object */ - client = nm_client_new (NULL, &error); - if (!client) { - g_message ("Error: Could not create NMClient: %s.", error->message); - g_error_free (error); - 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; 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); - - return EXIT_SUCCESS; + NMClient * client; + const GPtrArray *devices; + int i; + GError * error = NULL; + + /* Get NMClient object */ + client = nm_client_new(NULL, &error); + if (!client) { + g_message("Error: Could not create NMClient: %s.", error->message); + g_error_free(error); + 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; 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); + + return EXIT_SUCCESS; } diff --git a/examples/C/glib/list-connections-gdbus.c b/examples/C/glib/list-connections-gdbus.c index 028ff910..84fcf0bd 100644 --- a/examples/C/glib/list-connections-gdbus.c +++ b/examples/C/glib/list-connections-gdbus.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2011, 2014 Red Hat, Inc. */ @@ -18,53 +18,56 @@ #include <nm-dbus-interface.h> static void -list_connections (GDBusProxy *proxy) +list_connections(GDBusProxy *proxy) { - int i; - GError *error = NULL; - GVariant *ret; - char **paths; + 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; - } + /* 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); + 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); + for (i = 0; paths[i]; i++) + g_print("%s\n", paths[i]); + g_strfreev(paths); } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - GDBusProxy *proxy; + GDBusProxy *proxy; - /* 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); + /* 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); + /* List connections of system settings service */ + list_connections(proxy); - g_object_unref (proxy); + g_object_unref(proxy); - return 0; + return 0; } diff --git a/examples/C/glib/list-connections-libnm.c b/examples/C/glib/list-connections-libnm.c index 7bed0c01..39affb4c 100644 --- a/examples/C/glib/list-connections-libnm.c +++ b/examples/C/glib/list-connections-libnm.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2011 Red Hat, Inc. */ @@ -20,66 +20,66 @@ /* Print details of connection */ static void -show_connection (NMConnection *connection) +show_connection(NMConnection *connection) { - NMSettingConnection *s_con; - guint64 timestamp; - char *timestamp_str; - char timestamp_real_str[64]; - const char *val1, *val2, *val3, *val4, *val5; - - s_con = nm_connection_get_setting_connection (connection); - if (s_con) { - struct tm localtime_data; - - /* 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_r ((time_t *) ×tamp, &localtime_data)); - - 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); - } + NMSettingConnection *s_con; + guint64 timestamp; + char * timestamp_str; + char timestamp_real_str[64]; + const char * val1, *val2, *val3, *val4, *val5; + + s_con = nm_connection_get_setting_connection(connection); + if (s_con) { + struct tm localtime_data; + + /* 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_r((time_t *) ×tamp, &localtime_data)); + + 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); + } } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - NMClient *client; - GError *error = NULL; - const GPtrArray *connections; - int i; + NMClient * client; + GError * error = NULL; + const GPtrArray *connections; + int i; - if (!(client = nm_client_new (NULL, &error))) { - g_message ("Error: Could not connect to NetworkManager: %s.", error->message); - g_error_free (error); - return EXIT_FAILURE; - } + if (!(client = nm_client_new(NULL, &error))) { + g_message("Error: Could not connect to NetworkManager: %s.", error->message); + g_error_free(error); + return EXIT_FAILURE; + } - if (!nm_client_get_nm_running (client)) { - g_message ("Error: Can't obtain connections: NetworkManager is not running."); - return EXIT_FAILURE; - } + if (!nm_client_get_nm_running(client)) { + g_message("Error: Can't obtain connections: NetworkManager is not running."); + return EXIT_FAILURE; + } - /* Now the connections can be listed. */ - connections = nm_client_get_connections (client); + /* Now the connections can be listed. */ + connections = nm_client_get_connections(client); - printf ("Connections:\n===================\n"); + printf("Connections:\n===================\n"); - for (i = 0; i < connections->len; i++) - show_connection (connections->pdata[i]); + for (i = 0; i < connections->len; i++) + show_connection(connections->pdata[i]); - g_object_unref (client); + g_object_unref(client); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/examples/C/glib/monitor-nm-running-gdbus.c b/examples/C/glib/monitor-nm-running-gdbus.c index 3856e731..e38fb533 100644 --- a/examples/C/glib/monitor-nm-running-gdbus.c +++ b/examples/C/glib/monitor-nm-running-gdbus.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2012 Red Hat, Inc. */ @@ -17,51 +17,47 @@ #include <gio/gio.h> static void -on_name_appeared (GDBusConnection *connection, - const char *name, - const char *name_owner, - gpointer user_data) +on_name_appeared(GDBusConnection *connection, + const char * name, + const char * name_owner, + gpointer user_data) { - g_print ("Name '%s' on the system bus is owned by %s => NM is running\n", - name, name_owner); + g_print("Name '%s' on the system bus is owned by %s => NM is running\n", name, name_owner); } static void -on_name_vanished (GDBusConnection *connection, - const char *name, - gpointer user_data) +on_name_vanished(GDBusConnection *connection, const char *name, gpointer user_data) { - g_print ("Name '%s' does not exist on the system bus => NM is not running\n", name); + g_print("Name '%s' does not exist on the system bus => NM is not running\n", name); } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - guint watcher_id; - GMainLoop *loop; - GBusNameWatcherFlags flags; + guint watcher_id; + GMainLoop * loop; + GBusNameWatcherFlags flags; - g_print ("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n"); - g_print ("===================================================\n"); + g_print("Monitor 'org.freedesktop.NetworkManager' D-Bus name\n"); + g_print("===================================================\n"); - flags = G_BUS_NAME_WATCHER_FLAGS_NONE; + flags = G_BUS_NAME_WATCHER_FLAGS_NONE; - /* Start to watch "org.freedesktop.NetworkManager" bus name */ - watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, - "org.freedesktop.NetworkManager", - flags, - on_name_appeared, - on_name_vanished, - NULL, - NULL); + /* Start to watch "org.freedesktop.NetworkManager" bus name */ + watcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM, + "org.freedesktop.NetworkManager", + flags, + on_name_appeared, + on_name_vanished, + NULL, + NULL); - /* Run main loop */ - loop = g_main_loop_new (NULL, FALSE); - g_main_loop_run (loop); + /* Run main loop */ + loop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(loop); - /* Stop watching the name */ - g_bus_unwatch_name (watcher_id); + /* Stop watching the name */ + g_bus_unwatch_name(watcher_id); - return 0; + return 0; } - diff --git a/examples/C/glib/monitor-nm-state-gdbus.c b/examples/C/glib/monitor-nm-state-gdbus.c index e7c23ca2..43259dd0 100644 --- a/examples/C/glib/monitor-nm-state-gdbus.c +++ b/examples/C/glib/monitor-nm-state-gdbus.c @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2012 Red Hat, Inc. */ @@ -20,95 +20,92 @@ #include <nm-dbus-interface.h> static const char * -nm_state_to_string (NMState state) +nm_state_to_string(NMState state) { - switch (state) { - case NM_STATE_ASLEEP: - return "asleep"; - case NM_STATE_CONNECTING: - return "connecting"; - case NM_STATE_CONNECTED_LOCAL: - return "connected (local only)"; - case NM_STATE_CONNECTED_SITE: - return "connected (site only)"; - case NM_STATE_CONNECTED_GLOBAL: - return "connected"; - case NM_STATE_DISCONNECTING: - return "disconnecting"; - case NM_STATE_DISCONNECTED: - return "disconnected"; - case NM_STATE_UNKNOWN: - default: - return "unknown"; - } + switch (state) { + case NM_STATE_ASLEEP: + return "asleep"; + case NM_STATE_CONNECTING: + return "connecting"; + case NM_STATE_CONNECTED_LOCAL: + return "connected (local only)"; + case NM_STATE_CONNECTED_SITE: + return "connected (site only)"; + case NM_STATE_CONNECTED_GLOBAL: + return "connected"; + case NM_STATE_DISCONNECTING: + return "disconnecting"; + case NM_STATE_DISCONNECTED: + return "disconnected"; + case NM_STATE_UNKNOWN: + default: + return "unknown"; + } } static void -on_signal (GDBusProxy *proxy, - char *sender_name, - char *signal_name, - GVariant *parameters, - gpointer user_data) +on_signal(GDBusProxy *proxy, + char * sender_name, + char * signal_name, + GVariant * parameters, + gpointer user_data) { - guint32 new_state; - - /* Print all signals */ - //char *parameters_str; - //parameters_str = g_variant_print (parameters, TRUE); - //g_print (" *** Received Signal: %s: %s\n", signal_name, parameters_str); - //g_free (parameters_str); - - /* We are only interested in "StateChanged" signal */ - if (strcmp (signal_name, "StateChanged") == 0) { - GVariant *tmp = g_variant_get_child_value (parameters, 0); - new_state = g_variant_get_uint32 (tmp); - g_variant_unref (tmp); - g_print ("NetworkManager state is: (%d) %s\n", new_state, nm_state_to_string ((NMState) new_state)); - } + guint32 new_state; + + /* Print all signals */ + //char *parameters_str; + //parameters_str = g_variant_print (parameters, TRUE); + //g_print (" *** Received Signal: %s: %s\n", signal_name, parameters_str); + //g_free (parameters_str); + + /* We are only interested in "StateChanged" signal */ + if (strcmp(signal_name, "StateChanged") == 0) { + GVariant *tmp = g_variant_get_child_value(parameters, 0); + new_state = g_variant_get_uint32(tmp); + g_variant_unref(tmp); + g_print("NetworkManager state is: (%d) %s\n", + new_state, + nm_state_to_string((NMState) new_state)); + } } int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { - GMainLoop *loop; - GError *error = NULL; - GDBusProxyFlags flags; - GDBusProxy *proxy; - - /* Monitor 'StateChanged' signal on 'org.freedesktop.NetworkManager' interface */ - g_print ("Monitor NetworkManager's state\n"); - g_print ("==============================\n"); - - flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START; - proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, - flags, - NULL, /* GDBusInterfaceInfo */ - "org.freedesktop.NetworkManager", - "/org/freedesktop/NetworkManager", - "org.freedesktop.NetworkManager", - NULL, /* GCancellable */ - &error); - - 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; - } - - /* Connect to g-signal to receive signals from proxy (remote object) */ - g_signal_connect (proxy, - "g-signal", - G_CALLBACK (on_signal), - NULL); - - /* Run main loop */ - loop = g_main_loop_new (NULL, FALSE); - g_main_loop_run (loop); - - g_object_unref (proxy); - - return 0; + GMainLoop * loop; + GError * error = NULL; + GDBusProxyFlags flags; + GDBusProxy * proxy; + + /* Monitor 'StateChanged' signal on 'org.freedesktop.NetworkManager' interface */ + g_print("Monitor NetworkManager's state\n"); + g_print("==============================\n"); + + flags = G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START; + proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, + flags, + NULL, /* GDBusInterfaceInfo */ + "org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager", + "org.freedesktop.NetworkManager", + NULL, /* GCancellable */ + &error); + + 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; + } + + /* Connect to g-signal to receive signals from proxy (remote object) */ + g_signal_connect(proxy, "g-signal", G_CALLBACK(on_signal), NULL); + + /* Run main loop */ + loop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(loop); + + g_object_unref(proxy); + + return 0; } - diff --git a/examples/C/qt/add-connection-wired.cpp b/examples/C/qt/add-connection-wired.cpp index 97f544ee..f9df13bc 100644 --- a/examples/C/qt/add-connection-wired.cpp +++ b/examples/C/qt/add-connection-wired.cpp @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2011 Eckhart Wörner */ diff --git a/examples/C/qt/change-ipv4-addresses.cpp b/examples/C/qt/change-ipv4-addresses.cpp index 4c42a6b6..059e2b4a 100644 --- a/examples/C/qt/change-ipv4-addresses.cpp +++ b/examples/C/qt/change-ipv4-addresses.cpp @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2011 Red Hat, Inc. */ diff --git a/examples/C/qt/list-connections.cpp b/examples/C/qt/list-connections.cpp index 70831ad5..39293ebc 100644 --- a/examples/C/qt/list-connections.cpp +++ b/examples/C/qt/list-connections.cpp @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2011 Eckhart Wörner */ diff --git a/examples/C/qt/monitor-nm-running.cpp b/examples/C/qt/monitor-nm-running.cpp index ace29698..db3ec07b 100644 --- a/examples/C/qt/monitor-nm-running.cpp +++ b/examples/C/qt/monitor-nm-running.cpp @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0+ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright (C) 2012 Red Hat, Inc. */ diff --git a/examples/python/dbus/add-connection-compat.py b/examples/python/dbus/add-connection-compat.py index 6acec734..2661eccb 100755 --- a/examples/python/dbus/add-connection-compat.py +++ b/examples/python/dbus/add-connection-compat.py @@ -16,16 +16,14 @@ import socket, struct, dbus, uuid -# Helper functions + def ip_to_int(ip_string): return struct.unpack("=I", socket.inet_aton(ip_string))[0] -def int_to_ip(ip_int): - return socket.inet_ntoa(struct.pack("=I", ip_int)) - - +# full duplex s_wired = dbus.Dictionary({"duplex": "full"}) + s_con = dbus.Dictionary( {"type": "802-3-ethernet", "uuid": str(uuid.uuid4()), "id": "MyConnectionExample"} ) @@ -47,7 +45,6 @@ con = dbus.Dictionary( {"802-3-ethernet": s_wired, "connection": s_con, "ipv4": s_ip4, "ipv6": s_ip6} ) - print("Creating connection:", s_con["id"], "-", s_con["uuid"]) bus = dbus.SystemBus() diff --git a/examples/python/dbus/add-connection.py b/examples/python/dbus/add-connection.py index ee2deb26..a7c8947c 100755 --- a/examples/python/dbus/add-connection.py +++ b/examples/python/dbus/add-connection.py @@ -18,11 +18,13 @@ import dbus, uuid s_wired = dbus.Dictionary({"duplex": "full"}) + s_con = dbus.Dictionary( {"type": "802-3-ethernet", "uuid": str(uuid.uuid4()), "id": "MyConnectionExample"} ) addr1 = dbus.Dictionary({"address": "10.1.2.3", "prefix": dbus.UInt32(8)}) + s_ip4 = dbus.Dictionary( { "address-data": dbus.Array([addr1], signature=dbus.Signature("a{sv}")), @@ -37,9 +39,7 @@ con = dbus.Dictionary( {"802-3-ethernet": s_wired, "connection": s_con, "ipv4": s_ip4, "ipv6": s_ip6} ) - print("Creating connection:", s_con["id"], "-", s_con["uuid"]) - bus = dbus.SystemBus() proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" diff --git a/examples/python/dbus/add-wifi-eap-connection.py b/examples/python/dbus/add-wifi-eap-connection.py index 0386084d..23962e76 100755 --- a/examples/python/dbus/add-wifi-eap-connection.py +++ b/examples/python/dbus/add-wifi-eap-connection.py @@ -50,10 +50,9 @@ con = dbus.Dictionary( "ipv6": s_ip6, } ) - +print("Creating connection:", s_con["id"], "-", s_con["uuid"]) bus = dbus.SystemBus() - proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" ) diff --git a/examples/python/dbus/add-wifi-psk-connection.py b/examples/python/dbus/add-wifi-psk-connection.py index 3e4f9da5..66d42e82 100755 --- a/examples/python/dbus/add-wifi-psk-connection.py +++ b/examples/python/dbus/add-wifi-psk-connection.py @@ -30,10 +30,9 @@ con = dbus.Dictionary( "ipv6": s_ip6, } ) - +print("Creating connection:", s_con["id"], "-", s_con["uuid"]) bus = dbus.SystemBus() - proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" ) diff --git a/examples/python/dbus/create-bond.py b/examples/python/dbus/create-bond.py index 458c9c06..77c2ece4 100755 --- a/examples/python/dbus/create-bond.py +++ b/examples/python/dbus/create-bond.py @@ -13,7 +13,7 @@ import dbus, sys, uuid from dbus.mainloop.glib import DBusGMainLoop -from gi.repository import GObject +from gi.repository import GLib DBusGMainLoop(set_as_default=True) @@ -94,7 +94,7 @@ ac = manager.ActivateConnection(bond_path, "/", "/") print("Activating bond: %s (%s)" % (bond_name, ac)) # Monitor the active bond connection -loop = GObject.MainLoop() +loop = GLib.MainLoop() def properties_changed(props): diff --git a/examples/python/dbus/get-active-connection-uuids.py b/examples/python/dbus/get-active-connection-uuids.py index e138ce60..3e80b603 100755 --- a/examples/python/dbus/get-active-connection-uuids.py +++ b/examples/python/dbus/get-active-connection-uuids.py @@ -6,9 +6,8 @@ import dbus -# This example takes a device interface name as a parameter and tells -# NetworkManager to disconnect that device, closing down any network -# connection it may have +# This example lists all of the active connections +# the system is connected to and prints it out bus = dbus.SystemBus() @@ -16,23 +15,15 @@ bus = dbus.SystemBus() m_proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager" ) -manager = dbus.Interface(m_proxy, "org.freedesktop.NetworkManager") mgr_props = dbus.Interface(m_proxy, "org.freedesktop.DBus.Properties") -s_proxy = bus.get_object( - "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" -) -settings = dbus.Interface(s_proxy, "org.freedesktop.NetworkManager.Settings") - -# Find the device the user wants to disconnect +# Find all active connections active = mgr_props.Get("org.freedesktop.NetworkManager", "ActiveConnections") + for a in active: a_proxy = bus.get_object("org.freedesktop.NetworkManager", a) - # Get the UUID directly; apps could use this to perform certain operations - # based on which network you're connected too a_props = dbus.Interface(a_proxy, "org.freedesktop.DBus.Properties") - uuid = a_props.Get("org.freedesktop.NetworkManager.Connection.Active", "Uuid") # Grab the connection object path so we can get all the connection's settings connection_path = a_props.Get( @@ -45,7 +36,11 @@ for a in active: settings = connection.GetSettings() print( "%s (%s) - %s" - % (settings["connection"]["id"], uuid, settings["connection"]["type"]) + % ( + settings["connection"]["id"], + settings["connection"]["uuid"], + settings["connection"]["type"], + ) ) if len(active) == 0: diff --git a/examples/python/dbus/is-wwan-default.py b/examples/python/dbus/is-wwan-default.py index 38dc1735..18279db0 100755 --- a/examples/python/dbus/is-wwan-default.py +++ b/examples/python/dbus/is-wwan-default.py @@ -8,30 +8,24 @@ import dbus, sys # This example indicates whether the default network connection is known to be WWAN -NM_DEVICE_TYPE_MODEM = 8 -NM_DEVICE_TYPE_BLUETOOTH = 5 -NM_SERVICE_NAME = "org.freedesktop.NetworkManager" -NM_MANAGER_IFACE = "org.freedesktop.NetworkManager" -DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties" - -NM_ACTIVE_CONNECTION_INTERFACE = "org.freedesktop.NetworkManager.Connection.Active" - bus = dbus.SystemBus() # Exit early if NetworkManager is not running proxy = bus.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus") busdaemon = dbus.Interface(proxy, "org.freedesktop.DBus") -if not busdaemon.NameHasOwner(NM_SERVICE_NAME): +if not busdaemon.NameHasOwner("org.freedesktop.NetworkManager"): print("NetworkManager not running") sys.exit(1) # Get a proxy for the NetworkManager object -proxy = bus.get_object(NM_SERVICE_NAME, "/org/freedesktop/NetworkManager") -manager = dbus.Interface(proxy, NM_MANAGER_IFACE) -props = dbus.Interface(proxy, DBUS_PROPS_IFACE) +proxy = bus.get_object( + "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager" +) +props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") - -def found_connection_type(ctype): +# Shortcut #1, for NM 1.0 +try: + ctype = props.Get("org.freedesktop.NetworkManager", "PrimaryConnectionType") if ctype == "": print("No active connection") elif ctype in ["gsm", "cdma", "bluetooth"]: @@ -40,48 +34,5 @@ def found_connection_type(ctype): print("WWAN is not default") sys.exit(0) - -# Shortcut #1, for NM 1.0 -try: - ctype = props.Get(NM_MANAGER_IFACE, "PrimaryConnectionType") - found_connection_type(ctype) -except KeyError: - pass - -# Shortcut #2, for NM 0.9.10 -try: - primary = props.Get(NM_MANAGER_IFACE, "PrimaryConnection") - if not primary: - found_connection_type("") - primary_proxy = bus.get_object(NM_SERVICE_NAME, primary) - primary_props = dbus.Interface(primary_proxy, DBUS_PROPS_IFACE) - ctype = primary_props.Get(NM_ACTIVE_CONNECTION_INTERFACE, "Type") - found_connection_type(ctype) except KeyError: pass - -# Fallback for NM 0.9.8 and earlier; look through all active network -# connections for the default one -default_is_wwan = False -active = props.Get(NM_MANAGER_IFACE, "ActiveConnections") -for a in active: - a_proxy = bus.get_object(NM_SERVICE_NAME, a) - a_props = dbus.Interface(a_proxy, DBUS_PROPS_IFACE) - all_props = a_props.GetAll(NM_ACTIVE_CONNECTION_INTERFACE) - - # Ignore this network connection if it's not default for IPv4 or IPv6 - if all_props["Default"] == False and all_props["Default6"] == False: - continue - - dev_path = all_props["Devices"][0] - dev_proxy = bus.get_object(NM_SERVICE_NAME, dev_path) - dev_props = dbus.Interface(dev_proxy, DBUS_PROPS_IFACE) - devtype = dev_props.Get("org.freedesktop.NetworkManager.Device", "DeviceType") - if devtype == NM_DEVICE_TYPE_MODEM or devtype == NM_DEVICE_TYPE_BLUETOOTH: - default_is_wwan = True - break - -if default_is_wwan: - print("WWAN is default") -else: - print("WWAN is not default") diff --git a/examples/python/dbus/list-connections.py b/examples/python/dbus/list-connections.py index 2e096b63..81b32896 100755 --- a/examples/python/dbus/list-connections.py +++ b/examples/python/dbus/list-connections.py @@ -18,7 +18,6 @@ def merge_secrets(proxy, config, setting_name): # returns a dict of dicts mapping name::setting, where setting is a dict # mapping key::value. Each member of the 'setting' dict is a secret secrets = proxy.GetSecrets(setting_name) - # Copy the secrets into our connection config for setting in secrets: for key in secrets[setting]: diff --git a/examples/python/dbus/list-devices.py b/examples/python/dbus/list-devices.py index 882841e6..910bc461 100755 --- a/examples/python/dbus/list-devices.py +++ b/examples/python/dbus/list-devices.py @@ -4,11 +4,17 @@ # Copyright (C) 2011 - 2012 Red Hat, Inc. # -import dbus +import dbus, socket, struct # This example lists basic information about network interfaces known to NM # For the types see include/NetworkManager.h + + +def int_to_ip(ip_int): + return socket.inet_ntoa(struct.pack("=I", ip_int)) + + devtypes = { 1: "Ethernet", 2: "Wi-Fi", @@ -56,19 +62,23 @@ manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager") # Get all devices known to NM and print their properties devices = manager.GetDevices() + for d in devices: dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d) prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties") props = prop_iface.GetAll("org.freedesktop.NetworkManager.Device") + print("============================") print("Interface: %s" % props["Interface"]) + print("Ip 4 Address: %s" % int_to_ip(props["Ip4Address"])) + print("Ip 4 Config: %s" % props["Ip4Config"]) + try: devtype = devtypes[props["DeviceType"]] except KeyError: devtype = "Unknown" print("Type: %s" % devtype) - print("Driver: %s" % props["Driver"]) try: diff --git a/examples/python/dbus/nm-state.py b/examples/python/dbus/nm-state.py index c380bd09..4bc9285e 100755 --- a/examples/python/dbus/nm-state.py +++ b/examples/python/dbus/nm-state.py @@ -68,7 +68,6 @@ for d in devices: else: print("Device %s is not activated (state=%s)" % (name, device_states[state])) - # Get active connection state manager_prop_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") active = manager_prop_iface.Get("org.freedesktop.NetworkManager", "ActiveConnections") @@ -90,4 +89,4 @@ for a in active: con_details = con_iface.GetSettings() con_name = con_details["connection"]["id"] - print("Connection '%s' is %s" % (con_name, connectivity_states[state].lower())) + print("Connection '%s' is %s" % (con_name, connectivity_states[state])) diff --git a/examples/python/dbus/show-bssids.py b/examples/python/dbus/show-bssids.py index 1e7181fe..b56822fb 100755 --- a/examples/python/dbus/show-bssids.py +++ b/examples/python/dbus/show-bssids.py @@ -4,7 +4,6 @@ # Copyright (C) 2010 Red Hat, Inc. # - # This example prints out all the AP BSSIDs that all Wi-Fi devices on the # machine can see. Useful for location-based services like Skyhook that # can geolocate you based on the APs you can see. diff --git a/examples/python/dbus/update-secrets.py b/examples/python/dbus/update-secrets.py index d5c20b2d..5df25e5c 100755 --- a/examples/python/dbus/update-secrets.py +++ b/examples/python/dbus/update-secrets.py @@ -13,8 +13,7 @@ # values, and add them to the settings that we pass to Update(). # -import dbus -import sys +import dbus, sys bus = dbus.SystemBus() diff --git a/examples/python/dbus/vpn.py b/examples/python/dbus/vpn.py index ed381372..814e4415 100755 --- a/examples/python/dbus/vpn.py +++ b/examples/python/dbus/vpn.py @@ -8,17 +8,15 @@ # Run this script without any arguments to list the available connection uuids. # The uuid of the connection to activate -CONNECTION_UUID = "ac6dc9b2-85ef-4311-83d8-add5d7db3f59" +CONNECTION_UUID = "c08142a4-00d9-45bd-a3b1-7610fe146374" # UID to use. Note that NM only allows the owner of the connection to activate it. # UID=1000 UID = 0 -import sys -import os -import dbus +import sys, os, dbus from dbus.mainloop.glib import DBusGMainLoop -import gobject +from gi.repository import GLib DBusGMainLoop(set_as_default=True) @@ -156,5 +154,5 @@ if get_active_connection_path(CONNECTION_UUID): print("Activating connection...") activate_connection(connection_path, device_path) -loop = gobject.MainLoop() +loop = GLib.MainLoop() loop.run() diff --git a/examples/python/dbus/wifi-hotspot.py b/examples/python/dbus/wifi-hotspot.py index a3c573a6..77582b6a 100755 --- a/examples/python/dbus/wifi-hotspot.py +++ b/examples/python/dbus/wifi-hotspot.py @@ -50,22 +50,15 @@ def usage(): bus = dbus.SystemBus() -service_name = "org.freedesktop.NetworkManager" -proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings") +proxy = bus.get_object( + "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" +) settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings") -if len(sys.argv) != 3: - usage() - -iface = sys.argv[1] -proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager") -nm = dbus.Interface(proxy, "org.freedesktop.NetworkManager") -devpath = nm.GetDeviceByIpIface(iface) - # Find our existing hotspot connection connection_path = None for path in settings.ListConnections(): - proxy = bus.get_object(service_name, path) + proxy = bus.get_object("org.freedesktop.NetworkManager", path) settings_connection = dbus.Interface( proxy, "org.freedesktop.NetworkManager.Settings.Connection" ) @@ -78,13 +71,25 @@ for path in settings.ListConnections(): if not connection_path: connection_path = settings.AddConnection(con) + +if len(sys.argv) != 3: + usage() + +# Get device using iface +iface = sys.argv[1] +proxy = bus.get_object( + "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager" +) +nm = dbus.Interface(proxy, "org.freedesktop.NetworkManager") +devpath = nm.GetDeviceByIpIface(iface) + # Now start or stop the hotspot on the requested device -proxy = bus.get_object(service_name, devpath) +proxy = bus.get_object("org.freedesktop.NetworkManager", devpath) device = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Device") operation = sys.argv[2] if operation == "up": acpath = nm.ActivateConnection(connection_path, devpath, "/") - proxy = bus.get_object(service_name, acpath) + proxy = bus.get_object("org.freedesktop.NetworkManager", acpath) active_props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") # Wait for the hotspot to start up |