5 #include <ngadmin.h> /* FIXME */
7 #include <nsdp/protocol.h>
10 struct attr* newAttr (unsigned short attr, unsigned short size, void *data)
14 at = malloc(sizeof(struct attr));
26 void freeAttr (struct attr *at)
35 void filterAttributes (List *attr, ...)
40 unsigned short attrcode;
44 for (ln = attr->first; ln != NULL; ) {
50 while (!keep && attrcode != ATTR_END) {
51 attrcode = (unsigned short)va_arg(ap, unsigned int);
52 keep = keep || (at->attr == attrcode);
61 destroyElement(attr, pr, (void(*)(void*))freeAttr);
67 static int ports_status_endecode (struct attr *at)
69 struct attr_port_status *ps = at->data;
71 if (at->size != sizeof(struct attr_port_status))
91 static int port_stat_endecode (struct attr *at, bool encode)
93 struct attr_port_stat *ps = at->data;
94 unsigned long long *v;
96 if (at->size != sizeof(struct attr_port_stat))
102 for (v = &ps->recv; ((void*)v) - ((void*)ps) < (int)sizeof(struct attr_port_stat); v++)
103 *v = encode ? htobe64(*v) : be64toh(*v);
109 static int qos_endecode (struct attr *at)
111 struct attr_qos *aq = at->data;
113 if (at->size != sizeof(struct attr_qos))
119 if (aq->prio < PRIO_HIGH || aq->prio > PRIO_LOW)
126 static int bitrate_endecode (struct attr *at, bool encode)
128 struct attr_bitrate *sb = at->data;
130 if (at->size != sizeof(struct attr_bitrate))
137 sb->bitrate = ntohl(sb->bitrate);
139 if (sb->bitrate < BITRATE_UNSPEC || sb->bitrate > BITRATE_512M)
143 sb->bitrate = htonl(sb->bitrate);
149 static int pvid_endecode (struct attr *at, bool encode)
151 struct attr_pvid *ap = at->data;
153 if (at->size != sizeof(struct attr_pvid))
160 ap->vlan = ntohs(ap->vlan);
162 if (ap->vlan < VLAN_MIN || ap->vlan > VLAN_DOT_MAX)
166 ap->vlan = htons(ap->vlan);
172 static int cabletest_do_endecode (struct attr *at)
174 struct attr_cabletest_do *acd = at->data;
176 if (at->size != sizeof(struct attr_cabletest_do))
179 if (acd->port < 1 || acd->action != 1)
186 static int cabletest_result_endecode (struct attr *at, bool encode)
188 struct attr_cabletest_result *acr = at->data;
190 /* Note: this attribute is special
191 * - when sent by the client, it contains the port number whe want to
192 * get cabletest results from
193 * - when the switch replies, it contains the actual test results data
196 /* no need to check the size, the only possible value under 1
197 * is 0 and in that case the decoder is not called
205 if (at->size != sizeof(struct attr_cabletest_result))
209 acr->v1 = htonl(acr->v1);
210 acr->v2 = htonl(acr->v2);
212 acr->v1 = ntohl(acr->v1);
213 acr->v2 = ntohl(acr->v2);
220 static int igmp_vlan_endecode (struct attr *at, bool encode)
222 struct attr_igmp_vlan *aiv = at->data;
224 if (at->size != sizeof(struct attr_igmp_vlan))
228 aiv->enable = ntohs(aiv->enable);
229 aiv->vlan = ntohs(aiv->vlan);
232 aiv->enable = (aiv->enable != 0);
233 if (aiv->vlan < VLAN_MIN || aiv->vlan > VLAN_DOT_MAX)
237 aiv->enable = htons(aiv->enable);
238 aiv->vlan = htons(aiv->vlan);
245 static int mirror_encode (struct attr *at)
247 struct attr_mirror *am = at->data;
248 unsigned char p, ports, *r;
252 /* no need to check attribute size, since
253 * sizeof(struct attr_mirror) == 1 and encoder is not
254 * called when attribute size is zero (ie empty attribute)
256 * am->outport encodes the outgoing mirror port and the array
257 * the ports from which the data is copied, it must not be empty
259 * am->outport == 0 is allowed and means mirroring is disabled
260 * but in that case the ports array must be empty
262 ports = at->size - sizeof(struct attr_mirror);
263 if ((am->outport == 0) ^ (ports == 0))
266 sz = 3 + ((ports - 1) >> 3);
274 /* FIXME: if ports > 8 */
275 for (p = 1; p <= ports; p++) {
276 if (am->outport != p)
277 r[2] |= (am->ports[p] & 1) << (8 - p);
289 static int mirror_decode (struct attr *at)
291 struct attr_mirror *am;
292 unsigned char p, ports, *r = at->data;
299 /* note: we cannot compute the exact amount of ports from here,
300 * instead we have the immediate superior multiple of 8
301 * it will be the user's job to ignore extra entries
303 ports = ((at->size - 2) << 3);
305 /* r[0] == 0 is allowed and means mirroring is disabled */
308 sz = sizeof(struct attr_mirror) + ports;
317 /* FIXME: if ports > 8 */
318 for (p = 1; p <= ports; p++)
319 am->ports[p] = (r[2] >> (8 - p)) & 1;
330 static int vlan_port_encode (struct attr *at)
332 struct attr_vlan_conf *avc = at->data;
333 unsigned char p, ports, *r;
337 /* just a header is valid */
338 if (at->size < sizeof(struct attr_vlan_conf))
340 ports = at->size - sizeof(struct attr_vlan_conf);
342 if (avc->vlan < VLAN_MIN || avc->vlan > VLAN_PORT_MAX)
348 sz = (2 + 1 + ((ports - 1) >> 3));
355 *(unsigned short*)r = htons(avc->vlan);
357 /* FIXME: if ports > 8 */
358 for (p = 0; p < ports; p++) {
359 if (avc->ports[p] == VLAN_UNTAGGED)
360 r[2] |= (1 << (7 - p));
372 static int vlan_port_decode (struct attr *at)
374 unsigned char p, ports, *r = at->data;
375 struct attr_vlan_conf *avc;
382 /* note: we cannot compute the exact amount of ports from here,
383 * instead we have the immediate superior multiple of 8
384 * it will be the user's job to ignore extra entries
386 ports = ((at->size - 2) << 3);
388 sz = sizeof(struct attr_vlan_conf) + ports;
393 avc->vlan = ntohs(*(unsigned short*)r);
395 /* FIXME: if ports > 8 */
396 for (p = 0; p < ports; p++) {
397 if ((r[2] >> (7 - p)) & 1)
398 avc->ports[p] = VLAN_UNTAGGED;
400 avc->ports[p] = VLAN_NO;
412 static int vlan_dot_encode (struct attr *at)
414 struct attr_vlan_conf *avc = at->data;
415 unsigned char p, ports, *r, fl;
419 /* just a header is valid */
420 if (at->size < sizeof(struct attr_vlan_conf))
422 ports = at->size - sizeof(struct attr_vlan_conf);
424 if (avc->vlan < VLAN_MIN || avc->vlan > VLAN_DOT_MAX)
430 sz = 2 + 2 * (1 + ((ports - 1) >> 3));
437 *(unsigned short*)r = htons(avc->vlan);
439 /* FIXME: if ports > 8 */
440 for (p = 0; p < ports; p++) {
442 switch (avc->ports[p]) {
445 /* a tagged VLAN is also marked as untagged
446 * so do not put a "break" here
463 static int vlan_dot_decode (struct attr *at)
465 unsigned char p, ports, *r = at->data;
466 struct attr_vlan_conf *avc;
470 /* attribute size must be a multiple of 2 because there are
471 * 2 bytes (1 for tagged and 1 for untagged) per block of
472 * 8 ports, plus the 2 first bytes for the VLAN id
474 if (at->size < 2 || (at->size & 1) != 0)
477 /* note: we cannot compute the exact amount of ports from here,
478 * instead we have the immediate superior multiple of 8
479 * it will be the user's job to ignore extra entries
481 ports = ((at->size - 2) / 2) << 3;
483 sz = sizeof(struct attr_vlan_conf) + ports;
488 avc->vlan = ntohs(*(unsigned short*)r);
490 /* FIXME: if ports > 8 */
491 for (p = 0; p < ports; p++) {
492 if ((r[3] >> (7 - p)) & 1)
493 avc->ports[p] = VLAN_TAGGED;
494 else if ((r[2] >> (7 - p)) & 1)
495 avc->ports[p] = VLAN_UNTAGGED;
497 avc->ports[p] = VLAN_NO;
509 static int processAttr (struct attr *at, bool encode)
511 unsigned char *byte = at->data;
512 unsigned short *word = at->data;
513 unsigned int *dword = at->data;
516 /* empty attributes are not processed */
520 if (at->data == NULL)
526 if (at->size != ETH_ALEN)
533 /* IP addresses are kept in network byte order even on the host */
534 if (at->size != sizeof(struct in_addr))
539 case ATTR_PORTS_COUNT:
547 case ATTR_STATS_RESET:
548 case ATTR_STORM_ENABLE:
549 case ATTR_IGMP_BLOCK_UNK:
550 case ATTR_IGMP_VALID_V3:
554 *byte = (*byte != 0);
563 *word = ntohs(*word);
565 *word = (*word != 0);
568 *word = htons(*word);
577 *dword = ntohl(*dword);
579 *dword = (*dword != 0);
582 *dword = htonl(*dword);
590 /* no need to check if *byte < VLAN_DISABLED because
591 * byte is unsigned and VLAN_DISABLED is 0 */
592 if (*byte > VLAN_DOT_ADV)
601 if (*byte < QOS_PORT || *byte > QOS_DOT)
606 case ATTR_QOS_CONFIG:
607 return qos_endecode(at);
609 case ATTR_VLAN_DESTROY:
614 *word = ntohs(*word);
616 if (*word < VLAN_MIN || *word > VLAN_DOT_MAX)
620 *word = htons(*word);
624 case ATTR_PORT_STATUS:
625 return ports_status_endecode(at);
627 case ATTR_PORT_STATISTICS:
628 return port_stat_endecode(at, encode);
630 case ATTR_BITRATE_INPUT:
631 case ATTR_BITRATE_OUTPUT:
632 case ATTR_STORM_BITRATE:
633 return bitrate_endecode(at, encode);
636 return pvid_endecode(at, encode);
638 case ATTR_CABLETEST_DO:
639 return cabletest_do_endecode(at);
641 case ATTR_CABLETEST_RESULT:
642 return cabletest_result_endecode(at, encode);
644 case ATTR_IGMP_ENABLE_VLAN:
645 return igmp_vlan_endecode(at, encode);
649 return mirror_encode(at);
651 return mirror_decode(at);
653 case ATTR_VLAN_PORT_CONF:
655 return vlan_port_encode(at);
657 return vlan_port_decode(at);
659 case ATTR_VLAN_DOT_CONF:
661 return vlan_dot_encode(at);
663 return vlan_dot_decode(at);
665 /* undefined attributes are not modified */
672 int encodeAttr (struct attr *at)
674 return processAttr(at, true);
678 int decodeAttr (struct attr *at)
680 return processAttr(at, false);