add config state tracking
[project/netifd.git] / config.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "interface.h"
7 #include "proto.h"
8
9 struct uci_context *uci_ctx;
10 static struct uci_package *uci_network;
11 bool config_init = false;
12 static struct blob_buf b;
13 static unsigned int config_version = 1;
14
15
16 static void uci_attr_to_blob(struct blob_buf *b, const char *str,
17 const char *name, enum blobmsg_type type)
18 {
19 char *err;
20 int intval;
21
22 switch (type) {
23 case BLOBMSG_TYPE_STRING:
24 blobmsg_add_string(b, name, str);
25 break;
26 case BLOBMSG_TYPE_BOOL:
27 if (!strcmp(str, "true") || !strcmp(str, "1"))
28 intval = 1;
29 else if (!strcmp(str, "false") || !strcmp(str, "0"))
30 intval = 0;
31 else
32 return;
33
34 blobmsg_add_u8(b, name, intval);
35 break;
36 case BLOBMSG_TYPE_INT32:
37 intval = strtol(str, &err, 0);
38 if (*err)
39 return;
40
41 blobmsg_add_u32(b, name, intval);
42 break;
43 default:
44 break;
45 }
46 }
47
48 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
49 enum blobmsg_type type)
50 {
51 struct uci_element *e;
52 char *str, *next, *word;
53
54 if (o->type == UCI_TYPE_LIST) {
55 uci_foreach_element(&o->v.list, e) {
56 uci_attr_to_blob(b, e->name, NULL, type);
57 }
58 return;
59 }
60
61 str = strdup(o->v.string);
62 next = str;
63
64 while ((word = strsep(&next, " \t")) != NULL) {
65 if (!*word)
66 continue;
67
68 uci_attr_to_blob(b, word, NULL, type);
69 }
70
71 free(str);
72 }
73
74 static void __uci_to_blob(struct blob_buf *b, struct uci_section *s,
75 const struct config_param_list *p)
76 {
77 const struct blobmsg_policy *attr = NULL;
78 struct uci_element *e;
79 struct uci_option *o;
80 void *array;
81 int i;
82
83 uci_foreach_element(&s->options, e) {
84 for (i = 0; i < p->n_params; i++) {
85 attr = &p->params[i];
86 if (!strcmp(attr->name, e->name))
87 break;
88 }
89
90 if (i == p->n_params)
91 continue;
92
93 o = uci_to_option(e);
94
95 if (attr->type == BLOBMSG_TYPE_ARRAY) {
96 if (!p->info)
97 continue;
98
99 array = blobmsg_open_array(b, attr->name);
100 uci_array_to_blob(b, o, p->info[i].type);
101 blobmsg_close_array(b, array);
102 continue;
103 }
104
105 if (o->type == UCI_TYPE_LIST)
106 continue;
107
108 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
109 }
110 }
111
112 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
113 const struct config_param_list *p)
114 {
115 int i;
116
117 __uci_to_blob(b, s, p);
118 for (i = 0; i < p->n_next; i++)
119 uci_to_blob(b, s, p->next[i]);
120 }
121
122 static int
123 config_parse_bridge_interface(struct uci_section *s)
124 {
125 char *name;
126
127 name = alloca(strlen(s->e.name) + 4);
128 sprintf(name, "br-%s", s->e.name);
129 blobmsg_add_string(&b, "name", name);
130
131 uci_to_blob(&b, s, bridge_device_type.config_params);
132 if (!bridge_device_type.create(b.head)) {
133 DPRINTF("Failed to create bridge for interface '%s'\n", s->e.name);
134 return -EINVAL;
135 }
136
137 blob_buf_init(&b, 0);
138 blobmsg_add_string(&b, "ifname", name);
139 return 0;
140 }
141
142 void
143 config_set_state(struct config_state *state, struct blob_attr *attr)
144 {
145 state->data = malloc(blob_pad_len(attr));
146 if (!state->data)
147 return;
148
149 memcpy(state->data, attr, blob_pad_len(attr));
150 }
151
152 static void
153 config_parse_interface(struct uci_section *s)
154 {
155 struct interface *iface;
156 const char *type;
157
158 DPRINTF("Create interface '%s'\n", s->e.name);
159
160 blob_buf_init(&b, 0);
161
162 type = uci_lookup_option_string(uci_ctx, s, "type");
163 if (type && !strcmp(type, "bridge"))
164 if (config_parse_bridge_interface(s))
165 return;
166
167 uci_to_blob(&b, s, &interface_attr_list);
168 iface = interface_alloc(s->e.name, b.head);
169 if (!iface)
170 return;
171
172 blob_buf_init(&b, 0);
173 if (iface->proto_handler && iface->proto_handler->config_params)
174 uci_to_blob(&b, s, iface->proto_handler->config_params);
175
176 proto_init_interface(iface, b.head);
177 iface->config.version = config_version;
178 }
179
180 void
181 config_init_devices(void)
182 {
183 struct uci_element *e;
184
185 uci_foreach_element(&uci_network->sections, e) {
186 struct uci_section *s = uci_to_section(e);
187 const struct device_type *devtype;
188 const char *type;
189
190 if (strcmp(s->type, "device") != 0)
191 continue;
192
193 blob_buf_init(&b, 0);
194 type = uci_lookup_option_string(uci_ctx, s, "type");
195 if (type && !strcmp(type, "bridge"))
196 devtype = &bridge_device_type;
197 else
198 devtype = &simple_device_type;
199
200 uci_to_blob(&b, s, devtype->config_params);
201 devtype->create(b.head);
202 }
203 }
204
205 void
206 config_init_interfaces(const char *name)
207 {
208 struct uci_context *ctx;
209 struct uci_package *p = NULL;
210 struct uci_element *e;
211
212 ctx = uci_alloc_context();
213 uci_ctx = ctx;
214
215 uci_set_confdir(ctx, "./config");
216
217 if (uci_load(ctx, "network", &p)) {
218 fprintf(stderr, "Failed to load network config\n");
219 return;
220 }
221
222 uci_network = p;
223 config_init = true;
224
225 config_init_devices();
226
227 uci_foreach_element(&p->sections, e) {
228 struct uci_section *s = uci_to_section(e);
229
230 if (name && strcmp(s->e.name, name) != 0)
231 continue;
232
233 if (!strcmp(s->type, "interface"))
234 config_parse_interface(s);
235 }
236 device_free_unused(NULL);
237 config_init = false;
238
239 interface_start_pending();
240 }