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.
This commit is contained in:
Dan Winship 2014-09-16 16:42:46 -04:00
parent 303e84e65e
commit 21c8a6b20e
51 changed files with 2816 additions and 4522 deletions

View file

@ -95,8 +95,6 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix)
char **dns, **wins; char **dns, **wins;
GString *tmp; GString *tmp;
GVariant *val; GVariant *val;
char str_addr[INET_ADDRSTRLEN];
char str_gw[INET_ADDRSTRLEN];
int i; int i;
if (ip4_config == NULL) 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); addresses = nm_utils_ip4_addresses_from_variant (val);
for (i = 0; i < addresses->len; i++) { for (i = 0; i < addresses->len; i++) {
NMIP4Address *addr = addresses->pdata[i]; NMIPAddress *addr = addresses->pdata[i];
guint32 ip_prefix = nm_ip4_address_get_prefix (addr); const char *gw;
char *addrtmp; char *addrtmp;
nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), str_addr); gw = nm_ip_address_get_gateway (addr);
nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), str_gw); 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); items = g_slist_prepend (items, addrtmp);
} }
if (addresses->len) 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); routes = nm_utils_ip4_routes_from_variant (val);
for (i = 0; i < routes->len; i++) { for (i = 0; i < routes->len; i++) {
NMIP4Route *route = routes->pdata[i]; NMIPRoute *route = routes->pdata[i];
guint32 ip_prefix = nm_ip4_route_get_prefix (route); const char *next_hop;
guint32 metric = nm_ip4_route_get_metric (route);
char *routetmp; char *routetmp;
nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), str_addr); next_hop = nm_ip_route_get_next_hop (route);
nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), str_gw); 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, routetmp);
} }
items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, routes->len)); 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; char **dns;
GString *tmp; GString *tmp;
GVariant *val; GVariant *val;
char str_addr[INET6_ADDRSTRLEN];
char str_gw[INET6_ADDRSTRLEN];
int i; int i;
if (ip6_config == NULL) 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); addresses = nm_utils_ip6_addresses_from_variant (val);
for (i = 0; i < addresses->len; i++) { for (i = 0; i < addresses->len; i++) {
NMIP6Address *addr = addresses->pdata[i]; NMIPAddress *addr = addresses->pdata[i];
guint32 ip_prefix = nm_ip6_address_get_prefix (addr); const char *gw;
char *addrtmp; char *addrtmp;
nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), str_addr); gw = nm_ip_address_get_gateway (addr);
nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), str_gw); 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); items = g_slist_prepend (items, addrtmp);
} }
if (addresses->len) 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); routes = nm_utils_ip6_routes_from_variant (val);
for (i = 0; i < routes->len; i++) { for (i = 0; i < routes->len; i++) {
NMIP6Route *route = routes->pdata[i]; NMIPRoute *route = routes->pdata[i];
guint32 ip_prefix = nm_ip6_route_get_prefix (route); const char *next_hop;
guint32 metric = nm_ip6_route_get_metric (route);
char *routetmp; char *routetmp;
nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), str_addr); next_hop = nm_ip_route_get_next_hop (route);
nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), str_gw); 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); items = g_slist_prepend (items, routetmp);
} }
if (routes->len) if (routes->len)

View file

@ -218,32 +218,29 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err
g_free (tmp); g_free (tmp);
if (g_strv_length (split) > 0) { 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++) { for (iter = split; iter && *iter; iter++) {
NMIP4Address *addr; NMIPAddress *addr;
guint32 a; char *ip, *prefix, *gw;
char *p;
if (strlen (g_strstrip (*iter)) == 0) if (strlen (g_strstrip (*iter)) == 0)
continue; continue;
addr = nm_ip4_address_new (); ip = *iter;
p = strchr (*iter, '/'); prefix = strchr (ip, '/');
g_assert (p); g_assert (prefix);
*p++ = '\0'; *prefix++ = '\0';
g_assert_cmpint (inet_pton (AF_INET, *iter, &a), ==, 1); gw = strchr (prefix, ' ');
nm_ip4_address_set_address (addr, a); g_assert (gw);
nm_ip4_address_set_prefix (addr, (guint) atoi (p)); gw++;
p = strchr (p, ' ');
g_assert (p);
p++;
g_assert_cmpint (inet_pton (AF_INET, p, &a), ==, 1);
nm_ip4_address_set_gateway (addr, a);
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); 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); g_free (tmp);
if (g_strv_length (split) > 0) { 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++) { for (iter = split; iter && *iter; iter++) {
NMIP4Route *route; NMIPRoute *route;
guint32 a; char *dest, *prefix, *next_hop, *metric;
char *p;
if (strlen (g_strstrip (*iter)) == 0) if (strlen (g_strstrip (*iter)) == 0)
continue; continue;
route = nm_ip4_route_new (); dest = *iter;
p = strchr (*iter, '/'); prefix = strchr (dest, '/');
g_assert (p); g_assert (prefix);
*p++ = '\0'; *prefix++ = '\0';
g_assert_cmpint (inet_pton (AF_INET, *iter, &a), ==, 1); next_hop = strchr (prefix, ' ');
nm_ip4_route_set_dest (route, a); g_assert (next_hop);
nm_ip4_route_set_prefix (route, (guint) atoi (p)); next_hop++;
p = strchr (p, ' '); metric = strchr (next_hop, ' ');
g_assert (p); g_assert (metric);
p++; metric++;
g_assert_cmpint (inet_pton (AF_INET, p, &a), ==, 1);
nm_ip4_route_set_next_hop (route, a);
p = strchr (p, ' ');
g_assert (p);
p++;
nm_ip4_route_set_metric (route, (guint) atoi (p));
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); g_ptr_array_add (routes, route);
} }

View file

@ -30,6 +30,8 @@
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
#include "nm-glib-compat.h"
#include "common.h" #include "common.h"
#include "utils.h" #include "utils.h"
@ -104,17 +106,17 @@ print_ip4_config (NMIP4Config *cfg4,
if (ptr_array) { if (ptr_array) {
addr_arr = g_new (char *, ptr_array->len + 1); addr_arr = g_new (char *, ptr_array->len + 1);
for (i = 0; i < ptr_array->len; i++) { for (i = 0; i < ptr_array->len; i++) {
NMIP4Address *addr = (NMIP4Address *) g_ptr_array_index (ptr_array, i); NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i);
guint32 prefix; const char *gw;
char *ip_str, *gw_str;
ip_str = nmc_ip4_address_as_string (nm_ip4_address_get_address (addr), NULL); gw = nm_ip_address_get_gateway (addr);
prefix = nm_ip4_address_get_prefix (addr); if (!gw)
gw_str = nmc_ip4_address_as_string (nm_ip4_address_get_gateway (addr), NULL); gw = "0.0.0.0";
addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", ip_str, prefix, gw_str); addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s",
g_free (ip_str); nm_ip_address_get_address (addr),
g_free (gw_str); nm_ip_address_get_prefix (addr),
gw);
} }
addr_arr[i] = NULL; addr_arr[i] = NULL;
} }
@ -124,18 +126,18 @@ print_ip4_config (NMIP4Config *cfg4,
if (ptr_array) { if (ptr_array) {
route_arr = g_new (char *, ptr_array->len + 1); route_arr = g_new (char *, ptr_array->len + 1);
for (i = 0; i < ptr_array->len; i++) { for (i = 0; i < ptr_array->len; i++) {
NMIP4Route *route = (NMIP4Route *) g_ptr_array_index (ptr_array, i); NMIPRoute *route = (NMIPRoute *) g_ptr_array_index (ptr_array, i);
guint32 prefix, metric; const char *next_hop;
char *dest_str, *nexthop_str;
dest_str = nmc_ip4_address_as_string (nm_ip4_route_get_dest (route), NULL); next_hop = nm_ip_route_get_next_hop (route);
nexthop_str = nmc_ip4_address_as_string (nm_ip4_route_get_next_hop (route), NULL); if (!next_hop)
prefix = nm_ip4_route_get_prefix (route); next_hop = "0.0.0.0";
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); route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u",
g_free (dest_str); nm_ip_route_get_dest (route),
g_free (nexthop_str); nm_ip_route_get_prefix (route),
next_hop,
nm_ip_route_get_metric (route));
} }
route_arr[i] = NULL; route_arr[i] = NULL;
} }
@ -196,17 +198,17 @@ print_ip6_config (NMIP6Config *cfg6,
if (ptr_array) { if (ptr_array) {
addr_arr = g_new (char *, ptr_array->len + 1); addr_arr = g_new (char *, ptr_array->len + 1);
for (i = 0; i < ptr_array->len; i++) { for (i = 0; i < ptr_array->len; i++) {
NMIP6Address *addr = (NMIP6Address *) g_ptr_array_index (ptr_array, i); NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i);
guint32 prefix; const char *gw;
char *ip_str, *gw_str;
ip_str = nmc_ip6_address_as_string (nm_ip6_address_get_address (addr), NULL); gw = nm_ip_address_get_gateway (addr);
prefix = nm_ip6_address_get_prefix (addr); if (!gw)
gw_str = nmc_ip6_address_as_string (nm_ip6_address_get_gateway (addr), NULL); gw = "::";
addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", ip_str, prefix, gw_str); addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s",
g_free (ip_str); nm_ip_address_get_address (addr),
g_free (gw_str); nm_ip_address_get_prefix (addr),
gw);
} }
addr_arr[i] = NULL; addr_arr[i] = NULL;
} }
@ -216,18 +218,18 @@ print_ip6_config (NMIP6Config *cfg6,
if (ptr_array) { if (ptr_array) {
route_arr = g_new (char *, ptr_array->len + 1); route_arr = g_new (char *, ptr_array->len + 1);
for (i = 0; i < ptr_array->len; i++) { for (i = 0; i < ptr_array->len; i++) {
NMIP6Route *route = (NMIP6Route *) g_ptr_array_index (ptr_array, i); NMIPRoute *route = (NMIPRoute *) g_ptr_array_index (ptr_array, i);
guint32 prefix, metric; const char *next_hop;
char *dest_str, *nexthop_str;
dest_str = nmc_ip6_address_as_string (nm_ip6_route_get_dest (route), NULL); next_hop = nm_ip_route_get_next_hop (route);
nexthop_str = nmc_ip6_address_as_string (nm_ip6_route_get_next_hop (route), NULL); if (!next_hop)
prefix = nm_ip6_route_get_prefix (route); next_hop = "::";
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); route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u",
g_free (dest_str); nm_ip_route_get_dest (route),
g_free (nexthop_str); nm_ip_route_get_prefix (route),
next_hop,
nm_ip_route_get_metric (route));
} }
route_arr[i] = NULL; route_arr[i] = NULL;
} }
@ -351,18 +353,20 @@ print_dhcp6_config (NMDhcp6Config *dhcp6,
} }
/* /*
* Parse IPv4 address from string to NMIP4Address stucture. * Parse IP address from string to NMIPAddress stucture.
* ip_str is the IPv4 address in the form address/prefix * ip_str is the IP address in the form address/prefix
* gw_str is the gateway address (it is optional) * gw_str is the gateway address (it is optional)
*/ */
NMIP4Address * NMIPAddress *
nmc_parse_and_build_ip4_address (const char *ip_str, const char *gw_str, GError **error) nmc_parse_and_build_address (int family, const char *ip_str, const char *gw_str, GError **error)
{ {
NMIP4Address *addr = NULL; int max_prefix = (family == AF_INET) ? 32 : 128;
guint32 ip4_addr, gw_addr; NMIPAddress *addr = NULL;
const char *ip;
char *tmp; char *tmp;
char *plen; char *plen;
long int prefix; long int prefix;
GError *local = NULL;
g_return_val_if_fail (ip_str != NULL, NULL); g_return_val_if_fail (ip_str != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == 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) if (plen)
*plen++ = '\0'; *plen++ = '\0';
if (inet_pton (AF_INET, tmp, &ip4_addr) < 1) { ip = tmp;
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("invalid IPv4 address '%s'"), tmp);
goto finish;
}
prefix = 32; prefix = max_prefix;
if (plen) { 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, 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; 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, g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("invalid gateway '%s'"), gw_str); _("invalid IP address: %s"), local->message);
goto finish; 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: finish:
g_free (tmp); g_free (tmp);
return addr; return addr;
} }
/* /*
* Parse IPv6 address from string to NMIP6Address stucture. * nmc_parse_and_build_route:
* 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:
* @family: AF_INET or AF_INET6 * @family: AF_INET or AF_INET6
* @first: the route destination in the form of "address/prefix" * @first: the route destination in the form of "address/prefix"
(/prefix is optional) (/prefix is optional)
* @second: (allow-none): next hop address, if third is not NULL. Otherwise it could be * @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). either next hop address or metric. (It can be NULL when @third is NULL).
* @third: (allow-none): route metric * @third: (allow-none): route metric
* @out: (out): route struct to fill
* @error: location to store GError * @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 * Returns: %TRUE on success, %FALSE on failure
*/ */
static gboolean NMIPRoute *
_parse_and_build_route (int family, nmc_parse_and_build_route (int family,
const char *first, const char *first,
const char *second, const char *second,
const char *third, const char *third,
ParsedRoute *out, GError **error)
GError **error)
{ {
int max_prefix; int max_prefix = (family == AF_INET) ? 32 : 128;
char *tmp, *plen; 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; gboolean success = FALSE;
GError *local = NULL;
g_return_val_if_fail (family == AF_INET || family == AF_INET6, FALSE); 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 (first != NULL, FALSE);
g_return_val_if_fail (second || !third, 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); g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
max_prefix = (family == AF_INET) ? 32 : 128; dest = g_strdup (first);
/* initialize default values */ plen = strchr (dest, '/'); /* prefix delimiter */
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 */
if (plen) if (plen)
*plen++ = '\0'; *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 (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, g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("invalid prefix '%s'; <1-%d> allowed"), _("invalid prefix '%s'; <1-%d> allowed"),
plen, max_prefix); plen, max_prefix);
@ -532,113 +449,49 @@ _parse_and_build_route (int family,
} }
if (second) { if (second) {
if (inet_pton (family, second, family == AF_INET ? (void *) &out->nh.ip4_nh : (void *) &out->nh.ip6_nh) < 1) { if (third || nm_utils_ipaddr_valid (family, second))
if (third) { next_hop = second;
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, else {
_("invalid next hop address '%s'"), second); /* '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; 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 (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); g_set_error (error, 1, 0, _("invalid metric '%s'"), third);
goto finish; goto finish;
} }
} }
/* We don't accept default routes as NetworkManager handles it itself */ route = nm_ip_route_new (family, dest, prefix, next_hop, metric, &local);
if ( (family == AF_INET && out->dst.ip4_dst == 0) if (!route) {
|| (family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED (&out->dst.ip6_dst))) { 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, g_set_error_literal (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT,
_("default route cannot be added (NetworkManager handles it by itself)")); _("default route cannot be added (NetworkManager handles it by itself)"));
g_clear_pointer (&route, nm_ip_route_unref);
goto finish; goto finish;
} }
success = TRUE; success = TRUE;
finish: finish:
g_free (tmp); g_free (dest);
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);
}
return route; return route;
} }

View file

@ -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_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); 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); NMIPAddress *nmc_parse_and_build_address (int family, 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); NMIPRoute *nmc_parse_and_build_route (int family, const char *first, const char *second, const char *third, 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);
const char * nmc_device_state_to_string (NMDeviceState state); const char * nmc_device_state_to_string (NMDeviceState state);
const char * nmc_device_reason_to_string (NMDeviceStateReason reason); const char * nmc_device_reason_to_string (NMDeviceStateReason reason);

View file

@ -2887,7 +2887,7 @@ check_and_convert_vlan_prio_maps (const char *prio_map,
} }
static gboolean static gboolean
add_ip4_address_to_connection (NMIP4Address *ip4addr, NMConnection *connection) add_ip4_address_to_connection (NMIPAddress *ip4addr, NMConnection *connection)
{ {
NMSettingIP4Config *s_ip4; NMSettingIP4Config *s_ip4;
gboolean ret; gboolean ret;
@ -2904,13 +2904,13 @@ add_ip4_address_to_connection (NMIP4Address *ip4addr, NMConnection *connection)
NULL); NULL);
} }
ret = nm_setting_ip4_config_add_address (s_ip4, ip4addr); ret = nm_setting_ip4_config_add_address (s_ip4, ip4addr);
nm_ip4_address_unref (ip4addr); nm_ip_address_unref (ip4addr);
return ret; return ret;
} }
static gboolean static gboolean
add_ip6_address_to_connection (NMIP6Address *ip6addr, NMConnection *connection) add_ip6_address_to_connection (NMIPAddress *ip6addr, NMConnection *connection)
{ {
NMSettingIP6Config *s_ip6; NMSettingIP6Config *s_ip6;
gboolean ret; gboolean ret;
@ -2927,7 +2927,7 @@ add_ip6_address_to_connection (NMIP6Address *ip6addr, NMConnection *connection)
NULL); NULL);
} }
ret = nm_setting_ip6_config_add_address (s_ip6, ip6addr); ret = nm_setting_ip6_config_add_address (s_ip6, ip6addr);
nm_ip6_address_unref (ip6addr); nm_ip_address_unref (ip6addr);
return ret; return ret;
} }
@ -3841,9 +3841,9 @@ ask_for_ip_addresses (NMConnection *connection, int family)
char *str, *ip, *gw, *rest; char *str, *ip, *gw, *rest;
const char *prompt; const char *prompt;
gboolean added; gboolean added;
gpointer ipaddr; NMIPAddress *ipaddr;
if (family == 4) if (family == AF_INET)
prompt =_("IPv4 address (IP[/plen] [gateway]) [none]: "); prompt =_("IPv4 address (IP[/plen] [gateway]) [none]: ");
else else
prompt =_("IPv6 address (IP[/plen] [gateway]) [none]: "); prompt =_("IPv6 address (IP[/plen] [gateway]) [none]: ");
@ -3853,16 +3853,13 @@ ask_for_ip_addresses (NMConnection *connection, int family)
str = nmc_readline ("%s", prompt); str = nmc_readline ("%s", prompt);
split_address (str, &ip, &gw, &rest); split_address (str, &ip, &gw, &rest);
if (ip) { if (ip) {
if (family == 4) ipaddr = nmc_parse_and_build_address (family, ip, gw, &error);
ipaddr = nmc_parse_and_build_ip4_address (ip, gw, &error);
else
ipaddr = nmc_parse_and_build_ip6_address (ip, gw, &error);
if (ipaddr) { if (ipaddr) {
if (family == 4) if (family == AF_INET)
added = add_ip4_address_to_connection ((NMIP4Address *) ipaddr, connection); added = add_ip4_address_to_connection (ipaddr, connection);
else else
added = add_ip6_address_to_connection ((NMIP6Address *) ipaddr, connection); added = add_ip6_address_to_connection (ipaddr, connection);
gw = gw ? gw : (family == 4) ? "0.0.0.0" : "::"; gw = gw ? gw : (family == AF_INET) ? "0.0.0.0" : "::";
if (added) if (added)
g_print (_(" Address successfully added: %s %s\n"), ip, gw); g_print (_(" Address successfully added: %s %s\n"), ip, gw);
else else
@ -3896,8 +3893,8 @@ do_questionnaire_ip (NMConnection *connection)
g_print (_("Press <Enter> to finish adding addresses.\n")); g_print (_("Press <Enter> to finish adding addresses.\n"));
ask_for_ip_addresses (connection, 4); ask_for_ip_addresses (connection, AF_INET);
ask_for_ip_addresses (connection, 6); ask_for_ip_addresses (connection, AF_INET6);
g_free (answer); g_free (answer);
return; return;
@ -5151,8 +5148,7 @@ cleanup_olpc:
&& strcmp (con_type, "team-slave") != 0 && strcmp (con_type, "team-slave") != 0
&& strcmp (con_type, "bridge-slave") != 0) { && strcmp (con_type, "bridge-slave") != 0) {
NMIP4Address *ip4addr = NULL; NMIPAddress *ip4addr = NULL, *ip6addr = NULL;
NMIP6Address *ip6addr = NULL;
const char *ip4 = NULL, *gw4 = NULL, *ip6 = NULL, *gw6 = NULL; const char *ip4 = NULL, *gw4 = NULL, *ip6 = NULL, *gw6 = NULL;
nmc_arg_t exp_args[] = { {"ip4", TRUE, &ip4, FALSE}, {"gw4", TRUE, &gw4, FALSE}, nmc_arg_t exp_args[] = { {"ip4", TRUE, &ip4, FALSE}, {"gw4", TRUE, &gw4, FALSE},
{"ip6", TRUE, &ip6, FALSE}, {"gw6", TRUE, &gw6, FALSE}, {"ip6", TRUE, &ip6, FALSE}, {"gw6", TRUE, &gw6, FALSE},
@ -5172,7 +5168,7 @@ cleanup_olpc:
/* coverity[dead_error_begin] */ /* coverity[dead_error_begin] */
if (ip4) { 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) { if (!ip4addr) {
g_prefix_error (error, _("Error: ")); g_prefix_error (error, _("Error: "));
return FALSE; return FALSE;
@ -5182,7 +5178,7 @@ cleanup_olpc:
/* coverity[dead_error_begin] */ /* coverity[dead_error_begin] */
if (ip6) { 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) { if (!ip6addr) {
g_prefix_error (error, _("Error: ")); g_prefix_error (error, _("Error: "));
return FALSE; return FALSE;

View file

@ -1209,8 +1209,7 @@ nmc_property_ipv4_get_addresses (NMSetting *setting)
NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting);
GString *printable; GString *printable;
guint32 num_addresses, i; guint32 num_addresses, i;
NMIP4Address *addr; NMIPAddress *addr;
char buf[INET_ADDRSTRLEN];
printable = g_string_new (NULL); printable = g_string_new (NULL);
@ -1223,14 +1222,13 @@ nmc_property_ipv4_get_addresses (NMSetting *setting)
g_string_append (printable, "{ "); g_string_append (printable, "{ ");
nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), buf); g_string_append_printf (printable, "ip = %s/%u",
g_string_append_printf (printable, "ip = %s", buf); 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_ip_address_get_gateway (addr)) {
g_string_append_printf (printable, ", gw = %s",
if (nm_ip4_address_get_gateway (addr)) { nm_ip_address_get_gateway (addr));
nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), buf);
g_string_append_printf (printable, ", gw = %s", buf);
} }
g_string_append (printable, " }"); g_string_append (printable, " }");
@ -1245,8 +1243,7 @@ nmc_property_ipv4_get_routes (NMSetting *setting)
NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting);
GString *printable; GString *printable;
guint32 num_routes, i; guint32 num_routes, i;
NMIP4Route *route; NMIPRoute *route;
char buf[INET_ADDRSTRLEN];
printable = g_string_new (NULL); printable = g_string_new (NULL);
@ -1259,18 +1256,17 @@ nmc_property_ipv4_get_routes (NMSetting *setting)
g_string_append (printable, "{ "); g_string_append (printable, "{ ");
nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), buf); g_string_append_printf (printable, "ip = %s/%u",
g_string_append_printf (printable, "ip = %s", buf); nm_ip_route_get_dest (route),
nm_ip_route_get_prefix (route));
g_string_append_printf (printable, "/%u", nm_ip4_route_get_prefix (route)); if (nm_ip_route_get_next_hop (route)) {
g_string_append_printf (printable, ", nh = %s",
if (nm_ip4_route_get_next_hop (route)) { nm_ip_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_ip4_route_get_metric (route)) if (nm_ip_route_get_metric (route))
g_string_append_printf (printable, ", mt = %u", nm_ip4_route_get_metric (route)); g_string_append_printf (printable, ", mt = %u", nm_ip_route_get_metric (route));
g_string_append (printable, " }"); g_string_append (printable, " }");
} }
@ -1297,8 +1293,7 @@ nmc_property_ipv6_get_addresses (NMSetting *setting)
NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting);
GString *printable; GString *printable;
guint32 num_addresses, i; guint32 num_addresses, i;
NMIP6Address *addr; NMIPAddress *addr;
char buf[INET6_ADDRSTRLEN];
printable = g_string_new (NULL); printable = g_string_new (NULL);
@ -1311,14 +1306,13 @@ nmc_property_ipv6_get_addresses (NMSetting *setting)
g_string_append (printable, "{ "); g_string_append (printable, "{ ");
nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), buf); g_string_append_printf (printable, "ip = %s/%u",
g_string_append_printf (printable, "ip = %s", buf); nm_ip_address_get_address (addr),
nm_ip_address_get_prefix (addr));
g_string_append_printf (printable, "/%u", nm_ip6_address_get_prefix (addr)); if (nm_ip_address_get_gateway (addr)) {
g_string_append_printf (printable, ", gw = %s",
if (nm_ip6_address_get_gateway (addr)) { nm_ip_address_get_gateway (addr));
nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), buf);
g_string_append_printf (printable, ", gw = %s", buf);
} }
g_string_append (printable, " }"); g_string_append (printable, " }");
@ -1333,8 +1327,7 @@ nmc_property_ipv6_get_routes (NMSetting *setting)
NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting);
GString *printable; GString *printable;
guint32 num_routes, i; guint32 num_routes, i;
NMIP6Route *route; NMIPRoute *route;
char buf[INET6_ADDRSTRLEN];
printable = g_string_new (NULL); printable = g_string_new (NULL);
@ -1347,18 +1340,17 @@ nmc_property_ipv6_get_routes (NMSetting *setting)
g_string_append (printable, "{ "); g_string_append (printable, "{ ");
nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), buf); g_string_append_printf (printable, "ip = %s/%u",
g_string_append_printf (printable, "ip = %s", buf); 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_ip_route_get_next_hop (route)) {
g_string_append_printf (printable, ", nh = %s",
if (nm_ip6_route_get_next_hop (route)) { nm_ip_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_ip6_route_get_metric (route)) if (nm_ip_route_get_metric (route))
g_string_append_printf (printable, ", mt = %u", nm_ip6_route_get_metric (route)); g_string_append_printf (printable, ", mt = %u", nm_ip_route_get_metric (route));
g_string_append (printable, " }"); g_string_append (printable, " }");
} }
@ -3074,6 +3066,51 @@ nmc_property_ib_set_p_key (NMSetting *setting, const char *prop, const char *val
return TRUE; 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 --- */ /* --- NM_SETTING_IP4_CONFIG_SETTING_NAME property setter functions --- */
/* 'method' */ /* 'method' */
static const char *ipv4_valid_methods[] = { 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) _validate_and_remove_ipv4_dns_search)
/* 'addresses' */ /* 'addresses' */
static NMIP4Address * static NMIPAddress *
_parse_ipv4_address (const char *address, GError **error) _parse_ipv4_address (const char *address, GError **error)
{ {
char *value = g_strdup (address); return _parse_ip_address (AF_INET, address, error);
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;
} }
static gboolean static gboolean
nmc_property_ipv4_set_addresses (NMSetting *setting, const char *prop, const char *val, GError **error) nmc_property_ipv4_set_addresses (NMSetting *setting, const char *prop, const char *val, GError **error)
{ {
char **strv = NULL, **iter; char **strv = NULL, **iter;
NMIP4Address *ip4addr; NMIPAddress *ip4addr;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); 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; return FALSE;
} }
nm_setting_ip4_config_add_address (NM_SETTING_IP4_CONFIG (setting), ip4addr); 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); g_strfreev (strv);
return TRUE; return TRUE;
@ -3242,7 +3264,7 @@ _validate_and_remove_ipv4_address (NMSettingIP4Config *setting,
const char *address, const char *address,
GError **error) GError **error)
{ {
NMIP4Address *ip4addr; NMIPAddress *ip4addr;
gboolean ret; gboolean ret;
ip4addr = _parse_ipv4_address (address, error); ip4addr = _parse_ipv4_address (address, error);
@ -3253,7 +3275,7 @@ _validate_and_remove_ipv4_address (NMSettingIP4Config *setting,
if (!ret) if (!ret)
g_set_error (error, 1, 0, g_set_error (error, 1, 0,
_("the property doesn't contain IP address '%s'"), address); _("the property doesn't contain IP address '%s'"), address);
nm_ip4_address_unref (ip4addr); nm_ip_address_unref (ip4addr);
return ret; return ret;
} }
DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_addresses, DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_addresses,
@ -3307,34 +3329,17 @@ nmc_property_out2in_addresses (const char *out_format)
} }
/* 'routes' */ /* 'routes' */
static NMIP4Route * static NMIPRoute *
_parse_ipv4_route (const char *route, GError **error) _parse_ipv4_route (const char *route, GError **error)
{ {
char *value = g_strdup (route); return _parse_ip_route (AF_INET, route, error);
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;
} }
static gboolean static gboolean
nmc_property_ipv4_set_routes (NMSetting *setting, const char *prop, const char *val, GError **error) nmc_property_ipv4_set_routes (NMSetting *setting, const char *prop, const char *val, GError **error)
{ {
char **strv = NULL, **iter; char **strv = NULL, **iter;
NMIP4Route *ip4route; NMIPRoute *ip4route;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); 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; return FALSE;
} }
nm_setting_ip4_config_add_route (NM_SETTING_IP4_CONFIG (setting), ip4route); 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); g_strfreev (strv);
return TRUE; return TRUE;
@ -3357,7 +3362,7 @@ _validate_and_remove_ipv4_route (NMSettingIP4Config *setting,
const char *route, const char *route,
GError **error) GError **error)
{ {
NMIP4Route *ip4route; NMIPRoute *ip4route;
gboolean ret; gboolean ret;
ip4route = _parse_ipv4_route (route, error); 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); ret = nm_setting_ip4_config_remove_route_by_value (setting, ip4route);
if (!ret) if (!ret)
g_set_error (error, 1, 0, _("the property doesn't contain route '%s'"), route); 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; return ret;
} }
DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_routes, 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) _validate_and_remove_ipv6_dns_search)
/* 'addresses' */ /* 'addresses' */
static NMIP6Address * static NMIPAddress *
_parse_ipv6_address (const char *address, GError **error) _parse_ipv6_address (const char *address, GError **error)
{ {
char *value = g_strstrip (g_strdup (address)); return _parse_ip_address (AF_INET6, address, error);
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;
} }
static gboolean static gboolean
nmc_property_ipv6_set_addresses (NMSetting *setting, const char *prop, const char *val, GError **error) nmc_property_ipv6_set_addresses (NMSetting *setting, const char *prop, const char *val, GError **error)
{ {
char **strv = NULL, **iter; char **strv = NULL, **iter;
NMIP6Address *ip6addr; NMIPAddress *ip6addr;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); 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; return FALSE;
} }
nm_setting_ip6_config_add_address (NM_SETTING_IP6_CONFIG (setting), ip6addr); 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); g_strfreev (strv);
return TRUE; return TRUE;
@ -3594,7 +3584,7 @@ _validate_and_remove_ipv6_address (NMSettingIP6Config *setting,
const char *address, const char *address,
GError **error) GError **error)
{ {
NMIP6Address *ip6addr; NMIPAddress *ip6addr;
gboolean ret; gboolean ret;
ip6addr = _parse_ipv6_address (address, error); 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); ret = nm_setting_ip6_config_remove_address_by_value (setting, ip6addr);
if (!ret) if (!ret)
g_set_error (error, 1, 0, _("the property doesn't contain IP address '%s'"), address); 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; return ret;
} }
DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_addresses, 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' */ /* 'routes' */
static NMIP6Route * static NMIPRoute *
_parse_ipv6_route (const char *route, GError **error) _parse_ipv6_route (const char *route, GError **error)
{ {
char *value = g_strdup (route); return _parse_ip_route (AF_INET6, route, error);
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;
} }
static gboolean static gboolean
nmc_property_ipv6_set_routes (NMSetting *setting, const char *prop, const char *val, GError **error) nmc_property_ipv6_set_routes (NMSetting *setting, const char *prop, const char *val, GError **error)
{ {
char **strv = NULL, **iter; char **strv = NULL, **iter;
NMIP6Route *ip6route; NMIPRoute *ip6route;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); 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; return FALSE;
} }
nm_setting_ip6_config_add_route (NM_SETTING_IP6_CONFIG (setting), ip6route); 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); g_strfreev (strv);
return TRUE; return TRUE;
@ -3673,7 +3646,7 @@ _validate_and_remove_ipv6_route (NMSettingIP6Config *setting,
const char *route, const char *route,
GError **error) GError **error)
{ {
NMIP6Route *ip6route; NMIPRoute *ip6route;
gboolean ret; gboolean ret;
ip6route = _parse_ipv6_route (route, error); 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); ret = nm_setting_ip6_config_remove_route_by_value (setting, ip6route);
if (!ret) if (!ret)
g_set_error (error, 1, 0, _("the property doesn't contain route '%s'"), route); 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; return ret;
} }
DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_routes, DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_routes,

