static ip_block *
create_ip4_block (gchar * ip)
{
	ip_block *iblock = g_slice_new0 (ip_block);
	struct in_addr tmp_ip4_addr;
	int i;
	guint length;
	gchar **ip_mask;

	/* prefix format */
	if (strstr (ip, "/")) {
		gchar *prefix;

		ip_mask = g_strsplit (ip, "/", 0);
		length = g_strv_length (ip_mask);
		if (!inet_pton (AF_INET, ip_mask[0], &tmp_ip4_addr))
			goto error;
		iblock->ip = tmp_ip4_addr.s_addr;
		prefix = ip_mask[1];
		i = 0;
		while (i < length && isdigit (prefix[i]))
			i++;
		prefix[i] = '\0';
		iblock->netmask = nm_utils_ip4_prefix_to_netmask ((guint32)
								  atoi (ip_mask
									[1]));
	} else if (strstr (ip, "netmask")) {
		ip_mask = g_strsplit (ip, " ", 0);
		length = g_strv_length (ip_mask);
		if (!inet_pton (AF_INET, ip_mask[0], &tmp_ip4_addr))
			goto error;
		iblock->ip = tmp_ip4_addr.s_addr;
		i = 0;
		while (i < length && !strstr (ip_mask[++i], "netmask")) ;
		while (i < length && ip_mask[++i][0] == '\0') ;
		if (i >= length)
			goto error;
		if (!inet_pton (AF_INET, ip_mask[i], &tmp_ip4_addr))
			goto error;
		iblock->netmask = tmp_ip4_addr.s_addr;
	} else {
		g_slice_free (ip_block, iblock);
		if (!is_ip6_address (ip) && !strstr (ip, "dhcp"))
			PLUGIN_WARN (IFNET_PLUGIN_NAME,
				     "Can't handle ipv4 address: %s, missing netmask or prefix",
				     ip);
		return NULL;
	}
	g_strfreev (ip_mask);
	return iblock;
      error:
	if (!is_ip6_address (ip))
		PLUGIN_WARN (IFNET_PLUGIN_NAME, "Can't handle IPv4 address: %s",
			     ip);
	g_strfreev (ip_mask);
	g_slice_free (ip_block, iblock);
	return NULL;
}
Esempio n. 2
0
static ip_block *
create_ip4_block (gchar * ip)
{
	ip_block *iblock = g_slice_new0 (ip_block);
	guint32 tmp_ip4_addr;
	int i;
	guint length;
	gchar **ip_mask;

	/* prefix format */
	if (strstr (ip, "/")) {
		gchar *prefix;

		ip_mask = g_strsplit (ip, "/", 0);
		length = g_strv_length (ip_mask);
		if (!nm_utils_ipaddr_valid (AF_INET, ip_mask[0]))
			goto error;
		iblock->ip = g_strdup (ip_mask[0]);
		prefix = ip_mask[1];
		i = 0;
		while (i < length && g_ascii_isdigit (prefix[i]))
			i++;
		prefix[i] = '\0';
		iblock->prefix = (guint32) atoi (ip_mask[1]);
	} else if (strstr (ip, "netmask")) {
		ip_mask = g_strsplit (ip, " ", 0);
		length = g_strv_length (ip_mask);
		if (!nm_utils_ipaddr_valid (AF_INET, ip_mask[0]))
			goto error;
		iblock->ip = g_strdup (ip_mask[0]);
		i = 0;
		while (i < length && !strstr (ip_mask[++i], "netmask")) ;
		while (i < length && ip_mask[++i][0] == '\0') ;
		if (i >= length)
			goto error;
		if (!inet_pton (AF_INET, ip_mask[i], &tmp_ip4_addr))
			goto error;
		iblock->prefix = nm_utils_ip4_netmask_to_prefix (tmp_ip4_addr);
	} else {
		g_slice_free (ip_block, iblock);
		if (!is_ip6_address (ip) && !strstr (ip, "dhcp"))
			nm_log_warn (LOGD_SETTINGS, "Can't handle ipv4 address: %s, missing netmask or prefix", ip);
		return NULL;
	}
	if (iblock->prefix == 0 || iblock->prefix > 32) {
		nm_log_warn (LOGD_SETTINGS, "Can't handle ipv4 address: %s, invalid prefix", ip);
		goto error;
	}
	g_strfreev (ip_mask);
	return iblock;
error:
	if (!is_ip6_address (ip))
		nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 address: %s", ip);
	g_strfreev (ip_mask);
	g_free (iblock->ip);
	g_slice_free (ip_block, iblock);
	return NULL;
}
Esempio n. 3
0
static void
test_is_ip6_address (void)
{
	gchar *address1 = "4321:0:1:2:3:4:567:89ac/24";

	g_assert (is_ip6_address (address1));
}
static guint32
get_ip4_gateway (gchar * gateway)
{
	gchar *tmp, *split;
	struct in_addr tmp_ip4_addr;

	if (!gateway)
		return 0;
	tmp = find_gateway_str (gateway);
	if (!tmp) {
		PLUGIN_WARN (IFNET_PLUGIN_NAME,
			     "Couldn't obtain gateway in \"%s\"", gateway);
		return 0;
	}
	tmp = g_strdup (tmp);
	strip_string (tmp, ' ');
	strip_string (tmp, '"');
	if ((split = strstr (tmp, "\"")) != NULL)
		*split = '\0';
	if (!inet_pton (AF_INET, tmp, &tmp_ip4_addr))
		goto error;
	g_free (tmp);
	return tmp_ip4_addr.s_addr;
      error:
	if (!is_ip6_address (tmp))
		PLUGIN_WARN (IFNET_PLUGIN_NAME, "Can't handle IPv4 gateway: %s",
			     tmp);
	g_free (tmp);
	return 0;
}
Esempio n. 5
0
void
set_ip6_dns_servers (NMSettingIPConfig *s_ip6, const char *conn_name)
{
	const char *dns_servers;
	gchar **server_list, *stripped;
	guint length, i;
	struct in6_addr tmp_ip6_addr;

	dns_servers = ifnet_get_data (conn_name, "dns_servers");
	if (!dns_servers)
		return;

	stripped = g_strdup (dns_servers);
	strip_string (stripped, '"');
	server_list = g_strsplit (stripped, " ", 0);
	g_free (stripped);

	length = g_strv_length (server_list);
	if (length)
		g_object_set (s_ip6, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS,
			      TRUE, NULL);
	for (i = 0; i < length; i++) {
		g_strstrip (server_list[i]);
		if (server_list[i][0] == '\0')
			continue;
		if (!inet_pton (AF_INET6, server_list[i], &tmp_ip6_addr)) {
			if (is_ip6_address (server_list[i]))
				nm_log_warn (LOGD_SETTINGS, "ignored dns: %s\n", server_list[i]);
			continue;
		}
		if (!nm_setting_ip_config_add_dns (s_ip6, server_list[i]))
			nm_log_warn (LOGD_SETTINGS, "warning: duplicate DNS server %s", server_list[i]);
	}
	g_strfreev (server_list);
}
Esempio n. 6
0
static char *
get_ip4_gateway (gchar * gateway)
{
	gchar *tmp, *split;

	if (!gateway)
		return NULL;
	tmp = find_gateway_str (gateway);
	if (!tmp) {
		nm_log_warn (LOGD_SETTINGS, "Couldn't obtain gateway in \"%s\"", gateway);
		return NULL;
	}
	tmp = g_strdup (tmp);
	strip_string (tmp, ' ');
	strip_string (tmp, '"');

	// Only one gateway is selected
	if ((split = strstr (tmp, "\"")) != NULL)
		*split = '\0';

	if (!nm_utils_ipaddr_valid (AF_INET, tmp))
		goto error;
	return tmp;
error:
	if (!is_ip6_address (tmp))
		nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 gateway: %s", tmp);
	g_free (tmp);
	return NULL;
}
Esempio n. 7
0
static void
test_is_ip6_address ()
{
	gchar *address1 = "4321:0:1:2:3:4:567:89ac/24";

	ASSERT (is_ip6_address (address1), "is ip6 address",
		"%s should be a valid address", address1);
}
ip6_block *
convert_ip6_routes_block (gchar * conn_name)
{
	gchar **ipset;
	guint length;
	guint i;
	gchar *ip, *tmp_addr;
	gchar *routes;
	ip6_block *start = NULL, *current = NULL, *iblock = NULL;
	struct in6_addr *tmp_ip6_addr;

	g_return_val_if_fail (conn_name != NULL, NULL);
	routes = ifnet_get_data (conn_name, "routes");
	if (!routes)
		return NULL;
	ipset = g_strsplit (routes, "\" \"", 0);
	length = g_strv_length (ipset);
	for (i = 0; i < length; i++) {
		ip = ipset[i];
		ip = strip_string (ip, '"');
		if (ip[0] == '\0')
			continue;
		if ((tmp_addr = find_default_gateway_str (ip)) != NULL) {
			if (!is_ip6_address (tmp_addr))
				continue;
			else {
				tmp_ip6_addr = g_slice_new0 (struct in6_addr);

				if (inet_pton (AF_INET6, "::", tmp_ip6_addr)) {
					iblock = g_slice_new0 (ip6_block);
					iblock->ip = tmp_ip6_addr;
					iblock->prefix = 128;
				} else {
					g_slice_free (struct in6_addr,
						      tmp_ip6_addr);
					continue;
				}
			}
		} else
			iblock = create_ip6_block (ip);
		if (iblock == NULL)
			continue;
		iblock->next_hop = get_ip6_next_hop (ip);
		if (iblock->next_hop == NULL) {
			destroy_ip6_block (iblock);
			continue;
		}
		if (start == NULL)
			start = current = iblock;
		else {
			current->next = iblock;
			current = iblock;
		}
	}
Esempio n. 9
0
ip_block *
convert_ip6_routes_block (const char *conn_name)
{
	gchar **ipset;
	guint length;
	guint i;
	gchar *ip, *tmp_addr;
	ip_block *start = NULL, *current = NULL, *iblock = NULL;

	g_return_val_if_fail (conn_name != NULL, NULL);
	ipset = split_routes (ifnet_get_data (conn_name, "routes"));
	length = ipset ? g_strv_length (ipset) : 0;
	for (i = 0; i < length; i++) {
		ip = ipset[i];
		ip = strip_string (ip, '"');
		if (ip[0] == '\0')
			continue;
		if ((tmp_addr = find_default_gateway_str (ip)) != NULL) {
			if (!is_ip6_address (tmp_addr))
				continue;
			else {
				iblock = g_slice_new0 (ip_block);
				iblock->ip = g_strdup ("::");
				iblock->prefix = 128;
			}
		} else
			iblock = create_ip_block (ip);
		if (iblock == NULL)
			continue;
		iblock->next_hop = get_ip6_next_hop (ip);
		if (iblock->next_hop == NULL) {
			destroy_ip_block (iblock);
			continue;
		}
		if (start == NULL)
			start = current = iblock;
		else {
			current->next = iblock;
			current = iblock;
		}
	}
	g_strfreev (ipset);
	return start;
}
gboolean
has_ip6_address (gchar * conn_name)
{
	gchar **ipset;
	guint length;
	guint i;

	g_return_val_if_fail (conn_name != NULL, FALSE);
	ipset = g_strsplit (ifnet_get_data (conn_name, "config"), "\" \"", 0);
	length = g_strv_length (ipset);
	for (i = 0; i < length; i++) {
		if (!is_ip6_address (ipset[i]))
			continue;
		else {
			g_strfreev (ipset);
			return TRUE;
		}

	}
	g_strfreev (ipset);
	return FALSE;
}