diff options
author | Dan Winship <danw@gnome.org> | 2014-09-16 16:42:46 -0400 |
---|---|---|
committer | Dan Winship <danw@gnome.org> | 2014-11-07 07:49:40 -0500 |
commit | 21c8a6b20effbe1e689505a0cbb23594be06068c (patch) | |
tree | 15e43867853d0242950e40e049499e1c2d29cc78 | |
parent | 303e84e65e5b9b5a403e4f8366e094447d51a9fa (diff) | |
download | NetworkManager-21c8a6b20effbe1e689505a0cbb23594be06068c.tar.gz |
libnm-core, all: merge IPv4 and IPv6 address/route types
Merge NMIP4Address and NMIP6Address into NMIPAddress, and NMIP4Route
and NMIP6Route into NMIPRoute. The new types represent IP addresses as
strings, rather than in binary, and so are address-family agnostic.
51 files changed, 2796 insertions, 4502 deletions
diff --git a/callouts/nm-dispatcher-utils.c b/callouts/nm-dispatcher-utils.c index 66b6a07fd8..d2deb41fdf 100644 --- a/callouts/nm-dispatcher-utils.c +++ b/callouts/nm-dispatcher-utils.c @@ -95,8 +95,6 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) char **dns, **wins; GString *tmp; GVariant *val; - char str_addr[INET_ADDRSTRLEN]; - char str_gw[INET_ADDRSTRLEN]; int i; if (ip4_config == NULL) @@ -111,14 +109,18 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) addresses = nm_utils_ip4_addresses_from_variant (val); for (i = 0; i < addresses->len; i++) { - NMIP4Address *addr = addresses->pdata[i]; - guint32 ip_prefix = nm_ip4_address_get_prefix (addr); + NMIPAddress *addr = addresses->pdata[i]; + const char *gw; char *addrtmp; - nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), str_addr); - nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), str_gw); + gw = nm_ip_address_get_gateway (addr); + if (!gw) + gw = "0.0.0.0"; - addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, i, str_addr, ip_prefix, str_gw); + addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, i, + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr), + gw); items = g_slist_prepend (items, addrtmp); } if (addresses->len) @@ -177,15 +179,19 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) routes = nm_utils_ip4_routes_from_variant (val); for (i = 0; i < routes->len; i++) { - NMIP4Route *route = routes->pdata[i]; - guint32 ip_prefix = nm_ip4_route_get_prefix (route); - guint32 metric = nm_ip4_route_get_metric (route); + NMIPRoute *route = routes->pdata[i]; + const char *next_hop; char *routetmp; - nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), str_addr); - nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), str_gw); + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "0.0.0.0"; - routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, i, str_addr, ip_prefix, str_gw, metric); + routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, i, + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop, + nm_ip_route_get_metric (route)); items = g_slist_prepend (items, routetmp); } items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, routes->len)); @@ -225,8 +231,6 @@ construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) char **dns; GString *tmp; GVariant *val; - char str_addr[INET6_ADDRSTRLEN]; - char str_gw[INET6_ADDRSTRLEN]; int i; if (ip6_config == NULL) @@ -241,14 +245,18 @@ construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) addresses = nm_utils_ip6_addresses_from_variant (val); for (i = 0; i < addresses->len; i++) { - NMIP6Address *addr = addresses->pdata[i]; - guint32 ip_prefix = nm_ip6_address_get_prefix (addr); + NMIPAddress *addr = addresses->pdata[i]; + const char *gw; char *addrtmp; - nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), str_addr); - nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), str_gw); + gw = nm_ip_address_get_gateway (addr); + if (!gw) + gw = "::"; - addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, i, str_addr, ip_prefix, str_gw); + addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, i, + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr), + gw); items = g_slist_prepend (items, addrtmp); } if (addresses->len) @@ -287,15 +295,19 @@ construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) routes = nm_utils_ip6_routes_from_variant (val); for (i = 0; i < routes->len; i++) { - NMIP6Route *route = routes->pdata[i]; - guint32 ip_prefix = nm_ip6_route_get_prefix (route); - guint32 metric = nm_ip6_route_get_metric (route); + NMIPRoute *route = routes->pdata[i]; + const char *next_hop; char *routetmp; - nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), str_addr); - nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), str_gw); + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "::"; - routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, i, str_addr, ip_prefix, str_gw, metric); + routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, i, + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop, + nm_ip_route_get_metric (route)); items = g_slist_prepend (items, routetmp); } if (routes->len) diff --git a/callouts/tests/test-dispatcher-envp.c b/callouts/tests/test-dispatcher-envp.c index a8c65f8413..5daf5e9a64 100644 --- a/callouts/tests/test-dispatcher-envp.c +++ b/callouts/tests/test-dispatcher-envp.c @@ -218,32 +218,29 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err g_free (tmp); if (g_strv_length (split) > 0) { - addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_address_unref); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); for (iter = split; iter && *iter; iter++) { - NMIP4Address *addr; - guint32 a; - char *p; + NMIPAddress *addr; + char *ip, *prefix, *gw; if (strlen (g_strstrip (*iter)) == 0) continue; - addr = nm_ip4_address_new (); + ip = *iter; - p = strchr (*iter, '/'); - g_assert (p); - *p++ = '\0'; + prefix = strchr (ip, '/'); + g_assert (prefix); + *prefix++ = '\0'; - g_assert_cmpint (inet_pton (AF_INET, *iter, &a), ==, 1); - nm_ip4_address_set_address (addr, a); - nm_ip4_address_set_prefix (addr, (guint) atoi (p)); - - p = strchr (p, ' '); - g_assert (p); - p++; - - g_assert_cmpint (inet_pton (AF_INET, p, &a), ==, 1); - nm_ip4_address_set_gateway (addr, a); + gw = strchr (prefix, ' '); + g_assert (gw); + gw++; + addr = nm_ip_address_new (AF_INET, ip, (guint) atoi (prefix), gw, error); + if (!addr) { + g_ptr_array_unref (addresses); + return FALSE; + } g_ptr_array_add (addresses, addr); } @@ -261,37 +258,36 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err g_free (tmp); if (g_strv_length (split) > 0) { - routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); for (iter = split; iter && *iter; iter++) { - NMIP4Route *route; - guint32 a; - char *p; + NMIPRoute *route; + char *dest, *prefix, *next_hop, *metric; if (strlen (g_strstrip (*iter)) == 0) continue; - route = nm_ip4_route_new (); - - p = strchr (*iter, '/'); - g_assert (p); - *p++ = '\0'; - - g_assert_cmpint (inet_pton (AF_INET, *iter, &a), ==, 1); - nm_ip4_route_set_dest (route, a); - nm_ip4_route_set_prefix (route, (guint) atoi (p)); + dest = *iter; - p = strchr (p, ' '); - g_assert (p); - p++; + prefix = strchr (dest, '/'); + g_assert (prefix); + *prefix++ = '\0'; - g_assert_cmpint (inet_pton (AF_INET, p, &a), ==, 1); - nm_ip4_route_set_next_hop (route, a); + next_hop = strchr (prefix, ' '); + g_assert (next_hop); + next_hop++; - p = strchr (p, ' '); - g_assert (p); - p++; - nm_ip4_route_set_metric (route, (guint) atoi (p)); + metric = strchr (next_hop, ' '); + g_assert (metric); + metric++; + route = nm_ip_route_new (AF_INET, + dest, (guint) atoi (prefix), + next_hop, (guint) atoi (metric), + error); + if (!route) { + g_ptr_array_unref (routes); + return FALSE; + } g_ptr_array_add (routes, route); } diff --git a/clients/cli/common.c b/clients/cli/common.c index 0a7541c976..e7382b985b 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -30,6 +30,8 @@ #include <readline/readline.h> #include <readline/history.h> +#include "nm-glib-compat.h" + #include "common.h" #include "utils.h" @@ -104,17 +106,17 @@ print_ip4_config (NMIP4Config *cfg4, if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { - NMIP4Address *addr = (NMIP4Address *) g_ptr_array_index (ptr_array, i); - guint32 prefix; - char *ip_str, *gw_str; + NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i); + const char *gw; - ip_str = nmc_ip4_address_as_string (nm_ip4_address_get_address (addr), NULL); - prefix = nm_ip4_address_get_prefix (addr); - gw_str = nmc_ip4_address_as_string (nm_ip4_address_get_gateway (addr), NULL); + gw = nm_ip_address_get_gateway (addr); + if (!gw) + gw = "0.0.0.0"; - 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] = g_strdup_printf ("ip = %s/%u, gw = %s", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr), + gw); } addr_arr[i] = NULL; } @@ -124,18 +126,18 @@ print_ip4_config (NMIP4Config *cfg4, if (ptr_array) { route_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { - NMIP4Route *route = (NMIP4Route *) g_ptr_array_index (ptr_array, i); - guint32 prefix, metric; - char *dest_str, *nexthop_str; - - dest_str = nmc_ip4_address_as_string (nm_ip4_route_get_dest (route), NULL); - nexthop_str = nmc_ip4_address_as_string (nm_ip4_route_get_next_hop (route), NULL); - prefix = nm_ip4_route_get_prefix (route); - metric = nm_ip4_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); + NMIPRoute *route = (NMIPRoute *) g_ptr_array_index (ptr_array, i); + const char *next_hop; + + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "0.0.0.0"; + + route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop, + nm_ip_route_get_metric (route)); } route_arr[i] = NULL; } @@ -196,17 +198,17 @@ print_ip6_config (NMIP6Config *cfg6, if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { - NMIP6Address *addr = (NMIP6Address *) g_ptr_array_index (ptr_array, i); - guint32 prefix; - char *ip_str, *gw_str; + NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i); + const char *gw; - 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); + gw = nm_ip_address_get_gateway (addr); + if (!gw) + gw = "::"; - 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] = g_strdup_printf ("ip = %s/%u, gw = %s", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr), + gw); } addr_arr[i] = NULL; } @@ -216,18 +218,18 @@ print_ip6_config (NMIP6Config *cfg6, if (ptr_array) { route_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { - NMIP6Route *route = (NMIP6Route *) g_ptr_array_index (ptr_array, i); - 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); + NMIPRoute *route = (NMIPRoute *) g_ptr_array_index (ptr_array, i); + const char *next_hop; + + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "::"; + + route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop, + nm_ip_route_get_metric (route)); } route_arr[i] = NULL; } @@ -351,18 +353,20 @@ print_dhcp6_config (NMDhcp6Config *dhcp6, } /* - * Parse IPv4 address from string to NMIP4Address stucture. - * ip_str is the IPv4 address in the form address/prefix + * Parse IP address from string to NMIPAddress stucture. + * ip_str is the IP address in the form address/prefix * gw_str is the gateway address (it is optional) */ -NMIP4Address * -nmc_parse_and_build_ip4_address (const char *ip_str, const char *gw_str, GError **error) +NMIPAddress * +nmc_parse_and_build_address (int family, const char *ip_str, const char *gw_str, GError **error) { - NMIP4Address *addr = NULL; - guint32 ip4_addr, gw_addr; + int max_prefix = (family == AF_INET) ? 32 : 128; + NMIPAddress *addr = NULL; + const char *ip; char *tmp; char *plen; long int prefix; + GError *local = NULL; g_return_val_if_fail (ip_str != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); @@ -372,158 +376,71 @@ nmc_parse_and_build_ip4_address (const char *ip_str, const char *gw_str, GError if (plen) *plen++ = '\0'; - if (inet_pton (AF_INET, tmp, &ip4_addr) < 1) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid IPv4 address '%s'"), tmp); - goto finish; - } + ip = tmp; - prefix = 32; + prefix = max_prefix; if (plen) { - if (!nmc_string_to_int (plen, TRUE, 1, 32, &prefix)) { + if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &prefix)) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid prefix '%s'; <1-32> allowed"), plen); + _("invalid prefix '%s'; <1-%d> allowed"), plen, max_prefix); goto finish; } } - if (inet_pton (AF_INET, gw_str ? gw_str : "0.0.0.0", &gw_addr) < 1) { + addr = nm_ip_address_new (family, ip, (guint32) prefix, gw_str, &local); + if (!addr) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid gateway '%s'"), gw_str); - goto finish; + _("invalid IP address: %s"), local->message); + g_clear_error (&local); } - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip4_addr); - nm_ip4_address_set_prefix (addr, (guint32) prefix); - nm_ip4_address_set_gateway (addr, gw_addr); - finish: g_free (tmp); return addr; } /* - * Parse IPv6 address from string to NMIP6Address stucture. - * ip_str is the IPv6 address in the form address/prefix - * gw_str is the gateway address (it is optional) - */ -NMIP6Address * -nmc_parse_and_build_ip6_address (const char *ip_str, const char *gw_str, GError **error) -{ - NMIP6Address *addr = NULL; - struct in6_addr ip_addr, gw_addr; - char *tmp; - char *plen; - long int prefix; - - g_return_val_if_fail (ip_str != NULL, NULL); - g_return_val_if_fail (error == NULL || *error == NULL, NULL); - - tmp = g_strdup (ip_str); - plen = strchr (tmp, '/'); /* prefix delimiter */ - if (plen) - *plen++ = '\0'; - - if (inet_pton (AF_INET6, tmp, &ip_addr) < 1) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid IPv6 address '%s'"), tmp); - goto finish; - } - - prefix = 128; - if (plen) { - if (!nmc_string_to_int (plen, TRUE, 1, 128, &prefix)) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid prefix '%s'; <1-128> allowed"), plen); - goto finish; - } - } - - if (inet_pton (AF_INET6, gw_str ? gw_str : "::", &gw_addr) < 1) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid gateway '%s'"), gw_str); - goto finish; - } - - addr = nm_ip6_address_new (); - nm_ip6_address_set_address (addr, &ip_addr); - nm_ip6_address_set_prefix (addr, (guint32) prefix); - nm_ip6_address_set_gateway (addr, &gw_addr); - -finish: - g_free (tmp); - return addr; -} - -typedef struct { - long int prefix; - long int metric; - union _IpDest { - guint32 ip4_dst; - struct in6_addr ip6_dst; - } dst; - union _IpNextHop { - guint32 ip4_nh; - struct in6_addr ip6_nh; - } nh; -} ParsedRoute; - -/* - * _parse_and_build_route: + * nmc_parse_and_build_route: * @family: AF_INET or AF_INET6 * @first: the route destination in the form of "address/prefix" (/prefix is optional) * @second: (allow-none): next hop address, if third is not NULL. Otherwise it could be either next hop address or metric. (It can be NULL when @third is NULL). * @third: (allow-none): route metric - * @out: (out): route struct to fill * @error: location to store GError * - * Parse route from strings and fill @out parameter. + * Parse route from strings and return an #NMIPRoute * * Returns: %TRUE on success, %FALSE on failure */ -static gboolean -_parse_and_build_route (int family, - const char *first, - const char *second, - const char *third, - ParsedRoute *out, - GError **error) +NMIPRoute * +nmc_parse_and_build_route (int family, + const char *first, + const char *second, + const char *third, + GError **error) { - int max_prefix; - char *tmp, *plen; + int max_prefix = (family == AF_INET) ? 32 : 128; + char *dest = NULL, *plen = NULL; + const char *next_hop = NULL; + const char *canon_dest; + long int prefix = max_prefix, metric = 0; + NMIPRoute *route = NULL; gboolean success = FALSE; + GError *local = NULL; g_return_val_if_fail (family == AF_INET || family == AF_INET6, FALSE); g_return_val_if_fail (first != NULL, FALSE); g_return_val_if_fail (second || !third, FALSE); - g_return_val_if_fail (out, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - max_prefix = (family == AF_INET) ? 32 : 128; - /* initialize default values */ - out->prefix = max_prefix; - out->metric = 0; - if (family == AF_INET) - out->nh.ip4_nh = 0; - else - out->nh.ip6_nh = in6addr_any; - - tmp = g_strdup (first); - plen = strchr (tmp, '/'); /* prefix delimiter */ + dest = g_strdup (first); + plen = strchr (dest, '/'); /* prefix delimiter */ if (plen) *plen++ = '\0'; - if (inet_pton (family, tmp, family == AF_INET ? (void *) &out->dst.ip4_dst : (void *) &out->dst.ip6_dst) < 1) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid route destination address '%s'"), tmp); - goto finish; - } - if (plen) { - if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &out->prefix)) { + if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &prefix)) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, _("invalid prefix '%s'; <1-%d> allowed"), plen, max_prefix); @@ -532,113 +449,49 @@ _parse_and_build_route (int family, } if (second) { - if (inet_pton (family, second, family == AF_INET ? (void *) &out->nh.ip4_nh : (void *) &out->nh.ip6_nh) < 1) { - if (third) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid next hop address '%s'"), second); + if (third || nm_utils_ipaddr_valid (family, second)) + next_hop = second; + else { + /* 'second' can be a metric */ + if (!nmc_string_to_int (second, TRUE, 0, G_MAXUINT32, &metric)) { + g_set_error (error, 1, 0, _("the second component of route ('%s') is neither " + "a next hop address nor a metric"), second); goto finish; - } else { - /* 'second' can be a metric */ - if (!nmc_string_to_int (second, TRUE, 0, G_MAXUINT32, &out->metric)) { - g_set_error (error, 1, 0, _("the second component of route ('%s') is neither " - "a next hop address nor a metric"), second); - goto finish; - } } } } if (third) { - if (!nmc_string_to_int (third, TRUE, 0, G_MAXUINT32, &out->metric)) { + if (!nmc_string_to_int (third, TRUE, 0, G_MAXUINT32, &metric)) { g_set_error (error, 1, 0, _("invalid metric '%s'"), third); goto finish; } } - /* We don't accept default routes as NetworkManager handles it itself */ - if ( (family == AF_INET && out->dst.ip4_dst == 0) - || (family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED (&out->dst.ip6_dst))) { + route = nm_ip_route_new (family, dest, prefix, next_hop, metric, &local); + if (!route) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("invalid route: %s"), local->message); + g_clear_error (&local); + goto finish; + } + + /* We don't accept default routes as NetworkManager handles it + * itself. But we have to check this after @route has normalized the + * dest string. + */ + canon_dest = nm_ip_route_get_dest (route); + if (!strcmp (canon_dest, "0.0.0.0") || !strcmp (canon_dest, "::")) { g_set_error_literal (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, _("default route cannot be added (NetworkManager handles it by itself)")); + g_clear_pointer (&route, nm_ip_route_unref); goto finish; } success = TRUE; finish: - g_free (tmp); - return success; -} - -/* - * nmc_parse_and_build_ip4_route: - * @first: the IPv4 route destination in the form of "address/prefix" - (/prefix is optional) - * @second: (allow-none): next hop address, if third is not NULL. Otherwise it could be - either next hop address or metric. (It can be NULL when @third is NULL). - * @third: (allow-none): route metric - * @error: location to store GError - * - * Parse IPv4 route from strings to NMIP4Route stucture. - * - * Returns: route as a NMIP4Route object, or %NULL on failure - */ -NMIP4Route * -nmc_parse_and_build_ip4_route (const char *first, - const char *second, - const char *third, - GError **error) -{ - ParsedRoute tmp_route; - NMIP4Route *route = NULL; - - g_return_val_if_fail (first != NULL, NULL); - g_return_val_if_fail (second || !third, NULL); - g_return_val_if_fail (error == NULL || *error == NULL, NULL); - - if (_parse_and_build_route (AF_INET, first, second, third, &tmp_route, error)) { - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, tmp_route.dst.ip4_dst); - nm_ip4_route_set_prefix (route, (guint32) tmp_route.prefix); - nm_ip4_route_set_next_hop (route, tmp_route.nh.ip4_nh); - nm_ip4_route_set_metric (route, (guint32) tmp_route.metric); - } - return route; -} - -/* - * nmc_parse_and_build_ip6_route: - * @first: the IPv6 route destination in the form of "address/prefix" - (/prefix is optional) - * @second: (allow-none): next hop address, if third is not NULL. Otherwise it could be - either next hop address or metric. (It can be NULL when @third is NULL). - * @third: (allow-none): route metric - * @error: location to store GError - * - * Parse IPv6 route from strings to NMIP6Route stucture. - * - * Returns: route as a NMIP6Route object, or %NULL on failure - */ -NMIP6Route * -nmc_parse_and_build_ip6_route (const char *first, - const char *second, - const char *third, - GError **error) -{ - ParsedRoute tmp_route; - NMIP6Route *route = NULL; - - g_return_val_if_fail (first != NULL, NULL); - g_return_val_if_fail (second || !third, NULL); - g_return_val_if_fail (error == NULL || *error == NULL, NULL); - - if (_parse_and_build_route (AF_INET6, first, second, third, &tmp_route, error)) { - route = nm_ip6_route_new (); - nm_ip6_route_set_dest (route, &tmp_route.dst.ip6_dst); - nm_ip6_route_set_prefix (route, (guint32) tmp_route.prefix); - nm_ip6_route_set_next_hop (route, &tmp_route.nh.ip6_nh); - nm_ip6_route_set_metric (route, (guint32) tmp_route.metric); - } + g_free (dest); return route; } diff --git a/clients/cli/common.h b/clients/cli/common.h index 4f43674793..d608ef5ce0 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -29,11 +29,8 @@ gboolean print_ip6_config (NMIP6Config *cfg6, NmCli *nmc, const char *group_pref gboolean print_dhcp4_config (NMDhcp4Config *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field); gboolean print_dhcp6_config (NMDhcp6Config *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field); -NMIP4Address *nmc_parse_and_build_ip4_address (const char *ip_str, const char *gw_str, GError **error); -NMIP6Address *nmc_parse_and_build_ip6_address (const char *ip_str, const char *gw_str, GError **error); - -NMIP4Route *nmc_parse_and_build_ip4_route (const char *first, const char *second, const char *third, GError **error); -NMIP6Route *nmc_parse_and_build_ip6_route (const char *first, const char *second, const char *third, GError **error); +NMIPAddress *nmc_parse_and_build_address (int family, const char *ip_str, const char *gw_str, GError **error); +NMIPRoute *nmc_parse_and_build_route (int family, const char *first, const char *second, const char *third, GError **error); const char * nmc_device_state_to_string (NMDeviceState state); const char * nmc_device_reason_to_string (NMDeviceStateReason reason); diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 554e2107e6..d92b913cf8 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -2887,7 +2887,7 @@ check_and_convert_vlan_prio_maps (const char *prio_map, } static gboolean -add_ip4_address_to_connection (NMIP4Address *ip4addr, NMConnection *connection) +add_ip4_address_to_connection (NMIPAddress *ip4addr, NMConnection *connection) { NMSettingIP4Config *s_ip4; gboolean ret; @@ -2904,13 +2904,13 @@ add_ip4_address_to_connection (NMIP4Address *ip4addr, NMConnection *connection) NULL); } ret = nm_setting_ip4_config_add_address (s_ip4, ip4addr); - nm_ip4_address_unref (ip4addr); + nm_ip_address_unref (ip4addr); return ret; } static gboolean -add_ip6_address_to_connection (NMIP6Address *ip6addr, NMConnection *connection) +add_ip6_address_to_connection (NMIPAddress *ip6addr, NMConnection *connection) { NMSettingIP6Config *s_ip6; gboolean ret; @@ -2927,7 +2927,7 @@ add_ip6_address_to_connection (NMIP6Address *ip6addr, NMConnection *connection) NULL); } ret = nm_setting_ip6_config_add_address (s_ip6, ip6addr); - nm_ip6_address_unref (ip6addr); + nm_ip_address_unref (ip6addr); return ret; } @@ -3841,9 +3841,9 @@ ask_for_ip_addresses (NMConnection *connection, int family) char *str, *ip, *gw, *rest; const char *prompt; gboolean added; - gpointer ipaddr; + NMIPAddress *ipaddr; - if (family == 4) + if (family == AF_INET) prompt =_("IPv4 address (IP[/plen] [gateway]) [none]: "); else prompt =_("IPv6 address (IP[/plen] [gateway]) [none]: "); @@ -3853,16 +3853,13 @@ ask_for_ip_addresses (NMConnection *connection, int family) str = nmc_readline ("%s", prompt); split_address (str, &ip, &gw, &rest); if (ip) { - if (family == 4) - ipaddr = nmc_parse_and_build_ip4_address (ip, gw, &error); - else - ipaddr = nmc_parse_and_build_ip6_address (ip, gw, &error); + ipaddr = nmc_parse_and_build_address (family, ip, gw, &error); if (ipaddr) { - if (family == 4) - added = add_ip4_address_to_connection ((NMIP4Address *) ipaddr, connection); + if (family == AF_INET) + added = add_ip4_address_to_connection (ipaddr, connection); else - added = add_ip6_address_to_connection ((NMIP6Address *) ipaddr, connection); - gw = gw ? gw : (family == 4) ? "0.0.0.0" : "::"; + added = add_ip6_address_to_connection (ipaddr, connection); + gw = gw ? gw : (family == AF_INET) ? "0.0.0.0" : "::"; if (added) g_print (_(" Address successfully added: %s %s\n"), ip, gw); else @@ -3896,8 +3893,8 @@ do_questionnaire_ip (NMConnection *connection) g_print (_("Press <Enter> to finish adding addresses.\n")); - ask_for_ip_addresses (connection, 4); - ask_for_ip_addresses (connection, 6); + ask_for_ip_addresses (connection, AF_INET); + ask_for_ip_addresses (connection, AF_INET6); g_free (answer); return; @@ -5151,8 +5148,7 @@ cleanup_olpc: && strcmp (con_type, "team-slave") != 0 && strcmp (con_type, "bridge-slave") != 0) { - NMIP4Address *ip4addr = NULL; - NMIP6Address *ip6addr = NULL; + NMIPAddress *ip4addr = NULL, *ip6addr = NULL; const char *ip4 = NULL, *gw4 = NULL, *ip6 = NULL, *gw6 = NULL; nmc_arg_t exp_args[] = { {"ip4", TRUE, &ip4, FALSE}, {"gw4", TRUE, &gw4, FALSE}, {"ip6", TRUE, &ip6, FALSE}, {"gw6", TRUE, &gw6, FALSE}, @@ -5172,7 +5168,7 @@ cleanup_olpc: /* coverity[dead_error_begin] */ if (ip4) { - ip4addr = nmc_parse_and_build_ip4_address (ip4, gw4, error); + ip4addr = nmc_parse_and_build_address (AF_INET, ip4, gw4, error); if (!ip4addr) { g_prefix_error (error, _("Error: ")); return FALSE; @@ -5182,7 +5178,7 @@ cleanup_olpc: /* coverity[dead_error_begin] */ if (ip6) { - ip6addr = nmc_parse_and_build_ip6_address (ip6, gw6, error); + ip6addr = nmc_parse_and_build_address (AF_INET6, ip6, gw6, error); if (!ip6addr) { g_prefix_error (error, _("Error: ")); return FALSE; diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 136c818641..19dc06d702 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -1209,8 +1209,7 @@ nmc_property_ipv4_get_addresses (NMSetting *setting) NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); GString *printable; guint32 num_addresses, i; - NMIP4Address *addr; - char buf[INET_ADDRSTRLEN]; + NMIPAddress *addr; printable = g_string_new (NULL); @@ -1223,14 +1222,13 @@ nmc_property_ipv4_get_addresses (NMSetting *setting) g_string_append (printable, "{ "); - nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), buf); - g_string_append_printf (printable, "ip = %s", buf); + g_string_append_printf (printable, "ip = %s/%u", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); - g_string_append_printf (printable, "/%u", nm_ip4_address_get_prefix (addr)); - - if (nm_ip4_address_get_gateway (addr)) { - nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), buf); - g_string_append_printf (printable, ", gw = %s", buf); + if (nm_ip_address_get_gateway (addr)) { + g_string_append_printf (printable, ", gw = %s", + nm_ip_address_get_gateway (addr)); } g_string_append (printable, " }"); @@ -1245,8 +1243,7 @@ nmc_property_ipv4_get_routes (NMSetting *setting) NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); GString *printable; guint32 num_routes, i; - NMIP4Route *route; - char buf[INET_ADDRSTRLEN]; + NMIPRoute *route; printable = g_string_new (NULL); @@ -1259,18 +1256,17 @@ nmc_property_ipv4_get_routes (NMSetting *setting) g_string_append (printable, "{ "); - nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), buf); - g_string_append_printf (printable, "ip = %s", buf); - - g_string_append_printf (printable, "/%u", nm_ip4_route_get_prefix (route)); + g_string_append_printf (printable, "ip = %s/%u", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route)); - if (nm_ip4_route_get_next_hop (route)) { - nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), buf); - g_string_append_printf (printable, ", nh = %s", buf); + if (nm_ip_route_get_next_hop (route)) { + g_string_append_printf (printable, ", nh = %s", + nm_ip_route_get_next_hop (route)); } - if (nm_ip4_route_get_metric (route)) - g_string_append_printf (printable, ", mt = %u", nm_ip4_route_get_metric (route)); + if (nm_ip_route_get_metric (route)) + g_string_append_printf (printable, ", mt = %u", nm_ip_route_get_metric (route)); g_string_append (printable, " }"); } @@ -1297,8 +1293,7 @@ nmc_property_ipv6_get_addresses (NMSetting *setting) NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); GString *printable; guint32 num_addresses, i; - NMIP6Address *addr; - char buf[INET6_ADDRSTRLEN]; + NMIPAddress *addr; printable = g_string_new (NULL); @@ -1311,14 +1306,13 @@ nmc_property_ipv6_get_addresses (NMSetting *setting) g_string_append (printable, "{ "); - nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), buf); - g_string_append_printf (printable, "ip = %s", buf); - - g_string_append_printf (printable, "/%u", nm_ip6_address_get_prefix (addr)); + g_string_append_printf (printable, "ip = %s/%u", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); - if (nm_ip6_address_get_gateway (addr)) { - nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), buf); - g_string_append_printf (printable, ", gw = %s", buf); + if (nm_ip_address_get_gateway (addr)) { + g_string_append_printf (printable, ", gw = %s", + nm_ip_address_get_gateway (addr)); } g_string_append (printable, " }"); @@ -1333,8 +1327,7 @@ nmc_property_ipv6_get_routes (NMSetting *setting) NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); GString *printable; guint32 num_routes, i; - NMIP6Route *route; - char buf[INET6_ADDRSTRLEN]; + NMIPRoute *route; printable = g_string_new (NULL); @@ -1347,18 +1340,17 @@ nmc_property_ipv6_get_routes (NMSetting *setting) g_string_append (printable, "{ "); - nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), buf); - g_string_append_printf (printable, "ip = %s", buf); + g_string_append_printf (printable, "ip = %s/%u", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route)); - g_string_append_printf (printable, "/%u", nm_ip6_route_get_prefix (route)); - - if (nm_ip6_route_get_next_hop (route)) { - nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), buf); - g_string_append_printf (printable, ", nh = %s", buf); + if (nm_ip_route_get_next_hop (route)) { + g_string_append_printf (printable, ", nh = %s", + nm_ip_route_get_next_hop (route)); } - if (nm_ip6_route_get_metric (route)) - g_string_append_printf (printable, ", mt = %u", nm_ip6_route_get_metric (route)); + if (nm_ip_route_get_metric (route)) + g_string_append_printf (printable, ", mt = %u", nm_ip_route_get_metric (route)); g_string_append (printable, " }"); } @@ -3074,6 +3066,51 @@ nmc_property_ib_set_p_key (NMSetting *setting, const char *prop, const char *val return TRUE; } +/* --- IP4 / IP6 shared functions --- */ +static NMIPAddress * +_parse_ip_address (int family, const char *address, GError **error) +{ + char *value = g_strdup (address); + char **addrv; + NMIPAddress *ipaddr; + + addrv = nmc_strsplit_set (g_strstrip (value), " \t", 0); + if (addrv[0] == NULL || g_strv_length (addrv) > 2) { + g_set_error (error, 1, 0, _("'%s' is not valid (use ip[/prefix] [gateway])"), + address); + g_free (value); + g_strfreev (addrv); + return NULL; + } + ipaddr = nmc_parse_and_build_address (family, addrv[0], addrv[1], error); + g_free (value); + g_strfreev (addrv); + return ipaddr; +} + +static NMIPRoute * +_parse_ip_route (int family, const char *route, GError **error) +{ + char *value = g_strdup (route); + char **routev; + guint len; + NMIPRoute *iproute = NULL; + + routev = nmc_strsplit_set (g_strstrip (value), " \t", 0); + len = g_strv_length (routev); + if (len < 1 || len > 3) { + g_set_error (error, 1, 0, _("'%s' is not valid (the format is: ip[/prefix] [next-hop] [metric])"), + route); + goto finish; + } + iproute = nmc_parse_and_build_route (family, routev[0], routev[1], len >= 2 ? routev[2] : NULL, error); + +finish: + g_free (value); + g_strfreev (routev); + return iproute; +} + /* --- NM_SETTING_IP4_CONFIG_SETTING_NAME property setter functions --- */ /* 'method' */ static const char *ipv4_valid_methods[] = { @@ -3194,32 +3231,17 @@ DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_dns_search, _validate_and_remove_ipv4_dns_search) /* 'addresses' */ -static NMIP4Address * +static NMIPAddress * _parse_ipv4_address (const char *address, GError **error) { - char *value = g_strdup (address); - char **addrv; - NMIP4Address *ip4addr; - - addrv = nmc_strsplit_set (g_strstrip (value), " \t", 0); - if (addrv[0] == NULL || g_strv_length (addrv) > 2) { - g_set_error (error, 1, 0, _("'%s' is not valid (use ip[/prefix] [gateway])"), - address); - g_free (value); - g_strfreev (addrv); - return NULL; - } - ip4addr = nmc_parse_and_build_ip4_address (addrv[0], addrv[1], error); - g_free (value); - g_strfreev (addrv); - return ip4addr; + return _parse_ip_address (AF_INET, address, error); } static gboolean nmc_property_ipv4_set_addresses (NMSetting *setting, const char *prop, const char *val, GError **error) { char **strv = NULL, **iter; - NMIP4Address *ip4addr; + NMIPAddress *ip4addr; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -3231,7 +3253,7 @@ nmc_property_ipv4_set_addresses (NMSetting *setting, const char *prop, const cha return FALSE; } nm_setting_ip4_config_add_address (NM_SETTING_IP4_CONFIG (setting), ip4addr); - nm_ip4_address_unref (ip4addr); + nm_ip_address_unref (ip4addr); } g_strfreev (strv); return TRUE; @@ -3242,7 +3264,7 @@ _validate_and_remove_ipv4_address (NMSettingIP4Config *setting, const char *address, GError **error) { - NMIP4Address *ip4addr; + NMIPAddress *ip4addr; gboolean ret; ip4addr = _parse_ipv4_address (address, error); @@ -3253,7 +3275,7 @@ _validate_and_remove_ipv4_address (NMSettingIP4Config *setting, if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain IP address '%s'"), address); - nm_ip4_address_unref (ip4addr); + nm_ip_address_unref (ip4addr); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_addresses, @@ -3307,34 +3329,17 @@ nmc_property_out2in_addresses (const char *out_format) } /* 'routes' */ -static NMIP4Route * +static NMIPRoute * _parse_ipv4_route (const char *route, GError **error) { - char *value = g_strdup (route); - char **routev; - guint len; - NMIP4Route *ip4route = NULL; - - routev = nmc_strsplit_set (g_strstrip (value), " \t", 0); - len = g_strv_length (routev); - if (len < 1 || len > 3) { - g_set_error (error, 1, 0, _("'%s' is not valid (the format is: ip[/prefix] [next-hop] [metric])"), - route); - goto finish; - } - ip4route = nmc_parse_and_build_ip4_route (routev[0], routev[1], len >= 2 ? routev[2] : NULL, error); - -finish: - g_free (value); - g_strfreev (routev); - return ip4route; + return _parse_ip_route (AF_INET, route, error); } static gboolean nmc_property_ipv4_set_routes (NMSetting *setting, const char *prop, const char *val, GError **error) { char **strv = NULL, **iter; - NMIP4Route *ip4route; + NMIPRoute *ip4route; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -3346,7 +3351,7 @@ nmc_property_ipv4_set_routes (NMSetting *setting, const char *prop, const char * return FALSE; } nm_setting_ip4_config_add_route (NM_SETTING_IP4_CONFIG (setting), ip4route); - nm_ip4_route_unref (ip4route); + nm_ip_route_unref (ip4route); } g_strfreev (strv); return TRUE; @@ -3357,7 +3362,7 @@ _validate_and_remove_ipv4_route (NMSettingIP4Config *setting, const char *route, GError **error) { - NMIP4Route *ip4route; + NMIPRoute *ip4route; gboolean ret; ip4route = _parse_ipv4_route (route, error); @@ -3367,7 +3372,7 @@ _validate_and_remove_ipv4_route (NMSettingIP4Config *setting, ret = nm_setting_ip4_config_remove_route_by_value (setting, ip4route); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain route '%s'"), route); - nm_ip4_route_unref (ip4route); + nm_ip_route_unref (ip4route); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_routes, @@ -3546,32 +3551,17 @@ DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_dns_search, _validate_and_remove_ipv6_dns_search) /* 'addresses' */ -static NMIP6Address * +static NMIPAddress * _parse_ipv6_address (const char *address, GError **error) { - char *value = g_strstrip (g_strdup (address)); - char **addrv; - NMIP6Address *ip6addr; - - addrv = nmc_strsplit_set (g_strstrip (value), " \t", 0); - if (addrv[0] == NULL || g_strv_length (addrv) > 2) { - g_set_error (error, 1, 0, _("'%s' is not valid (use ip[/prefix] [gateway])"), - address); - g_free (value); - g_strfreev (addrv); - return NULL; - } - ip6addr = nmc_parse_and_build_ip6_address (addrv[0], addrv[1], error); - g_free (value); - g_strfreev (addrv); - return ip6addr; + return _parse_ip_address (AF_INET6, address, error); } static gboolean nmc_property_ipv6_set_addresses (NMSetting *setting, const char *prop, const char *val, GError **error) { char **strv = NULL, **iter; - NMIP6Address *ip6addr; + NMIPAddress *ip6addr; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -3583,7 +3573,7 @@ nmc_property_ipv6_set_addresses (NMSetting *setting, const char *prop, const cha return FALSE; } nm_setting_ip6_config_add_address (NM_SETTING_IP6_CONFIG (setting), ip6addr); - nm_ip6_address_unref (ip6addr); + nm_ip_address_unref (ip6addr); } g_strfreev (strv); return TRUE; @@ -3594,7 +3584,7 @@ _validate_and_remove_ipv6_address (NMSettingIP6Config *setting, const char *address, GError **error) { - NMIP6Address *ip6addr; + NMIPAddress *ip6addr; gboolean ret; ip6addr = _parse_ipv6_address (address, error); @@ -3604,7 +3594,7 @@ _validate_and_remove_ipv6_address (NMSettingIP6Config *setting, ret = nm_setting_ip6_config_remove_address_by_value (setting, ip6addr); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain IP address '%s'"), address); - nm_ip6_address_unref (ip6addr); + nm_ip_address_unref (ip6addr); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_addresses, @@ -3623,34 +3613,17 @@ nmc_property_ipv6_describe_addresses (NMSetting *setting, const char *prop) } /* 'routes' */ -static NMIP6Route * +static NMIPRoute * _parse_ipv6_route (const char *route, GError **error) { - char *value = g_strdup (route); - char **routev; - guint len; - NMIP6Route *ip6route = NULL; - - routev = nmc_strsplit_set (g_strstrip (value), " \t", 0); - len = g_strv_length (routev); - if (len < 1 || len > 3) { - g_set_error (error, 1, 0, _("'%s' is not valid (the format is: ip[/prefix] [next-hop] [metric])"), - route); - goto finish; - } - ip6route = nmc_parse_and_build_ip6_route (routev[0], routev[1], len >= 2 ? routev[2] : NULL, error); - -finish: - g_free (value); - g_strfreev (routev); - return ip6route; + return _parse_ip_route (AF_INET6, route, error); } static gboolean nmc_property_ipv6_set_routes (NMSetting *setting, const char *prop, const char *val, GError **error) { char **strv = NULL, **iter; - NMIP6Route *ip6route; + NMIPRoute *ip6route; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -3662,7 +3635,7 @@ nmc_property_ipv6_set_routes (NMSetting *setting, const char *prop, const char * return FALSE; } nm_setting_ip6_config_add_route (NM_SETTING_IP6_CONFIG (setting), ip6route); - nm_ip6_route_unref (ip6route); + nm_ip_route_unref (ip6route); } g_strfreev (strv); return TRUE; @@ -3673,7 +3646,7 @@ _validate_and_remove_ipv6_route (NMSettingIP6Config *setting, const char *route, GError **error) { - NMIP6Route *ip6route; + NMIPRoute *ip6route; gboolean ret; ip6route = _parse_ipv6_route (route, error); @@ -3683,7 +3656,7 @@ _validate_and_remove_ipv6_route (NMSettingIP6Config *setting, ret = nm_setting_ip6_config_remove_route_by_value (setting, ip6route); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain route '%s'"), route); - nm_ip6_route_unref (ip6route); + nm_ip_route_unref (ip6route); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_routes, diff --git a/clients/tui/nm-editor-bindings.c b/clients/tui/nm-editor-bindings.c index e3b210dec6..4c42caad35 100644 --- a/clients/tui/nm-editor-bindings.c +++ b/clients/tui/nm-editor-bindings.c @@ -72,10 +72,10 @@ nm_editor_bindings_init (void) } static gboolean -ip_string_parse (const char *text, - int family, - gpointer addr, - guint32 *prefix) +parse_addr_prefix (const char *text, + int family, + char **addr, + guint32 *prefix) { const char *slash; char *addrstr, *end; @@ -83,17 +83,11 @@ ip_string_parse (const char *text, slash = strchr (text, '/'); - if (slash) { - if (!prefix) - return FALSE; + if (slash) addrstr = g_strndup (text, slash - text); - } else + else addrstr = g_strdup (text); - valid = (inet_pton (family, addrstr, addr) == 1); - g_free (addrstr); - - if (!valid) - return FALSE; + valid = nm_utils_ipaddr_valid (family, addrstr); if (slash) { *prefix = strtoul (slash + 1, &end, 10); @@ -109,19 +103,24 @@ ip_string_parse (const char *text, *prefix = 128; } + if (addr && valid) + *addr = addrstr; + else + g_free (addrstr); return valid; } static gboolean -ip4_addresses_with_prefix_to_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_addresses_with_prefix_to_strv (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { GPtrArray *addrs; - NMIP4Address *addr; - guint32 addrbytes, prefix; - char buf[INET_ADDRSTRLEN], **strings; + NMIPAddress *addr; + const char *addrstr; + guint32 prefix; + char **strings; int i; addrs = g_value_get_boxed (source_value); @@ -129,14 +128,12 @@ ip4_addresses_with_prefix_to_strv (GBinding *binding, for (i = 0; i < addrs->len; i++) { addr = addrs->pdata[i]; - addrbytes = nm_ip4_address_get_address (addr); - prefix = nm_ip4_address_get_prefix (addr); + addrstr = nm_ip_address_get_address (addr); + prefix = nm_ip_address_get_prefix (addr); - if (addrbytes) { - strings[i] = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET, &addrbytes, buf, sizeof (buf)), - (int) prefix); - } else + if (addrstr) + strings[i] = g_strdup_printf ("%s/%d", addrstr, (int) prefix); + else strings[i] = g_strdup (""); } @@ -145,15 +142,17 @@ ip4_addresses_with_prefix_to_strv (GBinding *binding, } static gboolean -ip4_addresses_with_prefix_from_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_addresses_with_prefix_from_strv (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { + int family = GPOINTER_TO_INT (user_data); char **strings; GPtrArray *addrs; - NMIP4Address *addr; - guint32 addrbytes, prefix; + NMIPAddress *addr; + char *addrstr; + guint32 prefix; int i; strings = g_value_get_boxed (source_value); @@ -164,19 +163,22 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, for (i = 0; strings[i]; i++) { if (i >= addrs->len) { - addr = nm_ip4_address_new (); - nm_ip4_address_set_prefix (addr, 32); + if (family == AF_INET) + addr = nm_ip_address_new (AF_INET, "0.0.0.0", 32, NULL, NULL); + else + addr = nm_ip_address_new (AF_INET6, "::", 128, NULL, NULL); g_ptr_array_add (addrs, addr); } else addr = addrs->pdata[i]; - if (!ip_string_parse (strings[i], AF_INET, &addrbytes, &prefix)) { + if (!parse_addr_prefix (strings[i], family, &addrstr, &prefix)) { g_ptr_array_unref (addrs); return FALSE; } - nm_ip4_address_set_address (addr, addrbytes); - nm_ip4_address_set_prefix (addr, prefix); + nm_ip_address_set_address (addr, addrstr); + nm_ip_address_set_prefix (addr, prefix); + g_free (addrstr); } g_ptr_array_set_size (addrs, i); @@ -185,7 +187,8 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, } /** - * nm_editor_bind_ip4_addresses_with_prefix_to_strv: + * nm_editor_bind_ip_addresses_with_prefix_to_strv: + * @family: the IP address family * @source: the source object (eg, an #NMSettingIP4Config) * @source_property: the property on @source to bind (eg, * %NM_SETTING_IP4_CONFIG_ADDRESSES) @@ -194,44 +197,45 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, * (eg, "strings") * @flags: %GBindingFlags * - * Binds the #GPtrArray-of-#NMIP4Address property @source_property on @source to + * Binds the #GPtrArray-of-#NMIPAddress property @source_property on @source to * the %G_TYPE_STRV property @target_property on @target. * - * Each #NMIP4Address in @source_property will be converted to a string of the - * form "ip.ad.dr.ess/prefix" in @target_property (and vice versa if - * %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in + * Each #NMIPAddress in @source_property will be converted to a string of the + * form "ip.ad.dr.ess/prefix" or "ip:ad:dr:ess/prefix" in @target_property (and + * vice versa if %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in * @source_property are ignored when converting to strings, and unmodified when * converting from strings. */ void -nm_editor_bind_ip4_addresses_with_prefix_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) +nm_editor_bind_ip_addresses_with_prefix_to_strv (int family, + gpointer source, + const gchar *source_property, + gpointer target, + const gchar *target_property, + GBindingFlags flags) { g_object_bind_property_full (source, source_property, target, target_property, flags, - ip4_addresses_with_prefix_to_strv, - ip4_addresses_with_prefix_from_strv, - NULL, NULL); + ip_addresses_with_prefix_to_strv, + ip_addresses_with_prefix_from_strv, + GINT_TO_POINTER (family), NULL); } static gboolean -ip4_addresses_check_and_copy (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_addresses_check_and_copy (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { + int family = GPOINTER_TO_INT (user_data); char **strings; - guint32 addr; int i; strings = g_value_get_boxed (source_value); for (i = 0; strings[i]; i++) { - if (!ip_string_parse (strings[i], AF_INET, &addr, NULL)) + if (!nm_utils_ipaddr_valid (family, strings[i])) return FALSE; } @@ -240,7 +244,8 @@ ip4_addresses_check_and_copy (GBinding *binding, } /** - * nm_editor_bind_ip4_addresses_to_strv: + * nm_editor_bind_ip_addresses_to_strv: + * @family: the IP address family * @source: the source object (eg, an #NMSettingIP4Config) * @source_property: the property on @source to bind (eg, * %NM_SETTING_IP4_CONFIG_DNS) @@ -251,67 +256,67 @@ ip4_addresses_check_and_copy (GBinding *binding, * * Binds the %G_TYPE_STRV property @source_property on @source to the * %G_TYPE_STRV property @target_property on @target, verifying that - * each string is a valid IPv4 address when copying. + * each string is a valid address of type @family when copying. */ void -nm_editor_bind_ip4_addresses_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) +nm_editor_bind_ip_addresses_to_strv (int family, + gpointer source, + const gchar *source_property, + gpointer target, + const gchar *target_property, + GBindingFlags flags) { g_object_bind_property_full (source, source_property, target, target_property, flags, - ip4_addresses_check_and_copy, - ip4_addresses_check_and_copy, - NULL, NULL); + ip_addresses_check_and_copy, + ip_addresses_check_and_copy, + GINT_TO_POINTER (family), NULL); } static gboolean -ip4_gateway_to_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_gateway_to_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { GPtrArray *addrs; - NMIP4Address *addr; - guint32 gateway = 0; - const char *str; - char buf[INET_ADDRSTRLEN]; + NMIPAddress *addr; + const char *gateway = NULL; int i; addrs = g_value_get_boxed (source_value); for (i = 0; i < addrs->len; i++) { addr = addrs->pdata[i]; - gateway = nm_ip4_address_get_gateway (addr); + gateway = nm_ip_address_get_gateway (addr); if (gateway) break; } - if (gateway) - str = inet_ntop (AF_INET, &gateway, buf, sizeof (buf)); - else - str = ""; - g_value_set_string (target_value, str); + if (!gateway) + gateway = ""; + g_value_set_string (target_value, gateway); return TRUE; } static gboolean -ip4_gateway_from_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_gateway_from_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { + int family = GPOINTER_TO_INT (user_data); const char *text; GPtrArray *addrs; - NMIP4Address *addr; - guint32 addrbytes; + NMIPAddress *addr; int i; text = g_value_get_string (source_value); - if (!ip_string_parse (text, AF_INET, &addrbytes, NULL)) - return FALSE; + if (*text) { + if (!nm_utils_ipaddr_valid (family, text)) + return FALSE; + } else + text = NULL; /* Fetch the original property value, so as to preserve the IP address elements */ g_object_get (g_binding_get_source (binding), @@ -322,15 +327,15 @@ ip4_gateway_from_string (GBinding *binding, return FALSE; } addr = addrs->pdata[0]; - if (addrbytes == nm_ip4_address_get_gateway (addr)) { + if (!g_strcmp0 (text, nm_ip_address_get_gateway (addr))) { g_ptr_array_unref (addrs); return FALSE; } - nm_ip4_address_set_gateway (addr, addrbytes); + nm_ip_address_set_gateway (addr, text); for (i = 1; i < addrs->len; i++) { addr = addrs->pdata[i]; - nm_ip4_address_set_gateway (addr, 0); + nm_ip_address_set_gateway (addr, NULL); } g_value_take_boxed (target_value, addrs); @@ -338,7 +343,8 @@ ip4_gateway_from_string (GBinding *binding, } /** - * nm_editor_bind_ip4_gateway_to_string: + * nm_editor_bind_ip_gateway_to_string: + * @family: the IP address family * @source: the source object (eg, an #NMSettingIP4Config) * @source_property: the property on @source to bind (eg, * %NM_SETTING_IP4_CONFIG_ADDRESSES) @@ -347,7 +353,7 @@ ip4_gateway_from_string (GBinding *binding, * (eg, "text") * @flags: %GBindingFlags * - * Binds the #GPtrArray-of-#NMIP4Route property @source_property on @source to + * Binds the #GPtrArray-of-#NMIPRoute property @source_property on @source to * the %G_TYPE_STRING property @target_property on @target. * * Specifically, this binds the "gateway" field of the first address in @@ -355,495 +361,39 @@ ip4_gateway_from_string (GBinding *binding, * its "address" and "prefix" fields are unmodified. */ void -nm_editor_bind_ip4_gateway_to_string (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip4_gateway_to_string, - ip4_gateway_from_string, - NULL, NULL); -} - -static gboolean -ip4_route_transform_to_dest_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - char buf[INET_ADDRSTRLEN], *string; - guint32 addrbytes; - - route = g_value_get_boxed (source_value); - if (route) - addrbytes = nm_ip4_route_get_dest (route); - else - addrbytes = 0; - - if (addrbytes) { - string = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET, &addrbytes, buf, sizeof (buf)), - (int) nm_ip4_route_get_prefix (route)); - g_value_take_string (target_value, string); - } else - g_value_set_string (target_value, ""); - return TRUE; -} - -static gboolean -ip4_route_transform_to_next_hop_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - char buf[INET_ADDRSTRLEN]; - guint32 addrbytes; - - route = g_value_get_boxed (source_value); - if (route) - addrbytes = nm_ip4_route_get_next_hop (route); - else - addrbytes = 0; - - if (addrbytes) - inet_ntop (AF_INET, &addrbytes, buf, sizeof (buf)); - else - buf[0] = '\0'; - g_value_set_string (target_value, buf); - return TRUE; -} - -static gboolean -ip4_route_transform_to_metric_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - char *string; - - route = g_value_get_boxed (source_value); - if (route && nm_ip4_route_get_dest (route)) { - string = g_strdup_printf ("%lu", (gulong) nm_ip4_route_get_metric (route)); - g_value_take_string (target_value, string); - } else - g_value_set_string (target_value, ""); - return TRUE; -} - -static gboolean -ip4_route_transform_from_dest_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - const char *text; - guint32 addrbytes, prefix; - - text = g_value_get_string (source_value); - if (!ip_string_parse (text, AF_INET, &addrbytes, &prefix)) - return FALSE; - - /* Fetch the original property value */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &route, - NULL); - - nm_ip4_route_set_dest (route, addrbytes); - nm_ip4_route_set_prefix (route, prefix); - - g_value_take_boxed (target_value, route); - return TRUE; -} - -static gboolean -ip4_route_transform_from_next_hop_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - const char *text; - guint32 addrbytes; - - text = g_value_get_string (source_value); - if (*text) { - if (!ip_string_parse (text, AF_INET, &addrbytes, NULL)) - return FALSE; - } else - addrbytes = 0; - - /* Fetch the original property value */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &route, - NULL); - - nm_ip4_route_set_next_hop (route, addrbytes); - - g_value_take_boxed (target_value, route); - return TRUE; -} - -static gboolean -ip4_route_transform_from_metric_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - const char *text; - guint32 metric; - - text = g_value_get_string (source_value); - metric = strtoul (text, NULL, 10); - - /* Fetch the original property value */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &route, - NULL); - - nm_ip4_route_set_metric (route, metric); - - g_value_take_boxed (target_value, route); - return TRUE; -} - -/** - * nm_editor_bind_ip4_route_to_strings: - * @source: the source object - * @source_property: the source property - * @dest_target: the target object for the route's destionation - * @dest_target_property: the property on @dest_target - * @next_hop_target: the target object for the route's next hop - * @next_hop_target_property: the property on @next_hop_target - * @metric_target: the target object for the route's metric - * @metric_target_property: the property on @metric_target - * @flags: %GBindingFlags - * - * Binds the #NMIP4Route-valued property @source_property on @source to the - * three indicated string-valued target properties (and vice versa if - * %G_BINDING_BIDIRECTIONAL is specified). - * - * @dest_target_property should be an "address/prefix" string, as with - * nm_editor_bind_ip4_addresses_with_prefix_to_strv(). @next_hop_target_property - * is a plain IP address, and @metric_target_property is a number. - */ -void -nm_editor_bind_ip4_route_to_strings (gpointer source, +nm_editor_bind_ip_gateway_to_string (int family, + gpointer source, const gchar *source_property, - gpointer dest_target, - const gchar *dest_target_property, - gpointer next_hop_target, - const gchar *next_hop_target_property, - gpointer metric_target, - const gchar *metric_target_property, + gpointer target, + const gchar *target_property, GBindingFlags flags) { g_object_bind_property_full (source, source_property, - dest_target, dest_target_property, - flags, - ip4_route_transform_to_dest_string, - ip4_route_transform_from_dest_string, - NULL, NULL); - g_object_bind_property_full (source, source_property, - next_hop_target, next_hop_target_property, - flags, - ip4_route_transform_to_next_hop_string, - ip4_route_transform_from_next_hop_string, - NULL, NULL); - g_object_bind_property_full (source, source_property, - metric_target, metric_target_property, + target, target_property, flags, - ip4_route_transform_to_metric_string, - ip4_route_transform_from_metric_string, - NULL, NULL); + ip_gateway_to_string, + ip_gateway_from_string, + GINT_TO_POINTER (family), NULL); } -#define IP6_ADDRESS_SET(addr) ( addr \ - && memcmp (addr, &in6addr_any, sizeof (struct in6_addr)) != 0) - static gboolean -ip6_addresses_with_prefix_to_strv (GBinding *binding, +ip_route_transform_to_dest_string (GBinding *binding, const GValue *source_value, GValue *target_value, gpointer user_data) { - GPtrArray *addrs; - NMIP6Address *addr; - const struct in6_addr *addrbytes; - guint prefix; - char **strings, buf[INET6_ADDRSTRLEN]; - int i; - - addrs = g_value_get_boxed (source_value); - strings = g_new0 (char *, addrs->len + 1); - - for (i = 0; i < addrs->len; i++) { - addr = addrs->pdata[i]; - addrbytes = nm_ip6_address_get_address (addr); - prefix = nm_ip6_address_get_prefix (addr); - - if (IP6_ADDRESS_SET (addrbytes)) { - strings[i] = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)), - prefix); - } else - strings[i] = g_strdup (""); - } - - g_value_take_boxed (target_value, strings); - return TRUE; -} - -static gboolean -ip6_addresses_with_prefix_from_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - char **strings; - GPtrArray *addrs; - NMIP6Address *addr; - struct in6_addr addrbytes; - guint32 prefix; - int i; - - strings = g_value_get_boxed (source_value); - - /* Fetch the original property value, so as to preserve the gateway elements */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &addrs, - NULL); - - for (i = 0; strings[i]; i++) { - if (i >= addrs->len) { - addr = nm_ip6_address_new (); - nm_ip6_address_set_prefix (addr, 128); - g_ptr_array_add (addrs, addr); - } else - addr = addrs->pdata[i]; - - if (!ip_string_parse (strings[i], AF_INET6, &addrbytes, &prefix)) { - g_ptr_array_unref (addrs); - return FALSE; - } - - nm_ip6_address_set_address (addr, &addrbytes); - nm_ip6_address_set_prefix (addr, prefix); - } - - g_ptr_array_set_size (addrs, i); - g_value_set_boxed (target_value, addrs); - return TRUE; -} - -/** - * nm_editor_bind_ip6_addresses_with_prefix_to_strv: - * @source: the source object (eg, an #NMSettingIP6Config) - * @source_property: the property on @source to bind (eg, - * %NM_SETTING_IP6_CONFIG_ADDRESSES) - * @target: the target object (eg, an #NmtAddressList) - * @target_property: the property on @target to bind - * (eg, "strings") - * @flags: %GBindingFlags - * - * Binds the #GPtrArray-of-#NMIP6Address property @source_property on @source to - * the %G_TYPE_STRV property @target_property on @target. - * - * Each #NMIP6Address in triplet in @source_property will be converted to a - * string of the form "ip::ad:dr:ess/prefix" in @target_property (and vice versa - * if %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in - * @source_property are ignored when converting to strings, and unmodified when - * converting from strings. - */ -void -nm_editor_bind_ip6_addresses_with_prefix_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip6_addresses_with_prefix_to_strv, - ip6_addresses_with_prefix_from_strv, - NULL, NULL); -} - -static gboolean -ip6_addresses_check_and_copy (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - char **strings; - struct in6_addr addr; - int i; - - strings = g_value_get_boxed (source_value); - - for (i = 0; strings[i]; i++) { - if (!ip_string_parse (strings[i], AF_INET6, &addr, NULL)) - return FALSE; - } - - g_value_set_boxed (target_value, strings); - return TRUE; -} - -/** - * nm_editor_bind_ip6_addresses_to_strv: - * @source: the source object (eg, an #NMSettingIP6Config) - * @source_property: the property on @source to bind (eg, - * %NM_SETTING_IP6_CONFIG_DNS) - * @target: the target object (eg, an #NmtAddressList) - * @target_property: the property on @target to bind - * (eg, "strings") - * @flags: %GBindingFlags - * - * Binds the %G_TYPE_STRV property @source_property on @source to the - * %G_TYPE_STRV property @target_property on @target, verifying that - * each string is a valid IPv6 address when copying. - */ -void -nm_editor_bind_ip6_addresses_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip6_addresses_check_and_copy, - ip6_addresses_check_and_copy, - NULL, NULL); -} - -static gboolean -ip6_gateway_to_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - GPtrArray *addrs; - NMIP6Address *addr; - const struct in6_addr *gateway; - char buf[INET6_ADDRSTRLEN]; - const char *str; - - addrs = g_value_get_boxed (source_value); - if (addrs->len == 0) - return FALSE; - - addr = addrs->pdata[0]; - gateway = nm_ip6_address_get_gateway (addr); - - if (IP6_ADDRESS_SET (gateway)) - str = inet_ntop (AF_INET6, gateway, buf, sizeof (buf)); - else - str = ""; - g_value_set_string (target_value, str); - return TRUE; -} - -static gboolean -ip6_gateway_from_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - GPtrArray *addrs; - const char *text; - NMIP6Address *addr; - struct in6_addr gateway; - int i; - - text = g_value_get_string (source_value); - if (!ip_string_parse (text, AF_INET6, &gateway, NULL)) - return FALSE; - - /* Fetch the original property value, so as to preserve the IP address elements */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &addrs, - NULL); - if (!addrs->len) { - g_ptr_array_unref (addrs); - return FALSE; - } - - addr = addrs->pdata[0]; - nm_ip6_address_set_gateway (addr, &gateway); - - for (i = 1; i < addrs->len; i++) { - addr = addrs->pdata[i]; - nm_ip6_address_set_gateway (addr, &in6addr_any); - } - - g_value_take_boxed (target_value, addrs); - return TRUE; -} - -/** - * nm_editor_bind_ip6_gateway_to_string: - * @source: the source object (eg, an #NMSettingIP6Config) - * @source_property: the property on @source to bind (eg, - * %NM_SETTING_IP6_CONFIG_ADDRESSES) - * @target: the target object (eg, an #NmtNewtEntry) - * @target_property: the property on @target to bind - * (eg, "text") - * @flags: %GBindingFlags - * - * Binds the #GPtrArray-of-#NMIP6Address property @source_property on @source to - * the %G_TYPE_STRING property @target_property on @target. - * - * Specifically, this binds the "gateway" field of the first address in - * @source_property; all other addresses in @source_property are ignored, and - * its "address" and "prefix" fields are unmodified. - */ -void -nm_editor_bind_ip6_gateway_to_string (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip6_gateway_to_string, - ip6_gateway_from_string, - NULL, NULL); -} - -static gboolean -ip6_route_transform_to_dest_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP6Route *route; - char buf[INET6_ADDRSTRLEN], *string; - const struct in6_addr *addrbytes; + NMIPRoute *route; + const char *addrstr; + char *string; route = g_value_get_boxed (source_value); if (route) - addrbytes = nm_ip6_route_get_dest (route); + addrstr = nm_ip_route_get_dest (route); else - addrbytes = &in6addr_any; + addrstr = NULL; - if (IP6_ADDRESS_SET (addrbytes)) { - string = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)), - (int) nm_ip6_route_get_prefix (route)); + if (addrstr) { + string = g_strdup_printf ("%s/%d", addrstr, (int) nm_ip_route_get_prefix (route)); g_value_take_string (target_value, string); } else g_value_set_string (target_value, ""); @@ -851,41 +401,38 @@ ip6_route_transform_to_dest_string (GBinding *binding, } static gboolean -ip6_route_transform_to_next_hop_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_to_next_hop_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; - char buf[INET6_ADDRSTRLEN]; - const struct in6_addr *addrbytes; + NMIPRoute *route; + const char *addrstr; route = g_value_get_boxed (source_value); - if (route) - addrbytes = nm_ip6_route_get_next_hop (route); - else - addrbytes = &in6addr_any; + if (route) { + addrstr = nm_ip_route_get_next_hop (route); + if (!addrstr) + addrstr = ""; + } else + addrstr = ""; - if (IP6_ADDRESS_SET (addrbytes)) - inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)); - else - buf[0] = '\0'; - g_value_set_string (target_value, buf); + g_value_set_string (target_value, addrstr); return TRUE; } static gboolean -ip6_route_transform_to_metric_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_to_metric_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; + NMIPRoute *route; char *string; route = g_value_get_boxed (source_value); - if (route && IP6_ADDRESS_SET (nm_ip6_route_get_dest (route))) { - string = g_strdup_printf ("%lu", (gulong) nm_ip6_route_get_metric (route)); + if (route && nm_ip_route_get_dest (route)) { + string = g_strdup_printf ("%lu", (gulong) nm_ip_route_get_metric (route)); g_value_take_string (target_value, string); } else g_value_set_string (target_value, ""); @@ -893,18 +440,19 @@ ip6_route_transform_to_metric_string (GBinding *binding, } static gboolean -ip6_route_transform_from_dest_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_from_dest_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; + int family = GPOINTER_TO_INT (user_data); + NMIPRoute *route; const char *text; - struct in6_addr addrbytes; + char *addrstr; guint32 prefix; text = g_value_get_string (source_value); - if (!ip_string_parse (text, AF_INET6, &addrbytes, &prefix)) + if (!parse_addr_prefix (text, family, &addrstr, &prefix)) return FALSE; /* Fetch the original property value */ @@ -912,48 +460,49 @@ ip6_route_transform_from_dest_string (GBinding *binding, g_binding_get_source_property (binding), &route, NULL); - nm_ip6_route_set_dest (route, &addrbytes); - nm_ip6_route_set_prefix (route, prefix); + nm_ip_route_set_dest (route, addrstr); + nm_ip_route_set_prefix (route, prefix); + g_free (addrstr); g_value_take_boxed (target_value, route); return TRUE; } static gboolean -ip6_route_transform_from_next_hop_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_from_next_hop_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; + int family = GPOINTER_TO_INT (user_data); + NMIPRoute *route; const char *text; - struct in6_addr addrbytes; text = g_value_get_string (source_value); if (*text) { - if (!ip_string_parse (text, AF_INET6, &addrbytes, NULL)) + if (!nm_utils_ipaddr_valid (family, text)) return FALSE; } else - addrbytes = in6addr_any; + text = NULL; /* Fetch the original property value */ g_object_get (g_binding_get_source (binding), g_binding_get_source_property (binding), &route, NULL); - nm_ip6_route_set_next_hop (route, &addrbytes); + nm_ip_route_set_next_hop (route, text); g_value_take_boxed (target_value, route); return TRUE; } static gboolean -ip6_route_transform_from_metric_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_from_metric_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; + NMIPRoute *route; const char *text; guint32 metric; @@ -965,14 +514,15 @@ ip6_route_transform_from_metric_string (GBinding *binding, g_binding_get_source_property (binding), &route, NULL); - nm_ip6_route_set_metric (route, metric); + nm_ip_route_set_metric (route, metric); g_value_take_boxed (target_value, route); return TRUE; } /** - * nm_editor_bind_ip6_route_to_strings: + * nm_editor_bind_ip_route_to_strings: + * @family: the IP address family * @source: the source object * @source_property: the source property * @dest_target: the target object for the route's destionation @@ -983,43 +533,44 @@ ip6_route_transform_from_metric_string (GBinding *binding, * @metric_target_property: the property on @metric_target * @flags: %GBindingFlags * - * Binds the #NMIP6Route-valued property @source_property on @source - * to the three indicated string-valued target properties (and vice - * versa if %G_BINDING_BIDIRECTIONAL is specified). + * Binds the #NMIPRoute-valued property @source_property on @source to the + * three indicated string-valued target properties (and vice versa if + * %G_BINDING_BIDIRECTIONAL is specified). * * @dest_target_property should be an "address/prefix" string, as with - * nm_editor_bind_ip6_addresses_with_prefix_to_strv(). @next_hop_target - * is a plain IP address, and @metric_target is a number. + * nm_editor_bind_ip4_addresses_with_prefix_to_strv(). @next_hop_target_property + * is a plain IP address, and @metric_target_property is a number. */ void -nm_editor_bind_ip6_route_to_strings (gpointer source, - const gchar *source_property, - gpointer dest_target, - const gchar *dest_target_property, - gpointer next_hop_target, - const gchar *next_hop_target_property, - gpointer metric_target, - const gchar *metric_target_property, - GBindingFlags flags) +nm_editor_bind_ip_route_to_strings (int family, + gpointer source, + const gchar *source_property, + gpointer dest_target, + const gchar *dest_target_property, + gpointer next_hop_target, + const gchar *next_hop_target_property, + gpointer metric_target, + const gchar *metric_target_property, + GBindingFlags flags) { g_object_bind_property_full (source, source_property, dest_target, dest_target_property, flags, - ip6_route_transform_to_dest_string, - ip6_route_transform_from_dest_string, - NULL, NULL); + ip_route_transform_to_dest_string, + ip_route_transform_from_dest_string, + GINT_TO_POINTER (family), NULL); g_object_bind_property_full (source, source_property, next_hop_target, next_hop_target_property, flags, - ip6_route_transform_to_next_hop_string, - ip6_route_transform_from_next_hop_string, - NULL, NULL); + ip_route_transform_to_next_hop_string, + ip_route_transform_from_next_hop_string, + GINT_TO_POINTER (family), NULL); g_object_bind_property_full (source, source_property, metric_target, metric_target_property, flags, - ip6_route_transform_to_metric_string, - ip6_route_transform_from_metric_string, - NULL, NULL); + ip_route_transform_to_metric_string, + ip_route_transform_from_metric_string, + GINT_TO_POINTER (family), NULL); } /* Wireless security method binding */ diff --git a/clients/tui/nm-editor-bindings.h b/clients/tui/nm-editor-bindings.h index b595ef03c1..5e34c42dec 100644 --- a/clients/tui/nm-editor-bindings.h +++ b/clients/tui/nm-editor-bindings.h @@ -25,49 +25,27 @@ G_BEGIN_DECLS void nm_editor_bindings_init (void); -void nm_editor_bind_ip4_addresses_with_prefix_to_strv (gpointer source, +void nm_editor_bind_ip_addresses_with_prefix_to_strv (int family, + gpointer source, const gchar *source_property, gpointer target, const gchar *target_property, GBindingFlags flags); -void nm_editor_bind_ip4_addresses_to_strv (gpointer source, +void nm_editor_bind_ip_addresses_to_strv (int family, + gpointer source, const gchar *source_property, gpointer target, const gchar *target_property, GBindingFlags flags); -void nm_editor_bind_ip4_gateway_to_string (gpointer source, +void nm_editor_bind_ip_gateway_to_string (int family, + gpointer source, const gchar *source_property, gpointer target, const gchar *target_property, GBindingFlags flags); -void nm_editor_bind_ip4_route_to_strings (gpointer source, - const gchar *source_property, - gpointer dest_target, - const gchar *dest_target_property, - gpointer next_hop_target, - const gchar *next_hop_target_property, - gpointer metric_target, - const gchar *metric_target_property, - GBindingFlags flags); - -void nm_editor_bind_ip6_addresses_with_prefix_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags); -void nm_editor_bind_ip6_addresses_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags); -void nm_editor_bind_ip6_gateway_to_string (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags); - -void nm_editor_bind_ip6_route_to_strings (gpointer source, +void nm_editor_bind_ip_route_to_strings (int family, + gpointer source, const gchar *source_property, gpointer dest_target, const gchar *dest_target_property, diff --git a/clients/tui/nmt-page-ip4.c b/clients/tui/nmt-page-ip4.c index 8d10b4aa7f..686c2ce923 100644 --- a/clients/tui/nmt-page-ip4.c +++ b/clients/tui/nmt-page-ip4.c @@ -138,21 +138,24 @@ nmt_page_ip4_constructed (GObject *object) grid = NMT_PAGE_GRID (ip4); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4_WITH_PREFIX); - nm_editor_bind_ip4_addresses_with_prefix_to_strv (s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, - widget, "strings", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_addresses_with_prefix_to_strv (AF_INET, + s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, + widget, "strings", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Addresses"), widget, NULL); widget = nmt_ip_entry_new (25, AF_INET, FALSE, TRUE); - nm_editor_bind_ip4_gateway_to_string (s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, - widget, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_gateway_to_string (AF_INET, + s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, + widget, "text", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Gateway"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4); - nm_editor_bind_ip4_addresses_to_strv (s_ip4, NM_SETTING_IP4_CONFIG_DNS, - widget, "strings", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_addresses_to_strv (AF_INET, + s_ip4, NM_SETTING_IP4_CONFIG_DNS, + widget, "strings", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("DNS servers"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME); diff --git a/clients/tui/nmt-page-ip6.c b/clients/tui/nmt-page-ip6.c index 7d3ef548fd..05917fa0c8 100644 --- a/clients/tui/nmt-page-ip6.c +++ b/clients/tui/nmt-page-ip6.c @@ -138,21 +138,24 @@ nmt_page_ip6_constructed (GObject *object) grid = NMT_PAGE_GRID (ip6); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6_WITH_PREFIX); - nm_editor_bind_ip6_addresses_with_prefix_to_strv (s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, - widget, "strings", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_addresses_with_prefix_to_strv (AF_INET6, + s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, + widget, "strings", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Addresses"), widget, NULL); widget = nmt_ip_entry_new (25, AF_INET6, FALSE, TRUE); - nm_editor_bind_ip6_gateway_to_string (s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, - widget, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_gateway_to_string (AF_INET6, + s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, + widget, "text", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Gateway"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6); - nm_editor_bind_ip6_addresses_to_strv (s_ip6, NM_SETTING_IP6_CONFIG_DNS, - widget, "strings", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_addresses_to_strv (AF_INET6, + s_ip6, NM_SETTING_IP6_CONFIG_DNS, + widget, "strings", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("DNS servers"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME); diff --git a/clients/tui/nmt-route-editor.c b/clients/tui/nmt-route-editor.c index 98fd9ccbb6..3871b86302 100644 --- a/clients/tui/nmt-route-editor.c +++ b/clients/tui/nmt-route-editor.c @@ -109,12 +109,12 @@ nmt_route_editor_constructed (GObject *object) if (NM_IS_SETTING_IP4_CONFIG (priv->edit_setting)) { routes = nmt_route_table_new (AF_INET); g_object_bind_property (priv->edit_setting, NM_SETTING_IP4_CONFIG_ROUTES, - routes, "ip4-routes", + routes, "routes", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); } else { routes = nmt_route_table_new (AF_INET6); g_object_bind_property (priv->edit_setting, NM_SETTING_IP6_CONFIG_ROUTES, - routes, "ip6-routes", + routes, "routes", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); } diff --git a/clients/tui/nmt-route-entry.c b/clients/tui/nmt-route-entry.c index 9b22f389dc..3a87552089 100644 --- a/clients/tui/nmt-route-entry.c +++ b/clients/tui/nmt-route-entry.c @@ -49,8 +49,7 @@ typedef struct { int family; int ip_entry_width, metric_entry_width; - NMIP4Route *ip4_route; - NMIP6Route *ip6_route; + NMIPRoute *route; } NmtRouteEntryPrivate; enum { @@ -58,8 +57,7 @@ enum { PROP_FAMILY, PROP_IP_ENTRY_WIDTH, PROP_METRIC_ENTRY_WIDTH, - PROP_IP4_ROUTE, - PROP_IP6_ROUTE, + PROP_ROUTE, LAST_PROP }; @@ -143,20 +141,12 @@ nmt_route_entry_constructed (GObject *object) nmt_newt_grid_add (grid, priv->metric, 4, 0); nmt_newt_widget_set_padding (priv->metric, 1, 0, 0, 0); - if (priv->family == AF_INET) { - nm_editor_bind_ip4_route_to_strings (object, "ip4-route", - priv->dest, "text", - priv->next_hop, "text", - priv->metric, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); - } else if (priv->family == AF_INET6) { - nm_editor_bind_ip6_route_to_strings (object, "ip6-route", - priv->dest, "text", - priv->next_hop, "text", - priv->metric, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); - } else - g_assert_not_reached (); + nm_editor_bind_ip_route_to_strings (priv->family, + object, "route", + priv->dest, "text", + priv->next_hop, "text", + priv->metric, "text", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); G_OBJECT_CLASS (nmt_route_entry_parent_class)->constructed (object); } @@ -174,8 +164,7 @@ nmt_route_entry_finalize (GObject *object) { NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE (object); - g_clear_pointer (&priv->ip4_route, nm_ip4_route_unref); - g_clear_pointer (&priv->ip6_route, nm_ip6_route_unref); + g_clear_pointer (&priv->route, nm_ip_route_unref); G_OBJECT_CLASS (nmt_route_entry_parent_class)->finalize (object); } @@ -198,17 +187,10 @@ nmt_route_entry_set_property (GObject *object, case PROP_METRIC_ENTRY_WIDTH: priv->metric_entry_width = g_value_get_int (value); break; - case PROP_IP4_ROUTE: - g_return_if_fail (priv->family == AF_INET); - if (priv->ip4_route) - nm_ip4_route_unref (priv->ip4_route); - priv->ip4_route = g_value_dup_boxed (value); - break; - case PROP_IP6_ROUTE: - g_return_if_fail (priv->family == AF_INET6); - if (priv->ip6_route) - nm_ip6_route_unref (priv->ip6_route); - priv->ip6_route = g_value_dup_boxed (value); + case PROP_ROUTE: + if (priv->route) + nm_ip_route_unref (priv->route); + priv->route = g_value_dup_boxed (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -234,13 +216,8 @@ nmt_route_entry_get_property (GObject *object, case PROP_METRIC_ENTRY_WIDTH: g_value_set_int (value, priv->metric_entry_width); break; - case PROP_IP4_ROUTE: - g_return_if_fail (priv->family == AF_INET); - g_value_set_boxed (value, priv->ip4_route); - break; - case PROP_IP6_ROUTE: - g_return_if_fail (priv->family == AF_INET6); - g_value_set_boxed (value, priv->ip6_route); + case PROP_ROUTE: + g_value_set_boxed (value, priv->route); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -301,27 +278,14 @@ nmt_route_entry_class_init (NmtRouteEntryClass *entry_class) G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); /** - * NmtRouteEntry:ip4-route: - * - * The contents of the entries, as an #NMIP4Route. Only valid - * if #NmtRouteEntry:family is %AF_INET. - */ - g_object_class_install_property - (object_class, PROP_IP4_ROUTE, - g_param_spec_boxed ("ip4-route", "", "", - nm_ip4_route_get_type (), - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - /** - * NmtRouteEntry:ip6-route: + * NmtRouteEntry:route: * - * The contents of the entries, as an #NMIP6Route. Only valid - * if #NmtRouteEntry:family is %AF_INET6. + * The contents of the entries, as an #NMIPRoute. */ g_object_class_install_property - (object_class, PROP_IP6_ROUTE, - g_param_spec_boxed ("ip6-route", "", "", - nm_ip6_route_get_type (), + (object_class, PROP_ROUTE, + g_param_spec_boxed ("route", "", "", + nm_ip_route_get_type (), G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); } diff --git a/clients/tui/nmt-route-table.c b/clients/tui/nmt-route-table.c index 023928c34f..523d8ee32a 100644 --- a/clients/tui/nmt-route-table.c +++ b/clients/tui/nmt-route-table.c @@ -54,8 +54,7 @@ typedef struct { enum { PROP_0, PROP_FAMILY, - PROP_IP4_ROUTES, - PROP_IP6_ROUTES, + PROP_ROUTES, LAST_PROP }; @@ -85,7 +84,7 @@ route_list_transform_to_route (GBinding *binding, NmtRouteTable *table = NMT_ROUTE_TABLE (g_binding_get_source (binding)); NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); int n = GPOINTER_TO_INT (user_data); - gpointer route; + NMIPRoute *route; if (n >= priv->routes->len) return FALSE; @@ -105,24 +104,17 @@ route_list_transform_from_route (GBinding *binding, NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); int n = GPOINTER_TO_INT (user_data); GPtrArray *routes; - gpointer route; + NMIPRoute *route; if (n >= priv->routes->len) return FALSE; route = priv->routes->pdata[n]; routes = priv->routes; - if (priv->family == AF_INET) - priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); - else - priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_route_unref); + priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); - if (route) { - if (priv->family == AF_INET) - nm_ip4_route_unref (route); - else if (priv->family == AF_INET6) - nm_ip6_route_unref (route); - } + if (route) + nm_ip_route_unref (route); routes->pdata[n] = g_value_dup_boxed (source_value); g_value_take_boxed (target_value, routes); @@ -141,21 +133,12 @@ create_route_entry (NmtWidgetList *list, priv->ip_entry_width, priv->metric_entry_width); - if (priv->family == AF_INET) { - g_object_bind_property_full (table, "ip4-routes", - entry, "ip4-route", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE, - route_list_transform_to_route, - route_list_transform_from_route, - GINT_TO_POINTER (num), NULL); - } else { - g_object_bind_property_full (table, "ip6-routes", - entry, "ip6-route", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE, - route_list_transform_to_route, - route_list_transform_from_route, - GINT_TO_POINTER (num), NULL); - } + g_object_bind_property_full (table, "routes", + entry, "route", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE, + route_list_transform_to_route, + route_list_transform_from_route, + GINT_TO_POINTER (num), NULL); return entry; } @@ -164,24 +147,15 @@ add_route (NmtWidgetList *list, gpointer table) { NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); + NMIPRoute *route; - if (priv->family == AF_INET) { - NMIP4Route *route; - - route = nm_ip4_route_new (); - nm_ip4_route_set_prefix (route, 32); - g_ptr_array_add (priv->routes, route); - nmt_widget_list_set_length (list, priv->routes->len); - g_object_notify (table, "ip4-routes"); - } else { - NMIP6Route *route; - - route = nm_ip6_route_new (); - nm_ip6_route_set_prefix (route, 128); - g_ptr_array_add (priv->routes, route); - nmt_widget_list_set_length (list, priv->routes->len); - g_object_notify (table, "ip6-routes"); - } + if (priv->family == AF_INET) + route = nm_ip_route_new (AF_INET, "0.0.0.0", 32, NULL, 0, NULL); + else + route = nm_ip_route_new (AF_INET6, "::", 128, NULL, 0, NULL); + g_ptr_array_add (priv->routes, route); + nmt_widget_list_set_length (list, priv->routes->len); + g_object_notify (table, "routes"); } static void @@ -190,7 +164,7 @@ remove_route (NmtWidgetList *list, gpointer table) { NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); - gpointer route; + NMIPRoute *route; if (num >= priv->routes->len) return; @@ -199,10 +173,7 @@ remove_route (NmtWidgetList *list, g_ptr_array_remove_index (priv->routes, num); nmt_widget_list_set_length (list, priv->routes->len); - if (priv->family == AF_INET) - g_object_notify (table, "ip4-routes"); - else - g_object_notify (table, "ip6-routes"); + g_object_notify (table, "routes"); } static void @@ -214,6 +185,8 @@ nmt_route_table_init (NmtRouteTable *table) int dest_prefix_width, next_hop_width, metric_width; char *text; + priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); + header = nmt_newt_grid_new (); text = g_strdup_printf ("%s/%s", _("Destination"), _("Prefix")); @@ -283,27 +256,12 @@ nmt_route_table_set_property (GObject *object, switch (prop_id) { case PROP_FAMILY: priv->family = g_value_get_int (value); - if (priv->family == AF_INET) - priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); - else - priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_route_unref); - break; - case PROP_IP4_ROUTES: - g_return_if_fail (priv->family == AF_INET); - array = g_value_get_boxed (value); - g_ptr_array_set_size (priv->routes, 0); - for (i = 0; i < array->len; i++) { - nm_ip4_route_ref (array->pdata[i]); - g_ptr_array_add (priv->routes, array->pdata[i]); - } - nmt_widget_list_set_length (NMT_WIDGET_LIST (priv->list), priv->routes->len); break; - case PROP_IP6_ROUTES: - g_return_if_fail (priv->family == AF_INET6); + case PROP_ROUTES: array = g_value_get_boxed (value); g_ptr_array_set_size (priv->routes, 0); for (i = 0; i < array->len; i++) { - nm_ip6_route_ref (array->pdata[i]); + nm_ip_route_ref (array->pdata[i]); g_ptr_array_add (priv->routes, array->pdata[i]); } nmt_widget_list_set_length (NMT_WIDGET_LIST (priv->list), priv->routes->len); @@ -326,12 +284,7 @@ nmt_route_table_get_property (GObject *object, case PROP_FAMILY: g_value_set_int (value, priv->family); break; - case PROP_IP4_ROUTES: - g_return_if_fail (priv->family == AF_INET); - g_value_set_boxed (value, priv->routes); - break; - case PROP_IP6_ROUTES: - g_return_if_fail (priv->family == AF_INET6); + case PROP_ROUTES: g_value_set_boxed (value, priv->routes); break; default: @@ -365,34 +318,16 @@ nmt_route_table_class_init (NmtRouteTableClass *table_class) G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); /** - * NmtRouteTable:ip4-routes: - * - * The array of routes, suitable for binding to - * #NMSettingIP4Config:routes. - * - * Only valid if #NmtRouteTable:family is %AF_INET - * - * Element-type: NMIP4Route - */ - g_object_class_install_property - (object_class, PROP_IP4_ROUTES, - g_param_spec_boxed ("ip4-routes", "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - /** - * NmtRouteTable:ip6-routes: - * - * The array of routes, suitable for binding to - * #NMSettingIP6Config:routes. + * NmtRouteTable:routes: * - * Only valid if #NmtRouteTable:family is %AF_INET6 + * The array of routes, suitable for binding to #NMSettingIP4Config:routes + * or #NMSettingIP6Config:routes. * - * Element-type: NMIP6Route + * Element-type: NMIPRoute */ g_object_class_install_property - (object_class, PROP_IP6_ROUTES, - g_param_spec_boxed ("ip6-routes", "", "", + (object_class, PROP_ROUTES, + g_param_spec_boxed ("routes", "", "", G_TYPE_PTR_ARRAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); diff --git a/docs/libnm/libnm-docs.xml b/docs/libnm/libnm-docs.xml index 007cbe4108..130c690429 100644 --- a/docs/libnm/libnm-docs.xml +++ b/docs/libnm/libnm-docs.xml @@ -87,6 +87,7 @@ <xi:include href="xml/nm-setting-generic.xml"/> <xi:include href="xml/nm-setting-gsm.xml"/> <xi:include href="xml/nm-setting-infiniband.xml"/> + <xi:include href="xml/nm-setting-ip-config.xml"/> <xi:include href="xml/nm-setting-ip4-config.xml"/> <xi:include href="xml/nm-setting-ip6-config.xml"/> <xi:include href="xml/nm-setting-olpc-mesh.xml"/> diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h index 11ffd33033..ca6d97d598 100644 --- a/include/nm-test-utils.h +++ b/include/nm-test-utils.h @@ -952,18 +952,6 @@ nmtst_assert_connection_unnormalizable (NMConnection *con, #endif -static inline void -nmtst_assert_ip4_address_equals (guint32 addr, const char *expected, const char *loc) -{ - guint32 addr2 = nmtst_inet4_from_string (expected); - - if (addr != addr2) - g_error ("assert: %s: ip4 address '%s' expected, but got %s", - loc, expected ? expected : "any", nm_utils_inet4_ntop (addr, NULL)); -} -#define nmtst_assert_ip4_address_equals(addr, expected) \ - nmtst_assert_ip4_address_equals (addr, expected, G_STRLOC) - #ifdef __NM_UTILS_H__ static inline void nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const char *expected, const char *loc) diff --git a/libnm-core/Makefile.libnm-core b/libnm-core/Makefile.libnm-core index 481fc806ac..fc6d779b5e 100644 --- a/libnm-core/Makefile.libnm-core +++ b/libnm-core/Makefile.libnm-core @@ -24,6 +24,7 @@ libnm_core_headers = \ $(core)/nm-setting-generic.h \ $(core)/nm-setting-gsm.h \ $(core)/nm-setting-infiniband.h \ + $(core)/nm-setting-ip-config.h \ $(core)/nm-setting-ip4-config.h \ $(core)/nm-setting-ip6-config.h \ $(core)/nm-setting-olpc-mesh.h \ @@ -69,6 +70,7 @@ libnm_core_sources = \ $(core)/nm-setting-generic.c \ $(core)/nm-setting-gsm.c \ $(core)/nm-setting-infiniband.c \ + $(core)/nm-setting-ip-config.c \ $(core)/nm-setting-ip4-config.c \ $(core)/nm-setting-ip6-config.c \ $(core)/nm-setting-olpc-mesh.c \ diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index fced4ffe3d..ff44eb043f 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -77,7 +77,7 @@ const char *_nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, guint32 i); gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, - NMIP4Address *address, + NMIPAddress *address, const char *label); /* NM_SETTING_COMPARE_FLAG_INFERRABLE: check whether a device-generated diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c new file mode 100644 index 0000000000..12080bd05f --- /dev/null +++ b/libnm-core/nm-setting-ip-config.c @@ -0,0 +1,874 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2007 - 2014 Red Hat, Inc. + * Copyright 2007 - 2008 Novell, Inc. + */ + +#include <string.h> +#include <glib/gi18n.h> + +#include "nm-setting-ip-config.h" +#include "nm-utils.h" +#include "nm-glib-compat.h" +#include "nm-setting-private.h" +#include "nm-utils-private.h" + +static char * +canonicalize_ip (int family, const char *ip, gboolean null_any) +{ + guint8 addr_bytes[sizeof (struct in6_addr)]; + char addr_str[NM_UTILS_INET_ADDRSTRLEN]; + int ret; + + if (!ip) { + g_return_val_if_fail (null_any == TRUE, NULL); + return NULL; + } + + ret = inet_pton (family, ip, addr_bytes); + g_return_val_if_fail (ret == 1, NULL); + + if (null_any) { + int addrlen = (family == AF_INET ? sizeof (struct in_addr) : sizeof (struct in6_addr)); + + if (!memcmp (addr_bytes, &in6addr_any, addrlen)) + return NULL; + } + + return g_strdup (inet_ntop (family, addr_bytes, addr_str, sizeof (addr_str))); +} + +static gboolean +valid_ip (int family, const char *ip, GError **error) +{ + if (!nm_utils_ipaddr_valid (family, ip)) { + g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_FAILED, + family == AF_INET ? _("Invalid IPv4 address '%s'") : _("Invalid IPv6 address '%s"), + ip); + return FALSE; + } else + return TRUE; +} + +static gboolean +valid_prefix (int family, guint prefix, GError **error) +{ + if ( (family == AF_INET && prefix > 32) + || (family == AF_INET6 && prefix > 128) + || prefix == 0) { + g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_FAILED, + family == AF_INET ? _("Invalid IPv4 address prefix '%u'") : _("Invalid IPv6 address prefix '%u"), + prefix); + return FALSE; + } + + return TRUE; +} + + +G_DEFINE_BOXED_TYPE (NMIPAddress, nm_ip_address, nm_ip_address_dup, nm_ip_address_unref) + +struct NMIPAddress { + guint refcount; + + char *address, *gateway; + int prefix, family; +}; + +/** + * nm_ip_address_new: + * @family: the IP address family (%AF_INET or %AF_INET6) + * @addr: the IP address + * @prefix: the address prefix length + * @gateway: (allow-none): the gateway + * @error: location to store error, or %NULL + * + * Creates a new #NMIPAddress object. + * + * Returns: (transfer full): the new #NMIPAddress object, or %NULL on error + **/ +NMIPAddress * +nm_ip_address_new (int family, + const char *addr, guint prefix, const char *gateway, + GError **error) +{ + NMIPAddress *address; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6, NULL); + g_return_val_if_fail (addr != NULL, NULL); + + if (!valid_ip (family, addr, error)) + return NULL; + if (!valid_prefix (family, prefix, error)) + return NULL; + if (gateway && !valid_ip (family, gateway, error)) + return NULL; + + address = g_slice_new0 (NMIPAddress); + address->refcount = 1; + + address->family = family; + address->address = canonicalize_ip (family, addr, FALSE); + address->prefix = prefix; + address->gateway = canonicalize_ip (family, gateway, TRUE); + + return address; +} + +/** + * nm_ip_address_new_binary: + * @family: the IP address family (%AF_INET or %AF_INET6) + * @addr: the IP address + * @prefix: the address prefix length + * @gateway: (allow-none): the gateway + * @error: location to store error, or %NULL + * + * Creates a new #NMIPAddress object. @addr and @gateway (if non-%NULL) must + * point to buffers of the correct size for @family. + * + * Returns: (transfer full): the new #NMIPAddress object, or %NULL on error + **/ +NMIPAddress * +nm_ip_address_new_binary (int family, + gconstpointer addr, guint prefix, gconstpointer gateway, + GError **error) +{ + NMIPAddress *address; + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6, NULL); + g_return_val_if_fail (addr != NULL, NULL); + + if (!valid_prefix (family, prefix, error)) + return NULL; + + address = g_slice_new0 (NMIPAddress); + address->refcount = 1; + + address->family = family; + address->address = g_strdup (inet_ntop (family, addr, string, sizeof (string))); + address->prefix = prefix; + if (gateway) + address->gateway = g_strdup (inet_ntop (family, gateway, string, sizeof (string))); + + return address; +} + +/** + * nm_ip_address_ref: + * @address: the #NMIPAddress + * + * Increases the reference count of the object. + **/ +void +nm_ip_address_ref (NMIPAddress *address) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (address->refcount > 0); + + address->refcount++; +} + +/** + * nm_ip_address_unref: + * @address: the #NMIPAddress + * + * Decreases the reference count of the object. If the reference count + * reaches zero, the object will be destroyed. + **/ +void +nm_ip_address_unref (NMIPAddress *address) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (address->refcount > 0); + + address->refcount--; + if (address->refcount == 0) { + g_free (address->address); + g_free (address->gateway); + g_slice_free (NMIPAddress, address); + } +} + +/** + * nm_ip_address_equal: + * @address: the #NMIPAddress + * @other: the #NMIPAddress to compare @address to. + * + * Determines if two #NMIPAddress objects contain the same values. + * + * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. + **/ +gboolean +nm_ip_address_equal (NMIPAddress *address, NMIPAddress *other) +{ + g_return_val_if_fail (address != NULL, FALSE); + g_return_val_if_fail (address->refcount > 0, FALSE); + + g_return_val_if_fail (other != NULL, FALSE); + g_return_val_if_fail (other->refcount > 0, FALSE); + + if ( address->family != other->family + || address->prefix != other->prefix + || strcmp (address->address, other->address) != 0 + || g_strcmp0 (address->gateway, other->gateway) != 0) + return FALSE; + return TRUE; +} + +/** + * nm_ip_address_dup: + * @address: the #NMIPAddress + * + * Creates a copy of @address + * + * Returns: (transfer full): a copy of @address + **/ +NMIPAddress * +nm_ip_address_dup (NMIPAddress *address) +{ + NMIPAddress *copy; + + g_return_val_if_fail (address != NULL, NULL); + g_return_val_if_fail (address->refcount > 0, NULL); + + copy = nm_ip_address_new (address->family, + address->address, address->prefix, address->gateway, + NULL); + return copy; +} + +/** + * nm_ip_address_get_family: + * @address: the #NMIPAddress + * + * Gets the IP address family (eg, AF_INET) property of this address + * object. + * + * Returns: the IP address family + **/ +int +nm_ip_address_get_family (NMIPAddress *address) +{ + g_return_val_if_fail (address != NULL, 0); + g_return_val_if_fail (address->refcount > 0, 0); + + return address->family; +} + +/** + * nm_ip_address_get_address: + * @address: the #NMIPAddress + * + * Gets the IP address property of this address object. + * + * Returns: the IP address + **/ +const char * +nm_ip_address_get_address (NMIPAddress *address) +{ + g_return_val_if_fail (address != NULL, NULL); + g_return_val_if_fail (address->refcount > 0, NULL); + + return address->address; +} + +/** + * nm_ip_address_set_address: + * @address: the #NMIPAddress + * @addr: the IP address, as a string + * + * Sets the IP address property of this address object. + * + * @addr must be a valid address of @address's family. If you aren't sure you + * have a valid address, use nm_utils_ipaddr_valid() to check it. + **/ +void +nm_ip_address_set_address (NMIPAddress *address, + const char *addr) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (addr != NULL); + g_return_if_fail (nm_utils_ipaddr_valid (address->family, addr)); + + g_free (address->address); + address->address = canonicalize_ip (address->family, addr, FALSE); +} + +/** + * nm_ip_address_get_address_binary: (skip) + * @address: the #NMIPAddress + * @addr: a buffer in which to store the address in binary format. + * + * Gets the IP address property of this address object. + * + * @addr must point to a buffer that is the correct size for @address's family. + **/ +void +nm_ip_address_get_address_binary (NMIPAddress *address, + gpointer addr) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (addr != NULL); + + inet_pton (address->family, address->address, addr); +} + +/** + * nm_ip_address_set_address_binary: (skip) + * @address: the #NMIPAddress + * @addr: the address, in binary format + * + * Sets the IP address property of this address object. + * + * @addr must point to a buffer that is the correct size for @address's family. + **/ +void +nm_ip_address_set_address_binary (NMIPAddress *address, + gconstpointer addr) +{ + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_if_fail (address != NULL); + g_return_if_fail (addr != NULL); + + g_free (address->address); + address->address = g_strdup (inet_ntop (address->family, addr, string, sizeof (string))); +} + +/** + * nm_ip_address_get_prefix: + * @address: the #NMIPAddress + * + * Gets the IP address prefix (ie "24" or "30" etc) property of this address + * object. + * + * Returns: the IP address prefix + **/ +guint +nm_ip_address_get_prefix (NMIPAddress *address) +{ + g_return_val_if_fail (address != NULL, 0); + g_return_val_if_fail (address->refcount > 0, 0); + + return address->prefix; +} + +/** + * nm_ip_address_set_prefix: + * @address: the #NMIPAddress + * @prefix: the IP address prefix + * + * Sets the IP address prefix property of this address object. + **/ +void +nm_ip_address_set_prefix (NMIPAddress *address, + guint prefix) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (valid_prefix (address->family, prefix, NULL)); + + address->prefix = prefix; +} + +/** + * nm_ip_address_get_gateway: + * @address: the #NMIPAddress + * + * Gets the gateway property of this address object; this will be %NULL if the + * address has no associated gateway. + * + * Returns: the gateway + **/ +const char * +nm_ip_address_get_gateway (NMIPAddress *address) +{ + g_return_val_if_fail (address != NULL, NULL); + g_return_val_if_fail (address->refcount > 0, NULL); + + return address->gateway; +} + +/** + * nm_ip_address_set_gateway: + * @address: the #NMIPAddress + * @gateway: (allow-none): the gateway, as a string + * + * Sets the gateway property of this address object. + * + * @gateway (if non-%NULL) must be a valid address of @address's family. If you + * aren't sure you have a valid address, use nm_utils_ipaddr_valid() to check + * it. + **/ +void +nm_ip_address_set_gateway (NMIPAddress *address, + const char *gateway) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (!gateway || nm_utils_ipaddr_valid (address->family, gateway)); + + g_free (address->gateway); + address->gateway = canonicalize_ip (address->family, gateway, TRUE); +} + + +G_DEFINE_BOXED_TYPE (NMIPRoute, nm_ip_route, nm_ip_route_dup, nm_ip_route_unref) + +struct NMIPRoute { + guint refcount; + + int family; + char *dest; + guint prefix; + char *next_hop; + guint32 metric; +}; + +/** + * nm_ip_route_new: + * @family: the IP address family (%AF_INET or %AF_INET6) + * @dest: the IP address of the route's destination + * @prefix: the address prefix length + * @next_hop: (allow-none): the IP address of the next hop (or %NULL) + * @metric: the route metric (or 0 for "default") + * @error: location to store error, or %NULL + * + * Creates a new #NMIPRoute object. + * + * Returns: (transfer full): the new #NMIPRoute object, or %NULL on error + **/ +NMIPRoute * +nm_ip_route_new (int family, + const char *dest, + guint prefix, + const char *next_hop, + guint metric, + GError **error) +{ + NMIPRoute *route; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6, NULL); + + if (!valid_ip (family, dest, error)) + return NULL; + if (!valid_prefix (family, prefix, error)) + return NULL; + if (next_hop && !valid_ip (family, next_hop, error)) + return NULL; + + route = g_slice_new0 (NMIPRoute); + route->refcount = 1; + + route->family = family; + route->dest = canonicalize_ip (family, dest, FALSE); + route->prefix = prefix; + route->next_hop = canonicalize_ip (family, next_hop, TRUE); + route->metric = metric; + + return route; +} + +/** + * nm_ip_route_new_binary: + * @family: the IP address family (%AF_INET or %AF_INET6) + * @dest: the IP address of the route's destination + * @prefix: the address prefix length + * @next_hop: (allow-none): the IP address of the next hop (or %NULL) + * @metric: the route metric (or 0 for "default") + * @error: location to store error, or %NULL + * + * Creates a new #NMIPRoute object. @dest and @next_hop (if non-%NULL) must + * point to buffers of the correct size for @family. + * + * Returns: (transfer full): the new #NMIPRoute object, or %NULL on error + **/ +NMIPRoute * +nm_ip_route_new_binary (int family, + gconstpointer dest, + guint prefix, + gconstpointer next_hop, + guint metric, + GError **error) +{ + NMIPRoute *route; + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6, NULL); + + if (!valid_prefix (family, prefix, error)) + return NULL; + + route = g_slice_new0 (NMIPRoute); + route->refcount = 1; + + route->family = family; + route->dest = g_strdup (inet_ntop (family, dest, string, sizeof (string))); + route->prefix = prefix; + if (next_hop) + route->next_hop = g_strdup (inet_ntop (family, next_hop, string, sizeof (string))); + route->metric = metric; + + return route; +} + +/** + * nm_ip_route_ref: + * @route: the #NMIPRoute + * + * Increases the reference count of the object. + **/ +void +nm_ip_route_ref (NMIPRoute *route) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (route->refcount > 0); + + route->refcount++; +} + +/** + * nm_ip_route_unref: + * @route: the #NMIPRoute + * + * Decreases the reference count of the object. If the reference count + * reaches zero, the object will be destroyed. + **/ +void +nm_ip_route_unref (NMIPRoute *route) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (route->refcount > 0); + + route->refcount--; + if (route->refcount == 0) { + g_free (route->dest); + g_free (route->next_hop); + g_slice_free (NMIPRoute, route); + } +} + +/** + * nm_ip_route_equal: + * @route: the #NMIPRoute + * @other: the #NMIPRoute to compare @route to. + * + * Determines if two #NMIPRoute objects contain the same values. + * + * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. + **/ +gboolean +nm_ip_route_equal (NMIPRoute *route, NMIPRoute *other) +{ + g_return_val_if_fail (route != NULL, FALSE); + g_return_val_if_fail (route->refcount > 0, FALSE); + + g_return_val_if_fail (other != NULL, FALSE); + g_return_val_if_fail (other->refcount > 0, FALSE); + + if ( route->prefix != other->prefix + || route->metric != other->metric + || strcmp (route->dest, other->dest) != 0 + || g_strcmp0 (route->next_hop, other->next_hop) != 0) + return FALSE; + return TRUE; +} + +/** + * nm_ip_route_dup: + * @route: the #NMIPRoute + * + * Creates a copy of @route + * + * Returns: (transfer full): a copy of @route + **/ +NMIPRoute * +nm_ip_route_dup (NMIPRoute *route) +{ + NMIPRoute *copy; + + g_return_val_if_fail (route != NULL, NULL); + g_return_val_if_fail (route->refcount > 0, NULL); + + copy = nm_ip_route_new (route->family, + route->dest, route->prefix, + route->next_hop, route->metric, + NULL); + return copy; +} + +/** + * nm_ip_route_get_family: + * @route: the #NMIPRoute + * + * Gets the IP address family (eg, AF_INET) property of this route + * object. + * + * Returns: the IP address family + **/ +int +nm_ip_route_get_family (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, 0); + g_return_val_if_fail (route->refcount > 0, 0); + + return route->family; +} + +/** + * nm_ip_route_get_dest: + * @route: the #NMIPRoute + * + * Gets the IP destination address property of this route object. + * + * Returns: the IP address of the route's destination + **/ +const char * +nm_ip_route_get_dest (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, NULL); + g_return_val_if_fail (route->refcount > 0, NULL); + + return route->dest; +} + +/** + * nm_ip_route_set_dest: + * @route: the #NMIPRoute + * @dest: the route's destination, as a string + * + * Sets the destination property of this route object. + * + * @dest must be a valid address of @route's family. If you aren't sure you + * have a valid address, use nm_utils_ipaddr_valid() to check it. + **/ +void +nm_ip_route_set_dest (NMIPRoute *route, + const char *dest) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (dest != NULL); + g_return_if_fail (nm_utils_ipaddr_valid (route->family, dest)); + + g_free (route->dest); + route->dest = canonicalize_ip (route->family, dest, FALSE); +} + +/** + * nm_ip_route_get_dest_binary: (skip) + * @route: the #NMIPRoute + * @dest: a buffer in which to store the destination in binary format. + * + * Gets the destination property of this route object. + * + * @dest must point to a buffer that is the correct size for @route's family. + **/ +void +nm_ip_route_get_dest_binary (NMIPRoute *route, + gpointer dest) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (dest != NULL); + + inet_pton (route->family, route->dest, dest); +} + +/** + * nm_ip_route_set_dest_binary: (skip) + * @route: the #NMIPRoute + * @dest: the route's destination, in binary format + * + * Sets the destination property of this route object. + * + * @dest must point to a buffer that is the correct size for @route's family. + **/ +void +nm_ip_route_set_dest_binary (NMIPRoute *route, + gconstpointer dest) +{ + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_if_fail (route != NULL); + g_return_if_fail (dest != NULL); + + g_free (route->dest); + route->dest = g_strdup (inet_ntop (route->family, dest, string, sizeof (string))); +} + +/** + * nm_ip_route_get_prefix: + * @route: the #NMIPRoute + * + * Gets the IP prefix (ie "24" or "30" etc) of this route. + * + * Returns: the IP prefix + **/ +guint +nm_ip_route_get_prefix (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, 0); + g_return_val_if_fail (route->refcount > 0, 0); + + return route->prefix; +} + +/** + * nm_ip_route_set_prefix: + * @route: the #NMIPRoute + * @prefix: the route prefix + * + * Sets the prefix property of this route object. + **/ +void +nm_ip_route_set_prefix (NMIPRoute *route, + guint prefix) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (valid_prefix (route->family, prefix, NULL)); + + route->prefix = prefix; +} + +/** + * nm_ip_route_get_next_hop: + * @route: the #NMIPRoute + * + * Gets the IP address of the next hop of this route; this will be %NULL if the + * route has no next hop. + * + * Returns: the IP address of the next hop, or %NULL if this is a device route. + **/ +const char * +nm_ip_route_get_next_hop (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, NULL); + g_return_val_if_fail (route->refcount > 0, NULL); + + return route->next_hop; +} + +/** + * nm_ip_route_set_next_hop: + * @route: the #NMIPRoute + * @next_hop: (allow-none): the route's next hop, as a string + * + * Sets the next-hop property of this route object. + * + * @next_hop (if non-%NULL) must be a valid address of @route's family. If you + * aren't sure you have a valid address, use nm_utils_ipaddr_valid() to check + * it. + **/ +void +nm_ip_route_set_next_hop (NMIPRoute *route, + const char *next_hop) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (!next_hop || nm_utils_ipaddr_valid (route->family, next_hop)); + + g_free (route->next_hop); + route->next_hop = canonicalize_ip (route->family, next_hop, TRUE); +} + +/** + * nm_ip_route_get_next_hop_binary: (skip) + * @route: the #NMIPRoute + * @next_hop: a buffer in which to store the next hop in binary format. + * + * Gets the next hop property of this route object. + * + * @next_hop must point to a buffer that is the correct size for @route's family. + * + * Returns: %TRUE if @route has a next hop, %FALSE if not (in which case + * @next_hop will be zeroed out) + **/ +gboolean +nm_ip_route_get_next_hop_binary (NMIPRoute *route, + gpointer next_hop) +{ + g_return_val_if_fail (route != NULL, FALSE); + g_return_val_if_fail (next_hop != NULL, FALSE); + + if (route->next_hop) { + inet_pton (route->family, route->next_hop, next_hop); + return TRUE; + } else { + memset (next_hop, 0, + route->family == AF_INET ? sizeof (struct in_addr) : sizeof (struct in6_addr)); + return FALSE; + } +} + +/** + * nm_ip_route_set_next_hop_binary: (skip) + * @route: the #NMIPRoute + * @next_hop: the route's next hop, in binary format + * + * Sets the destination property of this route object. + * + * @next_hop (if non-%NULL) must point to a buffer that is the correct size for + * @route's family. + **/ +void +nm_ip_route_set_next_hop_binary (NMIPRoute *route, + gconstpointer next_hop) +{ + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_if_fail (route != NULL); + + g_free (route->next_hop); + if (next_hop) + route->next_hop = g_strdup (inet_ntop (route->family, next_hop, string, sizeof (string))); + else + route->next_hop = NULL; +} + +/** + * nm_ip_route_get_metric: + * @route: the #NMIPRoute + * + * Gets the route metric property of this route object; lower values + * indicate "better" or more preferred routes; 0 indicates "default" + * (meaning NetworkManager will set it appropriately). + * + * Returns: the route metric + **/ +guint32 +nm_ip_route_get_metric (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, 0); + g_return_val_if_fail (route->refcount > 0, 0); + + return route->metric; +} + +/** + * nm_ip_route_set_metric: + * @route: the #NMIPRoute + * @metric: the route metric + * + * Sets the metric property of this route object. + **/ +void +nm_ip_route_set_metric (NMIPRoute *route, + guint32 metric) +{ + g_return_if_fail (route != NULL); + + route->metric = metric; +} diff --git a/libnm-core/nm-setting-ip-config.h b/libnm-core/nm-setting-ip-config.h new file mode 100644 index 0000000000..d282935083 --- /dev/null +++ b/libnm-core/nm-setting-ip-config.h @@ -0,0 +1,117 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2007 - 2014 Red Hat, Inc. + * Copyright 2007 - 2008 Novell, Inc. + */ + +#ifndef NM_SETTING_IP_CONFIG_H +#define NM_SETTING_IP_CONFIG_H + +#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) +#error "Only <NetworkManager.h> can be included directly." +#endif + +#include "nm-setting.h" + +G_BEGIN_DECLS + +typedef struct NMIPAddress NMIPAddress; + +GType nm_ip_address_get_type (void); + +NMIPAddress *nm_ip_address_new (int family, + const char *addr, + guint prefix, + const char *gateway, + GError **error); +NMIPAddress *nm_ip_address_new_binary (int family, + gconstpointer addr, + guint prefix, + gconstpointer gateway, + GError **error); + +void nm_ip_address_ref (NMIPAddress *address); +void nm_ip_address_unref (NMIPAddress *address); +gboolean nm_ip_address_equal (NMIPAddress *address, + NMIPAddress *other); +NMIPAddress *nm_ip_address_dup (NMIPAddress *address); + +int nm_ip_address_get_family (NMIPAddress *address); +const char *nm_ip_address_get_address (NMIPAddress *address); +void nm_ip_address_set_address (NMIPAddress *address, + const char *addr); +void nm_ip_address_get_address_binary (NMIPAddress *address, + gpointer addr); +void nm_ip_address_set_address_binary (NMIPAddress *address, + gconstpointer addr); +guint nm_ip_address_get_prefix (NMIPAddress *address); +void nm_ip_address_set_prefix (NMIPAddress *address, + guint prefix); +const char *nm_ip_address_get_gateway (NMIPAddress *address); +void nm_ip_address_set_gateway (NMIPAddress *address, + const char *gateway); + +typedef struct NMIPRoute NMIPRoute; + +GType nm_ip_route_get_type (void); + +NMIPRoute *nm_ip_route_new (int family, + const char *dest, + guint prefix, + const char *next_hop, + guint metric, + GError **error); +NMIPRoute *nm_ip_route_new_binary (int family, + gconstpointer dest, + guint prefix, + gconstpointer next_hop, + guint metric, + GError **error); + +void nm_ip_route_ref (NMIPRoute *route); +void nm_ip_route_unref (NMIPRoute *route); +gboolean nm_ip_route_equal (NMIPRoute *route, + NMIPRoute *other); +NMIPRoute *nm_ip_route_dup (NMIPRoute *route); + +int nm_ip_route_get_family (NMIPRoute *route); +const char *nm_ip_route_get_dest (NMIPRoute *route); +void nm_ip_route_set_dest (NMIPRoute *route, + const char *dest); +void nm_ip_route_get_dest_binary (NMIPRoute *route, + gpointer dest); +void nm_ip_route_set_dest_binary (NMIPRoute *route, + gconstpointer dest); +guint nm_ip_route_get_prefix (NMIPRoute *route); +void nm_ip_route_set_prefix (NMIPRoute *route, + guint prefix); +const char *nm_ip_route_get_next_hop (NMIPRoute *route); +void nm_ip_route_set_next_hop (NMIPRoute *route, + const char *next_hop); +gboolean nm_ip_route_get_next_hop_binary (NMIPRoute *route, + gpointer next_hop); +void nm_ip_route_set_next_hop_binary (NMIPRoute *route, + gconstpointer next_hop); +guint32 nm_ip_route_get_metric (NMIPRoute *route); +void nm_ip_route_set_metric (NMIPRoute *route, + guint32 metric); + +G_END_DECLS + +#endif /* NM_SETTING_IP_CONFIG_H */ diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 00f2df502e..2cc00093f9 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -39,9 +39,6 @@ * properties related to IPv4 addressing, routing, and Domain Name Service **/ -G_DEFINE_BOXED_TYPE (NMIP4Address, nm_ip4_address, nm_ip4_address_dup, nm_ip4_address_unref) -G_DEFINE_BOXED_TYPE (NMIP4Route, nm_ip4_route, nm_ip4_route_dup, nm_ip4_route_unref) - G_DEFINE_TYPE_WITH_CODE (NMSettingIP4Config, nm_setting_ip4_config, NM_TYPE_SETTING, _nm_register_setting (IP4_CONFIG, 4)) NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP4_CONFIG) @@ -52,9 +49,9 @@ typedef struct { char *method; GSList *dns; /* list of IP address strings */ GSList *dns_search; /* list of strings */ - GSList *addresses; /* array of NMIP4Address */ + GSList *addresses; /* array of NMIPAddress */ GSList *address_labels; /* list of strings */ - GSList *routes; /* array of NMIP4Route */ + GSList *routes; /* array of NMIPRoute */ gboolean ignore_auto_routes; gboolean ignore_auto_dns; char *dhcp_client_id; @@ -424,7 +421,7 @@ nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting) * * Returns: the address at index @i **/ -NMIP4Address * +NMIPAddress * nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i) { NMSettingIP4ConfigPrivate *priv; @@ -434,7 +431,7 @@ nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i) priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); - return (NMIP4Address *) g_slist_nth_data (priv->addresses, i); + return (NMIPAddress *) g_slist_nth_data (priv->addresses, i); } const char * @@ -463,18 +460,18 @@ _nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, guint32 i **/ gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, - NMIP4Address *address) + NMIPAddress *address) { return _nm_setting_ip4_config_add_address_with_label (setting, address, ""); } gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, - NMIP4Address *address, + NMIPAddress *address, const char *label) { NMSettingIP4ConfigPrivate *priv; - NMIP4Address *copy; + NMIPAddress *copy; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); @@ -483,11 +480,11 @@ _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip4_address_compare ((NMIP4Address *) iter->data, address)) + if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) return FALSE; } - copy = nm_ip4_address_dup (address); + copy = nm_ip_address_dup (address); priv->addresses = g_slist_append (priv->addresses, copy); priv->address_labels = g_slist_append (priv->address_labels, g_strdup (label)); @@ -515,7 +512,7 @@ nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i) label = g_slist_nth (priv->address_labels, i); g_return_if_fail (addr != NULL && label != NULL); - nm_ip4_address_unref ((NMIP4Address *) addr->data); + nm_ip_address_unref ((NMIPAddress *) addr->data); priv->addresses = g_slist_delete_link (priv->addresses, addr); g_free (label->data); priv->address_labels = g_slist_delete_link (priv->address_labels, label); @@ -534,7 +531,7 @@ nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i) **/ gboolean nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, - NMIP4Address *address) + NMIPAddress *address) { NMSettingIP4ConfigPrivate *priv; GSList *iter; @@ -544,8 +541,8 @@ nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip4_address_compare ((NMIP4Address *) iter->data, address)) { - nm_ip4_address_unref ((NMIP4Address *) iter->data); + if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) { + nm_ip_address_unref ((NMIPAddress *) iter->data); priv->addresses = g_slist_delete_link (priv->addresses, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); return TRUE; @@ -567,7 +564,7 @@ nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting) g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); priv->addresses = NULL; g_slist_free_full (priv->address_labels, g_free); priv->address_labels = NULL; @@ -595,7 +592,7 @@ nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting) * * Returns: the route at index @i **/ -NMIP4Route * +NMIPRoute * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) { NMSettingIP4ConfigPrivate *priv; @@ -605,7 +602,7 @@ nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); - return (NMIP4Route *) g_slist_nth_data (priv->routes, i); + return (NMIPRoute *) g_slist_nth_data (priv->routes, i); } /** @@ -620,10 +617,10 @@ nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) **/ gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, - NMIP4Route *route) + NMIPRoute *route) { NMSettingIP4ConfigPrivate *priv; - NMIP4Route *copy; + NMIPRoute *copy; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); @@ -631,11 +628,11 @@ nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip4_route_compare ((NMIP4Route *) iter->data, route)) + if (nm_ip_route_equal (iter->data, route)) return FALSE; } - copy = nm_ip4_route_dup (route); + copy = nm_ip_route_dup (route); priv->routes = g_slist_append (priv->routes, copy); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); return TRUE; @@ -660,7 +657,7 @@ nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i) elt = g_slist_nth (priv->routes, i); g_return_if_fail (elt != NULL); - nm_ip4_route_unref ((NMIP4Route *) elt->data); + nm_ip_route_unref ((NMIPRoute *) elt->data); priv->routes = g_slist_delete_link (priv->routes, elt); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); } @@ -676,7 +673,7 @@ nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i) **/ gboolean nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, - NMIP4Route *route) + NMIPRoute *route) { NMSettingIP4ConfigPrivate *priv; GSList *iter; @@ -686,8 +683,8 @@ nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip4_route_compare ((NMIP4Route *) iter->data, route)) { - nm_ip4_route_unref ((NMIP4Route *) iter->data); + if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) { + nm_ip_route_unref ((NMIPRoute *) iter->data); priv->routes = g_slist_delete_link (priv->routes, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); return TRUE; @@ -709,7 +706,7 @@ nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting) g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); priv->routes = NULL; g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); } @@ -871,7 +868,7 @@ static gboolean verify (NMSetting *setting, NMConnection *connection, GError **error) { NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - GSList *iter, *l_iter; + GSList *iter; int i; if (!priv->method) { @@ -957,33 +954,9 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) return FALSE; } - /* Validate addresses */ - for (iter = priv->addresses, l_iter = priv->address_labels, i = 0; - iter && l_iter; - iter = g_slist_next (iter), l_iter = g_slist_next (l_iter), i++) { - NMIP4Address *addr = (NMIP4Address *) iter->data; - const char *label = (const char *) l_iter->data; - guint32 prefix = nm_ip4_address_get_prefix (addr); - - if (!nm_ip4_address_get_address (addr)) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. IPv4 address is invalid"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); - return FALSE; - } - - if (!prefix || prefix > 32) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. IPv4 address has invalid prefix"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); - return FALSE; - } + /* Validate address labels */ + for (iter = priv->address_labels, i = 0; iter; iter = g_slist_next (iter), i++) { + const char *label = (const char *) iter->data; if (!verify_label (label)) { g_set_error (error, @@ -996,7 +969,7 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) } } - if (iter || l_iter) { + if (g_slist_length (priv->addresses) != g_slist_length (priv->address_labels)) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, @@ -1007,32 +980,6 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) return FALSE; } - /* Validate routes */ - for (iter = priv->routes, i = 0; iter; iter = g_slist_next (iter), i++) { - NMIP4Route *route = (NMIP4Route *) iter->data; - guint32 prefix = nm_ip4_route_get_prefix (route); - - if (!nm_ip4_route_get_dest (route)) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. route is invalid"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ROUTES); - return FALSE; - } - - if (!prefix || prefix > 32) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. route has invalid prefix"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ROUTES); - return FALSE; - } - } - /* Validate DNS */ for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) { const char *dns = (const char *) iter->data; @@ -1070,9 +1017,9 @@ finalize (GObject *object) g_slist_free_full (priv->dns, g_free); g_slist_free_full (priv->dns_search, g_free); - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); g_slist_free_full (priv->address_labels, g_free); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); G_OBJECT_CLASS (nm_setting_ip4_config_parent_class)->finalize (object); } @@ -1138,9 +1085,9 @@ set_property (GObject *object, guint prop_id, priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ADDRESSES: - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip4_address_dup); + (NMUtilsCopyFunc) nm_ip_address_dup); if (g_slist_length (priv->addresses) != g_slist_length (priv->address_labels)) { g_slist_free_full (priv->address_labels, g_free); @@ -1154,9 +1101,9 @@ set_property (GObject *object, guint prop_id, priv->address_labels = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ROUTES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip4_route_dup); + (NMUtilsCopyFunc) nm_ip_route_dup); break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); @@ -1205,13 +1152,13 @@ get_property (GObject *object, guint prop_id, g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search)); break; case PROP_ADDRESSES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip4_address_dup, (GDestroyNotify) nm_ip4_address_unref)); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref)); break; case PROP_ADDRESS_LABELS: g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->address_labels)); break; case PROP_ROUTES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip4_route_dup, (GDestroyNotify) nm_ip4_route_unref)); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref)); break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_routes (setting)); @@ -1325,7 +1272,7 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) * with the "shared", "link-local", or "disabled" methods as addressing is * either automatic or disabled with these methods. * - * Element-Type: NMIP4Address + * Element-Type: NMIPAddress **/ g_object_class_install_property (object_class, PROP_ADDRESSES, @@ -1360,7 +1307,7 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) * the 'shared', 'link-local', or 'disabled' methods because there is no * upstream network. * - * Element-Type: NMIP4Route + * Element-Type: NMIPRoute **/ g_object_class_install_property (object_class, PROP_ROUTES, @@ -1482,463 +1429,3 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); } - - -struct NMIP4Address { - guint32 refcount; - guint32 address; /* network byte order */ - guint32 prefix; - guint32 gateway; /* network byte order */ -}; - -/** - * nm_ip4_address_new: - * - * Creates and returns a new #NMIP4Address object. - * - * Returns: (transfer full): the new empty #NMIP4Address object - **/ -NMIP4Address * -nm_ip4_address_new (void) -{ - NMIP4Address *address; - - address = g_malloc0 (sizeof (NMIP4Address)); - address->refcount = 1; - return address; -} - -/** - * nm_ip4_address_dup: - * @source: the #NMIP4Address object to copy - * - * Copies a given #NMIP4Address object and returns the copy. - * - * Returns: (transfer full): the copy of the given #NMIP4Address copy - **/ -NMIP4Address * -nm_ip4_address_dup (NMIP4Address *source) -{ - NMIP4Address *address; - - g_return_val_if_fail (source != NULL, NULL); - g_return_val_if_fail (source->refcount > 0, NULL); - - address = nm_ip4_address_new (); - address->address = source->address; - address->prefix = source->prefix; - address->gateway = source->gateway; - - return address; -} - -/** - * nm_ip4_address_ref: - * @address: the #NMIP4Address - * - * Increases the reference count of the object. - **/ -void -nm_ip4_address_ref (NMIP4Address *address) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->refcount++; -} - -/** - * nm_ip4_address_unref: - * @address: the #NMIP4Address - * - * Decreases the reference count of the object. If the reference count - * reaches zero, the object will be destroyed. - **/ -void -nm_ip4_address_unref (NMIP4Address *address) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->refcount--; - if (address->refcount == 0) { - memset (address, 0, sizeof (NMIP4Address)); - g_free (address); - } -} - -/** - * nm_ip4_address_compare: - * @address: the #NMIP4Address - * @other: the #NMIP4Address to compare @address to. - * - * Determines if two #NMIP4Address objects contain the same values. - * - * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. - **/ -gboolean -nm_ip4_address_compare (NMIP4Address *address, NMIP4Address *other) -{ - g_return_val_if_fail (address != NULL, FALSE); - g_return_val_if_fail (address->refcount > 0, FALSE); - - g_return_val_if_fail (other != NULL, FALSE); - g_return_val_if_fail (other->refcount > 0, FALSE); - - if ( address->address != other->address - || address->prefix != other->prefix - || address->gateway != other->gateway) - return FALSE; - return TRUE; -} - -/** - * nm_ip4_address_get_address: - * @address: the #NMIP4Address - * - * Gets the IPv4 address property of this address object. - * - * Returns: the IPv4 address in network byte order - **/ -guint32 -nm_ip4_address_get_address (NMIP4Address *address) -{ - g_return_val_if_fail (address != NULL, 0); - g_return_val_if_fail (address->refcount > 0, 0); - - return address->address; -} - -/** - * nm_ip4_address_set_address: - * @address: the #NMIP4Address - * @addr: the IPv4 address in network byte order - * - * Sets the IPv4 address property of this object. - **/ -void -nm_ip4_address_set_address (NMIP4Address *address, guint32 addr) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->address = addr; -} - -/** - * nm_ip4_address_get_prefix: - * @address: the #NMIP4Address - * - * Gets the IPv4 address prefix (ie "24" or "30" etc) property of this address - * object. - * - * Returns: the IPv4 address prefix - **/ -guint32 -nm_ip4_address_get_prefix (NMIP4Address *address) -{ - g_return_val_if_fail (address != NULL, 0); - g_return_val_if_fail (address->refcount > 0, 0); - - return address->prefix; -} - -/** - * nm_ip4_address_set_prefix: - * @address: the #NMIP4Address - * @prefix: the address prefix, a number between 1 and 32 inclusive - * - * Sets the IPv4 address prefix. - **/ -void -nm_ip4_address_set_prefix (NMIP4Address *address, guint32 prefix) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - g_return_if_fail (prefix <= 32); - g_return_if_fail (prefix > 0); - - address->prefix = prefix; -} - -/** - * nm_ip4_address_get_gateway: - * @address: the #NMIP4Address - * - * Gets the IPv4 default gateway property of this address object. - * - * Returns: the IPv4 gateway address in network byte order - **/ -guint32 -nm_ip4_address_get_gateway (NMIP4Address *address) -{ - g_return_val_if_fail (address != NULL, 0); - g_return_val_if_fail (address->refcount > 0, 0); - - return address->gateway; -} - -/** - * nm_ip4_address_set_gateway: - * @address: the #NMIP4Address - * @gateway: the IPv4 default gateway in network byte order - * - * Sets the IPv4 default gateway property of this address object. - **/ -void -nm_ip4_address_set_gateway (NMIP4Address *address, guint32 gateway) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->gateway = gateway; -} - - -struct NMIP4Route { - guint32 refcount; - - guint32 dest; /* network byte order */ - guint32 prefix; - guint32 next_hop; /* network byte order */ - guint32 metric; /* lower metric == more preferred */ -}; - -/** - * nm_ip4_route_new: - * - * Creates and returns a new #NMIP4Route object. - * - * Returns: (transfer full): the new empty #NMIP4Route object - **/ -NMIP4Route * -nm_ip4_route_new (void) -{ - NMIP4Route *route; - - route = g_malloc0 (sizeof (NMIP4Route)); - route->refcount = 1; - return route; -} - -/** - * nm_ip4_route_dup: - * @source: the #NMIP4Route object to copy - * - * Copies a given #NMIP4Route object and returns the copy. - * - * Returns: (transfer full): the copy of the given #NMIP4Route copy - **/ -NMIP4Route * -nm_ip4_route_dup (NMIP4Route *source) -{ - NMIP4Route *route; - - g_return_val_if_fail (source != NULL, NULL); - g_return_val_if_fail (source->refcount > 0, NULL); - - route = nm_ip4_route_new (); - route->dest = source->dest; - route->prefix = source->prefix; - route->next_hop = source->next_hop; - route->metric = source->metric; - - return route; -} - -/** - * nm_ip4_route_ref: - * @route: the #NMIP4Route - * - * Increases the reference count of the object. - **/ -void -nm_ip4_route_ref (NMIP4Route *route) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->refcount++; -} - -/** - * nm_ip4_route_unref: - * @route: the #NMIP4Route - * - * Decreases the reference count of the object. If the reference count - * reaches zero, the object will be destroyed. - **/ -void -nm_ip4_route_unref (NMIP4Route *route) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->refcount--; - if (route->refcount == 0) { - memset (route, 0, sizeof (NMIP4Route)); - g_free (route); - } -} - -/** - * nm_ip4_route_compare: - * @route: the #NMIP4Route - * @other: the #NMIP4Route to compare @route to. - * - * Determines if two #NMIP4Route objects contain the same values. - * - * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. - **/ -gboolean -nm_ip4_route_compare (NMIP4Route *route, NMIP4Route *other) -{ - g_return_val_if_fail (route != NULL, FALSE); - g_return_val_if_fail (route->refcount > 0, FALSE); - - g_return_val_if_fail (other != NULL, FALSE); - g_return_val_if_fail (other->refcount > 0, FALSE); - - if ( route->dest != other->dest - || route->prefix != other->prefix - || route->next_hop != other->next_hop - || route->metric != other->metric) - return FALSE; - return TRUE; -} - -/** - * nm_ip4_route_get_dest: - * @route: the #NMIP4Route - * - * Gets the IPv4 destination address property of this route object. - * - * Returns: the IPv4 address in network byte order - **/ -guint32 -nm_ip4_route_get_dest (NMIP4Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->dest; -} - -/** - * nm_ip4_route_set_dest: - * @route: the #NMIP4Route - * @dest: the destination address in network byte order - * - * Sets the IPv4 destination address property of this route object. - **/ -void -nm_ip4_route_set_dest (NMIP4Route *route, guint32 dest) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->dest = dest; -} - -/** - * nm_ip4_route_get_prefix: - * @route: the #NMIP4Route - * - * Gets the IPv4 prefix (ie "24" or "30" etc) of this route. - * - * Returns: the IPv4 prefix - **/ -guint32 -nm_ip4_route_get_prefix (NMIP4Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->prefix; -} - -/** - * nm_ip4_route_set_prefix: - * @route: the #NMIP4Route - * @prefix: the prefix, a number between 1 and 32 inclusive - * - * Sets the IPv4 prefix of this route. - **/ -void -nm_ip4_route_set_prefix (NMIP4Route *route, guint32 prefix) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - g_return_if_fail (prefix <= 32); - g_return_if_fail (prefix > 0); - - route->prefix = prefix; -} - -/** - * nm_ip4_route_get_next_hop: - * @route: the #NMIP4Route - * - * Gets the IPv4 address of the next hop of this route. - * - * Returns: the IPv4 address in network byte order - **/ -guint32 -nm_ip4_route_get_next_hop (NMIP4Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->next_hop; -} - -/** - * nm_ip4_route_set_next_hop: - * @route: the #NMIP4Route - * @next_hop: the IPv4 address of the next hop in network byte order - * - * Sets the IPv4 address of the next hop of this route. - **/ -void -nm_ip4_route_set_next_hop (NMIP4Route *route, guint32 next_hop) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->next_hop = next_hop; -} - -/** - * nm_ip4_route_get_metric: - * @route: the #NMIP4Route - * - * Gets the route metric property of this route object; lower values indicate - * "better" or more preferred routes. - * - * Returns: the route metric - **/ -guint32 -nm_ip4_route_get_metric (NMIP4Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->metric; -} - -/** - * nm_ip4_route_set_metric: - * @route: the #NMIP4Route - * @metric: the route metric - * - * Sets the route metric property of this route object; lower values indicate - * "better" or more preferred routes. - **/ -void -nm_ip4_route_set_metric (NMIP4Route *route, guint32 metric) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->metric = metric; -} diff --git a/libnm-core/nm-setting-ip4-config.h b/libnm-core/nm-setting-ip4-config.h index 369e1d46d2..f683becda5 100644 --- a/libnm-core/nm-setting-ip4-config.h +++ b/libnm-core/nm-setting-ip4-config.h @@ -28,6 +28,7 @@ #endif #include "nm-setting.h" +#include "nm-setting-ip-config.h" G_BEGIN_DECLS @@ -97,57 +98,6 @@ G_BEGIN_DECLS */ #define NM_SETTING_IP4_CONFIG_METHOD_DISABLED "disabled" -typedef struct NMIP4Address NMIP4Address; - -GType nm_ip4_address_get_type (void); - -NMIP4Address * nm_ip4_address_new (void); -NMIP4Address * nm_ip4_address_dup (NMIP4Address *source); -void nm_ip4_address_ref (NMIP4Address *address); -void nm_ip4_address_unref (NMIP4Address *address); -/* Return TRUE if addresses are identical */ -gboolean nm_ip4_address_compare (NMIP4Address *address, NMIP4Address *other); - -guint32 nm_ip4_address_get_address (NMIP4Address *address); -void nm_ip4_address_set_address (NMIP4Address *address, - guint32 addr); /* network byte order */ - -guint32 nm_ip4_address_get_prefix (NMIP4Address *address); -void nm_ip4_address_set_prefix (NMIP4Address *address, - guint32 prefix); - -guint32 nm_ip4_address_get_gateway (NMIP4Address *address); -void nm_ip4_address_set_gateway (NMIP4Address *address, - guint32 gateway); /* network byte order */ - -typedef struct NMIP4Route NMIP4Route; - -GType nm_ip4_route_get_type (void); - -NMIP4Route * nm_ip4_route_new (void); -NMIP4Route * nm_ip4_route_dup (NMIP4Route *source); -void nm_ip4_route_ref (NMIP4Route *route); -void nm_ip4_route_unref (NMIP4Route *route); -/* Return TRUE if routes are identical */ -gboolean nm_ip4_route_compare (NMIP4Route *route, NMIP4Route *other); - -guint32 nm_ip4_route_get_dest (NMIP4Route *route); -void nm_ip4_route_set_dest (NMIP4Route *route, - guint32 dest); /* network byte order */ - -guint32 nm_ip4_route_get_prefix (NMIP4Route *route); -void nm_ip4_route_set_prefix (NMIP4Route *route, - guint32 prefix); - -guint32 nm_ip4_route_get_next_hop (NMIP4Route *route); -void nm_ip4_route_set_next_hop (NMIP4Route *route, - guint32 next_hop); /* network byte order */ - -guint32 nm_ip4_route_get_metric (NMIP4Route *route); -void nm_ip4_route_set_metric (NMIP4Route *route, - guint32 metric); - - struct _NMSettingIP4Config { NMSetting parent; }; @@ -179,17 +129,17 @@ gboolean nm_setting_ip4_config_remove_dns_search_by_value (NMSettingIP4Conf void nm_setting_ip4_config_clear_dns_searches (NMSettingIP4Config *setting); guint32 nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting); -NMIP4Address *nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIP4Address *address); +NMIPAddress * nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i); +gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIPAddress *address); void nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, NMIP4Address *address); +gboolean nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, NMIPAddress *address); void nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting); guint32 nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting); -NMIP4Route * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, NMIP4Route *route); +NMIPRoute * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i); +gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, NMIPRoute *route); void nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, NMIP4Route *route); +gboolean nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, NMIPRoute *route); void nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting); gboolean nm_setting_ip4_config_get_ignore_auto_routes (NMSettingIP4Config *setting); diff --git a/libnm-core/nm-setting-ip6-config.c b/libnm-core/nm-setting-ip6-config.c index cbc5e2c8be..3ac82eb4e1 100644 --- a/libnm-core/nm-setting-ip6-config.c +++ b/libnm-core/nm-setting-ip6-config.c @@ -37,9 +37,6 @@ * properties related to IPv6 addressing, routing, and Domain Name Service **/ -G_DEFINE_BOXED_TYPE (NMIP6Address, nm_ip6_address, nm_ip6_address_dup, nm_ip6_address_unref) -G_DEFINE_BOXED_TYPE (NMIP6Route, nm_ip6_route, nm_ip6_route_dup, nm_ip6_route_unref) - G_DEFINE_TYPE_WITH_CODE (NMSettingIP6Config, nm_setting_ip6_config, NM_TYPE_SETTING, _nm_register_setting (IP6_CONFIG, 4)) NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP6_CONFIG) @@ -51,8 +48,8 @@ typedef struct { char *dhcp_hostname; GSList *dns; /* array of struct in6_addr */ GSList *dns_search; /* list of strings */ - GSList *addresses; /* array of NMIP6Address */ - GSList *routes; /* array of NMIP6Route */ + GSList *addresses; /* array of NMIPAddress */ + GSList *routes; /* array of NMIPRoute */ gboolean ignore_auto_routes; gboolean ignore_auto_dns; gboolean never_default; @@ -437,7 +434,7 @@ nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting) * * Returns: the address at index @i **/ -NMIP6Address * +NMIPAddress * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) { NMSettingIP6ConfigPrivate *priv; @@ -447,7 +444,7 @@ nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); - return (NMIP6Address *) g_slist_nth_data (priv->addresses, i); + return (NMIPAddress *) g_slist_nth_data (priv->addresses, i); } /** @@ -463,10 +460,10 @@ nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, - NMIP6Address *address) + NMIPAddress *address) { NMSettingIP6ConfigPrivate *priv; - NMIP6Address *copy; + NMIPAddress *copy; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); @@ -474,11 +471,11 @@ nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip6_address_compare ((NMIP6Address *) iter->data, address)) + if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) return FALSE; } - copy = nm_ip6_address_dup (address); + copy = nm_ip_address_dup (address); priv->addresses = g_slist_append (priv->addresses, copy); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); return TRUE; @@ -503,7 +500,7 @@ nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i) elt = g_slist_nth (priv->addresses, i); g_return_if_fail (elt != NULL); - nm_ip6_address_unref ((NMIP6Address *) elt->data); + nm_ip_address_unref ((NMIPAddress *) elt->data); priv->addresses = g_slist_delete_link (priv->addresses, elt); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); } @@ -519,7 +516,7 @@ nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, - NMIP6Address *address) + NMIPAddress *address) { NMSettingIP6ConfigPrivate *priv; GSList *iter; @@ -529,7 +526,7 @@ nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip6_address_compare ((NMIP6Address *) iter->data, address)) { + if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) { priv->addresses = g_slist_delete_link (priv->addresses, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); return TRUE; @@ -551,7 +548,7 @@ nm_setting_ip6_config_clear_addresses (NMSettingIP6Config *setting) g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip6_address_unref); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); priv->addresses = NULL; g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); } @@ -577,7 +574,7 @@ nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting) * * Returns: the route at index @i **/ -NMIP6Route * +NMIPRoute * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) { NMSettingIP6ConfigPrivate *priv; @@ -587,7 +584,7 @@ nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); - return (NMIP6Route *) g_slist_nth_data (priv->routes, i); + return (NMIPRoute *) g_slist_nth_data (priv->routes, i); } /** @@ -602,10 +599,10 @@ nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, - NMIP6Route *route) + NMIPRoute *route) { NMSettingIP6ConfigPrivate *priv; - NMIP6Route *copy; + NMIPRoute *copy; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); @@ -613,11 +610,11 @@ nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip6_route_compare ((NMIP6Route *) iter->data, route)) + if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) return FALSE; } - copy = nm_ip6_route_dup (route); + copy = nm_ip_route_dup (route); priv->routes = g_slist_append (priv->routes, copy); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); return TRUE; @@ -642,7 +639,7 @@ nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i) elt = g_slist_nth (priv->routes, i); g_return_if_fail (elt != NULL); - nm_ip6_route_unref ((NMIP6Route *) elt->data); + nm_ip_route_unref ((NMIPRoute *) elt->data); priv->routes = g_slist_delete_link (priv->routes, elt); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); } @@ -658,7 +655,7 @@ nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, - NMIP6Route *route) + NMIPRoute *route) { NMSettingIP6ConfigPrivate *priv; GSList *iter; @@ -668,8 +665,8 @@ nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip6_route_compare ((NMIP6Route *) iter->data, route)) { - nm_ip6_route_unref ((NMIP6Route *) iter->data); + if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) { + nm_ip_route_unref ((NMIPRoute *) iter->data); priv->routes = g_slist_delete_link (priv->routes, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); return TRUE; @@ -691,7 +688,7 @@ nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting) g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); priv->routes = NULL; g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); } @@ -899,8 +896,8 @@ finalize (GObject *object) g_slist_free_full (priv->dns, g_free); g_slist_free_full (priv->dns_search, g_free); - g_slist_free_full (priv->addresses, g_free); - g_slist_free_full (priv->routes, g_free); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); G_OBJECT_CLASS (nm_setting_ip6_config_parent_class)->finalize (object); } @@ -964,14 +961,14 @@ set_property (GObject *object, guint prop_id, priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ADDRESSES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_address_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_address_unref); priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip6_address_dup); + (NMUtilsCopyFunc) nm_ip_address_dup); break; case PROP_ROUTES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip6_route_dup); + (NMUtilsCopyFunc) nm_ip_route_dup); break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); @@ -1015,10 +1012,10 @@ get_property (GObject *object, guint prop_id, g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search)); break; case PROP_ADDRESSES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip6_address_dup, (GDestroyNotify) nm_ip6_address_unref)); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref)); break; case PROP_ROUTES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip6_route_dup, (GDestroyNotify) nm_ip6_route_unref)); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref)); break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, priv->ignore_auto_routes); @@ -1139,7 +1136,7 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) * be used with the 'shared' or 'link-local' methods as the interface is * automatically assigned an address with these methods. * - * Element-Type: NMIP6Address + * Element-Type: NMIPAddress **/ g_object_class_install_property (object_class, PROP_ADDRESSES, @@ -1160,7 +1157,7 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) * to those returned by automatic configuration. Routes cannot be used with * the 'shared' or 'link-local' methods because there is no upstream network. * - * Element-Type: NMIP6Route + * Element-Type: NMIPRoute **/ g_object_class_install_property (object_class, PROP_ROUTES, @@ -1259,472 +1256,3 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); } - -/********************************************************************/ - -struct NMIP6Address { - guint32 refcount; - struct in6_addr address; - guint32 prefix; - struct in6_addr gateway; -}; - -/** - * nm_ip6_address_new: - * - * Creates and returns a new #NMIP6Address object. - * - * Returns: (transfer full): the new empty #NMIP6Address object - **/ -NMIP6Address * -nm_ip6_address_new (void) -{ - NMIP6Address *address; - - address = g_malloc0 (sizeof (NMIP6Address)); - address->refcount = 1; - return address; -} - -/** - * nm_ip6_address_dup: - * @source: the #NMIP6Address object to copy - * - * Copies a given #NMIP6Address object and returns the copy. - * - * Returns: (transfer full): the copy of the given #NMIP6Address copy - **/ -NMIP6Address * -nm_ip6_address_dup (NMIP6Address *source) -{ - NMIP6Address *address; - - g_return_val_if_fail (source != NULL, NULL); - g_return_val_if_fail (source->refcount > 0, NULL); - - address = nm_ip6_address_new (); - address->prefix = source->prefix; - memcpy (&address->address, &source->address, sizeof (struct in6_addr)); - memcpy (&address->gateway, &source->gateway, sizeof (struct in6_addr)); - - return address; -} - -/** - * nm_ip6_address_ref: - * @address: the #NMIP6Address - * - * Increases the reference count of the object. - **/ -void -nm_ip6_address_ref (NMIP6Address *address) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->refcount++; -} - -/** - * nm_ip6_address_unref: - * @address: the #NMIP6Address - * - * Decreases the reference count of the object. If the reference count - * reaches zero, the object will be destroyed. - **/ -void -nm_ip6_address_unref (NMIP6Address *address) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->refcount--; - if (address->refcount == 0) { - memset (address, 0, sizeof (NMIP6Address)); - g_free (address); - } -} - -/** - * nm_ip6_address_compare: - * @address: the #NMIP6Address - * @other: the #NMIP6Address to compare @address to. - * - * Determines if two #NMIP6Address objects contain the same values. - * - * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. - **/ -gboolean -nm_ip6_address_compare (NMIP6Address *address, NMIP6Address *other) -{ - g_return_val_if_fail (address != NULL, FALSE); - g_return_val_if_fail (address->refcount > 0, FALSE); - - g_return_val_if_fail (other != NULL, FALSE); - g_return_val_if_fail (other->refcount > 0, FALSE); - - if ( memcmp (&address->address, &other->address, sizeof (struct in6_addr)) - || address->prefix != other->prefix - || memcmp (&address->gateway, &other->gateway, sizeof (struct in6_addr))) - return FALSE; - return TRUE; -} - -/** - * nm_ip6_address_get_address: - * @address: the #NMIP6Address - * - * Gets the IPv6 address property of this address object. - * - * Returns: (array fixed-size=16) (element-type guint8) (transfer none): - * the IPv6 address - **/ -const struct in6_addr * -nm_ip6_address_get_address (NMIP6Address *address) -{ - g_return_val_if_fail (address != NULL, NULL); - g_return_val_if_fail (address->refcount > 0, NULL); - - return &address->address; -} - -/** - * nm_ip6_address_set_address: - * @address: the #NMIP6Address - * @addr: the IPv6 address - * - * Sets the IPv6 address property of this object. - **/ -void -nm_ip6_address_set_address (NMIP6Address *address, const struct in6_addr *addr) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - g_return_if_fail (addr != NULL); - - memcpy (&address->address, addr, sizeof (struct in6_addr)); -} - -/** - * nm_ip6_address_get_prefix: - * @address: the #NMIP6Address - * - * Gets the IPv6 address prefix property of this address object. - * - * Returns: the IPv6 address prefix - **/ -guint32 -nm_ip6_address_get_prefix (NMIP6Address *address) -{ - g_return_val_if_fail (address != NULL, 0); - g_return_val_if_fail (address->refcount > 0, 0); - - return address->prefix; -} - -/** - * nm_ip6_address_set_prefix: - * @address: the #NMIP6Address - * @prefix: the address prefix, a number between 0 and 128 inclusive - * - * Sets the IPv6 address prefix. - **/ -void -nm_ip6_address_set_prefix (NMIP6Address *address, guint32 prefix) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - g_return_if_fail (prefix <= 128); - g_return_if_fail (prefix > 0); - - address->prefix = prefix; -} - -/** - * nm_ip6_address_get_gateway: - * @address: the #NMIP6Address - * - * Gets the IPv6 default gateway property of this address object. - * - * Returns: (array fixed-size=16) (element-type guint8) (transfer none): - * the IPv6 gateway address - **/ -const struct in6_addr * -nm_ip6_address_get_gateway (NMIP6Address *address) -{ - g_return_val_if_fail (address != NULL, NULL); - g_return_val_if_fail (address->refcount > 0, NULL); - - return &address->gateway; -} - -/** - * nm_ip6_address_set_gateway: - * @address: the #NMIP6Address - * @gateway: the IPv6 default gateway - * - * Sets the IPv6 default gateway property of this address object. - **/ -void -nm_ip6_address_set_gateway (NMIP6Address *address, const struct in6_addr *gateway) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - g_return_if_fail (gateway != NULL); - - memcpy (&address->gateway, gateway, sizeof (struct in6_addr)); -} - -/********************************************************************/ - -struct NMIP6Route { - guint32 refcount; - - struct in6_addr dest; - guint32 prefix; - struct in6_addr next_hop; - guint32 metric; /* lower metric == more preferred */ -}; - -/** - * nm_ip6_route_new: - * - * Creates and returns a new #NMIP6Route object. - * - * Returns: (transfer full): the new empty #NMIP6Route object - **/ -NMIP6Route * -nm_ip6_route_new (void) -{ - NMIP6Route *route; - - route = g_malloc0 (sizeof (NMIP6Route)); - route->refcount = 1; - return route; -} - -/** - * nm_ip6_route_dup: - * @source: the #NMIP6Route object to copy - * - * Copies a given #NMIP6Route object and returns the copy. - * - * Returns: (transfer full): the copy of the given #NMIP6Route copy - **/ -NMIP6Route * -nm_ip6_route_dup (NMIP6Route *source) -{ - NMIP6Route *route; - - g_return_val_if_fail (source != NULL, NULL); - g_return_val_if_fail (source->refcount > 0, NULL); - - route = nm_ip6_route_new (); - route->prefix = source->prefix; - route->metric = source->metric; - memcpy (&route->dest, &source->dest, sizeof (struct in6_addr)); - memcpy (&route->next_hop, &source->next_hop, sizeof (struct in6_addr)); - - return route; -} - -/** - * nm_ip6_route_ref: - * @route: the #NMIP6Route - * - * Increases the reference count of the object. - **/ -void -nm_ip6_route_ref (NMIP6Route *route) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->refcount++; -} - -/** - * nm_ip6_route_unref: - * @route: the #NMIP6Route - * - * Decreases the reference count of the object. If the reference count - * reaches zero, the object will be destroyed. - **/ -void -nm_ip6_route_unref (NMIP6Route *route) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->refcount--; - if (route->refcount == 0) { - memset (route, 0, sizeof (NMIP6Route)); - g_free (route); - } -} - -/** - * nm_ip6_route_compare: - * @route: the #NMIP6Route - * @other: the #NMIP6Route to compare @route to. - * - * Determines if two #NMIP6Route objects contain the same values. - * - * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. - **/ -gboolean -nm_ip6_route_compare (NMIP6Route *route, NMIP6Route *other) -{ - g_return_val_if_fail (route != NULL, FALSE); - g_return_val_if_fail (route->refcount > 0, FALSE); - - g_return_val_if_fail (other != NULL, FALSE); - g_return_val_if_fail (other->refcount > 0, FALSE); - - if ( memcmp (&route->dest, &other->dest, sizeof (struct in6_addr)) - || route->prefix != other->prefix - || memcmp (&route->next_hop, &other->next_hop, sizeof (struct in6_addr)) - || route->metric != other->metric) - return FALSE; - return TRUE; -} - -/** - * nm_ip6_route_get_dest: - * @route: the #NMIP6Route - * - * Gets the IPv6 destination address property of this route object. - * - * Returns: (array fixed-size=16) (element-type guint8) (transfer none): - * the IPv6 address of destination - **/ -const struct in6_addr * -nm_ip6_route_get_dest (NMIP6Route *route) -{ - g_return_val_if_fail (route != NULL, NULL); - g_return_val_if_fail (route->refcount > 0, NULL); - - return &route->dest; -} - -/** - * nm_ip6_route_set_dest: - * @route: the #NMIP6Route - * @dest: the destination address - * - * Sets the IPv6 destination address property of this route object. - **/ -void -nm_ip6_route_set_dest (NMIP6Route *route, const struct in6_addr *dest) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - g_return_if_fail (dest != NULL); - - memcpy (&route->dest, dest, sizeof (struct in6_addr)); -} - -/** - * nm_ip6_route_get_prefix: - * @route: the #NMIP6Route - * - * Gets the IPv6 prefix (ie "32" or "64" etc) of this route. - * - * Returns: the IPv6 prefix - **/ -guint32 -nm_ip6_route_get_prefix (NMIP6Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->prefix; -} - -/** - * nm_ip6_route_set_prefix: - * @route: the #NMIP6Route - * @prefix: the prefix, a number between 1 and 128 inclusive - * - * Sets the IPv6 prefix of this route. - **/ -void -nm_ip6_route_set_prefix (NMIP6Route *route, guint32 prefix) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - g_return_if_fail (prefix <= 128); - g_return_if_fail (prefix > 0); - - route->prefix = prefix; -} - -/** - * nm_ip6_route_get_next_hop: - * @route: the #NMIP6Route - * - * Gets the IPv6 address of the next hop of this route. - * - * Returns: (array fixed-size=16) (element-type guint8) (transfer none): - * the IPv6 address of next hop - **/ -const struct in6_addr * -nm_ip6_route_get_next_hop (NMIP6Route *route) -{ - g_return_val_if_fail (route != NULL, NULL); - g_return_val_if_fail (route->refcount > 0, NULL); - - return &route->next_hop; -} - -/** - * nm_ip6_route_set_next_hop: - * @route: the #NMIP6Route - * @next_hop: the IPv6 address of the next hop - * - * Sets the IPv6 address of the next hop of this route. - **/ -void -nm_ip6_route_set_next_hop (NMIP6Route *route, const struct in6_addr *next_hop) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - g_return_if_fail (next_hop != NULL); - - memcpy (&route->next_hop, next_hop, sizeof (struct in6_addr)); -} - -/** - * nm_ip6_route_get_metric: - * @route: the #NMIP6Route - * - * Gets the route metric property of this route object; lower values indicate - * "better" or more preferred routes. - * - * Returns: the route metric - **/ -guint32 -nm_ip6_route_get_metric (NMIP6Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->metric; -} - -/** - * nm_ip6_route_set_metric: - * @route: the #NMIP6Route - * @metric: the route metric - * - * Sets the route metric property of this route object; lower values indicate - * "better" or more preferred routes. - **/ -void -nm_ip6_route_set_metric (NMIP6Route *route, guint32 metric) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->metric = metric; -} diff --git a/libnm-core/nm-setting-ip6-config.h b/libnm-core/nm-setting-ip6-config.h index 831f212bb6..73b93eeae8 100644 --- a/libnm-core/nm-setting-ip6-config.h +++ b/libnm-core/nm-setting-ip6-config.h @@ -29,6 +29,7 @@ #include <arpa/inet.h> #include "nm-setting.h" +#include "nm-setting-ip-config.h" G_BEGIN_DECLS @@ -127,57 +128,6 @@ typedef enum { NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR = 2 } NMSettingIP6ConfigPrivacy; - -typedef struct NMIP6Address NMIP6Address; - -GType nm_ip6_address_get_type (void); - -NMIP6Address * nm_ip6_address_new (void); -NMIP6Address * nm_ip6_address_dup (NMIP6Address *source); -void nm_ip6_address_ref (NMIP6Address *address); -void nm_ip6_address_unref (NMIP6Address *address); -/* Return TRUE if addresses are identical */ -gboolean nm_ip6_address_compare (NMIP6Address *address, NMIP6Address *other); - -const struct in6_addr *nm_ip6_address_get_address (NMIP6Address *address); -void nm_ip6_address_set_address (NMIP6Address *address, - const struct in6_addr *addr); - -guint32 nm_ip6_address_get_prefix (NMIP6Address *address); -void nm_ip6_address_set_prefix (NMIP6Address *address, - guint32 prefix); - -const struct in6_addr *nm_ip6_address_get_gateway (NMIP6Address *address); -void nm_ip6_address_set_gateway (NMIP6Address *address, - const struct in6_addr *gateway); - -typedef struct NMIP6Route NMIP6Route; - -GType nm_ip6_route_get_type (void); - -NMIP6Route * nm_ip6_route_new (void); -NMIP6Route * nm_ip6_route_dup (NMIP6Route *source); -void nm_ip6_route_ref (NMIP6Route *route); -void nm_ip6_route_unref (NMIP6Route *route); -/* Return TRUE if routes are identical */ -gboolean nm_ip6_route_compare (NMIP6Route *route, NMIP6Route *other); - -const struct in6_addr *nm_ip6_route_get_dest (NMIP6Route *route); -void nm_ip6_route_set_dest (NMIP6Route *route, - const struct in6_addr *dest); - -guint32 nm_ip6_route_get_prefix (NMIP6Route *route); -void nm_ip6_route_set_prefix (NMIP6Route *route, - guint32 prefix); - -const struct in6_addr *nm_ip6_route_get_next_hop (NMIP6Route *route); -void nm_ip6_route_set_next_hop (NMIP6Route *route, - const struct in6_addr *next_hop); - -guint32 nm_ip6_route_get_metric (NMIP6Route *route); -void nm_ip6_route_set_metric (NMIP6Route *route, - guint32 metric); - struct _NMSettingIP6Config { NMSetting parent; }; @@ -209,17 +159,17 @@ gboolean nm_setting_ip6_config_remove_dns_search_by_value (NMSetti void nm_setting_ip6_config_clear_dns_searches (NMSettingIP6Config *setting); guint32 nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting); -NMIP6Address * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, NMIP6Address *address); +NMIPAddress * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i); +gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, NMIPAddress *address); void nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, NMIP6Address *address); +gboolean nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, NMIPAddress *address); void nm_setting_ip6_config_clear_addresses (NMSettingIP6Config *setting); guint32 nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting); -NMIP6Route * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, NMIP6Route *route); +NMIPRoute * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i); +gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, NMIPRoute *route); void nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, NMIP6Route *route); +gboolean nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, NMIPRoute *route); void nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting); gboolean nm_setting_ip6_config_get_ignore_auto_routes (NMSettingIP6Config *setting); diff --git a/libnm-core/nm-utils-private.h b/libnm-core/nm-utils-private.h index bc42921514..4a4e82e0d9 100644 --- a/libnm-core/nm-utils-private.h +++ b/libnm-core/nm-utils-private.h @@ -40,29 +40,12 @@ GVariant * _nm_utils_bytes_to_dbus (const GValue *prop_value); void _nm_utils_bytes_from_dbus (GVariant *dbus_value, GValue *prop_value); -GVariant * _nm_utils_ip4_dns_to_dbus (const GValue *prop_value); -void _nm_utils_ip4_dns_from_dbus (GVariant *dbus_value, - GValue *prop_value); -GVariant * _nm_utils_ip4_addresses_to_dbus (const GValue *prop_value); -void _nm_utils_ip4_addresses_from_dbus (GVariant *dbus_value, - GValue *prop_value); -GVariant * _nm_utils_ip4_routes_to_dbus (const GValue *prop_value); -void _nm_utils_ip4_routes_from_dbus (GVariant *dbus_value, - GValue *prop_value); - -GVariant * _nm_utils_ip6_dns_to_dbus (const GValue *prop_value); -void _nm_utils_ip6_dns_from_dbus (GVariant *dbus_value, - GValue *prop_value); -GVariant * _nm_utils_ip6_addresses_to_dbus (const GValue *prop_value); -void _nm_utils_ip6_addresses_from_dbus (GVariant *dbus_value, - GValue *prop_value); -GVariant * _nm_utils_ip6_routes_to_dbus (const GValue *prop_value); -void _nm_utils_ip6_routes_from_dbus (GVariant *dbus_value, - GValue *prop_value); - GSList * _nm_utils_strv_to_slist (char **strv); char ** _nm_utils_slist_to_strv (GSList *slist); +GPtrArray * _nm_utils_strv_to_ptrarray (char **strv); +char ** _nm_utils_ptrarray_to_strv (GPtrArray *ptrarray); + char ** _nm_utils_strsplit_set (const char *str, const char *delimiters, int max_tokens); diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index dfa0d7cf81..a028f84d4d 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -662,7 +662,7 @@ _nm_utils_slist_to_strv (GSList *slist) { GSList *iter; char **strv; - int len, i = 0; + int len, i; len = g_slist_length (slist); strv = g_new (char *, len + 1); @@ -674,6 +674,40 @@ _nm_utils_slist_to_strv (GSList *slist) return strv; } +GPtrArray * +_nm_utils_strv_to_ptrarray (char **strv) +{ + GPtrArray *ptrarray; + int i; + + ptrarray = g_ptr_array_new_with_free_func (g_free); + + if (strv) { + for (i = 0; strv[i]; i++) + g_ptr_array_add (ptrarray, g_strdup (strv[i])); + } + + return ptrarray; +} + +char ** +_nm_utils_ptrarray_to_strv (GPtrArray *ptrarray) +{ + char **strv; + int i; + + if (!ptrarray) + return g_new0 (char *, 1); + + strv = g_new (char *, ptrarray->len + 1); + + for (i = 0; i < ptrarray->len; i++) + strv[i] = g_strdup (ptrarray->pdata[i]); + strv[i] = NULL; + + return strv; +} + /** * _nm_utils_strsplit_set: * @str: string to split @@ -1100,11 +1134,12 @@ nm_utils_ip4_dns_from_variant (GVariant *value) /** * nm_utils_ip4_addresses_to_variant: - * @addresses: (element-type NMIP4Address): an array of #NMIP4Address objects + * @addresses: (element-type NMIPAddress): an array of #NMIPAddress objects * - * Utility function to convert a #GPtrArray of #NMIP4Address objects into a - * #GVariant of type 'aau' representing an array of NetworkManager IPv4 - * addresses (which are tuples of address, prefix, and gateway). + * Utility function to convert a #GPtrArray of #NMIPAddress objects representing + * IPv4 addresses into a #GVariant of type 'aau' representing an array of + * NetworkManager IPv4 addresses (which are tuples of address, prefix, and + * gateway). * * Returns: (transfer none): a new floating #GVariant representing @addresses. **/ @@ -1118,12 +1153,18 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) if (addresses) { for (i = 0; i < addresses->len; i++) { - NMIP4Address *addr = addresses->pdata[i]; + NMIPAddress *addr = addresses->pdata[i]; guint32 array[3]; - array[0] = nm_ip4_address_get_address (addr); - array[1] = nm_ip4_address_get_prefix (addr); - array[2] = nm_ip4_address_get_gateway (addr); + if (nm_ip_address_get_family (addr) != AF_INET) + continue; + + nm_ip_address_get_address_binary (addr, &array[0]); + array[1] = nm_ip_address_get_prefix (addr); + if (nm_ip_address_get_gateway (addr)) + inet_pton (AF_INET, nm_ip_address_get_gateway (addr), &array[2]); + else + array[2] = 0; g_variant_builder_add (&builder, "@au", g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, @@ -1140,10 +1181,10 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) * * Utility function to convert a #GVariant of type 'aau' representing a list of * NetworkManager IPv4 addresses (which are tuples of address, prefix, and - * gateway) into a #GPtrArray of #NMIP4Address objects. + * gateway) into a #GPtrArray of #NMIPAddress objects. * - * Returns: (transfer full) (element-type NMIP4Address): a newly allocated - * #GPtrArray of #NMIP4Address objects + * Returns: (transfer full) (element-type NMIPAddress): a newly allocated + * #GPtrArray of #NMIPAddress objects **/ GPtrArray * nm_utils_ip4_addresses_from_variant (GVariant *value) @@ -1155,12 +1196,13 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aau")), NULL); g_variant_iter_init (&iter, value); - addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_address_unref); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); while (g_variant_iter_next (&iter, "@au", &addr_var)) { const guint32 *addr_array; gsize length; - NMIP4Address *addr; + NMIPAddress *addr; + GError *error = NULL; addr_array = g_variant_get_fixed_array (addr_var, &length, sizeof (guint32)); if (length < 3) { @@ -1169,12 +1211,15 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) continue; } - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, addr_array[0]); - nm_ip4_address_set_prefix (addr, addr_array[1]); - nm_ip4_address_set_gateway (addr, addr_array[2]); - - g_ptr_array_add (addresses, addr); + addr = nm_ip_address_new_binary (AF_INET, + &addr_array[0], addr_array[1], &addr_array[2], + &error); + if (addr) + g_ptr_array_add (addresses, addr); + else { + g_warning ("Ignoring invalid IP4 address: %s", error->message); + g_clear_error (&error); + } g_variant_unref (addr_var); } @@ -1183,11 +1228,12 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) /** * nm_utils_ip4_routes_to_variant: - * @routes: (element-type NMIP4Route): an array of #NMIP4Route objects + * @routes: (element-type NMIPRoute): an array of #NMIP4Route objects * - * Utility function to convert a #GPtrArray of #NMIP4Route objects into a - * #GVariant of type 'aau' representing an array of NetworkManager IPv4 routes - * (which are tuples of route, prefix, next hop, and metric). + * Utility function to convert a #GPtrArray of #NMIPRoute objects representing + * IPv4 routes into a #GVariant of type 'aau' representing an array of + * NetworkManager IPv4 routes (which are tuples of route, prefix, next hop, and + * metric). * * Returns: (transfer none): a new floating #GVariant representing @routes. **/ @@ -1201,13 +1247,16 @@ nm_utils_ip4_routes_to_variant (GPtrArray *routes) if (routes) { for (i = 0; i < routes->len; i++) { - NMIP4Route *route = routes->pdata[i]; + NMIPRoute *route = routes->pdata[i]; guint32 array[4]; - array[0] = nm_ip4_route_get_dest (route); - array[1] = nm_ip4_route_get_prefix (route); - array[2] = nm_ip4_route_get_next_hop (route); - array[3] = nm_ip4_route_get_metric (route); + if (nm_ip_route_get_family (route) != AF_INET) + continue; + + nm_ip_route_get_dest_binary (route, &array[0]); + array[1] = nm_ip_route_get_prefix (route); + nm_ip_route_get_next_hop_binary (route, &array[2]); + array[3] = nm_ip_route_get_metric (route); g_variant_builder_add (&builder, "@au", g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, @@ -1224,10 +1273,10 @@ nm_utils_ip4_routes_to_variant (GPtrArray *routes) * * Utility function to convert a #GVariant of type 'aau' representing an array * of NetworkManager IPv4 routes (which are tuples of route, prefix, next hop, - * and metric) into a #GPtrArray of #NMIP4Route objects. + * and metric) into a #GPtrArray of #NMIPRoute objects. * - * Returns: (transfer full) (element-type NMIP4Route): a newly allocated - * #GPtrArray of #NMIP4Route objects + * Returns: (transfer full) (element-type NMIPRoute): a newly allocated + * #GPtrArray of #NMIPRoute objects **/ GPtrArray * nm_utils_ip4_routes_from_variant (GVariant *value) @@ -1239,12 +1288,13 @@ nm_utils_ip4_routes_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aau")), NULL); g_variant_iter_init (&iter, value); - routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); while (g_variant_iter_next (&iter, "@au", &route_var)) { const guint32 *route_array; gsize length; - NMIP4Route *route; + NMIPRoute *route; + GError *error = NULL; route_array = g_variant_get_fixed_array (route_var, &length, sizeof (guint32)); if (length < 4) { @@ -1253,13 +1303,16 @@ nm_utils_ip4_routes_from_variant (GVariant *value) continue; } - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, route_array[0]); - nm_ip4_route_set_prefix (route, route_array[1]); - nm_ip4_route_set_next_hop (route, route_array[2]); - nm_ip4_route_set_metric (route, route_array[3]); - - g_ptr_array_add (routes, route); + route = nm_ip_route_new_binary (AF_INET, + &route_array[0], route_array[1], + &route_array[2], route_array[3], + &error); + if (route) + g_ptr_array_add (routes, route); + else { + g_warning ("Ignoring invalid IP4 route: %s", error->message); + g_clear_error (&error); + } g_variant_unref (route_var); } @@ -1412,11 +1465,12 @@ nm_utils_ip6_dns_from_variant (GVariant *value) /** * nm_utils_ip6_addresses_to_variant: - * @addresses: (element-type NMIP6Address): an array of #NMIP6Address objects + * @addresses: (element-type NMIPAddress): an array of #NMIPAddress objects * - * Utility function to convert a #GPtrArray of #NMIP6Address objects into a - * #GVariant of type 'a(ayuay)' representing an array of NetworkManager IPv6 - * addresses (which are tuples of address, prefix, and gateway). + * Utility function to convert a #GPtrArray of #NMIPAddress objects representing + * IPv6 addresses into a #GVariant of type 'a(ayuay)' representing an array of + * NetworkManager IPv6 addresses (which are tuples of address, prefix, and + * gateway). * * Returns: (transfer none): a new floating #GVariant representing @addresses. **/ @@ -1430,17 +1484,24 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) if (addresses) { for (i = 0; i < addresses->len; i++) { - NMIP6Address *addr = addresses->pdata[i]; + NMIPAddress *addr = addresses->pdata[i]; + struct in6_addr ip_bytes, gateway_bytes; GVariant *ip, *gateway; guint32 prefix; - ip = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, - nm_ip6_address_get_address (addr), - 16, 1); - prefix = nm_ip6_address_get_prefix (addr); - gateway = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, - nm_ip6_address_get_gateway (addr), - 16, 1); + if (nm_ip_address_get_family (addr) != AF_INET6) + continue; + + nm_ip_address_get_address_binary (addr, &ip_bytes); + ip = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &ip_bytes, 16, 1); + + prefix = nm_ip_address_get_prefix (addr); + + if (nm_ip_address_get_gateway (addr)) + inet_pton (AF_INET6, nm_ip_address_get_gateway (addr), &gateway_bytes); + else + memset (&gateway_bytes, 0, sizeof (gateway_bytes)); + gateway = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &gateway_bytes, 16, 1); g_variant_builder_add (&builder, "(@ayu@ay)", ip, prefix, gateway); } @@ -1455,10 +1516,10 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) * * Utility function to convert a #GVariant of type 'a(ayuay)' representing a * list of NetworkManager IPv6 addresses (which are tuples of address, prefix, - * and gateway) into a #GPtrArray of #NMIP6Address objects. + * and gateway) into a #GPtrArray of #NMIPAddress objects. * - * Returns: (transfer full) (element-type NMIP6Address): a newly allocated - * #GPtrArray of #NMIP6Address objects + * Returns: (transfer full) (element-type NMIPAddress): a newly allocated + * #GPtrArray of #NMIPAddress objects **/ GPtrArray * nm_utils_ip6_addresses_from_variant (GVariant *value) @@ -1471,12 +1532,13 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("a(ayuay)")), NULL); g_variant_iter_init (&iter, value); - addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_address_unref); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); while (g_variant_iter_next (&iter, "(@ayu@ay)", &addr_var, &prefix, &gateway_var)) { - NMIP6Address *addr; + NMIPAddress *addr; const struct in6_addr *addr_bytes, *gateway_bytes; gsize addr_len, gateway_len; + GError *error = NULL; if ( !g_variant_is_of_type (addr_var, G_VARIANT_TYPE_BYTESTRING) || !g_variant_is_of_type (gateway_var, G_VARIANT_TYPE_BYTESTRING)) { @@ -1490,11 +1552,6 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) __func__, (int) addr_len); goto next; } - if (prefix > 128) { - g_warning ("%s: ignoring invalid IP6 prefix %d", - __func__, prefix); - goto next; - } gateway_bytes = g_variant_get_fixed_array (gateway_var, &gateway_len, 1); if (gateway_len != 16) { g_warning ("%s: ignoring invalid IP6 address of length %d", @@ -1502,11 +1559,13 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) goto next; } - addr = nm_ip6_address_new (); - nm_ip6_address_set_address (addr, addr_bytes); - nm_ip6_address_set_prefix (addr, prefix); - nm_ip6_address_set_gateway (addr, gateway_bytes); - g_ptr_array_add (addresses, addr); + addr = nm_ip_address_new_binary (AF_INET6, addr_bytes, prefix, gateway_bytes, &error); + if (addr) + g_ptr_array_add (addresses, addr); + else { + g_warning ("Ignoring invalid IP4 address: %s", error->message); + g_clear_error (&error); + } next: g_variant_unref (addr_var); @@ -1518,11 +1577,12 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) /** * nm_utils_ip6_routes_to_variant: - * @routes: (element-type NMIP6Route): an array of #NMIP6Route objects + * @routes: (element-type NMIPRoute): an array of #NMIPRoute objects * - * Utility function to convert a #GPtrArray of #NMIP6Route objects into a - * #GVariant of type 'a(ayuayu)' representing an array of NetworkManager IPv6 - * routes (which are tuples of route, prefix, next hop, and metric). + * Utility function to convert a #GPtrArray of #NMIPRoute objects representing + * IPv6 routes into a #GVariant of type 'a(ayuayu)' representing an array of + * NetworkManager IPv6 routes (which are tuples of route, prefix, next hop, and + * metric). * * Returns: (transfer none): a new floating #GVariant representing @routes. **/ @@ -1536,18 +1596,20 @@ nm_utils_ip6_routes_to_variant (GPtrArray *routes) if (routes) { for (i = 0; i < routes->len; i++) { - NMIP6Route *route = routes->pdata[i]; + NMIPRoute *route = routes->pdata[i]; + struct in6_addr dest_bytes, next_hop_bytes; GVariant *dest, *next_hop; guint32 prefix, metric; - dest = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, - nm_ip6_route_get_dest (route), - 16, 1); - prefix = nm_ip6_route_get_prefix (route); - next_hop = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, - nm_ip6_route_get_next_hop (route), - 16, 1); - metric = nm_ip6_route_get_metric (route); + if (nm_ip_route_get_family (route) != AF_INET6) + continue; + + nm_ip_route_get_dest_binary (route, &dest_bytes); + dest = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &dest_bytes, 16, 1); + prefix = nm_ip_route_get_prefix (route); + nm_ip_route_get_next_hop_binary (route, &next_hop_bytes); + next_hop = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &next_hop_bytes, 16, 1); + metric = nm_ip_route_get_metric (route); g_variant_builder_add (&builder, "(@ayu@ayu)", dest, prefix, next_hop, metric); } @@ -1562,10 +1624,10 @@ nm_utils_ip6_routes_to_variant (GPtrArray *routes) * * Utility function to convert a #GVariant of type 'a(ayuayu)' representing an * array of NetworkManager IPv6 routes (which are tuples of route, prefix, next - * hop, and metric) into a #GPtrArray of #NMIP6Route objects. + * hop, and metric) into a #GPtrArray of #NMIPRoute objects. * - * Returns: (transfer full) (element-type NMIP6Route): a newly allocated - * #GPtrArray of #NMIP6Route objects + * Returns: (transfer full) (element-type NMIPRoute): a newly allocated + * #GPtrArray of #NMIPRoute objects **/ GPtrArray * nm_utils_ip6_routes_from_variant (GVariant *value) @@ -1579,11 +1641,12 @@ nm_utils_ip6_routes_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("a(ayuayu)")), NULL); - routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_route_unref); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); g_variant_iter_init (&iter, value); while (g_variant_iter_next (&iter, "(@ayu@ayu)", &dest_var, &prefix, &next_hop_var, &metric)) { - NMIP6Route *route; + NMIPRoute *route; + GError *error = NULL; if ( !g_variant_is_of_type (dest_var, G_VARIANT_TYPE_BYTESTRING) || !g_variant_is_of_type (next_hop_var, G_VARIANT_TYPE_BYTESTRING)) { @@ -1597,6 +1660,7 @@ nm_utils_ip6_routes_from_variant (GVariant *value) __func__, (int) dest_len); goto next; } + next_hop = g_variant_get_fixed_array (next_hop_var, &next_hop_len, 1); if (next_hop_len != 16) { g_warning ("%s: ignoring invalid IP6 address of length %d", @@ -1604,12 +1668,13 @@ nm_utils_ip6_routes_from_variant (GVariant *value) goto next; } - route = nm_ip6_route_new (); - nm_ip6_route_set_dest (route, dest); - nm_ip6_route_set_prefix (route, prefix); - nm_ip6_route_set_next_hop (route, next_hop); - nm_ip6_route_set_metric (route, metric); - g_ptr_array_add (routes, route); + route = nm_ip_route_new_binary (AF_INET6, dest, prefix, next_hop, metric, &error); + if (route) + g_ptr_array_add (routes, route); + else { + g_warning ("Ignoring invalid IP6 route: %s", error->message); + g_clear_error (&error); + } next: g_variant_unref (dest_var); @@ -2751,6 +2816,28 @@ nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst) } /** + * nm_utils_ipaddr_valid: + * @family: %AF_INET or %AF_INET6, or %AF_UNSPEC to accept either + * @ip: an IP address + * + * Checks if @ip contains a valid IP address of the given family. + * + * Return value: %TRUE or %FALSE + */ +gboolean +nm_utils_ipaddr_valid (int family, const char *ip) +{ + guint8 buf[sizeof (struct in6_addr)]; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6 || family == AF_UNSPEC, FALSE); + + if (family == AF_UNSPEC) + family = strchr (ip, ':') ? AF_INET6 : AF_INET; + + return inet_pton (family, ip, buf) == 1; +} + +/** * nm_utils_check_virtual_device_compatibility: * @virtual_type: a virtual connection type * @other_type: a connection type to test against @virtual_type @@ -2809,5 +2896,3 @@ nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_typ return FALSE; } } - - diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index 5dd3a84342..ccf15f7b84 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -174,6 +174,8 @@ gboolean nm_utils_is_uuid (const char *str); const char *nm_utils_inet4_ntop (in_addr_t inaddr, char *dst); const char *nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst); +gboolean nm_utils_ipaddr_valid (int family, const char *ip); + gboolean nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_type); G_END_DECLS diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index a71238929f..41189481f2 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -324,7 +324,7 @@ static void test_setting_ip4_config_labels (void) { NMSettingIP4Config *s_ip4; - NMIP4Address *addr; + NMIPAddress *addr; const char *label; GPtrArray *addrs; char **labels; @@ -336,12 +336,11 @@ test_setting_ip4_config_labels (void) NULL); /* addr 1 */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, 0x01010101); - nm_ip4_address_set_prefix (addr, 24); + addr = nm_ip_address_new (AF_INET, "1.1.1.1", 24, NULL, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); @@ -349,12 +348,11 @@ test_setting_ip4_config_labels (void) g_assert_cmpstr (label, ==, ""); /* addr 2 */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, 0x02020202); - nm_ip4_address_set_prefix (addr, 24); + addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, NULL, &error); + g_assert_no_error (error); _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, "eth0:1"); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); @@ -362,12 +360,11 @@ test_setting_ip4_config_labels (void) g_assert_cmpstr (label, ==, "eth0:1"); /* addr 3 */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, 0x03030303); - nm_ip4_address_set_prefix (addr, 24); + addr = nm_ip_address_new (AF_INET, "3.3.3.3", 24, NULL, &error); + g_assert_no_error (error); _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, ""); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); @@ -380,12 +377,12 @@ test_setting_ip4_config_labels (void) g_assert_no_error (error); addr = nm_setting_ip4_config_get_address (s_ip4, 0); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); g_assert_cmpstr (label, ==, "eth0:1"); addr = nm_setting_ip4_config_get_address (s_ip4, 1); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); g_assert_cmpstr (label, ==, ""); @@ -409,12 +406,12 @@ test_setting_ip4_config_labels (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); addr = nm_setting_ip4_config_get_address (s_ip4, 0); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); g_assert_cmpstr (label, ==, ""); addr = nm_setting_ip4_config_get_address (s_ip4, 1); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); g_assert_cmpstr (label, ==, ""); @@ -428,12 +425,12 @@ test_setting_ip4_config_labels (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); addr = nm_setting_ip4_config_get_address (s_ip4, 0); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); g_assert_cmpstr (label, ==, "eth0:1"); addr = nm_setting_ip4_config_get_address (s_ip4, 1); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); g_assert_cmpstr (label, ==, ""); @@ -2457,8 +2454,9 @@ test_setting_ip4_changed_signal (void) NMConnection *connection; gboolean changed = FALSE; NMSettingIP4Config *s_ip4; - NMIP4Address *addr; - NMIP4Route *route; + NMIPAddress *addr; + NMIPRoute *route; + GError *error = NULL; connection = nm_simple_connection_new (); g_signal_connect (connection, @@ -2489,22 +2487,20 @@ test_setting_ip4_changed_signal (void) ASSERT_CHANGED (nm_setting_ip4_config_add_dns_search (s_ip4, "foobar.com")); ASSERT_CHANGED (nm_setting_ip4_config_clear_dns_searches (s_ip4)); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, 0x2233); - nm_ip4_address_set_prefix (addr, 24); + addr = nm_ip_address_new (AF_INET, "22.33.0.0", 24, NULL, &error); + g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip4_config_add_address (s_ip4, addr)); ASSERT_CHANGED (nm_setting_ip4_config_remove_address (s_ip4, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*addr != NULL && label != NULL*"); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*addr != NULL*"); ASSERT_UNCHANGED (nm_setting_ip4_config_remove_address (s_ip4, 1)); g_test_assert_expected_messages (); nm_setting_ip4_config_add_address (s_ip4, addr); ASSERT_CHANGED (nm_setting_ip4_config_clear_addresses (s_ip4)); - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, 0x2233); - nm_ip4_route_set_prefix (route, 24); + route = nm_ip_route_new (AF_INET, "22.33.0.0", 24, NULL, 0, &error); + g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip4_config_add_route (s_ip4, route)); ASSERT_CHANGED (nm_setting_ip4_config_remove_route (s_ip4, 0)); @@ -2516,8 +2512,8 @@ test_setting_ip4_changed_signal (void) nm_setting_ip4_config_add_route (s_ip4, route); ASSERT_CHANGED (nm_setting_ip4_config_clear_routes (s_ip4)); - nm_ip4_address_unref (addr); - nm_ip4_route_unref (route); + nm_ip_address_unref (addr); + nm_ip_route_unref (route); g_object_unref (connection); } @@ -2527,9 +2523,9 @@ test_setting_ip6_changed_signal (void) NMConnection *connection; gboolean changed = FALSE; NMSettingIP6Config *s_ip6; - NMIP6Address *addr; - NMIP6Route *route; - const struct in6_addr t = { { { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 } } }; + NMIPAddress *addr; + NMIPRoute *route; + GError *error = NULL; connection = nm_simple_connection_new (); g_signal_connect (connection, @@ -2560,9 +2556,8 @@ test_setting_ip6_changed_signal (void) nm_setting_ip6_config_add_dns_search (s_ip6, "foobar.com"); ASSERT_CHANGED (nm_setting_ip6_config_clear_dns_searches (s_ip6)); - addr = nm_ip6_address_new (); - nm_ip6_address_set_address (addr, &t); - nm_ip6_address_set_prefix (addr, 64); + addr = nm_ip_address_new (AF_INET6, "1:2:3::4:5:6", 64, NULL, &error); + g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip6_config_add_address (s_ip6, addr)); ASSERT_CHANGED (nm_setting_ip6_config_remove_address (s_ip6, 0)); @@ -2574,9 +2569,8 @@ test_setting_ip6_changed_signal (void) nm_setting_ip6_config_add_address (s_ip6, addr); ASSERT_CHANGED (nm_setting_ip6_config_clear_addresses (s_ip6)); - route = nm_ip6_route_new (); - nm_ip6_route_set_dest (route, &t); - nm_ip6_route_set_prefix (route, 128); + route = nm_ip_route_new (AF_INET6, "1:2:3::4:5:6", 128, NULL, 0, &error); + g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip6_config_add_route (s_ip6, route)); ASSERT_CHANGED (nm_setting_ip6_config_remove_route (s_ip6, 0)); @@ -2588,8 +2582,8 @@ test_setting_ip6_changed_signal (void) nm_setting_ip6_config_add_route (s_ip6, route); ASSERT_CHANGED (nm_setting_ip6_config_clear_routes (s_ip6)); - nm_ip6_address_unref (addr); - nm_ip6_route_unref (route); + nm_ip_address_unref (addr); + nm_ip_route_unref (route); g_object_unref (connection); } diff --git a/libnm/NetworkManager.h b/libnm/NetworkManager.h index ce7bec74c2..d112e33480 100644 --- a/libnm/NetworkManager.h +++ b/libnm/NetworkManager.h @@ -63,6 +63,7 @@ #include <nm-setting-generic.h> #include <nm-setting-gsm.h> #include <nm-setting-infiniband.h> +#include <nm-setting-ip-config.h> #include <nm-setting-ip4-config.h> #include <nm-setting-ip6-config.h> #include <nm-setting-olpc-mesh.h> diff --git a/libnm/libnm.ver b/libnm/libnm.ver index d735a30e38..106beaa08f 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -268,18 +268,6 @@ global: nm_dhcp6_config_get_one_option; nm_dhcp6_config_get_options; nm_dhcp6_config_get_type; - nm_ip4_address_compare; - nm_ip4_address_dup; - nm_ip4_address_get_address; - nm_ip4_address_get_gateway; - nm_ip4_address_get_prefix; - nm_ip4_address_get_type; - nm_ip4_address_new; - nm_ip4_address_ref; - nm_ip4_address_set_address; - nm_ip4_address_set_gateway; - nm_ip4_address_set_prefix; - nm_ip4_address_unref; nm_ip4_config_get_addresses; nm_ip4_config_get_domains; nm_ip4_config_get_gateway; @@ -288,32 +276,6 @@ global: nm_ip4_config_get_searches; nm_ip4_config_get_type; nm_ip4_config_get_wins_servers; - nm_ip4_route_compare; - nm_ip4_route_dup; - nm_ip4_route_get_dest; - nm_ip4_route_get_metric; - nm_ip4_route_get_next_hop; - nm_ip4_route_get_prefix; - nm_ip4_route_get_type; - nm_ip4_route_new; - nm_ip4_route_ref; - nm_ip4_route_set_dest; - nm_ip4_route_set_metric; - nm_ip4_route_set_next_hop; - nm_ip4_route_set_prefix; - nm_ip4_route_unref; - nm_ip6_address_compare; - nm_ip6_address_dup; - nm_ip6_address_get_address; - nm_ip6_address_get_gateway; - nm_ip6_address_get_prefix; - nm_ip6_address_get_type; - nm_ip6_address_new; - nm_ip6_address_ref; - nm_ip6_address_set_address; - nm_ip6_address_set_gateway; - nm_ip6_address_set_prefix; - nm_ip6_address_unref; nm_ip6_config_get_addresses; nm_ip6_config_get_domains; nm_ip6_config_get_gateway; @@ -321,20 +283,40 @@ global: nm_ip6_config_get_routes; nm_ip6_config_get_searches; nm_ip6_config_get_type; - nm_ip6_route_compare; - nm_ip6_route_dup; - nm_ip6_route_get_dest; - nm_ip6_route_get_metric; - nm_ip6_route_get_next_hop; - nm_ip6_route_get_prefix; - nm_ip6_route_get_type; - nm_ip6_route_new; - nm_ip6_route_ref; - nm_ip6_route_set_dest; - nm_ip6_route_set_metric; - nm_ip6_route_set_next_hop; - nm_ip6_route_set_prefix; - nm_ip6_route_unref; + nm_ip_address_equal; + nm_ip_address_get_address; + nm_ip_address_get_address_binary; + nm_ip_address_get_family; + nm_ip_address_get_gateway; + nm_ip_address_get_prefix; + nm_ip_address_get_type; + nm_ip_address_new; + nm_ip_address_new_binary; + nm_ip_address_ref; + nm_ip_address_set_address; + nm_ip_address_set_address_binary; + nm_ip_address_set_gateway; + nm_ip_address_set_prefix; + nm_ip_address_unref; + nm_ip_route_equal; + nm_ip_route_get_dest; + nm_ip_route_get_dest_binary; + nm_ip_route_get_family; + nm_ip_route_get_metric; + nm_ip_route_get_next_hop; + nm_ip_route_get_next_hop_binary; + nm_ip_route_get_prefix; + nm_ip_route_get_type; + nm_ip_route_new; + nm_ip_route_new_binary; + nm_ip_route_ref; + nm_ip_route_set_dest; + nm_ip_route_set_dest_binary; + nm_ip_route_set_metric; + nm_ip_route_set_next_hop; + nm_ip_route_set_next_hop_binary; + nm_ip_route_set_prefix; + nm_ip_route_unref; nm_manager_error_get_type; nm_manager_error_quark; nm_object_get_path; @@ -833,6 +815,7 @@ global: nm_utils_ip6_dns_to_variant; nm_utils_ip6_routes_from_variant; nm_utils_ip6_routes_to_variant; + nm_utils_ipaddr_valid; nm_utils_is_empty_ssid; nm_utils_is_uuid; nm_utils_rsa_key_encrypt; diff --git a/libnm/nm-ip4-config.c b/libnm/nm-ip4-config.c index ac3e5b36e9..bc86821043 100644 --- a/libnm/nm-ip4-config.c +++ b/libnm/nm-ip4-config.c @@ -161,13 +161,13 @@ get_property (GObject *object, break; case PROP_ADDRESSES: g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_addresses (self), - (NMUtilsCopyFunc) nm_ip4_address_dup, - (GDestroyNotify) nm_ip4_address_unref)); + (NMUtilsCopyFunc) nm_ip_address_dup, + (GDestroyNotify) nm_ip_address_unref)); break; case PROP_ROUTES: g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_routes (self), - (NMUtilsCopyFunc) nm_ip4_route_dup, - (GDestroyNotify) nm_ip4_route_unref)); + (NMUtilsCopyFunc) nm_ip_route_dup, + (GDestroyNotify) nm_ip_route_unref)); break; case PROP_NAMESERVERS: g_value_set_boxed (value, (char **) nm_ip4_config_get_nameservers (self)); @@ -220,7 +220,7 @@ nm_ip4_config_class_init (NMIP4ConfigClass *config_class) /** * NMIP4Config:addresses: * - * A #GPtrArray containing the addresses (#NMIP4Address) of the configuration. + * A #GPtrArray containing the addresses (#NMIPAddress) of the configuration. **/ g_object_class_install_property (object_class, PROP_ADDRESSES, @@ -232,7 +232,7 @@ nm_ip4_config_class_init (NMIP4ConfigClass *config_class) /** * NMIP4Config:routes: * - * A #GPtrArray containing the routes (#NMIP4Route) of the configuration. + * A #GPtrArray containing the routes (#NMIPRoute) of the configuration. **/ g_object_class_install_property (object_class, PROP_ROUTES, @@ -312,8 +312,8 @@ nm_ip4_config_get_gateway (NMIP4Config *config) * * Gets the IP4 addresses (containing the address, prefix, and gateway). * - * Returns: (element-type NMIP4Address) (transfer none): the #GPtrArray - * containing #NMIP4Address<!-- -->es. This is the internal copy used by the + * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray + * containing #NMIPAddress<!-- -->es. This is the internal copy used by the * configuration and must not be modified. **/ GPtrArray * @@ -398,8 +398,8 @@ nm_ip4_config_get_wins_servers (NMIP4Config *config) * * Gets the routes. * - * Returns: (element-type NMIP4Route) (transfer none): the #GPtrArray containing - * #NMIP4Routes. This is the internal copy used by the configuration, and must + * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing + * #NMIPRoutes. This is the internal copy used by the configuration, and must * not be modified. **/ GPtrArray * diff --git a/libnm/nm-ip6-config.c b/libnm/nm-ip6-config.c index 8b48c15992..589b79ce1c 100644 --- a/libnm/nm-ip6-config.c +++ b/libnm/nm-ip6-config.c @@ -135,8 +135,8 @@ nm_ip6_config_get_gateway (NMIP6Config *config) * * Gets the IP6 addresses (containing the address, prefix, and gateway). * - * Returns: (element-type NMIP6Address) (transfer none): the #GPtrArray - * containing #NMIP6Address<!-- -->es. This is the internal copy used by the + * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray + * containing #NMIPAddress<!-- -->es. This is the internal copy used by the * configuration and must not be modified. **/ GPtrArray * @@ -203,8 +203,8 @@ nm_ip6_config_get_searches (NMIP6Config *config) * * Gets the routes. * - * Returns: (element-type NMIP6Route) (transfer none): the #GPtrArray containing - * #NMIP6Routes. This is the internal copy used by the configuration, and must + * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing + * #NMIPRoutes. This is the internal copy used by the configuration, and must * not be modified. **/ GPtrArray * @@ -246,13 +246,13 @@ get_property (GObject *object, break; case PROP_ADDRESSES: g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_addresses (self), - (NMUtilsCopyFunc) nm_ip6_address_dup, - (GDestroyNotify) nm_ip6_address_unref)); + (NMUtilsCopyFunc) nm_ip_address_dup, + (GDestroyNotify) nm_ip_address_unref)); break; case PROP_ROUTES: g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_routes (self), - (NMUtilsCopyFunc) nm_ip6_route_dup, - (GDestroyNotify) nm_ip6_route_unref)); + (NMUtilsCopyFunc) nm_ip_route_dup, + (GDestroyNotify) nm_ip_route_unref)); break; case PROP_NAMESERVERS: g_value_set_boxed (value, (char **) nm_ip6_config_get_nameservers (self)); @@ -314,7 +314,7 @@ nm_ip6_config_class_init (NMIP6ConfigClass *config_class) /** * NMIP6Config:addresses: * - * The #GPtrArray containing the IPv6 addresses (#NMIP6Address). + * The #GPtrArray containing the IPv6 addresses (#NMIPAddress). **/ g_object_class_install_property (object_class, PROP_ADDRESSES, @@ -326,7 +326,7 @@ nm_ip6_config_class_init (NMIP6ConfigClass *config_class) /** * NMIP6Config:routes: * - * The #GPtrArray containing the IPv6 routes (#NMIP6Route). + * The #GPtrArray containing the IPv6 routes (#NMIPRoute). **/ g_object_class_install_property (object_class, PROP_ROUTES, diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 526aba570c..9b624a2f6b 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -2391,19 +2391,25 @@ nm_utils_ip4_routes_from_gvalue (const GValue *value) routes = (GPtrArray *) g_value_get_boxed (value); for (i = 0; routes && (i < routes->len); i++) { GArray *array = (GArray *) g_ptr_array_index (routes, i); - NMIP4Route *route; + guint32 *array_val = (guint32 *) array->data; + NMIPRoute *route; + GError *error = NULL; if (array->len < 4) { g_warning ("Ignoring invalid IP4 route"); continue; } - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, g_array_index (array, guint32, 0)); - nm_ip4_route_set_prefix (route, g_array_index (array, guint32, 1)); - nm_ip4_route_set_next_hop (route, g_array_index (array, guint32, 2)); - nm_ip4_route_set_metric (route, g_array_index (array, guint32, 3)); - list = g_slist_prepend (list, route); + route = nm_ip_route_new_binary (AF_INET, + &array_val[0], array_val[1], + &array_val[2], array_val[3], + &error); + if (route) + list = g_slist_prepend (list, route); + else { + g_warning ("Ignoring invalid IP4 route: %s", error->message); + g_clear_error (&error); + } } return g_slist_reverse (list); @@ -2445,7 +2451,8 @@ nm_utils_ip6_routes_from_gvalue (const GValue *value) GValueArray *route_values = (GValueArray *) g_ptr_array_index (routes, i); GByteArray *dest, *next_hop; guint prefix, metric; - NMIP6Route *route; + NMIPRoute *route; + GError *error = NULL; if (!_nm_utils_gvalue_array_validate (route_values, 4, DBUS_TYPE_G_UCHAR_ARRAY, @@ -2474,12 +2481,16 @@ nm_utils_ip6_routes_from_gvalue (const GValue *value) metric = g_value_get_uint (g_value_array_get_nth (route_values, 3)); - route = nm_ip6_route_new (); - nm_ip6_route_set_dest (route, (struct in6_addr *)dest->data); - nm_ip6_route_set_prefix (route, prefix); - nm_ip6_route_set_next_hop (route, (struct in6_addr *)next_hop->data); - nm_ip6_route_set_metric (route, metric); - list = g_slist_prepend (list, route); + route = nm_ip_route_new_binary (AF_INET6, + dest->data, prefix, + next_hop->data, metric, + &error); + if (route) + list = g_slist_prepend (list, route); + else { + g_warning ("Ignoring invalid IP6 route: %s", error->message); + g_clear_error (&error); + } } return g_slist_reverse (list); diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index af5e52750c..6071b9527d 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -2873,11 +2873,11 @@ reserve_shared_ip (NMDevice *self, NMSettingIP4Config *s_ip4, NMPlatformIP4Addre if (s_ip4 && nm_setting_ip4_config_get_num_addresses (s_ip4)) { /* Use the first user-supplied address */ - NMIP4Address *user = nm_setting_ip4_config_get_address (s_ip4, 0); + NMIPAddress *user = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (user); - address->address = nm_ip4_address_get_address (user); - address->plen = nm_ip4_address_get_prefix (user); + nm_ip_address_get_address_binary (user, &address->address); + address->plen = nm_ip_address_get_prefix (user); } else { /* Find an unused address in the 10.42.x.x range */ guint32 start = (guint32) ntohl (0x0a2a0001); /* 10.42.0.1 */ @@ -4650,8 +4650,7 @@ send_arps (NMDevice *self, const char *mode_arg) NMConnection *connection; NMSettingIP4Config *s_ip4; int i, num; - NMIP4Address *addr; - guint32 ipaddr; + NMIPAddress *addr; GError *error = NULL; connection = nm_device_get_connection (self); @@ -4673,8 +4672,7 @@ send_arps (NMDevice *self, const char *mode_arg) for (i = 0; i < num; i++) { gs_free char *tmp_str = NULL; addr = nm_setting_ip4_config_get_address (s_ip4, i); - ipaddr = nm_ip4_address_get_address (addr); - argv[ip_arg] = (char *) nm_utils_inet4_ntop (ipaddr, NULL); + argv[ip_arg] = nm_ip_address_get_address (addr); _LOGD (LOGD_DEVICE | LOGD_IP4, "arping: run %s", (tmp_str = g_strjoinv (" ", (char **) argv))); diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 343aabcd0b..46b8fb1983 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -317,9 +317,11 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i else if (nm_setting_ip4_config_get_ignore_auto_routes (setting)) nm_ip4_config_set_never_default (config, FALSE); for (i = 0; i < naddresses; i++) { - guint32 gateway = nm_ip4_address_get_gateway (nm_setting_ip4_config_get_address (setting, i)); + const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip4_config_get_address (setting, i)); + guint32 gateway; - if (gateway) { + if (gateway_str) { + inet_pton (AF_INET, gateway_str, &gateway); nm_ip4_config_set_gateway (config, gateway); break; } @@ -327,13 +329,13 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i /* Addresses */ for (i = 0; i < naddresses; i++) { - NMIP4Address *s_addr = nm_setting_ip4_config_get_address (setting, i); + NMIPAddress *s_addr = nm_setting_ip4_config_get_address (setting, i); const char *label = _nm_setting_ip4_config_get_address_label (setting, i); NMPlatformIP4Address address; memset (&address, 0, sizeof (address)); - address.address = nm_ip4_address_get_address (s_addr); - address.plen = nm_ip4_address_get_prefix (s_addr); + nm_ip_address_get_address_binary (s_addr, &address.address); + address.plen = nm_ip_address_get_prefix (s_addr); address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT; address.preferred = NM_PLATFORM_LIFETIME_PERMANENT; address.source = NM_IP_CONFIG_SOURCE_USER; @@ -346,14 +348,14 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i if (nm_setting_ip4_config_get_ignore_auto_routes (setting)) nm_ip4_config_reset_routes (config); for (i = 0; i < nroutes; i++) { - NMIP4Route *s_route = nm_setting_ip4_config_get_route (setting, i); + NMIPRoute *s_route = nm_setting_ip4_config_get_route (setting, i); NMPlatformIP4Route route; memset (&route, 0, sizeof (route)); - route.network = nm_ip4_route_get_dest (s_route); - route.plen = nm_ip4_route_get_prefix (s_route); - route.gateway = nm_ip4_route_get_next_hop (s_route); - route.metric = nm_ip4_route_get_metric (s_route); + nm_ip_route_get_dest_binary (s_route, &route.network); + route.plen = nm_ip_route_get_prefix (s_route); + nm_ip_route_get_next_hop_binary (s_route, &route.gateway); + route.metric = nm_ip_route_get_metric (s_route); if (!route.metric) route.metric = default_route_metric; route.source = NM_IP_CONFIG_SOURCE_USER; @@ -406,7 +408,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config) /* Addresses */ for (i = 0; i < naddresses; i++) { const NMPlatformIP4Address *address = nm_ip4_config_get_address (config, i); - NMIP4Address *s_addr; + NMIPAddress *s_addr; /* Detect dynamic address */ if (address->lifetime != NM_PLATFORM_LIFETIME_PERMANENT) { @@ -418,18 +420,15 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (!method) method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL; - s_addr = nm_ip4_address_new (); - - nm_ip4_address_set_address (s_addr, address->address); - nm_ip4_address_set_prefix (s_addr, address->plen); + s_addr = nm_ip_address_new_binary (AF_INET, &address->address, address->plen, NULL, NULL); /* For backwards compatibility, attach the gateway to an address if it's * in the same subnet. */ if (same_prefix (address->address, gateway, address->plen)) - nm_ip4_address_set_gateway (s_addr, gateway); + nm_ip_address_set_gateway (s_addr, nm_utils_inet4_ntop (gateway, NULL)); _nm_setting_ip4_config_add_address_with_label (s_ip4, s_addr, address->label); - nm_ip4_address_unref (s_addr); + nm_ip_address_unref (s_addr); } /* Use 'disabled' if the method wasn't previously set */ @@ -440,7 +439,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config) /* Routes */ for (i = 0; i < nroutes; i++) { const NMPlatformIP4Route *route = nm_ip4_config_get_route (config, i); - NMIP4Route *s_route; + NMIPRoute *s_route; /* Ignore default route. */ if (!route->plen) @@ -450,14 +449,12 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (route->source != NM_IP_CONFIG_SOURCE_USER) continue; - s_route = nm_ip4_route_new (); - nm_ip4_route_set_dest (s_route, route->network); - nm_ip4_route_set_prefix (s_route, route->plen); - nm_ip4_route_set_next_hop (s_route, route->gateway); - nm_ip4_route_set_metric (s_route, route->metric); - + s_route = nm_ip_route_new_binary (AF_INET, + &route->network, route->plen, + &route->gateway, route->metric, + NULL); nm_setting_ip4_config_add_route (s_ip4, s_route); - nm_ip4_route_unref (s_route); + nm_ip_route_unref (s_route); } /* DNS */ diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index d18b35c6a3..ed7d41b46b 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -421,22 +421,24 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i else if (nm_setting_ip6_config_get_ignore_auto_routes (setting)) nm_ip6_config_set_never_default (config, FALSE); for (i = 0; i < naddresses; i++) { - const struct in6_addr *gateway = nm_ip6_address_get_gateway (nm_setting_ip6_config_get_address (setting, i)); + const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip6_config_get_address (setting, i)); + struct in6_addr gateway; - if (gateway && !IN6_IS_ADDR_UNSPECIFIED (gateway)) { - nm_ip6_config_set_gateway (config, gateway); + if (gateway_str) { + inet_pton (AF_INET6, gateway_str, &gateway); + nm_ip6_config_set_gateway (config, &gateway); break; } } /* Addresses */ for (i = 0; i < naddresses; i++) { - NMIP6Address *s_addr = nm_setting_ip6_config_get_address (setting, i); + NMIPAddress *s_addr = nm_setting_ip6_config_get_address (setting, i); NMPlatformIP6Address address; memset (&address, 0, sizeof (address)); - address.address = *nm_ip6_address_get_address (s_addr); - address.plen = nm_ip6_address_get_prefix (s_addr); + nm_ip_address_get_address_binary (s_addr, &address.address); + address.plen = nm_ip_address_get_prefix (s_addr); address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT; address.preferred = NM_PLATFORM_LIFETIME_PERMANENT; address.source = NM_IP_CONFIG_SOURCE_USER; @@ -448,14 +450,14 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i if (nm_setting_ip6_config_get_ignore_auto_routes (setting)) nm_ip6_config_reset_routes (config); for (i = 0; i < nroutes; i++) { - NMIP6Route *s_route = nm_setting_ip6_config_get_route (setting, i); + NMIPRoute *s_route = nm_setting_ip6_config_get_route (setting, i); NMPlatformIP6Route route; memset (&route, 0, sizeof (route)); - route.network = *nm_ip6_route_get_dest (s_route); - route.plen = nm_ip6_route_get_prefix (s_route); - route.gateway = *nm_ip6_route_get_next_hop (s_route); - route.metric = nm_ip6_route_get_metric (s_route); + nm_ip_route_get_dest_binary (s_route, &route.network); + route.plen = nm_ip_route_get_prefix (s_route); + nm_ip_route_get_next_hop_binary (s_route, &route.gateway); + route.metric = nm_ip_route_get_metric (s_route); if (!route.metric) route.metric = default_route_metric; route.source = NM_IP_CONFIG_SOURCE_USER; @@ -508,7 +510,7 @@ nm_ip6_config_create_setting (const NMIP6Config *config) /* Addresses */ for (i = 0; i < naddresses; i++) { const NMPlatformIP6Address *address = nm_ip6_config_get_address (config, i); - NMIP6Address *s_addr; + NMIPAddress *s_addr; /* Ignore link-local address. */ if (IN6_IS_ADDR_LINKLOCAL (&address->address)) { @@ -527,15 +529,9 @@ nm_ip6_config_create_setting (const NMIP6Config *config) if (!method || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0) method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; - s_addr = nm_ip6_address_new (); - - nm_ip6_address_set_address (s_addr, &address->address); - nm_ip6_address_set_prefix (s_addr, address->plen); - if (gateway) - nm_ip6_address_set_gateway (s_addr, gateway); - + s_addr = nm_ip_address_new_binary (AF_INET6, &address->address, address->plen, gateway, NULL); nm_setting_ip6_config_add_address (s_ip6, s_addr); - nm_ip6_address_unref (s_addr); + nm_ip_address_unref (s_addr); } /* Use 'ignore' if the method wasn't previously set */ @@ -546,7 +542,7 @@ nm_ip6_config_create_setting (const NMIP6Config *config) /* Routes */ for (i = 0; i < nroutes; i++) { const NMPlatformIP6Route *route = nm_ip6_config_get_route (config, i); - NMIP6Route *s_route; + NMIPRoute *s_route; /* Ignore link-local route. */ if (IN6_IS_ADDR_LINKLOCAL (&route->network)) @@ -560,14 +556,12 @@ nm_ip6_config_create_setting (const NMIP6Config *config) if (route->source != NM_IP_CONFIG_SOURCE_USER) continue; - s_route = nm_ip6_route_new (); - nm_ip6_route_set_dest (s_route, &route->network); - nm_ip6_route_set_prefix (s_route, route->plen); - nm_ip6_route_set_next_hop (s_route, &route->gateway); - nm_ip6_route_set_metric (s_route, route->metric); - + s_route = nm_ip_route_new_binary (AF_INET6, + &route->network, route->plen, + &route->gateway, route->metric, + NULL); nm_setting_ip6_config_add_route (s_ip6, s_route); - nm_ip6_route_unref (s_route); + nm_ip_route_unref (s_route); } /* DNS */ diff --git a/src/settings/plugins/ibft/reader.c b/src/settings/plugins/ibft/reader.c index aac7715a7a..be9e249678 100644 --- a/src/settings/plugins/ibft/reader.c +++ b/src/settings/plugins/ibft/reader.c @@ -268,18 +268,14 @@ ip4_setting_add_from_block (const GPtrArray *block, GError **error) { NMSettingIP4Config *s_ip4 = NULL; - NMIP4Address *addr; + NMIPAddress *addr; const char *s_method = NULL; const char *s_ipaddr = NULL; const char *s_gateway = NULL; const char *s_dns1 = NULL; const char *s_dns2 = NULL; const char *s_netmask = NULL; - guint32 ipaddr = 0; guint32 netmask = 0; - guint32 gateway = 0; - guint32 dns1 = 0; - guint32 dns2 = 0; guint32 prefix; g_assert (block); @@ -316,7 +312,7 @@ ip4_setting_add_from_block (const GPtrArray *block, g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP address */ - if (!s_ipaddr || inet_pton (AF_INET, s_ipaddr, &ipaddr) != 1) { + if (!s_ipaddr || !nm_utils_ipaddr_valid (AF_INET, s_ipaddr)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "iBFT: malformed iscsiadm record: invalid IP address '%s'.", s_ipaddr); @@ -332,33 +328,35 @@ ip4_setting_add_from_block (const GPtrArray *block, } prefix = nm_utils_ip4_netmask_to_prefix (netmask); - if (s_gateway && inet_pton (AF_INET, s_gateway, &gateway) != 1) { + if (s_gateway && !nm_utils_ipaddr_valid (AF_INET, s_gateway)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "iBFT: malformed iscsiadm record: invalid IP gateway '%s'.", s_gateway); goto error; } - if (s_dns1 && inet_pton (AF_INET, s_dns1, &dns1) != 1) { + if (s_dns1 && !nm_utils_ipaddr_valid (AF_INET, s_dns1)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "iBFT: malformed iscsiadm record: invalid DNS1 address '%s'.", s_dns1); goto error; } - if (s_dns2 && inet_pton (AF_INET, s_dns2, &dns2) != 1) { + if (s_dns2 && !nm_utils_ipaddr_valid (AF_INET, s_dns2)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "iBFT: malformed iscsiadm record: invalid DNS2 address '%s'.", s_dns2); goto error; } - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ipaddr); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gateway); + addr = nm_ip_address_new (AF_INET, s_ipaddr, prefix, s_gateway, error); + if (!addr) { + g_prefix_error (error, "iBFT: malformed iscsiadm record: "); + goto error; + } + nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); if (s_dns1) nm_setting_ip4_config_add_dns (s_ip4, s_dns1); diff --git a/src/settings/plugins/ibft/tests/test-ibft.c b/src/settings/plugins/ibft/tests/test-ibft.c index 3fb64ac4bd..32e1eb6a8b 100644 --- a/src/settings/plugins/ibft/tests/test-ibft.c +++ b/src/settings/plugins/ibft/tests/test-ibft.c @@ -120,7 +120,7 @@ test_read_ibft_static (void) GError *error = NULL; const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f0"; - NMIP4Address *ip4_addr; + NMIPAddress *ip4_addr; GPtrArray *block; block = read_block (TEST_IBFT_DIR "/iscsiadm-test-static", expected_mac_address); @@ -160,9 +160,9 @@ test_read_ibft_static (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.32.72"); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 22); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_gateway (ip4_addr), "192.168.35.254"); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.32.72"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 22); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.35.254"); g_object_unref (connection); g_ptr_array_unref (block); @@ -220,7 +220,7 @@ test_read_ibft_vlan (void) NMSettingIP4Config *s_ip4; const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f0"; - NMIP4Address *ip4_addr; + NMIPAddress *ip4_addr; GError *error = NULL; GPtrArray *block; @@ -257,9 +257,9 @@ test_read_ibft_vlan (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.6.200"); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_gateway (ip4_addr), "0.0.0.0"); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.6.200"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, NULL); g_object_unref (connection); g_ptr_array_ref (block); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 6994f6af93..4dc64b8cfd 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -255,12 +255,10 @@ make_connection_setting (const char *file, static gboolean read_ip4_address (shvarFile *ifcfg, const char *tag, - guint32 *out_addr, + char **out_addr, GError **error) { char *value = NULL; - guint32 ip4_addr; - gboolean success = FALSE; g_return_val_if_fail (ifcfg != NULL, FALSE); g_return_val_if_fail (tag != NULL, FALSE); @@ -268,45 +266,21 @@ read_ip4_address (shvarFile *ifcfg, if (error) g_return_val_if_fail (*error == NULL, FALSE); - *out_addr = 0; + *out_addr = NULL; value = svGetValue (ifcfg, tag, FALSE); if (!value) return TRUE; - if (inet_pton (AF_INET, value, &ip4_addr) > 0) { - *out_addr = ip4_addr; - success = TRUE; + if (nm_utils_ipaddr_valid (AF_INET, value)) { + *out_addr = value; + return TRUE; } else { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid %s IP4 address '%s'", tag, value); - } - g_free (value); - return success; -} - -/* Returns TRUE on valid address, including unspecified (::) */ -static gboolean -parse_ip6_address (const char *value, - struct in6_addr *out_addr, - GError **error) -{ - struct in6_addr ip6_addr; - - g_return_val_if_fail (value != NULL, FALSE); - g_return_val_if_fail (out_addr != NULL, FALSE); - if (error) - g_return_val_if_fail (*error == NULL, FALSE); - - *out_addr = in6addr_any; - if (inet_pton (AF_INET6, value, &ip6_addr) <= 0) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IP6 address '%s'", value); + g_free (value); return FALSE; } - - *out_addr = ip6_addr; - return TRUE; } static char * @@ -358,19 +332,23 @@ static gboolean read_full_ip4_address (shvarFile *ifcfg, const char *network_file, gint32 which, - NMIP4Address *addr, + NMIPAddress *base_addr, + NMIPAddress **out_address, GError **error) { char *ip_tag, *prefix_tag, *netmask_tag, *gw_tag; - guint32 tmp; + char *ip = NULL, *gw = NULL; + long prefix = 0; gboolean success = FALSE; shvarFile *network_ifcfg; char *value; + guint32 tmp; g_return_val_if_fail (which >= -1, FALSE); g_return_val_if_fail (ifcfg != NULL, FALSE); g_return_val_if_fail (network_file != NULL, FALSE); - g_return_val_if_fail (addr != NULL, FALSE); + g_return_val_if_fail (out_address != NULL, FALSE); + g_return_val_if_fail (*out_address == NULL, FALSE); if (error) g_return_val_if_fail (*error == NULL, FALSE); @@ -380,82 +358,86 @@ read_full_ip4_address (shvarFile *ifcfg, gw_tag = get_numbered_tag ("GATEWAY", which); /* IP address */ - if (!read_ip4_address (ifcfg, ip_tag, &tmp, error)) - goto done; - if (tmp) - nm_ip4_address_set_address (addr, tmp); - else if (!nm_ip4_address_get_address (addr)) { - success = TRUE; + if (!read_ip4_address (ifcfg, ip_tag, &ip, error)) goto done; + if (!ip) { + if (base_addr) + ip = g_strdup (nm_ip_address_get_address (base_addr)); + else { + success = TRUE; + goto done; + } } /* Gateway */ - if (!read_ip4_address (ifcfg, gw_tag, &tmp, error)) + if (!read_ip4_address (ifcfg, gw_tag, &gw, error)) goto done; - if (tmp) - nm_ip4_address_set_gateway (addr, tmp); - else { + if (!gw && base_addr) + gw = g_strdup (nm_ip_address_get_gateway (base_addr)); + if (!gw) { gboolean read_success; /* If no gateway in the ifcfg, try /etc/sysconfig/network instead */ network_ifcfg = svOpenFile (network_file, NULL); if (network_ifcfg) { - read_success = read_ip4_address (network_ifcfg, "GATEWAY", &tmp, error); + read_success = read_ip4_address (network_ifcfg, "GATEWAY", &gw, error); svCloseFile (network_ifcfg); if (!read_success) goto done; - nm_ip4_address_set_gateway (addr, tmp); } } /* Prefix */ value = svGetValue (ifcfg, prefix_tag, FALSE); if (value) { - long int prefix; - errno = 0; prefix = strtol (value, NULL, 10); - if (errno || prefix <= 0 || prefix > 32) { + if (errno || prefix < 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 prefix '%s'", value); g_free (value); goto done; } - nm_ip4_address_set_prefix (addr, (guint32) prefix); g_free (value); } /* Fall back to NETMASK if no PREFIX was specified */ - if (!nm_ip4_address_get_prefix (addr)) { - if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error)) + if (prefix == 0) { + if (!read_ip4_address (ifcfg, netmask_tag, &value, error)) goto done; - if (tmp) - nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp)); + if (value) { + inet_pton (AF_INET, value, &tmp); + prefix = nm_utils_ip4_netmask_to_prefix (tmp); + g_free (value); + } } - /* Try to autodetermine the prefix for the address' class */ - if (!nm_ip4_address_get_prefix (addr)) { - guint32 prefix = 0; + if (prefix == 0 && base_addr) + prefix = nm_ip_address_get_prefix (base_addr); - prefix = nm_utils_ip4_get_default_prefix (nm_ip4_address_get_address (addr)); - nm_ip4_address_set_prefix (addr, prefix); + /* Try to autodetermine the prefix for the address' class */ + if (prefix == 0) { + if (inet_pton (AF_INET, ip, &tmp) == 1) { + prefix = nm_utils_ip4_get_default_prefix (tmp); - value = svGetValue (ifcfg, ip_tag, FALSE); - PARSE_WARNING ("missing %s, assuming %s/%u", prefix_tag, value, prefix); - g_free (value); + PARSE_WARNING ("missing %s, assuming %s/%ld", prefix_tag, ip, prefix); + } } /* Validate the prefix */ - if (nm_ip4_address_get_prefix (addr) > 32) { + if (prefix == 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Missing or invalid IP4 prefix '%d'", - nm_ip4_address_get_prefix (addr)); + "Missing IP4 prefix"); goto done; } - success = TRUE; + *out_address = nm_ip_address_new (AF_INET, ip, prefix, gw, error); + if (*out_address) + success = TRUE; done: + g_free (ip); + g_free (gw); g_free (ip_tag); g_free (prefix_tag); g_free (netmask_tag); @@ -469,12 +451,12 @@ static gboolean read_one_ip4_route (shvarFile *ifcfg, const char *network_file, guint32 which, - NMIP4Route **out_route, + NMIPRoute **out_route, GError **error) { - NMIP4Route *route; char *ip_tag, *netmask_tag, *gw_tag, *metric_tag, *value; - guint32 tmp; + char *dest = NULL, *next_hop = NULL; + long prefix, metric; gboolean success = FALSE; g_return_val_if_fail (ifcfg != NULL, FALSE); @@ -484,75 +466,72 @@ read_one_ip4_route (shvarFile *ifcfg, if (error) g_return_val_if_fail (*error == NULL, FALSE); - route = nm_ip4_route_new (); - ip_tag = g_strdup_printf ("ADDRESS%u", which); netmask_tag = g_strdup_printf ("NETMASK%u", which); gw_tag = g_strdup_printf ("GATEWAY%u", which); metric_tag = g_strdup_printf ("METRIC%u", which); /* Destination */ - if (!read_ip4_address (ifcfg, ip_tag, &tmp, error)) + if (!read_ip4_address (ifcfg, ip_tag, &dest, error)) goto out; - if (!tmp) { + if (!dest) { /* Check whether IP is missing or 0.0.0.0 */ char *val; val = svGetValue (ifcfg, ip_tag, FALSE); if (!val) { - nm_ip4_route_unref (route); - route = NULL; + *out_route = NULL; success = TRUE; /* missing route = success */ goto out; } g_free (val); } - nm_ip4_route_set_dest (route, tmp); /* Next hop */ - if (!read_ip4_address (ifcfg, gw_tag, &tmp, error)) + if (!read_ip4_address (ifcfg, gw_tag, &next_hop, error)) goto out; - /* No need to check tmp, because we don't make distinction between missing GATEWAY IP and 0.0.0.0 */ - nm_ip4_route_set_next_hop (route, tmp); + /* We don't make distinction between missing GATEWAY IP and 0.0.0.0 */ /* Prefix */ - if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error)) + if (!read_ip4_address (ifcfg, netmask_tag, &value, error)) goto out; - if (tmp) - nm_ip4_route_set_prefix (route, nm_utils_ip4_netmask_to_prefix (tmp)); + if (value) { + guint32 netmask; - /* Validate the prefix */ - if ( !nm_ip4_route_get_prefix (route) - || nm_ip4_route_get_prefix (route) > 32) { + inet_pton (AF_INET, value, &netmask); + prefix = nm_utils_ip4_netmask_to_prefix (netmask); + g_free (value); + if (netmask != nm_utils_ip4_prefix_to_netmask (prefix)) { + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "Invalid IP4 netmask '%s' \"%s\"", netmask_tag, nm_utils_inet4_ntop (netmask, NULL)); + goto out; + } + } else { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Missing or invalid IP4 prefix '%d'", - nm_ip4_route_get_prefix (route)); + "Missing IP4 route element '%s'", netmask_tag); goto out; } /* Metric */ value = svGetValue (ifcfg, metric_tag, FALSE); if (value) { - long int metric; - - errno = 0; - metric = strtol (value, NULL, 10); - if (errno || metric < 0) { + metric = nm_utils_ascii_str_to_int64 (value, 10, 0, G_MAXUINT32, -1); + if (metric < 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route metric '%s'", value); g_free (value); goto out; } - nm_ip4_route_set_metric (route, (guint32) metric); g_free (value); - } + } else + metric = 0; - *out_route = route; - success = TRUE; + *out_route = nm_ip_route_new (AF_INET, dest, prefix, next_hop, metric, error); + if (*out_route) + success = TRUE; out: - if (!success && route) - nm_ip4_route_unref (route); - + g_free (dest); + g_free (next_hop); g_free (ip_tag); g_free (netmask_tag); g_free (gw_tag); @@ -568,9 +547,8 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError char **lines = NULL, **iter; GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric; GMatchInfo *match_info; - NMIP4Route *route; - guint32 ip4_addr; - char *dest = NULL, *prefix = NULL, *metric = NULL; + NMIPRoute *route = NULL; + char *dest = NULL, *prefix = NULL, *next_hop = NULL, *metric = NULL; long int prefix_int, metric_int; gboolean success = FALSE; @@ -599,9 +577,6 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError regex_via = g_regex_new (pattern_via, 0, 0, NULL); regex_metric = g_regex_new (pattern_metric, 0, 0, NULL); - /* New NMIP4Route structure */ - route = nm_ip4_route_new (); - /* Iterate through file lines */ lines = g_strsplit_set (contents, "\n\r", -1); for (iter = lines; iter && *iter; iter++) { @@ -625,14 +600,13 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError dest = g_match_info_fetch (match_info, 1); if (!strcmp (dest, "default")) strcpy (dest, "0.0.0.0"); - if (inet_pton (AF_INET, dest, &ip4_addr) != 1) { + if (!nm_utils_ipaddr_valid (AF_INET, dest)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route destination address '%s'", dest); g_free (dest); + g_match_info_free (match_info); goto error; } - nm_ip4_route_set_dest (route, ip4_addr); - g_free (dest); /* Prefix - is optional; 32 if missing */ prefix = g_match_info_fetch (match_info, 2); @@ -644,31 +618,30 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError if (errno || prefix_int <= 0 || prefix_int > 32) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route destination prefix '%s'", prefix); + g_free (dest); g_free (prefix); goto error; } } - nm_ip4_route_set_prefix (route, (guint32) prefix_int); g_free (prefix); /* Next hop */ g_regex_match (regex_via, *iter, 0, &match_info); if (g_match_info_matches (match_info)) { - char *next_hop = g_match_info_fetch (match_info, 1); - if (inet_pton (AF_INET, next_hop, &ip4_addr) != 1) { + next_hop = g_match_info_fetch (match_info, 1); + if (!nm_utils_ipaddr_valid (AF_INET, next_hop)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route gateway address '%s'", next_hop); g_match_info_free (match_info); + g_free (dest); g_free (next_hop); goto error; } - g_free (next_hop); } else { /* we don't make distinction between missing GATEWAY IP and 0.0.0.0 */ - ip4_addr = 0; + next_hop = NULL; } - nm_ip4_route_set_next_hop (route, ip4_addr); g_match_info_free (match_info); /* Metric */ @@ -682,18 +655,23 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError g_match_info_free (match_info); g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route metric '%s'", metric); + g_free (dest); + g_free (next_hop); g_free (metric); goto error; } g_free (metric); } - - nm_ip4_route_set_metric (route, (guint32) metric_int); g_match_info_free (match_info); + route = nm_ip_route_new (AF_INET, dest, prefix_int, next_hop, metric_int, error); + if (!route) { + g_free (dest); + g_free (next_hop); + goto error; + } if (!nm_setting_ip4_config_add_route (s_ip4, route)) PARSE_WARNING ("duplicate IP4 route"); - } success = TRUE; @@ -701,7 +679,8 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError error: g_free (contents); g_strfreev (lines); - nm_ip4_route_unref (route); + if (route) + nm_ip_route_unref (route); g_regex_unref (regex_to1); g_regex_unref (regex_to2); g_regex_unref (regex_via); @@ -715,15 +694,13 @@ parse_full_ip6_address (shvarFile *ifcfg, const char *network_file, const char *addr_str, int i, - NMIP6Address **out_address, + NMIPAddress **out_address, GError **error) { - NMIP6Address *addr = NULL; char **list; - char *ip_val, *prefix_val; + char *ip_val, *prefix_val, *gateway_val = NULL; + long prefix; shvarFile *network_ifcfg; - char *value = NULL; - struct in6_addr tmp = IN6ADDR_ANY_INIT; gboolean success = FALSE; g_return_val_if_fail (addr_str != NULL, FALSE); @@ -741,23 +718,9 @@ parse_full_ip6_address (shvarFile *ifcfg, } ip_val = list[0]; - prefix_val = list[1]; - - addr = nm_ip6_address_new (); - /* IP address */ - if (!parse_ip6_address (ip_val, &tmp, error)) - goto error; - if (IN6_IS_ADDR_UNSPECIFIED (&tmp)) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IP6 address '%s'", ip_val); - goto error; - } - nm_ip6_address_set_address (addr, &tmp); - /* Prefix */ + prefix_val = list[1]; if (prefix_val) { - long int prefix; - errno = 0; prefix = strtol (prefix_val, NULL, 10); if (errno || prefix <= 0 || prefix > 128) { @@ -765,47 +728,38 @@ parse_full_ip6_address (shvarFile *ifcfg, "Invalid IP6 prefix '%s'", prefix_val); goto error; } - nm_ip6_address_set_prefix (addr, (guint32) prefix); } else { /* Missing prefix is treated as prefix of 64 */ - nm_ip6_address_set_prefix (addr, 64); + prefix = 64; } /* Gateway */ - tmp = in6addr_any; - value = svGetValue (ifcfg, "IPV6_DEFAULTGW", FALSE); - if (i != 0) { - /* We don't support gateways for IPV6ADDR_SECONDARIES yet */ - g_free (value); - value = NULL; - } - if (!value) { - /* If no gateway in the ifcfg, try global /etc/sysconfig/network instead */ - network_ifcfg = svOpenFile (network_file, NULL); - if (network_ifcfg) { - value = svGetValue (network_ifcfg, "IPV6_DEFAULTGW", FALSE); - svCloseFile (network_ifcfg); - } - } - if (value) { + if (i == 0) { char *ptr; - if ((ptr = strchr (value, '%')) != NULL) - *ptr = '\0'; /* remove %interface prefix if present */ - if (!parse_ip6_address (value, &tmp, error)) - goto error; - nm_ip6_address_set_gateway (addr, &tmp); - } + gateway_val = svGetValue (ifcfg, "IPV6_DEFAULTGW", FALSE); + if (!gateway_val) { + /* If no gateway in the ifcfg, try global /etc/sysconfig/network instead */ + network_ifcfg = svOpenFile (network_file, NULL); + if (network_ifcfg) { + gateway_val = svGetValue (network_ifcfg, "IPV6_DEFAULTGW", FALSE); + svCloseFile (network_ifcfg); + } + } - *out_address = addr; - success = TRUE; + if ( gateway_val + && (ptr = strchr (gateway_val, '%')) != NULL) + *ptr = '\0'; /* remove %interface suffix if present */ + } else + gateway_val = NULL; -error: - if (!success && addr) - nm_ip6_address_unref (addr); + *out_address = nm_ip_address_new (AF_INET6, ip_val, prefix, gateway_val, error); + if (*out_address) + success = TRUE; +error: g_strfreev (list); - g_free (value); + g_free (gateway_val); return success; } @@ -824,9 +778,8 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro char **lines = NULL, **iter; GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric; GMatchInfo *match_info; - NMIP6Route *route; - struct in6_addr ip6_addr; - char *dest = NULL, *prefix = NULL, *metric = NULL; + NMIPRoute *route = NULL; + char *dest = NULL, *prefix = NULL, *next_hop = NULL, *metric = NULL; long int prefix_int, metric_int; gboolean success = FALSE; @@ -855,9 +808,6 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro regex_via = g_regex_new (pattern_via, 0, 0, NULL); regex_metric = g_regex_new (pattern_metric, 0, 0, NULL); - /* New NMIP6Route structure */ - route = nm_ip6_route_new (); - /* Iterate through file lines */ lines = g_strsplit_set (contents, "\n\r", -1); for (iter = lines; iter && *iter; iter++) { @@ -881,19 +831,11 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro dest = g_match_info_fetch (match_info, 1); if (!g_strcmp0 (dest, "default")) { /* Ignore default route - NM handles it internally */ - g_free (dest); + g_clear_pointer (&dest, g_free); g_match_info_free (match_info); PARSE_WARNING ("ignoring manual default route: '%s' (%s)", *iter, filename); continue; } - if (inet_pton (AF_INET6, dest, &ip6_addr) != 1) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IP6 route destination address '%s'", dest); - g_free (dest); - goto error; - } - nm_ip6_route_set_dest (route, &ip6_addr); - g_free (dest); /* Prefix - is optional; 128 if missing */ prefix = g_match_info_fetch (match_info, 2); @@ -905,31 +847,30 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro if (errno || prefix_int <= 0 || prefix_int > 128) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP6 route destination prefix '%s'", prefix); + g_free (dest); g_free (prefix); goto error; } } - nm_ip6_route_set_prefix (route, (guint32) prefix_int); g_free (prefix); /* Next hop */ g_regex_match (regex_via, *iter, 0, &match_info); if (g_match_info_matches (match_info)) { - char *next_hop = g_match_info_fetch (match_info, 1); - if (inet_pton (AF_INET6, next_hop, &ip6_addr) != 1) { + next_hop = g_match_info_fetch (match_info, 1); + if (!nm_utils_ipaddr_valid (AF_INET6, next_hop)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IPv6 route nexthop address '%s'", next_hop); g_match_info_free (match_info); + g_free (dest); g_free (next_hop); goto error; } - g_free (next_hop); } else { /* Missing "via" is taken as :: */ - ip6_addr = in6addr_any; + next_hop = NULL; } - nm_ip6_route_set_next_hop (route, &ip6_addr); g_match_info_free (match_info); /* Metric */ @@ -943,15 +884,20 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro g_match_info_free (match_info); g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP6 route metric '%s'", metric); + g_free (dest); + g_free (next_hop); g_free (metric); goto error; } g_free (metric); } - - nm_ip6_route_set_metric (route, (guint32) metric_int); g_match_info_free (match_info); + route = nm_ip_route_new (AF_INET6, dest, prefix_int, next_hop, metric_int, error); + g_free (dest); + g_free (next_hop); + if (!route) + goto error; if (!nm_setting_ip6_config_add_route (s_ip6, route)) PARSE_WARNING ("duplicate IP6 route"); } @@ -961,7 +907,8 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro error: g_free (contents); g_strfreev (lines); - nm_ip6_route_unref (route); + if (route) + nm_ip_route_unref (route); g_regex_unref (regex_to1); g_regex_unref (regex_to2); g_regex_unref (regex_via); @@ -1082,16 +1029,12 @@ make_ip4_setting (shvarFile *ifcfg, * the legacy 'network' service (ifup-eth). */ for (i = -1; i < 256; i++) { - NMIP4Address *addr = NULL; + NMIPAddress *addr = NULL; - addr = nm_ip4_address_new (); - if (!read_full_ip4_address (ifcfg, network_file, i, addr, error)) { - nm_ip4_address_unref (addr); + if (!read_full_ip4_address (ifcfg, network_file, i, NULL, &addr, error)) goto done; - } - if (!nm_ip4_address_get_address (addr)) { - nm_ip4_address_unref (addr); + if (!addr) { /* The first mandatory variable is 2-indexed (IPADDR2) * Variables IPADDR, IPADDR0 and IPADDR1 are optional */ if (i > 1) @@ -1101,7 +1044,7 @@ make_ip4_setting (shvarFile *ifcfg, if (!nm_setting_ip4_config_add_address (s_ip4, addr)) PARSE_WARNING ("duplicate IP4 address"); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); } /* DNS servers @@ -1109,27 +1052,22 @@ make_ip4_setting (shvarFile *ifcfg, */ for (i = 1; i <= 10; i++) { char *tag; - guint32 dns; - struct in6_addr ip6_dns; tag = g_strdup_printf ("DNS%u", i); value = svGetValue (ifcfg, tag, FALSE); if (value) { - if (!read_ip4_address (ifcfg, tag, &dns, error)) { - gboolean valid = TRUE; - + if (nm_utils_ipaddr_valid (AF_INET, value)) { + if (!nm_setting_ip4_config_add_dns (s_ip4, value)) + PARSE_WARNING ("duplicate DNS server %s", tag); + } else if (nm_utils_ipaddr_valid (AF_INET6, value)) { /* Ignore IPv6 addresses */ - valid = parse_ip6_address (value, &ip6_dns, NULL); - if (!valid) { - g_free (tag); - goto done; - } - g_clear_error (error); - dns = 0; + } else { + PARSE_WARNING ("invalid DNS server address %s", value); + g_free (tag); + g_free (value); + goto done; } - if (dns && !nm_setting_ip4_config_add_dns (s_ip4, value)) - PARSE_WARNING ("duplicate DNS server %s", tag); g_free (value); } @@ -1169,7 +1107,7 @@ make_ip4_setting (shvarFile *ifcfg, route_ifcfg = utils_get_route_ifcfg (ifcfg->fileName, FALSE); if (route_ifcfg) { for (i = 0; i < 256; i++) { - NMIP4Route *route = NULL; + NMIPRoute *route = NULL; if (!read_one_ip4_route (route_ifcfg, network_file, i, &route, error)) { svCloseFile (route_ifcfg); @@ -1181,7 +1119,7 @@ make_ip4_setting (shvarFile *ifcfg, if (!nm_setting_ip4_config_add_route (s_ip4, route)) PARSE_WARNING ("duplicate IP4 route"); - nm_ip4_route_unref (route); + nm_ip_route_unref (route); } svCloseFile (route_ifcfg); } @@ -1225,7 +1163,7 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo GDir *dir; char *dirname, *base; shvarFile *parsed; - NMIP4Address *base_addr; + NMIPAddress *base_addr; GError *err = NULL; g_return_if_fail (s_ip4 != NULL); @@ -1244,7 +1182,7 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo dir = g_dir_open (dirname, 0, &err); if (dir) { const char *item; - NMIP4Address *addr; + NMIPAddress *addr; gboolean ok; while ((item = g_dir_read_name (dir))) { @@ -1293,8 +1231,8 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo continue; } - addr = nm_ip4_address_dup (base_addr); - ok = read_full_ip4_address (parsed, network_file, -1, addr, &err); + addr = NULL; + ok = read_full_ip4_address (parsed, network_file, -1, base_addr, &addr, &err); svCloseFile (parsed); if (ok) { if (!_nm_setting_ip4_config_add_address_with_label (s_ip4, addr, device)) @@ -1304,7 +1242,7 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo full_path, err ? err->message : "no address"); g_clear_error (&err); } - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); g_free (device); g_free (full_path); @@ -1476,7 +1414,7 @@ make_ip6_setting (shvarFile *ifcfg, list = g_strsplit_set (value, " ", 0); g_free (value); for (iter = list, i = 0; iter && *iter; iter++, i++) { - NMIP6Address *addr = NULL; + NMIPAddress *addr = NULL; if (!parse_full_ip6_address (ifcfg, network_file, *iter, i, &addr, error)) { g_strfreev (list); @@ -1485,7 +1423,7 @@ make_ip6_setting (shvarFile *ifcfg, if (!nm_setting_ip6_config_add_address (s_ip6, addr)) PARSE_WARNING ("duplicate IP6 address"); - nm_ip6_address_unref (addr); + nm_ip_address_unref (addr); } g_strfreev (list); @@ -1494,8 +1432,6 @@ make_ip6_setting (shvarFile *ifcfg, */ for (i = 1; i <= 10; i++) { char *tag; - struct in6_addr ip6_dns; - guint32 ip4_addr; tag = g_strdup_printf ("DNS%u", i); value = svGetValue (ifcfg, tag, FALSE); @@ -1504,18 +1440,16 @@ make_ip6_setting (shvarFile *ifcfg, break; /* all done */ } - ip6_dns = in6addr_any; - if (parse_ip6_address (value, &ip6_dns, NULL)) { - if (!IN6_IS_ADDR_UNSPECIFIED (&ip6_dns) && !nm_setting_ip6_config_add_dns (s_ip6, value)) + if (nm_utils_ipaddr_valid (AF_INET6, value)) { + if (!nm_setting_ip6_config_add_dns (s_ip6, value)) PARSE_WARNING ("duplicate DNS server %s", tag); + } else if (nm_utils_ipaddr_valid (AF_INET, value)) { + /* Ignore IPv4 addresses */ } else { - /* Maybe an IPv4 address? If so ignore it */ - if (inet_pton (AF_INET, value, &ip4_addr) != 1) { - g_free (tag); - g_free (value); - PARSE_WARNING ("duplicate IP6 address"); - goto error; - } + PARSE_WARNING ("invalid DNS server address %s", value); + g_free (tag); + g_free (value); + goto error; } g_free (tag); diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index b2d91e46f6..0162cb130e 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -446,11 +446,8 @@ test_read_wired_static (const char *file, GError *error = NULL; const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0xee }; - struct in6_addr addr6; - const char *expected6_address1 = "dead:beaf::1"; - const char *expected6_address2 = "dead:beaf::2"; - NMIP4Address *ip4_addr; - NMIP6Address *ip6_addr; + NMIPAddress *ip4_addr; + NMIPAddress *ip6_addr; gboolean success; connection = connection_from_file (file, NULL, TYPE_ETHERNET, @@ -494,9 +491,9 @@ test_read_wired_static (const char *file, g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.1.5"); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_gateway (ip4_addr), "192.168.1.1"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.1"); /* ===== IPv6 SETTING ===== */ s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -515,15 +512,13 @@ test_read_wired_static (const char *file, ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 64); - g_assert_cmpint (inet_pton (AF_INET6, expected6_address1, &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "dead:beaf::1"); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 56); - g_assert_cmpint (inet_pton (AF_INET6, expected6_address2, &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "dead:beaf::2"); } else { g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); } @@ -540,7 +535,7 @@ test_read_wired_static_no_prefix (gconstpointer user_data) NMSettingConnection *s_con; NMSettingIP4Config *s_ip4; GError *error = NULL; - NMIP4Address *ip4_addr; + NMIPAddress *ip4_addr; char *file, *expected_id; file = g_strdup_printf (TEST_IFCFG_DIR "/network-scripts/ifcfg-test-wired-static-no-prefix-%u", expected_prefix); @@ -568,7 +563,7 @@ test_read_wired_static_no_prefix (gconstpointer user_data) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, expected_prefix); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, expected_prefix); g_free (file); g_free (expected_id); @@ -737,10 +732,8 @@ test_read_wired_dhcp_plus_ip (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; GError *error = NULL; - guint32 addr4; - struct in6_addr addr6; - NMIP4Address *ip4_addr; - NMIP6Address *ip6_addr; + NMIPAddress *ip4_addr; + NMIPAddress *ip6_addr; gboolean success; connection = connection_from_file (TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-dhcp-plus-ip", @@ -767,17 +760,14 @@ test_read_wired_dhcp_plus_ip (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); - g_assert_cmpint (inet_pton (AF_INET, "1.2.3.4", &addr4), >, 0); - g_assert_cmpint (nm_ip4_address_get_address (ip4_addr), ==, addr4); - g_assert_cmpint (inet_pton (AF_INET, "1.1.1.1", &addr4), >, 0); - g_assert_cmpint (nm_ip4_address_get_gateway (ip4_addr), ==, addr4); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4"); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "1.1.1.1"); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 1); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 16); - g_assert_cmpint (inet_pton (AF_INET, "9.8.7.6", &addr4), >, 0); - g_assert_cmpint (nm_ip4_address_get_address (ip4_addr), ==, addr4); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 16); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "9.8.7.6"); /* ===== IPv6 SETTING ===== */ s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -794,21 +784,18 @@ test_read_wired_dhcp_plus_ip (void) g_assert_cmpint (nm_setting_ip6_config_get_num_addresses (s_ip6), ==, 3); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 56); - g_assert_cmpint (inet_pton (AF_INET6, "1001:abba::1234", &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 64); - g_assert_cmpint (inet_pton (AF_INET6, "2001:abba::2234", &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "2001:abba::2234"); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 2); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 96); - g_assert_cmpint (inet_pton (AF_INET6, "3001:abba::3234", &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 96); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "3001:abba::3234"); g_object_unref (connection); } @@ -821,7 +808,7 @@ test_read_wired_global_gateway (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; GError *error = NULL; - NMIP4Address *ip4_addr; + NMIPAddress *ip4_addr; char *unmanaged = NULL; connection = connection_from_file (TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-global-gateway", @@ -847,9 +834,9 @@ test_read_wired_global_gateway (void) /* Address #1 */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.1.5"); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_gateway (ip4_addr), "192.168.1.2"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.2"); g_object_unref (connection); } @@ -1137,7 +1124,7 @@ test_read_wired_static_routes (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; GError *error = NULL; - NMIP4Route *ip4_route; + NMIPRoute *ip4_route; connection = connection_from_file (TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-static-routes", NULL, TYPE_ETHERNET, NULL, NULL, NULL, NULL, &error, NULL); @@ -1162,17 +1149,17 @@ test_read_wired_static_routes (void) ip4_route = nm_setting_ip4_config_get_route (s_ip4, 0); g_assert (ip4_route); - nmtst_assert_ip4_address_equals (nm_ip4_route_get_dest (ip4_route), "11.22.33.0"); - g_assert_cmpint (nm_ip4_route_get_prefix (ip4_route), ==, 24); - nmtst_assert_ip4_address_equals (nm_ip4_route_get_next_hop (ip4_route), "192.168.1.5"); - g_assert_cmpint (nm_ip4_route_get_metric (ip4_route), ==, 0); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "11.22.33.0"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 24); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "192.168.1.5"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 0); ip4_route = nm_setting_ip4_config_get_route (s_ip4, 1); g_assert (ip4_route); - nmtst_assert_ip4_address_equals (nm_ip4_route_get_dest (ip4_route), "44.55.66.77"); - g_assert_cmpint (nm_ip4_route_get_prefix (ip4_route), ==, 32); - nmtst_assert_ip4_address_equals (nm_ip4_route_get_next_hop (ip4_route), "192.168.1.7"); - g_assert_cmpint (nm_ip4_route_get_metric (ip4_route), ==, 3); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "44.55.66.77"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 32); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "192.168.1.7"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 3); g_object_unref (connection); } @@ -1193,15 +1180,8 @@ test_read_wired_static_routes_legacy (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - NMIP4Route *ip4_route; - guint32 addr; + NMIPRoute *ip4_route; const char *expected_id = "System test-wired-static-routes-legacy"; - const char *expected_dst1 = "21.31.41.0"; - const char *expected_dst2 = "32.42.52.62"; - const char *expected_dst3 = "43.53.0.0"; - const char *expected_gw1 = "9.9.9.9"; - const char *expected_gw2 = "8.8.8.8"; - const char *expected_gw3 = "7.7.7.7"; connection = connection_from_file (TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, NULL, @@ -1281,99 +1261,27 @@ test_read_wired_static_routes_legacy (void) /* Route #1 */ ip4_route = nm_setting_ip4_config_get_route (s_ip4, 0); - ASSERT (ip4_route, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: missing IP4 route #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_dst1, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert destination IP address #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_dest (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_prefix (ip4_route) == 24, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #1 prefix", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_gw1, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert next hop IP address #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_next_hop (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_metric (ip4_route) == 1, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #1 metric", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); + g_assert (ip4_route != NULL); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "21.31.41.0"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 24); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "9.9.9.9"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 1); /* Route #2 */ ip4_route = nm_setting_ip4_config_get_route (s_ip4, 1); - ASSERT (ip4_route, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: missing IP4 route #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_dst2, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert destination IP address #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_dest (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_prefix (ip4_route) == 32, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #2 prefix", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_gw2, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert next hop IP address #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_next_hop (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_metric (ip4_route) == 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #2 metric", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); + g_assert (ip4_route != NULL); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "32.42.52.62"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 32); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "8.8.8.8"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 0); /* Route #3 */ ip4_route = nm_setting_ip4_config_get_route (s_ip4, 2); - ASSERT (ip4_route, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: missing IP4 route #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_dst3, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert destination IP address #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_dest (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_prefix (ip4_route) == 16, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #3 prefix", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_gw3, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert next hop IP address #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_next_hop (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_metric (ip4_route) == 3, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #3 metric", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); + g_assert (ip4_route != NULL); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "43.53.0.0"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 16); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "7.7.7.7"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 3); g_free (unmanaged); g_free (keyfile); @@ -1396,14 +1304,7 @@ test_read_wired_ipv4_manual (const char *file, const char *expected_id) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const char *expected_address1 = "1.2.3.4"; - const char *expected_address2 = "9.8.7.6"; - const char *expected_address3 = "3.3.3.3"; - guint32 expected_prefix1 = 24; - guint32 expected_prefix2 = 16; - guint32 expected_prefix3 = 8; - NMIP4Address *ip4_addr; - guint32 addr; + NMIPAddress *ip4_addr; connection = connection_from_file (file, NULL, @@ -1477,54 +1378,21 @@ test_read_wired_ipv4_manual (const char *file, const char *expected_id) /* Address #1 */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - ASSERT (ip4_addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: missing IP4 address #1", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix1, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #1 prefix", - file); - - ASSERT (inet_pton (AF_INET, expected_address1, &addr) > 0, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: couldn't convert IP address #1", - file); - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #1", - file); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); /* Address #2 */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 1); - ASSERT (ip4_addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: missing IP4 address #2", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix2, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #2 prefix", - file); - - ASSERT (inet_pton (AF_INET, expected_address2, &addr) > 0, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: couldn't convert IP address #2", - file); - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #2", - file); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "9.8.7.6"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 16); /* Address #3 */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 2); - ASSERT (ip4_addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: missing IP4 address #3", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix3, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #3 prefix", - file); - - ASSERT (inet_pton (AF_INET, expected_address3, &addr) > 0, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: couldn't convert IP address #3", - file); - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #3", - file); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "3.3.3.3"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); g_free (unmanaged); g_free (keyfile); @@ -1551,15 +1419,8 @@ test_read_wired_ipv6_manual (void) GError *error = NULL; const char *tmp; const char *expected_id = "System test-wired-ipv6-manual"; - const char *expected_address1 = "1001:abba::1234"; - const char *expected_address2 = "2001:abba::2234"; - const char *expected_address3 = "3001:abba::3234"; - guint32 expected_prefix1 = 56; - guint32 expected_prefix2 = 64; - guint32 expected_prefix3 = 96; - NMIP6Address *ip6_addr; - NMIP6Route *ip6_route; - struct in6_addr addr; + NMIPAddress *ip6_addr; + NMIPRoute *ip6_route; g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, "*ignoring manual default route*"); @@ -1707,75 +1568,38 @@ test_read_wired_ipv6_manual (void) /* Address #1 */ ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); - ASSERT (ip6_addr, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: missing IP6 address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix1, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #1 prefix", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (inet_pton (AF_INET6, expected_address1, &addr) > 0, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: couldn't convert IP address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr), - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); /* Address #2 */ ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); - ASSERT (ip6_addr, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: missing IP6 address #2", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix2, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #2 prefix", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (inet_pton (AF_INET6, expected_address2, &addr) > 0, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: couldn't convert IP address #2", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr), - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #2", - TEST_IFCFG_WIRED_IPV6_MANUAL); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "2001:abba::2234"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); /* Address #3 */ ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 2); - ASSERT (ip6_addr, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: missing IP6 address #3", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix3, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #3 prefix", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (inet_pton (AF_INET6, expected_address3, &addr) > 0, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: couldn't convert IP address #3", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr), - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #3", - TEST_IFCFG_WIRED_IPV6_MANUAL); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "3001:abba::3234"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 96); /* Routes */ g_assert_cmpint (nm_setting_ip6_config_get_num_routes (s_ip6), ==, 2); /* Route #1 */ ip6_route = nm_setting_ip6_config_get_route (s_ip6, 0); g_assert (ip6_route); - g_assert_cmpint (inet_pton (AF_INET6, "9876::1234", &addr), >, 0); - g_assert_cmpint (memcmp (nm_ip6_route_get_dest (ip6_route), &addr, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpint (nm_ip6_route_get_prefix (ip6_route), ==, 96); - g_assert_cmpint (inet_pton (AF_INET6, "9876::7777", &addr), >, 0); - g_assert_cmpint (memcmp (nm_ip6_route_get_next_hop (ip6_route), &addr, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpint (nm_ip6_route_get_metric (ip6_route), ==, 2); + g_assert_cmpstr (nm_ip_route_get_dest (ip6_route), ==, "9876::1234"); + g_assert_cmpint (nm_ip_route_get_prefix (ip6_route), ==, 96); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip6_route), ==, "9876::7777"); + g_assert_cmpint (nm_ip_route_get_metric (ip6_route), ==, 2); /* Route #2 */ ip6_route = nm_setting_ip6_config_get_route (s_ip6, 1); g_assert (ip6_route); - g_assert_cmpint (inet_pton (AF_INET6, "abbe::cafe", &addr), >, 0); - g_assert_cmpint (memcmp (nm_ip6_route_get_dest (ip6_route), &addr, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpint (nm_ip6_route_get_prefix (ip6_route), ==, 64); - g_assert_cmpint (inet_pton (AF_INET6, "::", &addr), >, 0); - g_assert_cmpint (memcmp (nm_ip6_route_get_next_hop (ip6_route), &addr, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpint (nm_ip6_route_get_metric (ip6_route), ==, 777); + g_assert_cmpstr (nm_ip_route_get_dest (ip6_route), ==, "abbe::cafe"); + g_assert_cmpint (nm_ip_route_get_prefix (ip6_route), ==, 64); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip6_route), ==, NULL); + g_assert_cmpint (nm_ip_route_get_metric (ip6_route), ==, 777); /* DNS Addresses */ ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 2, @@ -1828,10 +1652,7 @@ test_read_wired_ipv6_only (void) GError *error = NULL; const char *tmp; const char *expected_id = "System test-wired-ipv6-only"; - const char *expected_address1 = "1001:abba::1234"; - guint32 expected_prefix1 = 56; - NMIP6Address *ip6_addr; - struct in6_addr addr; + NMIPAddress *ip6_addr; const char *method; connection = connection_from_file (TEST_IFCFG_WIRED_IPV6_ONLY, @@ -1921,20 +1742,9 @@ test_read_wired_ipv6_only (void) /* Address #1 */ ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); - ASSERT (ip6_addr, - "wired-ipv6-only-verify-ip6", "failed to verify %s: missing IP6 address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix1, - "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected IP6 address #1 prefix", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (inet_pton (AF_INET6, expected_address1, &addr) > 0, - "wired-ipv6-only-verify-ip6", "failed to verify %s: couldn't convert IP address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr), - "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected IP6 address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); /* DNS Addresses */ ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 1, @@ -2564,7 +2374,7 @@ test_read_wired_aliases_good (void) GError *error = NULL; const char *tmp; const char *expected_id = "System aliasem0"; - int expected_num_addresses = 4, expected_prefix = 24; + int expected_num_addresses = 4; const char *expected_address[4] = { "192.168.1.5", "192.168.1.6", "192.168.1.9", "192.168.1.99" }; const char *expected_label[4] = { "", "aliasem0:1", "aliasem0:2", "aliasem0:99" }; const char *expected_gateway[4] = { "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1" }; @@ -2630,53 +2440,24 @@ test_read_wired_aliases_good (void) /* Addresses */ for (i = 0; i < expected_num_addresses; i++) { - NMIP4Address *ip4_addr; - char buf[INET_ADDRSTRLEN]; - struct in_addr addr; + NMIPAddress *ip4_addr; + const char *addr; ip4_addr = nm_setting_ip4_config_get_address (s_ip4, i); - ASSERT (ip4_addr, - "aliases-good-verify-ip4", "failed to verify %s: missing IP4 address #%d", - TEST_IFCFG_ALIASES_GOOD, - i); + g_assert (ip4_addr != NULL); - addr.s_addr = nm_ip4_address_get_address (ip4_addr); - ASSERT (inet_ntop (AF_INET, &addr, buf, sizeof (buf)) > 0, - "aliases-good-verify-ip4", "failed to verify %s: couldn't convert IP address #%d", - TEST_IFCFG_ALIASES_GOOD, - i); + addr = nm_ip_address_get_address (ip4_addr); + g_assert (nm_utils_ipaddr_valid (AF_INET, addr)); for (j = 0; j < expected_num_addresses; j++) { - if (!g_strcmp0 (buf, expected_address[j])) + if (!g_strcmp0 (addr, expected_address[j])) break; } + g_assert (j < expected_num_addresses); - ASSERT (j < expected_num_addresses, - "aliases-good-verify-ip4", "failed to verify %s: unexpected IP4 address #%d", - TEST_IFCFG_ALIASES_GOOD, - i); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix, - "aliases-good-verify-ip4", "failed to verify %s: unexpected IP4 address prefix #%d", - TEST_IFCFG_ALIASES_GOOD, - i); - - if (expected_gateway[j]) { - ASSERT (inet_pton (AF_INET, expected_gateway[j], &addr) > 0, - "aliases-good-verify-ip4", "failed to verify %s: couldn't convert IP address gateway #%d", - TEST_IFCFG_ALIASES_GOOD, - i); - } else - addr.s_addr = 0; - ASSERT (nm_ip4_address_get_gateway (ip4_addr) == addr.s_addr, - "aliases-good-verify-ip4", "failed to verify %s: unexpected IP4 address gateway #%d", - TEST_IFCFG_ALIASES_GOOD, - i); - - ASSERT (g_strcmp0 (_nm_setting_ip4_config_get_address_label (s_ip4, i), expected_label[j]) == 0, - "aliases-good-verify-ip4", "failed to verify %s: unexpected IP4 address label #%d", - TEST_IFCFG_ALIASES_GOOD, - i); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, expected_gateway[j]); + g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, i), ==, expected_label[j]); expected_address[j] = NULL; expected_gateway[j] = NULL; @@ -2709,12 +2490,7 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - int expected_num_addresses = 1, expected_prefix = 24; - const char *expected_address = "192.168.1.5"; - const char *expected_label = ""; - const char *expected_gateway = "192.168.1.1"; - NMIP4Address *ip4_addr; - struct in_addr addr; + NMIPAddress *ip4_addr; connection = connection_from_file (base, NULL, @@ -2769,7 +2545,7 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_METHOD); - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == expected_num_addresses, + ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 1, "aliases-bad-verify-ip4", "failed to verify %s: unexpected %s / %s key value", base, NM_SETTING_IP4_CONFIG_SETTING_NAME, @@ -2777,31 +2553,11 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) /* Addresses */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - ASSERT (ip4_addr, - "aliases-bad-verify-ip4", "failed to verify %s: missing IP4 address", - base); - - ASSERT (inet_pton (AF_INET, expected_address, &addr) > 0, - "aliases-bad-verify-ip4", "failed to verify %s: couldn't convert IP address", - base); - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr.s_addr, - "aliases-bad-verify-ip4", "failed to verify %s: unexpected IP4 address", - base); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix, - "aliases-bad-verify-ip4", "failed to verify %s: unexpected IP4 address prefix", - base); - - ASSERT (inet_pton (AF_INET, expected_gateway, &addr) > 0, - "aliases-bad-verify-ip4", "failed to verify %s: couldn't convert IP address gateway", - base); - ASSERT (nm_ip4_address_get_gateway (ip4_addr) == addr.s_addr, - "aliases-bad-verify-ip4", "failed to verify %s: unexpected IP4 address gateway", - base); - - ASSERT (g_strcmp0 (_nm_setting_ip4_config_get_address_label (s_ip4, 0), expected_label) == 0, - "aliases-bad-verify-ip4", "failed to verify %s: unexpected IP4 address label", - base); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.1"); + g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, 0), ==, ""); g_free (keyfile); g_free (routefile); @@ -6470,25 +6226,17 @@ test_write_wired_static (void) static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 ip2 = htonl (0x01010105); - const guint32 gw = htonl (0x01010101); const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; - const guint32 prefix = 24; const char *dns_search1 = "foobar.com"; const char *dns_search2 = "lab.foobar.com"; const char *dns_search3 = "foobar6.com"; const char *dns_search4 = "lab6.foobar.com"; - struct in6_addr ip6, ip6_1, ip6_2; - struct in6_addr route1_dest, route2_dest, route1_nexthop, route2_nexthop; const char *dns6_1 = "fade:0102:0103::face"; const char *dns6_2 = "cafe:ffff:eeee:dddd:cccc:bbbb:aaaa:feed"; - const guint32 route1_prefix = 64, route2_prefix = 128; - const guint32 route1_metric = 99, route2_metric = 1; - NMIP4Address *addr; - NMIP6Address *addr6; - NMIP6Route *route6; + NMIPAddress *addr; + NMIPAddress *addr6; + NMIPRoute *route6; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -6498,14 +6246,6 @@ test_write_wired_static (void) char *route6file = NULL; gboolean ignore_error = FALSE; - inet_pton (AF_INET6, "1003:1234:abcd::1", &ip6); - inet_pton (AF_INET6, "2003:1234:abcd::2", &ip6_1); - inet_pton (AF_INET6, "3003:1234:abcd::3", &ip6_2); - inet_pton (AF_INET6, "2222:aaaa:bbbb:cccc::", &route1_dest); - inet_pton (AF_INET6, "2222:aaaa:bbbb:cccc:dddd:eeee:5555:6666", &route1_nexthop); - inet_pton (AF_INET6, "::", &route2_dest); - inet_pton (AF_INET6, "2222:aaaa::9999", &route2_nexthop); - connection = nm_simple_connection_new (); /* Connection setting */ @@ -6539,19 +6279,15 @@ test_write_wired_static (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip2); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_ip4_config_add_dns (s_ip4, dns1); nm_setting_ip4_config_add_dns (s_ip4, dns2); @@ -6569,40 +6305,33 @@ test_write_wired_static (void) NULL); /* Add addresses */ - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6); - nm_ip6_address_set_prefix (addr6, 11); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, NULL, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6_1); - nm_ip6_address_set_prefix (addr6, 22); + addr6 = nm_ip_address_new (AF_INET6, "2003:1234:abcd::2", 22, NULL, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6_2); - nm_ip6_address_set_prefix (addr6, 33); + addr6 = nm_ip_address_new (AF_INET6, "3003:1234:abcd::3", 33, NULL, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); /* Add routes */ - route6 = nm_ip6_route_new (); - nm_ip6_route_set_dest (route6, &route1_dest); - nm_ip6_route_set_prefix (route6, route1_prefix); - nm_ip6_route_set_next_hop (route6, &route1_nexthop); - nm_ip6_route_set_metric (route6, route1_metric); + route6 = nm_ip_route_new (AF_INET6, + "2222:aaaa:bbbb:cccc::", 64, + "2222:aaaa:bbbb:cccc:dddd:eeee:5555:6666", 99, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_route (s_ip6, route6); - nm_ip6_route_unref (route6); + nm_ip_route_unref (route6); - route6 = nm_ip6_route_new (); - nm_ip6_route_set_dest (route6, &route2_dest); - nm_ip6_route_set_prefix (route6, route2_prefix); - nm_ip6_route_set_next_hop (route6, &route2_nexthop); - nm_ip6_route_set_metric (route6, route2_metric); + route6 = nm_ip_route_new (AF_INET6, "::", 128, "2222:aaaa::9999", 1, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_route (s_ip6, route6); - nm_ip6_route_unref (route6); + nm_ip_route_unref (route6); /* DNS servers */ nm_setting_ip6_config_add_dns (s_ip6, dns6_1); @@ -6906,9 +6635,8 @@ test_write_wired_static_ip6_only (void) NMSettingIP6Config *s_ip6; static const char *mac = "31:33:33:37:be:cd"; char *uuid; - struct in6_addr ip6; const char *dns6 = "fade:0102:0103::face"; - NMIP6Address *addr6; + NMIPAddress *addr6; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -6918,8 +6646,6 @@ test_write_wired_static_ip6_only (void) char *route6file = NULL; gboolean ignore_error = FALSE; - inet_pton (AF_INET6, "1003:1234:abcd::1", &ip6); - connection = nm_simple_connection_new (); /* Connection setting */ @@ -6958,11 +6684,10 @@ test_write_wired_static_ip6_only (void) NULL); /* Add addresses */ - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6); - nm_ip6_address_set_prefix (addr6, 11); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, NULL, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); /* DNS server */ nm_setting_ip6_config_add_dns (s_ip6, dns6); @@ -7038,29 +6763,15 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) NMSettingIP6Config *s_ip6; static const char *mac = "31:33:33:37:be:cd"; char *uuid; - struct in6_addr ip6; const char *dns6 = "fade:0102:0103::face"; - NMIP6Address *addr6; + NMIPAddress *addr6; gboolean success; GError *error = NULL; char *testfile = NULL; char *id = NULL; gboolean ignore_error = FALSE; char *written_ifcfg_gateway; - char s_gateway6[INET6_ADDRSTRLEN] = { 0 }; - struct in6_addr gateway6_autovar; - const struct in6_addr *gateway6 = NULL; - - /* parsing the input argument and set the struct in6_addr "gateway6" to - * the gateway address. NULL means "do not set the gateway explicitly". */ - if (user_data) { - g_assert_cmpint (inet_pton (AF_INET6, user_data, &gateway6_autovar), ==, 1); - gateway6 = &gateway6_autovar; - } - - inet_pton (AF_INET6, "1003:1234:abcd::1", &ip6); - if (gateway6) - inet_ntop (AF_INET6, gateway6, s_gateway6, sizeof (s_gateway6)); + const char *gateway6 = user_data; connection = nm_simple_connection_new (); @@ -7069,7 +6780,7 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) nm_connection_add_setting (connection, NM_SETTING (s_con)); uuid = nm_utils_uuid_generate (); - id = g_strdup_printf ("Test Write Wired Static IP6 Only With Gateway %s", gateway6 ? s_gateway6 : "NULL"); + id = g_strdup_printf ("Test Write Wired Static IP6 Only With Gateway %s", gateway6 ? gateway6 : "NULL"); g_object_set (s_con, NM_SETTING_CONNECTION_ID, id, NM_SETTING_CONNECTION_UUID, uuid, @@ -7102,13 +6813,10 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) NULL); /* Add addresses */ - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6); - nm_ip6_address_set_prefix (addr6, 11); - if (gateway6) - nm_ip6_address_set_gateway (addr6, gateway6); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, gateway6, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); /* DNS server */ nm_setting_ip6_config_add_dns (s_ip6, dns6); @@ -7159,13 +6867,13 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) g_assert (addr6); /* assert that the gateway was written and reloaded as expected */ - if (!gateway6 || IN6_IS_ADDR_UNSPECIFIED (gateway6)) { - g_assert (IN6_IS_ADDR_UNSPECIFIED (nm_ip6_address_get_gateway (addr6))); - g_assert (written_ifcfg_gateway==NULL); + if (!gateway6 || !strcmp (gateway6, "::")) { + g_assert (nm_ip_address_get_gateway (addr6) == NULL); + g_assert (written_ifcfg_gateway == NULL); } else { - g_assert (!IN6_IS_ADDR_UNSPECIFIED (nm_ip6_address_get_gateway (addr6))); - g_assert_cmpint (memcmp (nm_ip6_address_get_gateway (addr6), gateway6, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpstr (written_ifcfg_gateway, ==, s_gateway6); + g_assert (nm_ip_address_get_gateway (addr6) != NULL); + g_assert_cmpstr (nm_ip_address_get_gateway (addr6), ==, gateway6); + g_assert_cmpstr (written_ifcfg_gateway, ==, gateway6); } g_free (testfile); @@ -7334,20 +7042,12 @@ test_write_wired_static_routes (void) static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 ip2 = htonl (0x01010105); - const guint32 gw = htonl (0x01010101); const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; - const guint32 route_dst1 = htonl (0x01020300); - const guint32 route_dst2= htonl (0x03020100); - const guint32 route_gw1 = htonl (0xdeadbeef); - const guint32 route_gw2 = htonl (0xcafeabbe); - const guint32 prefix = 24; const char *dns_search1 = "foobar.com"; const char *dns_search2 = "lab.foobar.com"; - NMIP4Address *addr; - NMIP4Route *route; + NMIPAddress *addr; + NMIPRoute *route; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -7389,35 +7089,26 @@ test_write_wired_static_routes (void) NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip2); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); /* Write out routes */ - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, route_dst1); - nm_ip4_route_set_prefix (route, prefix); - nm_ip4_route_set_next_hop (route, route_gw1); + route = nm_ip_route_new (AF_INET, "1.2.3.0", 24, "222.173.190.239", 0, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_route (s_ip4, route); - nm_ip4_route_unref (route); + nm_ip_route_unref (route); - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, route_dst2); - nm_ip4_route_set_prefix (route, prefix); - nm_ip4_route_set_next_hop (route, route_gw2); - nm_ip4_route_set_metric (route, 77); + route = nm_ip_route_new (AF_INET, "3.2.1.0", 24, "202.254.186.190", 77, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_route (s_ip4, route); - nm_ip4_route_unref (route); + nm_ip_route_unref (route); nm_setting_ip4_config_add_dns (s_ip4, dns1); nm_setting_ip4_config_add_dns (s_ip4, dns2); @@ -7865,11 +7556,9 @@ test_write_wired_aliases (void) NMSettingIP4Config *s_ip4; char *uuid; int num_addresses = 4; - guint32 ip[] = { 0x01010101, 0x01010102, 0x01010103, 0x01010104 }; + const char *ip[] = { "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" }; const char *label[] = { "", "alias0:2", "", "alias0:3" }; - const guint32 gw = htonl (0x01010101); - const guint32 prefix = 24; - NMIP4Address *addr; + NMIPAddress *addr; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -7920,12 +7609,10 @@ test_write_wired_aliases (void) NULL); for (i = 0; i < num_addresses; i++) { - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip[i]); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, ip[i], 24, "1.1.1.1", &error); + g_assert_no_error (error); _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, label[i]); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); } ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8002,41 +7689,23 @@ test_write_wired_aliases (void) /* Addresses */ for (i = 0; i < num_addresses; i++) { - guint32 addrbytes; + const char *addrstr; addr = nm_setting_ip4_config_get_address (s_ip4, i); - ASSERT (addr, - "wired-aliases-write-verify-ip4", "failed to verify %s: missing IP4 address #%d", - testfile, - i); + g_assert (addr != NULL); - addrbytes = nm_ip4_address_get_address (addr); + addrstr = nm_ip_address_get_address (addr); for (j = 0; j < num_addresses; j++) { - if (addrbytes == ip[j]) + if (!g_strcmp0 (addrstr, ip[j])) break; } + g_assert (j < num_addresses); - ASSERT (j < num_addresses, - "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected IP4 address #%d", - testfile, - i); - - ASSERT (nm_ip4_address_get_prefix (addr) == prefix, - "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected IP4 address prefix #%d", - testfile, - i); + g_assert_cmpint (nm_ip_address_get_prefix (addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_gateway (addr), ==, "1.1.1.1"); + g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, i), ==, label[j]); - ASSERT (nm_ip4_address_get_gateway (addr) == gw, - "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected IP4 address gateway #%d", - testfile, - i); - - ASSERT (g_strcmp0 (_nm_setting_ip4_config_get_address_label (s_ip4, i), label[j]) == 0, - "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected IP4 address label #%d", - testfile, - i); - - ip[j] = 0; + ip[j] = NULL; } for (i = 0; i < num_addresses; i++) { @@ -8065,13 +7734,7 @@ test_write_gateway (void) gboolean success; GError *error = NULL; shvarFile *f; - NMIP4Address *addr; - const char *ip1_str = "1.1.1.3"; - const char *ip2_str = "2.2.2.5"; - const char *gw1_str = "1.1.1.254"; - const char *gw2_str = "2.2.2.254"; - struct in_addr ip1, ip2, gw1, gw2; - const guint32 prefix = 24; + NMIPAddress *addr; connection = nm_simple_connection_new (); @@ -8100,24 +7763,15 @@ test_write_gateway (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - inet_pton (AF_INET, ip1_str, &ip1); - inet_pton (AF_INET, ip2_str, &ip2); - inet_pton (AF_INET, gw1_str, &gw1); - inet_pton (AF_INET, gw2_str, &gw2); - - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1.s_addr); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw1.s_addr); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.254", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip2.s_addr); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw2.s_addr); + addr = nm_ip_address_new (AF_INET, "2.2.2.5", 24, "2.2.2.254", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); success = nm_connection_verify (connection, &error); g_assert_no_error (error); @@ -8138,12 +7792,12 @@ test_write_gateway (void) /* re-read the file to check that the keys was written as IPADDR, GATEWAY and IPADDR1, GATEWAY1 */ val = svGetValue (f, "IPADDR", FALSE); g_assert (val); - g_assert_cmpstr (val, ==, ip1_str); + g_assert_cmpstr (val, ==, "1.1.1.3"); g_free (val); val = svGetValue (f, "IPADDR1", FALSE); g_assert (val); - g_assert_cmpstr (val, ==, ip2_str); + g_assert_cmpstr (val, ==, "2.2.2.5"); g_free (val); val = svGetValue (f, "IPADDR0", FALSE); @@ -8164,12 +7818,12 @@ test_write_gateway (void) val = svGetValue (f, "GATEWAY", FALSE); g_assert (val); - g_assert_cmpstr (val, ==, gw1_str); + g_assert_cmpstr (val, ==, "1.1.1.254"); g_free (val); val = svGetValue (f, "GATEWAY1", FALSE); g_assert (val); - g_assert_cmpstr (val, ==, gw2_str); + g_assert_cmpstr (val, ==, "2.2.2.254"); g_free (val); val = svGetValue (f, "GATEWAY0", FALSE); @@ -8616,11 +8270,8 @@ test_write_wifi_wep_adhoc (void) GBytes *ssid; const char *ssid_data = "blahblah"; struct stat statbuf; - NMIP4Address *addr; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); + NMIPAddress *addr; const char *dns1 = "4.2.2.1"; - const guint32 prefix = 24; connection = nm_simple_connection_new (); @@ -8664,12 +8315,10 @@ test_write_wifi_wep_adhoc (void) g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP Address */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_ip4_config_add_dns (s_ip4, dns1); @@ -9608,11 +9257,8 @@ test_write_wifi_wpa_psk_adhoc (void) gboolean ignore_error = FALSE; GBytes *ssid; const char *ssid_data = "blahblah"; - NMIP4Address *addr; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); + NMIPAddress *addr; const char *dns1 = "4.2.2.1"; - const guint32 prefix = 24; connection = nm_simple_connection_new (); @@ -9663,12 +9309,10 @@ test_write_wifi_wpa_psk_adhoc (void) g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP Address */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 25, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_ip4_config_add_dns (s_ip4, dns1); @@ -11566,10 +11210,7 @@ test_write_bridge_main (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); - const guint32 prefix = 24; - NMIP4Address *addr; + NMIPAddress *addr; static const char *mac = "31:33:33:37:be:cd"; gboolean success; GError *error = NULL; @@ -11617,12 +11258,10 @@ test_write_bridge_main (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); /* IP6 setting */ s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); @@ -12354,10 +11993,7 @@ test_write_bond_main (void) NMSettingIP6Config *s_ip6; NMSettingWired *s_wired; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); - const guint32 prefix = 24; - NMIP4Address *addr; + NMIPAddress *addr; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -12400,12 +12036,10 @@ test_write_bond_main (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); /* IP6 setting */ s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); @@ -12698,10 +12332,7 @@ test_write_infiniband (void) const char *mac = "80:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22"; guint32 mtu = 65520; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); - const guint32 prefix = 24; - NMIP4Address *addr; + NMIPAddress *addr; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -12745,12 +12376,10 @@ test_write_infiniband (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); /* IP6 setting */ s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); @@ -13829,7 +13458,7 @@ int main (int argc, char **argv) g_test_add_data_func (TPATH "static-ip6-only-gw/_NULL_", NULL, test_write_wired_static_ip6_only_gw); g_test_add_data_func (TPATH "static-ip6-only-gw/::", "::", test_write_wired_static_ip6_only_gw); g_test_add_data_func (TPATH "static-ip6-only-gw/2001:db8:8:4::2", "2001:db8:8:4::2", test_write_wired_static_ip6_only_gw); - g_test_add_data_func (TPATH "static-ip6-only-gw/ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255", "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255", test_write_wired_static_ip6_only_gw); + g_test_add_data_func (TPATH "static-ip6-only-gw/::ffff:255.255.255.255", "::ffff:255.255.255.255", test_write_wired_static_ip6_only_gw); test_read_wired_static (TEST_IFCFG_WIRED_STATIC, "System test-wired-static", TRUE); test_read_wired_static (TEST_IFCFG_WIRED_STATIC_BOOTPROTO, "System test-wired-static-bootproto", FALSE); diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index 55dca87c82..160f070cdc 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -1743,12 +1743,11 @@ write_connection_setting (NMSettingConnection *s_con, shvarFile *ifcfg) static gboolean write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError **error) { - char dest[INET_ADDRSTRLEN]; - char next_hop[INET_ADDRSTRLEN]; + const char *dest, *next_hop; char **route_items; char *route_contents; - NMIP4Route *route; - guint32 ip, prefix, metric; + NMIPRoute *route; + guint32 prefix, metric; guint32 i, num; gboolean success = FALSE; @@ -1767,17 +1766,10 @@ write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError for (i = 0; i < num; i++) { route = nm_setting_ip4_config_get_route (s_ip4, i); - memset (dest, 0, sizeof (dest)); - ip = nm_ip4_route_get_dest (route); - inet_ntop (AF_INET, (const void *) &ip, &dest[0], sizeof (dest)); - - prefix = nm_ip4_route_get_prefix (route); - - memset (next_hop, 0, sizeof (next_hop)); - ip = nm_ip4_route_get_next_hop (route); - inet_ntop (AF_INET, (const void *) &ip, &next_hop[0], sizeof (next_hop)); - - metric = nm_ip4_route_get_metric (route); + dest = nm_ip_route_get_dest (route); + prefix = nm_ip_route_get_prefix (route); + next_hop = nm_ip_route_get_next_hop (route); + metric = nm_ip_route_get_metric (route); route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", dest, prefix, next_hop, metric); } @@ -1887,9 +1879,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) */ num = nm_setting_ip4_config_get_num_addresses (s_ip4); for (i = n = 0; i < num; i++) { - char buf[INET_ADDRSTRLEN]; - NMIP4Address *addr; - guint32 ip; + NMIPAddress *addr; if (i > 0) { const char *label; @@ -1918,24 +1908,15 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) addr = nm_setting_ip4_config_get_address (s_ip4, i); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_address (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, addr_key, &buf[0], FALSE); + svSetValue (ifcfg, addr_key, nm_ip_address_get_address (addr), FALSE); - tmp = g_strdup_printf ("%u", nm_ip4_address_get_prefix (addr)); + tmp = g_strdup_printf ("%u", nm_ip_address_get_prefix (addr)); svSetValue (ifcfg, prefix_key, tmp, FALSE); g_free (tmp); svSetValue (ifcfg, netmask_key, NULL, FALSE); - if (nm_ip4_address_get_gateway (addr)) { - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_gateway (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, gw_key, &buf[0], FALSE); - } else - svSetValue (ifcfg, gw_key, NULL, FALSE); + svSetValue (ifcfg, gw_key, nm_ip_address_get_gateway (addr), FALSE); g_free (addr_key); g_free (prefix_key); @@ -2050,8 +2031,8 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) num = nm_setting_ip4_config_get_num_routes (s_ip4); for (i = 0; i < 256; i++) { char buf[INET_ADDRSTRLEN]; - NMIP4Route *route; - guint32 ip, metric; + NMIPRoute *route; + guint32 netmask, metric; addr_key = g_strdup_printf ("ADDRESS%d", i); netmask_key = g_strdup_printf ("NETMASK%d", i); @@ -2066,23 +2047,17 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) } else { route = nm_setting_ip4_config_get_route (s_ip4, i); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_route_get_dest (route); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (routefile, addr_key, &buf[0], FALSE); + svSetValue (routefile, addr_key, nm_ip_route_get_dest (route), FALSE); memset (buf, 0, sizeof (buf)); - ip = nm_utils_ip4_prefix_to_netmask (nm_ip4_route_get_prefix (route)); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); + netmask = nm_utils_ip4_prefix_to_netmask (nm_ip_route_get_prefix (route)); + inet_ntop (AF_INET, (const void *) &netmask, &buf[0], sizeof (buf)); svSetValue (routefile, netmask_key, &buf[0], FALSE); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_route_get_next_hop (route); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (routefile, gw_key, &buf[0], FALSE); + svSetValue (routefile, gw_key, nm_ip_route_get_next_hop (route), FALSE); memset (buf, 0, sizeof (buf)); - metric = nm_ip4_route_get_metric (route); + metric = nm_ip_route_get_metric (route); if (metric == 0) svSetValue (routefile, metric_key, NULL, FALSE); else { @@ -2162,9 +2137,8 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) num = nm_setting_ip4_config_get_num_addresses (s_ip4); for (i = 0; i < num; i++) { const char *label, *p; - char buf[INET_ADDRSTRLEN], *path, *tmp; - NMIP4Address *addr; - guint32 ip; + char *path, *tmp; + NMIPAddress *addr; shvarFile *ifcfg; label = _nm_setting_ip4_config_get_address_label (s_ip4, i); @@ -2186,22 +2160,13 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) svSetValue (ifcfg, "DEVICE", label, FALSE); addr = nm_setting_ip4_config_get_address (s_ip4, i); + svSetValue (ifcfg, "IPADDR", nm_ip_address_get_address (addr), FALSE); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_address (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, "IPADDR", &buf[0], FALSE); - - tmp = g_strdup_printf ("%u", nm_ip4_address_get_prefix (addr)); + tmp = g_strdup_printf ("%u", nm_ip_address_get_prefix (addr)); svSetValue (ifcfg, "PREFIX", tmp, FALSE); g_free (tmp); - if (nm_ip4_address_get_gateway (addr)) { - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_gateway (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, "GATEWAY", &buf[0], FALSE); - } + svSetValue (ifcfg, "GATEWAY", nm_ip_address_get_gateway (addr), FALSE); svWriteFile (ifcfg, 0644, NULL); svCloseFile (ifcfg); @@ -2214,13 +2179,9 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) static gboolean write_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **error) { - char dest[INET6_ADDRSTRLEN]; - char next_hop[INET6_ADDRSTRLEN]; char **route_items; char *route_contents; - NMIP6Route *route; - const struct in6_addr *ip; - guint32 prefix, metric; + NMIPRoute *route; guint32 i, num; gboolean success = FALSE; @@ -2238,20 +2199,11 @@ write_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **err route_items = g_malloc0 (sizeof (char*) * (num + 1)); for (i = 0; i < num; i++) { route = nm_setting_ip6_config_get_route (s_ip6, i); - - memset (dest, 0, sizeof (dest)); - ip = nm_ip6_route_get_dest (route); - inet_ntop (AF_INET6, (const void *) ip, &dest[0], sizeof (dest)); - - prefix = nm_ip6_route_get_prefix (route); - - memset (next_hop, 0, sizeof (next_hop)); - ip = nm_ip6_route_get_next_hop (route); - inet_ntop (AF_INET6, (const void *) ip, &next_hop[0], sizeof (next_hop)); - - metric = nm_ip6_route_get_metric (route); - - route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", dest, prefix, next_hop, metric); + route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + nm_ip_route_get_next_hop (route), + nm_ip_route_get_metric (route)); } route_items[num] = NULL; route_contents = g_strjoinv (NULL, route_items); @@ -2276,13 +2228,11 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) NMSettingIP6Config *s_ip6; NMSettingIP4Config *s_ip4; const char *value; - char *addr_key, *prefix; + char *addr_key; guint32 i, num, num4; GString *searches; - char buf[INET6_ADDRSTRLEN]; - char ipv6_defaultgw[INET6_ADDRSTRLEN]; - NMIP6Address *addr; - const struct in6_addr *ip; + const char *ipv6_defaultgw; + NMIPAddress *addr; const char *dns; GString *ip_str1, *ip_str2, *ip_ptr; char *route6_path; @@ -2336,7 +2286,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) num = nm_setting_ip6_config_get_num_addresses (s_ip6); ip_str1 = g_string_new (NULL); ip_str2 = g_string_new (NULL); - ipv6_defaultgw[0] = 0; + ipv6_defaultgw = NULL; for (i = 0; i < num; i++) { if (i == 0) ip_ptr = ip_str1; @@ -2344,23 +2294,16 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) ip_ptr = ip_str2; addr = nm_setting_ip6_config_get_address (s_ip6, i); - ip = nm_ip6_address_get_address (addr); - prefix = g_strdup_printf ("%u", nm_ip6_address_get_prefix (addr)); - memset (buf, 0, sizeof (buf)); - inet_ntop (AF_INET6, (const void *) ip, buf, sizeof (buf)); + if (i > 1) g_string_append_c (ip_ptr, ' '); /* separate addresses in IPV6ADDR_SECONDARIES */ - g_string_append (ip_ptr, buf); - g_string_append_c (ip_ptr, '/'); - g_string_append (ip_ptr, prefix); - g_free (prefix); - - /* We only support gateway for the first IP address for now */ - if (i == 0) { - ip = nm_ip6_address_get_gateway (addr); - if (!IN6_IS_ADDR_UNSPECIFIED (ip)) - inet_ntop (AF_INET6, ip, ipv6_defaultgw, sizeof (ipv6_defaultgw)); - } + g_string_append_printf (ip_ptr, "%s/%u", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); + + /* We only support gateway for the first IP address */ + if (i == 0) + ipv6_defaultgw = nm_ip_address_get_gateway (addr); } svSetValue (ifcfg, "IPV6ADDR", ip_str1->str, FALSE); svSetValue (ifcfg, "IPV6ADDR_SECONDARIES", ip_str2->str, FALSE); diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index f76876f11f..64dcd75a77 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -611,21 +611,25 @@ make_ip4_setting (NMConnection *connection, /************** add all ip settings to the connection**********/ while (iblock) { ip_block *current_iblock; - NMIP4Address *ip4_addr = nm_ip4_address_new (); + NMIPAddress *ip4_addr; + GError *local = NULL; - nm_ip4_address_set_address (ip4_addr, iblock->ip); - nm_ip4_address_set_prefix (ip4_addr, - nm_utils_ip4_netmask_to_prefix - (iblock->netmask)); /* currently all the IPs has the same gateway */ - nm_ip4_address_set_gateway (ip4_addr, iblock->gateway); - if (iblock->gateway) + ip4_addr = nm_ip_address_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, &local); + if (iblock->next_hop) g_object_set (ip4_setting, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, TRUE, NULL); - if (!nm_setting_ip4_config_add_address (ip4_setting, ip4_addr)) - nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP4 address"); - nm_ip4_address_unref (ip4_addr); + + if (ip4_addr) { + if (!nm_setting_ip4_config_add_address (ip4_setting, ip4_addr)) + nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP4 address"); + nm_ip_address_unref (ip4_addr); + } else { + nm_log_warn (LOGD_SETTINGS, " ignoring invalid address entry: %s", local->message); + g_clear_error (&local); + } + current_iblock = iblock; iblock = iblock->next; destroy_ip_block (current_iblock); @@ -690,33 +694,34 @@ make_ip4_setting (NMConnection *connection, const char *metric_str; char *stripped; long int metric; - NMIP4Route *route = nm_ip4_route_new (); + NMIPRoute *route; + GError *local = NULL; - nm_ip4_route_set_dest (route, iblock->ip); - nm_ip4_route_set_next_hop (route, iblock->gateway); - nm_ip4_route_set_prefix (route, - nm_utils_ip4_netmask_to_prefix - (iblock->netmask)); if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) { metric = strtol (metric_str, NULL, 10); - nm_ip4_route_set_metric (route, (guint32) metric); } else { metric_str = ifnet_get_global_data ("metric"); if (metric_str) { stripped = g_strdup (metric_str); strip_string (stripped, '"'); metric = strtol (metric_str, NULL, 10); - nm_ip4_route_set_metric (route, - (guint32) metric); g_free (stripped); - } + } else + metric = 0; } - if (!nm_setting_ip4_config_add_route (ip4_setting, route)) - nm_log_warn (LOGD_SETTINGS, "duplicate IP4 route"); - nm_log_info (LOGD_SETTINGS, "new IP4 route:%d\n", iblock->ip); + route = nm_ip_route_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local); - nm_ip4_route_unref (route); + if (route) { + if (nm_setting_ip4_config_add_route (ip4_setting, route)) + nm_log_info (LOGD_SETTINGS, "new IP4 route:%s\n", iblock->ip); + else + nm_log_warn (LOGD_SETTINGS, "duplicate IP4 route"); + nm_ip_route_unref (route); + } else { + nm_log_warn (LOGD_SETTINGS, " ignoring invalid route entry: %s", local->message); + g_clear_error (&local); + } current_iblock = iblock; iblock = iblock->next; @@ -739,7 +744,7 @@ make_ip6_setting (NMConnection *connection, gboolean ipv6_enabled = FALSE; gchar *method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; const char *value; - ip6_block *iblock; + ip_block *iblock; gboolean never_default = !has_default_ip6_route (conn_name); s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); @@ -776,7 +781,7 @@ make_ip6_setting (NMConnection *connection, /* Make manual settings */ if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { - ip6_block *current_iblock; + ip_block *current_iblock; iblock = convert_ip6_config_block (conn_name); if (!iblock) { @@ -787,20 +792,26 @@ make_ip6_setting (NMConnection *connection, } /* add all IPv6 addresses */ while (iblock) { - NMIP6Address *ip6_addr = nm_ip6_address_new (); - - nm_ip6_address_set_address (ip6_addr, iblock->ip); - nm_ip6_address_set_prefix (ip6_addr, iblock->prefix); - if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) { - nm_log_info (LOGD_SETTINGS, "ipv6 addresses count: %d", - nm_setting_ip6_config_get_num_addresses (s_ip6)); + NMIPAddress *ip6_addr; + GError *local = NULL; + + ip6_addr = nm_ip_address_new (AF_INET6, iblock->ip, iblock->prefix, NULL, &local); + if (ip6_addr) { + if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) { + nm_log_info (LOGD_SETTINGS, "ipv6 addresses count: %d", + nm_setting_ip6_config_get_num_addresses (s_ip6)); + } else { + nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP6 address"); + } + nm_ip_address_unref (ip6_addr); } else { - nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP4 address"); + nm_log_warn (LOGD_SETTINGS, " ignoring invalid address entry: %s", local->message); + g_clear_error (&local); } - nm_ip6_address_unref (ip6_addr); + current_iblock = iblock; iblock = iblock->next; - destroy_ip6_block (current_iblock); + destroy_ip_block (current_iblock); } } else if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) { @@ -818,42 +829,45 @@ make_ip6_setting (NMConnection *connection, TRUE, NULL); /* Add all IPv6 routes */ while (iblock) { - ip6_block *current_iblock = iblock; + ip_block *current_iblock = iblock; const char *metric_str; char *stripped; - long int metric = 1; - NMIP6Route *route = nm_ip6_route_new (); + long int metric; + NMIPRoute *route; + GError *local = NULL; - nm_ip6_route_set_dest (route, iblock->ip); - nm_ip6_route_set_next_hop (route, iblock->next_hop); - nm_ip6_route_set_prefix (route, iblock->prefix); /* metric is not per routes configuration right now * global metric is also supported (metric="x") */ if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) { metric = strtol (metric_str, NULL, 10); - nm_ip6_route_set_metric (route, (guint32) metric); + nm_ip_route_set_metric (route, (guint32) metric); } else { metric_str = ifnet_get_global_data ("metric"); if (metric_str) { stripped = g_strdup (metric_str); strip_string (stripped, '"'); metric = strtol (metric_str, NULL, 10); - nm_ip6_route_set_metric (route, - (guint32) metric); + nm_ip_route_set_metric (route, (guint32) metric); g_free (stripped); } else - nm_ip6_route_set_metric (route, (guint32) 1); + metric = 1; } - if (nm_setting_ip6_config_add_route (s_ip6, route)) - nm_log_info (LOGD_SETTINGS, " new IP6 route"); - else - nm_log_warn (LOGD_SETTINGS, " duplicate IP6 route"); - nm_ip6_route_unref (route); + route = nm_ip_route_new (AF_INET6, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local); + if (route) { + if (nm_setting_ip6_config_add_route (s_ip6, route)) + nm_log_info (LOGD_SETTINGS, " new IP6 route"); + else + nm_log_warn (LOGD_SETTINGS, " duplicate IP6 route"); + nm_ip_route_unref (route); + } else { + nm_log_warn (LOGD_SETTINGS, " ignoring invalid route entry: %s", local->message); + g_clear_error (&local); + } current_iblock = iblock; iblock = iblock->next; - destroy_ip6_block (current_iblock); + destroy_ip_block (current_iblock); } done: @@ -2361,7 +2375,6 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err { NMSettingIP4Config *s_ip4; const char *value; - char *tmp; guint32 i, num; GString *searches; GString *ips; @@ -2387,33 +2400,19 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err ips = g_string_new (NULL); /* IPv4 addresses */ for (i = 0; i < num; i++) { - char buf[INET_ADDRSTRLEN + 1]; - NMIP4Address *addr; - guint32 ip; + NMIPAddress *addr; addr = nm_setting_ip4_config_get_address (s_ip4, i); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_address (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); - g_string_append_printf (ips, "\"%s", &buf[0]); - - tmp = - g_strdup_printf ("%u", - nm_ip4_address_get_prefix (addr)); - g_string_append_printf (ips, "/%s\" ", tmp); - g_free (tmp); + g_string_append_printf (ips, "\"%s/%u", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); /* only the first gateway will be written */ - if (!has_def_route && nm_ip4_address_get_gateway (addr)) { - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_gateway (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); + if (!has_def_route && nm_ip_address_get_gateway (addr)) { g_string_append_printf (routes, - "\"default via %s\" ", - &buf[0]); + "\"default via %s\" ", + nm_ip_address_get_gateway (addr)); has_def_route = TRUE; } } @@ -2474,29 +2473,19 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err num = nm_setting_ip4_config_get_num_routes (s_ip4); if (num > 0) { for (i = 0; i < num; i++) { - char buf[INET_ADDRSTRLEN + 1]; - NMIP4Route *route; - guint32 ip; + NMIPRoute *route; + const char *next_hop; route = nm_setting_ip4_config_get_route (s_ip4, i); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_route_get_dest (route); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); - g_string_append_printf (routes, "\"%s", buf); - - tmp = - g_strdup_printf ("%u", - nm_ip4_route_get_prefix (route)); - g_string_append_printf (routes, "/%s via ", tmp); - g_free (tmp); + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "0.0.0.0"; - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_route_get_next_hop (route); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); - g_string_append_printf (routes, "%s\" ", buf); + g_string_append_printf (routes, "\"%s/%u via %s\" ", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop); } } if (routes->len > 0) @@ -2513,11 +2502,8 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err static gboolean write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **error) { - char dest[INET6_ADDRSTRLEN + 1]; - char next_hop[INET6_ADDRSTRLEN + 1]; - NMIP6Route *route; - const struct in6_addr *ip; - guint32 prefix; + NMIPRoute *route; + const char *next_hop; guint32 i, num; GString *routes_string; const char *old_routes; @@ -2535,20 +2521,14 @@ write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **er for (i = 0; i < num; i++) { route = nm_setting_ip6_config_get_route (s_ip6, i); - memset (dest, 0, sizeof (dest)); - ip = nm_ip6_route_get_dest (route); - inet_ntop (AF_INET6, (const void *) ip, &dest[0], - sizeof (dest)); - - prefix = nm_ip6_route_get_prefix (route); - - memset (next_hop, 0, sizeof (next_hop)); - ip = nm_ip6_route_get_next_hop (route); - inet_ntop (AF_INET6, (const void *) ip, &next_hop[0], - sizeof (next_hop)); + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "::"; g_string_append_printf (routes_string, "\"%s/%u via %s\" ", - dest, prefix, next_hop); + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop); } if (num > 0) ifnet_set_data (conn_name, "routes", routes_string->str); @@ -2562,12 +2542,9 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err { NMSettingIP6Config *s_ip6; const char *value; - char *prefix; guint32 i, num; GString *searches; - char buf[INET6_ADDRSTRLEN + 1]; - NMIP6Address *addr; - const struct in6_addr *ip; + NMIPAddress *addr; s_ip6 = nm_connection_get_setting_ip6_config (connection); if (!s_ip6) { @@ -2617,16 +2594,10 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err ip_str = g_string_new (NULL); for (i = 0; i < num; i++) { addr = nm_setting_ip6_config_get_address (s_ip6, i); - ip = nm_ip6_address_get_address (addr); - prefix = - g_strdup_printf ("%u", - nm_ip6_address_get_prefix (addr)); - memset (buf, 0, sizeof (buf)); - inet_ntop (AF_INET6, (const void *) ip, buf, - sizeof (buf)); - g_string_append_printf (ip_str, "\"%s/", buf); - g_string_append_printf (ip_str, "%s\" ", prefix); - g_free (prefix); + + g_string_append_printf (ip_str, "\"%s/%u\"", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); } tmp = g_strdup_printf ("%s\" %s", config, ip_str->str); ifnet_set_data (conn_name, "config", tmp); diff --git a/src/settings/plugins/ifnet/net_utils.c b/src/settings/plugins/ifnet/net_utils.c index be19134e96..9ead29f6e2 100644 --- a/src/settings/plugins/ifnet/net_utils.c +++ b/src/settings/plugins/ifnet/net_utils.c @@ -366,23 +366,21 @@ create_ip4_block (gchar * ip) ip_mask = g_strsplit (ip, "/", 0); length = g_strv_length (ip_mask); - if (!inet_pton (AF_INET, ip_mask[0], &tmp_ip4_addr)) + if (!nm_utils_ipaddr_valid (AF_INET, ip_mask[0])) goto error; - iblock->ip = tmp_ip4_addr; + 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->netmask = nm_utils_ip4_prefix_to_netmask ((guint32) - atoi (ip_mask - [1])); + 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 (!inet_pton (AF_INET, ip_mask[0], &tmp_ip4_addr)) + if (!nm_utils_ipaddr_valid (AF_INET, ip_mask[0])) goto error; - iblock->ip = tmp_ip4_addr; + 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') ; @@ -390,7 +388,7 @@ create_ip4_block (gchar * ip) goto error; if (!inet_pton (AF_INET, ip_mask[i], &tmp_ip4_addr)) goto error; - iblock->netmask = tmp_ip4_addr; + 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")) @@ -403,26 +401,25 @@ 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; } -static ip6_block * -create_ip6_block (gchar * ip) +static ip_block * +create_ip_block (gchar * ip) { - ip6_block *iblock = g_slice_new0 (ip6_block); + ip_block *iblock = g_slice_new0 (ip_block); gchar *dup_ip = g_strdup (ip); - struct in6_addr *tmp_ip6_addr = g_slice_new0 (struct in6_addr); gchar *prefix = NULL; if ((prefix = strstr (dup_ip, "/")) != NULL) { *prefix = '\0'; prefix++; } - if (!inet_pton (AF_INET6, dup_ip, tmp_ip6_addr)) { + if (!nm_utils_ipaddr_valid (AF_INET6, dup_ip)) goto error; - } - iblock->ip = tmp_ip6_addr; + iblock->ip = dup_ip; if (prefix) { errno = 0; iblock->prefix = strtol (prefix, NULL, 10); @@ -431,30 +428,26 @@ create_ip6_block (gchar * ip) } } else iblock->prefix = 64; - g_free (dup_ip); return iblock; error: if (!is_ip4_address (ip)) nm_log_warn (LOGD_SETTINGS, "Can't handle IPv6 address: %s", ip); - g_slice_free (ip6_block, iblock); - g_slice_free (struct in6_addr, tmp_ip6_addr); - + g_slice_free (ip_block, iblock); g_free (dup_ip); return NULL; } -static guint32 +static char * get_ip4_gateway (gchar * gateway) { gchar *tmp, *split; - guint32 tmp_ip4_addr; if (!gateway) - return 0; + return NULL; tmp = find_gateway_str (gateway); if (!tmp) { nm_log_warn (LOGD_SETTINGS, "Couldn't obtain gateway in \"%s\"", gateway); - return 0; + return NULL; } tmp = g_strdup (tmp); strip_string (tmp, ' '); @@ -464,43 +457,39 @@ get_ip4_gateway (gchar * gateway) if ((split = strstr (tmp, "\"")) != NULL) *split = '\0'; - if (!inet_pton (AF_INET, tmp, &tmp_ip4_addr)) + if (!nm_utils_ipaddr_valid (AF_INET, tmp)) goto error; - g_free (tmp); - return tmp_ip4_addr; + return tmp; error: if (!is_ip6_address (tmp)) nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 gateway: %s", tmp); g_free (tmp); - return 0; + return NULL; } -static struct in6_addr * +static char * get_ip6_next_hop (gchar * next_hop) { gchar *tmp; - struct in6_addr *tmp_ip6_addr = g_slice_new0 (struct in6_addr); if (!next_hop) - return 0; + return NULL; tmp = find_gateway_str (next_hop); if (!tmp) { nm_log_warn (LOGD_SETTINGS, "Couldn't obtain next_hop in \"%s\"", next_hop); - return 0; + return NULL; } tmp = g_strdup (tmp); strip_string (tmp, ' '); strip_string (tmp, '"'); g_strstrip (tmp); - if (!inet_pton (AF_INET6, tmp, tmp_ip6_addr)) + if (!nm_utils_ipaddr_valid (AF_INET6, tmp)) goto error; - g_free (tmp); - return tmp_ip6_addr; + return tmp; error: if (!is_ip4_address (tmp)) nm_log_warn (LOGD_SETTINGS, "Can't handle IPv6 next_hop: %s", tmp); g_free (tmp); - g_slice_free (struct in6_addr, tmp_ip6_addr); return NULL; } @@ -512,7 +501,7 @@ convert_ip4_config_block (const char *conn_name) guint length; guint i; gchar *ip; - guint32 def_gateway = 0; + char *def_gateway = NULL; const char *routes; ip_block *start = NULL, *current = NULL, *iblock = NULL; @@ -531,8 +520,8 @@ convert_ip4_config_block (const char *conn_name) iblock = create_ip4_block (ip); if (iblock == NULL) continue; - if (!iblock->gateway && def_gateway != 0) - iblock->gateway = def_gateway; + if (!iblock->next_hop && def_gateway != NULL) + iblock->next_hop = g_strdup (def_gateway); if (start == NULL) start = current = iblock; else { @@ -541,17 +530,18 @@ convert_ip4_config_block (const char *conn_name) } } g_strfreev (ipset); + g_free (def_gateway); return start; } -ip6_block * +ip_block * convert_ip6_config_block (const char *conn_name) { gchar **ipset; guint length; guint i; gchar *ip; - ip6_block *start = NULL, *current = NULL, *iblock = NULL; + 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")); @@ -559,7 +549,7 @@ convert_ip6_config_block (const char *conn_name) for (i = 0; i < length; i++) { ip = ipset[i]; ip = strip_string (ip, '"'); - iblock = create_ip6_block (ip); + iblock = create_ip_block (ip); if (iblock == NULL) continue; if (start == NULL) @@ -595,7 +585,7 @@ convert_ip4_routes_block (const char *conn_name) iblock = create_ip4_block (ip); if (iblock == NULL) continue; - iblock->gateway = get_ip4_gateway (ip); + iblock->next_hop = get_ip4_gateway (ip); if (start == NULL) start = current = iblock; else { @@ -607,15 +597,14 @@ convert_ip4_routes_block (const char *conn_name) return start; } -ip6_block * +ip_block * convert_ip6_routes_block (const char *conn_name) { gchar **ipset; guint length; guint i; gchar *ip, *tmp_addr; - ip6_block *start = NULL, *current = NULL, *iblock = NULL; - struct in6_addr *tmp_ip6_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")); @@ -629,25 +618,17 @@ convert_ip6_routes_block (const char *conn_name) 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; - } + iblock = g_slice_new0 (ip_block); + iblock->ip = g_strdup ("::"); + iblock->prefix = 128; } } else - iblock = create_ip6_block (ip); + iblock = create_ip_block (ip); if (iblock == NULL) continue; iblock->next_hop = get_ip6_next_hop (ip); if (iblock->next_hop == NULL) { - destroy_ip6_block (iblock); + destroy_ip_block (iblock); continue; } if (start == NULL) @@ -664,19 +645,12 @@ convert_ip6_routes_block (const char *conn_name) void destroy_ip_block (ip_block * iblock) { + g_free (iblock->ip); + g_free (iblock->next_hop); g_slice_free (ip_block, iblock); } void -destroy_ip6_block (ip6_block * iblock) -{ - g_slice_free (struct in6_addr, iblock->ip); - g_slice_free (struct in6_addr, iblock->next_hop); - - g_slice_free (ip6_block, iblock); -} - -void set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name) { const char *dns_servers; diff --git a/src/settings/plugins/ifnet/net_utils.h b/src/settings/plugins/ifnet/net_utils.h index cee71d57e5..83272e8956 100644 --- a/src/settings/plugins/ifnet/net_utils.h +++ b/src/settings/plugins/ifnet/net_utils.h @@ -31,19 +31,12 @@ #define has_default_ip6_route(conn_name) has_default_route((conn_name), &is_ip6_address) typedef struct _ip_block { - guint32 ip; - guint32 netmask; - guint32 gateway; + char *ip; + guint32 prefix; + char *next_hop; struct _ip_block *next; } ip_block; -typedef struct _ip6_block { - struct in6_addr *ip; - long int prefix; - struct in6_addr *next_hop; - struct _ip6_block *next; -} ip6_block; - gchar *read_hostname (const char *path); gboolean write_hostname (const char *path, const char *hostname); gboolean is_static_ip4 (const char *conn_name); @@ -55,11 +48,10 @@ gboolean has_default_route (const char *conn_name, gboolean (*check_fn) (const c gboolean reload_parsers (void); ip_block *convert_ip4_config_block (const char *conn_name); -ip6_block *convert_ip6_config_block (const char *conn_name); +ip_block *convert_ip6_config_block (const char *conn_name); ip_block *convert_ip4_routes_block (const char *conn_name); -ip6_block *convert_ip6_routes_block (const char *conn_name); +ip_block *convert_ip6_routes_block (const char *conn_name); void destroy_ip_block (ip_block * iblock); -void destroy_ip6_block (ip6_block * iblock); void set_ip4_dns_servers (NMSettingIP4Config * s_ip4, const char *conn_name); void set_ip6_dns_servers (NMSettingIP6Config * s_ip6, const char *conn_name); diff --git a/src/settings/plugins/ifnet/tests/test_all.c b/src/settings/plugins/ifnet/tests/test_all.c index 163cf6ede3..ed44a01cf8 100644 --- a/src/settings/plugins/ifnet/tests/test_all.c +++ b/src/settings/plugins/ifnet/tests/test_all.c @@ -160,25 +160,14 @@ test_is_ip6_address (void) } static void -check_ip_block (ip_block * iblock, gchar * ip, gchar * netmask, gchar * gateway) +check_ip_block (ip_block * iblock, gchar * ip, guint32 prefix, gchar * gateway) { - char *str; - guint32 tmp_ip4_addr; - - str = malloc (INET_ADDRSTRLEN); - tmp_ip4_addr = iblock->ip; - inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN); - ASSERT (strcmp (ip, str) == 0, "check ip", - "ip expected:%s, find:%s", ip, str); - tmp_ip4_addr = iblock->netmask; - inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN); - ASSERT (strcmp (netmask, str) == 0, "check netmask", - "netmask expected:%s, find:%s", netmask, str); - tmp_ip4_addr = iblock->gateway; - inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN); - ASSERT (strcmp (gateway, str) == 0, "check gateway", - "gateway expected:%s, find:%s", gateway, str); - free (str); + ASSERT (strcmp (ip, iblock->ip) == 0, "check ip", + "ip expected:%s, find:%s", ip, iblock->ip); + ASSERT (prefix == iblock->prefix, "check netmask", + "prefix expected:%d, find:%d", prefix, iblock->prefix); + ASSERT (g_strcmp0 (gateway, iblock->next_hop) == 0, "check gateway", + "gateway expected:%s, find:%s", gateway, iblock->next_hop); } static void @@ -189,14 +178,12 @@ test_convert_ipv4_config_block (void) ASSERT (iblock != NULL, "convert ipv4 block", "block eth0 should not be NULL"); - check_ip_block (iblock, "202.117.16.121", "255.255.255.0", - "202.117.16.1"); + check_ip_block (iblock, "202.117.16.121", 24, "202.117.16.1"); iblock = iblock->next; destroy_ip_block (tmp); ASSERT (iblock != NULL, "convert ipv4 block", "block eth0 should have a second IP address"); - check_ip_block (iblock, "192.168.4.121", "255.255.255.0", - "202.117.16.1"); + check_ip_block (iblock, "192.168.4.121", 24, "202.117.16.1"); destroy_ip_block (iblock); g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, @@ -206,7 +193,7 @@ test_convert_ipv4_config_block (void) ASSERT (iblock != NULL && iblock->next == NULL, "convert error IPv4 address", "should only get one address"); - check_ip_block (iblock, "192.168.4.121", "255.255.255.0", "0.0.0.0"); + check_ip_block (iblock, "192.168.4.121", 24, NULL); destroy_ip_block (iblock); g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, @@ -214,7 +201,6 @@ test_convert_ipv4_config_block (void) iblock = convert_ip4_config_block ("eth3"); ASSERT (iblock == NULL, "convert config_block", "convert error configuration"); - destroy_ip_block (iblock); } static void @@ -224,7 +210,7 @@ test_convert_ipv4_routes_block (void) ip_block *tmp = iblock; ASSERT (iblock != NULL, "convert ip4 routes", "should get one route"); - check_ip_block (iblock, "192.168.4.0", "255.255.255.0", "192.168.4.1"); + check_ip_block (iblock, "192.168.4.0", 24, "192.168.4.1"); iblock = iblock->next; destroy_ip_block (tmp); ASSERT (iblock == NULL, "convert ip4 routes", @@ -234,7 +220,7 @@ test_convert_ipv4_routes_block (void) tmp = iblock; ASSERT (iblock != NULL, "convert ip4 routes", "should get one route"); - check_ip_block (iblock, "10.0.0.0", "255.0.0.0", "192.168.0.1"); + check_ip_block (iblock, "10.0.0.0", 8, "192.168.0.1"); iblock = iblock->next; destroy_ip_block (tmp); ASSERT (iblock == NULL, "convert ip4 routes", diff --git a/src/settings/plugins/ifupdown/parser.c b/src/settings/plugins/ifupdown/parser.c index 415cfcc755..72340a90eb 100644 --- a/src/settings/plugins/ifupdown/parser.c +++ b/src/settings/plugins/ifupdown/parser.c @@ -447,8 +447,8 @@ update_ip4_setting_from_if_block(NMConnection *connection, if (!is_static) { g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); } else { - guint32 tmp_addr, tmp_mask, tmp_gw; - NMIP4Address *addr; + guint32 tmp_mask; + NMIPAddress *addr; const char *address_v; const char *netmask_v; const char *gateway_v; @@ -460,10 +460,9 @@ update_ip4_setting_from_if_block(NMConnection *connection, /* Address */ address_v = ifparser_getkey (block, "address"); - if (!address_v || !inet_pton (AF_INET, address_v, &tmp_addr)) { + if (!address_v) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Missing IPv4 address '%s'", - address_v ? address_v : "(none)"); + "Missing IPv4 address"); goto error; } @@ -472,11 +471,6 @@ update_ip4_setting_from_if_block(NMConnection *connection, if (netmask_v) { if (strlen (netmask_v) < 7) { netmask_int = atoi (netmask_v); - if (netmask_int > 32) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IPv4 netmask '%s'", netmask_v); - goto error; - } } else if (!inet_pton (AF_INET, netmask_v, &tmp_mask)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IPv4 netmask '%s'", netmask_v); @@ -490,17 +484,11 @@ update_ip4_setting_from_if_block(NMConnection *connection, gateway_v = ifparser_getkey (block, "gateway"); if (!gateway_v) gateway_v = address_v; /* dcbw: whaaa?? */ - if (!inet_pton (AF_INET, gateway_v, &tmp_gw)) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IPv4 gateway '%s'", gateway_v); - goto error; - } /* Add the new address to the setting */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, tmp_addr); - nm_ip4_address_set_prefix (addr, netmask_int); - nm_ip4_address_set_gateway (addr, tmp_gw); + addr = nm_ip_address_new (AF_INET, address_v, netmask_int, gateway_v, error); + if (!addr) + goto error; if (nm_setting_ip4_config_add_address (s_ip4, addr)) { nm_log_info (LOGD_SETTINGS, "addresses count: %d", @@ -508,7 +496,7 @@ update_ip4_setting_from_if_block(NMConnection *connection, } else { nm_log_info (LOGD_SETTINGS, "ignoring duplicate IP4 address"); } - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nameserver_v = ifparser_getkey (block, "dns-nameserver"); ifupdown_ip4_add_dns (s_ip4, nameserver_v); @@ -582,8 +570,7 @@ update_ip6_setting_from_if_block(NMConnection *connection, if (!is_static) { g_object_set(s_ip6, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); } else { - struct in6_addr tmp_addr, tmp_gw; - NMIP6Address *addr; + NMIPAddress *addr; const char *address_v; const char *prefix_v; const char *gateway_v; @@ -595,10 +582,9 @@ update_ip6_setting_from_if_block(NMConnection *connection, /* Address */ address_v = ifparser_getkey(block, "address"); - if (!address_v || !inet_pton (AF_INET6, address_v, &tmp_addr)) { + if (!address_v) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Missing IPv6 address '%s'", - address_v ? address_v : "(none)"); + "Missing IPv6 address"); goto error; } @@ -611,17 +597,11 @@ update_ip6_setting_from_if_block(NMConnection *connection, gateway_v = ifparser_getkey (block, "gateway"); if (!gateway_v) gateway_v = address_v; /* dcbw: whaaa?? */ - if (!inet_pton (AF_INET6, gateway_v, &tmp_gw)) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IPv6 gateway '%s'", gateway_v); - goto error; - } /* Add the new address to the setting */ - addr = nm_ip6_address_new (); - nm_ip6_address_set_address (addr, &tmp_addr); - nm_ip6_address_set_prefix (addr, prefix_int); - nm_ip6_address_set_gateway (addr, &tmp_gw); + addr = nm_ip_address_new (AF_INET6, address_v, prefix_int, gateway_v, error); + if (!addr) + goto error; if (nm_setting_ip6_config_add_address (s_ip6, addr)) { nm_log_info (LOGD_SETTINGS, "addresses count: %d", @@ -629,7 +609,7 @@ update_ip6_setting_from_if_block(NMConnection *connection, } else { nm_log_info (LOGD_SETTINGS, "ignoring duplicate IP6 address"); } - nm_ip6_address_unref (addr); + nm_ip_address_unref (addr); nameserver_v = ifparser_getkey(block, "dns-nameserver"); ifupdown_ip6_add_dns (s_ip6, nameserver_v); diff --git a/src/settings/plugins/ifupdown/tests/test-ifupdown.c b/src/settings/plugins/ifupdown/tests/test-ifupdown.c index 8100676d79..643fa7d08a 100644 --- a/src/settings/plugins/ifupdown/tests/test-ifupdown.c +++ b/src/settings/plugins/ifupdown/tests/test-ifupdown.c @@ -464,13 +464,10 @@ test17_read_static_ipv4 (const char *path) char *unmanaged = NULL; GError *error = NULL; const char* tmp; - const char *expected_address = "10.0.0.3"; const char *expected_id = "Ifupdown (eth0)"; const char *expected_search1 = "example.com"; const char *expected_search2 = "foo.example.com"; - guint32 expected_prefix = 8; - NMIP4Address *ip4_addr; - guint32 addr; + NMIPAddress *ip4_addr; #define TEST17_NAME "wired-static-verify-ip4" if_block *block = NULL; @@ -521,10 +518,6 @@ test17_read_static_ipv4 (const char *path) /* ===== IPv4 SETTING ===== */ - ASSERT (inet_pton (AF_INET, expected_address, &addr) > 0, - TEST17_NAME, "failed to verify %s: couldn't convert IP address #1", - file); - s_ip4 = nm_connection_get_setting_ip4_config (connection); ASSERT (s_ip4 != NULL, TEST17_NAME, "failed to verify %s: missing %s setting", @@ -547,17 +540,9 @@ test17_read_static_ipv4 (const char *path) NM_SETTING_IP4_CONFIG_ADDRESSES); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - ASSERT (ip4_addr, - TEST17_NAME, "failed to verify %s: missing IP4 address #1", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix, - TEST17_NAME, "failed to verify %s: unexpected IP4 address prefix", - file); - - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - TEST17_NAME, "failed to verify %s: unexpected IP4 address: %s", - file, addr); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); /* DNS Addresses */ ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 2, @@ -629,13 +614,10 @@ test18_read_static_ipv6 (const char *path) char *unmanaged = NULL; GError *error = NULL; const char* tmp; - const char *expected_address = "fc00::1"; const char *expected_id = "Ifupdown (myip6tunnel)"; const char *expected_search1 = "example.com"; const char *expected_search2 = "foo.example.com"; - guint32 expected_prefix = 64; - NMIP6Address *ip6_addr; - struct in6_addr addr; + NMIPAddress *ip6_addr; if_block *block = NULL; #define TEST18_NAME "wired-static-verify-ip6" const char* file = "test18-" TEST18_NAME; @@ -691,11 +673,6 @@ test18_read_static_ipv6 (const char *path) /* ===== IPv6 SETTING ===== */ - ASSERT (inet_pton (AF_INET6, expected_address, &addr) > 0, - TEST18_NAME, - "failed to verify %s: couldn't convert IP address #1", - file); - s_ip6 = nm_connection_get_setting_ip6_config (connection); ASSERT (s_ip6 != NULL, TEST18_NAME, @@ -721,27 +698,9 @@ test18_read_static_ipv6 (const char *path) NM_SETTING_IP6_CONFIG_ADDRESSES); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); - ASSERT (ip6_addr, - TEST18_NAME, - "failed to verify %s: missing %s / %s #1", - file, - NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix, - TEST18_NAME - "failed to verify %s: unexpected %s / %s prefix", - file, - NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); - - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), - &addr), - TEST18_NAME, - "failed to verify %s: unexpected %s / %s", - file, - NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "fc00::1"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); /* DNS Addresses */ ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 2, @@ -813,10 +772,7 @@ test19_read_static_ipv4_plen (const char *path) NMSettingIP4Config *s_ip4; char *unmanaged = NULL; GError *error = NULL; - const char *expected_address = "10.0.0.3"; - guint32 expected_prefix = 8; - NMIP4Address *ip4_addr; - guint32 addr; + NMIPAddress *ip4_addr; #define TEST19_NAME "wired-static-verify-ip4-plen" if_block *block = NULL; @@ -838,10 +794,6 @@ test19_read_static_ipv4_plen (const char *path) /* ===== IPv4 SETTING ===== */ - ASSERT (inet_pton (AF_INET, expected_address, &addr) > 0, - TEST19_NAME, "failed to verify %s: couldn't convert IP address #1", - file); - s_ip4 = nm_connection_get_setting_ip4_config (connection); ASSERT (s_ip4 != NULL, TEST19_NAME, "failed to verify %s: missing %s setting", @@ -856,17 +808,9 @@ test19_read_static_ipv4_plen (const char *path) NM_SETTING_IP4_CONFIG_ADDRESSES); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - ASSERT (ip4_addr, - TEST19_NAME, "failed to verify %s: missing IP4 address #1", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix, - TEST19_NAME, "failed to verify %s: unexpected IP4 address prefix", - file); - - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - TEST19_NAME, "failed to verify %s: unexpected IP4 address: %s", - file, addr); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); g_object_unref (connection); } diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index cbd6e49ab1..97ccf7d89b 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -108,130 +108,57 @@ get_one_int (const char *str, guint32 max_val, const char *key_name, guint32 *ou } static gpointer -build_ip4_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, gboolean route) +build_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, int family, gboolean route) { gpointer result; - guint32 addr; - guint32 address = 0; - guint32 gateway = 0; guint32 metric = 0; - int err; + GError *error = NULL; g_return_val_if_fail (address_str, NULL); - /* Address */ - err = inet_pton (AF_INET, address_str, &addr); - if (err <= 0) { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv4 address '%s'", __func__, address_str); - return NULL; - } - address = addr; - /* Gateway */ if (gateway_str && gateway_str[0]) { - err = inet_pton (AF_INET, gateway_str, &addr); - if (err <= 0) { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv4 gateway '%s'", __func__, gateway_str); - return NULL; - } - gateway = addr; - } - else - gateway = 0; - - /* parse metric, default to 0 */ - if (metric_str) { - if (!get_one_int (metric_str, G_MAXUINT32, key_name, &metric)) - return NULL; - } - - if (route) { - result = nm_ip4_route_new (); - nm_ip4_route_set_dest (result, address); - nm_ip4_route_set_prefix (result, plen); - nm_ip4_route_set_next_hop (result, gateway); - nm_ip4_route_set_metric (result, metric); - } else { - result = nm_ip4_address_new (); - nm_ip4_address_set_address (result, address); - nm_ip4_address_set_prefix (result, plen); - nm_ip4_address_set_gateway (result, gateway); - } - - return result; -} - -static gpointer -build_ip6_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, gboolean route) -{ - gpointer result; - struct in6_addr addr; - guint32 metric = 0; - int err; - - g_return_val_if_fail (address_str, NULL); - - if (route) - result = nm_ip6_route_new (); - else - result = nm_ip6_address_new (); - - /* add address and prefix length */ - err = inet_pton (AF_INET6, address_str, &addr); - if (err <= 0) { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv6 address '%s'", __func__, address_str); - goto error_out; - } - if (route) { - nm_ip6_route_set_dest (result, &addr); - nm_ip6_route_set_prefix (result, plen); - } else { - nm_ip6_address_set_address (result, &addr); - nm_ip6_address_set_prefix (result, plen); - } - - /* add gateway */ - if (gateway_str && gateway_str[0]) { - err = inet_pton (AF_INET6, gateway_str, &addr); - if (err <= 0) { + if (!nm_utils_ipaddr_valid (family, gateway_str)) { /* Try workaround for routes written by broken keyfile writer. * Due to bug bgo#719851, an older version of writer would have * written "a:b:c:d::/plen,metric" if the gateway was ::, instead * of "a:b:c:d::/plen,,metric" or "a:b:c:d::/plen,::,metric" - * Try workaround by interepeting gateway_str as metric to accept such + * Try workaround by interpreting gateway_str as metric to accept such * invalid routes. This broken syntax should not be not officially * supported. **/ - if (route && !metric_str && get_one_int (gateway_str, G_MAXUINT32, NULL, &metric)) - addr = in6addr_any; + if ( family == AF_INET6 + && route + && !metric_str + && get_one_int (gateway_str, G_MAXUINT32, NULL, &metric)) + gateway_str = NULL; else { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv6 gateway '%s'", __func__, gateway_str); - goto error_out; + nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid gateway '%s'", __func__, gateway_str); + return NULL; } } } else - addr = in6addr_any; - - if (route) { - nm_ip6_route_set_next_hop (result, &addr); + gateway_str = NULL; - /* parse metric, default to 0 */ - if (metric_str) { - if (!get_one_int (metric_str, G_MAXUINT32, key_name, &metric)) - goto error_out; - } - nm_ip6_route_set_metric (result, metric); - } else - nm_ip6_address_set_gateway (result, &addr); - - return result; + /* parse metric, default to 0 */ + if (metric_str) { + if (!get_one_int (metric_str, G_MAXUINT32, key_name, &metric)) + return NULL; + } -error_out: if (route) - nm_ip6_route_unref (result); + result = nm_ip_route_new (family, address_str, plen, gateway_str, metric, &error); else - nm_ip4_route_unref (result); - return NULL; + result = nm_ip_address_new (family, address_str, plen, gateway_str, &error); + if (!result) { + nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid %s %s: %s", __func__, + family == AF_INET ? "IPv4" : "IPv6", + route ? "route" : "address", + error->message); + g_error_free (error); + } + + return result; } /* On success, returns pointer to the zero-terminated field (original @current). @@ -390,8 +317,8 @@ read_one_ip_address_or_route (GKeyFile *file, } /* build the appropriate data structure for NetworkManager settings */ - result = (ipv6 ? build_ip6_address_or_route : build_ip4_address_or_route) ( - key_name, address_str, plen, gateway_str, metric_str, route); + result = build_address_or_route (key_name, address_str, plen, gateway_str, metric_str, + ipv6 ? AF_INET6 : AF_INET, route); g_free (value); return result; @@ -413,17 +340,10 @@ ip_address_or_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfi GDestroyNotify free_func; int i; - if (ipv6) { - if (routes) - free_func = (GDestroyNotify) nm_ip6_route_unref; - else - free_func = (GDestroyNotify) nm_ip6_address_unref; - } else { - if (routes) - free_func = (GDestroyNotify) nm_ip4_route_unref; - else - free_func = (GDestroyNotify) nm_ip4_address_unref; - } + if (routes) + free_func = (GDestroyNotify) nm_ip_route_unref; + else + free_func = (GDestroyNotify) nm_ip_address_unref; list = g_ptr_array_new_with_free_func (free_func); for (i = -1; i < 1000; i++) { diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index e37f7c8a90..0e05fed45b 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -38,69 +38,51 @@ #define TEST_WIRELESS_FILE TEST_KEYFILES_DIR"/Test_Wireless_Connection" static void -check_ip4_address (NMSettingIP4Config *config, int idx, const char *address_str, int plen, const char *gateway_str) +check_ip4_address (NMSettingIP4Config *config, int idx, const char *address, int plen, const char *gateway) { - NMIP4Address *ip4 = nm_setting_ip4_config_get_address (config, idx); - guint32 address, gateway; - - g_assert (inet_pton (AF_INET, address_str, &address) == 1); - g_assert (inet_pton (AF_INET, gateway_str, &gateway) == 1); + NMIPAddress *ip4 = nm_setting_ip4_config_get_address (config, idx); g_assert (ip4); - g_assert (nm_ip4_address_get_address (ip4) == address); - g_assert (nm_ip4_address_get_prefix (ip4) == plen); - g_assert (nm_ip4_address_get_gateway (ip4) == gateway); + g_assert_cmpstr (nm_ip_address_get_address (ip4), ==, address); + g_assert_cmpint (nm_ip_address_get_prefix (ip4), ==, plen); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4), ==, gateway); } static void -check_ip6_address (NMSettingIP6Config *config, int idx, const char *address_str, int plen, const char *gateway_str) +check_ip6_address (NMSettingIP6Config *config, int idx, const char *address, int plen, const char *gateway) { - NMIP6Address *ip6 = nm_setting_ip6_config_get_address (config, idx); - struct in6_addr address; - struct in6_addr gateway; - - g_assert (inet_pton (AF_INET6, address_str, &address) == 1); - g_assert (inet_pton (AF_INET6, gateway_str, &gateway) == 1); + NMIPAddress *ip6 = nm_setting_ip6_config_get_address (config, idx); g_assert (ip6); - g_assert (!memcmp (nm_ip6_address_get_address (ip6), &address, sizeof(address))); - g_assert (nm_ip6_address_get_prefix (ip6) == plen); - g_assert (!memcmp (nm_ip6_address_get_gateway (ip6), &gateway, sizeof(gateway))); + g_assert_cmpstr (nm_ip_address_get_address (ip6), ==, address); + g_assert_cmpint (nm_ip_address_get_prefix (ip6), ==, plen); + g_assert_cmpstr (nm_ip_address_get_gateway (ip6), ==, gateway); } static void -check_ip4_route (NMSettingIP4Config *config, int idx, const char *destination_str, int plen, - const char *nexthop_str, int metric) +check_ip4_route (NMSettingIP4Config *config, int idx, const char *destination, int plen, + const char *next_hop, int metric) { - NMIP4Route *route = nm_setting_ip4_config_get_route (config, idx); - guint32 destination, nexthop; - - g_assert (inet_pton (AF_INET, destination_str, &destination) == 1); - g_assert (inet_pton (AF_INET, nexthop_str, &nexthop) == 1); + NMIPRoute *route = nm_setting_ip4_config_get_route (config, idx); g_assert (route); - g_assert (nm_ip4_route_get_dest (route) == destination); - g_assert (nm_ip4_route_get_prefix (route) == plen); - g_assert (nm_ip4_route_get_next_hop (route) == nexthop); - g_assert (nm_ip4_route_get_metric (route) == metric); + g_assert_cmpstr (nm_ip_route_get_dest (route), ==, destination); + g_assert_cmpint (nm_ip_route_get_prefix (route), ==, plen); + g_assert_cmpstr (nm_ip_route_get_next_hop (route), ==, next_hop); + g_assert_cmpint (nm_ip_route_get_metric (route), ==, metric); } static void -check_ip6_route (NMSettingIP6Config *config, int idx, const char *destination_str, int plen, - const char *next_hop_str, int metric) +check_ip6_route (NMSettingIP6Config *config, int idx, const char *destination, int plen, + const char *next_hop, int metric) { - NMIP6Route *route = nm_setting_ip6_config_get_route (config, idx); - struct in6_addr destination; - struct in6_addr next_hop; - - g_assert (inet_pton (AF_INET6, destination_str, &destination) == 1); - g_assert (inet_pton (AF_INET6, next_hop_str, &next_hop) == 1); + NMIPRoute *route = nm_setting_ip6_config_get_route (config, idx); g_assert (route); - g_assert (!memcmp (nm_ip6_route_get_dest (route), &destination, sizeof(destination))); - g_assert (nm_ip6_route_get_prefix (route) == plen); - g_assert (!memcmp (nm_ip6_route_get_next_hop (route), &next_hop, sizeof(next_hop))); - g_assert (nm_ip6_route_get_metric (route) == metric); + g_assert_cmpstr (nm_ip_route_get_dest (route), ==, destination); + g_assert_cmpint (nm_ip_route_get_prefix (route), ==, plen); + g_assert_cmpstr (nm_ip_route_get_next_hop (route), ==, next_hop); + g_assert_cmpint (nm_ip_route_get_metric (route), ==, metric); } static NMConnection * @@ -291,24 +273,24 @@ test_read_valid_wired_connection (void) check_ip4_address (s_ip4, 0, "2.3.4.5", 24, "2.3.4.6"); check_ip4_address (s_ip4, 1, "192.168.0.5", 24, "192.168.0.1"); check_ip4_address (s_ip4, 2, "1.2.3.4", 16, "1.2.1.1"); - check_ip4_address (s_ip4, 3, "3.4.5.6", 16, "0.0.0.0"); + check_ip4_address (s_ip4, 3, "3.4.5.6", 16, NULL); check_ip4_address (s_ip4, 4, "4.5.6.7", 24, "1.2.3.4"); - check_ip4_address (s_ip4, 5, "5.6.7.8", 24, "0.0.0.0"); + check_ip4_address (s_ip4, 5, "5.6.7.8", 24, NULL); /* IPv4 routes */ g_assert (nm_setting_ip4_config_get_num_routes (s_ip4) == 12); - check_ip4_route (s_ip4, 0, "5.6.7.8", 32, "0.0.0.0", 0); + check_ip4_route (s_ip4, 0, "5.6.7.8", 32, NULL, 0); check_ip4_route (s_ip4, 1, "1.2.3.0", 24, "2.3.4.8", 99); - check_ip4_route (s_ip4, 2, "1.1.1.2", 12, "0.0.0.0", 0); - check_ip4_route (s_ip4, 3, "1.1.1.3", 13, "0.0.0.0", 0); + check_ip4_route (s_ip4, 2, "1.1.1.2", 12, NULL, 0); + check_ip4_route (s_ip4, 3, "1.1.1.3", 13, NULL, 0); check_ip4_route (s_ip4, 4, "1.1.1.4", 14, "2.2.2.4", 0); check_ip4_route (s_ip4, 5, "1.1.1.5", 15, "2.2.2.5", 0); check_ip4_route (s_ip4, 6, "1.1.1.6", 16, "2.2.2.6", 0); - check_ip4_route (s_ip4, 7, "1.1.1.7", 17, "0.0.0.0", 0); - check_ip4_route (s_ip4, 8, "1.1.1.8", 18, "0.0.0.0", 0); - check_ip4_route (s_ip4, 9, "1.1.1.9", 19, "0.0.0.0", 0); - check_ip4_route (s_ip4, 10, "1.1.1.10", 20, "0.0.0.0", 0); - check_ip4_route (s_ip4, 11, "1.1.1.11", 21, "0.0.0.0", 21); + check_ip4_route (s_ip4, 7, "1.1.1.7", 17, NULL, 0); + check_ip4_route (s_ip4, 8, "1.1.1.8", 18, NULL, 0); + check_ip4_route (s_ip4, 9, "1.1.1.9", 19, NULL, 0); + check_ip4_route (s_ip4, 10, "1.1.1.10", 20, NULL, 0); + check_ip4_route (s_ip4, 11, "1.1.1.11", 21, NULL, 21); /* ===== IPv6 SETTING ===== */ @@ -371,25 +353,25 @@ test_read_valid_wired_connection (void) /* IPv6 addresses */ g_assert (nm_setting_ip6_config_get_num_addresses (s_ip6) == 10); check_ip6_address (s_ip6, 0, "2:3:4:5:6:7:8:9", 64, "2:3:4:5:1:2:3:4"); - check_ip6_address (s_ip6, 1, "abcd:1234:ffff::cdde", 64, "::"); - check_ip6_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96, "::"); - check_ip6_address (s_ip6, 3, "3:4:5:6:7:8:9:0", 128, "::"); - check_ip6_address (s_ip6, 4, "3:4:5:6:7:8:9:14", 64, "::"); - check_ip6_address (s_ip6, 5, "3:4:5:6:7:8:9:15", 64, "::"); - check_ip6_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66, "::"); - check_ip6_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67, "::"); - check_ip6_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68, "::"); - check_ip6_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, "1::09"); + check_ip6_address (s_ip6, 1, "abcd:1234:ffff::cdde", 64, NULL); + check_ip6_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96, NULL); + check_ip6_address (s_ip6, 3, "3:4:5:6:7:8:9:0", 128, NULL); + check_ip6_address (s_ip6, 4, "3:4:5:6:7:8:9:14", 64, NULL); + check_ip6_address (s_ip6, 5, "3:4:5:6:7:8:9:15", 64, NULL); + check_ip6_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66, NULL); + check_ip6_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67, NULL); + check_ip6_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68, NULL); + check_ip6_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, "1::9"); /* Route #1 */ g_assert (nm_setting_ip6_config_get_num_routes (s_ip6) == 7); check_ip6_route (s_ip6, 0, "d:e:f:0:1:2:3:4", 64, "f:e:d:c:1:2:3:4", 0); check_ip6_route (s_ip6, 1, "a:b:c:d::", 64, "f:e:d:c:1:2:3:4", 99); - check_ip6_route (s_ip6, 2, "8:7:6:5:4:3:2:1", 128, "::", 0); - check_ip6_route (s_ip6, 3, "6:7:8:9:0:1:2:3", 126, "::", 1); - check_ip6_route (s_ip6, 4, "7:8:9:0:1:2:3:4", 125, "::", 5); - check_ip6_route (s_ip6, 5, "8:9:0:1:2:3:4:5", 124, "::", 6); - check_ip6_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, "::", 0); + check_ip6_route (s_ip6, 2, "8:7:6:5:4:3:2:1", 128, NULL, 0); + check_ip6_route (s_ip6, 3, "6:7:8:9:0:1:2:3", 126, NULL, 1); + check_ip6_route (s_ip6, 4, "7:8:9:0:1:2:3:4", 125, NULL, 5); + check_ip6_route (s_ip6, 5, "8:9:0:1:2:3:4:5", 124, NULL, 6); + check_ip6_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, NULL, 0); g_object_unref (connection); } @@ -399,20 +381,13 @@ add_one_ip4_address (NMSettingIP4Config *s_ip4, const char *gw, guint32 prefix) { - guint32 tmp; - NMIP4Address *ip4_addr; - - ip4_addr = nm_ip4_address_new (); - nm_ip4_address_set_prefix (ip4_addr, prefix); - - inet_pton (AF_INET, addr, &tmp); - nm_ip4_address_set_address (ip4_addr, tmp); - - inet_pton (AF_INET, gw, &tmp); - nm_ip4_address_set_gateway (ip4_addr, tmp); + NMIPAddress *ip4_addr; + GError *error = NULL; + ip4_addr = nm_ip_address_new (AF_INET, addr, prefix, gw, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, ip4_addr); - nm_ip4_address_unref (ip4_addr); + nm_ip_address_unref (ip4_addr); } static void @@ -422,21 +397,13 @@ add_one_ip4_route (NMSettingIP4Config *s_ip4, guint32 prefix, guint32 metric) { - guint32 addr; - NMIP4Route *route; - - route = nm_ip4_route_new (); - nm_ip4_route_set_prefix (route, prefix); - nm_ip4_route_set_metric (route, metric); - - inet_pton (AF_INET, dest, &addr); - nm_ip4_route_set_dest (route, addr); - - inet_pton (AF_INET, nh, &addr); - nm_ip4_route_set_next_hop (route, addr); + NMIPRoute *route; + GError *error = NULL; + route = nm_ip_route_new (AF_INET, dest, prefix, nh, metric, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_route (s_ip4, route); - nm_ip4_route_unref (route); + nm_ip_route_unref (route); } static void @@ -445,22 +412,13 @@ add_one_ip6_address (NMSettingIP6Config *s_ip6, guint32 prefix, const char *gw) { - struct in6_addr tmp; - NMIP6Address *ip6_addr; - - ip6_addr = nm_ip6_address_new (); - nm_ip6_address_set_prefix (ip6_addr, prefix); - - inet_pton (AF_INET6, addr, &tmp); - nm_ip6_address_set_address (ip6_addr, &tmp); - - if (gw) { - inet_pton (AF_INET6, gw, &tmp); - nm_ip6_address_set_gateway (ip6_addr, &tmp); - } + NMIPAddress *ip6_addr; + GError *error = NULL; + ip6_addr = nm_ip_address_new (AF_INET6, addr, prefix, gw, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, ip6_addr); - nm_ip6_address_unref (ip6_addr); + nm_ip_address_unref (ip6_addr); } static void @@ -470,21 +428,13 @@ add_one_ip6_route (NMSettingIP6Config *s_ip6, guint32 prefix, guint32 metric) { - struct in6_addr addr; - NMIP6Route *route; - - route = nm_ip6_route_new (); - nm_ip6_route_set_prefix (route, prefix); - nm_ip6_route_set_metric (route, metric); - - inet_pton (AF_INET6, dest, &addr); - nm_ip6_route_set_dest (route, &addr); - - inet_pton (AF_INET6, nh, &addr); - nm_ip6_route_set_next_hop (route, &addr); + NMIPRoute *route; + GError *error = NULL; + route = nm_ip_route_new (AF_INET6, dest, prefix, nh, metric, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_route (s_ip6, route); - nm_ip6_route_unref (route); + nm_ip_route_unref (route); } @@ -515,9 +465,9 @@ test_write_wired_connection (void) const char *route2 = "1.1.1.1"; const char *route2_nh = "1.2.1.1"; const char *route3 = "2.2.2.2"; - const char *route3_nh = "0.0.0.0"; + const char *route3_nh = NULL; const char *route4 = "3.3.3.3"; - const char *route4_nh = "0.0.0.0"; + const char *route4_nh = NULL; const char *dns6_1 = "1::cafe"; const char *dns6_2 = "2::cafe"; const char *address6_1 = "abcd::beef"; diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 17d4d9581a..766eabf44f 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -97,12 +97,12 @@ write_array_of_uint (GKeyFile *file, } static void -ip4_dns_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +dns_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { char **list; @@ -114,51 +114,59 @@ ip4_dns_writer (GKeyFile *file, } static void -write_ip4_values (GKeyFile *file, - const char *setting_name, - GPtrArray *array, - gboolean is_route) +write_ip_values (GKeyFile *file, + const char *setting_name, + GPtrArray *array, + gboolean is_route) { GString *output; - int i; - guint32 addr, gw, plen, metric; + int family, i; + const char *addr, *gw; + guint32 plen, metric; char key_name[30], *key_name_idx; if (!array->len) return; + family = !strcmp (setting_name, NM_SETTING_IP4_CONFIG_SETTING_NAME) ? AF_INET : AF_INET6; + strcpy (key_name, is_route ? "route" : "address"); key_name_idx = key_name + strlen (key_name); output = g_string_sized_new (2*INET_ADDRSTRLEN + 10); for (i = 0; i < array->len; i++) { if (is_route) { - NMIP4Route *route = array->pdata[i]; + NMIPRoute *route = array->pdata[i]; - addr = nm_ip4_route_get_dest (route); - plen = nm_ip4_route_get_prefix (route); - gw = nm_ip4_route_get_next_hop (route); - metric = nm_ip4_route_get_metric (route); + addr = nm_ip_route_get_dest (route); + plen = nm_ip_route_get_prefix (route); + gw = nm_ip_route_get_next_hop (route); + metric = nm_ip_route_get_metric (route); } else { - NMIP4Address *address = array->pdata[i]; + NMIPAddress *address = array->pdata[i]; - addr = nm_ip4_address_get_address (address); - plen = nm_ip4_address_get_prefix (address); - gw = nm_ip4_address_get_gateway (address); + addr = nm_ip_address_get_address (address); + plen = nm_ip_address_get_prefix (address); + gw = nm_ip_address_get_gateway (address); metric = 0; } g_string_set_size (output, 0); - g_string_append_printf (output, "%s/%u", - nm_utils_inet4_ntop (addr, NULL), - (unsigned) plen); + g_string_append_printf (output, "%s/%u", addr, plen); if (metric || gw) { /* Older versions of the plugin do not support the form * "a.b.c.d/plen,,metric", so, we always have to write the - * gateway, even if it's 0.0.0.0. - * The current version support reading of the above form. */ - g_string_append_c (output, ','); - g_string_append (output, nm_utils_inet4_ntop (gw, NULL)); + * gateway, even if there isn't one. + * The current version supports reading of the above form. + */ + if (!gw) { + if (family == AF_INET) + gw = "0.0.0.0"; + else + gw = "::"; + } + + g_string_append_printf (output, ",%s", gw); if (metric) g_string_append_printf (output, ",%lu", (unsigned long) metric); } @@ -170,19 +178,19 @@ write_ip4_values (GKeyFile *file, } static void -ip4_addr_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +addr_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip4_values (file, setting_name, array, FALSE); + write_ip_values (file, setting_name, array, FALSE); } static void @@ -197,155 +205,22 @@ ip4_addr_label_writer (GKeyFile *file, } static void -ip4_route_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +route_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip4_values (file, setting_name, array, TRUE); -} - -static void -ip6_dns_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - char **list; - - list = g_value_get_boxed (value); - if (list && list[0]) { - nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, - (const char **) list, g_strv_length (list)); - } -} - -static char * -ip6_values_to_addr_prefix (const struct in6_addr *addr, guint prefix, const struct in6_addr *gw, - gboolean force_write_gateway) -{ - GString *ip6_str; - char buf[INET6_ADDRSTRLEN]; - - /* address */ - nm_utils_inet6_ntop (addr, buf); - - /* Enough space for the address, '/', and the prefix */ - ip6_str = g_string_sized_new ((INET6_ADDRSTRLEN * 2) + 5); - - /* prefix */ - g_string_append (ip6_str, buf); - g_string_append_printf (ip6_str, "/%u", prefix); - - /* gateway */ - nm_utils_inet6_ntop (gw, buf); - if (force_write_gateway || !IN6_IS_ADDR_UNSPECIFIED (gw)) - g_string_append_printf (ip6_str, ",%s", buf); - - return g_string_free (ip6_str, FALSE); + write_ip_values (file, setting_name, array, TRUE); } static void -ip6_addr_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - GPtrArray *array; - const char *setting_name = nm_setting_get_name (setting); - int i, j; - - array = (GPtrArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - for (i = 0, j = 1; i < array->len; i++) { - NMIP6Address *addr = array->pdata[i]; - char *key_name, *ip6_addr; - - ip6_addr = ip6_values_to_addr_prefix (nm_ip6_address_get_address (addr), - nm_ip6_address_get_prefix (addr), - nm_ip6_address_get_gateway (addr), - /* we allow omitting the gateway if it's :: */ - FALSE); - /* Write it out */ - key_name = g_strdup_printf ("address%d", j++); - nm_keyfile_plugin_kf_set_string (file, setting_name, key_name, ip6_addr); - g_free (key_name); - g_free (ip6_addr); - } -} - -static void -ip6_route_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - GPtrArray *array; - const char *setting_name = nm_setting_get_name (setting); - GString *output; - int i, j; - - array = (GPtrArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - for (i = 0, j = 1; i < array->len; i++) { - NMIP6Route *route = array->pdata[i]; - char *key_name; - char *addr_str; - guint metric; - - output = g_string_new (""); - - /* Metric */ - metric = nm_ip6_route_get_metric (route); - - /* Address, prefix and next hop - * We allow omitting the gateway ::, if we also omit the metric - * and force writing of the gateway, if we add a non zero metric. - * The current version of the reader also supports the syntax - * "a:b:c::/plen,,metric" for a gateway ::. - * As older versions of the plugin, cannot read this form, - * we always write the gateway, whenever we also write the metric. - * But if possible, we omit them both (",::,0") or only the metric - * (",0"). - **/ - addr_str = ip6_values_to_addr_prefix (nm_ip6_route_get_dest (route), - nm_ip6_route_get_prefix (route), - nm_ip6_route_get_next_hop (route), - metric != 0); - g_string_append (output, addr_str); - g_free (addr_str); - - if (metric != 0) - g_string_append_printf (output, ",%u", metric); - - /* Write it out */ - key_name = g_strdup_printf ("route%d", j++); - nm_keyfile_plugin_kf_set_string (file, setting_name, key_name, output->str); - g_free (key_name); - - g_string_free (output, TRUE); - } -} - - -static void write_hash_of_string (GKeyFile *file, NMSetting *setting, const char *key, @@ -704,25 +579,25 @@ static KeyWriter key_writers[] = { setting_alias_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES, - ip4_addr_writer }, + addr_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, "address-labels", ip4_addr_label_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ADDRESSES, - ip6_addr_writer }, + addr_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ROUTES, - ip4_route_writer }, + route_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ROUTES, - ip6_route_writer }, + route_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS, - ip4_dns_writer }, + dns_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS, - ip6_dns_writer }, + dns_writer }, { NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID, ssid_writer }, diff --git a/src/tests/test-general.c b/src/tests/test-general.c index e6bc207bd7..e16d702ddb 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -576,8 +576,8 @@ test_connection_no_match_ip4_addr (void) GSList *connections = NULL; NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; - NMIP4Address *nm_addr; - guint32 addr, gw; + NMIPAddress *nm_addr; + GError *error = NULL; orig = _match_connection_new (); copy = nm_simple_connection_new_clone (orig); @@ -604,28 +604,20 @@ test_connection_no_match_ip4_addr (void) g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); - nm_addr = nm_ip4_address_new (); - inet_pton (AF_INET, "1.1.1.4", &addr); - inet_pton (AF_INET, "1.1.1.254", &gw); - nm_ip4_address_set_address (nm_addr, addr); - nm_ip4_address_set_prefix (nm_addr, 24); - nm_ip4_address_set_gateway (nm_addr, gw); + nm_addr = nm_ip_address_new (AF_INET, "1.1.1.4", 24, "1.1.1.254", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, nm_addr); - nm_ip4_address_unref (nm_addr); + nm_ip_address_unref (nm_addr); s_ip4 = nm_connection_get_setting_ip4_config (copy); g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); - nm_addr = nm_ip4_address_new (); - inet_pton (AF_INET, "2.2.2.4", &addr); - inet_pton (AF_INET, "2.2.2.254", &gw); - nm_ip4_address_set_address (nm_addr, addr); - nm_ip4_address_set_prefix (nm_addr, 24); - nm_ip4_address_set_gateway (nm_addr, gw); + nm_addr = nm_ip_address_new (AF_INET, "2.2.2.4", 24, "2.2.2.254", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, nm_addr); - nm_ip4_address_unref (nm_addr); + nm_ip_address_unref (nm_addr); matched = nm_utils_match_connection (connections, orig, TRUE, NULL, NULL); g_assert (matched != copy); diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 46e14ca153..2f02706b1f 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -1233,13 +1233,13 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, routes = nm_utils_ip4_routes_from_gvalue (val); for (iter = routes; iter; iter = iter->next) { - NMIP4Route *item = iter->data; + NMIPRoute *item = iter->data; NMPlatformIP4Route route; memset (&route, 0, sizeof (route)); - route.network = nm_ip4_route_get_dest (item); - route.plen = nm_ip4_route_get_prefix (item); - route.gateway = nm_ip4_route_get_next_hop (item); + nm_ip_route_get_dest_binary (item, &route.network); + route.plen = nm_ip_route_get_prefix (item); + nm_ip_route_get_next_hop_binary (item, &route.gateway); route.source = NM_IP_CONFIG_SOURCE_VPN; route.metric = vpn_routing_metric (connection); @@ -1255,7 +1255,7 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, nm_ip4_config_add_route (config, &route); } - g_slist_free_full (routes, (GDestroyNotify) nm_ip4_route_unref); + g_slist_free_full (routes, (GDestroyNotify) nm_ip_route_unref); } val = (GValue *) g_hash_table_lookup (config_hash, NM_VPN_PLUGIN_IP4_CONFIG_NEVER_DEFAULT); @@ -1380,13 +1380,13 @@ nm_vpn_connection_ip6_config_get (DBusGProxy *proxy, routes = nm_utils_ip6_routes_from_gvalue (val); for (iter = routes; iter; iter = iter->next) { - NMIP6Route *item = iter->data; + NMIPRoute *item = iter->data; NMPlatformIP6Route route; memset (&route, 0, sizeof (route)); - route.network = *nm_ip6_route_get_dest (item); - route.plen = nm_ip6_route_get_prefix (item); - route.gateway = *nm_ip6_route_get_next_hop (item); + nm_ip_route_get_dest_binary (item, &route.network); + route.plen = nm_ip_route_get_prefix (item); + nm_ip_route_get_next_hop_binary (item, &route.gateway); route.source = NM_IP_CONFIG_SOURCE_VPN; route.metric = vpn_routing_metric (connection); @@ -1402,7 +1402,7 @@ nm_vpn_connection_ip6_config_get (DBusGProxy *proxy, nm_ip6_config_add_route (config, &route); } - g_slist_free_full (routes, (GDestroyNotify) nm_ip6_route_unref); + g_slist_free_full (routes, (GDestroyNotify) nm_ip_route_unref); } val = (GValue *) g_hash_table_lookup (config_hash, NM_VPN_PLUGIN_IP6_CONFIG_NEVER_DEFAULT); |