static NMDevice * find_device_by_mac_address (NmtDeviceEntry *deventry, const char *mac_address) { NmtDeviceEntryPrivate *priv = NMT_DEVICE_ENTRY_GET_PRIVATE (deventry); const GPtrArray *devices; NMDevice *device = NULL; int i; devices = nm_client_get_devices (nm_client); for (i = 0; i < devices->len && !device; i++) { NMDevice *candidate = devices->pdata[i]; char *hwaddr; if ( priv->hardware_type != G_TYPE_NONE && !G_TYPE_CHECK_INSTANCE_TYPE (candidate, priv->hardware_type)) continue; if ( priv->device_filter && !priv->device_filter (deventry, candidate, priv->device_filter_data)) continue; g_object_get (G_OBJECT (candidate), "hw-address", &hwaddr, NULL); if (hwaddr && !g_ascii_strcasecmp (mac_address, hwaddr)) device = candidate; g_free (hwaddr); } return device; }
static void update_switches_visibility (NmnApplet *applet) { NmnAppletPrivate *priv = GET_PRIVATE (applet); GtkTable *table; gboolean visible; int ethernet; int wifi; int modem; int wimax; int bt; ethernet = wifi = modem = wimax = bt = 0; if (!nmn_model_offline_mode_get_active (priv->model)) { const GPtrArray *devices; int i; devices = nm_client_get_devices (nmn_model_get_client (priv->model)); for (i = 0; devices && i < devices->len; i++) { NMDevice *device = NM_DEVICE (g_ptr_array_index (devices, i)); if (NM_IS_DEVICE_ETHERNET (device)) ethernet++; else if (NM_IS_DEVICE_WIFI (device)) wifi++; else if (NM_IS_SERIAL_DEVICE (device)) modem++; else if (NM_IS_DEVICE_BT (device)) bt++; } } table = GTK_TABLE (priv->switches_table); visible = wifi > 0; gtk_table_set_row_spacing (table, SWITCHES_ROW_WIFI, visible ? 6 : 0); g_object_set (priv->enable_wifi_label, "visible", visible, NULL); g_object_set (priv->enable_wifi, "visible", visible, NULL); visible = ethernet > 0; gtk_table_set_row_spacing (table, SWITCHES_ROW_ETHERNET, visible ? 6 : 0); g_object_set (priv->enable_ethernet_label, "visible", visible, NULL); g_object_set (priv->enable_ethernet, "visible", visible, NULL); visible = modem > 0; gtk_table_set_row_spacing (table, SWITCHES_ROW_3G, visible ? 6 : 0); g_object_set (priv->enable_3g_label, "visible", visible, NULL); g_object_set (priv->enable_3g, "visible", visible, NULL); visible = wimax > 0; gtk_table_set_row_spacing (table, SWITCHES_ROW_WIMAX, visible ? 6 : 0); g_object_set (priv->enable_wimax_label, "visible", visible, NULL); g_object_set (priv->enable_wimax, "visible", visible, NULL); visible = bt > 0; gtk_table_set_row_spacing (table, SWITCHES_ROW_BT, visible ? 6 : 0); g_object_set (priv->enable_bt_label, "visible", visible, NULL); g_object_set (priv->enable_bt, "visible", visible, NULL); }
static NMDevice * find_device_by_interface_name (NmtDeviceEntry *deventry, const char *interface_name) { NmtDeviceEntryPrivate *priv = NMT_DEVICE_ENTRY_GET_PRIVATE (deventry); const GPtrArray *devices; NMDevice *device = NULL; int i; devices = nm_client_get_devices (nm_client); for (i = 0; i < devices->len && !device; i++) { NMDevice *candidate = devices->pdata[i]; if ( priv->hardware_type != G_TYPE_NONE && !G_TYPE_CHECK_INSTANCE_TYPE (candidate, priv->hardware_type)) continue; if ( priv->device_filter && !priv->device_filter (deventry, candidate, priv->device_filter_data)) continue; if (!g_strcmp0 (interface_name, nm_device_get_iface (candidate))) device = candidate; } return device; }
static NMDevice * find_vlan_parent (GSList *connections, NMApplet *applet) { const GPtrArray *devices; NMDevice *parent_device; GSList *iter; devices = nm_client_get_devices (applet->nm_client); if (!devices) return NULL; for (iter = connections; iter; iter = iter->next) { NMConnection *connection = iter->data; NMSettingVlan *s_vlan; const char *parent; s_vlan = nm_connection_get_setting_vlan (connection); g_return_val_if_fail (s_vlan != NULL, NULL); parent = nm_setting_vlan_get_parent (s_vlan); if (parent && nm_utils_iface_valid_name (parent)) { parent_device = find_device_by_iface (parent, devices); } else { NMSettingConnection *s_con; NMSetting *s_hw; const char *type; GByteArray *mac; s_con = nm_connection_get_setting_connection (connection); type = nm_setting_connection_get_connection_type (s_con); s_hw = nm_connection_get_setting_by_name (connection, type); if (!s_hw) { g_warn_if_reached (); continue; } if (!g_object_class_find_property (G_OBJECT_GET_CLASS (s_hw), "mac-address")) continue; g_object_get (G_OBJECT (s_hw), "mac-address", &mac, NULL); if (mac) { parent_device = find_device_by_mac (mac, devices); g_byte_array_unref (mac); } else parent_device = NULL; } if (parent_device) return parent_device; } return NULL; }
static gboolean device_combo_init (NMAWirelessDialog *self, NMDevice *device) { NMAWirelessDialogPrivate *priv = NMA_WIRELESS_DIALOG_GET_PRIVATE (self); const GPtrArray *devices; GtkListStore *store; int i, num_added = 0; g_return_val_if_fail (priv->device == NULL, FALSE); store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT); priv->device_model = GTK_TREE_MODEL (store); if (device) { if (!can_use_device (device)) return FALSE; add_device_to_model (store, device); num_added++; } else { devices = nm_client_get_devices (priv->client); if (devices->len == 0) return FALSE; for (i = 0; devices && (i < devices->len); i++) { device = NM_DEVICE (g_ptr_array_index (devices, i)); if (can_use_device (device)) { add_device_to_model (store, device); num_added++; } } } if (num_added > 0) { GtkWidget *widget; GtkTreeIter iter; widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "device_combo")); gtk_combo_box_set_model (GTK_COMBO_BOX (widget), priv->device_model); gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 0); g_signal_connect (G_OBJECT (widget), "changed", G_CALLBACK (device_combo_changed), self); if (num_added == 1) { gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->builder, "device_label"))); gtk_widget_hide (widget); } gtk_tree_model_get_iter_first (priv->device_model, &iter); gtk_tree_model_get (priv->device_model, &iter, D_DEV_COLUMN, &priv->device, -1); } return num_added > 0 ? TRUE : FALSE; }
static NMDevice * get_device_for_connection (NMClient *client, NMConnection *connection) { int i; const GPtrArray *devices = nm_client_get_devices (client); for (i = 0; devices && (i < devices->len); i++) { NMDevice *dev = g_ptr_array_index (devices, i); if (nm_device_is_connection_compatible (dev, connection, NULL)) { return dev; } } return NULL; }
static void on_nm_settings_read (NMRemoteSettings *settings, Network *self) { const GPtrArray *devices = NULL; guint i; devices = nm_client_get_devices (self->client); if (devices) { for (i = 0; i < devices->len; i++) { NMDevice *device = devices->pdata[i]; on_nm_device_added (self->client, device, self); } } g_signal_connect (self->client, "device-added", G_CALLBACK (on_nm_device_added), self); g_signal_connect (self->client, "device-removed", G_CALLBACK (on_nm_device_removed), self); }
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 gboolean test_devices (NMClient *client) { const GPtrArray *devices; int i; devices = nm_client_get_devices (client); g_print ("Got devices:\n"); if (!devices) { g_print (" NONE\n"); return TRUE; } for (i = 0; i < devices->len; i++) { NMDevice *device = g_ptr_array_index (devices, i); dump_device (device); g_print ("\n"); } return TRUE; }
static GSList * get_vlan_devices (CEPageVlan *self) { const GPtrArray *devices_array; GSList *devices; NMDevice *device; int i; devices_array = nm_client_get_devices (CE_PAGE (self)->client); devices = NULL; for (i = 0; devices_array && (i < devices_array->len); i++) { device = devices_array->pdata[i]; /* FIXME: this supported-device-types logic belongs in NM somewhere. */ if (!NM_IS_DEVICE_ETHERNET (device)) continue; devices = g_slist_prepend (devices, device); } return devices; }
int main (int argc, char *argv[]) { NMClient *client; GError *error = NULL; const GPtrArray *devices; int i; #if !GLIB_CHECK_VERSION (2, 35, 0) /* Initialize GType system */ g_type_init (); #endif 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 devices: NetworkManager is not running."); return EXIT_FAILURE; } /* Now the devices can be listed. */ devices = nm_client_get_devices (client); printf ("Devices:\n============\n"); for (i = 0; i < devices->len; i++) show_device (devices->pdata[i]); g_object_unref (client); return EXIT_SUCCESS; }
int main (int argc, char *argv[]) { DBusGConnection *bus; NMClient *client; const GPtrArray *devices; int i; /* Initialize GType system */ g_type_init (); /* Get system bus */ bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL); /* Get NMClient object */ client = nm_client_new (); if (!client) { dbus_g_connection_unref (bus); g_message ("Error: Could not create NMClient."); return EXIT_FAILURE; } /* Get all devices managed by NetworkManager */ devices = nm_client_get_devices (client); /* Go through the array and process Wi-Fi devices */ for (i = 0; devices && (i < devices->len); i++) { NMDevice *device = g_ptr_array_index (devices, i); if (NM_IS_DEVICE_WIFI (device)) show_wifi_device_info (device); } g_object_unref (client); dbus_g_connection_unref (bus); return EXIT_SUCCESS; }
static void gis_network_page_constructed (GObject *object) { GisNetworkPage *page = GIS_NETWORK_PAGE (object); GisNetworkPagePrivate *priv = gis_network_page_get_instance_private (page); const GPtrArray *devices; NMDevice *device; guint i; gboolean visible = FALSE; G_OBJECT_CLASS (gis_network_page_parent_class)->constructed (object); priv->icons = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); priv->nm_client = nm_client_new (); g_object_bind_property (priv->nm_client, "wireless-enabled", priv->turn_on_switch, "active", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); devices = nm_client_get_devices (priv->nm_client); if (devices) { for (i = 0; i < devices->len; i++) { device = g_ptr_array_index (devices, i); if (!nm_device_get_managed (device)) continue; if (nm_device_get_device_type (device) == NM_DEVICE_TYPE_WIFI) { /* FIXME deal with multiple, dynamic devices */ priv->nm_device = g_object_ref (device); break; } } } if (priv->nm_device == NULL) { g_debug ("No network device found, hiding network page"); goto out; } if (nm_device_get_state (priv->nm_device) == NM_DEVICE_STATE_ACTIVATED) { g_debug ("Activated network device found, hiding network page"); goto out; } visible = TRUE; priv->nm_settings = nm_remote_settings_new (NULL); g_signal_connect (priv->nm_device, "notify::state", G_CALLBACK (device_state_changed), page); g_signal_connect (priv->nm_client, "notify::active-connections", G_CALLBACK (active_connections_changed), page); gtk_list_box_set_selection_mode (GTK_LIST_BOX (priv->network_list), GTK_SELECTION_NONE); gtk_list_box_set_header_func (GTK_LIST_BOX (priv->network_list), update_header_func, NULL, NULL); gtk_list_box_set_sort_func (GTK_LIST_BOX (priv->network_list), ap_sort, NULL, NULL); g_signal_connect (priv->network_list, "row-activated", G_CALLBACK (row_activated), page); refresh_wireless_list (page); sync_complete (page); gis_page_set_skippable (GIS_PAGE (page), TRUE); out: gtk_widget_set_visible (GTK_WIDGET (page), visible); }
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"); }
/* * Given an interface name (e.g., eth0) and address family (e.g., AF_INET), * return the IP address in human readable format (i.e., the output from * inet_ntop()). Return NULL for no match or error. */ char *iface_ip2str(char *ifname, int family) { int i; NMClient *client = NULL; NMIP4Config *ip4config = NULL; NMIP4Address *ipaddr = NULL; NMDevice *candidate = NULL; struct in_addr tmp_addr; const GPtrArray *devices; const char *iface; char ipstr[INET_ADDRSTRLEN+1]; if (ifname == NULL) { return NULL; } /* DCFIXME: add IPv6 once NM gains support */ if (family != AF_INET) { return NULL; } #if !defined(GLIB_VERSION_2_36) g_type_init(); #endif client = nm_client_new(); if (!client) { return NULL; } if (nm_client_get_state(client) != NM_STATE_CONNECTED) { g_object_unref(client); return NULL; } devices = nm_client_get_devices(client); for (i=0; i < devices->len; i++) { candidate = g_ptr_array_index(devices, i); iface = nm_device_get_iface(candidate); if (nm_device_get_state(candidate) != NM_DEVICE_STATE_ACTIVATED) continue; if (!iface || strcmp(iface, ifname)) continue; if (!(ip4config = nm_device_get_ip4_config(candidate))) continue; if (!(ipaddr = nm_ip4_config_get_addresses(ip4config)->data)) continue; memset(&ipstr, '\0', sizeof(ipstr)); tmp_addr.s_addr = nm_ip4_address_get_address(ipaddr); if (inet_ntop(AF_INET, &tmp_addr, ipstr, INET_ADDRSTRLEN) == NULL) { g_object_unref(client); return NULL; } g_object_unref(client); return g_strdup(ipstr); } g_object_unref(client); return NULL; }
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); } } } }