コード例 #1
0
static void
print_ip6_addr (gpointer object, gpointer user_data)
{
	const struct in6_addr * tmp_addr;
	char buf[INET6_ADDRSTRLEN + 1];
	NMIP6Address * address = (NMIP6Address *) object;
	guint32 prefix = nm_ip6_address_get_prefix (address);

	tmp_addr = nm_ip6_address_get_address (address);

	inet_ntop (AF_INET6, &tmp_addr, buf, sizeof (buf));
	g_print ("%-9s IPv6:%s/%d\n", "", buf, prefix);
}
コード例 #2
0
static void
add_address_section (CEPageIP6 *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), update_header, 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_ip6_config_get_num_addresses (page->setting); i++) {
                NMIP6Address *addr;
                const struct in6_addr *tmp_addr;
                gchar address[INET6_ADDRSTRLEN + 1];
                gchar network[INET6_ADDRSTRLEN + 1];
                gchar gateway[INET6_ADDRSTRLEN + 1];

                addr = nm_setting_ip6_config_get_address (page->setting, i);
                if (!addr)
                        continue;

                tmp_addr = nm_ip6_address_get_address (addr);
                (void) inet_ntop (AF_INET6, tmp_addr, &address[0], sizeof (address));

                snprintf (network, sizeof (network), "%u", nm_ip6_address_get_prefix (addr));

                tmp_addr = nm_ip6_address_get_gateway (addr);
                if (tmp_addr && !IN6_IS_ADDR_UNSPECIFIED (tmp_addr))
                        (void) inet_ntop (AF_INET6, tmp_addr, &gateway[0], sizeof (gateway));
                else
                        gateway[0] = '\0';

                add_address_row (page, address, network, gateway);
        }
        if (nm_setting_ip6_config_get_num_addresses (page->setting) == 0)
                add_empty_address_row (page);

        gtk_widget_show_all (widget);
}
コード例 #3
0
ファイル: nm-ip6-config.c プロジェクト: vzupanovic/projekt
void
nm_ip6_config_hash (NMIP6Config *config, GChecksum *sum, gboolean dns_only)
{
	guint32 i;
	const struct in6_addr *in6a;
	const char *s;

	g_return_if_fail (config != NULL);
	g_return_if_fail (sum != NULL);

	if (dns_only == FALSE) {
		for (i = 0; i < nm_ip6_config_get_num_addresses (config); i++) {
			NMIP6Address *a = nm_ip6_config_get_address (config, i);

			hash_in6addr (sum, nm_ip6_address_get_address (a));
			hash_u32 (sum, nm_ip6_address_get_prefix (a));
			hash_in6addr (sum, nm_ip6_address_get_gateway (a));
		}

		for (i = 0; i < nm_ip6_config_get_num_routes (config); i++) {
			NMIP6Route *r = nm_ip6_config_get_route (config, i);

			hash_in6addr (sum, nm_ip6_route_get_dest (r));
			hash_u32 (sum, nm_ip6_route_get_prefix (r));
			hash_in6addr (sum, nm_ip6_route_get_next_hop (r));
			hash_u32 (sum, nm_ip6_route_get_metric (r));
		}

		in6a = nm_ip6_config_get_ptp_address (config);
		if (in6a)
			hash_in6addr (sum, in6a);
	}

	for (i = 0; i < nm_ip6_config_get_num_nameservers (config); i++)
		hash_in6addr (sum, nm_ip6_config_get_nameserver (config, i));

	for (i = 0; i < nm_ip6_config_get_num_domains (config); i++) {
		s = nm_ip6_config_get_domain (config, i);
		g_checksum_update (sum, (const guint8 *) s, strlen (s));
	}

	for (i = 0; i < nm_ip6_config_get_num_searches (config); i++) {
		s = nm_ip6_config_get_search (config, i);
		g_checksum_update (sum, (const guint8 *) s, strlen (s));
	}
}
コード例 #4
0
gchar *
panel_get_ip6_address_as_string (NMIP6Config *ip6_config)
{
        const GSList *list;
        const struct in6_addr *addr;
        gchar *str = NULL;
        gchar tmp[INET6_ADDRSTRLEN];
        NMIP6Address *address;

        /* get address */
        list = nm_ip6_config_get_addresses (ip6_config);
        if (list == NULL)
                goto out;

        /* we only care about one address */
        address = list->data;
        addr = nm_ip6_address_get_address (address);
        if (addr == NULL)
                goto out;
        inet_ntop (AF_INET6, addr, tmp, sizeof(tmp));
        str = g_strdup (tmp);
out:
        return str;
}
コード例 #5
0
ファイル: nm-tool.c プロジェクト: alfmatos/NetworkManager
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");
}
コード例 #6
0
gboolean
print_ip6_config (NMIP6Config *cfg6, NmCli *nmc, const char *group_prefix)
{
	GSList *list, *iter;
	const GPtrArray *ptr_array;
	char **addr_arr = NULL;
	char **route_arr = NULL;
	char **dns_arr = NULL;
	char **domain_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 (cfg6 == NULL)
		return FALSE;

	nmc->allowed_fields = nmc_fields_ip6_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_IP6_CONFIG_ALL, nmc->allowed_fields, NULL);
	print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */

	/* addresses */
	list = (GSList *) nm_ip6_config_get_addresses (cfg6);
	addr_arr = g_new (char *, g_slist_length (list) + 1);
	for (iter = list; iter; iter = g_slist_next (iter)) {
		NMIP6Address *addr = (NMIP6Address *) iter->data;
		guint32 prefix;
		char *ip_str, *gw_str;

		ip_str = nmc_ip6_address_as_string (nm_ip6_address_get_address (addr), NULL);
		prefix = nm_ip6_address_get_prefix (addr);
		gw_str = nmc_ip6_address_as_string (nm_ip6_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_ip6_config_get_routes (cfg6);
	route_arr = g_new (char *, g_slist_length (list) + 1);
	i = 0;
	for (iter = list; iter; iter = g_slist_next (iter)) {
		NMIP6Route *route = (NMIP6Route *) iter->data;
		guint32 prefix, metric;
		char *dest_str, *nexthop_str;

		dest_str = nmc_ip6_address_as_string (nm_ip6_route_get_dest (route), NULL);
		nexthop_str = nmc_ip6_address_as_string (nm_ip6_route_get_next_hop (route), NULL);
		prefix = nm_ip6_route_get_prefix (route);
		metric = nm_ip6_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 */
	list = (GSList *) nm_ip6_config_get_nameservers (cfg6);
	dns_arr = g_new (char *, g_slist_length (list) + 1);
	i = 0;
	for (iter = list; iter; iter = g_slist_next (iter))
		dns_arr[i++] = nmc_ip6_address_as_string (iter->data, NULL);

	dns_arr[i] = NULL;

	/* domains */
	ptr_array = nm_ip6_config_get_domains (cfg6);
	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;
	}

	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);

	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);

	return TRUE;
}
コード例 #7
0
static void
update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6)
{
	char *configured_hostname = NULL;
	NMActRequest *best_req4 = NULL;
	NMActRequest *best_req6 = NULL;
	const char *dhcp_hostname, *p;

	g_return_if_fail (policy != NULL);

	if (policy->lookup) {
		hostname_thread_kill (policy->lookup);
		policy->lookup = NULL;
	}

	/* Hostname precedence order:
	 *
	 * 1) a configured hostname (from system-settings)
	 * 2) automatic hostname from the default device's config (DHCP, VPN, etc)
	 * 3) the original hostname when NM started
	 * 4) reverse-DNS of the best device's IPv4 address
	 *
	 */

	/* Try a persistent hostname first */
	g_object_get (G_OBJECT (policy->manager), NM_MANAGER_HOSTNAME, &configured_hostname, NULL);
	if (configured_hostname) {
		_set_hostname (policy, TRUE, configured_hostname, "from system configuration");
		g_free (configured_hostname);
		return;
	}

	/* Try automatically determined hostname from the best device's IP config */
	if (!best4)
		best4 = get_best_ip4_device (policy->manager, &best_req4);
	if (!best6)
		best6 = get_best_ip6_device (policy->manager, &best_req6);

	if (!best4 && !best6) {
		/* No best device; fall back to original hostname or if there wasn't
		 * one, 'localhost.localdomain'
		 */
		_set_hostname (policy, TRUE, policy->orig_hostname, "no default device");
		return;
	}

	if (best4) {
		NMDHCP4Config *dhcp4_config;

		/* Grab a hostname out of the device's DHCP4 config */
		dhcp4_config = nm_device_get_dhcp4_config (best4);
		if (dhcp4_config) {
			p = dhcp_hostname = nm_dhcp4_config_get_option (dhcp4_config, "host_name");
			if (dhcp_hostname && strlen (dhcp_hostname)) {
				/* Sanity check; strip leading spaces */
				while (*p) {
					if (!isblank (*p++)) {
						_set_hostname (policy, TRUE, dhcp_hostname, "from DHCPv4");
						return;
					}
				}
				nm_log_warn (LOGD_DNS, "DHCPv4-provided hostname '%s' looks invalid; ignoring it",
					         dhcp_hostname);
			}
		}
	} else if (best6) {
		NMDHCP6Config *dhcp6_config;

		/* Grab a hostname out of the device's DHCP4 config */
		dhcp6_config = nm_device_get_dhcp6_config (best6);
		if (dhcp6_config) {
			p = dhcp_hostname = nm_dhcp6_config_get_option (dhcp6_config, "host_name");
			if (dhcp_hostname && strlen (dhcp_hostname)) {
				/* Sanity check; strip leading spaces */
				while (*p) {
					if (!isblank (*p++)) {
						_set_hostname (policy, TRUE, dhcp_hostname, "from DHCPv6");
						return;
					}
				}
				nm_log_warn (LOGD_DNS, "DHCPv6-provided hostname '%s' looks invalid; ignoring it",
					         dhcp_hostname);
			}
		}
	}

	/* If no automatically-configured hostname, try using the hostname from
	 * when NM started up.
	 */
	if (policy->orig_hostname) {
		_set_hostname (policy, TRUE, policy->orig_hostname, "from system startup");
		return;
	}

	/* No configured hostname, no automatically determined hostname, and no
	 * bootup hostname. Start reverse DNS of the current IPv4 or IPv6 address.
	 */
	if (best4) {
		NMIP4Config *ip4_config;
		NMIP4Address *addr4;

		ip4_config = nm_device_get_ip4_config (best4);
		if (   !ip4_config
		    || (nm_ip4_config_get_num_nameservers (ip4_config) == 0)
		    || (nm_ip4_config_get_num_addresses (ip4_config) == 0)) {
			/* No valid IP4 config (!!); fall back to localhost.localdomain */
			_set_hostname (policy, TRUE, NULL, "no IPv4 config");
			return;
		}

		addr4 = nm_ip4_config_get_address (ip4_config, 0);
		g_assert (addr4); /* checked for > 1 address above */

		/* Start the hostname lookup thread */
		policy->lookup = hostname4_thread_new (nm_ip4_address_get_address (addr4), lookup_callback, policy);
	} else if (best6) {
		NMIP6Config *ip6_config;
		NMIP6Address *addr6;

		ip6_config = nm_device_get_ip6_config (best6);
		if (   !ip6_config
		    || (nm_ip6_config_get_num_nameservers (ip6_config) == 0)
		    || (nm_ip6_config_get_num_addresses (ip6_config) == 0)) {
			/* No valid IP6 config (!!); fall back to localhost.localdomain */
			_set_hostname (policy, TRUE, NULL, "no IPv6 config");
			return;
		}

		addr6 = nm_ip6_config_get_address (ip6_config, 0);
		g_assert (addr6); /* checked for > 1 address above */

		/* Start the hostname lookup thread */
		policy->lookup = hostname6_thread_new (nm_ip6_address_get_address (addr6), lookup_callback, policy);
	}

	if (!policy->lookup) {
		/* Fall back to 'localhost.localdomain' */
		_set_hostname (policy, TRUE, NULL, "error starting hostname thread");
	}
}
コード例 #8
0
ファイル: network.c プロジェクト: finid/cockpit
static void
synchronize_device (Network *self,
                    NMDevice *device)
{
  const char *iface_name = nm_device_get_iface (device);
  Netinterface *iface;
  NMIP4Config *ip4config;
  NMIP6Config *ip6config;

  iface = g_hash_table_lookup (self->ifname_to_netinterface,
                               iface_name);
  g_assert (iface);
  cockpit_network_netinterface_set_name (COCKPIT_NETWORK_NETINTERFACE (iface), iface_name);

  ip4config = nm_device_get_ip4_config (device);
  if (ip4config)
    {
      const GSList *addresses = nm_ip4_config_get_addresses (ip4config);
      const GSList *iter;
      GVariantBuilder builder;

      g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(yyyyu)"));

      for (iter = addresses; iter; iter = iter->next)
        {
          NMIP4Address *ip4addr = iter->data;
          guint32 ipaddr = ntohl (nm_ip4_address_get_address (ip4addr));
          guint32 prefix = nm_ip4_address_get_prefix (ip4addr);

          g_variant_builder_add_value (&builder, ip4_addr_variant_new (ipaddr, prefix));
        }

      cockpit_network_netinterface_set_ip4_addresses (COCKPIT_NETWORK_NETINTERFACE (iface), g_variant_builder_end (&builder));
    }
  ip6config = nm_device_get_ip6_config (device);
  if (ip6config)
    {
      const GSList *addresses = nm_ip6_config_get_addresses (ip6config);
      const GSList *iter;
      GVariantBuilder builder;

      g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(yyyyyyyyyyyyyyyyu)"));

      for (iter = addresses; iter; iter = iter->next)
        {
          NMIP6Address *ip6addr = iter->data;

          g_variant_builder_add_value (&builder, ip6_addr_variant_new (nm_ip6_address_get_address (ip6addr),
                                                                       nm_ip6_address_get_prefix (ip6addr)));
        }

      cockpit_network_netinterface_set_ip6_addresses (COCKPIT_NETWORK_NETINTERFACE (iface), g_variant_builder_end (&builder));
    }

  synchronize_device_config (self, iface, device);

  switch (nm_device_get_device_type (device))
    {
    case NM_DEVICE_TYPE_ETHERNET:
      synchronize_device_ethernet (self, iface, NM_DEVICE_ETHERNET (device));
      break;
    case NM_DEVICE_TYPE_WIFI:
      synchronize_device_wifi (self, iface, NM_DEVICE_WIFI (device));
      break;
    default:
      break;
    }
}
コード例 #9
0
static GSList *
construct_ip6_items (GSList *items, GHashTable *ip6_config, const char *prefix)
{
	GSList *addresses = NULL, *routes = NULL, *dns = NULL, *iter;
	guint32 num;
	GString *tmp;
	GValue *val;

	if (ip6_config == NULL)
		return items;

	if (prefix == NULL)
		prefix = "";

	/* IP addresses */
	val = g_hash_table_lookup (ip6_config, "addresses");
	if (val)
		addresses = nm_utils_ip6_addresses_from_gvalue (val);

	for (iter = addresses, num = 0; iter; iter = g_slist_next (iter)) {
		NMIP6Address *addr = (NMIP6Address *) iter->data;
		char str_addr[INET6_ADDRSTRLEN + 1];
		char str_gw[INET6_ADDRSTRLEN + 1];
		const struct in6_addr *tmp_addr;
		guint32 ip_prefix = nm_ip6_address_get_prefix (addr);
		char *addrtmp;

		memset (str_addr, 0, sizeof (str_addr));
		tmp_addr = nm_ip6_address_get_address (addr);
		if (!inet_ntop (AF_INET6, &tmp_addr, str_addr, sizeof (str_addr)))
			continue;

		memset (str_gw, 0, sizeof (str_gw));
		tmp_addr = nm_ip6_address_get_gateway (addr);
		inet_ntop (AF_INET6, &tmp_addr, str_gw, sizeof (str_gw));

		addrtmp = g_strdup_printf ("%sIP6_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 ("%sIP6_NUM_ADDRESSES=%d", prefix, num));
	if (addresses) {
		g_slist_foreach (addresses, (GFunc) nm_ip6_address_unref, NULL);
		g_slist_free (addresses);
	}

	/* DNS servers */
	val = g_hash_table_lookup (ip6_config, "nameservers");
	if (val)
		dns = nm_utils_ip6_dns_from_gvalue (val);

	if (g_slist_length (dns)) {
		tmp = g_string_new (NULL);
		g_string_append_printf (tmp, "%sIP6_NAMESERVERS=", prefix);

		for (iter = dns; iter; iter = g_slist_next (iter)) {
			const struct in6_addr *addr = iter->data;
			gboolean first = TRUE;
			char buf[INET6_ADDRSTRLEN + 1];

			memset (buf, 0, sizeof (buf));
			if (inet_ntop (AF_INET6, 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, ip6_config, prefix, '6');

	/* Static routes */
	val = g_hash_table_lookup (ip6_config, "routes");
	if (val)
		routes = nm_utils_ip6_routes_from_gvalue (val);

	for (iter = routes, num = 0; iter; iter = g_slist_next (iter)) {
		NMIP6Route *route = (NMIP6Route *) iter->data;
		char str_addr[INET6_ADDRSTRLEN + 1];
		char str_nh[INET6_ADDRSTRLEN + 1];
		const struct in6_addr *tmp_addr;
		guint32 ip_prefix = nm_ip6_route_get_prefix (route);
		guint32 metric = nm_ip6_route_get_metric (route);
		char *routetmp;

		memset (str_addr, 0, sizeof (str_addr));
		tmp_addr = nm_ip6_route_get_dest (route);
		if (!inet_ntop (AF_INET6, &tmp_addr, str_addr, sizeof (str_addr)))
			continue;

		memset (str_nh, 0, sizeof (str_nh));
		tmp_addr = nm_ip6_route_get_next_hop (route);
		inet_ntop (AF_INET6, &tmp_addr, str_nh, sizeof (str_nh));

		routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, num++, str_addr, ip_prefix, str_nh, metric);
		items = g_slist_prepend (items, routetmp);
	}
	if (num)
		items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ROUTES=%d", prefix, num));
	if (routes) {
		g_slist_foreach (routes, (GFunc) nm_ip6_route_unref, NULL);
		g_slist_free (routes);
	}

	return items;
}
コード例 #10
0
/* This is exactly identical to nm_utils_merge_ip4_config, with s/4/6/,
 * except that we can't compare addresses with ==.
 */
void
nm_utils_merge_ip6_config (NMIP6Config *ip6_config, NMSettingIP6Config *setting)
{
	int i, j;

	if (!setting)
		return; /* Defaults are just fine */

	if (nm_setting_ip6_config_get_ignore_auto_dns (setting)) {
		nm_ip6_config_reset_nameservers (ip6_config);
		nm_ip6_config_reset_domains (ip6_config);
		nm_ip6_config_reset_searches (ip6_config);
	}

	if (nm_setting_ip6_config_get_ignore_auto_routes (setting))
		nm_ip6_config_reset_routes (ip6_config);

	for (i = 0; i < nm_setting_ip6_config_get_num_dns (setting); i++) {
		const struct in6_addr *ns;
		gboolean found = FALSE;

		/* Avoid dupes */
		ns = nm_setting_ip6_config_get_dns (setting, i);
		for (j = 0; j < nm_ip6_config_get_num_nameservers (ip6_config); j++) {
			if (ip6_addresses_equal (nm_ip6_config_get_nameserver (ip6_config, j), ns)) {
				found = TRUE;
				break;
			}
		}

		if (!found)
			nm_ip6_config_add_nameserver (ip6_config, ns);
	}

	/* DNS search domains */
	for (i = 0; i < nm_setting_ip6_config_get_num_dns_searches (setting); i++) {
		const char *search = nm_setting_ip6_config_get_dns_search (setting, i);
		gboolean found = FALSE;

		/* Avoid dupes */
		for (j = 0; j < nm_ip6_config_get_num_searches (ip6_config); j++) {
			if (!strcmp (search, nm_ip6_config_get_search (ip6_config, j))) {
				found = TRUE;
				break;
			}
		}

		if (!found)
			nm_ip6_config_add_search (ip6_config, search);
	}

	/* IPv6 addresses */
	for (i = 0; i < nm_setting_ip6_config_get_num_addresses (setting); i++) {
		NMIP6Address *setting_addr = nm_setting_ip6_config_get_address (setting, i);
		guint32 num;

		num = nm_ip6_config_get_num_addresses (ip6_config);
		for (j = 0; j < num; j++) {
			NMIP6Address *cfg_addr = nm_ip6_config_get_address (ip6_config, j);

			/* Dupe, override with user-specified address */
			if (ip6_addresses_equal (nm_ip6_address_get_address (cfg_addr), nm_ip6_address_get_address (setting_addr))) {
				nm_ip6_config_replace_address (ip6_config, j, setting_addr);
				break;
			}
		}

		if (j == num)
			nm_ip6_config_add_address (ip6_config, setting_addr);
	}

	/* IPv6 routes */
	for (i = 0; i < nm_setting_ip6_config_get_num_routes (setting); i++) {
		NMIP6Route *setting_route = nm_setting_ip6_config_get_route (setting, i);
		guint32 num;

		num = nm_ip6_config_get_num_routes (ip6_config);
		for (j = 0; j < num; j++) {
			NMIP6Route *cfg_route = nm_ip6_config_get_route (ip6_config, j);

			/* Dupe, override with user-specified route */
			if (   ip6_addresses_equal (nm_ip6_route_get_dest (cfg_route), nm_ip6_route_get_dest (setting_route))
			    && (nm_ip6_route_get_prefix (cfg_route) == nm_ip6_route_get_prefix (setting_route))
				&& ip6_addresses_equal (nm_ip6_route_get_next_hop (cfg_route), nm_ip6_route_get_next_hop (setting_route))) {
				nm_ip6_config_replace_route (ip6_config, j, setting_route);
				break;
			}
		}

		if (j == num)
			nm_ip6_config_add_route (ip6_config, setting_route);
	}

	if (nm_setting_ip6_config_get_never_default (setting))
		nm_ip6_config_set_never_default (ip6_config, TRUE);
}
コード例 #11
0
ファイル: common.c プロジェクト: heftig/NetworkManager
gboolean
print_ip6_config (NMIP6Config *cfg6,
                  NmCli *nmc,
                  const char *group_prefix,
                  const char *one_field)
{
	GSList *list, *iter;
	const GPtrArray *ptr_array;
	char **addr_arr = NULL;
	char **route_arr = NULL;
	char **dns_arr = NULL;
	char **domain_arr = NULL;
	int i = 0;
	NmcOutputField *tmpl, *arr;
	size_t tmpl_len;

	if (cfg6 == NULL)
		return FALSE;

	tmpl = nmc_fields_ip6_config;
	tmpl_len = sizeof (nmc_fields_ip6_config);
	nmc->print_fields.indices = parse_output_fields (one_field ? one_field : NMC_FIELDS_IP6_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_ip6_config_get_addresses (cfg6);
	addr_arr = g_new (char *, g_slist_length (list) + 1);
	for (iter = list; iter; iter = g_slist_next (iter)) {
		NMIP6Address *addr = (NMIP6Address *) iter->data;
		guint32 prefix;
		char *ip_str, *gw_str;

		ip_str = nmc_ip6_address_as_string (nm_ip6_address_get_address (addr), NULL);
		prefix = nm_ip6_address_get_prefix (addr);
		gw_str = nmc_ip6_address_as_string (nm_ip6_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_ip6_config_get_routes (cfg6);
	route_arr = g_new (char *, g_slist_length (list) + 1);
	i = 0;
	for (iter = list; iter; iter = g_slist_next (iter)) {
		NMIP6Route *route = (NMIP6Route *) iter->data;
		guint32 prefix, metric;
		char *dest_str, *nexthop_str;

		dest_str = nmc_ip6_address_as_string (nm_ip6_route_get_dest (route), NULL);
		nexthop_str = nmc_ip6_address_as_string (nm_ip6_route_get_next_hop (route), NULL);
		prefix = nm_ip6_route_get_prefix (route);
		metric = nm_ip6_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 */
	list = (GSList *) nm_ip6_config_get_nameservers (cfg6);
	dns_arr = g_new (char *, g_slist_length (list) + 1);
	i = 0;
	for (iter = list; iter; iter = g_slist_next (iter))
		dns_arr[i++] = nmc_ip6_address_as_string (iter->data, NULL);

	dns_arr[i] = NULL;

	/* domains */
	ptr_array = nm_ip6_config_get_domains (cfg6);
	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;
	}

	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);
	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;
}
コード例 #12
0
ファイル: na-application.c プロジェクト: stfacc/gnome-usage
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);
            }
        }
    }
}