12 bool config_init
= false;
14 static struct uci_context
*uci_ctx
;
15 static struct uci_package
*uci_network
;
16 static struct blob_buf b
;
18 static void uci_attr_to_blob(struct blob_buf
*b
, const char *str
,
19 const char *name
, enum blobmsg_type type
)
25 case BLOBMSG_TYPE_STRING
:
26 blobmsg_add_string(b
, name
, str
);
28 case BLOBMSG_TYPE_BOOL
:
29 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
31 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
36 blobmsg_add_u8(b
, name
, intval
);
38 case BLOBMSG_TYPE_INT32
:
39 intval
= strtol(str
, &err
, 0);
43 blobmsg_add_u32(b
, name
, intval
);
50 static void uci_array_to_blob(struct blob_buf
*b
, struct uci_option
*o
,
51 enum blobmsg_type type
)
53 struct uci_element
*e
;
54 char *str
, *next
, *word
;
56 if (o
->type
== UCI_TYPE_LIST
) {
57 uci_foreach_element(&o
->v
.list
, e
) {
58 uci_attr_to_blob(b
, e
->name
, NULL
, type
);
63 str
= strdup(o
->v
.string
);
66 while ((word
= strsep(&next
, " \t")) != NULL
) {
70 uci_attr_to_blob(b
, word
, NULL
, type
);
76 static void __uci_to_blob(struct blob_buf
*b
, struct uci_section
*s
,
77 const struct config_param_list
*p
)
79 const struct blobmsg_policy
*attr
= NULL
;
80 struct uci_element
*e
;
85 uci_foreach_element(&s
->options
, e
) {
86 for (i
= 0; i
< p
->n_params
; i
++) {
88 if (!strcmp(attr
->name
, e
->name
))
97 if (attr
->type
== BLOBMSG_TYPE_ARRAY
) {
101 array
= blobmsg_open_array(b
, attr
->name
);
102 uci_array_to_blob(b
, o
, p
->info
[i
].type
);
103 blobmsg_close_array(b
, array
);
107 if (o
->type
== UCI_TYPE_LIST
)
110 uci_attr_to_blob(b
, o
->v
.string
, attr
->name
, attr
->type
);
114 static void uci_to_blob(struct blob_buf
*b
, struct uci_section
*s
,
115 const struct config_param_list
*p
)
119 __uci_to_blob(b
, s
, p
);
120 for (i
= 0; i
< p
->n_next
; i
++)
121 uci_to_blob(b
, s
, p
->next
[i
]);
125 config_parse_bridge_interface(struct uci_section
*s
)
129 name
= alloca(strlen(s
->e
.name
) + 4);
130 sprintf(name
, "br-%s", s
->e
.name
);
131 blobmsg_add_string(&b
, "name", name
);
133 uci_to_blob(&b
, s
, bridge_device_type
.config_params
);
134 if (!device_create(name
, &bridge_device_type
, b
.head
)) {
135 D(INTERFACE
, "Failed to create bridge for interface '%s'\n", s
->e
.name
);
139 blob_buf_init(&b
, 0);
140 blobmsg_add_string(&b
, "ifname", name
);
145 config_parse_interface(struct uci_section
*s
)
147 struct interface
*iface
;
149 struct blob_attr
*config
;
152 blob_buf_init(&b
, 0);
154 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
155 if (type
&& !strcmp(type
, "bridge"))
156 if (config_parse_bridge_interface(s
))
159 uci_to_blob(&b
, s
, &interface_attr_list
);
160 iface
= calloc(1, sizeof(*iface
));
164 interface_init(iface
, s
->e
.name
, b
.head
);
166 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
167 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
169 config
= malloc(blob_pad_len(b
.head
));
175 memcpy(config
, b
.head
, blob_pad_len(b
.head
));
176 interface_add(iface
, config
);
178 dev
= iface
->main_dev
.dev
;
179 if (!dev
|| !dev
->default_config
)
182 blob_buf_init(&b
, 0);
183 uci_to_blob(&b
, s
, dev
->type
->config_params
);
184 if (blob_len(b
.head
) == 0)
187 device_set_config(dev
, dev
->type
, b
.head
);
191 config_init_devices(void)
193 struct uci_element
*e
;
195 uci_foreach_element(&uci_network
->sections
, e
) {
196 struct uci_section
*s
= uci_to_section(e
);
197 const struct device_type
*devtype
;
198 const char *type
, *name
;
200 if (strcmp(s
->type
, "device") != 0)
203 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
207 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
208 if (type
&& !strcmp(type
, "bridge"))
209 devtype
= &bridge_device_type
;
211 devtype
= &simple_device_type
;
213 blob_buf_init(&b
, 0);
214 uci_to_blob(&b
, s
, devtype
->config_params
);
215 device_create(name
, devtype
, b
.head
);
220 config_diff(struct blob_attr
**tb1
, struct blob_attr
**tb2
,
221 const struct config_param_list
*config
, unsigned long *diff
)
226 for (i
= 0; i
< config
->n_params
; i
++) {
227 if (!tb1
[i
] && !tb2
[i
])
230 if (!!tb1
[i
] != !!tb2
[i
])
233 if (blob_len(tb1
[i
]) != blob_len(tb2
[i
]))
236 if (memcmp(tb1
[i
], tb2
[i
], blob_raw_len(tb1
[i
])) != 0)
254 __config_check_equal(struct blob_attr
*c1
, struct blob_attr
*c2
,
255 const struct config_param_list
*config
)
257 struct blob_attr
**tb1
, **tb2
;
265 tb1
= alloca(config
->n_params
* sizeof(struct blob_attr
*));
266 blobmsg_parse(config
->params
, config
->n_params
, tb1
,
267 blob_data(c1
), blob_len(c1
));
269 tb2
= alloca(config
->n_params
* sizeof(struct blob_attr
*));
270 blobmsg_parse(config
->params
, config
->n_params
, tb2
,
271 blob_data(c2
), blob_len(c2
));
273 return !config_diff(tb1
, tb2
, config
, NULL
);
277 config_check_equal(struct blob_attr
*c1
, struct blob_attr
*c2
,
278 const struct config_param_list
*config
)
282 if (!__config_check_equal(c1
, c2
, config
))
285 for (i
= 0; i
< config
->n_next
; i
++) {
286 if (!__config_check_equal(c1
, c2
, config
->next
[i
]))
294 config_memdup(struct blob_attr
*attr
)
296 struct blob_attr
*ret
;
297 int size
= blob_pad_len(attr
);
303 memcpy(ret
, attr
, size
);
307 static struct uci_package
*
308 config_init_package(const char *config
)
310 struct uci_context
*ctx
= uci_ctx
;
311 struct uci_package
*p
= NULL
;
314 ctx
= uci_alloc_context();
318 uci_set_confdir(ctx
, "./config");
319 uci_set_savedir(ctx
, "./tmp");
322 p
= uci_lookup_package(ctx
, config
);
327 if (uci_load(ctx
, "network", &p
))
334 config_init_interfaces(void)
336 struct uci_element
*e
;
338 uci_foreach_element(&uci_network
->sections
, e
) {
339 struct uci_section
*s
= uci_to_section(e
);
341 if (!strcmp(s
->type
, "interface"))
342 config_parse_interface(s
);
347 config_init_all(void)
349 uci_network
= config_init_package("network");
351 fprintf(stderr
, "Failed to load network config\n");
358 device_reset_config();
359 config_init_devices();
360 config_init_interfaces();
366 device_init_pending();
367 device_free_unused(NULL
);
368 interface_start_pending();