summaryrefslogtreecommitdiffhomepage
path: root/proto.c
diff options
context:
space:
mode:
authorFelix Fietkau <nbd@openwrt.org>2011-10-14 00:30:54 +0200
committerFelix Fietkau <nbd@openwrt.org>2011-10-14 00:30:54 +0200
commitc000b546d97aa97144ee2cdf396d46a039bb7f98 (patch)
tree90668d89cd3d96c041f9a117fc4c10d492125338 /proto.c
parentb021a49efe0641f7c5ca763bc43e5ba73b7a1b5f (diff)
proto-shell: allow passing netmask in ip address format
Diffstat (limited to 'proto.c')
-rw-r--r--proto.c38
1 files changed, 31 insertions, 7 deletions
diff --git a/proto.c b/proto.c
index 786d600..bd3b09a 100644
--- a/proto.c
+++ b/proto.c
@@ -12,18 +12,42 @@
static struct avl_tree handlers;
+unsigned int
+parse_netmask_string(const char *str, bool v6)
+{
+ struct in_addr addr;
+ unsigned int ret;
+ char *err = NULL;
+
+ if (!strchr(str, '.')) {
+ ret = strtoul(str, &err, 0);
+ if (err && *err)
+ goto error;
+
+ return ret;
+ }
+
+ if (v6)
+ goto error;
+
+ if (inet_aton(str, &addr) != 1)
+ goto error;
+
+ return 32 - fls(~(ntohl(addr.s_addr)));
+
+error:
+ return ~0;
+}
+
static bool
-split_netmask(char *str, unsigned int *netmask)
+split_netmask(char *str, unsigned int *netmask, bool v6)
{
- char *delim, *err = NULL;
+ char *delim = strchr(str, '/');
- delim = strchr(str, '/');
if (delim) {
*(delim++) = 0;
- *netmask = strtoul(delim, &err, 10);
- if (err && *err)
- return false;
+ *netmask = parse_netmask_string(delim, v6);
}
return true;
}
@@ -34,7 +58,7 @@ parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
char *astr = alloca(strlen(str) + 1);
strcpy(astr, str);
- if (!split_netmask(astr, netmask))
+ if (!split_netmask(astr, netmask, af == AF_INET6))
return 0;
if (af == AF_INET6) {