d433f6155bdaf374fca440e04227012f4fb7a75b
[project/uci.git] / ucimap-example.c
1 /*
2 * ucimap-example - sample code for the ucimap library
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #include <strings.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <ucimap.h>
19
20 struct list_head ifs;
21
22 struct uci_network {
23 struct ucimap_section_data map;
24 struct list_head list;
25 struct list_head alias;
26
27 const char *name;
28 const char *proto;
29 const char *ifname;
30 unsigned char *ipaddr;
31 int test;
32 bool enabled;
33 struct ucimap_list *aliases;
34 };
35
36 struct uci_alias {
37 struct ucimap_section_data map;
38 struct list_head list;
39
40 const char *name;
41 struct uci_network *interface;
42 };
43
44 static int
45 network_parse_ip(void *section, struct uci_optmap *om, union ucimap_data *data, const char *str)
46 {
47 unsigned char *target;
48 int tmp[4];
49 int i;
50
51 if (sscanf(str, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
52 return -1;
53
54 target = malloc(4);
55 if (!target)
56 return -1;
57
58 *data->data = target;
59 for (i = 0; i < 4; i++)
60 target[i] = (char) tmp[i];
61
62 return 0;
63 }
64
65 static int
66 network_format_ip(void *section, struct uci_optmap *om, union ucimap_data *data, char **str)
67 {
68 static char buf[16];
69 unsigned char *ip = (unsigned char *) data->data[0];
70
71 if (ip) {
72 sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
73 *str = buf;
74 } else {
75 *str = NULL;
76 }
77
78 return 0;
79 }
80
81 static void
82 network_free_ip(void *section, struct uci_optmap *om, void *ptr)
83 {
84 free(ptr);
85 }
86
87 static int
88 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
89 {
90 struct uci_network *net = section;
91
92 INIT_LIST_HEAD(&net->list);
93 INIT_LIST_HEAD(&net->alias);
94 net->name = s->e.name;
95 net->test = -1;
96 return 0;
97 }
98
99 static int
100 network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
101 {
102 struct uci_alias *alias = section;
103
104 INIT_LIST_HEAD(&alias->list);
105 alias->name = s->e.name;
106 return 0;
107 }
108
109 static int
110 network_add_interface(struct uci_map *map, void *section)
111 {
112 struct uci_network *net = section;
113
114 list_add_tail(&net->list, &ifs);
115
116 return 0;
117 }
118
119 static int
120 network_add_alias(struct uci_map *map, void *section)
121 {
122 struct uci_alias *a = section;
123
124 if (a->interface)
125 list_add_tail(&a->list, &a->interface->alias);
126
127 return 0;
128 }
129
130 static struct ucimap_section_data *
131 network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
132 {
133 struct uci_network *p = malloc(sizeof(struct uci_network));
134 memset(p, 0, sizeof(struct uci_network));
135 return &p->map;
136 }
137
138 struct my_optmap {
139 struct uci_optmap map;
140 int test;
141 };
142
143 static struct uci_sectionmap network_interface;
144 static struct uci_sectionmap network_alias;
145
146 static struct my_optmap network_interface_options[] = {
147 {
148 .map = {
149 UCIMAP_OPTION(struct uci_network, proto),
150 .type = UCIMAP_STRING,
151 .name = "proto",
152 .data.s.maxlen = 32,
153 }
154 },
155 {
156 .map = {
157 UCIMAP_OPTION(struct uci_network, ifname),
158 .type = UCIMAP_STRING,
159 .name = "ifname"
160 }
161 },
162 {
163 .map = {
164 UCIMAP_OPTION(struct uci_network, ipaddr),
165 .type = UCIMAP_CUSTOM,
166 .name = "ipaddr",
167 .parse = network_parse_ip,
168 .format = network_format_ip,
169 .free = network_free_ip,
170 }
171 },
172 {
173 .map = {
174 UCIMAP_OPTION(struct uci_network, enabled),
175 .type = UCIMAP_BOOL,
176 .name = "enabled",
177 }
178 },
179 {
180 .map = {
181 UCIMAP_OPTION(struct uci_network, test),
182 .type = UCIMAP_INT,
183 .name = "test"
184 }
185 },
186 {
187 .map = {
188 UCIMAP_OPTION(struct uci_network, aliases),
189 .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
190 .data.sm = &network_alias
191 }
192 }
193 };
194
195 static struct uci_sectionmap network_interface = {
196 UCIMAP_SECTION(struct uci_network, map),
197 .type = "interface",
198 .alloc = network_allocate,
199 .init = network_init_interface,
200 .add = network_add_interface,
201 .options = &network_interface_options[0].map,
202 .n_options = ARRAY_SIZE(network_interface_options),
203 .options_size = sizeof(struct my_optmap)
204 };
205
206 static struct uci_optmap network_alias_options[] = {
207 {
208 UCIMAP_OPTION(struct uci_alias, interface),
209 .type = UCIMAP_SECTION,
210 .data.sm = &network_interface
211 }
212 };
213
214 static struct uci_sectionmap network_alias = {
215 UCIMAP_SECTION(struct uci_alias, map),
216 .type = "alias",
217 .options = network_alias_options,
218 .init = network_init_alias,
219 .add = network_add_alias,
220 .n_options = ARRAY_SIZE(network_alias_options),
221 };
222
223 static struct uci_sectionmap *network_smap[] = {
224 &network_interface,
225 &network_alias,
226 };
227
228 static struct uci_map network_map = {
229 .sections = network_smap,
230 .n_sections = ARRAY_SIZE(network_smap),
231 };
232
233
234 int main(int argc, char **argv)
235 {
236 struct uci_context *ctx;
237 struct uci_package *pkg;
238 struct list_head *p;
239 struct uci_network *net;
240 struct uci_alias *alias;
241 bool set = false;
242 int i;
243
244 INIT_LIST_HEAD(&ifs);
245 ctx = uci_alloc_context();
246 ucimap_init(&network_map);
247
248 if ((argc >= 2) && !strcmp(argv[1], "-s")) {
249 uci_set_savedir(ctx, "./test/save");
250 set = true;
251 }
252
253 uci_set_confdir(ctx, "./test/config");
254 uci_load(ctx, "network", &pkg);
255
256 ucimap_parse(&network_map, pkg);
257
258 list_for_each(p, &ifs) {
259 const unsigned char *ipaddr;
260 int n_aliases = 0;
261
262 net = list_entry(p, struct uci_network, list);
263 ipaddr = net->ipaddr;
264 if (!ipaddr)
265 ipaddr = (const unsigned char *) "\x00\x00\x00\x00";
266
267 printf("New network section '%s'\n"
268 " type: %s\n"
269 " ifname: %s\n"
270 " ipaddr: %d.%d.%d.%d\n"
271 " test: %d\n"
272 " enabled: %s\n",
273 net->name,
274 net->proto,
275 net->ifname,
276 ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3],
277 net->test,
278 (net->enabled ? "on" : "off"));
279
280 if (net->aliases->n_items > 0) {
281 printf("Configured aliases:");
282 for (i = 0; i < net->aliases->n_items; i++) {
283 alias = net->aliases->item[i].ptr;
284 printf(" %s", alias->name);
285 }
286 printf("\n");
287 }
288 list_for_each_entry(alias, &net->alias, list) {
289 n_aliases++;
290 for (i = 0; i < net->aliases->n_items; i++) {
291 if (alias == net->aliases->item[i].ptr)
292 goto next_alias;
293 }
294 printf("New alias: %s\n", alias->name);
295 next_alias:
296 continue;
297 }
298 if (set && !strcmp(net->name, "lan")) {
299 ucimap_free_item(&net->map, &net->ipaddr);
300 ucimap_set_changed(&net->map, &net->ipaddr);
301 ucimap_resize_list(&net->map, &net->aliases, n_aliases);
302 net->aliases->n_items = 0;
303 list_for_each_entry(alias, &net->alias, list) {
304 net->aliases->item[net->aliases->n_items++].ptr = alias;
305 }
306 ucimap_set_changed(&net->map, &net->aliases);
307 ucimap_store_section(&network_map, pkg, &net->map);
308 uci_save(ctx, pkg);
309 }
310 }
311
312
313 ucimap_cleanup(&network_map);
314 uci_free_context(ctx);
315
316 return 0;
317 }