X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=proto.c;h=c51637f32dac82697eed3bb00ab6637c0cdf82f5;hp=c54206fd70c2a96519d04040cade53641da66a54;hb=6803aef644a5112f331e6de4d37d78718b76a9bf;hpb=be8cbaecc2490b561bb7d7df81da1bb6258e92b5 diff --git a/proto.c b/proto.c index c54206f..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) { @@ -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;