summaryrefslogtreecommitdiff
path: root/libnm-core
diff options
context:
space:
mode:
authorDan Winship <danw@gnome.org>2014-11-04 15:48:48 -0500
committerDan Winship <danw@gnome.org>2014-11-07 07:49:41 -0500
commite374923bbe4a9f608644756f749b9bae9aa5f349 (patch)
treed7e0fffead6c3e4d1e3433d2b76dec3431b0816f /libnm-core
parentff608c24cd1ac409092f1a883452225a8be6513a (diff)
downloadNetworkManager-e374923bbe4a9f608644756f749b9bae9aa5f349.tar.gz
all: allow route metrics to be "0"
Change NMIPRoute to use "-1" for "default", so that "0" is a valid metric. Update everything for that.
Diffstat (limited to 'libnm-core')
-rw-r--r--libnm-core/nm-setting-ip-config.c41
-rw-r--r--libnm-core/nm-setting-ip-config.h8
-rw-r--r--libnm-core/nm-utils.c30
3 files changed, 56 insertions, 23 deletions
diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c
index 30e7b267c8..8d0504e2b1 100644
--- a/libnm-core/nm-setting-ip-config.c
+++ b/libnm-core/nm-setting-ip-config.c
@@ -96,6 +96,24 @@ valid_prefix (int family, guint prefix, GError **error)
return TRUE;
}
+static gboolean
+valid_metric (gint64 metric, GError **error)
+{
+ if (metric < -1 || metric > G_MAXUINT32) {
+ if (error) {
+ char buf[64];
+
+ /* We can't concatenate G_GINT64_FORMAT into a translatable string */
+ g_snprintf (buf, sizeof (buf), "%" G_GINT64_FORMAT, metric);
+ g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_FAILED,
+ _("Invalid routing metric '%s'"), buf);
+ }
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
G_DEFINE_BOXED_TYPE (NMIPAddress, nm_ip_address, nm_ip_address_dup, nm_ip_address_unref)
@@ -495,7 +513,7 @@ struct NMIPRoute {
char *dest;
guint prefix;
char *next_hop;
- guint32 metric;
+ gint64 metric;
GHashTable *attributes;
};
@@ -506,7 +524,7 @@ struct NMIPRoute {
* @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")
+ * @metric: the route metric (or -1 for "default")
* @error: location to store error, or %NULL
*
* Creates a new #NMIPRoute object.
@@ -518,7 +536,7 @@ nm_ip_route_new (int family,
const char *dest,
guint prefix,
const char *next_hop,
- guint metric,
+ gint64 metric,
GError **error)
{
NMIPRoute *route;
@@ -531,6 +549,8 @@ nm_ip_route_new (int family,
return NULL;
if (next_hop && !valid_ip (family, next_hop, error))
return NULL;
+ if (!valid_metric (metric, error))
+ return NULL;
route = g_slice_new0 (NMIPRoute);
route->refcount = 1;
@@ -550,7 +570,7 @@ nm_ip_route_new (int family,
* @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")
+ * @metric: the route metric (or -1 for "default")
* @error: location to store error, or %NULL
*
* Creates a new #NMIPRoute object. @dest and @next_hop (if non-%NULL) must
@@ -563,7 +583,7 @@ nm_ip_route_new_binary (int family,
gconstpointer dest,
guint prefix,
gconstpointer next_hop,
- guint metric,
+ gint64 metric,
GError **error)
{
NMIPRoute *route;
@@ -573,6 +593,8 @@ nm_ip_route_new_binary (int family,
if (!valid_prefix (family, prefix, error))
return NULL;
+ if (!valid_metric (metric, error))
+ return NULL;
route = g_slice_new0 (NMIPRoute);
route->refcount = 1;
@@ -916,12 +938,12 @@ nm_ip_route_set_next_hop_binary (NMIPRoute *route,
* @route: the #NMIPRoute
*
* Gets the route metric property of this route object; lower values
- * indicate "better" or more preferred routes; 0 indicates "default"
+ * indicate "better" or more preferred routes; -1 indicates "default"
* (meaning NetworkManager will set it appropriately).
*
* Returns: the route metric
**/
-guint32
+gint64
nm_ip_route_get_metric (NMIPRoute *route)
{
g_return_val_if_fail (route != NULL, 0);
@@ -933,15 +955,16 @@ nm_ip_route_get_metric (NMIPRoute *route)
/**
* nm_ip_route_set_metric:
* @route: the #NMIPRoute
- * @metric: the route metric
+ * @metric: the route metric (or -1 for "default")
*
* Sets the metric property of this route object.
**/
void
nm_ip_route_set_metric (NMIPRoute *route,
- guint32 metric)
+ gint64 metric)
{
g_return_if_fail (route != NULL);
+ g_return_if_fail (valid_metric (metric, NULL));
route->metric = metric;
}
diff --git a/libnm-core/nm-setting-ip-config.h b/libnm-core/nm-setting-ip-config.h
index 37437761c4..fba226c331 100644
--- a/libnm-core/nm-setting-ip-config.h
+++ b/libnm-core/nm-setting-ip-config.h
@@ -78,13 +78,13 @@ NMIPRoute *nm_ip_route_new (int family,
const char *dest,
guint prefix,
const char *next_hop,
- guint metric,
+ gint64 metric,
GError **error);
NMIPRoute *nm_ip_route_new_binary (int family,
gconstpointer dest,
guint prefix,
gconstpointer next_hop,
- guint metric,
+ gint64 metric,
GError **error);
void nm_ip_route_ref (NMIPRoute *route);
@@ -111,9 +111,9 @@ 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);
+gint64 nm_ip_route_get_metric (NMIPRoute *route);
void nm_ip_route_set_metric (NMIPRoute *route,
- guint32 metric);
+ gint64 metric);
char **nm_ip_route_get_attribute_names (NMIPRoute *route);
GVariant *nm_ip_route_get_attribute (NMIPRoute *route,
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index f8105da810..5356399baa 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -1269,7 +1269,8 @@ nm_utils_ip4_routes_to_variant (GPtrArray *routes)
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);
+ /* The old routes format uses "0" for default, not "-1" */
+ array[3] = MAX (0, nm_ip_route_get_metric (route));
g_variant_builder_add (&builder, "@au",
g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32,
@@ -1317,8 +1318,11 @@ nm_utils_ip4_routes_from_variant (GVariant *value)
}
route = nm_ip_route_new_binary (AF_INET,
- &route_array[0], route_array[1],
- &route_array[2], route_array[3],
+ &route_array[0],
+ route_array[1],
+ &route_array[2],
+ /* The old routes format uses "0" for default, not "-1" */
+ route_array[3] ? (gint64) route_array[3] : -1,
&error);
if (route)
g_ptr_array_add (routes, route);
@@ -1636,7 +1640,8 @@ nm_utils_ip6_routes_to_variant (GPtrArray *routes)
prefix = nm_ip_route_get_prefix (route);
nm_ip_route_get_next_hop_binary (route, &next_hop_bytes);
next_hop = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &next_hop_bytes, 16, 1);
- metric = nm_ip_route_get_metric (route);
+ /* The old routes format uses "0" for default, not "-1" */
+ metric = MAX (0, nm_ip_route_get_metric (route));
g_variant_builder_add (&builder, "(@ayu@ayu)", dest, prefix, next_hop, metric);
}
@@ -1695,7 +1700,9 @@ nm_utils_ip6_routes_from_variant (GVariant *value)
goto next;
}
- route = nm_ip_route_new_binary (AF_INET6, dest, prefix, next_hop, metric, &error);
+ route = nm_ip_route_new_binary (AF_INET6, dest, prefix, next_hop,
+ metric ? (gint64) metric : -1,
+ &error);
if (route)
g_ptr_array_add (routes, route);
else {
@@ -1861,10 +1868,10 @@ nm_utils_ip_routes_to_variant (GPtrArray *routes)
"next-hop",
g_variant_new_string (nm_ip_route_get_next_hop (route)));
}
- if (nm_ip_route_get_metric (route)) {
+ if (nm_ip_route_get_metric (route) != -1) {
g_variant_builder_add (&route_builder, "{sv}",
"metric",
- g_variant_new_uint32 (nm_ip_route_get_metric (route)));
+ g_variant_new_uint32 ((guint32) nm_ip_route_get_metric (route)));
}
names = nm_ip_route_get_attribute_names (route);
@@ -1903,7 +1910,8 @@ nm_utils_ip_routes_from_variant (GVariant *value,
GVariantIter iter, attrs_iter;
GVariant *route_var;
const char *dest, *next_hop;
- guint32 prefix, metric;
+ guint32 prefix, metric32;
+ gint64 metric;
const char *attr_name;
GVariant *attr_val;
NMIPRoute *route;
@@ -1923,8 +1931,10 @@ nm_utils_ip_routes_from_variant (GVariant *value,
}
if (!g_variant_lookup (route_var, "next-hop", "&s", &next_hop))
next_hop = NULL;
- if (!g_variant_lookup (route_var, "metric", "u", &metric))
- metric = 0;
+ if (g_variant_lookup (route_var, "metric", "u", &metric32))
+ metric = metric32;
+ else
+ metric = -1;
route = nm_ip_route_new (family, dest, prefix, next_hop, metric, &error);
if (!route) {