add mac address to device info
[project/netifd.git] / proto.c
diff --git a/proto.c b/proto.c
index c54206f..c51637f 100644 (file)
--- a/proto.c
+++ b/proto.c
 
 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;