2a4653259b9ff3fbafa73c49295724afe45d2275
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;
16 static void uci_attr_to_blob(struct blob_buf
*b
, const char *str
,
17 const char *name
, enum blobmsg_type type
)
23 case BLOBMSG_TYPE_STRING
:
24 blobmsg_add_string(b
, name
, str
);
26 case BLOBMSG_TYPE_BOOL
:
27 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
29 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
34 blobmsg_add_u8(b
, name
, intval
);
36 case BLOBMSG_TYPE_INT32
:
37 intval
= strtol(str
, &err
, 0);
41 blobmsg_add_u32(b
, name
, intval
);
48 static void uci_array_to_blob(struct blob_buf
*b
, struct uci_option
*o
,
49 enum blobmsg_type type
)
51 struct uci_element
*e
;
52 char *str
, *next
, *word
;
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
);
61 str
= strdup(o
->v
.string
);
64 while ((word
= strsep(&next
, " \t")) != NULL
) {
68 uci_attr_to_blob(b
, word
, NULL
, type
);
74 static void __uci_to_blob(struct blob_buf
*b
, struct uci_section
*s
,
75 const struct config_param_list
*p
)
77 const struct blobmsg_policy
*attr
= NULL
;
78 struct uci_element
*e
;
83 uci_foreach_element(&s
->options
, e
) {
84 for (i
= 0; i
< p
->n_params
; i
++) {
86 if (!strcmp(attr
->name
, e
->name
))
95 if (attr
->type
== BLOBMSG_TYPE_ARRAY
) {
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
);
105 if (o
->type
== UCI_TYPE_LIST
)
108 uci_attr_to_blob(b
, o
->v
.string
, attr
->name
, attr
->type
);
112 static void uci_to_blob(struct blob_buf
*b
, struct uci_section
*s
,
113 const struct config_param_list
*p
)
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
]);
123 config_parse_bridge_interface(struct uci_section
*s
)
127 name
= alloca(strlen(s
->e
.name
) + 4);
128 sprintf(name
, "br-%s", s
->e
.name
);
129 blobmsg_add_string(&b
, "name", name
);
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
);
137 blob_buf_init(&b
, 0);
138 blobmsg_add_string(&b
, "ifname", name
);
143 config_set_state(struct config_state
*state
, const struct blob_attr
*attr
)
145 state
->data
= malloc(blob_pad_len(attr
));
149 memcpy(state
->data
, attr
, blob_pad_len(attr
));
153 config_parse_interface(struct uci_section
*s
)
155 struct interface
*iface
;
158 DPRINTF("Create interface '%s'\n", s
->e
.name
);
160 blob_buf_init(&b
, 0);
162 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
163 if (type
&& !strcmp(type
, "bridge"))
164 if (config_parse_bridge_interface(s
))
167 uci_to_blob(&b
, s
, &interface_attr_list
);
168 iface
= interface_alloc(s
->e
.name
, b
.head
);
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
);
176 proto_init_interface(iface
, b
.head
);
177 iface
->config
.version
= config_version
;
181 config_init_devices(void)
183 struct uci_element
*e
;
185 uci_foreach_element(&uci_network
->sections
, e
) {
186 struct uci_section
*s
= uci_to_section(e
);
187 const struct device_type
*devtype
;
190 if (strcmp(s
->type
, "device") != 0)
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
;
198 devtype
= &simple_device_type
;
200 uci_to_blob(&b
, s
, devtype
->config_params
);
201 devtype
->create(b
.head
);
206 config_init_interfaces(const char *name
)
208 struct uci_context
*ctx
;
209 struct uci_package
*p
= NULL
;
210 struct uci_element
*e
;
212 ctx
= uci_alloc_context();
215 uci_set_confdir(ctx
, "./config");
217 if (uci_load(ctx
, "network", &p
)) {
218 fprintf(stderr
, "Failed to load network config\n");
225 config_init_devices();
227 uci_foreach_element(&p
->sections
, e
) {
228 struct uci_section
*s
= uci_to_section(e
);
230 if (name
&& strcmp(s
->e
.name
, name
) != 0)
233 if (!strcmp(s
->type
, "interface"))
234 config_parse_interface(s
);
236 device_free_unused(NULL
);
239 interface_start_pending();