File diff suppressed because it is too large Load diff

View file

@ -25,49 +25,27 @@ G_BEGIN_DECLS
void nm_editor_bindings_init (void); 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, const gchar *source_property,
gpointer target, gpointer target,
const gchar *target_property, const gchar *target_property,
GBindingFlags flags); 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, const gchar *source_property,
gpointer target, gpointer target,
const gchar *target_property, const gchar *target_property,
GBindingFlags flags); 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, const gchar *source_property,
gpointer target, gpointer target,
const gchar *target_property, const gchar *target_property,
GBindingFlags flags); GBindingFlags flags);
void nm_editor_bind_ip4_route_to_strings (gpointer source, void nm_editor_bind_ip_route_to_strings (int family,
const gchar *source_property, gpointer source,
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,
const gchar *source_property, const gchar *source_property,
gpointer dest_target, gpointer dest_target,
const gchar *dest_target_property, const gchar *dest_target_property,

View file

@ -138,21 +138,24 @@ nmt_page_ip4_constructed (GObject *object)
grid = NMT_PAGE_GRID (ip4); grid = NMT_PAGE_GRID (ip4);
widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4_WITH_PREFIX); 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, nm_editor_bind_ip_addresses_with_prefix_to_strv (AF_INET,
widget, "strings", s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); widget, "strings",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
nmt_page_grid_append (grid, _("Addresses"), widget, NULL); nmt_page_grid_append (grid, _("Addresses"), widget, NULL);
widget = nmt_ip_entry_new (25, AF_INET, FALSE, TRUE); widget = nmt_ip_entry_new (25, AF_INET, FALSE, TRUE);
nm_editor_bind_ip4_gateway_to_string (s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, nm_editor_bind_ip_gateway_to_string (AF_INET,
widget, "text", s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); widget, "text",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
nmt_page_grid_append (grid, _("Gateway"), widget, NULL); nmt_page_grid_append (grid, _("Gateway"), widget, NULL);
widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4);
nm_editor_bind_ip4_addresses_to_strv (s_ip4, NM_SETTING_IP4_CONFIG_DNS, nm_editor_bind_ip_addresses_to_strv (AF_INET,
widget, "strings", s_ip4, NM_SETTING_IP4_CONFIG_DNS,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); widget, "strings",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
nmt_page_grid_append (grid, _("DNS servers"), widget, NULL); nmt_page_grid_append (grid, _("DNS servers"), widget, NULL);
widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME); widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME);

View file

@ -138,21 +138,24 @@ nmt_page_ip6_constructed (GObject *object)
grid = NMT_PAGE_GRID (ip6); grid = NMT_PAGE_GRID (ip6);
widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6_WITH_PREFIX); 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, nm_editor_bind_ip_addresses_with_prefix_to_strv (AF_INET6,
widget, "strings", s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); widget, "strings",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
nmt_page_grid_append (grid, _("Addresses"), widget, NULL); nmt_page_grid_append (grid, _("Addresses"), widget, NULL);
widget = nmt_ip_entry_new (25, AF_INET6, FALSE, TRUE); widget = nmt_ip_entry_new (25, AF_INET6, FALSE, TRUE);
nm_editor_bind_ip6_gateway_to_string (s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, nm_editor_bind_ip_gateway_to_string (AF_INET6,
widget, "text", s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); widget, "text",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
nmt_page_grid_append (grid, _("Gateway"), widget, NULL); nmt_page_grid_append (grid, _("Gateway"), widget, NULL);
widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6);
nm_editor_bind_ip6_addresses_to_strv (s_ip6, NM_SETTING_IP6_CONFIG_DNS, nm_editor_bind_ip_addresses_to_strv (AF_INET6,
widget, "strings", s_ip6, NM_SETTING_IP6_CONFIG_DNS,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); widget, "strings",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
nmt_page_grid_append (grid, _("DNS servers"), widget, NULL); nmt_page_grid_append (grid, _("DNS servers"), widget, NULL);
widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME); widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME);

View file

@ -109,12 +109,12 @@ nmt_route_editor_constructed (GObject *object)
if (NM_IS_SETTING_IP4_CONFIG (priv->edit_setting)) { if (NM_IS_SETTING_IP4_CONFIG (priv->edit_setting)) {
routes = nmt_route_table_new (AF_INET); routes = nmt_route_table_new (AF_INET);
g_object_bind_property (priv->edit_setting, NM_SETTING_IP4_CONFIG_ROUTES, g_object_bind_property (priv->edit_setting, NM_SETTING_IP4_CONFIG_ROUTES,
routes, "ip4-routes", routes, "routes",
G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
} else { } else {
routes = nmt_route_table_new (AF_INET6); routes = nmt_route_table_new (AF_INET6);
g_object_bind_property (priv->edit_setting, NM_SETTING_IP6_CONFIG_ROUTES, g_object_bind_property (priv->edit_setting, NM_SETTING_IP6_CONFIG_ROUTES,
routes, "ip6-routes", routes, "routes",
G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
} }

View file

@ -49,8 +49,7 @@ typedef struct {
int family; int family;
int ip_entry_width, metric_entry_width; int ip_entry_width, metric_entry_width;
NMIP4Route *ip4_route; NMIPRoute *route;
NMIP6Route *ip6_route;
} NmtRouteEntryPrivate; } NmtRouteEntryPrivate;
enum { enum {
@ -58,8 +57,7 @@ enum {
PROP_FAMILY, PROP_FAMILY,
PROP_IP_ENTRY_WIDTH, PROP_IP_ENTRY_WIDTH,
PROP_METRIC_ENTRY_WIDTH, PROP_METRIC_ENTRY_WIDTH,
PROP_IP4_ROUTE, PROP_ROUTE,
PROP_IP6_ROUTE,
LAST_PROP LAST_PROP
}; };
@ -143,20 +141,12 @@ nmt_route_entry_constructed (GObject *object)
nmt_newt_grid_add (grid, priv->metric, 4, 0); nmt_newt_grid_add (grid, priv->metric, 4, 0);
nmt_newt_widget_set_padding (priv->metric, 1, 0, 0, 0); nmt_newt_widget_set_padding (priv->metric, 1, 0, 0, 0);
if (priv->family == AF_INET) { nm_editor_bind_ip_route_to_strings (priv->family,
nm_editor_bind_ip4_route_to_strings (object, "ip4-route", object, "route",
priv->dest, "text", priv->dest, "text",
priv->next_hop, "text", priv->next_hop, "text",
priv->metric, "text", priv->metric, "text",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); 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 ();
G_OBJECT_CLASS (nmt_route_entry_parent_class)->constructed (object); 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); NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE (object);
g_clear_pointer (&priv->ip4_route, nm_ip4_route_unref); g_clear_pointer (&priv->route, nm_ip_route_unref);
g_clear_pointer (&priv->ip6_route, nm_ip6_route_unref);
G_OBJECT_CLASS (nmt_route_entry_parent_class)->finalize (object); 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: case PROP_METRIC_ENTRY_WIDTH:
priv->metric_entry_width = g_value_get_int (value); priv->metric_entry_width = g_value_get_int (value);
break; break;
case PROP_IP4_ROUTE: case PROP_ROUTE:
g_return_if_fail (priv->family == AF_INET); if (priv->route)
if (priv->ip4_route) nm_ip_route_unref (priv->route);
nm_ip4_route_unref (priv->ip4_route); priv->route = g_value_dup_boxed (value);
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);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 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: case PROP_METRIC_ENTRY_WIDTH:
g_value_set_int (value, priv->metric_entry_width); g_value_set_int (value, priv->metric_entry_width);
break; break;
case PROP_IP4_ROUTE: case PROP_ROUTE:
g_return_if_fail (priv->family == AF_INET); g_value_set_boxed (value, priv->route);
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);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); 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_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS)); G_PARAM_STATIC_STRINGS));
/** /**
* NmtRouteEntry:ip4-route: * NmtRouteEntry:route:
* *
* The contents of the entries, as an #NMIP4Route. Only valid * The contents of the entries, as an #NMIPRoute.
* if #NmtRouteEntry:family is %AF_INET.
*/ */
g_object_class_install_property g_object_class_install_property
(object_class, PROP_IP4_ROUTE, (object_class, PROP_ROUTE,
g_param_spec_boxed ("ip4-route", "", "", g_param_spec_boxed ("route", "", "",
nm_ip4_route_get_type (), nm_ip_route_get_type (),
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* NmtRouteEntry:ip6-route:
*
* The contents of the entries, as an #NMIP6Route. Only valid
* if #NmtRouteEntry:family is %AF_INET6.
*/
g_object_class_install_property
(object_class, PROP_IP6_ROUTE,
g_param_spec_boxed ("ip6-route", "", "",
nm_ip6_route_get_type (),
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS)); G_PARAM_STATIC_STRINGS));
} }

View file

@ -54,8 +54,7 @@ typedef struct {
enum { enum {
PROP_0, PROP_0,
PROP_FAMILY, PROP_FAMILY,
PROP_IP4_ROUTES, PROP_ROUTES,
PROP_IP6_ROUTES,
LAST_PROP LAST_PROP
}; };
@ -85,7 +84,7 @@ route_list_transform_to_route (GBinding *binding,
NmtRouteTable *table = NMT_ROUTE_TABLE (g_binding_get_source (binding)); NmtRouteTable *table = NMT_ROUTE_TABLE (g_binding_get_source (binding));
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table);
int n = GPOINTER_TO_INT (user_data); int n = GPOINTER_TO_INT (user_data);
gpointer route; NMIPRoute *route;
if (n >= priv->routes->len) if (n >= priv->routes->len)
return FALSE; return FALSE;
@ -105,24 +104,17 @@ route_list_transform_from_route (GBinding *binding,
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table);
int n = GPOINTER_TO_INT (user_data); int n = GPOINTER_TO_INT (user_data);
GPtrArray *routes; GPtrArray *routes;
gpointer route; NMIPRoute *route;
if (n >= priv->routes->len) if (n >= priv->routes->len)
return FALSE; return FALSE;
route = priv->routes->pdata[n]; route = priv->routes->pdata[n];
routes = priv->routes; routes = priv->routes;
if (priv->family == AF_INET) priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref);
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);
if (route) { if (route)
if (priv->family == AF_INET) nm_ip_route_unref (route);
nm_ip4_route_unref (route);
else if (priv->family == AF_INET6)
nm_ip6_route_unref (route);
}
routes->pdata[n] = g_value_dup_boxed (source_value); routes->pdata[n] = g_value_dup_boxed (source_value);
g_value_take_boxed (target_value, routes); g_value_take_boxed (target_value, routes);
@ -141,21 +133,12 @@ create_route_entry (NmtWidgetList *list,
priv->ip_entry_width, priv->ip_entry_width,
priv->metric_entry_width); priv->metric_entry_width);
if (priv->family == AF_INET) { g_object_bind_property_full (table, "routes",
g_object_bind_property_full (table, "ip4-routes", entry, "route",
entry, "ip4-route", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE,
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE, route_list_transform_to_route,
route_list_transform_to_route, route_list_transform_from_route,
route_list_transform_from_route, GINT_TO_POINTER (num), NULL);
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);
}
return entry; return entry;
} }
@ -164,24 +147,15 @@ add_route (NmtWidgetList *list,
gpointer table) gpointer table)
{ {
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table);
NMIPRoute *route;
if (priv->family == AF_INET) { if (priv->family == AF_INET)
NMIP4Route *route; route = nm_ip_route_new (AF_INET, "0.0.0.0", 32, NULL, 0, NULL);
else
route = nm_ip4_route_new (); route = nm_ip_route_new (AF_INET6, "::", 128, NULL, 0, NULL);
nm_ip4_route_set_prefix (route, 32); g_ptr_array_add (priv->routes, route);
g_ptr_array_add (priv->routes, route); nmt_widget_list_set_length (list, priv->routes->len);
nmt_widget_list_set_length (list, priv->routes->len); g_object_notify (table, "routes");
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");
}
} }
static void static void
@ -190,7 +164,7 @@ remove_route (NmtWidgetList *list,
gpointer table) gpointer table)
{ {
NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table);
gpointer route; NMIPRoute *route;
if (num >= priv->routes->len) if (num >= priv->routes->len)
return; return;
@ -199,10 +173,7 @@ remove_route (NmtWidgetList *list,
g_ptr_array_remove_index (priv->routes, num); g_ptr_array_remove_index (priv->routes, num);
nmt_widget_list_set_length (list, priv->routes->len); nmt_widget_list_set_length (list, priv->routes->len);
if (priv->family == AF_INET) g_object_notify (table, "routes");
g_object_notify (table, "ip4-routes");
else
g_object_notify (table, "ip6-routes");
} }
static void static void
@ -214,6 +185,8 @@ nmt_route_table_init (NmtRouteTable *table)
int dest_prefix_width, next_hop_width, metric_width; int dest_prefix_width, next_hop_width, metric_width;
char *text; char *text;
priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref);
header = nmt_newt_grid_new (); header = nmt_newt_grid_new ();
text = g_strdup_printf ("%s/%s", _("Destination"), _("Prefix")); text = g_strdup_printf ("%s/%s", _("Destination"), _("Prefix"));
@ -283,27 +256,12 @@ nmt_route_table_set_property (GObject *object,
switch (prop_id) { switch (prop_id) {
case PROP_FAMILY: case PROP_FAMILY:
priv->family = g_value_get_int (value); 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; break;
case PROP_IP4_ROUTES: case PROP_ROUTES:
g_return_if_fail (priv->family == AF_INET);
array = g_value_get_boxed (value); array = g_value_get_boxed (value);
g_ptr_array_set_size (priv->routes, 0); g_ptr_array_set_size (priv->routes, 0);
for (i = 0; i < array->len; i++) { for (i = 0; i < array->len; i++) {
nm_ip4_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);
break;
case PROP_IP6_ROUTES:
g_return_if_fail (priv->family == AF_INET6);
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]);
g_ptr_array_add (priv->routes, 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); 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: case PROP_FAMILY:
g_value_set_int (value, priv->family); g_value_set_int (value, priv->family);
break; break;
case PROP_IP4_ROUTES: case PROP_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);
g_value_set_boxed (value, priv->routes); g_value_set_boxed (value, priv->routes);
break; break;
default: default:
@ -365,34 +318,16 @@ nmt_route_table_class_init (NmtRouteTableClass *table_class)
G_PARAM_CONSTRUCT_ONLY | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS)); G_PARAM_STATIC_STRINGS));
/** /**
* NmtRouteTable:ip4-routes: * NmtRouteTable:routes:
* *
* The array of routes, suitable for binding to * The array of routes, suitable for binding to #NMSettingIP4Config:routes
* #NMSettingIP4Config:routes. * or #NMSettingIP6Config:routes.
* *
* Only valid if #NmtRouteTable:family is %AF_INET * Element-type: NMIPRoute
*
* Element-type: NMIP4Route
*/ */
g_object_class_install_property g_object_class_install_property
(object_class, PROP_IP4_ROUTES, (object_class, PROP_ROUTES,
g_param_spec_boxed ("ip4-routes", "", "", g_param_spec_boxed ("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.
*
* Only valid if #NmtRouteTable:family is %AF_INET6
*
* Element-type: NMIP6Route
*/
g_object_class_install_property
(object_class, PROP_IP6_ROUTES,
g_param_spec_boxed ("ip6-routes", "", "",
G_TYPE_PTR_ARRAY, G_TYPE_PTR_ARRAY,
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS)); G_PARAM_STATIC_STRINGS));

View file

@ -87,6 +87,7 @@
<xi:include href="xml/nm-setting-generic.xml"/> <xi:include href="xml/nm-setting-generic.xml"/>
<xi:include href="xml/nm-setting-gsm.xml"/> <xi:include href="xml/nm-setting-gsm.xml"/>
<xi:include href="xml/nm-setting-infiniband.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-ip4-config.xml"/>
<xi:include href="xml/nm-setting-ip6-config.xml"/> <xi:include href="xml/nm-setting-ip6-config.xml"/>
<xi:include href="xml/nm-setting-olpc-mesh.xml"/> <xi:include href="xml/nm-setting-olpc-mesh.xml"/>

View file

@ -952,18 +952,6 @@ nmtst_assert_connection_unnormalizable (NMConnection *con,
#endif #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__ #ifdef __NM_UTILS_H__
static inline void static inline void
nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const char *expected, const char *loc) nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const char *expected, const char *loc)

View file

@ -24,6 +24,7 @@ libnm_core_headers = \
$(core)/nm-setting-generic.h \ $(core)/nm-setting-generic.h \
$(core)/nm-setting-gsm.h \ $(core)/nm-setting-gsm.h \
$(core)/nm-setting-infiniband.h \ $(core)/nm-setting-infiniband.h \
$(core)/nm-setting-ip-config.h \
$(core)/nm-setting-ip4-config.h \ $(core)/nm-setting-ip4-config.h \
$(core)/nm-setting-ip6-config.h \ $(core)/nm-setting-ip6-config.h \
$(core)/nm-setting-olpc-mesh.h \ $(core)/nm-setting-olpc-mesh.h \
@ -69,6 +70,7 @@ libnm_core_sources = \
$(core)/nm-setting-generic.c \ $(core)/nm-setting-generic.c \
$(core)/nm-setting-gsm.c \ $(core)/nm-setting-gsm.c \
$(core)/nm-setting-infiniband.c \ $(core)/nm-setting-infiniband.c \
$(core)/nm-setting-ip-config.c \
$(core)/nm-setting-ip4-config.c \ $(core)/nm-setting-ip4-config.c \
$(core)/nm-setting-ip6-config.c \ $(core)/nm-setting-ip6-config.c \
$(core)/nm-setting-olpc-mesh.c \ $(core)/nm-setting-olpc-mesh.c \

View file

@ -77,7 +77,7 @@
const char *_nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, const char *_nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting,
guint32 i); guint32 i);
gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting,
NMIP4Address *address, NMIPAddress *address,
const char *label); const char *label);
/* NM_SETTING_COMPARE_FLAG_INFERRABLE: check whether a device-generated /* NM_SETTING_COMPARE_FLAG_INFERRABLE: check whether a device-generated

View file

@ -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;
}

View file

@ -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 */

View file

