static GtkWidget * info_dialog_update (NMApplet *applet) { GtkNotebook *notebook; const GPtrArray *connections; int i; int pages = 0; notebook = GTK_NOTEBOOK (glade_xml_get_widget (applet->info_dialog_xml, "info_notebook")); /* Remove old pages */ for (i = gtk_notebook_get_n_pages (notebook); i > 0; i--) gtk_notebook_remove_page (notebook, -1); /* Add new pages */ connections = nm_client_get_active_connections (applet->nm_client); for (i = 0; connections && (i < connections->len); i++) { NMActiveConnection *active_connection = g_ptr_array_index (connections, i); NMConnection *connection; const GPtrArray *devices; if (nm_active_connection_get_state (active_connection) != NM_ACTIVE_CONNECTION_STATE_ACTIVATED) continue; devices = nm_active_connection_get_devices (active_connection); if (!devices || !devices->len) { g_warning ("Active connection %s had no devices!", nm_object_get_path (NM_OBJECT (active_connection))); continue; } connection = get_connection_for_active (applet, active_connection); if (!connection) { g_warning ("%s: couldn't find the default active connection's NMConnection!", __func__); continue; } info_dialog_add_page (notebook, connection, nm_active_connection_get_default (active_connection), g_ptr_array_index (devices, 0)); pages++; } if (pages == 0) { /* Shouldn't really happen but ... */ info_dialog_show_error (_("No valid active connections found!")); return NULL; } return glade_xml_get_widget (applet->info_dialog_xml, "info_dialog"); }
static void test_get_active_connections (NMClient *client) { const GPtrArray *connections; int i; g_print ("Active connections:\n"); connections = nm_client_get_active_connections (client); for (i = 0; connections && (i < connections->len); i++) { const GPtrArray *devices; g_print (" %s\n", nm_object_get_path (g_ptr_array_index (connections, i))); devices = nm_active_connection_get_devices (g_ptr_array_index (connections, i)); if (devices) g_ptr_array_foreach ((GPtrArray *) devices, show_active_connection_device, NULL); } }
static void active_connections_changed (NMClient *client, GParamSpec *pspec, GisNetworkPage *page) { const GPtrArray *connections; guint i; connections = nm_client_get_active_connections (client); for (i = 0; connections && (i < connections->len); i++) { NMActiveConnection *connection; connection = g_ptr_array_index (connections, i); if (!g_object_get_data (G_OBJECT (connection), "has-state-changed-handler")) { g_signal_connect (connection, "notify::state", G_CALLBACK (connection_state_changed), page); g_object_set_data (G_OBJECT (connection), "has-state-changed-handler", GINT_TO_POINTER (1)); } } refresh_wireless_list (page); }
int main (int argc, char *argv[]) { NMClient *client; const GPtrArray *devices; struct cb_info info; g_type_init (); client = nm_client_new (); if (!client) { exit (1); } printf ("\nNetworkManager Tool\n\n"); if (!get_nm_state (client)) { g_warning ("error: could not connect to NetworkManager"); exit (1); } if (!get_all_connections ()) exit (1); info.client = client; info.active = nm_client_get_active_connections (client); devices = nm_client_get_devices (client); if (devices) g_ptr_array_foreach ((GPtrArray *) devices, detail_device, &info); if (info.active) g_ptr_array_foreach ((GPtrArray *) info.active, detail_vpn, &info); g_object_unref (client); g_hash_table_unref (user_connections); g_hash_table_unref (system_connections); return 0; }
static void active_connections_changed (NMClient *client, GParamSpec *pspec, gpointer user_data) { const GPtrArray *connections; int i, j; g_print ("Active connections changed:\n"); connections = nm_client_get_active_connections (client); for (i = 0; connections && (i < connections->len); i++) { NMActiveConnection *connection; const GPtrArray *devices; connection = g_ptr_array_index (connections, i); g_print (" %s\n", nm_object_get_path (NM_OBJECT (connection))); devices = nm_active_connection_get_devices (connection); for (j = 0; devices && j < devices->len; j++) g_print (" %s\n", nm_device_get_udi (g_ptr_array_index (devices, j))); if (NM_IS_VPN_CONNECTION (connection)) g_print (" VPN base connection: %s\n", nm_active_connection_get_specific_object (connection)); } }
/** * nm_client_get_active_connection_by_path: * @client: a #NMClient * @object_path: the object path to search for * * Gets a #NMActiveConnection from a #NMClient. * * Returns: the #NMActiveConnection for the given @object_path or %NULL if none is found. **/ static NMActiveConnection * nm_client_get_active_connection_by_path (NMClient *client, const char *object_path) { const GPtrArray *actives; int i; NMActiveConnection *active = NULL; g_return_val_if_fail (NM_IS_CLIENT (client), NULL); g_return_val_if_fail (object_path, NULL); actives = nm_client_get_active_connections (client); if (!actives) return NULL; for (i = 0; i < actives->len; i++) { NMActiveConnection *candidate = g_ptr_array_index (actives, i); if (!strcmp (nm_object_get_path (NM_OBJECT (candidate)), object_path)) { active = candidate; break; } } return active; }
static void nmt_connect_connection_list_rebuild (NmtConnectConnectionList *list) { NmtConnectConnectionListPrivate *priv = NMT_CONNECT_CONNECTION_LIST_GET_PRIVATE (list); NmtNewtListbox *listbox = NMT_NEWT_LISTBOX (list); const GPtrArray *devices, *acs, *connections; int max_width; char **names, *row, active_col; const char *strength_col; GSList *nmt_devices, *diter, *citer; NmtConnectDevice *nmtdev; NmtConnectConnection *nmtconn; g_slist_free_full (priv->nmt_devices, (GDestroyNotify) nmt_connect_device_free); priv->nmt_devices = NULL; nmt_newt_listbox_clear (listbox); devices = nm_client_get_devices (nm_client); acs = nm_client_get_active_connections (nm_client); connections = nm_client_get_connections (nm_client); nmt_devices = NULL; names = nm_device_disambiguate_names ((NMDevice **) devices->pdata, devices->len); nmt_devices = append_nmt_devices_for_devices (nmt_devices, devices, names, connections); g_strfreev (names); nmt_devices = append_nmt_devices_for_virtual_devices (nmt_devices, connections); nmt_devices = append_nmt_devices_for_vpns (nmt_devices, connections); nmt_devices = g_slist_sort (nmt_devices, sort_nmt_devices); max_width = 0; for (diter = nmt_devices; diter; diter = diter->next) { nmtdev = diter->data; for (citer = nmtdev->conns; citer; citer = citer->next) { nmtconn = citer->data; max_width = MAX (max_width, nmt_newt_text_width (nmtconn->name)); } } for (diter = nmt_devices; diter; diter = diter->next) { nmtdev = diter->data; if (diter != nmt_devices) nmt_newt_listbox_append (listbox, "", NULL); nmt_newt_listbox_append (listbox, nmtdev->name, NULL); for (citer = nmtdev->conns; citer; citer = citer->next) { nmtconn = citer->data; if (nmtconn->conn) nmtconn->active = connection_find_ac (nmtconn->conn, acs); if (nmtconn->active) { g_object_ref (nmtconn->active); active_col = '*'; } else active_col = ' '; if (nmtconn->ap) { guint8 strength = nm_access_point_get_strength (nmtconn->ap); strength_col = nm_utils_wifi_strength_bars (strength); } else strength_col = NULL; row = g_strdup_printf ("%c %s%-*s%s%s", active_col, nmtconn->name, (int)(max_width - nmt_newt_text_width (nmtconn->name)), "", strength_col ? " " : "", strength_col ? strength_col : ""); nmt_newt_listbox_append (listbox, row, nmtconn); g_free (row); } } priv->nmt_devices = nmt_devices; g_object_notify (G_OBJECT (listbox), "active"); g_object_notify (G_OBJECT (listbox), "active-key"); }
static void get_devices (NAApplication *application) { int i; NMClient *nm_client = nm_client_new (); const GPtrArray *nm_connections = nm_client_get_active_connections (nm_client); for (i = 0; i < nm_connections->len; i++) { NMActiveConnection *active_conn = NM_ACTIVE_CONNECTION (g_ptr_array_index (nm_connections, i)); g_debug ("Active connection: %s", nm_active_connection_get_connection (active_conn)); } const GPtrArray *nm_devices = nm_client_get_devices (nm_client); for (i = 0; i < nm_devices->len; i++) { NMDevice *nm_device = NM_DEVICE (g_ptr_array_index (nm_devices, i)); if (nm_device_get_state (nm_device) == NM_DEVICE_STATE_ACTIVATED) { const char *iface = nm_device_get_iface (nm_device); const char *ip_iface = nm_device_get_ip_iface (nm_device); g_debug ("Active device found: %s with ip iface %s", iface, ip_iface); NMIP4Config *ip4_config = nm_device_get_ip4_config (nm_device); if (ip4_config != NULL) { const GSList *ip4_addresses = nm_ip4_config_get_addresses (ip4_config); const GSList *iter = ip4_addresses; while (iter != NULL) { guint32 address = nm_ip4_address_get_address ((NMIP4Address *)iter->data); struct in_addr addr = { address, }; char *str_addr = g_new (char, INET_ADDRSTRLEN); inet_ntop (AF_INET, &addr, str_addr, INET_ADDRSTRLEN); add_local_address (str_addr); g_debug ("Adding local IP4 address %s", str_addr); iter = iter->next; } } NMIP6Config *ip6_config = nm_device_get_ip6_config (nm_device); if (ip6_config != NULL) { const GSList *ip6_addresses = nm_ip6_config_get_addresses (ip6_config); const GSList *iter = ip6_addresses; while (iter != NULL) { const struct in6_addr *address = nm_ip6_address_get_address ((NMIP6Address *)iter->data); char *str_addr = g_new (char, INET6_ADDRSTRLEN); inet_ntop (AF_INET6, address, str_addr, INET6_ADDRSTRLEN); add_local_address (str_addr); g_debug ("Adding local IP6 address %s", str_addr); iter = iter->next; } } GError *error = NULL; NAPCapHandle *handle = na_pcap_open (iface, &error); if (handle != NULL) { application->priv->iface_handles = g_slist_append (application->priv->iface_handles, handle); } else { g_debug ("Error opening handler for interface %s: %s\n", iface, error->message); g_error_free (error); } } } }