9 static struct avl_tree handlers;
11 void add_proto_handler(struct proto_handler *p)
14 avl_init(&handlers, avl_strcmp, false, NULL);
20 avl_insert(&handlers, &p->avl);
24 default_proto_free(struct interface_proto_state *proto)
30 invalid_proto_handler(struct interface_proto_state *proto,
31 enum interface_proto_cmd cmd, bool force)
37 no_proto_handler(struct interface_proto_state *proto,
38 enum interface_proto_cmd cmd, bool force)
43 static struct interface_proto_state *
44 default_proto_attach(const struct proto_handler *h,
45 struct interface *iface,
46 struct uci_section *s)
48 struct interface_proto_state *proto;
50 proto = calloc(1, sizeof(*proto));
51 proto->free = default_proto_free;
52 proto->flags = PROTO_FLAG_IMMEDIATE;
53 proto->handler = no_proto_handler;
58 static const struct proto_handler no_proto = {
60 .attach = default_proto_attach,
63 static const struct proto_handler *
64 get_proto_handler(const char *name)
66 struct proto_handler *proto;
68 if (!strcmp(name, "none"))
74 return avl_find_element(&handlers, name, proto, avl);
78 proto_init_interface(struct interface *iface, struct uci_section *s)
80 const struct proto_handler *proto = iface->proto_handler;
81 struct interface_proto_state *state = NULL;
84 state = proto->attach(proto, iface, s);
87 state = no_proto.attach(&no_proto, iface, s);
88 state->handler = invalid_proto_handler;
91 interface_set_proto_state(iface, state);
95 proto_attach_interface(struct interface *iface, const char *proto_name)
97 const struct proto_handler *proto = NULL;
100 interface_add_error(iface, "proto", "NO_PROTO", NULL, 0);
104 proto = get_proto_handler(proto_name);
106 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
108 iface->proto_handler = proto;
112 interface_proto_event(struct interface_proto_state *proto,
113 enum interface_proto_cmd cmd, bool force)
115 enum interface_event ev;
118 ret = proto->handler(proto, cmd, force);
119 if (ret || !(proto->flags & PROTO_FLAG_IMMEDIATE))
123 case PROTO_CMD_SETUP:
126 case PROTO_CMD_TEARDOWN:
132 proto->proto_event(proto, ev);