@ -39,9 +39,6 @@
* properties related to IPv4 addressing, routing, and Domain Name Service * 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, G_DEFINE_TYPE_WITH_CODE (NMSettingIP4Config, nm_setting_ip4_config, NM_TYPE_SETTING,
_nm_register_setting (IP4_CONFIG, 4)) _nm_register_setting (IP4_CONFIG, 4))
NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP4_CONFIG) NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP4_CONFIG)
@ -52,9 +49,9 @@ typedef struct {
char *method; char *method;
GSList *dns; /* list of IP address strings */ GSList *dns; /* list of IP address strings */
GSList *dns_search; /* list of strings */ GSList *dns_search; /* list of strings */
GSList *addresses; /* array of NMIP4Address */ GSList *addresses; /* array of NMIPAddress */
GSList *address_labels; /* list of strings */ GSList *address_labels; /* list of strings */
GSList *routes; /* array of NMIP4Route */ GSList *routes; /* array of NMIPRoute */
gboolean ignore_auto_routes; gboolean ignore_auto_routes;
gboolean ignore_auto_dns; gboolean ignore_auto_dns;
char *dhcp_client_id; char *dhcp_client_id;
@ -424,7 +421,7 @@ nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting)
* *
* Returns: the address at index @i * Returns: the address at index @i
**/ **/
NMIP4Address * NMIPAddress *
nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i) nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i)
{ {
NMSettingIP4ConfigPrivate *priv; NMSettingIP4ConfigPrivate *priv;
@ -434,7 +431,7 @@ nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i)
priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); 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 * const char *
@ -463,18 +460,18 @@ _nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, guint32 i
**/ **/
gboolean gboolean
nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, nm_setting_ip4_config_add_address (NMSettingIP4Config *setting,
NMIP4Address *address) NMIPAddress *address)
{ {
return _nm_setting_ip4_config_add_address_with_label (setting, address, ""); return _nm_setting_ip4_config_add_address_with_label (setting, address, "");
} }
gboolean gboolean
_nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting,
NMIP4Address *address, NMIPAddress *address,
const char *label) const char *label)
{ {
NMSettingIP4ConfigPrivate *priv; NMSettingIP4ConfigPrivate *priv;
NMIP4Address *copy; NMIPAddress *copy;
GSList *iter; GSList *iter;
g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); 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); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { 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; return FALSE;
} }
copy = nm_ip4_address_dup (address); copy = nm_ip_address_dup (address);
priv->addresses = g_slist_append (priv->addresses, copy); priv->addresses = g_slist_append (priv->addresses, copy);
priv->address_labels = g_slist_append (priv->address_labels, g_strdup (label)); 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); label = g_slist_nth (priv->address_labels, i);
g_return_if_fail (addr != NULL && label != NULL); 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); priv->addresses = g_slist_delete_link (priv->addresses, addr);
g_free (label->data); g_free (label->data);
priv->address_labels = g_slist_delete_link (priv->address_labels, label); 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 gboolean
nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting,
NMIP4Address *address) NMIPAddress *address)
{ {
NMSettingIP4ConfigPrivate *priv; NMSettingIP4ConfigPrivate *priv;
GSList *iter; GSList *iter;
@ -544,8 +541,8 @@ nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting,
priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { 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)) {
nm_ip4_address_unref ((NMIP4Address *) iter->data); nm_ip_address_unref ((NMIPAddress *) iter->data);
priv->addresses = g_slist_delete_link (priv->addresses, iter); priv->addresses = g_slist_delete_link (priv->addresses, iter);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES);
return TRUE; 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_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; priv->addresses = NULL;
g_slist_free_full (priv->address_labels, g_free); g_slist_free_full (priv->address_labels, g_free);
priv->address_labels = NULL; priv->address_labels = NULL;
@ -595,7 +592,7 @@ nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting)
* *
* Returns: the route at index @i * Returns: the route at index @i
**/ **/
NMIP4Route * NMIPRoute *
nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i)
{ {
NMSettingIP4ConfigPrivate *priv; NMSettingIP4ConfigPrivate *priv;
@ -605,7 +602,7 @@ nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i)
priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); 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 gboolean
nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, nm_setting_ip4_config_add_route (NMSettingIP4Config *setting,
NMIP4Route *route) NMIPRoute *route)
{ {
NMSettingIP4ConfigPrivate *priv; NMSettingIP4ConfigPrivate *priv;
NMIP4Route *copy; NMIPRoute *copy;
GSList *iter; GSList *iter;
g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); 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); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
for (iter = priv->routes; iter; iter = g_slist_next (iter)) { 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; return FALSE;
} }
copy = nm_ip4_route_dup (route); copy = nm_ip_route_dup (route);
priv->routes = g_slist_append (priv->routes, copy); priv->routes = g_slist_append (priv->routes, copy);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES);
return TRUE; return TRUE;
@ -660,7 +657,7 @@ nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i)
elt = g_slist_nth (priv->routes, i); elt = g_slist_nth (priv->routes, i);
g_return_if_fail (elt != NULL); 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); priv->routes = g_slist_delete_link (priv->routes, elt);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); 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 gboolean
nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting,
NMIP4Route *route) NMIPRoute *route)
{ {
NMSettingIP4ConfigPrivate *priv; NMSettingIP4ConfigPrivate *priv;
GSList *iter; GSList *iter;
@ -686,8 +683,8 @@ nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting,
priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
for (iter = priv->routes; iter; iter = g_slist_next (iter)) { for (iter = priv->routes; iter; iter = g_slist_next (iter)) {
if (nm_ip4_route_compare ((NMIP4Route *) iter->data, route)) { if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) {
nm_ip4_route_unref ((NMIP4Route *) iter->data); nm_ip_route_unref ((NMIPRoute *) iter->data);
priv->routes = g_slist_delete_link (priv->routes, iter); priv->routes = g_slist_delete_link (priv->routes, iter);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES);
return TRUE; 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_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; priv->routes = NULL;
g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES);
} }
@ -871,7 +868,7 @@ static gboolean
verify (NMSetting *setting, NMConnection *connection, GError **error) verify (NMSetting *setting, NMConnection *connection, GError **error)
{ {
NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
GSList *iter, *l_iter; GSList *iter;
int i; int i;
if (!priv->method) { if (!priv->method) {
@ -957,33 +954,9 @@ verify (NMSetting *setting, NMConnection *connection, GError **error)
return FALSE; return FALSE;
} }
/* Validate addresses */ /* Validate address labels */
for (iter = priv->addresses, l_iter = priv->address_labels, i = 0; for (iter = priv->address_labels, i = 0; iter; iter = g_slist_next (iter), i++) {
iter && l_iter; const char *label = (const char *) iter->data;
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;
}
if (!verify_label (label)) { if (!verify_label (label)) {
g_set_error (error, 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, g_set_error (error,
NM_CONNECTION_ERROR, NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY, NM_CONNECTION_ERROR_INVALID_PROPERTY,
@ -1007,32 +980,6 @@ verify (NMSetting *setting, NMConnection *connection, GError **error)
return FALSE; 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 */ /* Validate DNS */
for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) { for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) {
const char *dns = (const char *) iter->data; 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, g_free);
g_slist_free_full (priv->dns_search, 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->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); 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)); priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value));
break; break;
case PROP_ADDRESSES: 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), 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)) { if (g_slist_length (priv->addresses) != g_slist_length (priv->address_labels)) {
g_slist_free_full (priv->address_labels, g_free); 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)); priv->address_labels = _nm_utils_strv_to_slist (g_value_get_boxed (value));
break; break;
case PROP_ROUTES: 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), priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value),
(NMUtilsCopyFunc) nm_ip4_route_dup); (NMUtilsCopyFunc) nm_ip_route_dup);
break; break;
case PROP_IGNORE_AUTO_ROUTES: case PROP_IGNORE_AUTO_ROUTES:
priv->ignore_auto_routes = g_value_get_boolean (value); 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)); g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search));
break; break;
case PROP_ADDRESSES: 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; break;
case PROP_ADDRESS_LABELS: case PROP_ADDRESS_LABELS:
g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->address_labels)); g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->address_labels));
break; break;
case PROP_ROUTES: 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; break;
case PROP_IGNORE_AUTO_ROUTES: case PROP_IGNORE_AUTO_ROUTES:
g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_routes (setting)); 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 * with the "shared", "link-local", or "disabled" methods as addressing is
* either automatic or disabled with these methods. * either automatic or disabled with these methods.
* *
* Element-Type: NMIP4Address * Element-Type: NMIPAddress
**/ **/
g_object_class_install_property g_object_class_install_property
(object_class, PROP_ADDRESSES, (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 * the 'shared', 'link-local', or 'disabled' methods because there is no
* upstream network. * upstream network.
* *
* Element-Type: NMIP4Route * Element-Type: NMIPRoute
**/ **/
g_object_class_install_property g_object_class_install_property
(object_class, PROP_ROUTES, (object_class, PROP_ROUTES,
@ -1482,463 +1429,3 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class)
G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS)); 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;
}

View file

@ -28,6 +28,7 @@
#endif #endif
#include "nm-setting.h" #include "nm-setting.h"
#include "nm-setting-ip-config.h"
G_BEGIN_DECLS G_BEGIN_DECLS
@ -97,57 +98,6 @@ G_BEGIN_DECLS
*/ */
#define NM_SETTING_IP4_CONFIG_METHOD_DISABLED "disabled" #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 { struct _NMSettingIP4Config {
NMSetting parent; 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); void nm_setting_ip4_config_clear_dns_searches (NMSettingIP4Config *setting);
guint32 nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting); guint32 nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting);
NMIP4Address *nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i); NMIPAddress * nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i);
gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIP4Address *address); gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIPAddress *address);
void nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i); 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); void nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting);
guint32 nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting); guint32 nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting);
NMIP4Route * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i); NMIPRoute * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i);
gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, NMIP4Route *route); gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, NMIPRoute *route);
void nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i); 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); void nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting);
gboolean nm_setting_ip4_config_get_ignore_auto_routes (NMSettingIP4Config *setting); gboolean nm_setting_ip4_config_get_ignore_auto_routes (NMSettingIP4Config *setting);

View file

@ -37,9 +37,6 @@
* properties related to IPv6 addressing, routing, and Domain Name Service * 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, G_DEFINE_TYPE_WITH_CODE (NMSettingIP6Config, nm_setting_ip6_config, NM_TYPE_SETTING,
_nm_register_setting (IP6_CONFIG, 4)) _nm_register_setting (IP6_CONFIG, 4))
NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP6_CONFIG) NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP6_CONFIG)
@ -51,8 +48,8 @@ typedef struct {
char *dhcp_hostname; char *dhcp_hostname;
GSList *dns; /* array of struct in6_addr */ GSList *dns; /* array of struct in6_addr */
GSList *dns_search; /* list of strings */ GSList *dns_search; /* list of strings */
GSList *addresses; /* array of NMIP6Address */ GSList *addresses; /* array of NMIPAddress */
GSList *routes; /* array of NMIP6Route */ GSList *routes; /* array of NMIPRoute */
gboolean ignore_auto_routes; gboolean ignore_auto_routes;
gboolean ignore_auto_dns; gboolean ignore_auto_dns;
gboolean never_default; gboolean never_default;
@ -437,7 +434,7 @@ nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting)
* *
* Returns: the address at index @i * Returns: the address at index @i
**/ **/
NMIP6Address * NMIPAddress *
nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i)
{ {
NMSettingIP6ConfigPrivate *priv; NMSettingIP6ConfigPrivate *priv;
@ -447,7 +444,7 @@ nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i)
priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); 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 gboolean
nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, nm_setting_ip6_config_add_address (NMSettingIP6Config *setting,
NMIP6Address *address) NMIPAddress *address)
{ {
NMSettingIP6ConfigPrivate *priv; NMSettingIP6ConfigPrivate *priv;
NMIP6Address *copy; NMIPAddress *copy;
GSList *iter; GSList *iter;
g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); 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); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { 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; return FALSE;
} }
copy = nm_ip6_address_dup (address); copy = nm_ip_address_dup (address);
priv->addresses = g_slist_append (priv->addresses, copy); priv->addresses = g_slist_append (priv->addresses, copy);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES);
return TRUE; return TRUE;
@ -503,7 +500,7 @@ nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i)
elt = g_slist_nth (priv->addresses, i); elt = g_slist_nth (priv->addresses, i);
g_return_if_fail (elt != NULL); 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); priv->addresses = g_slist_delete_link (priv->addresses, elt);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); 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 gboolean
nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting,
NMIP6Address *address) NMIPAddress *address)
{ {
NMSettingIP6ConfigPrivate *priv; NMSettingIP6ConfigPrivate *priv;
GSList *iter; GSList *iter;
@ -529,7 +526,7 @@ nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting,
priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { 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); priv->addresses = g_slist_delete_link (priv->addresses, iter);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES);
return TRUE; 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_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; priv->addresses = NULL;
g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); 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 * Returns: the route at index @i
**/ **/
NMIP6Route * NMIPRoute *
nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i)
{ {
NMSettingIP6ConfigPrivate *priv; NMSettingIP6ConfigPrivate *priv;
@ -587,7 +584,7 @@ nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i)
priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); 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 gboolean
nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, nm_setting_ip6_config_add_route (NMSettingIP6Config *setting,
NMIP6Route *route) NMIPRoute *route)
{ {
NMSettingIP6ConfigPrivate *priv; NMSettingIP6ConfigPrivate *priv;
NMIP6Route *copy; NMIPRoute *copy;
GSList *iter; GSList *iter;
g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); 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); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
for (iter = priv->routes; iter; iter = g_slist_next (iter)) { 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; return FALSE;
} }
copy = nm_ip6_route_dup (route); copy = nm_ip_route_dup (route);
priv->routes = g_slist_append (priv->routes, copy); priv->routes = g_slist_append (priv->routes, copy);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES);
return TRUE; return TRUE;
@ -642,7 +639,7 @@ nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i)
elt = g_slist_nth (priv->routes, i); elt = g_slist_nth (priv->routes, i);
g_return_if_fail (elt != NULL); 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); priv->routes = g_slist_delete_link (priv->routes, elt);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); 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 gboolean
nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting,
NMIP6Route *route) NMIPRoute *route)
{ {
NMSettingIP6ConfigPrivate *priv; NMSettingIP6ConfigPrivate *priv;
GSList *iter; GSList *iter;
@ -668,8 +665,8 @@ nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting,
priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting);
for (iter = priv->routes; iter; iter = g_slist_next (iter)) { 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)) {
nm_ip6_route_unref ((NMIP6Route *) iter->data); nm_ip_route_unref ((NMIPRoute *) iter->data);
priv->routes = g_slist_delete_link (priv->routes, iter); priv->routes = g_slist_delete_link (priv->routes, iter);
g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES);
return TRUE; 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_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; priv->routes = NULL;
g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); 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, g_free);
g_slist_free_full (priv->dns_search, g_free); g_slist_free_full (priv->dns_search, g_free);
g_slist_free_full (priv->addresses, g_free); g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref);
g_slist_free_full (priv->routes, g_free); g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref);
G_OBJECT_CLASS (nm_setting_ip6_config_parent_class)->finalize (object); 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)); priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value));
break; break;
case PROP_ADDRESSES: 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), priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value),
(NMUtilsCopyFunc) nm_ip6_address_dup); (NMUtilsCopyFunc) nm_ip_address_dup);
break; break;
case PROP_ROUTES: 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), priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value),
(NMUtilsCopyFunc) nm_ip6_route_dup); (NMUtilsCopyFunc) nm_ip_route_dup);
break; break;
case PROP_IGNORE_AUTO_ROUTES: case PROP_IGNORE_AUTO_ROUTES:
priv->ignore_auto_routes = g_value_get_boolean (value); 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)); g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search));
break; break;
case PROP_ADDRESSES: 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; break;
case PROP_ROUTES: 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; break;
case PROP_IGNORE_AUTO_ROUTES: case PROP_IGNORE_AUTO_ROUTES:
g_value_set_boolean (value, priv->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 * be used with the 'shared' or 'link-local' methods as the interface is
* automatically assigned an address with these methods. * automatically assigned an address with these methods.
* *
* Element-Type: NMIP6Address * Element-Type: NMIPAddress
**/ **/
g_object_class_install_property g_object_class_install_property
(object_class, PROP_ADDRESSES, (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 * to those returned by automatic configuration. Routes cannot be used with
* the 'shared' or 'link-local' methods because there is no upstream network. * the 'shared' or 'link-local' methods because there is no upstream network.
* *
* Element-Type: NMIP6Route * Element-Type: NMIPRoute
**/ **/
g_object_class_install_property g_object_class_install_property
(object_class, PROP_ROUTES, (object_class, PROP_ROUTES,
@ -1259,472 +1256,3 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class)
G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS)); 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;
}

View file

@ -29,6 +29,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include "nm-setting.h" #include "nm-setting.h"
#include "nm-setting-ip-config.h"
G_BEGIN_DECLS G_BEGIN_DECLS
@ -127,57 +128,6 @@ typedef enum {
NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR = 2 NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR = 2
} NMSettingIP6ConfigPrivacy; } 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 { struct _NMSettingIP6Config {
NMSetting parent; 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); void nm_setting_ip6_config_clear_dns_searches (NMSettingIP6Config *setting);
guint32 nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting); guint32 nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting);
NMIP6Address * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i); NMIPAddress * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i);
gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, NMIP6Address *address); gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, NMIPAddress *address);
void nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i); 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); void nm_setting_ip6_config_clear_addresses (NMSettingIP6Config *setting);
guint32 nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting); guint32 nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting);
NMIP6Route * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i); NMIPRoute * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i);
gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, NMIP6Route *route); gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, NMIPRoute *route);
void nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i); 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); void nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting);
gboolean nm_setting_ip6_config_get_ignore_auto_routes (NMSettingIP6Config *setting); gboolean nm_setting_ip6_config_get_ignore_auto_routes (NMSettingIP6Config *setting);

View file

@ -40,29 +40,12 @@ GVariant * _nm_utils_bytes_to_dbus (const GValue *prop_value);
void _nm_utils_bytes_from_dbus (GVariant *dbus_value, void _nm_utils_bytes_from_dbus (GVariant *dbus_value,
GValue *prop_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); GSList * _nm_utils_strv_to_slist (char **strv);
char ** _nm_utils_slist_to_strv (GSList *slist); 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, char ** _nm_utils_strsplit_set (const char *str,
const char *delimiters, const char *delimiters,
int max_tokens); int max_tokens);

View file

@ -662,7 +662,7 @@ _nm_utils_slist_to_strv (GSList *slist)
{ {
GSList *iter; GSList *iter;
char **strv; char **strv;
int len, i = 0; int len, i;
len = g_slist_length (slist); len = g_slist_length (slist);
strv = g_new (char *, len + 1); strv = g_new (char *, len + 1);
@ -674,6 +674,40 @@ _nm_utils_slist_to_strv (GSList *slist)
return strv; 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: * _nm_utils_strsplit_set:
* @str: string to split * @str: string to split
@ -1100,11 +1134,12 @@ nm_utils_ip4_dns_from_variant (GVariant *value)
/** /**
* nm_utils_ip4_addresses_to_variant: * 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 * Utility function to convert a #GPtrArray of #NMIPAddress objects representing
* #GVariant of type 'aau' representing an array of NetworkManager IPv4 * IPv4 addresses into a #GVariant of type 'aau' representing an array of
* addresses (which are tuples of address, prefix, and gateway). * NetworkManager IPv4 addresses (which are tuples of address, prefix, and
* gateway).
* *
* Returns: (transfer none): a new floating #GVariant representing @addresses. * Returns: (transfer none): a new floating #GVariant representing @addresses.
**/ **/
@ -1118,12 +1153,18 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses)
if (addresses) { if (addresses) {
for (i = 0; i < addresses->len; i++) { for (i = 0; i < addresses->len; i++) {
NMIP4Address *addr = addresses->pdata[i]; NMIPAddress *addr = addresses->pdata[i];
guint32 array[3]; guint32 array[3];
array[0] = nm_ip4_address_get_address (addr); if (nm_ip_address_get_family (addr) != AF_INET)
array[1] = nm_ip4_address_get_prefix (addr); continue;
array[2] = nm_ip4_address_get_gateway (addr);
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_builder_add (&builder, "@au",
g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, 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 * Utility function to convert a #GVariant of type 'aau' representing a list of
* NetworkManager IPv4 addresses (which are tuples of address, prefix, and * 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 * Returns: (transfer full) (element-type NMIPAddress): a newly allocated
* #GPtrArray of #NMIP4Address objects * #GPtrArray of #NMIPAddress objects
**/ **/
GPtrArray * GPtrArray *
nm_utils_ip4_addresses_from_variant (GVariant *value) 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_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aau")), NULL);
g_variant_iter_init (&iter, value); 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)) { while (g_variant_iter_next (&iter, "@au", &addr_var)) {
const guint32 *addr_array; const guint32 *addr_array;
gsize length; gsize length;
NMIP4Address *addr; NMIPAddress *addr;
GError *error = NULL;
addr_array = g_variant_get_fixed_array (addr_var, &length, sizeof (guint32)); addr_array = g_variant_get_fixed_array (addr_var, &length, sizeof (guint32));
if (length < 3) { if (length < 3) {
@ -1169,12 +1211,15 @@ nm_utils_ip4_addresses_from_variant (GVariant *value)
continue; continue;
} }
addr = nm_ip4_address_new (); addr = nm_ip_address_new_binary (AF_INET,
nm_ip4_address_set_address (addr, addr_array[0]); &addr_array[0], addr_array[1], &addr_array[2],
nm_ip4_address_set_prefix (addr, addr_array[1]); &error);
nm_ip4_address_set_gateway (addr, addr_array[2]); if (addr)
g_ptr_array_add (addresses, 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); g_variant_unref (addr_var);
} }
@ -1183,11 +1228,12 @@ nm_utils_ip4_addresses_from_variant (GVariant *value)
/** /**
* nm_utils_ip4_routes_to_variant: * 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 * Utility function to convert a #GPtrArray of #NMIPRoute objects representing
* #GVariant of type 'aau' representing an array of NetworkManager IPv4 routes * IPv4 routes into a #GVariant of type 'aau' representing an array of
* (which are tuples of route, prefix, next hop, and metric). * NetworkManager IPv4 routes (which are tuples of route, prefix, next hop, and
* metric).
* *
* Returns: (transfer none): a new floating #GVariant representing @routes. * Returns: (transfer none): a new floating #GVariant representing @routes.
**/ **/
@ -1201,13 +1247,16 @@ nm_utils_ip4_routes_to_variant (GPtrArray *routes)
if (routes) { if (routes) {
for (i = 0; i < routes->len; i++) { for (i = 0; i < routes->len; i++) {
NMIP4Route *route = routes->pdata[i]; NMIPRoute *route = routes->pdata[i];
guint32 array[4]; guint32 array[4];
array[0] = nm_ip4_route_get_dest (route); if (nm_ip_route_get_family (route) != AF_INET)
array[1] = nm_ip4_route_get_prefix (route); continue;
array[2] = nm_ip4_route_get_next_hop (route);
array[3] = nm_ip4_route_get_metric (route); 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_builder_add (&builder, "@au",
g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, 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 * Utility function to convert a #GVariant of type 'aau' representing an array
* of NetworkManager IPv4 routes (which are tuples of route, prefix, next hop, * 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 * Returns: (transfer full) (element-type NMIPRoute): a newly allocated
* #GPtrArray of #NMIP4Route objects * #GPtrArray of #NMIPRoute objects
**/ **/
GPtrArray * GPtrArray *
nm_utils_ip4_routes_from_variant (GVariant *value) 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_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aau")), NULL);
g_variant_iter_init (&iter, value); 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)) { while (g_variant_iter_next (&iter, "@au", &route_var)) {
const guint32 *route_array; const guint32 *route_array;
gsize length; gsize length;
NMIP4Route *route; NMIPRoute *route;
GError *error = NULL;
route_array = g_variant_get_fixed_array (route_var, &length, sizeof (guint32)); route_array = g_variant_get_fixed_array (route_var, &length, sizeof (guint32));
if (length < 4) { if (length < 4) {
@ -1253,13 +1303,16 @@ nm_utils_ip4_routes_from_variant (GVariant *value)
continue; continue;
} }
route = nm_ip4_route_new (); route = nm_ip_route_new_binary (AF_INET,
nm_ip4_route_set_dest (route, route_array[0]); &route_array[0], route_array[1],
nm_ip4_route_set_prefix (route, route_array[1]); &route_array[2], route_array[3],
nm_ip4_route_set_next_hop (route, route_array[2]); &error);
nm_ip4_route_set_metric (route, route_array[3]); if (route)
g_ptr_array_add (routes, 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); g_variant_unref (route_var);
} }
@ -1412,11 +1465,12 @@ nm_utils_ip6_dns_from_variant (GVariant *value)
/** /**
* nm_utils_ip6_addresses_to_variant: * 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 * Utility function to convert a #GPtrArray of #NMIPAddress objects representing
* #GVariant of type 'a(ayuay)' representing an array of NetworkManager IPv6 * IPv6 addresses into a #GVariant of type 'a(ayuay)' representing an array of
* addresses (which are tuples of address, prefix, and gateway). * NetworkManager IPv6 addresses (which are tuples of address, prefix, and
* gateway).
* *
* Returns: (transfer none): a new floating #GVariant representing @addresses. * Returns: (transfer none): a new floating #GVariant representing @addresses.
**/ **/
@ -1430,17 +1484,24 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses)
if (addresses) { if (addresses) {
for (i = 0; i < addresses->len; i++) { 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; GVariant *ip, *gateway;
guint32 prefix; guint32 prefix;
ip = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, if (nm_ip_address_get_family (addr) != AF_INET6)
nm_ip6_address_get_address (addr), continue;
16, 1);
prefix = nm_ip6_address_get_prefix (addr); nm_ip_address_get_address_binary (addr, &ip_bytes);
gateway = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, ip = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &ip_bytes, 16, 1);
nm_ip6_address_get_gateway (addr),
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); 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 * Utility function to convert a #GVariant of type 'a(ayuay)' representing a
* list of NetworkManager IPv6 addresses (which are tuples of address, prefix, * 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 * Returns: (transfer full) (element-type NMIPAddress): a newly allocated
* #GPtrArray of #NMIP6Address objects * #GPtrArray of #NMIPAddress objects
**/ **/
GPtrArray * GPtrArray *
nm_utils_ip6_addresses_from_variant (GVariant *value) 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_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("a(ayuay)")), NULL);
g_variant_iter_init (&iter, value); 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)) { 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; const struct in6_addr *addr_bytes, *gateway_bytes;
gsize addr_len, gateway_len; gsize addr_len, gateway_len;
GError *error = NULL;
if ( !g_variant_is_of_type (addr_var, G_VARIANT_TYPE_BYTESTRING) if ( !g_variant_is_of_type (addr_var, G_VARIANT_TYPE_BYTESTRING)
|| !g_variant_is_of_type (gateway_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); __func__, (int) addr_len);
goto next; 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); gateway_bytes = g_variant_get_fixed_array (gateway_var, &gateway_len, 1);
if (gateway_len != 16) { if (gateway_len != 16) {
g_warning ("%s: ignoring invalid IP6 address of length %d", g_warning ("%s: ignoring invalid IP6 address of length %d",
@ -1502,11 +1559,13 @@ nm_utils_ip6_addresses_from_variant (GVariant *value)
goto next; goto next;
} }
addr = nm_ip6_address_new (); addr = nm_ip_address_new_binary (AF_INET6, addr_bytes, prefix, gateway_bytes, &error);
nm_ip6_address_set_address (addr, addr_bytes); if (addr)
nm_ip6_address_set_prefix (addr, prefix); g_ptr_array_add (addresses, addr);
nm_ip6_address_set_gateway (addr, gateway_bytes); else {
g_ptr_array_add (addresses, addr); g_warning ("Ignoring invalid IP4 address: %s", error->message);
g_clear_error (&error);
}
next: next:
g_variant_unref (addr_var); g_variant_unref (addr_var);
@ -1518,11 +1577,12 @@ nm_utils_ip6_addresses_from_variant (GVariant *value)
/** /**
* nm_utils_ip6_routes_to_variant: * 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 * Utility function to convert a #GPtrArray of #NMIPRoute objects representing
* #GVariant of type 'a(ayuayu)' representing an array of NetworkManager IPv6 * IPv6 routes into a #GVariant of type 'a(ayuayu)' representing an array of
* routes (which are tuples of route, prefix, next hop, and metric). * NetworkManager IPv6 routes (which are tuples of route, prefix, next hop, and
* metric).
* *
* Returns: (transfer none): a new floating #GVariant representing @routes. * Returns: (transfer none): a new floating #GVariant representing @routes.
**/ **/
@ -1536,18 +1596,20 @@ nm_utils_ip6_routes_to_variant (GPtrArray *routes)
if (routes) { if (routes) {
for (i = 0; i < routes->len; i++) { 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; GVariant *dest, *next_hop;
guint32 prefix, metric; guint32 prefix, metric;
dest = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, if (nm_ip_route_get_family (route) != AF_INET6)
nm_ip6_route_get_dest (route), continue;
16, 1);
prefix = nm_ip6_route_get_prefix (route); nm_ip_route_get_dest_binary (route, &dest_bytes);
next_hop = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, dest = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &dest_bytes, 16, 1);
nm_ip6_route_get_next_hop (route), prefix = nm_ip_route_get_prefix (route);
16, 1); nm_ip_route_get_next_hop_binary (route, &next_hop_bytes);
metric = nm_ip6_route_get_metric (route); 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); 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 * Utility function to convert a #GVariant of type 'a(ayuayu)' representing an
* array of NetworkManager IPv6 routes (which are tuples of route, prefix, next * 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 * Returns: (transfer full) (element-type NMIPRoute): a newly allocated
* #GPtrArray of #NMIP6Route objects * #GPtrArray of #NMIPRoute objects
**/ **/
GPtrArray * GPtrArray *
nm_utils_ip6_routes_from_variant (GVariant *value) 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); 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); g_variant_iter_init (&iter, value);
while (g_variant_iter_next (&iter, "(@ayu@ayu)", &dest_var, &prefix, &next_hop_var, &metric)) { 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) if ( !g_variant_is_of_type (dest_var, G_VARIANT_TYPE_BYTESTRING)
|| !g_variant_is_of_type (next_hop_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); __func__, (int) dest_len);
goto next; goto next;
} }
next_hop = g_variant_get_fixed_array (next_hop_var, &next_hop_len, 1); next_hop = g_variant_get_fixed_array (next_hop_var, &next_hop_len, 1);
if (next_hop_len != 16) { if (next_hop_len != 16) {
g_warning ("%s: ignoring invalid IP6 address of length %d", g_warning ("%s: ignoring invalid IP6 address of length %d",
@ -1604,12 +1668,13 @@ nm_utils_ip6_routes_from_variant (GVariant *value)
goto next; goto next;
} }
route = nm_ip6_route_new (); route = nm_ip_route_new_binary (AF_INET6, dest, prefix, next_hop, metric, &error);
nm_ip6_route_set_dest (route, dest); if (route)
nm_ip6_route_set_prefix (route, prefix); g_ptr_array_add (routes, route);
nm_ip6_route_set_next_hop (route, next_hop); else {
nm_ip6_route_set_metric (route, metric); g_warning ("Ignoring invalid IP6 route: %s", error->message);
g_ptr_array_add (routes, route); g_clear_error (&error);
}
next: next:
g_variant_unref (dest_var); g_variant_unref (dest_var);
@ -2750,6 +2815,28 @@ nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst)
INET6_ADDRSTRLEN); INET6_ADDRSTRLEN);
} }
/**
* 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: * nm_utils_check_virtual_device_compatibility:
* @virtual_type: a virtual connection type * @virtual_type: a virtual connection type
@ -2809,5 +2896,3 @@ nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_typ
return FALSE; return FALSE;
} }
} }

View file

@ -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_inet4_ntop (in_addr_t inaddr, char *dst);
const char *nm_utils_inet6_ntop (const struct in6_addr *in6addr, 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); gboolean nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_type);
G_END_DECLS G_END_DECLS

View file

@ -324,7 +324,7 @@ static void
test_setting_ip4_config_labels (void) test_setting_ip4_config_labels (void)
{ {
NMSettingIP4Config *s_ip4; NMSettingIP4Config *s_ip4;
NMIP4Address *addr; NMIPAddress *addr;
const char *label; const char *label;
GPtrArray *addrs; GPtrArray *addrs;
char **labels; char **labels;
@ -336,12 +336,11 @@ test_setting_ip4_config_labels (void)
NULL); NULL);
/* addr 1 */ /* addr 1 */
addr = nm_ip4_address_new (); addr = nm_ip_address_new (AF_INET, "1.1.1.1", 24, NULL, &error);
nm_ip4_address_set_address (addr, 0x01010101); g_assert_no_error (error);
nm_ip4_address_set_prefix (addr, 24);
nm_setting_ip4_config_add_address (s_ip4, addr); 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); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
@ -349,12 +348,11 @@ test_setting_ip4_config_labels (void)
g_assert_cmpstr (label, ==, ""); g_assert_cmpstr (label, ==, "");
/* addr 2 */ /* addr 2 */
addr = nm_ip4_address_new (); addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, NULL, &error);
nm_ip4_address_set_address (addr, 0x02020202); g_assert_no_error (error);
nm_ip4_address_set_prefix (addr, 24);
_nm_setting_ip4_config_add_address_with_label (s_ip4, addr, "eth0:1"); _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); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
@ -362,12 +360,11 @@ test_setting_ip4_config_labels (void)
g_assert_cmpstr (label, ==, "eth0:1"); g_assert_cmpstr (label, ==, "eth0:1");
/* addr 3 */ /* addr 3 */
addr = nm_ip4_address_new (); addr = nm_ip_address_new (AF_INET, "3.3.3.3", 24, NULL, &error);
nm_ip4_address_set_address (addr, 0x03030303); g_assert_no_error (error);
nm_ip4_address_set_prefix (addr, 24);
_nm_setting_ip4_config_add_address_with_label (s_ip4, addr, ""); _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); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
@ -380,12 +377,12 @@ test_setting_ip4_config_labels (void)
g_assert_no_error (error); g_assert_no_error (error);
addr = nm_setting_ip4_config_get_address (s_ip4, 0); 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); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0);
g_assert_cmpstr (label, ==, "eth0:1"); g_assert_cmpstr (label, ==, "eth0:1");
addr = nm_setting_ip4_config_get_address (s_ip4, 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); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1);
g_assert_cmpstr (label, ==, ""); 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); g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2);
addr = nm_setting_ip4_config_get_address (s_ip4, 0); 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); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0);
g_assert_cmpstr (label, ==, ""); g_assert_cmpstr (label, ==, "");
addr = nm_setting_ip4_config_get_address (s_ip4, 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); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1);
g_assert_cmpstr (label, ==, ""); 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); g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2);
addr = nm_setting_ip4_config_get_address (s_ip4, 0); 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); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0);
g_assert_cmpstr (label, ==, "eth0:1"); g_assert_cmpstr (label, ==, "eth0:1");
addr = nm_setting_ip4_config_get_address (s_ip4, 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); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1);
g_assert_cmpstr (label, ==, ""); g_assert_cmpstr (label, ==, "");
@ -2457,8 +2454,9 @@ test_setting_ip4_changed_signal (void)
NMConnection *connection; NMConnection *connection;
gboolean changed = FALSE; gboolean changed = FALSE;
NMSettingIP4Config *s_ip4; NMSettingIP4Config *s_ip4;
NMIP4Address *addr; NMIPAddress *addr;
NMIP4Route *route; NMIPRoute *route;
GError *error = NULL;
connection = nm_simple_connection_new (); connection = nm_simple_connection_new ();
g_signal_connect (connection, 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_add_dns_search (s_ip4, "foobar.com"));
ASSERT_CHANGED (nm_setting_ip4_config_clear_dns_searches (s_ip4)); ASSERT_CHANGED (nm_setting_ip4_config_clear_dns_searches (s_ip4));
addr = nm_ip4_address_new (); addr = nm_ip_address_new (AF_INET, "22.33.0.0", 24, NULL, &error);
nm_ip4_address_set_address (addr, 0x2233); g_assert_no_error (error);
nm_ip4_address_set_prefix (addr, 24);
ASSERT_CHANGED (nm_setting_ip4_config_add_address (s_ip4, addr)); ASSERT_CHANGED (nm_setting_ip4_config_add_address (s_ip4, addr));
ASSERT_CHANGED (nm_setting_ip4_config_remove_address (s_ip4, 0)); 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)); ASSERT_UNCHANGED (nm_setting_ip4_config_remove_address (s_ip4, 1));
g_test_assert_expected_messages (); g_test_assert_expected_messages ();
nm_setting_ip4_config_add_address (s_ip4, addr); nm_setting_ip4_config_add_address (s_ip4, addr);
ASSERT_CHANGED (nm_setting_ip4_config_clear_addresses (s_ip4)); ASSERT_CHANGED (nm_setting_ip4_config_clear_addresses (s_ip4));
route = nm_ip4_route_new (); route = nm_ip_route_new (AF_INET, "22.33.0.0", 24, NULL, 0, &error);
nm_ip4_route_set_dest (route, 0x2233); g_assert_no_error (error);
nm_ip4_route_set_prefix (route, 24);
ASSERT_CHANGED (nm_setting_ip4_config_add_route (s_ip4, route)); ASSERT_CHANGED (nm_setting_ip4_config_add_route (s_ip4, route));
ASSERT_CHANGED (nm_setting_ip4_config_remove_route (s_ip4, 0)); 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); nm_setting_ip4_config_add_route (s_ip4, route);
ASSERT_CHANGED (nm_setting_ip4_config_clear_routes (s_ip4)); ASSERT_CHANGED (nm_setting_ip4_config_clear_routes (s_ip4));
nm_ip4_address_unref (addr); nm_ip_address_unref (addr);
nm_ip4_route_unref (route); nm_ip_route_unref (route);
g_object_unref (connection); g_object_unref (connection);
} }
@ -2527,9 +2523,9 @@ test_setting_ip6_changed_signal (void)
NMConnection *connection; NMConnection *connection;
gboolean changed = FALSE; gboolean changed = FALSE;
NMSettingIP6Config *s_ip6; NMSettingIP6Config *s_ip6;
NMIP6Address *addr; NMIPAddress *addr;
NMIP6Route *route; NMIPRoute *route;
const struct in6_addr t = { { { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 } } }; GError *error = NULL;
connection = nm_simple_connection_new (); connection = nm_simple_connection_new ();
g_signal_connect (connection, 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"); nm_setting_ip6_config_add_dns_search (s_ip6, "foobar.com");
ASSERT_CHANGED (nm_setting_ip6_config_clear_dns_searches (s_ip6)); ASSERT_CHANGED (nm_setting_ip6_config_clear_dns_searches (s_ip6));
addr = nm_ip6_address_new (); addr = nm_ip_address_new (AF_INET6, "1:2:3::4:5:6", 64, NULL, &error);
nm_ip6_address_set_address (addr, &t); g_assert_no_error (error);
nm_ip6_address_set_prefix (addr, 64);
ASSERT_CHANGED (nm_setting_ip6_config_add_address (s_ip6, addr)); ASSERT_CHANGED (nm_setting_ip6_config_add_address (s_ip6, addr));
ASSERT_CHANGED (nm_setting_ip6_config_remove_address (s_ip6, 0)); 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); nm_setting_ip6_config_add_address (s_ip6, addr);
ASSERT_CHANGED (nm_setting_ip6_config_clear_addresses (s_ip6)); ASSERT_CHANGED (nm_setting_ip6_config_clear_addresses (s_ip6));
route = nm_ip6_route_new (); route = nm_ip_route_new (AF_INET6, "1:2:3::4:5:6", 128, NULL, 0, &error);
nm_ip6_route_set_dest (route, &t); g_assert_no_error (error);
nm_ip6_route_set_prefix (route, 128);
ASSERT_CHANGED (nm_setting_ip6_config_add_route (s_ip6, route)); ASSERT_CHANGED (nm_setting_ip6_config_add_route (s_ip6, route));
ASSERT_CHANGED (nm_setting_ip6_config_remove_route (s_ip6, 0)); 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); nm_setting_ip6_config_add_route (s_ip6, route);
ASSERT_CHANGED (nm_setting_ip6_config_clear_routes (s_ip6)); ASSERT_CHANGED (nm_setting_ip6_config_clear_routes (s_ip6));
nm_ip6_address_unref (addr); nm_ip_address_unref (addr);
nm_ip6_route_unref (route); nm_ip_route_unref (route);
g_object_unref (connection); g_object_unref (connection);
} }

