7 #include "encoding_attr.h"
11 void initNsdpHeader (struct nsdp_header *nh, const struct nsdp_cmd *nc)
13 memset(nh, 0, sizeof(struct nsdp_header));
14 nh->version = NSDP_VERSION;
16 nh->error = nc->error;
17 nh->attr = htons(nc->attr_error);
19 memcpy(nh->client_mac, &nc->client_mac, ETH_ALEN);
20 memcpy(nh->switch_mac, &nc->switch_mac, ETH_ALEN);
22 nh->seqnum = htonl(nc->seqnum);
23 memcpy(nh->proto_id, NSDP_PROTOID, 4);
27 bool validateNsdpHeader (const struct nsdp_header *nh, const struct nsdp_cmd *nc)
32 if (nh->version != NSDP_VERSION)
35 if (nc->code > 0 && nh->code != nc->code)
41 if (*(unsigned short*)nh->unk2 != 0)
44 for (i = 0; i < ETH_ALEN && nc->client_mac.ether_addr_octet[i] == 0; i++);
45 if (i < ETH_ALEN && memcmp(nh->client_mac, &nc->client_mac, ETH_ALEN) != 0)
48 for (i = 0; i < ETH_ALEN && nc->switch_mac.ether_addr_octet[i] == 0; i++);
49 if (i < ETH_ALEN && memcmp(nh->switch_mac, &nc->switch_mac, ETH_ALEN) != 0)
52 if (nc->seqnum > 0 && ntohl(nh->seqnum) != nc->seqnum)
55 if (memcmp(nh->proto_id, NSDP_PROTOID, 4) != 0)
58 if (*(unsigned int*)nh->unk3 != 0)
65 static void addPacketAttr (struct nsdp_packet *np, struct attr *at)
67 struct attr_header *ah = np->ah;
70 if ((int)(getPacketTotalSize(np) + sizeof(struct attr_header) + at->size) > np->maxlen)
73 ah->attr = htons(at->attr);
74 ah->size = htons(at->size);
76 if (at->size > 0 && at->data != NULL)
77 memcpy(ah->data, at->data, at->size);
79 np->ah = (struct attr_header*)(ah->data + at->size);
83 int addPacketAttributes (struct nsdp_packet *np, const List* attr, unsigned char ports)
87 const struct attr_handler *ah;
93 for (ln = attr->first; ln != NULL; ln = ln->next) {
95 ah = getAttrHandler(at->attr);
97 if (ah->size > 0 && at->size > 0 && at->size != ah->size)
99 if (at->size > 0 && ah->encode != NULL && !ah->encode(at, ports))
103 addPacketAttr(np, at);
111 int extractPacketAttributes (struct nsdp_packet *np, List *attr, unsigned char ports)
114 const struct attr_handler *ah;
120 while (getPacketTotalSize(np) < np->maxlen) {
121 /* no room for an attribute header: error */
122 if (getPacketTotalSize(np) + (int)sizeof(struct attr_header) > np->maxlen) {
127 /* create new attribute */
128 size = ntohs(np->ah->size);
129 at = newAttr(ntohs(np->ah->attr), size, NULL);
135 /* attribute data bigger than the remaining size: error */
136 if (getPacketTotalSize(np) + (int)sizeof(struct attr_header) + size > np->maxlen) {
142 /* copy attribute raw data */
146 at->data = malloc(size * sizeof(unsigned char));
147 memcpy(at->data, np->ah->data, size);
150 /* decode attribute data */
153 ah = getAttrHandler(at->attr);
154 if (at->data == NULL || ah == NULL)
157 if (ah->size > 0 && size != ah->size) {
162 if (ah->decode != NULL)
163 valid = ah->decode(at, ports);
166 /* stop on an END attribute */
167 if (at->attr == ATTR_END) {
172 /* move to next attribute */
173 np->ah = (struct attr_header*)(np->ah->data + size);
176 pushBackList(attr, at);