9906592acabff3e4551da001b769497b680fa648
[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[4];
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 = (unsigned char *) data->s;
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 for (i = 0; i < 4; i++)
55 target[i] = (char) tmp[i];
56
57 return 0;
58 }
59
60 static int
61 network_format_ip(void *sction, struct uci_optmap *om, union ucimap_data *data, char **str)
62 {
63 static char buf[16];
64 unsigned char *ip = (unsigned char *) data->s;
65
66 sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
67 *str = buf;
68
69 return 0;
70 }
71
72 static int
73 network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
74 {
75 struct uci_network *net = section;
76
77 INIT_LIST_HEAD(&net->list);
78 INIT_LIST_HEAD(&net->alias);
79 net->name = s->e.name;
80 net->test = -1;
81 return 0;
82 }
83
84 static int
85 network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
86 {
87 struct uci_alias *alias = section;
88
89 INIT_LIST_HEAD(&alias->list);
90 alias->name = s->e.name;
91 return 0;
92 }
93
94 static int
95 network_add_interface(struct uci_map *map, void *section)
96 {
97 struct uci_network *net = section;
98
99 list_add(&net->list, &ifs);
100
101 return 0;
102 }
103
104 static int
105 network_add_alias(struct uci_map *map, void *section)
106 {
107 struct uci_alias *a = section;
108
109 if (a->interface)
110 list_add(&a->list, &a->interface->alias);
111
112 return 0;
113 }
114
115 static struct ucimap_section_data *
116 network_allocate(struct uci_map *map, struct uci_sectionmap *sm, struct uci_section *s)
117 {
118 struct uci_network *p = malloc(sizeof(struct uci_network));
119 memset(p, 0, sizeof(struct uci_network));
120 return &p->map;
121 }
122
123 struct my_optmap {
124 struct uci_optmap map;
125 int test;
126 };
127
128 static struct uci_sectionmap network_interface;
129 static struct uci_sectionmap network_alias;
130
131 static struct my_optmap network_interface_options[] = {
132 {
133 .map = {
134 UCIMAP_OPTION(struct uci_network, proto),
135 .type = UCIMAP_STRING,
136 .name = "proto",
137 .data.s.maxlen = 32,
138 }
139 },
140 {
141 .map = {
142 UCIMAP_OPTION(struct uci_network, ifname),
143 .type = UCIMAP_STRING,
144 .name = "ifname"
145 }
146 },
147 {
148 .map = {
149 UCIMAP_OPTION(struct uci_network, ipaddr),
150 .type = UCIMAP_CUSTOM,
151 .name = "ipaddr",
152 .parse = network_parse_ip,
153 .format = network_format_ip,
154 }
155 },
156 {
157 .map = {
158 UCIMAP_OPTION(struct uci_network, enabled),
159 .type = UCIMAP_BOOL,
160 .name = "enabled",
161 }
162 },
163 {
164 .map = {
165 UCIMAP_OPTION(struct uci_network, test),
166 .type = UCIMAP_INT,
167 .name = "test"
168 }
169 },
170 {
171 .map = {
172 UCIMAP_OPTION(struct uci_network, aliases),
173 .type = UCIMAP_LIST | UCIMAP_SECTION | UCIMAP_LIST_AUTO,
174 .data.sm = &network_alias
175 }
176 }
177 };
178
179 static struct uci_sectionmap network_interface = {
180 UCIMAP_SECTION(struct uci_network, map),
181 .type = "interface",
182 .alloc = network_allocate,
183 .init = network_init_interface,
184 .add = network_add_interface,
185 .options = &network_interface_options[0].map,
186 .n_options = ARRAY_SIZE(network_interface_options),
187 .options_size = sizeof(struct my_optmap)
188 };
189
190 static struct uci_optmap network_alias_options[] = {
191 {
192 UCIMAP_OPTION(struct uci_alias, interface),
193 .type = UCIMAP_SECTION,
194 .data.sm = &network_interface
195 }
196 };
197
198 static struct uci_sectionmap network_alias = {
199 UCIMAP_SECTION(struct uci_alias, map),
200 .type = "alias",
201 .options = network_alias_options,
202 .init = network_init_alias,
203 .add = network_add_alias,
204 .n_options = ARRAY_SIZE(network_alias_options),
205 };
206
207 static struct uci_sectionmap *network_smap[] = {
208 &network_interface,
209 &network_alias,
210 };
211
212 static struct uci_map network_map = {
213 .sections = network_smap,
214 .n_sections = ARRAY_SIZE(network_smap),
215 };
216
217
218 int main(int argc, char **argv)
219 {
220 struct uci_context *ctx;
221 struct uci_package *pkg;
222 struct list_head *p;
223 struct uci_network *net;
224 struct uci_alias *alias;
225 int i;
226
227 INIT_LIST_HEAD(&ifs);
228 ctx = uci_alloc_context();
229 ucimap_init(&network_map);
230
231 uci_set_confdir(ctx, "./test/config");
232 uci_load(ctx, "network", &pkg);
233
234 ucimap_parse(&network_map, pkg);
235
236 list_for_each(p, &ifs) {
237 net = list_entry(p, struct uci_network, list);
238 printf("New network section '%s'\n"
239 " type: %s\n"
240 " ifname: %s\n"
241 " ipaddr: %d.%d.%d.%d\n"
242 " test: %d\n"
243 " enabled: %s\n",
244 net->name,
245 net->proto,
246 net->ifname,
247 net->ipaddr[0], net->ipaddr[1],
248 net->ipaddr[2], net->ipaddr[3],
249 net->test,
250 (net->enabled ? "on" : "off"));
251
252 for (i = 0; i < net->aliases->n_items; i++) {
253 alias = net->aliases->item[i].ptr;
254 printf("New alias: %s\n", alias->name);
255 }
256 #if 0
257 memcpy(net->ipaddr, "\x01\x03\x04\x05", 4);
258 ucimap_set_changed(&net->map, &net->ipaddr);
259 ucimap_store_section(&network_map, pkg, &net->map);
260 uci_save(ctx, pkg);
261 #endif
262 }
263
264
265 ucimap_cleanup(&network_map);
266 uci_free_context(ctx);
267
268 return 0;
269 }