0f7a6527bd94d02192a8928fcde68dcf255b387e
[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
8 struct uci_context *uci_ctx;
9 static struct uci_package *uci_network;
10 bool config_init = false;
11 static struct blob_buf b;
12
13
14 static void uci_attr_to_blob(struct blob_buf *b, const char *str,
15                              const char *name, enum blobmsg_type type)
16 {
17         char *err;
18         int intval;
19
20         switch (type) {
21         case BLOBMSG_TYPE_STRING:
22                 blobmsg_add_string(b, name, str);
23                 break;
24         case BLOBMSG_TYPE_BOOL:
25                 if (!strcmp(str, "true") || !strcmp(str, "1"))
26                         intval = 1;
27                 else if (!strcmp(str, "false") || !strcmp(str, "0"))
28                         intval = 0;
29                 else
30                         return;
31
32                 blobmsg_add_u8(b, name, intval);
33                 break;
34         case BLOBMSG_TYPE_INT32:
35                 intval = strtol(str, &err, 0);
36                 if (*err)
37                         return;
38
39                 blobmsg_add_u32(b, name, intval);
40                 break;
41         default:
42                 break;
43         }
44 }
45
46 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
47                               enum blobmsg_type type)
48 {
49         struct uci_element *e;
50         char *str, *next, *word;
51
52         if (o->type == UCI_TYPE_LIST) {
53                 uci_foreach_element(&o->v.list, e) {
54                         uci_attr_to_blob(b, e->name, NULL, type);
55                 }
56                 return;
57         }
58
59         str = strdup(o->v.string);
60         next = str;
61
62         while ((word = strsep(&next, " \t")) != NULL) {
63                 if (!*word)
64                         continue;
65
66                 uci_attr_to_blob(b, word, NULL, type);
67         }
68
69         free(str);
70 }
71
72 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
73                         const struct config_param_list *p)
74 {
75         const struct blobmsg_policy *attr;
76         struct uci_element *e;
77         struct uci_option *o;
78         void *array;
79         int i;
80
81         uci_foreach_element(&s->options, e) {
82                 for (i = 0; i < p->n_params; i++) {
83                         attr = &p->params[i];
84                         if (!strcmp(attr->name, e->name))
85                                 break;
86                 }
87
88                 if (i == p->n_params)
89                         continue;
90
91                 o = uci_to_option(e);
92
93                 if (attr->type == BLOBMSG_TYPE_ARRAY) {
94                         if (!p->info)
95                                 continue;
96
97                         array = blobmsg_open_array(b, attr->name);
98                         uci_array_to_blob(b, o, p->info[i].type);
99                         blobmsg_close_array(b, array);
100                         continue;
101                 }
102
103                 if (o->type == UCI_TYPE_LIST)
104                         continue;
105
106                 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
107         }
108 }
109
110 static void
111 config_parse_interface(struct uci_section *s)
112 {
113         DPRINTF("Create interface '%s'\n", s->e.name);
114
115         blob_buf_init(&b, 0);
116         uci_to_blob(&b, s, &interface_attr_list);
117         alloc_interface(s->e.name, s, b.head);
118 }
119
120 void
121 config_init_devices(void)
122 {
123         struct uci_element *e;
124
125         uci_foreach_element(&uci_network->sections, e) {
126                 struct uci_section *s = uci_to_section(e);
127
128                 if (strcmp(s->type, "device") != 0)
129                         continue;
130
131                 blob_buf_init(&b, 0);
132                 uci_to_blob(&b, s, &device_attr_list);
133                 device_create(b.head, s);
134         }
135 }
136
137 void
138 config_init_interfaces(const char *name)
139 {
140         struct uci_context *ctx;
141         struct uci_package *p = NULL;
142         struct uci_element *e;
143
144         ctx = uci_alloc_context();
145         uci_ctx = ctx;
146
147         uci_set_confdir(ctx, "./config");
148
149         if (uci_load(ctx, "network", &p)) {
150                 fprintf(stderr, "Failed to load network config\n");
151                 return;
152         }
153
154         uci_network = p;
155         config_init = true;
156
157         config_init_devices();
158
159         uci_foreach_element(&p->sections, e) {
160                 struct uci_section *s = uci_to_section(e);
161
162                 if (name && strcmp(s->e.name, name) != 0)
163                         continue;
164
165                 if (!strcmp(s->type, "interface"))
166                         config_parse_interface(s);
167         }
168         device_free_all();
169         config_init = false;
170
171         start_pending_interfaces();
172 }