libnm-core: ensure any next_hop in NMIPRoute is NULL

NMIPRoute has the convention that a next-hop 0.0.0.0/:: is stored
as NULL. However, the binary setters could violate that.
This commit is contained in:
Thomas Haller
2016-01-28 21:09:36 +01:00
parent 91f06323c7
commit 0ba4322eec

View File

@@ -86,6 +86,29 @@ canonicalize_ip (int family, const char *ip, gboolean null_any)
return g_strdup (inet_ntop (family, addr_bytes, addr_str, sizeof (addr_str))); return g_strdup (inet_ntop (family, addr_bytes, addr_str, sizeof (addr_str)));
} }
static char *
canonicalize_ip_binary (int family, gconstpointer ip, gboolean null_any)
{
char string[NM_UTILS_INET_ADDRSTRLEN];
if (!ip) {
if (null_any)
return NULL;
if (family == AF_INET)
return g_strdup ("0.0.0.0");
if (family == AF_INET6)
return g_strdup ("::");
g_return_val_if_reached (NULL);
}
if (null_any) {
int addrlen = (family == AF_INET ? sizeof (struct in_addr) : sizeof (struct in6_addr));
if (!memcmp (ip, &in6addr_any, addrlen))
return NULL;
}
return g_strdup (inet_ntop (family, ip, string, sizeof (string)));
}
static gboolean static gboolean
valid_ip (int family, const char *ip, GError **error) valid_ip (int family, const char *ip, GError **error)
{ {
@@ -629,8 +652,7 @@ nm_ip_route_new_binary (int family,
route->family = family; route->family = family;
route->dest = g_strdup (inet_ntop (family, dest, string, sizeof (string))); route->dest = g_strdup (inet_ntop (family, dest, string, sizeof (string)));
route->prefix = prefix; route->prefix = prefix;
if (next_hop) route->next_hop = canonicalize_ip_binary (family, next_hop, TRUE);
route->next_hop = g_strdup (inet_ntop (family, next_hop, string, sizeof (string)));
route->metric = metric; route->metric = metric;
return route; return route;
@@ -954,15 +976,10 @@ void
nm_ip_route_set_next_hop_binary (NMIPRoute *route, nm_ip_route_set_next_hop_binary (NMIPRoute *route,
gconstpointer next_hop) gconstpointer next_hop)
{ {
char string[NM_UTILS_INET_ADDRSTRLEN];
g_return_if_fail (route != NULL); g_return_if_fail (route != NULL);
g_free (route->next_hop); g_free (route->next_hop);
if (next_hop) route->next_hop = canonicalize_ip_binary (route->family, next_hop, TRUE);
route->next_hop = g_strdup (inet_ntop (route->family, next_hop, string, sizeof (string)));
else
route->next_hop = NULL;
} }
/** /**