View file

@ -63,6 +63,7 @@
#include <nm-setting-generic.h> #include <nm-setting-generic.h>
#include <nm-setting-gsm.h> #include <nm-setting-gsm.h>
#include <nm-setting-infiniband.h> #include <nm-setting-infiniband.h>
#include <nm-setting-ip-config.h>
#include <nm-setting-ip4-config.h> #include <nm-setting-ip4-config.h>
#include <nm-setting-ip6-config.h> #include <nm-setting-ip6-config.h>
#include <nm-setting-olpc-mesh.h> #include <nm-setting-olpc-mesh.h>

View file

@ -268,18 +268,6 @@ global:
nm_dhcp6_config_get_one_option; nm_dhcp6_config_get_one_option;
nm_dhcp6_config_get_options; nm_dhcp6_config_get_options;
nm_dhcp6_config_get_type; 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_addresses;
nm_ip4_config_get_domains; nm_ip4_config_get_domains;
nm_ip4_config_get_gateway; nm_ip4_config_get_gateway;
@ -288,32 +276,6 @@ global:
nm_ip4_config_get_searches; nm_ip4_config_get_searches;
nm_ip4_config_get_type; nm_ip4_config_get_type;
nm_ip4_config_get_wins_servers; 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_addresses;
nm_ip6_config_get_domains; nm_ip6_config_get_domains;
nm_ip6_config_get_gateway; nm_ip6_config_get_gateway;
@ -321,20 +283,40 @@ global:
nm_ip6_config_get_routes; nm_ip6_config_get_routes;
nm_ip6_config_get_searches; nm_ip6_config_get_searches;
nm_ip6_config_get_type; nm_ip6_config_get_type;
nm_ip6_route_compare; nm_ip_address_equal;
nm_ip6_route_dup; nm_ip_address_get_address;
nm_ip6_route_get_dest; nm_ip_address_get_address_binary;
nm_ip6_route_get_metric; nm_ip_address_get_family;
nm_ip6_route_get_next_hop; nm_ip_address_get_gateway;
nm_ip6_route_get_prefix; nm_ip_address_get_prefix;
nm_ip6_route_get_type; nm_ip_address_get_type;
nm_ip6_route_new; nm_ip_address_new;
nm_ip6_route_ref; nm_ip_address_new_binary;
nm_ip6_route_set_dest; nm_ip_address_ref;
nm_ip6_route_set_metric; nm_ip_address_set_address;
nm_ip6_route_set_next_hop; nm_ip_address_set_address_binary;
nm_ip6_route_set_prefix; nm_ip_address_set_gateway;
nm_ip6_route_unref; 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_get_type;
nm_manager_error_quark; nm_manager_error_quark;
nm_object_get_path; nm_object_get_path;
@ -833,6 +815,7 @@ global:
nm_utils_ip6_dns_to_variant; nm_utils_ip6_dns_to_variant;
nm_utils_ip6_routes_from_variant; nm_utils_ip6_routes_from_variant;
nm_utils_ip6_routes_to_variant; nm_utils_ip6_routes_to_variant;
nm_utils_ipaddr_valid;
nm_utils_is_empty_ssid; nm_utils_is_empty_ssid;
nm_utils_is_uuid; nm_utils_is_uuid;
nm_utils_rsa_key_encrypt; nm_utils_rsa_key_encrypt;

View file

