extend vlist code to allow keeping the old data structure instead of the new one...
[project/netifd.git] / interface-ip.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "device.h"
7 #include "interface.h"
8 #include "interface-ip.h"
9 #include "proto.h"
10 #include "ubus.h"
11 #include "system.h"
12
13 static int
14 addr_cmp(const void *k1, const void *k2, void *ptr)
15 {
16 const struct device_addr *a1 = k1, *a2 = k2;
17
18 return memcmp(&a1->mask, &a2->mask,
19 sizeof(*a1) - offsetof(struct device_addr, mask));
20 }
21
22 static int
23 route_cmp(const void *k1, const void *k2, void *ptr)
24 {
25 const struct device_route *r1 = k1, *r2 = k2;
26
27 return memcmp(&r1->mask, &r2->mask,
28 sizeof(*r1) - offsetof(struct device_route, mask));
29 }
30
31 static void
32 interface_update_proto_addr(struct vlist_tree *tree,
33 struct vlist_node *node_new,
34 struct vlist_node *node_old)
35 {
36 struct interface *iface;
37 struct device *dev;
38 struct device_addr *addr;
39
40 iface = container_of(tree, struct interface, proto_addr);
41 dev = iface->l3_dev->dev;
42
43 if (node_old) {
44 addr = container_of(node_old, struct device_addr, node);
45 if (!(addr->flags & DEVADDR_EXTERNAL))
46 system_del_address(dev, addr);
47 free(addr);
48 }
49
50 if (node_new) {
51 addr = container_of(node_new, struct device_addr, node);
52 if (!(addr->flags & DEVADDR_EXTERNAL))
53 system_add_address(dev, addr);
54 }
55 }
56
57 static void
58 interface_update_proto_route(struct vlist_tree *tree,
59 struct vlist_node *node_new,
60 struct vlist_node *node_old)
61 {
62 struct interface *iface;
63 struct device *dev;
64 struct device_route *route;
65
66 iface = container_of(tree, struct interface, proto_route);
67 dev = iface->l3_dev->dev;
68
69 if (node_old) {
70 route = container_of(node_old, struct device_route, node);
71 if (!(route->flags & DEVADDR_EXTERNAL))
72 system_del_route(dev, route);
73 free(route);
74 }
75
76 if (node_new) {
77 route = container_of(node_new, struct device_route, node);
78 if (!(route->flags & DEVADDR_EXTERNAL))
79 system_add_route(dev, route);
80 }
81 }
82
83 void
84 interface_ip_init(struct interface *iface)
85 {
86 vlist_init(&iface->proto_route, route_cmp, interface_update_proto_route,
87 struct device_route, node);
88 vlist_init(&iface->proto_addr, addr_cmp, interface_update_proto_addr,
89 struct device_addr, node);
90 }