2 * ucimap-example - sample code for the ucimap library
3 * Copyright (C) 2008-2009 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
24 struct ucimap_section_data map
;
25 struct list_head list
;
26 struct list_head alias
;
31 unsigned char *ipaddr
;
34 struct ucimap_list
*aliases
;
38 struct ucimap_section_data map
;
39 struct list_head list
;
42 struct uci_network
*interface
;
46 network_parse_ip(void *section
, struct uci_optmap
*om
, union ucimap_data
*data
, const char *str
)
48 unsigned char *target
;
52 if (sscanf(str
, "%d.%d.%d.%d", &tmp
[0], &tmp
[1], &tmp
[2], &tmp
[3]) != 4)
60 for (i
= 0; i
< 4; i
++)
61 target
[i
] = (char) tmp
[i
];
67 network_format_ip(void *section
, struct uci_optmap
*om
, union ucimap_data
*data
, char **str
)
70 unsigned char *ip
= (unsigned char *) data
->ptr
;
73 sprintf(buf
, "%d.%d.%d.%d", ip
[0], ip
[1], ip
[2], ip
[3]);
83 network_free_ip(void *section
, struct uci_optmap
*om
, void *ptr
)
89 network_init_interface(struct uci_map
*map
, void *section
, struct uci_section
*s
)
91 struct uci_network
*net
= section
;
93 INIT_LIST_HEAD(&net
->list
);
94 INIT_LIST_HEAD(&net
->alias
);
95 net
->name
= s
->e
.name
;
101 network_init_alias(struct uci_map
*map
, void *section
, struct uci_section
*s
)
103 struct uci_alias
*alias
= section
;
105 INIT_LIST_HEAD(&alias
->list
);
106 alias
->name
= s
->e
.name
;
111 network_add_interface(struct uci_map
*map
, void *section
)
113 struct uci_network
*net
= section
;
115 list_add_tail(&net
->list
, &ifs
);
121 network_add_alias(struct uci_map
*map
, void *section
)
123 struct uci_alias
*a
= section
;
126 list_add_tail(&a
->list
, &a
->interface
->alias
);
131 static struct ucimap_section_data
*
132 network_allocate(struct uci_map
*map
, struct uci_sectionmap
*sm
, struct uci_section
*s
)
134 struct uci_network
*p
= malloc(sizeof(struct uci_network
));
137 memset(p
, 0, sizeof(struct uci_network
));
142 struct uci_optmap map
;
146 static struct uci_sectionmap network_interface
;
147 static struct uci_sectionmap network_alias
;
149 static struct my_optmap network_interface_options
[] = {
152 UCIMAP_OPTION(struct uci_network
, proto
),
153 .type
= UCIMAP_STRING
,
160 UCIMAP_OPTION(struct uci_network
, ifname
),
161 .type
= UCIMAP_STRING
,
167 UCIMAP_OPTION(struct uci_network
, ipaddr
),
168 .type
= UCIMAP_CUSTOM
,
170 .parse
= network_parse_ip
,
171 .format
= network_format_ip
,
172 .free
= network_free_ip
,
177 UCIMAP_OPTION(struct uci_network
, enabled
),
184 UCIMAP_OPTION(struct uci_network
, test
),
191 UCIMAP_OPTION(struct uci_network
, aliases
),
192 .type
= UCIMAP_LIST
| UCIMAP_SECTION
| UCIMAP_LIST_AUTO
,
193 .data
.sm
= &network_alias
198 static struct uci_sectionmap network_interface
= {
199 UCIMAP_SECTION(struct uci_network
, map
),
201 .alloc
= network_allocate
,
202 .init
= network_init_interface
,
203 .add
= network_add_interface
,
204 .options
= &network_interface_options
[0].map
,
205 .n_options
= ARRAY_SIZE(network_interface_options
),
206 .options_size
= sizeof(struct my_optmap
)
209 static struct uci_optmap network_alias_options
[] = {
211 UCIMAP_OPTION(struct uci_alias
, interface
),
212 .type
= UCIMAP_SECTION
,
213 .data
.sm
= &network_interface
217 static struct uci_sectionmap network_alias
= {
218 UCIMAP_SECTION(struct uci_alias
, map
),
220 .options
= network_alias_options
,
221 .init
= network_init_alias
,
222 .add
= network_add_alias
,
223 .n_options
= ARRAY_SIZE(network_alias_options
),
226 static struct uci_sectionmap
*network_smap
[] = {
231 static struct uci_map network_map
= {
232 .sections
= network_smap
,
233 .n_sections
= ARRAY_SIZE(network_smap
),
237 int main(int argc
, char **argv
)
239 struct uci_context
*ctx
;
240 struct uci_package
*pkg
;
242 struct uci_network
*net
;
243 struct uci_alias
*alias
;
247 INIT_LIST_HEAD(&ifs
);
248 ctx
= uci_alloc_context();
251 ucimap_init(&network_map
);
253 if ((argc
>= 2) && !strcmp(argv
[1], "-s")) {
254 uci_set_savedir(ctx
, "./test/save");
258 uci_set_confdir(ctx
, "./test/config");
259 uci_load(ctx
, "network", &pkg
);
261 ucimap_parse(&network_map
, pkg
);
263 list_for_each(p
, &ifs
) {
264 const unsigned char *ipaddr
;
267 net
= list_entry(p
, struct uci_network
, list
);
268 ipaddr
= net
->ipaddr
;
270 ipaddr
= (const unsigned char *) "\x00\x00\x00\x00";
272 printf("New network section '%s'\n"
275 " ipaddr: %d.%d.%d.%d\n"
281 ipaddr
[0], ipaddr
[1], ipaddr
[2], ipaddr
[3],
283 (net
->enabled
? "on" : "off"));
285 if (net
->aliases
->n_items
> 0) {
286 printf("Configured aliases:");
287 for (i
= 0; i
< net
->aliases
->n_items
; i
++) {
288 alias
= net
->aliases
->item
[i
].ptr
;
289 printf(" %s", alias
->name
);
293 list_for_each_entry(alias
, &net
->alias
, list
) {
295 for (i
= 0; i
< net
->aliases
->n_items
; i
++) {
296 if (alias
== net
->aliases
->item
[i
].ptr
)
299 printf("New alias: %s\n", alias
->name
);
303 if (set
&& !strcmp(net
->name
, "lan")) {
304 ucimap_free_item(&net
->map
, &net
->ipaddr
);
305 ucimap_set_changed(&net
->map
, &net
->ipaddr
);
306 ucimap_resize_list(&net
->map
, &net
->aliases
, n_aliases
);
307 net
->aliases
->n_items
= 0;
308 list_for_each_entry(alias
, &net
->alias
, list
) {
309 net
->aliases
->item
[net
->aliases
->n_items
++].ptr
= alias
;
311 ucimap_set_changed(&net
->map
, &net
->aliases
);
312 ucimap_store_section(&network_map
, pkg
, &net
->map
);
318 ucimap_cleanup(&network_map
);
319 uci_free_context(ctx
);