@ -161,13 +161,13 @@ get_property (GObject *object,
break; break;
case PROP_ADDRESSES: case PROP_ADDRESSES:
g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_addresses (self), g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_addresses (self),
(NMUtilsCopyFunc) nm_ip4_address_dup, (NMUtilsCopyFunc) nm_ip_address_dup,
(GDestroyNotify) nm_ip4_address_unref)); (GDestroyNotify) nm_ip_address_unref));
break; break;
case PROP_ROUTES: case PROP_ROUTES:
g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_routes (self), g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_routes (self),
(NMUtilsCopyFunc) nm_ip4_route_dup, (NMUtilsCopyFunc) nm_ip_route_dup,
(GDestroyNotify) nm_ip4_route_unref)); (GDestroyNotify) nm_ip_route_unref));
break; break;
case PROP_NAMESERVERS: case PROP_NAMESERVERS:
g_value_set_boxed (value, (char **) nm_ip4_config_get_nameservers (self)); 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: * 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 g_object_class_install_property
(object_class, PROP_ADDRESSES, (object_class, PROP_ADDRESSES,
@ -232,7 +232,7 @@ nm_ip4_config_class_init (NMIP4ConfigClass *config_class)
/** /**
* NMIP4Config:routes: * 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 g_object_class_install_property
(object_class, PROP_ROUTES, (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). * Gets the IP4 addresses (containing the address, prefix, and gateway).
* *
* Returns: (element-type NMIP4Address) (transfer none): the #GPtrArray * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray
* containing #NMIP4Address<!-- -->es. This is the internal copy used by the * containing #NMIPAddress<!-- -->es. This is the internal copy used by the
* configuration and must not be modified. * configuration and must not be modified.
**/ **/
GPtrArray * GPtrArray *
@ -398,8 +398,8 @@ nm_ip4_config_get_wins_servers (NMIP4Config *config)
* *
* Gets the routes. * Gets the routes.
* *
* Returns: (element-type NMIP4Route) (transfer none): the #GPtrArray containing * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing
* #NMIP4Routes. This is the internal copy used by the configuration, and must * #NMIPRoutes. This is the internal copy used by the configuration, and must
* not be modified. * not be modified.
**/ **/
GPtrArray * GPtrArray *

View file

@ -135,8 +135,8 @@ nm_ip6_config_get_gateway (NMIP6Config *config)
* *
* Gets the IP6 addresses (containing the address, prefix, and gateway). * Gets the IP6 addresses (containing the address, prefix, and gateway).
* *
* Returns: (element-type NMIP6Address) (transfer none): the #GPtrArray * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray
* containing #NMIP6Address<!-- -->es. This is the internal copy used by the * containing #NMIPAddress<!-- -->es. This is the internal copy used by the
* configuration and must not be modified. * configuration and must not be modified.
**/ **/
GPtrArray * GPtrArray *
@ -203,8 +203,8 @@ nm_ip6_config_get_searches (NMIP6Config *config)
* *
* Gets the routes. * Gets the routes.
* *
* Returns: (element-type NMIP6Route) (transfer none): the #GPtrArray containing * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing
* #NMIP6Routes. This is the internal copy used by the configuration, and must * #NMIPRoutes. This is the internal copy used by the configuration, and must
* not be modified. * not be modified.
**/ **/
GPtrArray * GPtrArray *
@ -246,13 +246,13 @@ get_property (GObject *object,
break; break;
case PROP_ADDRESSES: case PROP_ADDRESSES:
g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_addresses (self), g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_addresses (self),
(NMUtilsCopyFunc) nm_ip6_address_dup, (NMUtilsCopyFunc) nm_ip_address_dup,
(GDestroyNotify) nm_ip6_address_unref)); (GDestroyNotify) nm_ip_address_unref));
break; break;
case PROP_ROUTES: case PROP_ROUTES:
g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_routes (self), g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_routes (self),
(NMUtilsCopyFunc) nm_ip6_route_dup, (NMUtilsCopyFunc) nm_ip_route_dup,
(GDestroyNotify) nm_ip6_route_unref)); (GDestroyNotify) nm_ip_route_unref));
break; break;
case PROP_NAMESERVERS: case PROP_NAMESERVERS:
g_value_set_boxed (value, (char **) nm_ip6_config_get_nameservers (self)); 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: * NMIP6Config:addresses:
* *
* The #GPtrArray containing the IPv6 addresses (#NMIP6Address). * The #GPtrArray containing the IPv6 addresses (#NMIPAddress).
**/ **/
g_object_class_install_property g_object_class_install_property
(object_class, PROP_ADDRESSES, (object_class, PROP_ADDRESSES,
@ -326,7 +326,7 @@ nm_ip6_config_class_init (NMIP6ConfigClass *config_class)
/** /**
* NMIP6Config:routes: * NMIP6Config:routes:
* *
* The #GPtrArray containing the IPv6 routes (#NMIP6Route). * The #GPtrArray containing the IPv6 routes (#NMIPRoute).
**/ **/
g_object_class_install_property g_object_class_install_property
(object_class, PROP_ROUTES, (object_class, PROP_ROUTES,

View file

@ -2391,19 +2391,25 @@ nm_utils_ip4_routes_from_gvalue (const GValue *value)
routes = (GPtrArray *) g_value_get_boxed (value); routes = (GPtrArray *) g_value_get_boxed (value);
for (i = 0; routes && (i < routes->len); i++) { for (i = 0; routes && (i < routes->len); i++) {
GArray *array = (GArray *) g_ptr_array_index (routes, 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) { if (array->len < 4) {
g_warning ("Ignoring invalid IP4 route"); g_warning ("Ignoring invalid IP4 route");
continue; continue;
} }
route = nm_ip4_route_new (); route = nm_ip_route_new_binary (AF_INET,
nm_ip4_route_set_dest (route, g_array_index (array, guint32, 0)); &array_val[0], array_val[1],
nm_ip4_route_set_prefix (route, g_array_index (array, guint32, 1)); &array_val[2], array_val[3],
nm_ip4_route_set_next_hop (route, g_array_index (array, guint32, 2)); &error);
nm_ip4_route_set_metric (route, g_array_index (array, guint32, 3)); if (route)
list = g_slist_prepend (list, 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); 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); GValueArray *route_values = (GValueArray *) g_ptr_array_index (routes, i);
GByteArray *dest, *next_hop; GByteArray *dest, *next_hop;
guint prefix, metric; guint prefix, metric;
NMIP6Route *route; NMIPRoute *route;
GError *error = NULL;
if (!_nm_utils_gvalue_array_validate (route_values, 4, if (!_nm_utils_gvalue_array_validate (route_values, 4,
DBUS_TYPE_G_UCHAR_ARRAY, 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)); metric = g_value_get_uint (g_value_array_get_nth (route_values, 3));
route = nm_ip6_route_new (); route = nm_ip_route_new_binary (AF_INET6,
nm_ip6_route_set_dest (route, (struct in6_addr *)dest->data); dest->data, prefix,
nm_ip6_route_set_prefix (route, prefix); next_hop->data, metric,
nm_ip6_route_set_next_hop (route, (struct in6_addr *)next_hop->data); &error);
nm_ip6_route_set_metric (route, metric); if (route)
list = g_slist_prepend (list, 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); return g_slist_reverse (list);

View file

@ -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)) { if (s_ip4 && nm_setting_ip4_config_get_num_addresses (s_ip4)) {
/* Use the first user-supplied address */ /* 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); g_assert (user);
address->address = nm_ip4_address_get_address (user); nm_ip_address_get_address_binary (user, &address->address);
address->plen = nm_ip4_address_get_prefix (user); address->plen = nm_ip_address_get_prefix (user);
} else { } else {
/* Find an unused address in the 10.42.x.x range */ /* Find an unused address in the 10.42.x.x range */
guint32 start = (guint32) ntohl (0x0a2a0001); /* 10.42.0.1 */ guint32 start = (guint32) ntohl (0x0a2a0001); /* 10.42.0.1 */
@ -4650,8 +4650,7 @@ send_arps (NMDevice *self, const char *mode_arg)
NMConnection *connection; NMConnection *connection;
NMSettingIP4Config *s_ip4; NMSettingIP4Config *s_ip4;
int i, num; int i, num;
NMIP4Address *addr; NMIPAddress *addr;
guint32 ipaddr;
GError *error = NULL; GError *error = NULL;
connection = nm_device_get_connection (self); connection = nm_device_get_connection (self);
@ -4673,8 +4672,7 @@ send_arps (NMDevice *self, const char *mode_arg)
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
gs_free char *tmp_str = NULL; gs_free char *tmp_str = NULL;
addr = nm_setting_ip4_config_get_address (s_ip4, i); addr = nm_setting_ip4_config_get_address (s_ip4, i);
ipaddr = nm_ip4_address_get_address (addr); argv[ip_arg] = nm_ip_address_get_address (addr);
argv[ip_arg] = (char *) nm_utils_inet4_ntop (ipaddr, NULL);
_LOGD (LOGD_DEVICE | LOGD_IP4, _LOGD (LOGD_DEVICE | LOGD_IP4,
"arping: run %s", (tmp_str = g_strjoinv (" ", (char **) argv))); "arping: run %s", (tmp_str = g_strjoinv (" ", (char **) argv)));

View file

@ -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)) else if (nm_setting_ip4_config_get_ignore_auto_routes (setting))
nm_ip4_config_set_never_default (config, FALSE); nm_ip4_config_set_never_default (config, FALSE);
for (i = 0; i < naddresses; i++) { 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); nm_ip4_config_set_gateway (config, gateway);
break; break;
} }
@ -327,13 +329,13 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i
/* Addresses */ /* Addresses */
for (i = 0; i < naddresses; i++) { 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); const char *label = _nm_setting_ip4_config_get_address_label (setting, i);
NMPlatformIP4Address address; NMPlatformIP4Address address;
memset (&address, 0, sizeof (address)); memset (&address, 0, sizeof (address));
address.address = nm_ip4_address_get_address (s_addr); nm_ip_address_get_address_binary (s_addr, &address.address);
address.plen = nm_ip4_address_get_prefix (s_addr); address.plen = nm_ip_address_get_prefix (s_addr);
address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT; address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT;
address.preferred = NM_PLATFORM_LIFETIME_PERMANENT; address.preferred = NM_PLATFORM_LIFETIME_PERMANENT;
address.source = NM_IP_CONFIG_SOURCE_USER; 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)) if (nm_setting_ip4_config_get_ignore_auto_routes (setting))
nm_ip4_config_reset_routes (config); nm_ip4_config_reset_routes (config);
for (i = 0; i < nroutes; i++) { 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; NMPlatformIP4Route route;
memset (&route, 0, sizeof (route)); memset (&route, 0, sizeof (route));
route.network = nm_ip4_route_get_dest (s_route); nm_ip_route_get_dest_binary (s_route, &route.network);
route.plen = nm_ip4_route_get_prefix (s_route); route.plen = nm_ip_route_get_prefix (s_route);
route.gateway = nm_ip4_route_get_next_hop (s_route); nm_ip_route_get_next_hop_binary (s_route, &route.gateway);
route.metric = nm_ip4_route_get_metric (s_route); route.metric = nm_ip_route_get_metric (s_route);
if (!route.metric) if (!route.metric)
route.metric = default_route_metric; route.metric = default_route_metric;
route.source = NM_IP_CONFIG_SOURCE_USER; route.source = NM_IP_CONFIG_SOURCE_USER;
@ -406,7 +408,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config)
/* Addresses */ /* Addresses */
for (i = 0; i < naddresses; i++) { for (i = 0; i < naddresses; i++) {
const NMPlatformIP4Address *address = nm_ip4_config_get_address (config, i); const NMPlatformIP4Address *address = nm_ip4_config_get_address (config, i);
NMIP4Address *s_addr; NMIPAddress *s_addr;
/* Detect dynamic address */ /* Detect dynamic address */
if (address->lifetime != NM_PLATFORM_LIFETIME_PERMANENT) { if (address->lifetime != NM_PLATFORM_LIFETIME_PERMANENT) {
@ -418,18 +420,15 @@ nm_ip4_config_create_setting (const NMIP4Config *config)
if (!method) if (!method)
method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL; method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL;
s_addr = nm_ip4_address_new (); s_addr = nm_ip_address_new_binary (AF_INET, &address->address, address->plen, NULL, NULL);
nm_ip4_address_set_address (s_addr, address->address);
nm_ip4_address_set_prefix (s_addr, address->plen);
/* For backwards compatibility, attach the gateway to an address if it's /* For backwards compatibility, attach the gateway to an address if it's
* in the same subnet. * in the same subnet.
*/ */
if (same_prefix (address->address, gateway, address->plen)) 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_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 */ /* Use 'disabled' if the method wasn't previously set */
@ -440,7 +439,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config)
/* Routes */ /* Routes */
for (i = 0; i < nroutes; i++) { for (i = 0; i < nroutes; i++) {
const NMPlatformIP4Route *route = nm_ip4_config_get_route (config, i); const NMPlatformIP4Route *route = nm_ip4_config_get_route (config, i);
NMIP4Route *s_route; NMIPRoute *s_route;
/* Ignore default route. */ /* Ignore default route. */
if (!route->plen) if (!route->plen)
@ -450,14 +449,12 @@ nm_ip4_config_create_setting (const NMIP4Config *config)
if (route->source != NM_IP_CONFIG_SOURCE_USER) if (route->source != NM_IP_CONFIG_SOURCE_USER)
continue; continue;
s_route = nm_ip4_route_new (); s_route = nm_ip_route_new_binary (AF_INET,
nm_ip4_route_set_dest (s_route, route->network); &route->network, route->plen,
nm_ip4_route_set_prefix (s_route, route->plen); &route->gateway, route->metric,
nm_ip4_route_set_next_hop (s_route, route->gateway); NULL);
nm_ip4_route_set_metric (s_route, route->metric);
nm_setting_ip4_config_add_route (s_ip4, s_route); nm_setting_ip4_config_add_route (s_ip4, s_route);
nm_ip4_route_unref (s_route); nm_ip_route_unref (s_route);
} }
/* DNS */ /* DNS */

View file

@ -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)) else if (nm_setting_ip6_config_get_ignore_auto_routes (setting))
nm_ip6_config_set_never_default (config, FALSE); nm_ip6_config_set_never_default (config, FALSE);
for (i = 0; i < naddresses; i++) { 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)) { if (gateway_str) {
nm_ip6_config_set_gateway (config, gateway); inet_pton (AF_INET6, gateway_str, &gateway);
nm_ip6_config_set_gateway (config, &gateway);
break; break;
} }
} }
/* Addresses */ /* Addresses */
for (i = 0; i < naddresses; i++) { 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; NMPlatformIP6Address address;
memset (&address, 0, sizeof (address)); memset (&address, 0, sizeof (address));
address.address = *nm_ip6_address_get_address (s_addr); nm_ip_address_get_address_binary (s_addr, &address.address);
address.plen = nm_ip6_address_get_prefix (s_addr); address.plen = nm_ip_address_get_prefix (s_addr);
address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT; address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT;
address.preferred = NM_PLATFORM_LIFETIME_PERMANENT; address.preferred = NM_PLATFORM_LIFETIME_PERMANENT;
address.source = NM_IP_CONFIG_SOURCE_USER; 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)) if (nm_setting_ip6_config_get_ignore_auto_routes (setting))
nm_ip6_config_reset_routes (config); nm_ip6_config_reset_routes (config);
for (i = 0; i < nroutes; i++) { 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; NMPlatformIP6Route route;
memset (&route, 0, sizeof (route)); memset (&route, 0, sizeof (route));
route.network = *nm_ip6_route_get_dest (s_route); nm_ip_route_get_dest_binary (s_route, &route.network);
route.plen = nm_ip6_route_get_prefix (s_route); route.plen = nm_ip_route_get_prefix (s_route);
route.gateway = *nm_ip6_route_get_next_hop (s_route); nm_ip_route_get_next_hop_binary (s_route, &route.gateway);
route.metric = nm_ip6_route_get_metric (s_route); route.metric = nm_ip_route_get_metric (s_route);
if (!route.metric) if (!route.metric)
route.metric = default_route_metric; route.metric = default_route_metric;
route.source = NM_IP_CONFIG_SOURCE_USER; route.source = NM_IP_CONFIG_SOURCE_USER;
@ -508,7 +510,7 @@ nm_ip6_config_create_setting (const NMIP6Config *config)
/* Addresses */ /* Addresses */
for (i = 0; i < naddresses; i++) { for (i = 0; i < naddresses; i++) {
const NMPlatformIP6Address *address = nm_ip6_config_get_address (config, i); const NMPlatformIP6Address *address = nm_ip6_config_get_address (config, i);
NMIP6Address *s_addr; NMIPAddress *s_addr;
/* Ignore link-local address. */ /* Ignore link-local address. */
if (IN6_IS_ADDR_LINKLOCAL (&address->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) if (!method || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0)
method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL;
s_addr = nm_ip6_address_new (); s_addr = nm_ip_address_new_binary (AF_INET6, &address->address, address->plen, gateway, NULL);
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);
nm_setting_ip6_config_add_address (s_ip6, s_addr); 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 */ /* Use 'ignore' if the method wasn't previously set */
@ -546,7 +542,7 @@ nm_ip6_config_create_setting (const NMIP6Config *config)
/* Routes */ /* Routes */
for (i = 0; i < nroutes; i++) { for (i = 0; i < nroutes; i++) {
const NMPlatformIP6Route *route = nm_ip6_config_get_route (config, i); const NMPlatformIP6Route *route = nm_ip6_config_get_route (config, i);
NMIP6Route *s_route; NMIPRoute *s_route;
/* Ignore link-local route. */ /* Ignore link-local route. */
if (IN6_IS_ADDR_LINKLOCAL (&route->network)) 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) if (route->source != NM_IP_CONFIG_SOURCE_USER)
continue; continue;
s_route = nm_ip6_route_new (); s_route = nm_ip_route_new_binary (AF_INET6,
nm_ip6_route_set_dest (s_route, &route->network); &route->network, route->plen,
nm_ip6_route_set_prefix (s_route, route->plen); &route->gateway, route->metric,
nm_ip6_route_set_next_hop (s_route, &route->gateway); NULL);
nm_ip6_route_set_metric (s_route, route->metric);
nm_setting_ip6_config_add_route (s_ip6, s_route); nm_setting_ip6_config_add_route (s_ip6, s_route);
nm_ip6_route_unref (s_route); nm_ip_route_unref (s_route);
} }
/* DNS */ /* DNS */

View file

@ -268,18 +268,14 @@ ip4_setting_add_from_block (const GPtrArray *block,
GError **error) GError **error)
{ {
NMSettingIP4Config *s_ip4 = NULL; NMSettingIP4Config *s_ip4 = NULL;
NMIP4Address *addr; NMIPAddress *addr;
const char *s_method = NULL; const char *s_method = NULL;
const char *s_ipaddr = NULL; const char *s_ipaddr = NULL;
const char *s_gateway = NULL; const char *s_gateway = NULL;
const char *s_dns1 = NULL; const char *s_dns1 = NULL;
const char *s_dns2 = NULL; const char *s_dns2 = NULL;
const char *s_netmask = NULL; const char *s_netmask = NULL;
guint32 ipaddr = 0;
guint32 netmask = 0; guint32 netmask = 0;
guint32 gateway = 0;
guint32 dns1 = 0;
guint32 dns2 = 0;
guint32 prefix; guint32 prefix;
g_assert (block); 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); g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL);
/* IP address */ /* 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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"iBFT: malformed iscsiadm record: invalid IP address '%s'.", "iBFT: malformed iscsiadm record: invalid IP address '%s'.",
s_ipaddr); s_ipaddr);
@ -332,33 +328,35 @@ ip4_setting_add_from_block (const GPtrArray *block,
} }
prefix = nm_utils_ip4_netmask_to_prefix (netmask); 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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"iBFT: malformed iscsiadm record: invalid IP gateway '%s'.", "iBFT: malformed iscsiadm record: invalid IP gateway '%s'.",
s_gateway); s_gateway);
goto error; 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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"iBFT: malformed iscsiadm record: invalid DNS1 address '%s'.", "iBFT: malformed iscsiadm record: invalid DNS1 address '%s'.",
s_dns1); s_dns1);
goto error; 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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"iBFT: malformed iscsiadm record: invalid DNS2 address '%s'.", "iBFT: malformed iscsiadm record: invalid DNS2 address '%s'.",
s_dns2); s_dns2);
goto error; goto error;
} }
addr = nm_ip4_address_new (); addr = nm_ip_address_new (AF_INET, s_ipaddr, prefix, s_gateway, error);
nm_ip4_address_set_address (addr, ipaddr); if (!addr) {
nm_ip4_address_set_prefix (addr, prefix); g_prefix_error (error, "iBFT: malformed iscsiadm record: ");
nm_ip4_address_set_gateway (addr, gateway); goto error;
}
nm_setting_ip4_config_add_address (s_ip4, addr); nm_setting_ip4_config_add_address (s_ip4, addr);
nm_ip4_address_unref (addr); nm_ip_address_unref (addr);
if (s_dns1) if (s_dns1)
nm_setting_ip4_config_add_dns (s_ip4, s_dns1); nm_setting_ip4_config_add_dns (s_ip4, s_dns1);

View file

@ -120,7 +120,7 @@ test_read_ibft_static (void)
GError *error = NULL; GError *error = NULL;
const char *mac_address; const char *mac_address;
const char *expected_mac_address = "00:33:21:98:b9:f0"; const char *expected_mac_address = "00:33:21:98:b9:f0";
NMIP4Address *ip4_addr; NMIPAddress *ip4_addr;
GPtrArray *block; GPtrArray *block;
block = read_block (TEST_IBFT_DIR "/iscsiadm-test-static", expected_mac_address); 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); g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1);
ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0);
g_assert (ip4_addr); g_assert (ip4_addr);
nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.32.72"); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.32.72");
g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 22); g_assert_cmpint (nm_ip_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_gateway (ip4_addr), ==, "192.168.35.254");
g_object_unref (connection); g_object_unref (connection);
g_ptr_array_unref (block); g_ptr_array_unref (block);
@ -220,7 +220,7 @@ test_read_ibft_vlan (void)
NMSettingIP4Config *s_ip4; NMSettingIP4Config *s_ip4;
const char *mac_address; const char *mac_address;
const char *expected_mac_address = "00:33:21:98:b9:f0"; const char *expected_mac_address = "00:33:21:98:b9:f0";
NMIP4Address *ip4_addr; NMIPAddress *ip4_addr;
GError *error = NULL; GError *error = NULL;
GPtrArray *block; GPtrArray *block;
@ -257,9 +257,9 @@ test_read_ibft_vlan (void)
g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1);
ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0);
g_assert (ip4_addr); g_assert (ip4_addr);
nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.6.200"); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.6.200");
g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpint (nm_ip_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_gateway (ip4_addr), ==, NULL);
g_object_unref (connection); g_object_unref (connection);
g_ptr_array_ref (block); g_ptr_array_ref (block);

View file

@ -255,12 +255,10 @@ make_connection_setting (const char *file,
static gboolean static gboolean
read_ip4_address (shvarFile *ifcfg, read_ip4_address (shvarFile *ifcfg,
const char *tag, const char *tag,
guint32 *out_addr, char **out_addr,
GError **error) GError **error)
{ {
char *value = NULL; char *value = NULL;
guint32 ip4_addr;
gboolean success = FALSE;
g_return_val_if_fail (ifcfg != NULL, FALSE); g_return_val_if_fail (ifcfg != NULL, FALSE);
g_return_val_if_fail (tag != NULL, FALSE); g_return_val_if_fail (tag != NULL, FALSE);
@ -268,45 +266,21 @@ read_ip4_address (shvarFile *ifcfg,
if (error) if (error)
g_return_val_if_fail (*error == NULL, FALSE); g_return_val_if_fail (*error == NULL, FALSE);
*out_addr = 0; *out_addr = NULL;
value = svGetValue (ifcfg, tag, FALSE); value = svGetValue (ifcfg, tag, FALSE);
if (!value) if (!value)
return TRUE; return TRUE;
if (inet_pton (AF_INET, value, &ip4_addr) > 0) { if (nm_utils_ipaddr_valid (AF_INET, value)) {
*out_addr = ip4_addr; *out_addr = value;
success = TRUE; return TRUE;
} else { } else {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid %s IP4 address '%s'", tag, value); "Invalid %s IP4 address '%s'", tag, value);
} g_free (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);
return FALSE; return FALSE;
} }
*out_addr = ip6_addr;
return TRUE;
} }
static char * static char *
@ -358,19 +332,23 @@ static gboolean
read_full_ip4_address (shvarFile *ifcfg, read_full_ip4_address (shvarFile *ifcfg,
const char *network_file, const char *network_file,
gint32 which, gint32 which,
NMIP4Address *addr, NMIPAddress *base_addr,
NMIPAddress **out_address,
GError **error) GError **error)
{ {
char *ip_tag, *prefix_tag, *netmask_tag, *gw_tag; char *ip_tag, *prefix_tag, *netmask_tag, *gw_tag;
guint32 tmp; char *ip = NULL, *gw = NULL;
long prefix = 0;
gboolean success = FALSE; gboolean success = FALSE;
shvarFile *network_ifcfg; shvarFile *network_ifcfg;
char *value; char *value;
guint32 tmp;
g_return_val_if_fail (which >= -1, FALSE); g_return_val_if_fail (which >= -1, FALSE);
g_return_val_if_fail (ifcfg != NULL, FALSE); g_return_val_if_fail (ifcfg != NULL, FALSE);
g_return_val_if_fail (network_file != 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) if (error)
g_return_val_if_fail (*error == NULL, FALSE); 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); gw_tag = get_numbered_tag ("GATEWAY", which);
/* IP address */ /* IP address */
if (!read_ip4_address (ifcfg, ip_tag, &tmp, error)) if (!read_ip4_address (ifcfg, ip_tag, &ip, error))
goto done;
if (tmp)
nm_ip4_address_set_address (addr, tmp);
else if (!nm_ip4_address_get_address (addr)) {
success = TRUE;
goto done; goto done;
if (!ip) {
if (base_addr)
ip = g_strdup (nm_ip_address_get_address (base_addr));
else {
success = TRUE;
goto done;
}
} }
/* Gateway */ /* Gateway */
if (!read_ip4_address (ifcfg, gw_tag, &tmp, error)) if (!read_ip4_address (ifcfg, gw_tag, &gw, error))
goto done; goto done;
if (tmp) if (!gw && base_addr)
nm_ip4_address_set_gateway (addr, tmp); gw = g_strdup (nm_ip_address_get_gateway (base_addr));
else { if (!gw) {
gboolean read_success; gboolean read_success;
/* If no gateway in the ifcfg, try /etc/sysconfig/network instead */ /* If no gateway in the ifcfg, try /etc/sysconfig/network instead */
network_ifcfg = svOpenFile (network_file, NULL); network_ifcfg = svOpenFile (network_file, NULL);
if (network_ifcfg) { 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); svCloseFile (network_ifcfg);
if (!read_success) if (!read_success)
goto done; goto done;
nm_ip4_address_set_gateway (addr, tmp);
} }
} }
/* Prefix */ /* Prefix */
value = svGetValue (ifcfg, prefix_tag, FALSE); value = svGetValue (ifcfg, prefix_tag, FALSE);
if (value) { if (value) {
long int prefix;
errno = 0; errno = 0;
prefix = strtol (value, NULL, 10); 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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP4 prefix '%s'", value); "Invalid IP4 prefix '%s'", value);
g_free (value); g_free (value);
goto done; goto done;
} }
nm_ip4_address_set_prefix (addr, (guint32) prefix);
g_free (value); g_free (value);
} }
/* Fall back to NETMASK if no PREFIX was specified */ /* Fall back to NETMASK if no PREFIX was specified */
if (!nm_ip4_address_get_prefix (addr)) { if (prefix == 0) {
if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error)) if (!read_ip4_address (ifcfg, netmask_tag, &value, error))
goto done; goto done;
if (tmp) if (value) {
nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp)); inet_pton (AF_INET, value, &tmp);
prefix = nm_utils_ip4_netmask_to_prefix (tmp);
g_free (value);
}
} }
if (prefix == 0 && base_addr)
prefix = nm_ip_address_get_prefix (base_addr);
/* Try to autodetermine the prefix for the address' class */ /* Try to autodetermine the prefix for the address' class */
if (!nm_ip4_address_get_prefix (addr)) { if (prefix == 0) {
guint32 prefix = 0; if (inet_pton (AF_INET, ip, &tmp) == 1) {
prefix = nm_utils_ip4_get_default_prefix (tmp);
prefix = nm_utils_ip4_get_default_prefix (nm_ip4_address_get_address (addr)); PARSE_WARNING ("missing %s, assuming %s/%ld", prefix_tag, ip, prefix);
nm_ip4_address_set_prefix (addr, prefix); }
value = svGetValue (ifcfg, ip_tag, FALSE);
PARSE_WARNING ("missing %s, assuming %s/%u", prefix_tag, value, prefix);
g_free (value);
} }
/* Validate the 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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Missing or invalid IP4 prefix '%d'", "Missing IP4 prefix");
nm_ip4_address_get_prefix (addr));
goto done; goto done;
} }
success = TRUE; *out_address = nm_ip_address_new (AF_INET, ip, prefix, gw, error);
if (*out_address)
success = TRUE;
done: done:
g_free (ip);
g_free (gw);
g_free (ip_tag); g_free (ip_tag);
g_free (prefix_tag); g_free (prefix_tag);
g_free (netmask_tag); g_free (netmask_tag);
@ -469,12 +451,12 @@ static gboolean
read_one_ip4_route (shvarFile *ifcfg, read_one_ip4_route (shvarFile *ifcfg,
const char *network_file, const char *network_file,
guint32 which, guint32 which,
NMIP4Route **out_route, NMIPRoute **out_route,
GError **error) GError **error)
{ {
NMIP4Route *route;
char *ip_tag, *netmask_tag, *gw_tag, *metric_tag, *value; char *ip_tag, *netmask_tag, *gw_tag, *metric_tag, *value;
guint32 tmp; char *dest = NULL, *next_hop = NULL;
long prefix, metric;
gboolean success = FALSE; gboolean success = FALSE;
g_return_val_if_fail (ifcfg != NULL, FALSE); g_return_val_if_fail (ifcfg != NULL, FALSE);
@ -484,75 +466,72 @@ read_one_ip4_route (shvarFile *ifcfg,
if (error) if (error)
g_return_val_if_fail (*error == NULL, FALSE); g_return_val_if_fail (*error == NULL, FALSE);
route = nm_ip4_route_new ();
ip_tag = g_strdup_printf ("ADDRESS%u", which); ip_tag = g_strdup_printf ("ADDRESS%u", which);
netmask_tag = g_strdup_printf ("NETMASK%u", which); netmask_tag = g_strdup_printf ("NETMASK%u", which);
gw_tag = g_strdup_printf ("GATEWAY%u", which); gw_tag = g_strdup_printf ("GATEWAY%u", which);
metric_tag = g_strdup_printf ("METRIC%u", which); metric_tag = g_strdup_printf ("METRIC%u", which);
/* Destination */ /* Destination */
if (!read_ip4_address (ifcfg, ip_tag, &tmp, error)) if (!read_ip4_address (ifcfg, ip_tag, &dest, error))
goto out; goto out;
if (!tmp) { if (!dest) {
/* Check whether IP is missing or 0.0.0.0 */ /* Check whether IP is missing or 0.0.0.0 */
char *val; char *val;
val = svGetValue (ifcfg, ip_tag, FALSE); val = svGetValue (ifcfg, ip_tag, FALSE);
if (!val) { if (!val) {
nm_ip4_route_unref (route); *out_route = NULL;
route = NULL;
success = TRUE; /* missing route = success */ success = TRUE; /* missing route = success */
goto out; goto out;
} }
g_free (val); g_free (val);
} }
nm_ip4_route_set_dest (route, tmp);
/* Next hop */ /* Next hop */
if (!read_ip4_address (ifcfg, gw_tag, &tmp, error)) if (!read_ip4_address (ifcfg, gw_tag, &next_hop, error))
goto out; goto out;
/* No need to check tmp, because we don't make distinction between missing GATEWAY IP and 0.0.0.0 */ /* We don't make distinction between missing GATEWAY IP and 0.0.0.0 */
nm_ip4_route_set_next_hop (route, tmp);
/* Prefix */ /* Prefix */
if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error)) if (!read_ip4_address (ifcfg, netmask_tag, &value, error))
goto out; goto out;
if (tmp) if (value) {
nm_ip4_route_set_prefix (route, nm_utils_ip4_netmask_to_prefix (tmp)); guint32 netmask;
/* Validate the prefix */ inet_pton (AF_INET, value, &netmask);
if ( !nm_ip4_route_get_prefix (route) prefix = nm_utils_ip4_netmask_to_prefix (netmask);
|| nm_ip4_route_get_prefix (route) > 32) { 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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Missing or invalid IP4 prefix '%d'", "Missing IP4 route element '%s'", netmask_tag);
nm_ip4_route_get_prefix (route));
goto out; goto out;
} }
/* Metric */ /* Metric */
value = svGetValue (ifcfg, metric_tag, FALSE); value = svGetValue (ifcfg, metric_tag, FALSE);
if (value) { if (value) {
long int metric; metric = nm_utils_ascii_str_to_int64 (value, 10, 0, G_MAXUINT32, -1);
if (metric < 0) {
errno = 0;
metric = strtol (value, NULL, 10);
if (errno || metric < 0) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP4 route metric '%s'", value); "Invalid IP4 route metric '%s'", value);
g_free (value); g_free (value);
goto out; goto out;
} }
nm_ip4_route_set_metric (route, (guint32) metric);
g_free (value); g_free (value);
} } else
metric = 0;
*out_route = route; *out_route = nm_ip_route_new (AF_INET, dest, prefix, next_hop, metric, error);
success = TRUE; if (*out_route)
success = TRUE;
out: out:
if (!success && route) g_free (dest);
nm_ip4_route_unref (route); g_free (next_hop);
g_free (ip_tag); g_free (ip_tag);
g_free (netmask_tag); g_free (netmask_tag);
g_free (gw_tag); g_free (gw_tag);
@ -568,9 +547,8 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError
char **lines = NULL, **iter; char **lines = NULL, **iter;
GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric; GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric;
GMatchInfo *match_info; GMatchInfo *match_info;
NMIP4Route *route; NMIPRoute *route = NULL;
guint32 ip4_addr; char *dest = NULL, *prefix = NULL, *next_hop = NULL, *metric = NULL;
char *dest = NULL, *prefix = NULL, *metric = NULL;
long int prefix_int, metric_int; long int prefix_int, metric_int;
gboolean success = FALSE; 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_via = g_regex_new (pattern_via, 0, 0, NULL);
regex_metric = g_regex_new (pattern_metric, 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 */ /* Iterate through file lines */
lines = g_strsplit_set (contents, "\n\r", -1); lines = g_strsplit_set (contents, "\n\r", -1);
for (iter = lines; iter && *iter; iter++) { 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); dest = g_match_info_fetch (match_info, 1);
if (!strcmp (dest, "default")) if (!strcmp (dest, "default"))
strcpy (dest, "0.0.0.0"); 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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP4 route destination address '%s'", dest); "Invalid IP4 route destination address '%s'", dest);
g_free (dest); g_free (dest);
g_match_info_free (match_info);
goto error; goto error;
} }
nm_ip4_route_set_dest (route, ip4_addr);
g_free (dest);
/* Prefix - is optional; 32 if missing */ /* Prefix - is optional; 32 if missing */
prefix = g_match_info_fetch (match_info, 2); 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) { if (errno || prefix_int <= 0 || prefix_int > 32) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP4 route destination prefix '%s'", prefix); "Invalid IP4 route destination prefix '%s'", prefix);
g_free (dest);
g_free (prefix); g_free (prefix);
goto error; goto error;
} }
} }
nm_ip4_route_set_prefix (route, (guint32) prefix_int);
g_free (prefix); g_free (prefix);
/* Next hop */ /* Next hop */
g_regex_match (regex_via, *iter, 0, &match_info); g_regex_match (regex_via, *iter, 0, &match_info);
if (g_match_info_matches (match_info)) { if (g_match_info_matches (match_info)) {
char *next_hop = g_match_info_fetch (match_info, 1); next_hop = g_match_info_fetch (match_info, 1);
if (inet_pton (AF_INET, next_hop, &ip4_addr) != 1) { if (!nm_utils_ipaddr_valid (AF_INET, next_hop)) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP4 route gateway address '%s'", "Invalid IP4 route gateway address '%s'",
next_hop); next_hop);
g_match_info_free (match_info); g_match_info_free (match_info);
g_free (dest);
g_free (next_hop); g_free (next_hop);
goto error; goto error;
} }
g_free (next_hop);
} else { } else {
/* we don't make distinction between missing GATEWAY IP and 0.0.0.0 */ /* 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); g_match_info_free (match_info);
/* Metric */ /* Metric */
@ -682,18 +655,23 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError
g_match_info_free (match_info); g_match_info_free (match_info);
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP4 route metric '%s'", metric); "Invalid IP4 route metric '%s'", metric);
g_free (dest);
g_free (next_hop);
g_free (metric); g_free (metric);
goto error; goto error;
} }
g_free (metric); g_free (metric);
} }
nm_ip4_route_set_metric (route, (guint32) metric_int);
g_match_info_free (match_info); 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)) if (!nm_setting_ip4_config_add_route (s_ip4, route))
PARSE_WARNING ("duplicate IP4 route"); PARSE_WARNING ("duplicate IP4 route");
} }
success = TRUE; success = TRUE;
@ -701,7 +679,8 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError
error: error:
g_free (contents); g_free (contents);
g_strfreev (lines); g_strfreev (lines);
nm_ip4_route_unref (route); if (route)
nm_ip_route_unref (route);
g_regex_unref (regex_to1); g_regex_unref (regex_to1);
g_regex_unref (regex_to2); g_regex_unref (regex_to2);
g_regex_unref (regex_via); g_regex_unref (regex_via);
@ -715,15 +694,13 @@ parse_full_ip6_address (shvarFile *ifcfg,
const char *network_file, const char *network_file,
const char *addr_str, const char *addr_str,
int i, int i,
NMIP6Address **out_address, NMIPAddress **out_address,
GError **error) GError **error)
{ {
NMIP6Address *addr = NULL;
char **list; char **list;
char *ip_val, *prefix_val; char *ip_val, *prefix_val, *gateway_val = NULL;
long prefix;
shvarFile *network_ifcfg; shvarFile *network_ifcfg;
char *value = NULL;
struct in6_addr tmp = IN6ADDR_ANY_INIT;
gboolean success = FALSE; gboolean success = FALSE;
g_return_val_if_fail (addr_str != NULL, FALSE); g_return_val_if_fail (addr_str != NULL, FALSE);
@ -741,23 +718,9 @@ parse_full_ip6_address (shvarFile *ifcfg,
} }
ip_val = list[0]; ip_val = list[0];
prefix_val = list[1]; 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 */
if (prefix_val) { if (prefix_val) {
long int prefix;
errno = 0; errno = 0;
prefix = strtol (prefix_val, NULL, 10); prefix = strtol (prefix_val, NULL, 10);
if (errno || prefix <= 0 || prefix > 128) { if (errno || prefix <= 0 || prefix > 128) {
@ -765,47 +728,38 @@ parse_full_ip6_address (shvarFile *ifcfg,
"Invalid IP6 prefix '%s'", prefix_val); "Invalid IP6 prefix '%s'", prefix_val);
goto error; goto error;
} }
nm_ip6_address_set_prefix (addr, (guint32) prefix);
} else { } else {
/* Missing prefix is treated as prefix of 64 */ /* Missing prefix is treated as prefix of 64 */
nm_ip6_address_set_prefix (addr, 64); prefix = 64;
} }
/* Gateway */ /* Gateway */
tmp = in6addr_any; if (i == 0) {
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) {
char *ptr; char *ptr;
if ((ptr = strchr (value, '%')) != NULL) gateway_val = svGetValue (ifcfg, "IPV6_DEFAULTGW", FALSE);
*ptr = '\0'; /* remove %interface prefix if present */ if (!gateway_val) {
if (!parse_ip6_address (value, &tmp, error)) /* If no gateway in the ifcfg, try global /etc/sysconfig/network instead */
goto error; network_ifcfg = svOpenFile (network_file, NULL);
nm_ip6_address_set_gateway (addr, &tmp); if (network_ifcfg) {
} gateway_val = svGetValue (network_ifcfg, "IPV6_DEFAULTGW", FALSE);
svCloseFile (network_ifcfg);
}
}
*out_address = addr; if ( gateway_val
success = TRUE; && (ptr = strchr (gateway_val, '%')) != NULL)
*ptr = '\0'; /* remove %interface suffix if present */
} else
gateway_val = NULL;
*out_address = nm_ip_address_new (AF_INET6, ip_val, prefix, gateway_val, error);
if (*out_address)
success = TRUE;
error: error:
if (!success && addr)
nm_ip6_address_unref (addr);
g_strfreev (list); g_strfreev (list);
g_free (value); g_free (gateway_val);
return success; return success;
} }
@ -824,9 +778,8 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro
char **lines = NULL, **iter; char **lines = NULL, **iter;
GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric; GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric;
GMatchInfo *match_info; GMatchInfo *match_info;
NMIP6Route *route; NMIPRoute *route = NULL;
struct in6_addr ip6_addr; char *dest = NULL, *prefix = NULL, *next_hop = NULL, *metric = NULL;
char *dest = NULL, *prefix = NULL, *metric = NULL;
long int prefix_int, metric_int; long int prefix_int, metric_int;
gboolean success = FALSE; 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_via = g_regex_new (pattern_via, 0, 0, NULL);
regex_metric = g_regex_new (pattern_metric, 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 */ /* Iterate through file lines */
lines = g_strsplit_set (contents, "\n\r", -1); lines = g_strsplit_set (contents, "\n\r", -1);
for (iter = lines; iter && *iter; iter++) { 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); dest = g_match_info_fetch (match_info, 1);
if (!g_strcmp0 (dest, "default")) { if (!g_strcmp0 (dest, "default")) {
/* Ignore default route - NM handles it internally */ /* Ignore default route - NM handles it internally */
g_free (dest); g_clear_pointer (&dest, g_free);
g_match_info_free (match_info); g_match_info_free (match_info);
PARSE_WARNING ("ignoring manual default route: '%s' (%s)", *iter, filename); PARSE_WARNING ("ignoring manual default route: '%s' (%s)", *iter, filename);
continue; 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 - is optional; 128 if missing */
prefix = g_match_info_fetch (match_info, 2); 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) { if (errno || prefix_int <= 0 || prefix_int > 128) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP6 route destination prefix '%s'", prefix); "Invalid IP6 route destination prefix '%s'", prefix);
g_free (dest);
g_free (prefix); g_free (prefix);
goto error; goto error;
} }
} }
nm_ip6_route_set_prefix (route, (guint32) prefix_int);
g_free (prefix); g_free (prefix);
/* Next hop */ /* Next hop */
g_regex_match (regex_via, *iter, 0, &match_info); g_regex_match (regex_via, *iter, 0, &match_info);
if (g_match_info_matches (match_info)) { if (g_match_info_matches (match_info)) {
char *next_hop = g_match_info_fetch (match_info, 1); next_hop = g_match_info_fetch (match_info, 1);
if (inet_pton (AF_INET6, next_hop, &ip6_addr) != 1) { if (!nm_utils_ipaddr_valid (AF_INET6, next_hop)) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IPv6 route nexthop address '%s'", "Invalid IPv6 route nexthop address '%s'",
next_hop); next_hop);
g_match_info_free (match_info); g_match_info_free (match_info);
g_free (dest);
g_free (next_hop); g_free (next_hop);
goto error; goto error;
} }
g_free (next_hop);
} else { } else {
/* Missing "via" is taken as :: */ /* 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); g_match_info_free (match_info);
/* Metric */ /* Metric */
@ -943,15 +884,20 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro
g_match_info_free (match_info); g_match_info_free (match_info);
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IP6 route metric '%s'", metric); "Invalid IP6 route metric '%s'", metric);
g_free (dest);
g_free (next_hop);
g_free (metric); g_free (metric);
goto error; goto error;
} }
g_free (metric); g_free (metric);
} }
nm_ip6_route_set_metric (route, (guint32) metric_int);
g_match_info_free (match_info); 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)) if (!nm_setting_ip6_config_add_route (s_ip6, route))
PARSE_WARNING ("duplicate IP6 route"); PARSE_WARNING ("duplicate IP6 route");
} }
@ -961,7 +907,8 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro
error: error:
g_free (contents); g_free (contents);
g_strfreev (lines); g_strfreev (lines);
nm_ip6_route_unref (route); if (route)
nm_ip_route_unref (route);
g_regex_unref (regex_to1); g_regex_unref (regex_to1);
g_regex_unref (regex_to2); g_regex_unref (regex_to2);
g_regex_unref (regex_via); g_regex_unref (regex_via);
@ -1082,16 +1029,12 @@ make_ip4_setting (shvarFile *ifcfg,
* the legacy 'network' service (ifup-eth). * the legacy 'network' service (ifup-eth).
*/ */
for (i = -1; i < 256; i++) { 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, NULL, &addr, error))
if (!read_full_ip4_address (ifcfg, network_file, i, addr, error)) {
nm_ip4_address_unref (addr);
goto done; goto done;
}
if (!nm_ip4_address_get_address (addr)) {
nm_ip4_address_unref (addr);
if (!addr) {
/* The first mandatory variable is 2-indexed (IPADDR2) /* The first mandatory variable is 2-indexed (IPADDR2)
* Variables IPADDR, IPADDR0 and IPADDR1 are optional */ * Variables IPADDR, IPADDR0 and IPADDR1 are optional */
if (i > 1) if (i > 1)
@ -1101,7 +1044,7 @@ make_ip4_setting (shvarFile *ifcfg,
if (!nm_setting_ip4_config_add_address (s_ip4, addr)) if (!nm_setting_ip4_config_add_address (s_ip4, addr))
PARSE_WARNING ("duplicate IP4 address"); PARSE_WARNING ("duplicate IP4 address");
nm_ip4_address_unref (addr); nm_ip_address_unref (addr);
} }
/* DNS servers /* DNS servers
@ -1109,27 +1052,22 @@ make_ip4_setting (shvarFile *ifcfg,
*/ */
for (i = 1; i <= 10; i++) { for (i = 1; i <= 10; i++) {
char *tag; char *tag;
guint32 dns;
struct in6_addr ip6_dns;
tag = g_strdup_printf ("DNS%u", i); tag = g_strdup_printf ("DNS%u", i);
value = svGetValue (ifcfg, tag, FALSE); value = svGetValue (ifcfg, tag, FALSE);
if (value) { if (value) {
if (!read_ip4_address (ifcfg, tag, &dns, error)) { if (nm_utils_ipaddr_valid (AF_INET, value)) {
gboolean valid = TRUE; 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 */ /* Ignore IPv6 addresses */
valid = parse_ip6_address (value, &ip6_dns, NULL); } else {
if (!valid) { PARSE_WARNING ("invalid DNS server address %s", value);
g_free (tag); g_free (tag);
goto done; g_free (value);
} goto done;
g_clear_error (error);
dns = 0;
} }
if (dns && !nm_setting_ip4_config_add_dns (s_ip4, value))
PARSE_WARNING ("duplicate DNS server %s", tag);
g_free (value); g_free (value);
} }
@ -1169,7 +1107,7 @@ make_ip4_setting (shvarFile *ifcfg,
route_ifcfg = utils_get_route_ifcfg (ifcfg->fileName, FALSE); route_ifcfg = utils_get_route_ifcfg (ifcfg->fileName, FALSE);
if (route_ifcfg) { if (route_ifcfg) {
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
NMIP4Route *route = NULL; NMIPRoute *route = NULL;
if (!read_one_ip4_route (route_ifcfg, network_file, i, &route, error)) { if (!read_one_ip4_route (route_ifcfg, network_file, i, &route, error)) {
svCloseFile (route_ifcfg); svCloseFile (route_ifcfg);
@ -1181,7 +1119,7 @@ make_ip4_setting (shvarFile *ifcfg,
if (!nm_setting_ip4_config_add_route (s_ip4, route)) if (!nm_setting_ip4_config_add_route (s_ip4, route))
PARSE_WARNING ("duplicate IP4 route"); PARSE_WARNING ("duplicate IP4 route");
nm_ip4_route_unref (route); nm_ip_route_unref (route);
} }
svCloseFile (route_ifcfg); svCloseFile (route_ifcfg);
} }
@ -1225,7 +1163,7 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo
GDir *dir; GDir *dir;
char *dirname, *base; char *dirname, *base;
shvarFile *parsed; shvarFile *parsed;
NMIP4Address *base_addr; NMIPAddress *base_addr;
GError *err = NULL; GError *err = NULL;
g_return_if_fail (s_ip4 != 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); dir = g_dir_open (dirname, 0, &err);
if (dir) { if (dir) {
const char *item; const char *item;
NMIP4Address *addr; NMIPAddress *addr;
gboolean ok; gboolean ok;
while ((item = g_dir_read_name (dir))) { while ((item = g_dir_read_name (dir))) {
@ -1293,8 +1231,8 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo
continue; continue;
} }
addr = nm_ip4_address_dup (base_addr); addr = NULL;
ok = read_full_ip4_address (parsed, network_file, -1, addr, &err); ok = read_full_ip4_address (parsed, network_file, -1, base_addr, &addr, &err);
svCloseFile (parsed); svCloseFile (parsed);
if (ok) { if (ok) {
if (!_nm_setting_ip4_config_add_address_with_label (s_ip4, addr, device)) 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"); full_path, err ? err->message : "no address");
g_clear_error (&err); g_clear_error (&err);
} }
nm_ip4_address_unref (addr); nm_ip_address_unref (addr);
g_free (device); g_free (device);
g_free (full_path); g_free (full_path);
@ -1476,7 +1414,7 @@ make_ip6_setting (shvarFile *ifcfg,
list = g_strsplit_set (value, " ", 0); list = g_strsplit_set (value, " ", 0);
g_free (value); g_free (value);
for (iter = list, i = 0; iter && *iter; iter++, i++) { 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)) { if (!parse_full_ip6_address (ifcfg, network_file, *iter, i, &addr, error)) {
g_strfreev (list); g_strfreev (list);
@ -1485,7 +1423,7 @@ make_ip6_setting (shvarFile *ifcfg,
if (!nm_setting_ip6_config_add_address (s_ip6, addr)) if (!nm_setting_ip6_config_add_address (s_ip6, addr))
PARSE_WARNING ("duplicate IP6 address"); PARSE_WARNING ("duplicate IP6 address");
nm_ip6_address_unref (addr); nm_ip_address_unref (addr);
} }
g_strfreev (list); g_strfreev (list);
@ -1494,8 +1432,6 @@ make_ip6_setting (shvarFile *ifcfg,
*/ */
for (i = 1; i <= 10; i++) { for (i = 1; i <= 10; i++) {
char *tag; char *tag;
struct in6_addr ip6_dns;
guint32 ip4_addr;
tag = g_strdup_printf ("DNS%u", i); tag = g_strdup_printf ("DNS%u", i);
value = svGetValue (ifcfg, tag, FALSE); value = svGetValue (ifcfg, tag, FALSE);
@ -1504,18 +1440,16 @@ make_ip6_setting (shvarFile *ifcfg,
break; /* all done */ break; /* all done */
} }
ip6_dns = in6addr_any; if (nm_utils_ipaddr_valid (AF_INET6, value)) {
if (parse_ip6_address (value, &ip6_dns, NULL)) { if (!nm_setting_ip6_config_add_dns (s_ip6, value))
if (!IN6_IS_ADDR_UNSPECIFIED (&ip6_dns) && !nm_setting_ip6_config_add_dns (s_ip6, value))
PARSE_WARNING ("duplicate DNS server %s", tag); PARSE_WARNING ("duplicate DNS server %s", tag);
} else if (nm_utils_ipaddr_valid (AF_INET, value)) {
/* Ignore IPv4 addresses */
} else { } else {
/* Maybe an IPv4 address? If so ignore it */ PARSE_WARNING ("invalid DNS server address %s", value);
if (inet_pton (AF_INET, value, &ip4_addr) != 1) { g_free (tag);
g_free (tag); g_free (value);
g_free (value); goto error;
PARSE_WARNING ("duplicate IP6 address");
goto error;
}
} }
g_free (tag); g_free (tag);

File diff suppressed because it is too large Load diff

View file

@ -1743,12 +1743,11 @@ write_connection_setting (NMSettingConnection *s_con, shvarFile *ifcfg)
static gboolean static gboolean
write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError **error) write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError **error)
{ {
char dest[INET_ADDRSTRLEN]; const char *dest, *next_hop;
char next_hop[INET_ADDRSTRLEN];
char **route_items; char **route_items;
char *route_contents; char *route_contents;
NMIP4Route *route; NMIPRoute *route;
guint32 ip, prefix, metric; guint32 prefix, metric;
guint32 i, num; guint32 i, num;
gboolean success = FALSE; gboolean success = FALSE;
@ -1767,17 +1766,10 @@ write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
route = nm_setting_ip4_config_get_route (s_ip4, i); route = nm_setting_ip4_config_get_route (s_ip4, i);
memset (dest, 0, sizeof (dest)); dest = nm_ip_route_get_dest (route);
ip = nm_ip4_route_get_dest (route); prefix = nm_ip_route_get_prefix (route);
inet_ntop (AF_INET, (const void *) &ip, &dest[0], sizeof (dest)); next_hop = nm_ip_route_get_next_hop (route);
metric = nm_ip_route_get_metric (route);
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);
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", 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); num = nm_setting_ip4_config_get_num_addresses (s_ip4);
for (i = n = 0; i < num; i++) { for (i = n = 0; i < num; i++) {
char buf[INET_ADDRSTRLEN]; NMIPAddress *addr;
NMIP4Address *addr;
guint32 ip;
if (i > 0) { if (i > 0) {
const char *label; 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); addr = nm_setting_ip4_config_get_address (s_ip4, i);
memset (buf, 0, sizeof (buf)); svSetValue (ifcfg, addr_key, nm_ip_address_get_address (addr), FALSE);
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);
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); svSetValue (ifcfg, prefix_key, tmp, FALSE);
g_free (tmp); g_free (tmp);
svSetValue (ifcfg, netmask_key, NULL, FALSE); svSetValue (ifcfg, netmask_key, NULL, FALSE);
if (nm_ip4_address_get_gateway (addr)) { svSetValue (ifcfg, gw_key, nm_ip_address_get_gateway (addr), FALSE);
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);
g_free (addr_key); g_free (addr_key);
g_free (prefix_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); num = nm_setting_ip4_config_get_num_routes (s_ip4);
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
char buf[INET_ADDRSTRLEN]; char buf[INET_ADDRSTRLEN];
NMIP4Route *route; NMIPRoute *route;
guint32 ip, metric; guint32 netmask, metric;
addr_key = g_strdup_printf ("ADDRESS%d", i); addr_key = g_strdup_printf ("ADDRESS%d", i);
netmask_key = g_strdup_printf ("NETMASK%d", i); netmask_key = g_strdup_printf ("NETMASK%d", i);
@ -2066,23 +2047,17 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
} else { } else {
route = nm_setting_ip4_config_get_route (s_ip4, i); route = nm_setting_ip4_config_get_route (s_ip4, i);
memset (buf, 0, sizeof (buf)); svSetValue (routefile, addr_key, nm_ip_route_get_dest (route), FALSE);
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);
memset (buf, 0, sizeof (buf)); memset (buf, 0, sizeof (buf));
ip = nm_utils_ip4_prefix_to_netmask (nm_ip4_route_get_prefix (route)); netmask = nm_utils_ip4_prefix_to_netmask (nm_ip_route_get_prefix (route));
inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); inet_ntop (AF_INET, (const void *) &netmask, &buf[0], sizeof (buf));
svSetValue (routefile, netmask_key, &buf[0], FALSE); svSetValue (routefile, netmask_key, &buf[0], FALSE);
memset (buf, 0, sizeof (buf)); svSetValue (routefile, gw_key, nm_ip_route_get_next_hop (route), FALSE);
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);
memset (buf, 0, sizeof (buf)); memset (buf, 0, sizeof (buf));
metric = nm_ip4_route_get_metric (route); metric = nm_ip_route_get_metric (route);
if (metric == 0) if (metric == 0)
svSetValue (routefile, metric_key, NULL, FALSE); svSetValue (routefile, metric_key, NULL, FALSE);
else { else {
@ -2162,9 +2137,8 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path)
num = nm_setting_ip4_config_get_num_addresses (s_ip4); num = nm_setting_ip4_config_get_num_addresses (s_ip4);
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
const char *label, *p; const char *label, *p;
char buf[INET_ADDRSTRLEN], *path, *tmp; char *path, *tmp;
NMIP4Address *addr; NMIPAddress *addr;
guint32 ip;
shvarFile *ifcfg; shvarFile *ifcfg;
label = _nm_setting_ip4_config_get_address_label (s_ip4, i); 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); svSetValue (ifcfg, "DEVICE", label, FALSE);
addr = nm_setting_ip4_config_get_address (s_ip4, i); 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)); tmp = g_strdup_printf ("%u", nm_ip_address_get_prefix (addr));
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));
svSetValue (ifcfg, "PREFIX", tmp, FALSE); svSetValue (ifcfg, "PREFIX", tmp, FALSE);
g_free (tmp); g_free (tmp);
if (nm_ip4_address_get_gateway (addr)) { svSetValue (ifcfg, "GATEWAY", nm_ip_address_get_gateway (addr), FALSE);
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);
}
svWriteFile (ifcfg, 0644, NULL); svWriteFile (ifcfg, 0644, NULL);
svCloseFile (ifcfg); svCloseFile (ifcfg);
@ -2214,13 +2179,9 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path)
static gboolean static gboolean
write_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **error) 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_items;
char *route_contents; char *route_contents;
NMIP6Route *route; NMIPRoute *route;
const struct in6_addr *ip;
guint32 prefix, metric;
guint32 i, num; guint32 i, num;
gboolean success = FALSE; 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)); route_items = g_malloc0 (sizeof (char*) * (num + 1));
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
route = nm_setting_ip6_config_get_route (s_ip6, i); route = nm_setting_ip6_config_get_route (s_ip6, i);
route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n",
memset (dest, 0, sizeof (dest)); nm_ip_route_get_dest (route),
ip = nm_ip6_route_get_dest (route); nm_ip_route_get_prefix (route),
inet_ntop (AF_INET6, (const void *) ip, &dest[0], sizeof (dest)); nm_ip_route_get_next_hop (route),
nm_ip_route_get_metric (route));
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[num] = NULL; route_items[num] = NULL;
route_contents = g_strjoinv (NULL, route_items); route_contents = g_strjoinv (NULL, route_items);
@ -2276,13 +2228,11 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
NMSettingIP6Config *s_ip6; NMSettingIP6Config *s_ip6;
NMSettingIP4Config *s_ip4; NMSettingIP4Config *s_ip4;
const char *value; const char *value;
char *addr_key, *prefix; char *addr_key;
guint32 i, num, num4; guint32 i, num, num4;
GString *searches; GString *searches;
char buf[INET6_ADDRSTRLEN]; const char *ipv6_defaultgw;
char ipv6_defaultgw[INET6_ADDRSTRLEN]; NMIPAddress *addr;
NMIP6Address *addr;
const struct in6_addr *ip;
const char *dns; const char *dns;
GString *ip_str1, *ip_str2, *ip_ptr; GString *ip_str1, *ip_str2, *ip_ptr;
char *route6_path; 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); num = nm_setting_ip6_config_get_num_addresses (s_ip6);
ip_str1 = g_string_new (NULL); ip_str1 = g_string_new (NULL);
ip_str2 = g_string_new (NULL); ip_str2 = g_string_new (NULL);
ipv6_defaultgw[0] = 0; ipv6_defaultgw = NULL;
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
if (i == 0) if (i == 0)
ip_ptr = ip_str1; ip_ptr = ip_str1;
@ -2344,23 +2294,16 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
ip_ptr = ip_str2; ip_ptr = ip_str2;
addr = nm_setting_ip6_config_get_address (s_ip6, 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));
if (i > 1) if (i > 1)
g_string_append_c (ip_ptr, ' '); /* separate addresses in IPV6ADDR_SECONDARIES */ g_string_append_c (ip_ptr, ' '); /* separate addresses in IPV6ADDR_SECONDARIES */
g_string_append (ip_ptr, buf); g_string_append_printf (ip_ptr, "%s/%u",
g_string_append_c (ip_ptr, '/'); nm_ip_address_get_address (addr),
g_string_append (ip_ptr, prefix); nm_ip_address_get_prefix (addr));
g_free (prefix);
/* We only support gateway for the first IP address for now */ /* We only support gateway for the first IP address */
if (i == 0) { if (i == 0)
ip = nm_ip6_address_get_gateway (addr); ipv6_defaultgw = nm_ip_address_get_gateway (addr);
if (!IN6_IS_ADDR_UNSPECIFIED (ip))
inet_ntop (AF_INET6, ip, ipv6_defaultgw, sizeof (ipv6_defaultgw));
}
} }
svSetValue (ifcfg, "IPV6ADDR", ip_str1->str, FALSE); svSetValue (ifcfg, "IPV6ADDR", ip_str1->str, FALSE);
svSetValue (ifcfg, "IPV6ADDR_SECONDARIES", ip_str2->str, FALSE); svSetValue (ifcfg, "IPV6ADDR_SECONDARIES", ip_str2->str, FALSE);

