static void test_classless_static_routes (void) { GHashTable *options; NMIP4Config *ip4_config; 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_route2_dest = "10.0.0.0"; const char *expected_route2_gw = "10.17.66.41"; options = fill_table (generic_options, NULL); options = fill_table (classless_routes_options, options); ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-rfc3442", "failed to parse DHCP4 options"); /* IP4 routes */ ASSERT (nm_ip4_config_get_num_routes (ip4_config) == 2, "dhcp-rfc3442", "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", "couldn't convert expected route destination #1"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "dhcp-rfc3442", "unexpected route #1 destination"); ASSERT (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0, "dhcp-rfc3442", "couldn't convert expected route next hop #1"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "dhcp-rfc3442", "unexpected route #1 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 24, "dhcp-rfc3442", "unexpected route #1 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "dhcp-rfc3442", "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-rfc3442", "couldn't convert expected route destination #2"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "dhcp-rfc3442", "unexpected route #2 destination"); ASSERT (inet_pton (AF_INET, expected_route2_gw, &tmp) > 0, "dhcp-rfc3442", "couldn't convert expected route next hop #2"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "dhcp-rfc3442", "unexpected route #2 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 8, "dhcp-rfc3442", "unexpected route #2 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "dhcp-rfc3442", "unexpected route #2 metric"); g_hash_table_destroy (options); }
static void ip4_test_route (const char *test, NMIP4Config *ip4_config, guint route_num, const char *expected_dest, const char *expected_gw, guint expected_prefix) { NMIP4Route *route; struct in_addr tmp; route = nm_ip4_config_get_route (ip4_config, route_num); ASSERT (inet_pton (AF_INET, expected_dest, &tmp) > 0, test, "couldn't convert expected route destination #1"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, test, "unexpected route %d destination", route_num + 1); ASSERT (inet_pton (AF_INET, expected_gw, &tmp) > 0, test, "couldn't convert expected route next hop %d", route_num + 1); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, test, "unexpected route %d next hop", route_num + 1); ASSERT (nm_ip4_route_get_prefix (route) == expected_prefix, test, "unexpected route %d prefix", route_num + 1); ASSERT (nm_ip4_route_get_metric (route) == 0, test, "unexpected route %d metric", route_num + 1); }
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_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); }
void nm_utils_merge_ip4_config (NMIP4Config *ip4_config, NMSettingIP4Config *setting) { int i, j; gboolean setting_never_default; if (!setting) return; /* Defaults are just fine */ if (nm_setting_ip4_config_get_ignore_auto_dns (setting)) { nm_ip4_config_reset_nameservers (ip4_config); nm_ip4_config_reset_domains (ip4_config); nm_ip4_config_reset_searches (ip4_config); } if (nm_setting_ip4_config_get_ignore_auto_routes (setting)) nm_ip4_config_reset_routes (ip4_config); for (i = 0; i < nm_setting_ip4_config_get_num_dns (setting); i++) { guint32 ns; gboolean found = FALSE; /* Avoid dupes */ ns = nm_setting_ip4_config_get_dns (setting, i); for (j = 0; j < nm_ip4_config_get_num_nameservers (ip4_config); j++) { if (nm_ip4_config_get_nameserver (ip4_config, j) == ns) { found = TRUE; break; } } if (!found) nm_ip4_config_add_nameserver (ip4_config, ns); } /* DNS search domains */ for (i = 0; i < nm_setting_ip4_config_get_num_dns_searches (setting); i++) { const char *search = nm_setting_ip4_config_get_dns_search (setting, i); gboolean found = FALSE; /* Avoid dupes */ for (j = 0; j < nm_ip4_config_get_num_searches (ip4_config); j++) { if (!strcmp (search, nm_ip4_config_get_search (ip4_config, j))) { found = TRUE; break; } } if (!found) nm_ip4_config_add_search (ip4_config, search); } /* IPv4 addresses */ for (i = 0; i < nm_setting_ip4_config_get_num_addresses (setting); i++) { NMIP4Address *setting_addr = nm_setting_ip4_config_get_address (setting, i); guint32 num; num = nm_ip4_config_get_num_addresses (ip4_config); for (j = 0; j < num; j++) { NMIP4Address *cfg_addr = nm_ip4_config_get_address (ip4_config, j); /* Dupe, override with user-specified address */ if (nm_ip4_address_get_address (cfg_addr) == nm_ip4_address_get_address (setting_addr)) { nm_ip4_config_replace_address (ip4_config, j, setting_addr); break; } } if (j == num) nm_ip4_config_add_address (ip4_config, setting_addr); } /* IPv4 routes */ for (i = 0; i < nm_setting_ip4_config_get_num_routes (setting); i++) { NMIP4Route *setting_route = nm_setting_ip4_config_get_route (setting, i); guint32 num; num = nm_ip4_config_get_num_routes (ip4_config); for (j = 0; j < num; j++) { NMIP4Route *cfg_route = nm_ip4_config_get_route (ip4_config, j); /* Dupe, override with user-specified route */ if ( (nm_ip4_route_get_dest (cfg_route) == nm_ip4_route_get_dest (setting_route)) && (nm_ip4_route_get_prefix (cfg_route) == nm_ip4_route_get_prefix (setting_route)) && (nm_ip4_route_get_next_hop (cfg_route) == nm_ip4_route_get_next_hop (setting_route))) { nm_ip4_config_replace_route (ip4_config, j, setting_route); break; } } if (j == num) nm_ip4_config_add_route (ip4_config, setting_route); } setting_never_default = nm_setting_ip4_config_get_never_default (setting); if (nm_setting_ip4_config_get_ignore_auto_routes (setting)) nm_ip4_config_set_never_default (ip4_config, setting_never_default); else { if (setting_never_default) nm_ip4_config_set_never_default (ip4_config, TRUE); } }
static void dump_ip4_to_props (NMIP4Config *ip4, GVariantBuilder *builder) { GVariantBuilder int_builder; guint n, i; const NMPlatformIP4Address *addr; const NMPlatformIP4Route *route; guint32 array[4]; /* Addresses */ g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("aau")); n = nm_ip4_config_get_num_addresses (ip4); for (i = 0; i < n; i++) { addr = nm_ip4_config_get_address (ip4, i); array[0] = addr->address; array[1] = addr->plen; array[2] = (i == 0) ? nm_ip4_config_get_gateway (ip4) : 0; g_variant_builder_add (&int_builder, "@au", g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, array, 3, sizeof (guint32))); } g_variant_builder_add (builder, "{sv}", "addresses", g_variant_builder_end (&int_builder)); /* DNS servers */ g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("au")); n = nm_ip4_config_get_num_nameservers (ip4); for (i = 0; i < n; i++) g_variant_builder_add (&int_builder, "u", nm_ip4_config_get_nameserver (ip4, i)); g_variant_builder_add (builder, "{sv}", "nameservers", g_variant_builder_end (&int_builder)); /* Search domains */ g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("as")); n = nm_ip4_config_get_num_domains (ip4); for (i = 0; i < n; i++) g_variant_builder_add (&int_builder, "s", nm_ip4_config_get_domain (ip4, i)); g_variant_builder_add (builder, "{sv}", "domains", g_variant_builder_end (&int_builder)); /* WINS servers */ g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("au")); n = nm_ip4_config_get_num_wins (ip4); for (i = 0; i < n; i++) g_variant_builder_add (&int_builder, "u", nm_ip4_config_get_wins (ip4, i)); g_variant_builder_add (builder, "{sv}", "wins-servers", g_variant_builder_end (&int_builder)); /* Static routes */ g_variant_builder_init (&int_builder, G_VARIANT_TYPE ("aau")); n = nm_ip4_config_get_num_routes (ip4); for (i = 0; i < n; i++) { route = nm_ip4_config_get_route (ip4, i); array[0] = route->network; array[1] = route->plen; array[2] = route->gateway; array[3] = route->metric; g_variant_builder_add (&int_builder, "@au", g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, array, 4, sizeof (guint32))); } g_variant_builder_add (builder, "{sv}", "routes", g_variant_builder_end (&int_builder)); }
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 test_invalid_classless_routes2 (void) { GHashTable *options; NMIP4Config *ip4_config; NMIP4Route *route; struct in_addr tmp; 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); options = fill_table (invalid_classless_routes2, options); ip4_config = nm_dhcp_manager_options_to_ip4_config ("eth0", options); ASSERT (ip4_config != NULL, "dhcp-rfc3442-invalid-2", "failed to parse DHCP4 options"); /* Test falling back to old-style static routes if the classless static * routes are invalid. */ /* Routes */ ASSERT (nm_ip4_config_get_num_routes (ip4_config) == 2, "dhcp-rfc3442-invalid-2", "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-rfc3442-invalid-2", "couldn't convert expected route destination #1"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "dhcp-rfc3442-invalid-2", "unexpected route #1 destination"); ASSERT (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0, "dhcp-rfc3442-invalid-2", "couldn't convert expected route next hop #1"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "dhcp-rfc3442-invalid-2", "unexpected route #1 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 32, "dhcp-rfc3442-invalid-2", "unexpected route #1 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "dhcp-rfc3442-invalid-2", "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-rfc3442-invalid-2", "couldn't convert expected route destination #2"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "dhcp-rfc3442-invalid-2", "unexpected route #2 destination"); ASSERT (inet_pton (AF_INET, expected_route2_gw, &tmp) > 0, "dhcp-rfc3442-invalid-2", "couldn't convert expected route next hop #2"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "dhcp-rfc3442-invalid-2", "unexpected route #2 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 32, "dhcp-rfc3442-invalid-2", "unexpected route #2 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "dhcp-rfc3442-invalid-2", "unexpected route #2 metric"); g_hash_table_destroy (options); }