Exemplo n.º 1
0
ip_block *
convert_ip4_routes_block (const char *conn_name)
{
	gchar **ipset;
	guint length;
	guint i;
	gchar *ip;
	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];
		if (find_default_gateway_str (ip) || strstr (ip, "::")
		    || !find_gateway_str (ip))
			continue;
		ip = strip_string (ip, '"');
		iblock = create_ip4_block (ip);
		if (iblock == NULL)
			continue;
		iblock->next_hop = get_ip4_gateway (ip);
		if (start == NULL)
			start = current = iblock;
		else {
			current->next = iblock;
			current = iblock;
		}
	}
	g_strfreev (ipset);
	return start;
}
Exemplo n.º 2
0
ip_block *
convert_ip4_config_block (const char *conn_name)
{
	gchar **ipset;
	guint length;
	guint i;
	gchar *ip;
	char *def_gateway = NULL;
	const char *routes;
	ip_block *start = NULL, *current = NULL, *iblock = NULL;

	g_return_val_if_fail (conn_name != NULL, NULL);

	ipset = split_addresses (ifnet_get_data (conn_name, "config"));
	length = ipset ? g_strv_length (ipset) : 0;

	routes = ifnet_get_data (conn_name, "routes");
	if (routes)
		def_gateway = get_ip4_gateway (strstr (routes, "default"));

	for (i = 0; i < length; i++) {
		ip = ipset[i];
		ip = strip_string (ip, '"');
		iblock = create_ip4_block (ip);
		if (iblock == NULL)
			continue;
		if (!iblock->next_hop && def_gateway != NULL)
			iblock->next_hop = g_strdup (def_gateway);
		if (start == NULL)
			start = current = iblock;
		else {
			current->next = iblock;
			current = iblock;
		}
	}
	g_strfreev (ipset);
	g_free (def_gateway);
	return start;
}
ip_block *
convert_ip4_config_block (gchar * conn_name)
{
	gchar **ipset;
	guint length;
	guint i;
	gchar *ip;
	guint32 def_gateway;
	gchar *routes;
	gchar *pos;
	ip_block *start = NULL, *current = NULL, *iblock = NULL;
	gchar *pattern =
	    "((\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.)\\{(\\d{1,3})\\.\\.(\\d{1,3})\\}(/\\d{1,2}))";
	GRegex *regex = g_regex_new (pattern, 0, 0, NULL);

	g_return_val_if_fail (conn_name != NULL, NULL);
	ipset = g_strsplit (ifnet_get_data (conn_name, "config"), "\" \"", 0);
	length = g_strv_length (ipset);
	routes = ifnet_get_data (conn_name, "routes");
	if (routes)
		def_gateway = get_ip4_gateway (strstr (routes, "default"));
	else
		def_gateway = 0;
	for (i = 0; i < length; i++) {
		ip = ipset[i];
		ip = strip_string (ip, '"');
		//Handle ip like 192.168.4.{1..3}
		if ((pos = strchr (ip, '{')) != NULL) {
			gchar *ip_start, *ip_prefix;
			gchar *begin_str, *end_str;
			int begin, end, j;
			GMatchInfo *match_info;

			g_regex_match (regex, ip, 0, &match_info);
			if (!g_match_info_matches (match_info)) {
				g_match_info_free (match_info);
				continue;
			}
			begin_str = g_match_info_fetch (match_info, 3);
			end_str = g_match_info_fetch (match_info, 4);
			begin = atoi (begin_str);
			end = atoi (end_str);
			ip_start = g_match_info_fetch (match_info, 2);
			ip_prefix = g_match_info_fetch (match_info, 5);
			if (end < begin || begin < 1 || end > 254) {
				g_match_info_free (match_info);
				continue;
			}

			for (j = begin; j <= end; j++) {
				char suf[4];
				gchar *newip;

				sprintf (suf, "%d", j);
				newip =
				    g_strconcat (ip_start, suf, ip_prefix,
						 NULL);
				iblock = create_ip4_block (newip);
				if (iblock == NULL) {
					g_free (newip);
					continue;
				}
				if (!iblock->gateway && def_gateway != 0)
					iblock->gateway = def_gateway;
				if (start == NULL)
					start = current = iblock;
				else {
					current->next = iblock;
					current = iblock;
				}
				g_free (newip);
			}
			g_free (begin_str);
			g_free (end_str);
			g_free (ip_start);
			g_free (ip_prefix);
			g_match_info_free (match_info);
		} else {
			iblock = create_ip4_block (ip);
			if (iblock == NULL)
				continue;
			if (!iblock->gateway && def_gateway != 0)
				iblock->gateway = def_gateway;
			if (start == NULL)
				start = current = iblock;
			else {
				current->next = iblock;
				current = iblock;
			}
		}
	}
	g_strfreev (ipset);
	g_regex_unref (regex);
	return start;
}