X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;ds=sidebyside;f=proto.c;h=c51637f32dac82697eed3bb00ab6637c0cdf82f5;hb=91fa29a0d17b40963f67f08e740ca0a07365db90;hp=99f083c60025c045897387c9febd29c8249667f0;hpb=3bd56db589071948eebd30b00cda7ef1fb38a61b;p=project%2Fnetifd.git diff --git a/proto.c b/proto.c index 99f083c..c51637f 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) { @@ -45,7 +69,7 @@ parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask) return 0; } - return inet_pton(af, str, addr); + return inet_pton(af, astr, addr); } struct device_addr * @@ -136,9 +160,10 @@ proto_init_interface(struct interface *iface, struct blob_attr *attr) const struct proto_handler *proto = iface->proto_handler; struct interface_proto_state *state = NULL; - if (proto) - state = proto->attach(proto, iface, attr); + if (!proto) + proto = &no_proto; + state = proto->attach(proto, iface, attr); if (!state) { state = no_proto.attach(&no_proto, iface, attr); state->cb = invalid_proto_handler; @@ -159,8 +184,10 @@ proto_attach_interface(struct interface *iface, const char *proto_name) } proto = get_proto_handler(proto_name); - if (!proto) + if (!proto) { interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0); + proto = &no_proto; + } iface->proto_handler = proto; } @@ -169,7 +196,7 @@ int interface_proto_event(struct interface_proto_state *proto, enum interface_proto_cmd cmd, bool force) { - enum interface_event ev; + enum interface_proto_event ev; int ret; ret = proto->cb(proto, cmd, force); @@ -178,10 +205,10 @@ interface_proto_event(struct interface_proto_state *proto, switch(cmd) { case PROTO_CMD_SETUP: - ev = IFEV_UP; + ev = IFPEV_UP; break; case PROTO_CMD_TEARDOWN: - ev = IFEV_DOWN; + ev = IFPEV_DOWN; break; default: return -EINVAL;