View file

@ -611,21 +611,25 @@ make_ip4_setting (NMConnection *connection,
/************** add all ip settings to the connection**********/ /************** add all ip settings to the connection**********/
while (iblock) { while (iblock) {
ip_block *current_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 */ /* currently all the IPs has the same gateway */
nm_ip4_address_set_gateway (ip4_addr, iblock->gateway); ip4_addr = nm_ip_address_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, &local);
if (iblock->gateway) if (iblock->next_hop)
g_object_set (ip4_setting, g_object_set (ip4_setting,
NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES,
TRUE, NULL); TRUE, NULL);
if (!nm_setting_ip4_config_add_address (ip4_setting, ip4_addr))
nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP4 address"); if (ip4_addr) {
nm_ip4_address_unref (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; current_iblock = iblock;
iblock = iblock->next; iblock = iblock->next;
destroy_ip_block (current_iblock); destroy_ip_block (current_iblock);
@ -690,33 +694,34 @@ make_ip4_setting (NMConnection *connection,
const char *metric_str; const char *metric_str;
char *stripped; char *stripped;
long int metric; 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) { if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) {
metric = strtol (metric_str, NULL, 10); metric = strtol (metric_str, NULL, 10);
nm_ip4_route_set_metric (route, (guint32) metric);
} else { } else {
metric_str = ifnet_get_global_data ("metric"); metric_str = ifnet_get_global_data ("metric");
if (metric_str) { if (metric_str) {
stripped = g_strdup (metric_str); stripped = g_strdup (metric_str);
strip_string (stripped, '"'); strip_string (stripped, '"');
metric = strtol (metric_str, NULL, 10); metric = strtol (metric_str, NULL, 10);
nm_ip4_route_set_metric (route,
(guint32) metric);
g_free (stripped); g_free (stripped);
} } else
metric = 0;
} }
if (!nm_setting_ip4_config_add_route (ip4_setting, route)) route = nm_ip_route_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local);
nm_log_warn (LOGD_SETTINGS, "duplicate IP4 route");
nm_log_info (LOGD_SETTINGS, "new IP4 route:%d\n", iblock->ip);
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; current_iblock = iblock;
iblock = iblock->next; iblock = iblock->next;
@ -739,7 +744,7 @@ make_ip6_setting (NMConnection *connection,
gboolean ipv6_enabled = FALSE; gboolean ipv6_enabled = FALSE;
gchar *method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; gchar *method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL;
const char *value; const char *value;
ip6_block *iblock; ip_block *iblock;
gboolean never_default = !has_default_ip6_route (conn_name); gboolean never_default = !has_default_ip6_route (conn_name);
s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new ();
@ -776,7 +781,7 @@ make_ip6_setting (NMConnection *connection,
/* Make manual settings */ /* Make manual settings */
if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) {
ip6_block *current_iblock; ip_block *current_iblock;
iblock = convert_ip6_config_block (conn_name); iblock = convert_ip6_config_block (conn_name);
if (!iblock) { if (!iblock) {
@ -787,20 +792,26 @@ make_ip6_setting (NMConnection *connection,
} }
/* add all IPv6 addresses */ /* add all IPv6 addresses */
while (iblock) { while (iblock) {
NMIP6Address *ip6_addr = nm_ip6_address_new (); NMIPAddress *ip6_addr;
GError *local = NULL;
nm_ip6_address_set_address (ip6_addr, iblock->ip); ip6_addr = nm_ip_address_new (AF_INET6, iblock->ip, iblock->prefix, NULL, &local);
nm_ip6_address_set_prefix (ip6_addr, iblock->prefix); if (ip6_addr) {
if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) { if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) {
nm_log_info (LOGD_SETTINGS, "ipv6 addresses count: %d", nm_log_info (LOGD_SETTINGS, "ipv6 addresses count: %d",
nm_setting_ip6_config_get_num_addresses (s_ip6)); 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 { } 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; current_iblock = iblock;
iblock = iblock->next; iblock = iblock->next;
destroy_ip6_block (current_iblock); destroy_ip_block (current_iblock);
} }
} else if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) { } else if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) {
@ -818,42 +829,45 @@ make_ip6_setting (NMConnection *connection,
TRUE, NULL); TRUE, NULL);
/* Add all IPv6 routes */ /* Add all IPv6 routes */
while (iblock) { while (iblock) {
ip6_block *current_iblock = iblock; ip_block *current_iblock = iblock;
const char *metric_str; const char *metric_str;
char *stripped; char *stripped;
long int metric = 1; long int metric;
NMIP6Route *route = nm_ip6_route_new (); 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 /* metric is not per routes configuration right now
* global metric is also supported (metric="x") */ * global metric is also supported (metric="x") */
if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) { if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) {
metric = strtol (metric_str, NULL, 10); metric = strtol (metric_str, NULL, 10);
nm_ip6_route_set_metric (route, (guint32) metric); nm_ip_route_set_metric (route, (guint32) metric);
} else { } else {
metric_str = ifnet_get_global_data ("metric"); metric_str = ifnet_get_global_data ("metric");
if (metric_str) { if (metric_str) {
stripped = g_strdup (metric_str); stripped = g_strdup (metric_str);
strip_string (stripped, '"'); strip_string (stripped, '"');
metric = strtol (metric_str, NULL, 10); metric = strtol (metric_str, NULL, 10);
nm_ip6_route_set_metric (route, nm_ip_route_set_metric (route, (guint32) metric);
(guint32) metric);
g_free (stripped); g_free (stripped);
} else } else
nm_ip6_route_set_metric (route, (guint32) 1); metric = 1;
} }
if (nm_setting_ip6_config_add_route (s_ip6, route)) route = nm_ip_route_new (AF_INET6, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local);
nm_log_info (LOGD_SETTINGS, " new IP6 route"); if (route) {
else if (nm_setting_ip6_config_add_route (s_ip6, route))
nm_log_warn (LOGD_SETTINGS, " duplicate IP6 route"); nm_log_info (LOGD_SETTINGS, " new IP6 route");
nm_ip6_route_unref (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; current_iblock = iblock;
iblock = iblock->next; iblock = iblock->next;
destroy_ip6_block (current_iblock); destroy_ip_block (current_iblock);
} }
done: done:
@ -2361,7 +2375,6 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err
{ {
NMSettingIP4Config *s_ip4; NMSettingIP4Config *s_ip4;
const char *value; const char *value;
char *tmp;
guint32 i, num; guint32 i, num;
GString *searches; GString *searches;
GString *ips; GString *ips;
@ -2387,33 +2400,19 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err
ips = g_string_new (NULL); ips = g_string_new (NULL);
/* IPv4 addresses */ /* IPv4 addresses */
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
char buf[INET_ADDRSTRLEN + 1]; NMIPAddress *addr;
NMIP4Address *addr;
guint32 ip;
addr = nm_setting_ip4_config_get_address (s_ip4, i); addr = nm_setting_ip4_config_get_address (s_ip4, i);
memset (buf, 0, sizeof (buf)); g_string_append_printf (ips, "\"%s/%u",
ip = nm_ip4_address_get_address (addr); nm_ip_address_get_address (addr),
inet_ntop (AF_INET, (const void *) &ip, &buf[0], nm_ip_address_get_prefix (addr));
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);
/* only the first gateway will be written */ /* only the first gateway will be written */
if (!has_def_route && nm_ip4_address_get_gateway (addr)) { if (!has_def_route && nm_ip_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));
g_string_append_printf (routes, g_string_append_printf (routes,
"\"default via %s\" ", "\"default via %s\" ",
&buf[0]); nm_ip_address_get_gateway (addr));
has_def_route = TRUE; 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); num = nm_setting_ip4_config_get_num_routes (s_ip4);
if (num > 0) { if (num > 0) {
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
char buf[INET_ADDRSTRLEN + 1]; NMIPRoute *route;
NMIP4Route *route; const char *next_hop;
guint32 ip;
route = nm_setting_ip4_config_get_route (s_ip4, i); route = nm_setting_ip4_config_get_route (s_ip4, i);
memset (buf, 0, sizeof (buf)); next_hop = nm_ip_route_get_next_hop (route);
ip = nm_ip4_route_get_dest (route); if (!next_hop)
inet_ntop (AF_INET, (const void *) &ip, &buf[0], next_hop = "0.0.0.0";
sizeof (buf));
g_string_append_printf (routes, "\"%s", buf);
tmp = g_string_append_printf (routes, "\"%s/%u via %s\" ",
g_strdup_printf ("%u", nm_ip_route_get_dest (route),
nm_ip4_route_get_prefix (route)); nm_ip_route_get_prefix (route),
g_string_append_printf (routes, "/%s via ", tmp); next_hop);
g_free (tmp);
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);
} }
} }
if (routes->len > 0) if (routes->len > 0)
@ -2513,11 +2502,8 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err
static gboolean static gboolean
write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **error) write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **error)
{ {
char dest[INET6_ADDRSTRLEN + 1]; NMIPRoute *route;
char next_hop[INET6_ADDRSTRLEN + 1]; const char *next_hop;
NMIP6Route *route;
const struct in6_addr *ip;
guint32 prefix;
guint32 i, num; guint32 i, num;
GString *routes_string; GString *routes_string;
const char *old_routes; 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++) { for (i = 0; i < num; i++) {
route = nm_setting_ip6_config_get_route (s_ip6, i); route = nm_setting_ip6_config_get_route (s_ip6, i);
memset (dest, 0, sizeof (dest)); next_hop = nm_ip_route_get_next_hop (route);
ip = nm_ip6_route_get_dest (route); if (!next_hop)
inet_ntop (AF_INET6, (const void *) ip, &dest[0], next_hop = "::";
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));
g_string_append_printf (routes_string, "\"%s/%u via %s\" ", 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) if (num > 0)
ifnet_set_data (conn_name, "routes", routes_string->str); 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; NMSettingIP6Config *s_ip6;
const char *value; const char *value;
char *prefix;
guint32 i, num; guint32 i, num;
GString *searches; GString *searches;
char buf[INET6_ADDRSTRLEN + 1]; NMIPAddress *addr;
NMIP6Address *addr;
const struct in6_addr *ip;
s_ip6 = nm_connection_get_setting_ip6_config (connection); s_ip6 = nm_connection_get_setting_ip6_config (connection);
if (!s_ip6) { if (!s_ip6) {
@ -2617,16 +2594,10 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err
ip_str = g_string_new (NULL); ip_str = g_string_new (NULL);
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
addr = nm_setting_ip6_config_get_address (s_ip6, i); addr = nm_setting_ip6_config_get_address (s_ip6, i);
ip = nm_ip6_address_get_address (addr);
prefix = g_string_append_printf (ip_str, "\"%s/%u\"",
g_strdup_printf ("%u", nm_ip_address_get_address (addr),
nm_ip6_address_get_prefix (addr)); nm_ip_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);
} }
tmp = g_strdup_printf ("%s\" %s", config, ip_str->str); tmp = g_strdup_printf ("%s\" %s", config, ip_str->str);
ifnet_set_data (conn_name, "config", tmp); ifnet_set_data (conn_name, "config", tmp);

View file

@ -366,23 +366,21 @@ create_ip4_block (gchar * ip)
ip_mask = g_strsplit (ip, "/", 0); ip_mask = g_strsplit (ip, "/", 0);
length = g_strv_length (ip_mask); 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; goto error;
iblock->ip = tmp_ip4_addr; iblock->ip = g_strdup (ip_mask[0]);
prefix = ip_mask[1]; prefix = ip_mask[1];
i = 0; i = 0;
while (i < length && g_ascii_isdigit (prefix[i])) while (i < length && g_ascii_isdigit (prefix[i]))
i++; i++;
prefix[i] = '\0'; prefix[i] = '\0';
iblock->netmask = nm_utils_ip4_prefix_to_netmask ((guint32) iblock->prefix = (guint32) atoi (ip_mask[1]);
atoi (ip_mask
[1]));
} else if (strstr (ip, "netmask")) { } else if (strstr (ip, "netmask")) {
ip_mask = g_strsplit (ip, " ", 0); ip_mask = g_strsplit (ip, " ", 0);
length = g_strv_length (ip_mask); 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; goto error;
iblock->ip = tmp_ip4_addr; iblock->ip = g_strdup (ip_mask[0]);
i = 0; i = 0;
while (i < length && !strstr (ip_mask[++i], "netmask")) ; while (i < length && !strstr (ip_mask[++i], "netmask")) ;
while (i < length && ip_mask[++i][0] == '\0') ; while (i < length && ip_mask[++i][0] == '\0') ;
@ -390,7 +388,7 @@ create_ip4_block (gchar * ip)
goto error; goto error;
if (!inet_pton (AF_INET, ip_mask[i], &tmp_ip4_addr)) if (!inet_pton (AF_INET, ip_mask[i], &tmp_ip4_addr))
goto error; goto error;
iblock->netmask = tmp_ip4_addr; iblock->prefix = nm_utils_ip4_netmask_to_prefix (tmp_ip4_addr);
} else { } else {
g_slice_free (ip_block, iblock); g_slice_free (ip_block, iblock);
if (!is_ip6_address (ip) && !strstr (ip, "dhcp")) if (!is_ip6_address (ip) && !strstr (ip, "dhcp"))
@ -403,26 +401,25 @@ error:
if (!is_ip6_address (ip)) if (!is_ip6_address (ip))
nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 address: %s", ip); nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 address: %s", ip);
g_strfreev (ip_mask); g_strfreev (ip_mask);
g_free (iblock->ip);
g_slice_free (ip_block, iblock); g_slice_free (ip_block, iblock);
return NULL; return NULL;
} }
static ip6_block * static ip_block *
create_ip6_block (gchar * ip) 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); gchar *dup_ip = g_strdup (ip);
struct in6_addr *tmp_ip6_addr = g_slice_new0 (struct in6_addr);
gchar *prefix = NULL; gchar *prefix = NULL;
if ((prefix = strstr (dup_ip, "/")) != NULL) { if ((prefix = strstr (dup_ip, "/")) != NULL) {
*prefix = '\0'; *prefix = '\0';
prefix++; prefix++;
} }
if (!inet_pton (AF_INET6, dup_ip, tmp_ip6_addr)) { if (!nm_utils_ipaddr_valid (AF_INET6, dup_ip))
goto error; goto error;
} iblock->ip = dup_ip;
iblock->ip = tmp_ip6_addr;
if (prefix) { if (prefix) {
errno = 0; errno = 0;
iblock->prefix = strtol (prefix, NULL, 10); iblock->prefix = strtol (prefix, NULL, 10);
@ -431,30 +428,26 @@ create_ip6_block (gchar * ip)
} }
} else } else
iblock->prefix = 64; iblock->prefix = 64;
g_free (dup_ip);
return iblock; return iblock;
error: error:
if (!is_ip4_address (ip)) if (!is_ip4_address (ip))
nm_log_warn (LOGD_SETTINGS, "Can't handle IPv6 address: %s", ip); nm_log_warn (LOGD_SETTINGS, "Can't handle IPv6 address: %s", ip);
g_slice_free (ip6_block, iblock); g_slice_free (ip_block, iblock);
g_slice_free (struct in6_addr, tmp_ip6_addr);
g_free (dup_ip); g_free (dup_ip);
return NULL; return NULL;
} }
static guint32 static char *
get_ip4_gateway (gchar * gateway) get_ip4_gateway (gchar * gateway)
{ {
gchar *tmp, *split; gchar *tmp, *split;
guint32 tmp_ip4_addr;
if (!gateway) if (!gateway)
return 0; return NULL;
tmp = find_gateway_str (gateway); tmp = find_gateway_str (gateway);
if (!tmp) { if (!tmp) {
nm_log_warn (LOGD_SETTINGS, "Couldn't obtain gateway in \"%s\"", gateway); nm_log_warn (LOGD_SETTINGS, "Couldn't obtain gateway in \"%s\"", gateway);
return 0; return NULL;
} }
tmp = g_strdup (tmp); tmp = g_strdup (tmp);
strip_string (tmp, ' '); strip_string (tmp, ' ');
@ -464,43 +457,39 @@ get_ip4_gateway (gchar * gateway)
if ((split = strstr (tmp, "\"")) != NULL) if ((split = strstr (tmp, "\"")) != NULL)
*split = '\0'; *split = '\0';
if (!inet_pton (AF_INET, tmp, &tmp_ip4_addr)) if (!nm_utils_ipaddr_valid (AF_INET, tmp))
goto error; goto error;
g_free (tmp); return tmp;
return tmp_ip4_addr;
error: error:
if (!is_ip6_address (tmp)) if (!is_ip6_address (tmp))
nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 gateway: %s", tmp); nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 gateway: %s", tmp);
g_free (tmp); g_free (tmp);
return 0; return NULL;
} }
static struct in6_addr * static char *
get_ip6_next_hop (gchar * next_hop) get_ip6_next_hop (gchar * next_hop)
{ {
gchar *tmp; gchar *tmp;
struct in6_addr *tmp_ip6_addr = g_slice_new0 (struct in6_addr);
if (!next_hop) if (!next_hop)
return 0; return NULL;
tmp = find_gateway_str (next_hop); tmp = find_gateway_str (next_hop);
if (!tmp) { if (!tmp) {
nm_log_warn (LOGD_SETTINGS, "Couldn't obtain next_hop in \"%s\"", next_hop); nm_log_warn (LOGD_SETTINGS, "Couldn't obtain next_hop in \"%s\"", next_hop);
return 0; return NULL;
} }
tmp = g_strdup (tmp); tmp = g_strdup (tmp);
strip_string (tmp, ' '); strip_string (tmp, ' ');
strip_string (tmp, '"'); strip_string (tmp, '"');
g_strstrip (tmp); g_strstrip (tmp);
if (!inet_pton (AF_INET6, tmp, tmp_ip6_addr)) if (!nm_utils_ipaddr_valid (AF_INET6, tmp))
goto error; goto error;
g_free (tmp); return tmp;
return tmp_ip6_addr;
error: error:
if (!is_ip4_address (tmp)) if (!is_ip4_address (tmp))
nm_log_warn (LOGD_SETTINGS, "Can't handle IPv6 next_hop: %s", tmp); nm_log_warn (LOGD_SETTINGS, "Can't handle IPv6 next_hop: %s", tmp);
g_free (tmp); g_free (tmp);
g_slice_free (struct in6_addr, tmp_ip6_addr);
return NULL; return NULL;
} }
@ -512,7 +501,7 @@ convert_ip4_config_block (const char *conn_name)
guint length; guint length;
guint i; guint i;
gchar *ip; gchar *ip;
guint32 def_gateway = 0; char *def_gateway = NULL;
const char *routes; const char *routes;
ip_block *start = NULL, *current = NULL, *iblock = NULL; 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); iblock = create_ip4_block (ip);
if (iblock == NULL) if (iblock == NULL)
continue; continue;
if (!iblock->gateway && def_gateway != 0) if (!iblock->next_hop && def_gateway != NULL)
iblock->gateway = def_gateway; iblock->next_hop = g_strdup (def_gateway);
if (start == NULL) if (start == NULL)
start = current = iblock; start = current = iblock;
else { else {
@ -541,17 +530,18 @@ convert_ip4_config_block (const char *conn_name)
} }
} }
g_strfreev (ipset); g_strfreev (ipset);
g_free (def_gateway);
return start; return start;
} }
ip6_block * ip_block *
convert_ip6_config_block (const char *conn_name) convert_ip6_config_block (const char *conn_name)
{ {
gchar **ipset; gchar **ipset;
guint length; guint length;
guint i; guint i;
gchar *ip; 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); g_return_val_if_fail (conn_name != NULL, NULL);
ipset = split_addresses (ifnet_get_data (conn_name, "config")); 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++) { for (i = 0; i < length; i++) {
ip = ipset[i]; ip = ipset[i];
ip = strip_string (ip, '"'); ip = strip_string (ip, '"');
iblock = create_ip6_block (ip); iblock = create_ip_block (ip);
if (iblock == NULL) if (iblock == NULL)
continue; continue;
if (start == NULL) if (start == NULL)
@ -595,7 +585,7 @@ convert_ip4_routes_block (const char *conn_name)
iblock = create_ip4_block (ip); iblock = create_ip4_block (ip);
if (iblock == NULL) if (iblock == NULL)
continue; continue;
iblock->gateway = get_ip4_gateway (ip); iblock->next_hop = get_ip4_gateway (ip);
if (start == NULL) if (start == NULL)
start = current = iblock; start = current = iblock;
else { else {
@ -607,15 +597,14 @@ convert_ip4_routes_block (const char *conn_name)
return start; return start;
} }
ip6_block * ip_block *
convert_ip6_routes_block (const char *conn_name) convert_ip6_routes_block (const char *conn_name)
{ {
gchar **ipset; gchar **ipset;
guint length; guint length;
guint i; guint i;
gchar *ip, *tmp_addr; gchar *ip, *tmp_addr;
ip6_block *start = NULL, *current = NULL, *iblock = NULL; ip_block *start = NULL, *current = NULL, *iblock = NULL;
struct in6_addr *tmp_ip6_addr;
g_return_val_if_fail (conn_name != NULL, NULL); g_return_val_if_fail (conn_name != NULL, NULL);
ipset = split_routes (ifnet_get_data (conn_name, "routes")); 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)) if (!is_ip6_address (tmp_addr))
continue; continue;
else { else {
tmp_ip6_addr = g_slice_new0 (struct in6_addr); iblock = g_slice_new0 (ip_block);
iblock->ip = g_strdup ("::");
if (inet_pton (AF_INET6, "::", tmp_ip6_addr)) { iblock->prefix = 128;
iblock = g_slice_new0 (ip6_block);
iblock->ip = tmp_ip6_addr;
iblock->prefix = 128;
} else {
g_slice_free (struct in6_addr,
tmp_ip6_addr);
continue;
}
} }
} else } else
iblock = create_ip6_block (ip); iblock = create_ip_block (ip);
if (iblock == NULL) if (iblock == NULL)
continue; continue;
iblock->next_hop = get_ip6_next_hop (ip); iblock->next_hop = get_ip6_next_hop (ip);
if (iblock->next_hop == NULL) { if (iblock->next_hop == NULL) {
destroy_ip6_block (iblock); destroy_ip_block (iblock);
continue; continue;
} }
if (start == NULL) if (start == NULL)
@ -664,18 +645,11 @@ convert_ip6_routes_block (const char *conn_name)
void void
destroy_ip_block (ip_block * iblock) destroy_ip_block (ip_block * iblock)
{ {
g_free (iblock->ip);
g_free (iblock->next_hop);
g_slice_free (ip_block, iblock); 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 void
set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name) set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name)
{ {

View file

@ -31,19 +31,12 @@
#define has_default_ip6_route(conn_name) has_default_route((conn_name), &is_ip6_address) #define has_default_ip6_route(conn_name) has_default_route((conn_name), &is_ip6_address)
typedef struct _ip_block { typedef struct _ip_block {
guint32 ip; char *ip;
guint32 netmask; guint32 prefix;
guint32 gateway; char *next_hop;
struct _ip_block *next; struct _ip_block *next;
} ip_block; } 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); gchar *read_hostname (const char *path);
gboolean write_hostname (const char *path, const char *hostname); gboolean write_hostname (const char *path, const char *hostname);
gboolean is_static_ip4 (const char *conn_name); 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); gboolean reload_parsers (void);
ip_block *convert_ip4_config_block (const char *conn_name); 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); 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_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_ip4_dns_servers (NMSettingIP4Config * s_ip4, const char *conn_name);
void set_ip6_dns_servers (NMSettingIP6Config * s_ip6, const char *conn_name); void set_ip6_dns_servers (NMSettingIP6Config * s_ip6, const char *conn_name);

