keyfile: fix reading/writing route metric zero

Zero is a valid route metric and distinct from -1, which means unspecified.
Fix reader and writer.

Fixes: e374923bbe
This commit is contained in:
Thomas Haller 2017-10-04 11:28:15 +02:00
parent 29e9b567f0
commit 099be8e4db
3 changed files with 19 additions and 14 deletions

View file

@ -185,7 +185,8 @@ build_route (KeyfileReaderInfo *info,
const char *gateway_str, const char *metric_str)
{
NMIPRoute *route;
guint32 metric = 0;
guint32 u32;
gint64 metric = -1;
GError *error = NULL;
g_return_val_if_fail (plen, NULL);
@ -204,9 +205,10 @@ build_route (KeyfileReaderInfo *info,
**/
if ( family == AF_INET6
&& !metric_str
&& get_one_int (NULL, NULL, gateway_str, G_MAXUINT32, &metric))
&& get_one_int (NULL, NULL, gateway_str, G_MAXUINT32, &u32)) {
metric = u32;
gateway_str = NULL;
else {
} else {
if (!info->error) {
handle_warn (info, property_name, NM_KEYFILE_WARN_SEVERITY_WARN,
_("ignoring invalid gateway '%s' for %s route"),
@ -218,14 +220,15 @@ build_route (KeyfileReaderInfo *info,
} else
gateway_str = NULL;
/* parse metric, default to 0 */
/* parse metric, default to -1 */
if (metric_str) {
if (!get_one_int (info, property_name, metric_str, G_MAXUINT32, &metric))
if (!get_one_int (info, property_name, metric_str, G_MAXUINT32, &u32))
return NULL;
metric = u32;
}
route = nm_ip_route_new (family, dest_str, plen, gateway_str,
metric ? (gint64) metric : -1,
metric,
&error);
if (!route) {
handle_warn (info, property_name, NM_KEYFILE_WARN_SEVERITY_WARN,

View file

@ -137,7 +137,7 @@ write_ip_values (GKeyFile *file,
GString *output;
int family, i;
const char *addr, *gw;
guint32 plen, metric;
guint32 plen;
char key_name[64], *key_name_idx;
if (!array->len)
@ -150,25 +150,27 @@ write_ip_values (GKeyFile *file,
output = g_string_sized_new (2*INET_ADDRSTRLEN + 10);
for (i = 0; i < array->len; i++) {
gint64 metric = -1;
if (is_route) {
NMIPRoute *route = array->pdata[i];
addr = nm_ip_route_get_dest (route);
plen = nm_ip_route_get_prefix (route);
gw = nm_ip_route_get_next_hop (route);
metric = MAX (0, nm_ip_route_get_metric (route));
metric = nm_ip_route_get_metric (route);
} else {
NMIPAddress *address = array->pdata[i];
addr = nm_ip_address_get_address (address);
plen = nm_ip_address_get_prefix (address);
gw = i == 0 ? gateway : NULL;
metric = 0;
}
g_string_set_size (output, 0);
g_string_append_printf (output, "%s/%u", addr, plen);
if (metric || gw) {
if ( metric != -1
|| gw) {
/* Older versions of the plugin do not support the form
* "a.b.c.d/plen,,metric", so, we always have to write the
* gateway, even if there isn't one.
@ -182,7 +184,7 @@ write_ip_values (GKeyFile *file,
}
g_string_append_printf (output, ",%s", gw);
if (metric)
if (is_route && metric != -1)
g_string_append_printf (output, ",%lu", (unsigned long) metric);
}

View file

@ -312,11 +312,11 @@ test_read_valid_wired_connection (void)
check_ip_route (s_ip4, 3, "1.1.1.3", 13, NULL, -1);
check_ip_route (s_ip4, 4, "1.1.1.4", 14, "2.2.2.4", -1);
check_ip_route (s_ip4, 5, "1.1.1.5", 15, "2.2.2.5", -1);
check_ip_route (s_ip4, 6, "1.1.1.6", 16, "2.2.2.6", -1);
check_ip_route (s_ip4, 6, "1.1.1.6", 16, "2.2.2.6", 0);
check_ip_route (s_ip4, 7, "1.1.1.7", 17, NULL, -1);
check_ip_route (s_ip4, 8, "1.1.1.8", 18, NULL, -1);
check_ip_route (s_ip4, 9, "1.1.1.9", 19, NULL, -1);
check_ip_route (s_ip4, 10, "1.1.1.10", 20, NULL, -1);
check_ip_route (s_ip4, 9, "1.1.1.9", 19, NULL, 0);
check_ip_route (s_ip4, 10, "1.1.1.10", 20, NULL, 0);
check_ip_route (s_ip4, 11, "1.1.1.11", 21, NULL, 21);
/* Route attributes */