summaryrefslogtreecommitdiff
path: root/clients/tui/nm-editor-bindings.c
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-09-04 14:03:33 +0200
committerThomas Haller <thaller@redhat.com>2017-09-05 18:44:04 +0200
commit506fca65b337aa49a4b246f40f720b840673711e (patch)
treedf38ca4c6c9ab1f18d14125fa81b3ac02b288275 /clients/tui/nm-editor-bindings.c
parentb434d7d43690d91a360403149fe01cb048e6657f (diff)
downloadNetworkManager-506fca65b337aa49a4b246f40f720b840673711e.tar.gz
tui: guess the prefix length (netmask) of private IPv4 addresses and routes based on network class
For RFC1918 private IPv4addresses, guess a better prefix length for addresses and routes. nmtui is an interactive program. It makes sense to be a bit smarter about what the user probably meant. It would be nice if nmtui would update the entry field immediately when the cursor leaves the field, to show the guessed prefix length. However, that is not easily possible, so lets to that another time. For IPv6 addresses, default to /64 instead of /128. https://bugzilla.redhat.com/show_bug.cgi?id=1474295
Diffstat (limited to 'clients/tui/nm-editor-bindings.c')
-rw-r--r--clients/tui/nm-editor-bindings.c75
1 files changed, 32 insertions, 43 deletions
diff --git a/clients/tui/nm-editor-bindings.c b/clients/tui/nm-editor-bindings.c
index 3753aaa9ac..7ac7f0c7e2 100644
--- a/clients/tui/nm-editor-bindings.c
+++ b/clients/tui/nm-editor-bindings.c
@@ -71,45 +71,6 @@ nm_editor_bindings_init (void)
}
static gboolean
-parse_addr_prefix (const char *text,
- int family,
- char **addr,
- guint32 *prefix)
-{
- const char *slash;
- char *addrstr, *end;
- gboolean valid;
-
- slash = strchr (text, '/');
-
- if (slash)
- addrstr = g_strndup (text, slash - text);
- else
- addrstr = g_strdup (text);
- valid = nm_utils_ipaddr_valid (family, addrstr);
-
- if (slash) {
- *prefix = strtoul (slash + 1, &end, 10);
- if ( *end
- || *prefix == 0
- || (family == AF_INET && *prefix > 32)
- || (family == AF_INET6 && *prefix > 128))
- valid = FALSE;
- } else if (prefix) {
- if (family == AF_INET)
- *prefix = 32;
- else
- *prefix = 128;
- }
-
- if (addr && valid)
- *addr = addrstr;
- else
- g_free (addrstr);
- return valid;
-}
-
-static gboolean
ip_addresses_with_prefix_to_strv (GBinding *binding,
const GValue *source_value,
GValue *target_value,
@@ -151,7 +112,7 @@ ip_addresses_with_prefix_from_strv (GBinding *binding,
GPtrArray *addrs;
NMIPAddress *addr;
char *addrstr;
- guint32 prefix;
+ int prefix;
int i;
strings = g_value_get_boxed (source_value);
@@ -170,11 +131,24 @@ ip_addresses_with_prefix_from_strv (GBinding *binding,
} else
addr = addrs->pdata[i];
- if (!parse_addr_prefix (strings[i], family, &addrstr, &prefix)) {
+ if (!nm_utils_parse_inaddr_prefix (strings[i], family, &addrstr, &prefix)) {
g_ptr_array_unref (addrs);
return FALSE;
}
+ if (prefix == -1) {
+ if (family == AF_INET) {
+ in_addr_t v4;
+
+ inet_pton (family, addrstr, &v4);
+ if (nm_utils_ip_is_site_local (AF_INET, &v4))
+ prefix = nm_utils_ip4_get_default_prefix (v4);
+ else
+ prefix = 32;
+ } else
+ prefix = 64;
+ }
+
nm_ip_address_set_address (addr, addrstr);
nm_ip_address_set_prefix (addr, prefix);
g_free (addrstr);
@@ -451,10 +425,10 @@ ip_route_transform_from_dest_string (GBinding *binding,
NMIPRoute *route;
const char *text;
char *addrstr;
- guint32 prefix;
+ int prefix;
text = g_value_get_string (source_value);
- if (!parse_addr_prefix (text, family, &addrstr, &prefix))
+ if (!nm_utils_parse_inaddr_prefix (text, family, &addrstr, &prefix))
return FALSE;
/* Fetch the original property value */
@@ -462,6 +436,21 @@ ip_route_transform_from_dest_string (GBinding *binding,
g_binding_get_source_property (binding), &route,
NULL);
+ if (prefix == -1) {
+ if (family == AF_INET) {
+ in_addr_t v4;
+
+ inet_pton (family, addrstr, &v4);
+ if (nm_utils_ip_is_site_local (AF_INET, &v4)) {
+ prefix = nm_utils_ip4_get_default_prefix (v4);
+ if (v4 & (~nm_utils_ip4_prefix_to_netmask (prefix)))
+ prefix = 32;
+ } else
+ prefix = 32;
+ } else
+ prefix = 64;
+ }
+
nm_ip_route_set_dest (route, addrstr);
nm_ip_route_set_prefix (route, prefix);
g_free (addrstr);