View file

@ -160,25 +160,14 @@ test_is_ip6_address (void)
} }
static 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; ASSERT (strcmp (ip, iblock->ip) == 0, "check ip",
guint32 tmp_ip4_addr; "ip expected:%s, find:%s", ip, iblock->ip);
ASSERT (prefix == iblock->prefix, "check netmask",
str = malloc (INET_ADDRSTRLEN); "prefix expected:%d, find:%d", prefix, iblock->prefix);
tmp_ip4_addr = iblock->ip; ASSERT (g_strcmp0 (gateway, iblock->next_hop) == 0, "check gateway",
inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN); "gateway expected:%s, find:%s", gateway, iblock->next_hop);
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);
} }
static void static void
@ -189,14 +178,12 @@ test_convert_ipv4_config_block (void)
ASSERT (iblock != NULL, "convert ipv4 block", ASSERT (iblock != NULL, "convert ipv4 block",
"block eth0 should not be NULL"); "block eth0 should not be NULL");
check_ip_block (iblock, "202.117.16.121", "255.255.255.0", check_ip_block (iblock, "202.117.16.121", 24, "202.117.16.1");
"202.117.16.1");
iblock = iblock->next; iblock = iblock->next;
destroy_ip_block (tmp); destroy_ip_block (tmp);
ASSERT (iblock != NULL, "convert ipv4 block", ASSERT (iblock != NULL, "convert ipv4 block",
"block eth0 should have a second IP address"); "block eth0 should have a second IP address");
check_ip_block (iblock, "192.168.4.121", "255.255.255.0", check_ip_block (iblock, "192.168.4.121", 24, "202.117.16.1");
"202.117.16.1");
destroy_ip_block (iblock); destroy_ip_block (iblock);
g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING,
@ -206,7 +193,7 @@ test_convert_ipv4_config_block (void)
ASSERT (iblock != NULL ASSERT (iblock != NULL
&& iblock->next == NULL, && iblock->next == NULL,
"convert error IPv4 address", "should only get one address"); "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); destroy_ip_block (iblock);
g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, 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"); iblock = convert_ip4_config_block ("eth3");
ASSERT (iblock == NULL, "convert config_block", ASSERT (iblock == NULL, "convert config_block",
"convert error configuration"); "convert error configuration");
destroy_ip_block (iblock);
} }
static void static void
@ -224,7 +210,7 @@ test_convert_ipv4_routes_block (void)
ip_block *tmp = iblock; ip_block *tmp = iblock;
ASSERT (iblock != NULL, "convert ip4 routes", "should get one route"); 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; iblock = iblock->next;
destroy_ip_block (tmp); destroy_ip_block (tmp);
ASSERT (iblock == NULL, "convert ip4 routes", ASSERT (iblock == NULL, "convert ip4 routes",
@ -234,7 +220,7 @@ test_convert_ipv4_routes_block (void)
tmp = iblock; tmp = iblock;
ASSERT (iblock != NULL, "convert ip4 routes", "should get one route"); 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; iblock = iblock->next;
destroy_ip_block (tmp); destroy_ip_block (tmp);
ASSERT (iblock == NULL, "convert ip4 routes", ASSERT (iblock == NULL, "convert ip4 routes",

View file

@ -447,8 +447,8 @@ update_ip4_setting_from_if_block(NMConnection *connection,
if (!is_static) { if (!is_static) {
g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL);
} else { } else {
guint32 tmp_addr, tmp_mask, tmp_gw; guint32 tmp_mask;
NMIP4Address *addr; NMIPAddress *addr;
const char *address_v; const char *address_v;
const char *netmask_v; const char *netmask_v;
const char *gateway_v; const char *gateway_v;
@ -460,10 +460,9 @@ update_ip4_setting_from_if_block(NMConnection *connection,
/* Address */ /* Address */
address_v = ifparser_getkey (block, "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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Missing IPv4 address '%s'", "Missing IPv4 address");
address_v ? address_v : "(none)");
goto error; goto error;
} }
@ -472,11 +471,6 @@ update_ip4_setting_from_if_block(NMConnection *connection,
if (netmask_v) { if (netmask_v) {
if (strlen (netmask_v) < 7) { if (strlen (netmask_v) < 7) {
netmask_int = atoi (netmask_v); 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)) { } else if (!inet_pton (AF_INET, netmask_v, &tmp_mask)) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid IPv4 netmask '%s'", netmask_v); "Invalid IPv4 netmask '%s'", netmask_v);
@ -490,17 +484,11 @@ update_ip4_setting_from_if_block(NMConnection *connection,
gateway_v = ifparser_getkey (block, "gateway"); gateway_v = ifparser_getkey (block, "gateway");
if (!gateway_v) if (!gateway_v)
gateway_v = address_v; /* dcbw: whaaa?? */ 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 */ /* Add the new address to the setting */
addr = nm_ip4_address_new (); addr = nm_ip_address_new (AF_INET, address_v, netmask_int, gateway_v, error);
nm_ip4_address_set_address (addr, tmp_addr); if (!addr)
nm_ip4_address_set_prefix (addr, netmask_int); goto error;
nm_ip4_address_set_gateway (addr, tmp_gw);
if (nm_setting_ip4_config_add_address (s_ip4, addr)) { if (nm_setting_ip4_config_add_address (s_ip4, addr)) {
nm_log_info (LOGD_SETTINGS, "addresses count: %d", nm_log_info (LOGD_SETTINGS, "addresses count: %d",
@ -508,7 +496,7 @@ update_ip4_setting_from_if_block(NMConnection *connection,
} else { } else {
nm_log_info (LOGD_SETTINGS, "ignoring duplicate IP4 address"); 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"); nameserver_v = ifparser_getkey (block, "dns-nameserver");
ifupdown_ip4_add_dns (s_ip4, nameserver_v); ifupdown_ip4_add_dns (s_ip4, nameserver_v);
@ -582,8 +570,7 @@ update_ip6_setting_from_if_block(NMConnection *connection,
if (!is_static) { if (!is_static) {
g_object_set(s_ip6, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); g_object_set(s_ip6, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL);
} else { } else {
struct in6_addr tmp_addr, tmp_gw; NMIPAddress *addr;
NMIP6Address *addr;
const char *address_v; const char *address_v;
const char *prefix_v; const char *prefix_v;
const char *gateway_v; const char *gateway_v;
@ -595,10 +582,9 @@ update_ip6_setting_from_if_block(NMConnection *connection,
/* Address */ /* Address */
address_v = ifparser_getkey(block, "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, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Missing IPv6 address '%s'", "Missing IPv6 address");
address_v ? address_v : "(none)");
goto error; goto error;
} }
@ -611,17 +597,11 @@ update_ip6_setting_from_if_block(NMConnection *connection,
gateway_v = ifparser_getkey (block, "gateway"); gateway_v = ifparser_getkey (block, "gateway");
if (!gateway_v) if (!gateway_v)
gateway_v = address_v; /* dcbw: whaaa?? */ 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 */ /* Add the new address to the setting */
addr = nm_ip6_address_new (); addr = nm_ip_address_new (AF_INET6, address_v, prefix_int, gateway_v, error);
nm_ip6_address_set_address (addr, &tmp_addr); if (!addr)
nm_ip6_address_set_prefix (addr, prefix_int); goto error;
nm_ip6_address_set_gateway (addr, &tmp_gw);
if (nm_setting_ip6_config_add_address (s_ip6, addr)) { if (nm_setting_ip6_config_add_address (s_ip6, addr)) {
nm_log_info (LOGD_SETTINGS, "addresses count: %d", nm_log_info (LOGD_SETTINGS, "addresses count: %d",
@ -629,7 +609,7 @@ update_ip6_setting_from_if_block(NMConnection *connection,
} else { } else {
nm_log_info (LOGD_SETTINGS, "ignoring duplicate IP6 address"); 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"); nameserver_v = ifparser_getkey(block, "dns-nameserver");
ifupdown_ip6_add_dns (s_ip6, nameserver_v); ifupdown_ip6_add_dns (s_ip6, nameserver_v);

View file

@ -464,13 +464,10 @@ test17_read_static_ipv4 (const char *path)
char *unmanaged = NULL; char *unmanaged = NULL;
GError *error = NULL; GError *error = NULL;
const char* tmp; const char* tmp;
const char *expected_address = "10.0.0.3";
const char *expected_id = "Ifupdown (eth0)"; const char *expected_id = "Ifupdown (eth0)";
const char *expected_search1 = "example.com"; const char *expected_search1 = "example.com";
const char *expected_search2 = "foo.example.com"; const char *expected_search2 = "foo.example.com";
guint32 expected_prefix = 8; NMIPAddress *ip4_addr;
NMIP4Address *ip4_addr;
guint32 addr;
#define TEST17_NAME "wired-static-verify-ip4" #define TEST17_NAME "wired-static-verify-ip4"
if_block *block = NULL; if_block *block = NULL;
@ -521,10 +518,6 @@ test17_read_static_ipv4 (const char *path)
/* ===== IPv4 SETTING ===== */ /* ===== 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); s_ip4 = nm_connection_get_setting_ip4_config (connection);
ASSERT (s_ip4 != NULL, ASSERT (s_ip4 != NULL,
TEST17_NAME, "failed to verify %s: missing %s setting", 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); NM_SETTING_IP4_CONFIG_ADDRESSES);
ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0);
ASSERT (ip4_addr, g_assert (ip4_addr != NULL);
TEST17_NAME, "failed to verify %s: missing IP4 address #1", g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3");
file); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8);
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);
/* DNS Addresses */ /* DNS Addresses */
ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 2, 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; char *unmanaged = NULL;
GError *error = NULL; GError *error = NULL;
const char* tmp; const char* tmp;
const char *expected_address = "fc00::1";
const char *expected_id = "Ifupdown (myip6tunnel)"; const char *expected_id = "Ifupdown (myip6tunnel)";
const char *expected_search1 = "example.com"; const char *expected_search1 = "example.com";
const char *expected_search2 = "foo.example.com"; const char *expected_search2 = "foo.example.com";
guint32 expected_prefix = 64; NMIPAddress *ip6_addr;
NMIP6Address *ip6_addr;
struct in6_addr addr;
if_block *block = NULL; if_block *block = NULL;
#define TEST18_NAME "wired-static-verify-ip6" #define TEST18_NAME "wired-static-verify-ip6"
const char* file = "test18-" TEST18_NAME; const char* file = "test18-" TEST18_NAME;
@ -691,11 +673,6 @@ test18_read_static_ipv6 (const char *path)
/* ===== IPv6 SETTING ===== */ /* ===== 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); s_ip6 = nm_connection_get_setting_ip6_config (connection);
ASSERT (s_ip6 != NULL, ASSERT (s_ip6 != NULL,
TEST18_NAME, TEST18_NAME,
@ -721,27 +698,9 @@ test18_read_static_ipv6 (const char *path)
NM_SETTING_IP6_CONFIG_ADDRESSES); NM_SETTING_IP6_CONFIG_ADDRESSES);
ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0);
ASSERT (ip6_addr, g_assert (ip6_addr != NULL);
TEST18_NAME, g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "fc00::1");
"failed to verify %s: missing %s / %s #1", g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64);
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);
/* DNS Addresses */ /* DNS Addresses */
ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 2, 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; NMSettingIP4Config *s_ip4;
char *unmanaged = NULL; char *unmanaged = NULL;
GError *error = NULL; GError *error = NULL;
const char *expected_address = "10.0.0.3"; NMIPAddress *ip4_addr;
guint32 expected_prefix = 8;
NMIP4Address *ip4_addr;
guint32 addr;
#define TEST19_NAME "wired-static-verify-ip4-plen" #define TEST19_NAME "wired-static-verify-ip4-plen"
if_block *block = NULL; if_block *block = NULL;
@ -838,10 +794,6 @@ test19_read_static_ipv4_plen (const char *path)
/* ===== IPv4 SETTING ===== */ /* ===== 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); s_ip4 = nm_connection_get_setting_ip4_config (connection);
ASSERT (s_ip4 != NULL, ASSERT (s_ip4 != NULL,
TEST19_NAME, "failed to verify %s: missing %s setting", 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); NM_SETTING_IP4_CONFIG_ADDRESSES);
ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0);
ASSERT (ip4_addr, g_assert (ip4_addr != NULL);
TEST19_NAME, "failed to verify %s: missing IP4 address #1", g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3");
file); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8);
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_object_unref (connection); g_object_unref (connection);
} }

View file

@ -108,36 +108,37 @@ get_one_int (const char *str, guint32 max_val, const char *key_name, guint32 *ou
} }
static gpointer 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; gpointer result;
guint32 addr;
guint32 address = 0;
guint32 gateway = 0;
guint32 metric = 0; guint32 metric = 0;
int err; GError *error = NULL;
g_return_val_if_fail (address_str, 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 */ /* Gateway */
if (gateway_str && gateway_str[0]) { if (gateway_str && gateway_str[0]) {
err = inet_pton (AF_INET, gateway_str, &addr); if (!nm_utils_ipaddr_valid (family, gateway_str)) {
if (err <= 0) { /* Try workaround for routes written by broken keyfile writer.
nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv4 gateway '%s'", __func__, gateway_str); * Due to bug bgo#719851, an older version of writer would have
return NULL; * 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 interpreting gateway_str as metric to accept such
* invalid routes. This broken syntax should not be not officially
* supported.
**/
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 gateway '%s'", __func__, gateway_str);
return NULL;
}
} }
gateway = addr; } else
} gateway_str = NULL;
else
gateway = 0;
/* parse metric, default to 0 */ /* parse metric, default to 0 */
if (metric_str) { if (metric_str) {
@ -145,95 +146,21 @@ build_ip4_address_or_route (const char *key_name, const char *address_str, guint
return NULL; return NULL;
} }
if (route) { if (route)
result = nm_ip4_route_new (); result = nm_ip_route_new (family, address_str, plen, gateway_str, metric, &error);
nm_ip4_route_set_dest (result, address); else
nm_ip4_route_set_prefix (result, plen); result = nm_ip_address_new (family, address_str, plen, gateway_str, &error);
nm_ip4_route_set_next_hop (result, gateway); if (!result) {
nm_ip4_route_set_metric (result, metric); nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid %s %s: %s", __func__,
} else { family == AF_INET ? "IPv4" : "IPv6",
result = nm_ip4_address_new (); route ? "route" : "address",
nm_ip4_address_set_address (result, address); error->message);
nm_ip4_address_set_prefix (result, plen); g_error_free (error);
nm_ip4_address_set_gateway (result, gateway);
} }
return result; 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) {
/* 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
* 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;
else {
nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv6 gateway '%s'", __func__, gateway_str);
goto error_out;
}
}
} else
addr = in6addr_any;
if (route) {
nm_ip6_route_set_next_hop (result, &addr);
/* 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;
error_out:
if (route)
nm_ip6_route_unref (result);
else
nm_ip4_route_unref (result);
return NULL;
}
/* On success, returns pointer to the zero-terminated field (original @current). /* On success, returns pointer to the zero-terminated field (original @current).
* The @current * pointer target is set to point to the rest of the input * The @current * pointer target is set to point to the rest of the input
* or %NULL if there is no more input. Sets error to %NULL for convenience. * or %NULL if there is no more input. Sets error to %NULL for convenience.
@ -390,8 +317,8 @@ read_one_ip_address_or_route (GKeyFile *file,
} }
/* build the appropriate data structure for NetworkManager settings */ /* build the appropriate data structure for NetworkManager settings */
result = (ipv6 ? build_ip6_address_or_route : build_ip4_address_or_route) ( result = build_address_or_route (key_name, address_str, plen, gateway_str, metric_str,
key_name, address_str, plen, gateway_str, metric_str, route); ipv6 ? AF_INET6 : AF_INET, route);
g_free (value); g_free (value);
return result; return result;
@ -413,17 +340,10 @@ ip_address_or_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfi
GDestroyNotify free_func; GDestroyNotify free_func;
int i; int i;
if (ipv6) { if (routes)
if (routes) free_func = (GDestroyNotify) nm_ip_route_unref;
free_func = (GDestroyNotify) nm_ip6_route_unref; else
else free_func = (GDestroyNotify) nm_ip_address_unref;
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;
}
list = g_ptr_array_new_with_free_func (free_func); list = g_ptr_array_new_with_free_func (free_func);
for (i = -1; i < 1000; i++) { for (i = -1; i < 1000; i++) {

View file

@ -38,69 +38,51 @@
#define TEST_WIRELESS_FILE TEST_KEYFILES_DIR"/Test_Wireless_Connection" #define TEST_WIRELESS_FILE TEST_KEYFILES_DIR"/Test_Wireless_Connection"
static void 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); NMIPAddress *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);
g_assert (ip4); g_assert (ip4);
g_assert (nm_ip4_address_get_address (ip4) == address); g_assert_cmpstr (nm_ip_address_get_address (ip4), ==, address);
g_assert (nm_ip4_address_get_prefix (ip4) == plen); g_assert_cmpint (nm_ip_address_get_prefix (ip4), ==, plen);
g_assert (nm_ip4_address_get_gateway (ip4) == gateway); g_assert_cmpstr (nm_ip_address_get_gateway (ip4), ==, gateway);
} }
static void 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); NMIPAddress *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);
g_assert (ip6); g_assert (ip6);
g_assert (!memcmp (nm_ip6_address_get_address (ip6), &address, sizeof(address))); g_assert_cmpstr (nm_ip_address_get_address (ip6), ==, address);
g_assert (nm_ip6_address_get_prefix (ip6) == plen); g_assert_cmpint (nm_ip_address_get_prefix (ip6), ==, plen);
g_assert (!memcmp (nm_ip6_address_get_gateway (ip6), &gateway, sizeof(gateway))); g_assert_cmpstr (nm_ip_address_get_gateway (ip6), ==, gateway);
} }
static void static void
check_ip4_route (NMSettingIP4Config *config, int idx, const char *destination_str, int plen, check_ip4_route (NMSettingIP4Config *config, int idx, const char *destination, int plen,
const char *nexthop_str, int metric) const char *next_hop, int metric)
{ {
NMIP4Route *route = nm_setting_ip4_config_get_route (config, idx); NMIPRoute *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);
g_assert (route); g_assert (route);
g_assert (nm_ip4_route_get_dest (route) == destination); g_assert_cmpstr (nm_ip_route_get_dest (route), ==, destination);
g_assert (nm_ip4_route_get_prefix (route) == plen); g_assert_cmpint (nm_ip_route_get_prefix (route), ==, plen);
g_assert (nm_ip4_route_get_next_hop (route) == nexthop); g_assert_cmpstr (nm_ip_route_get_next_hop (route), ==, next_hop);
g_assert (nm_ip4_route_get_metric (route) == metric); g_assert_cmpint (nm_ip_route_get_metric (route), ==, metric);
} }
static void static void
check_ip6_route (NMSettingIP6Config *config, int idx, const char *destination_str, int plen, check_ip6_route (NMSettingIP6Config *config, int idx, const char *destination, int plen,
const char *next_hop_str, int metric) const char *next_hop, int metric)
{ {
NMIP6Route *route = nm_setting_ip6_config_get_route (config, idx); NMIPRoute *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);
g_assert (route); g_assert (route);
g_assert (!memcmp (nm_ip6_route_get_dest (route), &destination, sizeof(destination))); g_assert_cmpstr (nm_ip_route_get_dest (route), ==, destination);
g_assert (nm_ip6_route_get_prefix (route) == plen); g_assert_cmpint (nm_ip_route_get_prefix (route), ==, plen);
g_assert (!memcmp (nm_ip6_route_get_next_hop (route), &next_hop, sizeof(next_hop))); g_assert_cmpstr (nm_ip_route_get_next_hop (route), ==, next_hop);
g_assert (nm_ip6_route_get_metric (route) == metric); g_assert_cmpint (nm_ip_route_get_metric (route), ==, metric);
} }
static NMConnection * 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, 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, 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, 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, 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 */ /* IPv4 routes */
g_assert (nm_setting_ip4_config_get_num_routes (s_ip4) == 12); 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, 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, 2, "1.1.1.2", 12, NULL, 0);
check_ip4_route (s_ip4, 3, "1.1.1.3", 13, "0.0.0.0", 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, 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, 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, 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, 7, "1.1.1.7", 17, NULL, 0);
check_ip4_route (s_ip4, 8, "1.1.1.8", 18, "0.0.0.0", 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, "0.0.0.0", 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, "0.0.0.0", 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, "0.0.0.0", 21); check_ip4_route (s_ip4, 11, "1.1.1.11", 21, NULL, 21);
/* ===== IPv6 SETTING ===== */ /* ===== IPv6 SETTING ===== */
@ -371,25 +353,25 @@ test_read_valid_wired_connection (void)
/* IPv6 addresses */ /* IPv6 addresses */
g_assert (nm_setting_ip6_config_get_num_addresses (s_ip6) == 10); 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, 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, 1, "abcd:1234:ffff::cdde", 64, NULL);
check_ip6_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96, "::"); 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, "::"); 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, "::"); 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, "::"); 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, "::"); 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, "::"); 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, "::"); 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::09"); check_ip6_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, "1::9");
/* Route #1 */ /* Route #1 */
g_assert (nm_setting_ip6_config_get_num_routes (s_ip6) == 7); 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, 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, 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, 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, "::", 1); 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, "::", 5); 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, "::", 6); 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, "::", 0); check_ip6_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, NULL, 0);
g_object_unref (connection); g_object_unref (connection);
} }
@ -399,20 +381,13 @@ add_one_ip4_address (NMSettingIP4Config *s_ip4,
const char *gw, const char *gw,
guint32 prefix) guint32 prefix)
{ {
guint32 tmp; NMIPAddress *ip4_addr;
NMIP4Address *ip4_addr; GError *error = NULL;
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);
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_setting_ip4_config_add_address (s_ip4, ip4_addr);
nm_ip4_address_unref (ip4_addr); nm_ip_address_unref (ip4_addr);
} }
static void static void
@ -422,21 +397,13 @@ add_one_ip4_route (NMSettingIP4Config *s_ip4,
guint32 prefix, guint32 prefix,
guint32 metric) guint32 metric)
{ {
guint32 addr; NMIPRoute *route;
NMIP4Route *route; GError *error = NULL;
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);
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_setting_ip4_config_add_route (s_ip4, route);
nm_ip4_route_unref (route); nm_ip_route_unref (route);
} }
static void static void
@ -445,22 +412,13 @@ add_one_ip6_address (NMSettingIP6Config *s_ip6,
guint32 prefix, guint32 prefix,
const char *gw) const char *gw)
{ {
struct in6_addr tmp; NMIPAddress *ip6_addr;
NMIP6Address *ip6_addr; GError *error = NULL;
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);
}
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_setting_ip6_config_add_address (s_ip6, ip6_addr);
nm_ip6_address_unref (ip6_addr); nm_ip_address_unref (ip6_addr);
} }
static void static void
@ -470,21 +428,13 @@ add_one_ip6_route (NMSettingIP6Config *s_ip6,
guint32 prefix, guint32 prefix,
guint32 metric) guint32 metric)
{ {
struct in6_addr addr; NMIPRoute *route;
NMIP6Route *route; GError *error = NULL;
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);
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_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 = "1.1.1.1";
const char *route2_nh = "1.2.1.1"; const char *route2_nh = "1.2.1.1";
const char *route3 = "2.2.2.2"; 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 = "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_1 = "1::cafe";
const char *dns6_2 = "2::cafe"; const char *dns6_2 = "2::cafe";
const char *address6_1 = "abcd::beef"; const char *address6_1 = "abcd::beef";

View file

@ -97,12 +97,12 @@ write_array_of_uint (GKeyFile *file,
} }
static void static void
ip4_dns_writer (GKeyFile *file, dns_writer (GKeyFile *file,
const char *keyfile_dir, const char *keyfile_dir,
const char *uuid, const char *uuid,
NMSetting *setting, NMSetting *setting,
const char *key, const char *key,
const GValue *value) const GValue *value)
{ {
char **list; char **list;
@ -114,51 +114,59 @@ ip4_dns_writer (GKeyFile *file,
} }
static void static void
write_ip4_values (GKeyFile *file, write_ip_values (GKeyFile *file,
const char *setting_name, const char *setting_name,
GPtrArray *array, GPtrArray *array,
gboolean is_route) gboolean is_route)
{ {
GString *output; GString *output;
int i; int family, i;
guint32 addr, gw, plen, metric; const char *addr, *gw;
guint32 plen, metric;
char key_name[30], *key_name_idx; char key_name[30], *key_name_idx;
if (!array->len) if (!array->len)
return; return;
family = !strcmp (setting_name, NM_SETTING_IP4_CONFIG_SETTING_NAME) ? AF_INET : AF_INET6;
strcpy (key_name, is_route ? "route" : "address"); strcpy (key_name, is_route ? "route" : "address");
key_name_idx = key_name + strlen (key_name); key_name_idx = key_name + strlen (key_name);
output = g_string_sized_new (2*INET_ADDRSTRLEN + 10); output = g_string_sized_new (2*INET_ADDRSTRLEN + 10);
for (i = 0; i < array->len; i++) { for (i = 0; i < array->len; i++) {
if (is_route) { if (is_route) {
NMIP4Route *route = array->pdata[i]; NMIPRoute *route = array->pdata[i];
addr = nm_ip4_route_get_dest (route); addr = nm_ip_route_get_dest (route);
plen = nm_ip4_route_get_prefix (route); plen = nm_ip_route_get_prefix (route);
gw = nm_ip4_route_get_next_hop (route); gw = nm_ip_route_get_next_hop (route);
metric = nm_ip4_route_get_metric (route); metric = nm_ip_route_get_metric (route);
} else { } else {
NMIP4Address *address = array->pdata[i]; NMIPAddress *address = array->pdata[i];
addr = nm_ip4_address_get_address (address); addr = nm_ip_address_get_address (address);
plen = nm_ip4_address_get_prefix (address); plen = nm_ip_address_get_prefix (address);
gw = nm_ip4_address_get_gateway (address); gw = nm_ip_address_get_gateway (address);
metric = 0; metric = 0;
} }
g_string_set_size (output, 0); g_string_set_size (output, 0);
g_string_append_printf (output, "%s/%u", g_string_append_printf (output, "%s/%u", addr, plen);
nm_utils_inet4_ntop (addr, NULL),
(unsigned) plen);
if (metric || gw) { if (metric || gw) {
/* Older versions of the plugin do not support the form /* Older versions of the plugin do not support the form
* "a.b.c.d/plen,,metric", so, we always have to write the * "a.b.c.d/plen,,metric", so, we always have to write the
* gateway, even if it's 0.0.0.0. * gateway, even if there isn't one.
* The current version support reading of the above form. */ * The current version supports reading of the above form.
g_string_append_c (output, ','); */
g_string_append (output, nm_utils_inet4_ntop (gw, NULL)); if (!gw) {
if (family == AF_INET)
gw = "0.0.0.0";
else
gw = "::";
}
g_string_append_printf (output, ",%s", gw);
if (metric) if (metric)
g_string_append_printf (output, ",%lu", (unsigned long) metric); g_string_append_printf (output, ",%lu", (unsigned long) metric);
} }
@ -170,19 +178,19 @@ write_ip4_values (GKeyFile *file,
} }
static void static void
ip4_addr_writer (GKeyFile *file, addr_writer (GKeyFile *file,
const char *keyfile_dir, const char *keyfile_dir,
const char *uuid, const char *uuid,
NMSetting *setting, NMSetting *setting,
const char *key, const char *key,
const GValue *value) const GValue *value)
{ {
GPtrArray *array; GPtrArray *array;
const char *setting_name = nm_setting_get_name (setting); const char *setting_name = nm_setting_get_name (setting);
array = (GPtrArray *) g_value_get_boxed (value); array = (GPtrArray *) g_value_get_boxed (value);
if (array && array->len) if (array && array->len)
write_ip4_values (file, setting_name, array, FALSE); write_ip_values (file, setting_name, array, FALSE);
} }
static void static void
@ -197,154 +205,21 @@ ip4_addr_label_writer (GKeyFile *file,
} }
static void static void
ip4_route_writer (GKeyFile *file, route_writer (GKeyFile *file,
const char *keyfile_dir, const char *keyfile_dir,
const char *uuid, const char *uuid,
NMSetting *setting, NMSetting *setting,
const char *key, const char *key,
const GValue *value) const GValue *value)
{ {
GPtrArray *array; GPtrArray *array;
const char *setting_name = nm_setting_get_name (setting); const char *setting_name = nm_setting_get_name (setting);
array = (GPtrArray *) g_value_get_boxed (value); array = (GPtrArray *) g_value_get_boxed (value);
if (array && array->len) if (array && array->len)
write_ip4_values (file, setting_name, array, TRUE); write_ip_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);
}
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 static void
write_hash_of_string (GKeyFile *file, write_hash_of_string (GKeyFile *file,
NMSetting *setting, NMSetting *setting,
@ -704,25 +579,25 @@ static KeyWriter key_writers[] = {
setting_alias_writer }, setting_alias_writer },
{ NM_SETTING_IP4_CONFIG_SETTING_NAME, { NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP4_CONFIG_ADDRESSES, NM_SETTING_IP4_CONFIG_ADDRESSES,
ip4_addr_writer }, addr_writer },
{ NM_SETTING_IP4_CONFIG_SETTING_NAME, { NM_SETTING_IP4_CONFIG_SETTING_NAME,
"address-labels", "address-labels",
ip4_addr_label_writer }, ip4_addr_label_writer },
{ NM_SETTING_IP6_CONFIG_SETTING_NAME, { NM_SETTING_IP6_CONFIG_SETTING_NAME,
NM_SETTING_IP6_CONFIG_ADDRESSES, NM_SETTING_IP6_CONFIG_ADDRESSES,
ip6_addr_writer }, addr_writer },
{ NM_SETTING_IP4_CONFIG_SETTING_NAME, { NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP4_CONFIG_ROUTES, NM_SETTING_IP4_CONFIG_ROUTES,
ip4_route_writer }, route_writer },
{ NM_SETTING_IP6_CONFIG_SETTING_NAME, { NM_SETTING_IP6_CONFIG_SETTING_NAME,
NM_SETTING_IP6_CONFIG_ROUTES, NM_SETTING_IP6_CONFIG_ROUTES,
ip6_route_writer }, route_writer },
{ NM_SETTING_IP4_CONFIG_SETTING_NAME, { NM_SETTING_IP4_CONFIG_SETTING_NAME,
NM_SETTING_IP4_CONFIG_DNS, NM_SETTING_IP4_CONFIG_DNS,
ip4_dns_writer }, dns_writer },
{ NM_SETTING_IP6_CONFIG_SETTING_NAME, { NM_SETTING_IP6_CONFIG_SETTING_NAME,
NM_SETTING_IP6_CONFIG_DNS, NM_SETTING_IP6_CONFIG_DNS,
ip6_dns_writer }, dns_writer },
{ NM_SETTING_WIRELESS_SETTING_NAME, { NM_SETTING_WIRELESS_SETTING_NAME,
NM_SETTING_WIRELESS_SSID, NM_SETTING_WIRELESS_SSID,
ssid_writer }, ssid_writer },

View file

@ -576,8 +576,8 @@ test_connection_no_match_ip4_addr (void)
GSList *connections = NULL; GSList *connections = NULL;
NMSettingIP4Config *s_ip4; NMSettingIP4Config *s_ip4;
NMSettingIP6Config *s_ip6; NMSettingIP6Config *s_ip6;
NMIP4Address *nm_addr; NMIPAddress *nm_addr;
guint32 addr, gw; GError *error = NULL;
orig = _match_connection_new (); orig = _match_connection_new ();
copy = nm_simple_connection_new_clone (orig); 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), g_object_set (G_OBJECT (s_ip4),
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
NULL); NULL);
nm_addr = nm_ip4_address_new (); nm_addr = nm_ip_address_new (AF_INET, "1.1.1.4", 24, "1.1.1.254", &error);
inet_pton (AF_INET, "1.1.1.4", &addr); g_assert_no_error (error);
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_setting_ip4_config_add_address (s_ip4, nm_addr); 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); s_ip4 = nm_connection_get_setting_ip4_config (copy);
g_assert (s_ip4); g_assert (s_ip4);
g_object_set (G_OBJECT (s_ip4), g_object_set (G_OBJECT (s_ip4),
NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
NULL); NULL);
nm_addr = nm_ip4_address_new (); nm_addr = nm_ip_address_new (AF_INET, "2.2.2.4", 24, "2.2.2.254", &error);
inet_pton (AF_INET, "2.2.2.4", &addr); g_assert_no_error (error);
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_setting_ip4_config_add_address (s_ip4, nm_addr); 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); matched = nm_utils_match_connection (connections, orig, TRUE, NULL, NULL);
g_assert (matched != copy); g_assert (matched != copy);

