static void print_ip4_addr (gpointer object, gpointer user_data) { NMIP4Address * address = (NMIP4Address *) object; guint32 addr = nm_ip4_address_get_address (address); guint32 prefix = nm_ip4_address_get_prefix (address); guint32 netmask = nm_utils_ip4_prefix_to_netmask (prefix); guint32 gateway = nm_ip4_address_get_gateway (address); struct in_addr tmp_addr; char buf[INET_ADDRSTRLEN + 1]; g_print ("%-9s ", ""); tmp_addr.s_addr = addr; inet_ntop (AF_INET, &tmp_addr, buf, sizeof (buf)); g_print ("IPv4:%s ", buf); tmp_addr.s_addr = netmask; inet_ntop (AF_INET, &tmp_addr, buf, sizeof (buf)); g_print ("Netmask:%s ", buf); tmp_addr.s_addr = gateway; inet_ntop (AF_INET, &tmp_addr, buf, sizeof (buf)); g_print ("Gateway:%s\n", buf); }
gchar * panel_get_ip4_address_as_string (NMIP4Config *ip4_config, const char *what) { const GSList *list; struct in_addr addr; gchar *str = NULL; gchar tmp[INET_ADDRSTRLEN]; NMIP4Address *address; /* get address */ list = nm_ip4_config_get_addresses (ip4_config); if (list == NULL) goto out; /* we only care about one address */ address = list->data; if (!strcmp (what, "address")) addr.s_addr = nm_ip4_address_get_address (address); else if (!strcmp (what, "gateway")) addr.s_addr = nm_ip4_address_get_gateway (address); else if (!strcmp (what, "netmask")) addr.s_addr = nm_utils_ip4_prefix_to_netmask (nm_ip4_address_get_prefix (address)); else goto out; if (!inet_ntop (AF_INET, &addr, tmp, sizeof(tmp))) goto out; if (g_strcmp0 (tmp, "0.0.0.0") == 0) goto out; str = g_strdup (tmp); out: return str; }
void nm_ip4_config_hash (NMIP4Config *config, GChecksum *sum, gboolean dns_only) { guint32 i, n; const char *s; g_return_if_fail (config != NULL); g_return_if_fail (sum != NULL); if (dns_only == FALSE) { for (i = 0; i < nm_ip4_config_get_num_addresses (config); i++) { NMIP4Address *a = nm_ip4_config_get_address (config, i); hash_u32 (sum, nm_ip4_address_get_address (a)); hash_u32 (sum, nm_ip4_address_get_prefix (a)); hash_u32 (sum, nm_ip4_address_get_gateway (a)); } for (i = 0; i < nm_ip4_config_get_num_routes (config); i++) { NMIP4Route *r = nm_ip4_config_get_route (config, i); hash_u32 (sum, nm_ip4_route_get_dest (r)); hash_u32 (sum, nm_ip4_route_get_prefix (r)); hash_u32 (sum, nm_ip4_route_get_next_hop (r)); hash_u32 (sum, nm_ip4_route_get_metric (r)); } n = nm_ip4_config_get_ptp_address (config); if (n) hash_u32 (sum, n); for (i = 0; i < nm_ip4_config_get_num_nis_servers (config); i++) hash_u32 (sum, nm_ip4_config_get_nis_server (config, i)); s = nm_ip4_config_get_nis_domain (config); if (s) g_checksum_update (sum, (const guint8 *) s, strlen (s)); } for (i = 0; i < nm_ip4_config_get_num_nameservers (config); i++) hash_u32 (sum, nm_ip4_config_get_nameserver (config, i)); for (i = 0; i < nm_ip4_config_get_num_wins (config); i++) hash_u32 (sum, nm_ip4_config_get_wins (config, i)); for (i = 0; i < nm_ip4_config_get_num_domains (config); i++) { s = nm_ip4_config_get_domain (config, i); g_checksum_update (sum, (const guint8 *) s, strlen (s)); } for (i = 0; i < nm_ip4_config_get_num_searches (config); i++) { s = nm_ip4_config_get_search (config, i); g_checksum_update (sum, (const guint8 *) s, strlen (s)); } }
static void dump_ip4_config (NMIP4Config *cfg) { char *tmp; const GArray *array; const GPtrArray *ptr_array; GSList *iter; int i; for (iter = (GSList *) nm_ip4_config_get_addresses (cfg); iter; iter = g_slist_next (iter)) { NMIP4Address *addr = iter->data; guint32 u; tmp = ip4_address_as_string (nm_ip4_address_get_address (addr)); g_print ("IP4 address: %s\n", tmp); g_free (tmp); u = nm_ip4_address_get_prefix (addr); tmp = ip4_address_as_string (nm_utils_ip4_prefix_to_netmask (u)); g_print ("IP4 prefix: %d (%s)\n", u, tmp); g_free (tmp); tmp = ip4_address_as_string (nm_ip4_address_get_gateway (addr)); g_print ("IP4 gateway: %s\n\n", tmp); g_free (tmp); } array = nm_ip4_config_get_nameservers (cfg); if (array) { g_print ("IP4 DNS:\n"); for (i = 0; i < array->len; i++) { tmp = ip4_address_as_string (g_array_index (array, guint32, i)); g_print ("\t%s\n", tmp); g_free (tmp); } } ptr_array = nm_ip4_config_get_domains (cfg); if (ptr_array) { g_print ("IP4 domains:\n"); for (i = 0; i < ptr_array->len; i++) g_print ("\t%s\n", (const char *) g_ptr_array_index (ptr_array, i)); } array = nm_ip4_config_get_wins_servers (cfg); if (array) { g_print ("IP4 WINS:\n"); for (i = 0; i < array->len; i++) { tmp = ip4_address_as_string (g_array_index (array, guint32, i)); g_print ("\t%s\n", tmp); g_free (tmp); } } }
static void test_gateway_in_classless_routes (void) { GHashTable *options; NMIP4Config *ip4_config; NMIP4Address *addr; NMIP4Route *route; struct in_addr tmp; const char *expected_route1_dest = "192.168.10.0"; const char *expected_route1_gw = "192.168.1.1"; const char *expected_gateway = "192.2.3.4"; options = fill_table (generic_options, NULL); options = fill_table (gw_in_classless_routes, options); ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-rfc3442-gateway", "failed to parse DHCP4 options"); /* IP4 routes */ ASSERT (nm_ip4_config_get_num_routes (ip4_config) == 1, "dhcp-rfc3442-gateway", "unexpected number of IP routes"); /* Route #1 */ route = nm_ip4_config_get_route (ip4_config, 0); ASSERT (inet_pton (AF_INET, expected_route1_dest, &tmp) > 0, "dhcp-rfc3442-gateway", "couldn't convert expected route destination #1"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "dhcp-rfc3442-gateway", "unexpected route #1 destination"); ASSERT (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0, "dhcp-rfc3442-gateway", "couldn't convert expected route next hop #1"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "dhcp-rfc3442-gateway", "unexpected route #1 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 24, "dhcp-rfc3442-gateway", "unexpected route #1 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "dhcp-rfc3442-gateway", "unexpected route #1 metric"); /* Address */ ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1, "dhcp-rfc3442-gateway", "unexpected number of IP addresses"); addr = nm_ip4_config_get_address (ip4_config, 0); ASSERT (inet_pton (AF_INET, expected_gateway, &tmp) > 0, "dhcp-rfc3442-gateway", "couldn't convert expected IP gateway"); ASSERT (nm_ip4_address_get_gateway (addr) == tmp.s_addr, "dhcp-rfc3442-gateway", "unexpected IP gateway"); g_hash_table_destroy (options); }
static void ip4_test_gateway (const char *test, NMIP4Config *ip4_config, const char *expected_gw) { NMIP4Address *addr; struct in_addr tmp; ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1, test, "unexpected number of IP addresses"); addr = nm_ip4_config_get_address (ip4_config, 0); ASSERT (inet_pton (AF_INET, expected_gw, &tmp) > 0, test, "couldn't convert expected IP gateway"); ASSERT (nm_ip4_address_get_gateway (addr) == tmp.s_addr, test, "unexpected IP gateway"); }
static void add_address_section (CEPageIP4 *page) { GtkWidget *widget; GtkWidget *frame; GtkWidget *list; gint i; widget = GTK_WIDGET (gtk_builder_get_object (CE_PAGE (page)->builder, "address_section")); frame = gtk_frame_new (NULL); gtk_container_add (GTK_CONTAINER (widget), frame); page->address_list = list = gtk_list_box_new (); gtk_list_box_set_selection_mode (GTK_LIST_BOX (list), GTK_SELECTION_NONE); gtk_list_box_set_header_func (GTK_LIST_BOX (list), cc_list_box_update_header_func, NULL, NULL); gtk_list_box_set_sort_func (GTK_LIST_BOX (list), (GtkListBoxSortFunc)sort_first_last, NULL, NULL); gtk_container_add (GTK_CONTAINER (frame), list); add_section_toolbar (page, widget, G_CALLBACK (add_empty_address_row)); for (i = 0; i < nm_setting_ip4_config_get_num_addresses (page->setting); i++) { NMIP4Address *addr; struct in_addr tmp_addr; gchar address[INET_ADDRSTRLEN + 1]; gchar network[INET_ADDRSTRLEN + 1]; gchar gateway[INET_ADDRSTRLEN + 1]; addr = nm_setting_ip4_config_get_address (page->setting, i); if (!addr) continue; tmp_addr.s_addr = nm_ip4_address_get_address (addr); (void) inet_ntop (AF_INET, &tmp_addr, &address[0], sizeof (address)); tmp_addr.s_addr = nm_utils_ip4_prefix_to_netmask (nm_ip4_address_get_prefix (addr)); (void) inet_ntop (AF_INET, &tmp_addr, &network[0], sizeof (network)); tmp_addr.s_addr = nm_ip4_address_get_gateway (addr); (void) inet_ntop (AF_INET, &tmp_addr, &gateway[0], sizeof (gateway)); add_address_row (page, address, network, gateway); } if (nm_setting_ip4_config_get_num_addresses (page->setting) == 0) add_empty_address_row (page); gtk_widget_show_all (widget); }
static NMDevice * get_best_ip4_device (NMManager *manager, NMActRequest **out_req) { GSList *devices, *iter; NMDevice *best = NULL; int best_prio = G_MAXINT; g_return_val_if_fail (manager != NULL, NULL); g_return_val_if_fail (NM_IS_MANAGER (manager), NULL); g_return_val_if_fail (out_req != NULL, NULL); g_return_val_if_fail (*out_req == NULL, NULL); devices = nm_manager_get_devices (manager); for (iter = devices; iter; iter = g_slist_next (iter)) { NMDevice *dev = NM_DEVICE (iter->data); NMActRequest *req; NMConnection *connection; NMIP4Config *ip4_config; NMSettingIP4Config *s_ip4; int prio; guint i; gboolean can_default = FALSE; const char *method = NULL; if (nm_device_get_state (dev) != NM_DEVICE_STATE_ACTIVATED) continue; ip4_config = nm_device_get_ip4_config (dev); if (!ip4_config) continue; req = nm_device_get_act_request (dev); g_assert (req); connection = nm_act_request_get_connection (req); g_assert (connection); /* Never set the default route through an IPv4LL-addressed device */ s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG); if (s_ip4) method = nm_setting_ip4_config_get_method (s_ip4); if (s_ip4 && !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)) continue; /* Make sure at least one of this device's IP addresses has a gateway */ for (i = 0; i < nm_ip4_config_get_num_addresses (ip4_config); i++) { NMIP4Address *addr; addr = nm_ip4_config_get_address (ip4_config, i); if (nm_ip4_address_get_gateway (addr)) { can_default = TRUE; break; } } if (!can_default && !NM_IS_DEVICE_MODEM (dev)) continue; /* 'never-default' devices can't ever be the default */ if ( (s_ip4 && nm_setting_ip4_config_get_never_default (s_ip4)) || nm_ip4_config_get_never_default (ip4_config)) continue; prio = nm_device_get_priority (dev); if (prio > 0 && prio < best_prio) { best = dev; best_prio = prio; *out_req = req; } } return best; }
static void update_ip4_routing_and_dns (NMPolicy *policy, gboolean force_update) { NMDnsIPConfigType dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE; NMDevice *best = NULL; NMActRequest *best_req = NULL; NMDnsManager *dns_mgr; GSList *devices = NULL, *iter, *vpns; NMIP4Config *ip4_config = NULL; NMIP4Address *addr; const char *ip_iface = NULL; NMConnection *connection = NULL; NMSettingConnection *s_con = NULL; const char *connection_id; best = get_best_ip4_device (policy->manager, &best_req); if (!best) goto out; if (!force_update && (best == policy->default_device4)) goto out; /* If a VPN connection is active, it is preferred */ vpns = nm_vpn_manager_get_active_connections (policy->vpn_manager); for (iter = vpns; iter; iter = g_slist_next (iter)) { NMVPNConnection *candidate = NM_VPN_CONNECTION (iter->data); NMConnection *vpn_connection; NMSettingIP4Config *s_ip4; gboolean can_default = TRUE; NMVPNConnectionState vpn_state; /* If it's marked 'never-default', don't make it default */ vpn_connection = nm_vpn_connection_get_connection (candidate); g_assert (vpn_connection); /* Check the active IP4 config from the VPN service daemon */ ip4_config = nm_vpn_connection_get_ip4_config (candidate); if (ip4_config && nm_ip4_config_get_never_default (ip4_config)) can_default = FALSE; /* Check the user's preference from the NMConnection */ s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (vpn_connection, NM_TYPE_SETTING_IP4_CONFIG); if (s_ip4 && nm_setting_ip4_config_get_never_default (s_ip4)) can_default = FALSE; vpn_state = nm_vpn_connection_get_vpn_state (candidate); if (can_default && (vpn_state == NM_VPN_CONNECTION_STATE_ACTIVATED)) { NMIP4Config *parent_ip4; NMDevice *parent; ip_iface = nm_vpn_connection_get_ip_iface (candidate); connection = nm_vpn_connection_get_connection (candidate); addr = nm_ip4_config_get_address (ip4_config, 0); parent = nm_vpn_connection_get_parent_device (candidate); parent_ip4 = nm_device_get_ip4_config (parent); nm_system_replace_default_ip4_route_vpn (ip_iface, nm_ip4_address_get_gateway (addr), nm_vpn_connection_get_ip4_internal_gateway (candidate), nm_ip4_config_get_mss (ip4_config), nm_device_get_ip_iface (parent), nm_ip4_config_get_mss (parent_ip4)); dns_type = NM_DNS_IP_CONFIG_TYPE_VPN; } g_object_unref (candidate); } g_slist_free (vpns); /* The best device gets the default route if a VPN connection didn't */ if (!ip_iface || !ip4_config) { connection = nm_act_request_get_connection (best_req); ip_iface = nm_device_get_ip_iface (best); ip4_config = nm_device_get_ip4_config (best); g_assert (ip4_config); addr = nm_ip4_config_get_address (ip4_config, 0); nm_system_replace_default_ip4_route (ip_iface, nm_ip4_address_get_gateway (addr), nm_ip4_config_get_mss (ip4_config)); dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE; } if (!ip_iface || !ip4_config) { nm_log_warn (LOGD_CORE, "couldn't determine IP interface (%p) or IPv4 config (%p)!", ip_iface, ip4_config); goto out; } /* Update the default active connection. Only mark the new default * active connection after setting default = FALSE on all other connections * first. The order is important, we don't want two connections marked * default at the same time ever. */ devices = nm_manager_get_devices (policy->manager); for (iter = devices; iter; iter = g_slist_next (iter)) { NMDevice *dev = NM_DEVICE (iter->data); NMActRequest *req; req = nm_device_get_act_request (dev); if (req && (req != best_req)) nm_act_request_set_default (req, FALSE); } dns_mgr = nm_dns_manager_get (NULL); nm_dns_manager_add_ip4_config (dns_mgr, ip_iface, ip4_config, dns_type); g_object_unref (dns_mgr); /* Now set new default active connection _after_ updating DNS info, so that * if the connection is shared dnsmasq picks up the right stuff. */ if (best_req) nm_act_request_set_default (best_req, TRUE); if (connection) s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION); connection_id = s_con ? nm_setting_connection_get_id (s_con) : NULL; if (connection_id) { nm_log_info (LOGD_CORE, "Policy set '%s' (%s) as default for IPv4 routing and DNS.", connection_id, ip_iface); } else { nm_log_info (LOGD_CORE, "Policy set (%s) as default for IPv4 routing and DNS.", ip_iface); } out: policy->default_device4 = best; }
static GSList * construct_ip4_items (GSList *items, GHashTable *ip4_config, const char *prefix) { GSList *addresses = NULL, *routes = NULL, *iter; GArray *dns = NULL, *wins = NULL; guint32 num, i; GString *tmp; GValue *val; if (ip4_config == NULL) return items; if (prefix == NULL) prefix = ""; /* IP addresses */ val = g_hash_table_lookup (ip4_config, "addresses"); if (val) addresses = nm_utils_ip4_addresses_from_gvalue (val); for (iter = addresses, num = 0; iter; iter = g_slist_next (iter)) { NMIP4Address *addr = (NMIP4Address *) iter->data; char str_addr[INET_ADDRSTRLEN + 1]; char str_gw[INET_ADDRSTRLEN + 1]; struct in_addr tmp_addr; guint32 ip_prefix = nm_ip4_address_get_prefix (addr); char *addrtmp; memset (str_addr, 0, sizeof (str_addr)); tmp_addr.s_addr = nm_ip4_address_get_address (addr); if (!inet_ntop (AF_INET, &tmp_addr, str_addr, sizeof (str_addr))) continue; memset (str_gw, 0, sizeof (str_gw)); tmp_addr.s_addr = nm_ip4_address_get_gateway (addr); inet_ntop (AF_INET, &tmp_addr, str_gw, sizeof (str_gw)); addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, num++, str_addr, ip_prefix, str_gw); items = g_slist_prepend (items, addrtmp); } if (num) items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ADDRESSES=%d", prefix, num)); if (addresses) { g_slist_foreach (addresses, (GFunc) nm_ip4_address_unref, NULL); g_slist_free (addresses); } /* DNS servers */ val = g_hash_table_lookup (ip4_config, "nameservers"); if (val && G_VALUE_HOLDS (val, DBUS_TYPE_G_UINT_ARRAY)) dns = (GArray *) g_value_get_boxed (val); if (dns && (dns->len > 0)) { gboolean first = TRUE; tmp = g_string_new (NULL); g_string_append_printf (tmp, "%sIP4_NAMESERVERS=", prefix); for (i = 0; i < dns->len; i++) { struct in_addr addr; char buf[INET_ADDRSTRLEN + 1]; addr.s_addr = g_array_index (dns, guint32, i); memset (buf, 0, sizeof (buf)); if (inet_ntop (AF_INET, &addr, buf, sizeof (buf))) { if (!first) g_string_append_c (tmp, ' '); g_string_append (tmp, buf); first = FALSE; } } items = g_slist_prepend (items, tmp->str); g_string_free (tmp, FALSE); } /* Search domains */ items = add_domains (items, ip4_config, prefix, '4'); /* WINS servers */ val = g_hash_table_lookup (ip4_config, "wins-servers"); if (val && G_VALUE_HOLDS (val, DBUS_TYPE_G_UINT_ARRAY)) wins = (GArray *) g_value_get_boxed (val); if (wins && wins->len) { gboolean first = TRUE; tmp = g_string_new (NULL); g_string_append_printf (tmp, "%sIP4_WINS_SERVERS=", prefix); for (i = 0; i < wins->len; i++) { struct in_addr addr; char buf[INET_ADDRSTRLEN + 1]; addr.s_addr = g_array_index (wins, guint32, i); memset (buf, 0, sizeof (buf)); if (inet_ntop (AF_INET, &addr, buf, sizeof (buf))) { if (!first) g_string_append_c (tmp, ' '); g_string_append (tmp, buf); first = FALSE; } } items = g_slist_prepend (items, tmp->str); g_string_free (tmp, FALSE); } /* Static routes */ val = g_hash_table_lookup (ip4_config, "routes"); if (val) routes = nm_utils_ip4_routes_from_gvalue (val); for (iter = routes, num = 0; iter; iter = g_slist_next (iter)) { NMIP4Route *route = (NMIP4Route *) iter->data; char str_addr[INET_ADDRSTRLEN + 1]; char str_nh[INET_ADDRSTRLEN + 1]; struct in_addr tmp_addr; guint32 ip_prefix = nm_ip4_route_get_prefix (route); guint32 metric = nm_ip4_route_get_metric (route); char *routetmp; memset (str_addr, 0, sizeof (str_addr)); tmp_addr.s_addr = nm_ip4_route_get_dest (route); if (!inet_ntop (AF_INET, &tmp_addr, str_addr, sizeof (str_addr))) continue; memset (str_nh, 0, sizeof (str_nh)); tmp_addr.s_addr = nm_ip4_route_get_next_hop (route); inet_ntop (AF_INET, &tmp_addr, str_nh, sizeof (str_nh)); routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_nh, metric); items = g_slist_prepend (items, routetmp); } items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, num)); if (routes) { g_slist_foreach (routes, (GFunc) nm_ip4_route_unref, NULL); g_slist_free (routes); } return items; }
static void info_dialog_add_page (GtkNotebook *notebook, NMConnection *connection, gboolean is_default, NMDevice *device) { GtkTable *table; guint32 speed = 0; char *str; const char *iface; NMIP4Config *ip4_config; const GArray *dns; NMIP4Address *def_addr = NULL; guint32 hostmask, network, bcast, netmask; int row = 0; SpeedInfo* info = NULL; GtkWidget* speed_label; const GSList *addresses; table = GTK_TABLE (gtk_table_new (12, 2, FALSE)); gtk_table_set_col_spacings (table, 12); gtk_table_set_row_spacings (table, 6); gtk_container_set_border_width (GTK_CONTAINER (table), 12); /* Interface */ iface = nm_device_get_iface (device); if (NM_IS_DEVICE_ETHERNET (device)) str = g_strdup_printf (_("Ethernet (%s)"), iface); else if (NM_IS_DEVICE_WIFI (device)) str = g_strdup_printf (_("802.11 WiFi (%s)"), iface); else if (NM_IS_GSM_DEVICE (device)) str = g_strdup_printf (_("GSM (%s)"), iface); else if (NM_IS_CDMA_DEVICE (device)) str = g_strdup_printf (_("CDMA (%s)"), iface); else str = g_strdup (iface); gtk_table_attach_defaults (table, create_info_label (_("Interface:"), FALSE), 0, 1, row, row + 1); gtk_table_attach_defaults (table, create_info_label (str, TRUE), 1, 2, row, row + 1); g_free (str); row++; /* Hardware address */ str = NULL; if (NM_IS_DEVICE_ETHERNET (device)) str = g_strdup (nm_device_ethernet_get_hw_address (NM_DEVICE_ETHERNET (device))); else if (NM_IS_DEVICE_WIFI (device)) str = g_strdup (nm_device_wifi_get_hw_address (NM_DEVICE_WIFI (device))); gtk_table_attach_defaults (table, create_info_label (_("Hardware Address:"), FALSE), 0, 1, row, row + 1); gtk_table_attach_defaults (table, create_info_label (str, TRUE), 1, 2, row, row + 1); g_free (str); row++; /* Driver */ gtk_table_attach_defaults (table, create_info_label (_("Driver:"), FALSE), 0, 1, row, row + 1); gtk_table_attach_defaults (table, create_info_label (nm_device_get_driver (device), TRUE), 1, 2, row, row + 1); row++; speed_label = create_info_label ("", TRUE); /* Speed */ str = NULL; if (NM_IS_DEVICE_ETHERNET (device)) { /* Wired speed in Mb/s */ speed = nm_device_ethernet_get_speed (NM_DEVICE_ETHERNET (device)); } else if (NM_IS_DEVICE_WIFI (device)) { /* Wireless speed in Kb/s */ speed = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device)) / 1000; /* Listen for wifi speed changes */ info = g_malloc0 (sizeof (SpeedInfo)); info->device = device; info->label = speed_label; info->id = g_signal_connect (device, "notify::" NM_DEVICE_WIFI_BITRATE, G_CALLBACK (bitrate_changed_cb), speed_label); g_object_weak_ref (G_OBJECT(speed_label), label_destroyed, info); g_object_weak_ref (G_OBJECT(device), device_destroyed, info); } if (speed) str = g_strdup_printf (_("%u Mb/s"), speed); gtk_label_set_text (GTK_LABEL(speed_label), str ? str : _("Unknown")); g_free (str); gtk_table_attach_defaults (table, create_info_label (_("Speed:"), FALSE), 0, 1, row, row + 1); gtk_table_attach_defaults (table, speed_label, 1, 2, row, row + 1); row++; /* Security */ gtk_table_attach_defaults (table, create_info_label (_("Security:"), FALSE), 0, 1, row, row + 1); gtk_table_attach_defaults (table, create_info_label_security (connection), 1, 2, row, row + 1); row++; /* Empty line */ gtk_table_attach_defaults (table, gtk_label_new (""), 0, 2, row, row + 1); row++; /* IP4 */ ip4_config = nm_device_get_ip4_config (device); addresses = nm_ip4_config_get_addresses (ip4_config); if (g_slist_length ((GSList *) addresses)) def_addr = addresses->data; /* Address */ gtk_table_attach_defaults (table, create_info_label (_("IP Address:"), FALSE), 0, 1, row, row + 1); str = def_addr ? ip4_address_as_string (nm_ip4_address_get_address (def_addr)) : g_strdup (_("Unknown")); gtk_table_attach_defaults (table, create_info_label (str, TRUE), 1, 2, row, row + 1); g_free (str); row++; /* Broadcast */ if (def_addr) { netmask = nm_utils_ip4_prefix_to_netmask (nm_ip4_address_get_prefix (def_addr)); network = ntohl (nm_ip4_address_get_address (def_addr)) & ntohl (netmask); hostmask = ~ntohl (netmask); bcast = htonl (network | hostmask); } gtk_table_attach_defaults (table, create_info_label (_("Broadcast Address:"), FALSE), 0, 1, row, row + 1); str = def_addr ? ip4_address_as_string (bcast) : g_strdup (_("Unknown")); gtk_table_attach_defaults (table, create_info_label (str, TRUE), 1, 2, row, row + 1); g_free (str); row++; /* Prefix */ gtk_table_attach_defaults (table, create_info_label (_("Subnet Mask:"), FALSE), 0, 1, row, row + 1); str = def_addr ? ip4_address_as_string (netmask) : g_strdup (_("Unknown")); gtk_table_attach_defaults (table, create_info_label (str, TRUE), 1, 2, row, row + 1); g_free (str); row++; /* Gateway */ if (def_addr && nm_ip4_address_get_gateway (def_addr)) { gtk_table_attach_defaults (table, create_info_label (_("Default Route:"), FALSE), 0, 1, row, row + 1); str = ip4_address_as_string (nm_ip4_address_get_gateway (def_addr)); gtk_table_attach_defaults (table, create_info_label (str, TRUE), 1, 2, row, row + 1); g_free (str); row++; } /* DNS */ dns = def_addr ? nm_ip4_config_get_nameservers (ip4_config) : NULL; if (dns && dns->len) { gtk_table_attach_defaults (table, create_info_label (_("Primary DNS:"), FALSE), 0, 1, row, row + 1); str = ip4_address_as_string (g_array_index (dns, guint32, 0)); gtk_table_attach_defaults (table, create_info_label (str, TRUE), 1, 2, row, row + 1); g_free (str); row++; if (dns->len > 1) { gtk_table_attach_defaults (table, create_info_label (_("Secondary DNS:"), FALSE), 0, 1, row, row + 1); str = ip4_address_as_string (g_array_index (dns, guint32, 1)); gtk_table_attach_defaults (table, create_info_label (str, TRUE), 1, 2, row, row + 1); g_free (str); row++; } } gtk_notebook_append_page (notebook, GTK_WIDGET (table), create_info_notebook_label (connection, is_default)); gtk_widget_show_all (GTK_WIDGET (table)); }
static void detail_device (gpointer data, gpointer user_data) { NMDevice *device = NM_DEVICE (data); struct cb_info *info = user_data; char *tmp; NMDeviceState state; guint32 caps; guint32 speed; const GArray *array; int j; gboolean is_default = FALSE; const char *id = NULL; state = nm_device_get_state (device); for (j = 0; info->active && (j < info->active->len); j++) { NMActiveConnection *candidate = g_ptr_array_index (info->active, j); const GPtrArray *devices = nm_active_connection_get_devices (candidate); NMDevice *candidate_dev; NMConnection *connection; NMSettingConnection *s_con; if (!devices || !devices->len) continue; candidate_dev = g_ptr_array_index (devices, 0); if (candidate_dev == device) { if (nm_active_connection_get_default (candidate)) is_default = TRUE; connection = get_connection_for_active (candidate); if (!connection) break; s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION); if (s_con) id = nm_setting_connection_get_id (s_con); break; } } print_header ("Device", nm_device_get_iface (device), id); /* General information */ if (NM_IS_DEVICE_ETHERNET (device)) print_string ("Type", "Wired"); else if (NM_IS_DEVICE_WIFI (device)) print_string ("Type", "802.11 WiFi"); else if (NM_IS_GSM_DEVICE (device)) print_string ("Type", "Mobile Broadband (GSM)"); else if (NM_IS_CDMA_DEVICE (device)) print_string ("Type", "Mobile Broadband (CDMA)"); else if (NM_IS_DEVICE_BT (device)) print_string ("Type", "Bluetooth"); print_string ("Driver", nm_device_get_driver (device) ? nm_device_get_driver (device) : "(unknown)"); print_string ("State", get_dev_state_string (state)); if (is_default) print_string ("Default", "yes"); else print_string ("Default", "no"); tmp = NULL; if (NM_IS_DEVICE_ETHERNET (device)) tmp = g_strdup (nm_device_ethernet_get_hw_address (NM_DEVICE_ETHERNET (device))); else if (NM_IS_DEVICE_WIFI (device)) tmp = g_strdup (nm_device_wifi_get_hw_address (NM_DEVICE_WIFI (device))); if (tmp) { print_string ("HW Address", tmp); g_free (tmp); } /* Capabilities */ caps = nm_device_get_capabilities (device); printf ("\n Capabilities:\n"); if (caps & NM_DEVICE_CAP_CARRIER_DETECT) print_string (" Carrier Detect", "yes"); speed = 0; if (NM_IS_DEVICE_ETHERNET (device)) { /* Speed in Mb/s */ speed = nm_device_ethernet_get_speed (NM_DEVICE_ETHERNET (device)); } else if (NM_IS_DEVICE_WIFI (device)) { /* Speed in b/s */ speed = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device)); speed /= 1000; } if (speed) { char *speed_string; speed_string = g_strdup_printf ("%u Mb/s", speed); print_string (" Speed", speed_string); g_free (speed_string); } /* Wireless specific information */ if ((NM_IS_DEVICE_WIFI (device))) { guint32 wcaps; NMAccessPoint *active_ap = NULL; const char *active_bssid = NULL; const GPtrArray *aps; printf ("\n Wireless Properties\n"); wcaps = nm_device_wifi_get_capabilities (NM_DEVICE_WIFI (device)); if (wcaps & (NM_WIFI_DEVICE_CAP_CIPHER_WEP40 | NM_WIFI_DEVICE_CAP_CIPHER_WEP104)) print_string (" WEP Encryption", "yes"); if (wcaps & NM_WIFI_DEVICE_CAP_WPA) print_string (" WPA Encryption", "yes"); if (wcaps & NM_WIFI_DEVICE_CAP_RSN) print_string (" WPA2 Encryption", "yes"); if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) { active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)); active_bssid = active_ap ? nm_access_point_get_hw_address (active_ap) : NULL; } printf ("\n Wireless Access Points %s\n", active_ap ? "(* = current AP)" : ""); aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device)); if (aps && aps->len) g_ptr_array_foreach ((GPtrArray *) aps, detail_access_point, (gpointer) active_bssid); } else if (NM_IS_DEVICE_ETHERNET (device)) { printf ("\n Wired Properties\n"); if (nm_device_ethernet_get_carrier (NM_DEVICE_ETHERNET (device))) print_string (" Carrier", "on"); else print_string (" Carrier", "off"); } /* IP Setup info */ if (state == NM_DEVICE_STATE_ACTIVATED) { NMIP4Config *cfg = nm_device_get_ip4_config (device); GSList *iter; printf ("\n IPv4 Settings:\n"); for (iter = (GSList *) nm_ip4_config_get_addresses (cfg); iter; iter = g_slist_next (iter)) { NMIP4Address *addr = (NMIP4Address *) iter->data; guint32 prefix = nm_ip4_address_get_prefix (addr); char *tmp2; tmp = ip4_address_as_string (nm_ip4_address_get_address (addr)); print_string (" Address", tmp); g_free (tmp); tmp2 = ip4_address_as_string (nm_utils_ip4_prefix_to_netmask (prefix)); tmp = g_strdup_printf ("%d (%s)", prefix, tmp2); g_free (tmp2); print_string (" Prefix", tmp); g_free (tmp); tmp = ip4_address_as_string (nm_ip4_address_get_gateway (addr)); print_string (" Gateway", tmp); g_free (tmp); printf ("\n"); } array = nm_ip4_config_get_nameservers (cfg); if (array) { int i; for (i = 0; i < array->len; i++) { tmp = ip4_address_as_string (g_array_index (array, guint32, i)); print_string (" DNS", tmp); g_free (tmp); } } } printf ("\n\n"); }
static void test_generic_options (void) { GHashTable *options; NMIP4Config *ip4_config; NMIP4Address *addr; NMIP4Route *route; struct in_addr tmp; const char *expected_addr = "192.168.1.106"; const char *expected_gw = "192.168.1.1"; const char *expected_dns1 = "216.254.95.2"; const char *expected_dns2 = "216.231.41.2"; const char *expected_search1 = "foobar.com"; const char *expected_search2 = "blah.foobar.com"; const char *expected_route1_dest = "10.1.1.5"; const char *expected_route1_gw = "10.1.1.1"; const char *expected_route2_dest = "100.99.88.56"; const char *expected_route2_gw = "10.1.1.1"; options = fill_table (generic_options, NULL); ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-generic", "failed to parse DHCP4 options"); /* IP4 address */ ASSERT (nm_ip4_config_get_num_addresses (ip4_config) == 1, "dhcp-generic", "unexpected number of IP addresses"); addr = nm_ip4_config_get_address (ip4_config, 0); ASSERT (inet_pton (AF_INET, expected_addr, &tmp) > 0, "dhcp-generic", "couldn't convert expected IP address"); ASSERT (nm_ip4_address_get_address (addr) == tmp.s_addr, "dhcp-generic", "unexpected IP address"); ASSERT (nm_ip4_address_get_prefix (addr) == 24, "dhcp-generic", "unexpected IP address prefix length"); /* Gateway */ ASSERT (inet_pton (AF_INET, expected_gw, &tmp) > 0, "dhcp-generic", "couldn't convert expected IP gateway"); ASSERT (nm_ip4_address_get_gateway (addr) == tmp.s_addr, "dhcp-generic", "unexpected IP gateway"); ASSERT (nm_ip4_config_get_ptp_address (ip4_config) == 0, "dhcp-generic", "unexpected PTP address"); ASSERT (nm_ip4_config_get_num_wins (ip4_config) == 0, "dhcp-generic", "unexpected number of WINS servers"); ASSERT (nm_ip4_config_get_mtu (ip4_config) == 987, "dhcp-generic", "unexpected MTU"); /* Domain searches */ ASSERT (nm_ip4_config_get_num_searches (ip4_config) == 2, "dhcp-generic", "unexpected number of domain searches"); ASSERT (strcmp (nm_ip4_config_get_search (ip4_config, 0), expected_search1) == 0, "dhcp-generic", "unexpected domain search #1"); ASSERT (strcmp (nm_ip4_config_get_search (ip4_config, 1), expected_search2) == 0, "dhcp-generic", "unexpected domain search #2"); /* DNS servers */ ASSERT (nm_ip4_config_get_num_nameservers (ip4_config) == 2, "dhcp-generic", "unexpected number of domain name servers"); ASSERT (inet_pton (AF_INET, expected_dns1, &tmp) > 0, "dhcp-generic", "couldn't convert expected DNS server address #1"); ASSERT (nm_ip4_config_get_nameserver (ip4_config, 0) == tmp.s_addr, "dhcp-generic", "unexpected domain name server #1"); ASSERT (inet_pton (AF_INET, expected_dns2, &tmp) > 0, "dhcp-generic", "couldn't convert expected DNS server address #2"); ASSERT (nm_ip4_config_get_nameserver (ip4_config, 1) == tmp.s_addr, "dhcp-generic", "unexpected domain name server #2"); /* Routes */ ASSERT (nm_ip4_config_get_num_routes (ip4_config) == 2, "dhcp-generic", "unexpected number of routes"); /* Route #1 */ route = nm_ip4_config_get_route (ip4_config, 0); ASSERT (inet_pton (AF_INET, expected_route1_dest, &tmp) > 0, "dhcp-generic", "couldn't convert expected route destination #1"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "dhcp-generic", "unexpected route #1 destination"); ASSERT (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0, "dhcp-generic", "couldn't convert expected route next hop #1"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "dhcp-generic", "unexpected route #1 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 32, "dhcp-generic", "unexpected route #1 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "dhcp-generic", "unexpected route #1 metric"); /* Route #2 */ route = nm_ip4_config_get_route (ip4_config, 1); ASSERT (inet_pton (AF_INET, expected_route2_dest, &tmp) > 0, "dhcp-generic", "couldn't convert expected route destination #2"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "dhcp-generic", "unexpected route #2 destination"); ASSERT (inet_pton (AF_INET, expected_route2_gw, &tmp) > 0, "dhcp-generic", "couldn't convert expected route next hop #2"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "dhcp-generic", "unexpected route #2 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 32, "dhcp-generic", "unexpected route #2 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "dhcp-generic", "unexpected route #2 metric"); g_hash_table_destroy (options); }
static void populate_ui (CEPageIP4 *self) { CEPageIP4Private *priv = CE_PAGE_IP4_GET_PRIVATE (self); NMSettingIP4Config *setting = priv->setting; GtkListStore *store; GtkTreeIter model_iter; int method = IP4_METHOD_AUTO; GString *string = NULL; SetMethodInfo info; const char *str_method; int i; /* Method */ gtk_combo_box_set_active (priv->method, 0); str_method = nm_setting_ip4_config_get_method (setting); if (str_method) { if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)) method = IP4_METHOD_LINK_LOCAL; else if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) method = IP4_METHOD_MANUAL; else if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)) method = IP4_METHOD_SHARED; } if (method == IP4_METHOD_AUTO && nm_setting_ip4_config_get_ignore_auto_dns (setting)) method = IP4_METHOD_AUTO_ADDRESSES; info.method = method; info.combo = priv->method; gtk_tree_model_foreach (GTK_TREE_MODEL (priv->method_store), set_method, &info); /* Addresses */ store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); for (i = 0; i < nm_setting_ip4_config_get_num_addresses (setting); i++) { NMIP4Address *addr = nm_setting_ip4_config_get_address (setting, i); struct in_addr tmp_addr; char buf[INET_ADDRSTRLEN + 1]; const char *ignored; if (!addr) { g_warning ("%s: empty IP4 Address structure!", __func__); continue; } gtk_list_store_append (store, &model_iter); tmp_addr.s_addr = nm_ip4_address_get_address (addr); ignored = inet_ntop (AF_INET, &tmp_addr, &buf[0], sizeof (buf)); gtk_list_store_set (store, &model_iter, COL_ADDRESS, buf, -1); tmp_addr.s_addr = nm_utils_ip4_prefix_to_netmask (nm_ip4_address_get_prefix (addr)); ignored = inet_ntop (AF_INET, &tmp_addr, &buf[0], sizeof (buf)); gtk_list_store_set (store, &model_iter, COL_PREFIX, buf, -1); tmp_addr.s_addr = nm_ip4_address_get_gateway (addr); ignored = inet_ntop (AF_INET, &tmp_addr, &buf[0], sizeof (buf)); gtk_list_store_set (store, &model_iter, COL_GATEWAY, buf, -1); } gtk_tree_view_set_model (priv->addr_list, GTK_TREE_MODEL (store)); g_signal_connect_swapped (store, "row-inserted", G_CALLBACK (ce_page_changed), self); g_signal_connect_swapped (store, "row-deleted", G_CALLBACK (ce_page_changed), self); g_object_unref (store); /* DNS servers */ string = g_string_new (""); for (i = 0; i < nm_setting_ip4_config_get_num_dns (setting); i++) { struct in_addr tmp_addr; char buf[INET_ADDRSTRLEN + 1]; const char *ignored; tmp_addr.s_addr = nm_setting_ip4_config_get_dns (setting, i); if (!tmp_addr.s_addr) continue; ignored = inet_ntop (AF_INET, &tmp_addr, &buf[0], sizeof (buf)); if (string->len) g_string_append (string, ", "); g_string_append (string, buf); } gtk_entry_set_text (priv->dns_servers, string->str); g_string_free (string, TRUE); /* DNS searches */ string = g_string_new (""); for (i = 0; i < nm_setting_ip4_config_get_num_dns_searches (setting); i++) { if (string->len) g_string_append (string, ", "); g_string_append (string, nm_setting_ip4_config_get_dns_search (setting, i)); } gtk_entry_set_text (priv->dns_searches, string->str); g_string_free (string, TRUE); if ((method == IP4_METHOD_AUTO) || (method = IP4_METHOD_AUTO_ADDRESSES)) { if (nm_setting_ip4_config_get_dhcp_client_id (setting)) { gtk_entry_set_text (priv->dhcp_client_id, nm_setting_ip4_config_get_dhcp_client_id (setting)); } } }
gboolean print_ip4_config (NMIP4Config *cfg4, NmCli *nmc, const char *group_prefix) { GSList *list, *iter; const GArray *array; const GPtrArray *ptr_array; char **addr_arr = NULL; char **route_arr = NULL; char **dns_arr = NULL; char **domain_arr = NULL; char **wins_arr = NULL; int i = 0; guint32 mode_flag = (nmc->print_output == NMC_PRINT_PRETTY) ? NMC_PF_FLAG_PRETTY : (nmc->print_output == NMC_PRINT_TERSE) ? NMC_PF_FLAG_TERSE : 0; guint32 multiline_flag = nmc->multiline_output ? NMC_PF_FLAG_MULTILINE : 0; guint32 escape_flag = nmc->escape_values ? NMC_PF_FLAG_ESCAPE : 0; if (cfg4 == NULL) return FALSE; nmc->allowed_fields = nmc_fields_ip4_config; nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_FIELD_NAMES; nmc->print_fields.indices = parse_output_fields (NMC_FIELDS_IP4_CONFIG_ALL, nmc->allowed_fields, NULL); print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */ /* addresses */ list = (GSList *) nm_ip4_config_get_addresses (cfg4); addr_arr = g_new (char *, g_slist_length (list) + 1); for (iter = list; iter; iter = g_slist_next (iter)) { NMIP4Address *addr = (NMIP4Address *) iter->data; guint32 prefix; char *ip_str, *gw_str; ip_str = nmc_ip4_address_as_string (nm_ip4_address_get_address (addr), NULL); prefix = nm_ip4_address_get_prefix (addr); gw_str = nmc_ip4_address_as_string (nm_ip4_address_get_gateway (addr), NULL); addr_arr[i++] = g_strdup_printf ("ip = %s/%u, gw = %s", ip_str, prefix, gw_str); g_free (ip_str); g_free (gw_str); } addr_arr[i] = NULL; /* routes */ list = (GSList *) nm_ip4_config_get_routes (cfg4); route_arr = g_new (char *, g_slist_length (list) + 1); i = 0; for (iter = list; iter; iter = g_slist_next (iter)) { NMIP4Route *route = (NMIP4Route *) iter->data; guint32 prefix, metric; char *dest_str, *nexthop_str; dest_str = nmc_ip4_address_as_string (nm_ip4_route_get_dest (route), NULL); nexthop_str = nmc_ip4_address_as_string (nm_ip4_route_get_next_hop (route), NULL); prefix = nm_ip4_route_get_prefix (route); metric = nm_ip4_route_get_metric (route); route_arr[i++] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", dest_str, prefix, nexthop_str, metric); g_free (dest_str); g_free (nexthop_str); } route_arr[i] = NULL; /* DNS */ array = nm_ip4_config_get_nameservers (cfg4); if (array) { dns_arr = g_new (char *, array->len + 1); for (i = 0; i < array->len; i++) dns_arr[i] = nmc_ip4_address_as_string (g_array_index (array, guint32, i), NULL); dns_arr[i] = NULL; } /* domains */ ptr_array = nm_ip4_config_get_domains (cfg4); if (ptr_array) { domain_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) domain_arr[i] = g_ptr_array_index (ptr_array, i); domain_arr[i] = NULL; } /* WINS */ array = nm_ip4_config_get_wins_servers (cfg4); if (array) { wins_arr = g_new (char *, array->len + 1); for (i = 0; i < array->len; i++) wins_arr[i] = nmc_ip4_address_as_string (g_array_index (array, guint32, i), NULL); wins_arr[i] = NULL; } set_val_str (nmc->allowed_fields, 0, group_prefix); set_val_arr (nmc->allowed_fields, 1, (const char **) addr_arr); set_val_arr (nmc->allowed_fields, 2, (const char **) route_arr); set_val_arr (nmc->allowed_fields, 3, (const char **) dns_arr); set_val_arr (nmc->allowed_fields, 4, (const char **) domain_arr); set_val_arr (nmc->allowed_fields, 5, (const char **) wins_arr); nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_SECTION_PREFIX; print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */ g_strfreev (addr_arr); g_strfreev (route_arr); g_strfreev (dns_arr); g_free (domain_arr); g_strfreev (wins_arr); return TRUE; }
static void detail_device (gpointer data, gpointer user_data) { NMDevice *device = NM_DEVICE (data); char *tmp; NMDeviceState state; guint32 caps; guint32 speed; const GArray *array; gboolean is_default = FALSE; const char *id = NULL; NMActiveConnection *active; active = nm_device_get_active_connection (device); if (active) { NMConnection *connection; NMSettingConnection *s_con; is_default = nm_active_connection_get_default (active); connection = get_connection_for_active (active); if (connection) { s_con = nm_connection_get_setting_connection (connection); if (s_con) id = nm_setting_connection_get_id (s_con); } } print_header ("Device", nm_device_get_iface (device), id); /* General information */ if (NM_IS_DEVICE_ETHERNET (device)) print_string ("Type", "Wired"); else if (NM_IS_DEVICE_WIFI (device)) print_string ("Type", "802.11 WiFi"); else if (NM_IS_DEVICE_MODEM (device)) { NMDeviceModemCapabilities modem_caps; modem_caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device)); if (modem_caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) print_string ("Type", "Mobile Broadband (GSM)"); else if (modem_caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) print_string ("Type", "Mobile Broadband (CDMA)"); else print_string ("Type", "Mobile Broadband (unknown)"); } else if (NM_IS_DEVICE_BT (device)) print_string ("Type", "Bluetooth"); else if (NM_IS_DEVICE_WIMAX (device)) print_string ("Type", "WiMAX"); print_string ("Driver", nm_device_get_driver (device) ? nm_device_get_driver (device) : "(unknown)"); state = nm_device_get_state (device); print_string ("State", get_dev_state_string (state)); if (is_default) print_string ("Default", "yes"); else print_string ("Default", "no"); tmp = NULL; if (NM_IS_DEVICE_ETHERNET (device)) tmp = g_strdup (nm_device_ethernet_get_hw_address (NM_DEVICE_ETHERNET (device))); else if (NM_IS_DEVICE_WIFI (device)) tmp = g_strdup (nm_device_wifi_get_hw_address (NM_DEVICE_WIFI (device))); else if (NM_IS_DEVICE_WIMAX (device)) tmp = g_strdup (nm_device_wimax_get_hw_address (NM_DEVICE_WIMAX (device))); if (tmp) { print_string ("HW Address", tmp); g_free (tmp); } /* Capabilities */ caps = nm_device_get_capabilities (device); printf ("\n Capabilities:\n"); if (caps & NM_DEVICE_CAP_CARRIER_DETECT) print_string (" Carrier Detect", "yes"); speed = 0; if (NM_IS_DEVICE_ETHERNET (device)) { /* Speed in Mb/s */ speed = nm_device_ethernet_get_speed (NM_DEVICE_ETHERNET (device)); } else if (NM_IS_DEVICE_WIFI (device)) { /* Speed in b/s */ speed = nm_device_wifi_get_bitrate (NM_DEVICE_WIFI (device)); speed /= 1000; } if (speed) { char *speed_string; speed_string = g_strdup_printf ("%u Mb/s", speed); print_string (" Speed", speed_string); g_free (speed_string); } /* Wireless specific information */ if ((NM_IS_DEVICE_WIFI (device))) { guint32 wcaps; NMAccessPoint *active_ap = NULL; const char *active_bssid = NULL; const GPtrArray *aps; printf ("\n Wireless Properties\n"); wcaps = nm_device_wifi_get_capabilities (NM_DEVICE_WIFI (device)); if (wcaps & (NM_WIFI_DEVICE_CAP_CIPHER_WEP40 | NM_WIFI_DEVICE_CAP_CIPHER_WEP104)) print_string (" WEP Encryption", "yes"); if (wcaps & NM_WIFI_DEVICE_CAP_WPA) print_string (" WPA Encryption", "yes"); if (wcaps & NM_WIFI_DEVICE_CAP_RSN) print_string (" WPA2 Encryption", "yes"); if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) { active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)); active_bssid = active_ap ? nm_access_point_get_hw_address (active_ap) : NULL; } printf ("\n Wireless Access Points %s\n", active_ap ? "(* = current AP)" : ""); aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (device)); if (aps && aps->len) g_ptr_array_foreach ((GPtrArray *) aps, detail_access_point, (gpointer) active_bssid); } else if (NM_IS_DEVICE_ETHERNET (device)) { printf ("\n Wired Properties\n"); if (nm_device_ethernet_get_carrier (NM_DEVICE_ETHERNET (device))) print_string (" Carrier", "on"); else print_string (" Carrier", "off"); } else if (NM_IS_DEVICE_WIMAX (device)) { NMDeviceWimax *wimax = NM_DEVICE_WIMAX (device); NMWimaxNsp *active_nsp = NULL; const char *active_name = NULL; const GPtrArray *nsps; if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) { guint tmp_uint; gint tmp_int; const char *tmp_str; active_nsp = nm_device_wimax_get_active_nsp (wimax); active_name = active_nsp ? nm_wimax_nsp_get_name (active_nsp) : NULL; printf ("\n Link Status\n"); tmp_uint = nm_device_wimax_get_center_frequency (wimax); if (tmp_uint) tmp = g_strdup_printf ("%'.1f MHz", (double) tmp_uint / 1000.0); else tmp = g_strdup ("(unknown)"); print_string (" Center Freq.", tmp); g_free (tmp); tmp_int = nm_device_wimax_get_rssi (wimax); if (tmp_int) tmp = g_strdup_printf ("%d dBm", tmp_int); else tmp = g_strdup ("(unknown)"); print_string (" RSSI", tmp); g_free (tmp); tmp_int = nm_device_wimax_get_cinr (wimax); if (tmp_int) tmp = g_strdup_printf ("%d dB", tmp_int); else tmp = g_strdup ("(unknown)"); print_string (" CINR", tmp); g_free (tmp); tmp_int = nm_device_wimax_get_tx_power (wimax); if (tmp_int) tmp = g_strdup_printf ("%'.2f dBm", (float) tmp_int / 2.0); else tmp = g_strdup ("(unknown)"); print_string (" TX Power", tmp); g_free (tmp); tmp_str = nm_device_wimax_get_bsid (wimax); if (tmp_str) print_string (" BSID", tmp_str); else print_string (" BSID", "(unknown)"); } printf ("\n WiMAX NSPs %s\n", active_nsp ? "(* current NSP)" : ""); nsps = nm_device_wimax_get_nsps (NM_DEVICE_WIMAX (device)); if (nsps && nsps->len) g_ptr_array_foreach ((GPtrArray *) nsps, detail_nsp, (gpointer) active_name); } /* IP Setup info */ if (state == NM_DEVICE_STATE_ACTIVATED) { NMIP4Config *cfg4 = nm_device_get_ip4_config (device); NMIP6Config *cfg6 = nm_device_get_ip6_config (device); GSList *iter; if (cfg4) { printf ("\n IPv4 Settings:\n"); for (iter = (GSList *) nm_ip4_config_get_addresses (cfg4); iter; iter = g_slist_next (iter)) { NMIP4Address *addr = (NMIP4Address *) iter->data; guint32 prefix = nm_ip4_address_get_prefix (addr); char *tmp2; tmp = ip4_address_as_string (nm_ip4_address_get_address (addr)); print_string (" Address", tmp); g_free (tmp); tmp2 = ip4_address_as_string (nm_utils_ip4_prefix_to_netmask (prefix)); tmp = g_strdup_printf ("%d (%s)", prefix, tmp2); g_free (tmp2); print_string (" Prefix", tmp); g_free (tmp); tmp = ip4_address_as_string (nm_ip4_address_get_gateway (addr)); print_string (" Gateway", tmp); g_free (tmp); printf ("\n"); } array = nm_ip4_config_get_nameservers (cfg4); if (array) { int i; for (i = 0; i < array->len; i++) { tmp = ip4_address_as_string (g_array_index (array, guint32, i)); print_string (" DNS", tmp); g_free (tmp); } } } if (cfg6) { printf ("\n IPv6 Settings:\n"); for (iter = (GSList *) nm_ip6_config_get_addresses (cfg6); iter; iter = g_slist_next (iter)) { NMIP6Address *addr = (NMIP6Address *) iter->data; guint32 prefix = nm_ip6_address_get_prefix (addr); tmp = ip6_address_as_string (nm_ip6_address_get_address (addr)); print_string (" Address", tmp); g_free (tmp); tmp = g_strdup_printf ("%d", prefix); print_string (" Prefix", tmp); g_free (tmp); tmp = ip6_address_as_string (nm_ip6_address_get_gateway (addr)); print_string (" Gateway", tmp); g_free (tmp); printf ("\n"); } for (iter = (GSList *) nm_ip6_config_get_nameservers (cfg6); iter; iter = g_slist_next (iter)) { tmp = ip6_address_as_string (iter->data); print_string (" DNS", tmp); g_free (tmp); } } } printf ("\n\n"); }
gboolean print_ip4_config (NMIP4Config *cfg4, NmCli *nmc, const char *group_prefix, const char *one_field) { GSList *list, *iter; const GArray *array; const GPtrArray *ptr_array; char **addr_arr = NULL; char **route_arr = NULL; char **dns_arr = NULL; char **domain_arr = NULL; char **wins_arr = NULL; int i = 0; NmcOutputField *tmpl, *arr; size_t tmpl_len; if (cfg4 == NULL) return FALSE; tmpl = nmc_fields_ip4_config; tmpl_len = sizeof (nmc_fields_ip4_config); nmc->print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP4_CONFIG_ALL, tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_FIELD_NAMES); g_ptr_array_add (nmc->output_data, arr); /* addresses */ list = (GSList *) nm_ip4_config_get_addresses (cfg4); addr_arr = g_new (char *, g_slist_length (list) + 1); for (iter = list; iter; iter = g_slist_next (iter)) { NMIP4Address *addr = (NMIP4Address *) iter->data; guint32 prefix; char *ip_str, *gw_str; ip_str = nmc_ip4_address_as_string (nm_ip4_address_get_address (addr), NULL); prefix = nm_ip4_address_get_prefix (addr); gw_str = nmc_ip4_address_as_string (nm_ip4_address_get_gateway (addr), NULL); addr_arr[i++] = g_strdup_printf ("ip = %s/%u, gw = %s", ip_str, prefix, gw_str); g_free (ip_str); g_free (gw_str); } addr_arr[i] = NULL; /* routes */ list = (GSList *) nm_ip4_config_get_routes (cfg4); route_arr = g_new (char *, g_slist_length (list) + 1); i = 0; for (iter = list; iter; iter = g_slist_next (iter)) { NMIP4Route *route = (NMIP4Route *) iter->data; guint32 prefix, metric; char *dest_str, *nexthop_str; dest_str = nmc_ip4_address_as_string (nm_ip4_route_get_dest (route), NULL); nexthop_str = nmc_ip4_address_as_string (nm_ip4_route_get_next_hop (route), NULL); prefix = nm_ip4_route_get_prefix (route); metric = nm_ip4_route_get_metric (route); route_arr[i++] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", dest_str, prefix, nexthop_str, metric); g_free (dest_str); g_free (nexthop_str); } route_arr[i] = NULL; /* DNS */ array = nm_ip4_config_get_nameservers (cfg4); if (array) { dns_arr = g_new (char *, array->len + 1); for (i = 0; i < array->len; i++) dns_arr[i] = nmc_ip4_address_as_string (g_array_index (array, guint32, i), NULL); dns_arr[i] = NULL; } /* domains */ ptr_array = nm_ip4_config_get_domains (cfg4); if (ptr_array) { domain_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) domain_arr[i] = g_strdup (g_ptr_array_index (ptr_array, i)); domain_arr[i] = NULL; } /* WINS */ array = nm_ip4_config_get_wins_servers (cfg4); if (array) { wins_arr = g_new (char *, array->len + 1); for (i = 0; i < array->len; i++) wins_arr[i] = nmc_ip4_address_as_string (g_array_index (array, guint32, i), NULL); wins_arr[i] = NULL; } arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); set_val_arr (arr, 1, addr_arr); set_val_arr (arr, 2, route_arr); set_val_arr (arr, 3, dns_arr); set_val_arr (arr, 4, domain_arr); set_val_arr (arr, 5, wins_arr); g_ptr_array_add (nmc->output_data, arr); print_data (nmc); /* Print all data */ /* Remove any previous data */ nmc_empty_output_fields (nmc); return TRUE; }