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