13 int trim (char *txt, int start)
21 while (p >= txt && (*p == ' ' || *p == '\n')) {
30 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum)
32 memset(nh, 0, sizeof(struct ng_header));
36 memcpy(nh->client_mac, client_mac, ETH_ALEN);
38 if (switch_mac != NULL)
39 memcpy(nh->switch_mac, switch_mac, ETH_ALEN);
41 nh->seqnum = htonl(seqnum);
42 memcpy(nh->proto_id, "NSDP", 4);
46 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum)
51 if (code > 0 && nh->code != code)
57 if (*(unsigned short*)nh->unk2 != 0)
60 if (client_mac != NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN) != 0)
63 if (switch_mac != NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN) != 0)
66 if (seqnum > 0 && ntohl(nh->seqnum) != seqnum)
69 if (memcmp(nh->proto_id, "NSDP", 4) != 0)
72 if (*(unsigned int*)nh->unk3 != 0)
79 void addPacketAttr (struct ng_packet *np, struct attr *at)
81 struct attr_header *ah = np->ah;
84 if ((int)(getPacketTotalSize(np) + sizeof(struct attr_header) + at->size) > np->maxlen)
87 ah->attr = htons(at->attr);
88 ah->size = htons(at->size);
90 if (at->size > 0 && at->data != NULL)
91 memcpy(ah->data, at->data, at->size);
93 np->ah = (struct attr_header*)(ah->data + at->size);
97 struct attr* newAttr (unsigned short attr, unsigned short size, void *data)
102 at = malloc(sizeof(struct attr));
115 void freeAttr (struct attr *at)
124 int addPacketAttributes (struct ng_packet *np, const List* attr, unsigned char ports)
128 const struct attr_handler *ah;
134 for (ln = attr->first; ln != NULL; ln = ln->next) {
136 ah = getAttrHandler(at->attr);
138 if (ah->size > 0 && at->size > 0 && at->size != ah->size)
140 if (at->size > 0 && ah->encode != NULL && !ah->encode(at, ports))
144 addPacketAttr(np, at);
152 int extractPacketAttributes (struct ng_packet *np, List *attr, unsigned char ports)
155 const struct attr_handler *ah;
161 while (getPacketTotalSize(np) < np->maxlen) {
163 /* no room for an attribute header: error */
164 if (getPacketTotalSize(np) + (int)sizeof(struct attr_header) > np->maxlen) {
169 /* create new attribute */
170 size = ntohs(np->ah->size);
171 at = newAttr(ntohs(np->ah->attr), size, NULL);
177 /* attribute data bigger than the remaining size: error */
178 if (getPacketTotalSize(np) + (int)sizeof(struct attr_header) + size > np->maxlen) {
184 /* copy attribute raw data */
188 at->data = malloc(size * sizeof(char));
189 memcpy(at->data, np->ah->data, size);
192 /* decode attribute data */
195 ah = getAttrHandler(at->attr);
196 if (at->data == NULL || ah == NULL)
199 if (ah->size > 0 && size != ah->size) {
204 if (ah->decode != NULL)
205 valid = ah->decode(at, ports);
208 /* stop on an END attribute */
209 if (at->attr == ATTR_END) {
214 /* move to next attribute */
215 np->ah = (struct attr_header*)(np->ah->data + size);
218 pushBackList(attr, at);
228 void filterAttributes (List *attr, ...)
233 unsigned short attrcode;
244 while (!keep && attrcode != ATTR_END) {
245 attrcode = (unsigned short)va_arg(ap, unsigned int);
246 keep = keep || (at->attr == attrcode);
255 destroyElement(attr, pr, (void(*)(void*))freeAttr);