add functions for adding dns servers to the proto list, hook them up in proto-static.c
[project/netifd.git] / interface-ip.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include <arpa/inet.h>
7
8 #include "netifd.h"
9 #include "device.h"
10 #include "interface.h"
11 #include "interface-ip.h"
12 #include "proto.h"
13 #include "ubus.h"
14 #include "system.h"
15
16 static int
17 addr_cmp(const void *k1, const void *k2, void *ptr)
18 {
19         return memcmp(k1, k2, sizeof(struct device_addr) -
20                       offsetof(struct device_addr, mask));
21 }
22
23 static int
24 route_cmp(const void *k1, const void *k2, void *ptr)
25 {
26         return memcmp(k1, k2, sizeof(struct device_route) -
27                       offsetof(struct device_route, mask));
28 }
29
30 static void
31 interface_update_proto_addr(struct vlist_tree *tree,
32                             struct vlist_node *node_new,
33                             struct vlist_node *node_old)
34 {
35         struct interface *iface;
36         struct device *dev;
37         struct device_addr *addr;
38
39         iface = container_of(tree, struct interface, proto_addr);
40         dev = iface->l3_dev->dev;
41
42         if (node_old) {
43                 addr = container_of(node_old, struct device_addr, node);
44                 if (!(addr->flags & DEVADDR_EXTERNAL))
45                         system_del_address(dev, addr);
46                 free(addr);
47         }
48
49         if (node_new) {
50                 addr = container_of(node_new, struct device_addr, node);
51                 if (!(addr->flags & DEVADDR_EXTERNAL))
52                         system_add_address(dev, addr);
53         }
54 }
55
56 static void
57 interface_update_proto_route(struct vlist_tree *tree,
58                              struct vlist_node *node_new,
59                              struct vlist_node *node_old)
60 {
61         struct interface *iface;
62         struct device *dev;
63         struct device_route *route;
64
65         iface = container_of(tree, struct interface, proto_route);
66         dev = iface->l3_dev->dev;
67
68         if (node_old) {
69                 route = container_of(node_old, struct device_route, node);
70                 if (!(route->flags & DEVADDR_EXTERNAL))
71                         system_del_route(dev, route);
72                 free(route);
73         }
74
75         if (node_new) {
76                 route = container_of(node_new, struct device_route, node);
77                 if (!(route->flags & DEVADDR_EXTERNAL))
78                         system_add_route(dev, route);
79         }
80 }
81
82 void
83 interface_add_dns_server(struct interface *iface, const char *str)
84 {
85         struct dns_server *s;
86
87         s = calloc(1, sizeof(*s));
88         s->af = AF_INET;
89         if (inet_pton(s->af, str, &s->addr.in))
90                 goto add;
91
92         s->af = AF_INET6;
93         if (inet_pton(s->af, str, &s->addr.in))
94                 goto add;
95
96         free(s);
97         return;
98
99 add:
100         list_add_tail(&s->list, &iface->proto_dns_servers);
101 }
102
103 void
104 interface_add_dns_server_list(struct interface *iface, struct blob_attr *list)
105 {
106         struct blob_attr *cur;
107         int rem;
108
109         blobmsg_for_each_attr(cur, list, rem) {
110                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
111                         continue;
112
113                 if (!blobmsg_check_attr(cur, NULL))
114                         continue;
115
116                 interface_add_dns_server(iface, blobmsg_data(cur));
117         }
118 }
119
120 static void
121 interface_clear_dns_servers(struct interface *iface)
122 {
123         struct dns_server *s, *tmp;
124
125         list_for_each_entry_safe(s, tmp, &iface->proto_dns_servers, list) {
126                 list_del(&s->list);
127                 free(s);
128         }
129 }
130
131 static void
132 interface_clear_dns_search(struct interface *iface)
133 {
134         struct dns_search_domain *s, *tmp;
135
136         list_for_each_entry_safe(s, tmp, &iface->proto_dns_search, list) {
137                 list_del(&s->list);
138                 free(s);
139         }
140 }
141
142 void
143 interface_clear_dns(struct interface *iface)
144 {
145         interface_clear_dns_servers(iface);
146         interface_clear_dns_search(iface);
147 }
148
149 void
150 interface_write_resolv_conf(void)
151 {
152         struct interface *iface;
153         struct dns_server *s;
154         struct dns_search_domain *d;
155         char *path = alloca(strlen(resolv_conf) + 5);
156         const char *str;
157         char buf[32];
158         FILE *f;
159
160         sprintf(path, "%s.tmp", resolv_conf);
161         unlink(path);
162         f = fopen(path, "w");
163         if (!f) {
164                 D(INTERFACE, "Failed to open %s for writing\n", path);
165                 return;
166         }
167
168         vlist_for_each_element(&interfaces, iface, node) {
169                 if (iface->state != IFS_UP)
170                         continue;
171
172                 if (list_empty(&iface->proto_dns_search) &&
173                     list_empty(&iface->proto_dns_servers))
174                         continue;
175
176                 fprintf(f, "# Interface %s\n", iface->name);
177                 list_for_each_entry(s, &iface->proto_dns_servers, list) {
178                         str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
179                         if (!str)
180                                 continue;
181
182                         fprintf(f, "nameserver %s\n", str);
183                 }
184
185                 list_for_each_entry(d, &iface->proto_dns_search, list) {
186                         fprintf(f, "search %s\n", d->name);
187                 }
188         }
189         fclose(f);
190         if (rename(path, resolv_conf) < 0) {
191                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
192                 unlink(path);
193         }
194 }
195
196 void
197 interface_ip_init(struct interface *iface)
198 {
199         vlist_init(&iface->proto_route, route_cmp, interface_update_proto_route,
200                    struct device_route, node, mask);
201         vlist_init(&iface->proto_addr, addr_cmp, interface_update_proto_addr,
202                    struct device_addr, node, mask);
203 }