[package] dnsmasq: allow multiple hostnames per domain section
[openwrt.git] / package / swconfig / src / uci.c
1 /*
2 * uci.c: UCI binding for the switch configuration utility
3 *
4 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundatio.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <inttypes.h>
20 #include <errno.h>
21 #include <stdint.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <uci.h>
26
27 #include <linux/types.h>
28 #include <linux/netlink.h>
29 #include <linux/genetlink.h>
30 #include <netlink/netlink.h>
31 #include <netlink/genl/genl.h>
32 #include <netlink/genl/ctrl.h>
33 #include <linux/switch.h>
34 #include "swlib.h"
35
36 #ifndef ARRAY_SIZE
37 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
38 #endif
39
40 struct swlib_setting {
41 struct switch_attr *attr;
42 const char *name;
43 int port_vlan;
44 const char *val;
45 struct swlib_setting *next;
46 };
47
48 struct swlib_setting early_settings[] = {
49 { .name = "reset" },
50 { .name = "enable_vlan" },
51 };
52
53 static struct swlib_setting *settings;
54 static struct swlib_setting **head;
55
56 static int
57 swlib_map_settings(struct switch_dev *dev, int type, int port_vlan, struct uci_section *s)
58 {
59 struct swlib_setting *setting;
60 struct switch_attr *attr;
61 struct uci_element *e;
62 struct uci_option *o;
63 int i;
64
65 uci_foreach_element(&s->options, e) {
66 o = uci_to_option(e);
67
68 if (o->type != UCI_TYPE_STRING)
69 continue;
70
71 if (!strcmp(e->name, "device"))
72 continue;
73
74 /* map early settings */
75 if (type == SWLIB_ATTR_GROUP_GLOBAL) {
76 int i;
77
78 for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
79 if (strcmp(e->name, early_settings[i].name) != 0)
80 continue;
81
82 early_settings[i].val = o->v.string;
83 goto skip;
84 }
85 }
86
87 attr = swlib_lookup_attr(dev, type, e->name);
88 if (!attr)
89 continue;
90
91 setting = malloc(sizeof(struct swlib_setting));
92 memset(setting, 0, sizeof(struct swlib_setting));
93 setting->attr = attr;
94 setting->port_vlan = port_vlan;
95 setting->val = o->v.string;
96 *head = setting;
97 head = &setting->next;
98 skip:
99 continue;
100 }
101 }
102
103 int swlib_apply_from_uci(struct switch_dev *dev, struct uci_package *p)
104 {
105 struct switch_attr *attr;
106 struct uci_context *ctx = p->ctx;
107 struct uci_element *e;
108 struct uci_section *s;
109 struct uci_option *o;
110 struct uci_ptr ptr;
111 struct switch_val val;
112 int i;
113
114 settings = NULL;
115 head = &settings;
116
117 uci_foreach_element(&p->sections, e) {
118 struct uci_element *n;
119
120 s = uci_to_section(e);
121
122 if (strcmp(s->type, "switch") != 0)
123 continue;
124
125 uci_foreach_element(&s->options, n) {
126 struct uci_option *o = uci_to_option(n);
127
128 if (strcmp(n->name, "name") != 0)
129 continue;
130
131 if (o->type != UCI_TYPE_STRING)
132 continue;
133
134 if (!strcmp(o->v.string, dev->dev_name))
135 goto found;
136
137 break;
138 }
139
140 if (strcmp(e->name, dev->dev_name) != 0)
141 continue;
142
143 goto found;
144 }
145
146 /* not found */
147 return -1;
148
149 found:
150 /* look up available early options, which need to be taken care
151 * of in the correct order */
152 for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
153 early_settings[i].attr = swlib_lookup_attr(dev,
154 SWLIB_ATTR_GROUP_GLOBAL, early_settings[i].name);
155 }
156 swlib_map_settings(dev, SWLIB_ATTR_GROUP_GLOBAL, 0, s);
157
158 /* look for port or vlan sections */
159 uci_foreach_element(&p->sections, e) {
160 struct uci_element *os;
161 s = uci_to_section(e);
162
163 if (!strcmp(s->type, "switch_port")) {
164 char *devn, *port, *port_err = NULL;
165 int port_n;
166
167 uci_foreach_element(&s->options, os) {
168 o = uci_to_option(os);
169 if (o->type != UCI_TYPE_STRING)
170 continue;
171
172 if (!strcmp(os->name, "device")) {
173 devn = o->v.string;
174 if (strcmp(devn, dev->dev_name) != 0)
175 devn = NULL;
176 } else if (!strcmp(os->name, "port")) {
177 port = o->v.string;
178 }
179 }
180 if (!dev || !port || !port[0])
181 continue;
182
183 port_n = strtoul(port, &port_err, 0);
184 if (port_err && port_err[0])
185 continue;
186
187 swlib_map_settings(dev, SWLIB_ATTR_GROUP_PORT, port_n, s);
188 } else if (!strcmp(s->type, "switch_vlan")) {
189 char *devn, *vlan, *vlan_err = NULL;
190 int vlan_n;
191
192 uci_foreach_element(&s->options, os) {
193 o = uci_to_option(os);
194 if (o->type != UCI_TYPE_STRING)
195 continue;
196
197 if (!strcmp(os->name, "device")) {
198 devn = o->v.string;
199 if (strcmp(devn, dev->dev_name) != 0)
200 devn = NULL;
201 } else if (!strcmp(os->name, "vlan")) {
202 vlan = o->v.string;
203 }
204 }
205 if (!dev || !vlan || !vlan[0])
206 continue;
207
208 vlan_n = strtoul(vlan, &vlan_err, 0);
209 if (vlan_err && vlan_err[0])
210 continue;
211
212 swlib_map_settings(dev, SWLIB_ATTR_GROUP_VLAN, vlan_n, s);
213 }
214 }
215
216 for (i = 0; i < ARRAY_SIZE(early_settings); i++) {
217 struct swlib_setting *st = &early_settings[i];
218 if (!st->attr || !st->val)
219 continue;
220 swlib_set_attr_string(dev, st->attr, st->port_vlan, st->val);
221
222 }
223
224 while (settings) {
225 struct swlib_setting *st = settings;
226
227 swlib_set_attr_string(dev, st->attr, st->port_vlan, st->val);
228 st = st->next;
229 free(settings);
230 settings = st;
231 }
232
233 /* Apply the config */
234 attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, "apply");
235 if (!attr)
236 return 0;
237
238 memset(&val, 0, sizeof(val));
239 swlib_set_attr(dev, attr, &val);
240
241 return 0;
242 }
This page took 0.090638 seconds and 5 git commands to generate.