View file

@ -1233,13 +1233,13 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy,
routes = nm_utils_ip4_routes_from_gvalue (val); routes = nm_utils_ip4_routes_from_gvalue (val);
for (iter = routes; iter; iter = iter->next) { for (iter = routes; iter; iter = iter->next) {
NMIP4Route *item = iter->data; NMIPRoute *item = iter->data;
NMPlatformIP4Route route; NMPlatformIP4Route route;
memset (&route, 0, sizeof (route)); memset (&route, 0, sizeof (route));
route.network = nm_ip4_route_get_dest (item); nm_ip_route_get_dest_binary (item, &route.network);
route.plen = nm_ip4_route_get_prefix (item); route.plen = nm_ip_route_get_prefix (item);
route.gateway = nm_ip4_route_get_next_hop (item); nm_ip_route_get_next_hop_binary (item, &route.gateway);
route.source = NM_IP_CONFIG_SOURCE_VPN; route.source = NM_IP_CONFIG_SOURCE_VPN;
route.metric = vpn_routing_metric (connection); 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); 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); 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); routes = nm_utils_ip6_routes_from_gvalue (val);
for (iter = routes; iter; iter = iter->next) { for (iter = routes; iter; iter = iter->next) {
NMIP6Route *item = iter->data; NMIPRoute *item = iter->data;
NMPlatformIP6Route route; NMPlatformIP6Route route;
memset (&route, 0, sizeof (route)); memset (&route, 0, sizeof (route));
route.network = *nm_ip6_route_get_dest (item); nm_ip_route_get_dest_binary (item, &route.network);
route.plen = nm_ip6_route_get_prefix (item); route.plen = nm_ip_route_get_prefix (item);
route.gateway = *nm_ip6_route_get_next_hop (item); nm_ip_route_get_next_hop_binary (item, &route.gateway);
route.source = NM_IP_CONFIG_SOURCE_VPN; route.source = NM_IP_CONFIG_SOURCE_VPN;
route.metric = vpn_routing_metric (connection); 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); 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); val = (GValue *) g_hash_table_lookup (config_hash, NM_VPN_PLUGIN_IP6_CONFIG_NEVER_DEFAULT);