2 * ucimap-example - sample code for the ucimap library
3 * Copyright (C) 2008 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.
23 struct ucimap_section_data map
;
24 struct list_head list
;
25 struct list_head alias
;
30 unsigned char *ipaddr
;
33 struct ucimap_list
*aliases
;
37 struct ucimap_section_data map
;
38 struct list_head list
;
41 struct uci_network
*interface
;
45 network_parse_ip(void *section
, struct uci_optmap
*om
, union ucimap_data
*data
, const char *str
)
47 unsigned char *target
;
51 if (sscanf(str
, "%d.%d.%d.%d", &tmp
[0], &tmp
[1], &tmp
[2], &tmp
[3]) != 4)
59 for (i
= 0; i
< 4; i
++)
60 target
[i
] = (char) tmp
[i
];
66 network_format_ip(void *sction
, struct uci_optmap
*om
, union ucimap_data
*data
, char **str
)
69 unsigned char *ip
= (unsigned char *) data
->data
[0];
71 sprintf(buf
, "%d.%d.%d.%d", ip
[0], ip
[1], ip
[2], ip
[3]);
78 network_free_ip(void *section
, struct uci_optmap
*om
, void *ptr
)
84 network_init_interface(struct uci_map
*map
, void *section
, struct uci_section
*s
)
86 struct uci_network
*net
= section
;
88 INIT_LIST_HEAD(&net
->list
);
89 INIT_LIST_HEAD(&net
->alias
);
90 net
->name
= s
->e
.name
;
96 network_init_alias(struct uci_map
*map
, void *section
, struct uci_section
*s
)
98 struct uci_alias
*alias
= section
;
100 INIT_LIST_HEAD(&alias
->list
);
101 alias
->name
= s
->e
.name
;
106 network_add_interface(struct uci_map
*map
, void *section
)
108 struct uci_network
*net
= section
;
110 list_add_tail(&net
->list
, &ifs
);
116 network_add_alias(struct uci_map
*map
, void *section
)
118 struct uci_alias
*a
= section
;
121 list_add_tail(&a
->list
, &a
->interface
->alias
);
126 static struct ucimap_section_data
*
127 network_allocate(struct uci_map
*map
, struct uci_sectionmap
*sm
, struct uci_section
*s
)
129 struct uci_network
*p
= malloc(sizeof(struct uci_network
));
130 memset(p
, 0, sizeof(struct uci_network
));
135 struct uci_optmap map
;
139 static struct uci_sectionmap network_interface
;
140 static struct uci_sectionmap network_alias
;
142 static struct my_optmap network_interface_options
[] = {
145 UCIMAP_OPTION(struct uci_network
, proto
),
146 .type
= UCIMAP_STRING
,
153 UCIMAP_OPTION(struct uci_network
, ifname
),
154 .type
= UCIMAP_STRING
,
160 UCIMAP_OPTION(struct uci_network
, ipaddr
),
161 .type
= UCIMAP_CUSTOM
,
163 .parse
= network_parse_ip
,
164 .format
= network_format_ip
,
165 .free
= network_free_ip
,
170 UCIMAP_OPTION(struct uci_network
, enabled
),
177 UCIMAP_OPTION(struct uci_network
, test
),
184 UCIMAP_OPTION(struct uci_network
, aliases
),
185 .type
= UCIMAP_LIST
| UCIMAP_SECTION
| UCIMAP_LIST_AUTO
,
186 .data
.sm
= &network_alias
191 static struct uci_sectionmap network_interface
= {
192 UCIMAP_SECTION(struct uci_network
, map
),
194 .alloc
= network_allocate
,
195 .init
= network_init_interface
,
196 .add
= network_add_interface
,
197 .options
= &network_interface_options
[0].map
,
198 .n_options
= ARRAY_SIZE(network_interface_options
),
199 .options_size
= sizeof(struct my_optmap
)
202 static struct uci_optmap network_alias_options
[] = {
204 UCIMAP_OPTION(struct uci_alias
, interface
),
205 .type
= UCIMAP_SECTION
,
206 .data
.sm
= &network_interface
210 static struct uci_sectionmap network_alias
= {
211 UCIMAP_SECTION(struct uci_alias
, map
),
213 .options
= network_alias_options
,
214 .init
= network_init_alias
,
215 .add
= network_add_alias
,
216 .n_options
= ARRAY_SIZE(network_alias_options
),
219 static struct uci_sectionmap
*network_smap
[] = {
224 static struct uci_map network_map
= {
225 .sections
= network_smap
,
226 .n_sections
= ARRAY_SIZE(network_smap
),
230 int main(int argc
, char **argv
)
232 struct uci_context
*ctx
;
233 struct uci_package
*pkg
;
235 struct uci_network
*net
;
236 struct uci_alias
*alias
;
239 INIT_LIST_HEAD(&ifs
);
240 ctx
= uci_alloc_context();
241 ucimap_init(&network_map
);
243 uci_set_confdir(ctx
, "./test/config");
244 uci_load(ctx
, "network", &pkg
);
246 ucimap_parse(&network_map
, pkg
);
248 list_for_each(p
, &ifs
) {
249 const unsigned char *ipaddr
;
251 net
= list_entry(p
, struct uci_network
, list
);
252 ipaddr
= net
->ipaddr
;
254 ipaddr
= (const unsigned char *) "\x00\x00\x00\x00";
256 printf("New network section '%s'\n"
259 " ipaddr: %d.%d.%d.%d\n"
265 ipaddr
[0], ipaddr
[1], ipaddr
[2], ipaddr
[3],
267 (net
->enabled
? "on" : "off"));
269 for (i
= 0; i
< net
->aliases
->n_items
; i
++) {
270 alias
= net
->aliases
->item
[i
].ptr
;
271 printf("New alias: %s\n", alias
->name
);
274 memcpy(net
->ipaddr
, "\x01\x03\x04\x05", 4);
275 ucimap_set_changed(&net
->map
, &net
->ipaddr
);
276 ucimap_store_section(&network_map
, pkg
, &net
->map
);
282 ucimap_cleanup(&network_map
);
283 uci_free_context(ctx
);