]> git.sur5r.net Git - ngadmin/blob - raw/src/attr.c
Separated lib in two parts : low level and high level.
[ngadmin] / raw / src / attr.c
1
2 #include <ngadmin.h>
3
4 #include "attr.h"
5 #include "protocol.h"
6
7
8 #define ATTR_HANDLER_ENTRY(at, sz, enc, dec)    {.attr = at, .size = sz, .encode = enc, .decode = dec}
9
10
11
12 static bool bool_endecode (struct attr *at, unsigned char ports UNUSED)
13 {
14         *(char*)at->data = (*(char*)at->data != 0);
15         
16         return true;
17 }
18
19
20 static bool ports_status_decode (struct attr *at, unsigned char ports)
21 {
22         struct attr_port_status *ps = at->data;
23         
24         if (ps->port < 1 || ps->port > ports)
25                 return false;
26         
27         switch (ps->status) {
28         case SPEED_DOWN:
29         case SPEED_10:
30         case SPEED_100:
31         case SPEED_1000:
32                 break;
33         default:
34                 return false;
35         }
36         
37         return true;
38 }
39
40
41 static bool port_stat_decode (struct attr *at, unsigned char ports)
42 {
43         struct attr_port_stat *ps = at->data;
44         unsigned long long *v;
45         
46         if (ps->port < 1 || ps->port > ports)
47                 return false;
48         
49         for (v = &ps->recv; ((char*)v) - ((char*)ps) < (int)sizeof(struct attr_port_stat); v++)
50                 *v = be64toh(*v);
51         
52         
53         return true;
54 }
55
56
57 static bool bitrate_decode (struct attr *at, unsigned char ports)
58 {
59         struct attr_bitrate *sb = at->data;
60         
61         if (sb->port < 1 || sb->port > ports)
62                 return false;
63         
64         sb->bitrate = ntohl(sb->bitrate);
65         if (sb->bitrate < BITRATE_UNSPEC || sb->bitrate > BITRATE_512M)
66                 return false;
67         
68         return true;
69 }
70
71
72 static bool bitrate_encode (struct attr *at, unsigned char ports)
73 {
74         struct attr_bitrate *sb = at->data;
75         
76         if (sb->port < 1 || sb->port > ports)
77                 return false;
78         
79         if (sb->bitrate < BITRATE_UNSPEC || sb->bitrate > BITRATE_512M)
80                 return false;
81         sb->bitrate = htonl(sb->bitrate);
82         
83         return true;
84 }
85
86
87 static bool qos_mode_endecode (struct attr *at, unsigned char ports UNUSED)
88 {
89         unsigned char v = *(char*)at->data;
90         
91         return (v == QOS_PORT || v == QOS_DOT);
92 }
93
94
95 static bool qos_endecode (struct attr *at, unsigned char ports)
96 {
97         struct attr_qos *aq = at->data;
98         
99         if (aq->port < 1 || aq->port > ports)
100                 return false;
101         
102         return (aq->prio >= PRIO_HIGH && aq->prio <= PRIO_LOW);
103 }
104
105
106 static bool pvid_decode (struct attr *at, unsigned char ports)
107 {
108         struct attr_pvid *ap= at->data;
109         
110         if (ap->port < 1 || ap->port > ports)
111                 return false;
112         
113         ap->vlan = ntohs(ap->vlan);
114         
115         return (ap->vlan >= VLAN_MIN && ap->vlan <= VLAN_MAX);
116 }
117
118
119 static bool pvid_encode (struct attr *at, unsigned char ports)
120 {
121         struct attr_pvid *ap= at->data;
122         
123         if (ap->port < 1 || ap->port > ports)
124                 return false;
125         
126         if (ap->vlan < VLAN_MIN || ap->vlan > VLAN_MAX)
127                 return false;
128         
129         ap->vlan = htons(ap->vlan);
130         
131         return true;
132 }
133
134
135 static bool vlan_destroy_encode (struct attr *at, unsigned char ports UNUSED)
136 {
137         unsigned short v = *(unsigned short*)at->data;
138         
139         if (v < VLAN_MIN || v > VLAN_MAX)
140                 return false;
141         
142         *(unsigned short*)at->data = htons(v);
143         
144         return true;
145 }
146
147
148 static bool mirror_decode (struct attr *at, unsigned char ports)
149 {
150         unsigned char *r = at->data, *p;
151         int port;
152         
153         
154         if (at->size != 3 + ((ports - 1) >> 3))
155                 return false;
156         
157         /* r[0] == 0 is allowed and means mirroring is disabled */
158         if (r[0] > ports)
159                 return false;
160         
161         p = malloc(1 + ports);
162         if (p == NULL)
163                 return false;
164         
165         memset(p, 0, 1 + ports);
166         
167         if (r[0] == 0)
168                 goto end;
169         
170         p[0] = r[0];
171         
172         for (port = 1; port <= ports; port++)
173                 p[port] = (r[2] >> (8 - port)) & 1; /* FIXME: if ports > 8 */
174         
175 end:
176         free(at->data);
177         at->data = p;
178         at->size = 1 + ports;
179         
180         
181         return true;
182 }
183
184
185 static bool mirror_encode (struct attr *at, unsigned char ports)
186 {
187         unsigned char *p = at->data, *r;
188         int port;
189         
190         
191         if (at->size != 1 + ports)
192                 return false;
193         
194         /* p[0] == 0 is allowed and means mirroring is disabled */
195         if (p[0] > ports)
196                 return false;
197         
198         r = malloc(3 + ((ports - 1) >> 3));
199         if (r == NULL)
200                 return false;
201         
202         memset(r, 0, 3 + ((ports - 1) >> 3));
203         
204         if (p[0] == 0)
205                 goto end;
206         
207         r[0] = p[0];
208         
209         for (port = 1; port <= ports; port++) {
210                 if (p[0] != port)
211                         r[2] |= (p[port] & 1) << (8 - port); /* FIXME: if ports > 8 */
212         }
213         
214 end:
215         free(at->data);
216         at->data = r;
217         at->size = 3 + ((ports - 1) >> 3);
218         
219         
220         return true;
221 }
222
223
224 static bool igmp_vlan_decode (struct attr *at, unsigned char ports UNUSED)
225 {
226         struct attr_igmp_vlan *aiv = at->data;
227         
228         aiv->enable = ntohs(aiv->enable);
229         if (aiv->enable != 0 && aiv->enable != 1)
230                 return false;
231         
232         aiv->vlan = ntohs(aiv->vlan);
233         if (aiv->vlan < VLAN_MIN || aiv->vlan > VLAN_MAX)
234                 return false;
235         
236         return true;
237 }
238
239
240 static bool igmp_vlan_encode (struct attr *at, unsigned char ports UNUSED)
241 {
242         struct attr_igmp_vlan *aiv = at->data;
243         
244         if (aiv->enable != 0 && aiv->enable != 1)
245                 return false;
246         aiv->enable = htons(aiv->enable);
247         
248         if (aiv->vlan < VLAN_MIN || aiv->vlan > VLAN_MAX)
249                 return false;
250         aiv->vlan = htons(aiv->vlan);
251         
252         return true;
253 }
254
255
256 static bool cabletest_do_encode (struct attr *at, unsigned char ports)
257 {
258         struct attr_cabletest_do *acd = at->data;
259         
260         if (acd->port < 1 || acd->port > ports)
261                 return false;
262         
263         return (acd->action == 1);
264 }
265
266
267 static bool cabletest_result_encode (struct attr *at, unsigned char ports)
268 {
269         unsigned char v = *(unsigned char*)at->data;
270         
271         return (v >= 1 && v <= ports);
272 }
273
274
275 static bool cabletest_result_decode (struct attr *at, unsigned char ports)
276 {
277         struct attr_cabletest_result *acr = at->data;
278         
279         if (acr->port < 1 || acr->port > ports)
280                 return false;
281         
282         acr->v1 = ntohl(acr->v1);
283         acr->v2 = ntohl(acr->v2);
284         
285         return true;
286 }
287
288
289 static bool cabletest_result_endecode (struct attr *at, unsigned char ports)
290 {
291         switch (at->size) {
292         
293         case 1:
294                 return cabletest_result_encode(at, ports);
295         
296         case sizeof(struct attr_cabletest_result):
297                 return cabletest_result_decode(at, ports);
298         
299         default:
300                 return false;
301         }
302 }
303
304
305 static bool vlan_type_endecode (struct attr *at, unsigned char ports UNUSED)
306 {
307         char v = *(char*)at->data;
308         
309         return (v >= VLAN_DISABLED && v <= VLAN_DOT_ADV);
310 }
311
312
313 static bool vlan_dot_decode (struct attr *at, unsigned char ports)
314 {
315         char *r = at->data;
316         struct attr_vlan_dot *avd;
317         int port;
318         
319         
320         if (at->size != (2 + 2 * (1 + ((ports - 1) >> 3))))
321                 return false;
322         
323         avd = malloc(sizeof(struct attr_vlan_dot) + ports);
324         if (avd == NULL)
325                 return false;
326         
327         avd->vlan = ntohs(*(unsigned short*)r);
328         r += 2;
329         
330         for (port = 0; port < ports; port++) {
331                 /* FIXME: if ports > 8 */
332                 if ((r[1] >> (7 - port)) & 1)
333                         avd->ports[port] = VLAN_TAGGED;
334                 else if ((r[0] >> (7 - port)) & 1)
335                         avd->ports[port] = VLAN_UNTAGGED;
336                 else
337                         avd->ports[port] = VLAN_NO;
338         }
339         
340         free(at->data);
341         at->data = avd;
342         at->size = sizeof(struct attr_vlan_dot) + ports;
343         
344         
345         return true;
346 }
347
348
349 static bool vlan_dot_encode (struct attr *at, unsigned char ports)
350 {
351         struct attr_vlan_dot *avd = at->data;
352         char *r, fl;
353         unsigned int size, port;
354         
355         
356         if (avd->vlan < VLAN_MIN || avd->vlan > VLAN_MAX)
357                 return false;
358         
359         /* just a header is valid */
360         if (at->size == sizeof(struct attr_vlan_dot))
361                 size = 2;
362         else if (at->size == sizeof(struct attr_vlan_dot) + ports)
363                 size = (2 + 2 * (1 + ((ports - 1) >> 3)));
364         else
365                 return false;
366         
367         r = malloc(size);
368         if (r == NULL)
369                 return false;
370         
371         memset(r, 0, size);
372         *(unsigned short*)r = htons(avd->vlan);
373         
374         if (size == 2)
375                 goto end;
376         
377         r += 2;
378         
379         for (port = 0; port < ports; port++) {
380                 /* FIXME: if ports > 8 */
381                 fl = (1 << (7 - port));
382                 switch (avd->ports[port]) {
383                 case VLAN_TAGGED:
384                         r[1] |= fl;
385                 case VLAN_UNTAGGED:
386                         r[0] |= fl;
387                 }
388         }
389         
390         r -= 2;
391         
392 end:
393         free(at->data);
394         at->data = r;
395         at->size = size;
396         
397         
398         return true;
399 }
400
401
402
403 /* WARNING: attributes codes MUST be in ascending order */
404 static const struct attr_handler attrtab[] = {
405         ATTR_HANDLER_ENTRY(ATTR_MAC, 6, NULL, NULL),
406         ATTR_HANDLER_ENTRY(ATTR_IP, 4, NULL, NULL),
407         ATTR_HANDLER_ENTRY(ATTR_NETMASK, 4, NULL, NULL),
408         ATTR_HANDLER_ENTRY(ATTR_GATEWAY, 4, NULL, NULL),
409         ATTR_HANDLER_ENTRY(ATTR_DHCP, 2, NULL, NULL),
410         ATTR_HANDLER_ENTRY(ATTR_RESTART, 1, NULL, NULL),
411         ATTR_HANDLER_ENTRY(ATTR_DEFAULTS, 1, NULL, NULL),
412         ATTR_HANDLER_ENTRY(ATTR_PORT_STATUS, sizeof(struct attr_port_status), NULL, ports_status_decode),
413         ATTR_HANDLER_ENTRY(ATTR_PORT_STATISTICS, sizeof(struct attr_port_stat), NULL, port_stat_decode),
414         ATTR_HANDLER_ENTRY(ATTR_STATS_RESET, 1, bool_endecode, bool_endecode),
415         ATTR_HANDLER_ENTRY(ATTR_CABLETEST_DO, sizeof(struct attr_cabletest_do), cabletest_do_encode, NULL),
416         ATTR_HANDLER_ENTRY(ATTR_CABLETEST_RESULT, 0, cabletest_result_endecode, cabletest_result_endecode),
417         ATTR_HANDLER_ENTRY(ATTR_VLAN_TYPE, 1, vlan_type_endecode, vlan_type_endecode),
418         ATTR_HANDLER_ENTRY(ATTR_VLAN_DOT_CONF, 0, vlan_dot_encode, vlan_dot_decode),
419         ATTR_HANDLER_ENTRY(ATTR_VLAN_DESTROY, 2, vlan_destroy_encode, NULL),
420         ATTR_HANDLER_ENTRY(ATTR_VLAN_PVID, sizeof(struct attr_pvid), pvid_encode, pvid_decode),
421         ATTR_HANDLER_ENTRY(ATTR_QOS_TYPE, 1, qos_mode_endecode, qos_mode_endecode),
422         ATTR_HANDLER_ENTRY(ATTR_QOS_CONFIG, sizeof(struct attr_qos), qos_endecode, qos_endecode),
423         ATTR_HANDLER_ENTRY(ATTR_BITRATE_INPUT, sizeof(struct attr_bitrate), bitrate_encode, bitrate_decode),
424         ATTR_HANDLER_ENTRY(ATTR_BITRATE_OUTPUT, sizeof(struct attr_bitrate), bitrate_encode, bitrate_decode),
425         ATTR_HANDLER_ENTRY(ATTR_STORM_ENABLE, 1, bool_endecode, bool_endecode),
426         ATTR_HANDLER_ENTRY(ATTR_STORM_BITRATE, sizeof(struct attr_bitrate), bitrate_encode, bitrate_decode),
427         ATTR_HANDLER_ENTRY(ATTR_MIRROR, 0, mirror_encode, mirror_decode),
428         ATTR_HANDLER_ENTRY(ATTR_PORTS_COUNT, 1, NULL, NULL),
429         ATTR_HANDLER_ENTRY(ATTR_IGMP_ENABLE_VLAN, sizeof(struct attr_igmp_vlan), igmp_vlan_encode, igmp_vlan_decode),
430         ATTR_HANDLER_ENTRY(ATTR_IGMP_BLOCK_UNK, 1, bool_endecode, bool_endecode),
431         ATTR_HANDLER_ENTRY(ATTR_IGMP_VALID_V3, 1, bool_endecode, bool_endecode)
432 };
433
434
435
436 const struct attr_handler* getAttrHandler (unsigned short attrcode)
437 {
438         const struct attr_handler *ah;
439         const unsigned int tab_size = sizeof(attrtab) / sizeof(struct attr_handler);
440         unsigned int inf, sup, index;
441         
442         
443         inf = 0;
444         sup = tab_size;
445         index = tab_size >> 1;
446         while (index < sup) {
447                 ah = &attrtab[index];
448                 
449                 if (ah->attr > attrcode) {
450                         sup = index;
451                         index -= ((index - inf) >> 1) + ((index - inf) & 1);
452                 } else if (ah->attr < attrcode) {
453                         inf = index;
454                         index += ((sup - index) >> 1) + ((sup - index) & 1);
455                 } else {
456                         return ah;
457                 }
458         }
459         
460         
461         return NULL;
462 }
463
464