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 list_head list;
24 struct list_head alias;
29 unsigned char ipaddr[4];
32 struct ucimap_list *aliases;
36 struct list_head list;
39 struct uci_network *interface;
43 network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
45 struct uci_network *net = section;
46 unsigned char *target = data->s;
50 if (sscanf(str, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
53 for (i = 0; i < 4; i++)
54 target[i] = (char) tmp[i];
60 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
62 struct uci_network *net = section;
64 INIT_LIST_HEAD(&net->list);
65 INIT_LIST_HEAD(&net->alias);
66 net->name = s->e.name;
72 network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
74 struct uci_alias *alias = section;
76 INIT_LIST_HEAD(&alias->list);
77 alias->name = s->e.name;
82 network_add_interface(struct uci_map *map, void *section)
84 struct uci_network *net = section;
86 list_add(&net->list, &ifs);
92 network_add_alias(struct uci_map *map, void *section)
94 struct uci_alias *a = section;
97 list_add(&a->list, &a->interface->alias);
103 struct uci_optmap map;
107 static struct uci_sectionmap network_interface;
108 static struct uci_sectionmap network_alias;
110 static struct my_optmap network_interface_options[] = {
113 UCIMAP_OPTION(struct uci_network, proto),
114 .type = UCIMAP_STRING,
121 UCIMAP_OPTION(struct uci_network, ifname),
122 .type = UCIMAP_STRING,
128 UCIMAP_OPTION(struct uci_network, ipaddr),
129 .type = UCIMAP_CUSTOM,
131 .parse = network_parse_ip,
136 UCIMAP_OPTION(struct uci_network, enabled),
143 UCIMAP_OPTION(struct uci_network, test),
150 UCIMAP_OPTION(struct uci_network, aliases),
151 .type = UCIMAP_LIST | UCIMAP_SECTION,
152 .data.sm = &network_alias
157 static struct uci_sectionmap network_interface = {
159 .alloc_len = sizeof(struct uci_network),
160 .init = network_init_interface,
161 .add = network_add_interface,
162 .options = &network_interface_options[0].map,
163 .n_options = ARRAY_SIZE(network_interface_options),
164 .options_size = sizeof(struct my_optmap)
167 static struct uci_optmap network_alias_options[] = {
169 UCIMAP_OPTION(struct uci_alias, interface),
170 .type = UCIMAP_SECTION,
171 .data.sm = &network_interface
175 static struct uci_sectionmap network_alias = {
177 .options = network_alias_options,
178 .alloc_len = sizeof(struct uci_network),
179 .init = network_init_alias,
180 .add = network_add_alias,
181 .n_options = ARRAY_SIZE(network_alias_options),
184 static struct uci_sectionmap *network_smap[] = {
189 static struct uci_map network_map = {
190 .sections = network_smap,
191 .n_sections = ARRAY_SIZE(network_smap),
195 int main(int argc, char **argv)
197 struct uci_context *ctx;
198 struct uci_package *pkg;
199 struct list_head *p, *p2;
200 struct uci_network *net;
201 struct uci_alias *alias;
204 INIT_LIST_HEAD(&ifs);
205 ctx = uci_alloc_context();
206 ucimap_init(&network_map);
208 uci_load(ctx, "network", &pkg);
210 ucimap_parse(&network_map, pkg);
212 list_for_each(p, &ifs) {
213 net = list_entry(p, struct uci_network, list);
214 printf("New network section '%s'\n"
217 " ipaddr: %d.%d.%d.%d\n"
223 net->ipaddr[0], net->ipaddr[1],
224 net->ipaddr[2], net->ipaddr[3],
226 (net->enabled ? "on" : "off"));
228 for (i = 0; i < net->aliases->n_items; i++) {
229 alias = net->aliases->item[i].section;
230 printf("New alias: %s\n", alias->name);
233 net->ipaddr = "2.3.4.5";
234 ucimap_set_changed(net, &net->ipaddr);
235 ucimap_store_section(&network_map, pkg, net);
242 ucimap_cleanup(&network_map);
243 uci_free_context(ctx);