summaryrefslogtreecommitdiff
path: root/ucimap-example.c
blob: 3fd3765ab2c92ba0cb8d5f8b6a531aaddd1e6940 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * ucimap-example - sample code for the ucimap library
 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ucimap.h>

struct list_head ifs;

struct uci_network {
	struct list_head list;
	struct list_head alias;

	const char *name;
	const char *proto;
	const char *ifname;
	const char *ipaddr;
	int test;
	bool enabled;
	struct list_head aliases;
};

struct uci_alias {
	struct list_head list;

	const char *name;
	struct uci_network *interface;
};

static int
network_init_interface(struct uci_map *map, void *section, struct uci_section *s)
{
	struct uci_network *net = section;

	INIT_LIST_HEAD(&net->list);
	INIT_LIST_HEAD(&net->alias);
	net->name = s->e.name;
	net->test = -1;
	return 0;
}

static int
network_init_alias(struct uci_map *map, void *section, struct uci_section *s)
{
	struct uci_alias *alias = section;

	INIT_LIST_HEAD(&alias->list);
	alias->name = s->e.name;
	return 0;
}

static int
network_add_interface(struct uci_map *map, void *section)
{
	struct uci_network *net = section;

	list_add(&net->list, &ifs);

	return 0;
}

static int
network_add_alias(struct uci_map *map, void *section)
{
	struct uci_alias *a = section;

	if (a->interface)
		list_add(&a->list, &a->interface->alias);

	return 0;
}

struct uci_sectmap network_interface;
struct uci_sectmap network_alias;

struct uci_optmap network_interface_options[] = {
	OPTMAP_OPTION(UCIMAP_STRING, struct uci_network, proto, .data.s.maxlen = 32),
	OPTMAP_OPTION(UCIMAP_STRING, struct uci_network, ifname),
	OPTMAP_OPTION(UCIMAP_STRING, struct uci_network, ipaddr),
	OPTMAP_OPTION(UCIMAP_BOOL, struct uci_network, enabled),
	OPTMAP_OPTION(UCIMAP_INT, struct uci_network, test),
	OPTMAP_OPTION(UCIMAP_LIST | UCIMAP_SECTION, struct uci_network, aliases, .data.sm = &network_alias),
};

struct uci_sectmap network_interface = {
	.type = "interface",
	.options = network_interface_options,
	.alloc_len = sizeof(struct uci_network),
	.init_section = network_init_interface,
	.add_section = network_add_interface,
	.n_options = ARRAY_SIZE(network_interface_options),
};

struct uci_optmap network_alias_options[] = {
	OPTMAP_OPTION(UCIMAP_SECTION, struct uci_alias, interface, .data.sm = &network_interface),
};

struct uci_sectmap network_alias = {
	.type = "alias",
	.options = network_alias_options,
	.alloc_len = sizeof(struct uci_network),
	.init_section = network_init_alias,
	.add_section = network_add_alias,
	.n_options = ARRAY_SIZE(network_alias_options),
};

struct uci_sectmap *network_smap[] = {
	&network_interface,
	&network_alias,
};

struct uci_map network_map = {
	.sections = network_smap,
	.n_sections = ARRAY_SIZE(network_smap),
};


int main(int argc, char **argv)
{
	struct uci_context *ctx;
	struct uci_package *pkg;
	struct list_head *p, *p2;
	struct uci_network *net;
	struct uci_alias *alias;

	INIT_LIST_HEAD(&ifs);
	ctx = uci_alloc_context();
	ucimap_init(&network_map);

	uci_load(ctx, "network", &pkg);

	ucimap_parse(&network_map, pkg);

	list_for_each(p, &ifs) {
		net = list_entry(p, struct uci_network, list);
		printf("New network section '%s'\n"
			"	type: %s\n"
			"	ifname: %s\n"
			"	ipaddr: %s\n"
			"	test: %d\n"
			"	enabled: %s\n",
			net->name,
			net->proto,
			net->ifname,
			net->ipaddr,
			net->test,
			(net->enabled ? "on" : "off"));

		list_for_each(p2, &net->aliases) {
			struct uci_listmap *li = list_entry(p2, struct uci_listmap, list);
			alias = li->data.section;
			printf("New alias: %s\n", alias->name);
		}
#if 0
		net->ipaddr = "2.3.4.5";
		ucimap_set_changed(net, &net->ipaddr);
		ucimap_store_section(&network_map, pkg, net);
		uci_save(ctx, pkg);
#endif
	}


done:
	ucimap_cleanup(&network_map);
	uci_free_context(ctx);

	return 0;
}