示例#1
0
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
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);
}