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