From: Felix Fietkau Date: Thu, 13 Oct 2011 22:30:54 +0000 (+0200) Subject: proto-shell: allow passing netmask in ip address format X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=c000b546d97aa97144ee2cdf396d46a039bb7f98;p=oweals%2Fnetifd.git proto-shell: allow passing netmask in ip address format --- diff --git a/proto-shell.c b/proto-shell.c index f5a5fb1..c90f65c 100644 --- a/proto-shell.c +++ b/proto-shell.c @@ -236,7 +236,7 @@ enum { static const struct blobmsg_policy route_attr[__ROUTE_LAST] = { [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING }, - [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 }, + [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING }, [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING }, [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING }, }; @@ -352,7 +352,7 @@ proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb) } if (!tb[NOTIFY_IFNAME]) { - if (!state->iface->main_dev.dev) + if (!state->proto.iface->main_dev.dev) return UBUS_STATUS_INVALID_ARGUMENT; } else if (!state->l3_dev.dev) { device_add_user(&state->l3_dev, diff --git a/proto-static.c b/proto-static.c index f5d676a..b8f3412 100644 --- a/proto-static.c +++ b/proto-static.c @@ -103,20 +103,18 @@ static int proto_apply_static_settings(struct interface *iface, struct blob_attr *attr) { struct blob_attr *tb[__OPT_MAX]; - struct in_addr ina; const char *error; - int netmask = 32; + unsigned int netmask = 32; int n_v4 = 0, n_v6 = 0; blobmsg_parse(static_attrs, __OPT_MAX, tb, blob_data(attr), blob_len(attr)); if (tb[OPT_NETMASK]) { - if (!inet_aton(blobmsg_data(tb[OPT_NETMASK]), &ina)) { + netmask = parse_netmask_string(blobmsg_data(tb[OPT_NETMASK]), false); + if (netmask > 32) { error = "INVALID_NETMASK"; goto error; } - - netmask = 32 - fls(~(ntohl(ina.s_addr))); } if (tb[OPT_IPADDR]) 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) { diff --git a/proto.h b/proto.h index 32f4ffc..57426bd 100644 --- a/proto.h +++ b/proto.h @@ -53,5 +53,6 @@ void proto_attach_interface(struct interface *iface, const char *proto_name); int interface_proto_event(struct interface_proto_state *proto, enum interface_proto_cmd cmd, bool force); struct device_addr *proto_parse_ip_addr_string(const char *str, bool v6, int mask); +unsigned int parse_netmask_string(const char *str, bool v6); #endif