13 const char passwordKey[] = "NtgrSmartSwitchRock";
16 void passwordEndecode (char *buf, unsigned int len)
18 const char *k = passwordKey;
21 if (buf == NULL || len <= 0)
24 for (i = 0; i < len; i++) {
32 int trim (char *txt, int start)
40 while (p >= txt && (*p == ' ' || *p == '\n')) {
49 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum)
51 memset(nh, 0, sizeof(struct ng_header));
55 memcpy(nh->client_mac, client_mac, ETH_ALEN);
57 if (switch_mac != NULL)
58 memcpy(nh->switch_mac, switch_mac, ETH_ALEN);
60 nh->seqnum = htonl(seqnum);
61 memcpy(nh->proto_id, "NSDP", 4);
65 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum)
70 if (code > 0 && nh->code != code)
76 if (*(unsigned short*)nh->unk2 != 0)
79 if (client_mac != NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN) != 0)
82 if (switch_mac != NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN) != 0)
85 if (seqnum > 0 && ntohl(nh->seqnum) != seqnum)
88 if (memcmp(nh->proto_id, "NSDP", 4) != 0)
91 if (*(unsigned int*)nh->unk3 != 0)
98 void addPacketAttr (struct ng_packet *np, struct attr *at)
100 struct attr_header *ah = np->ah;
103 if ((int)(getPacketTotalSize(np) + sizeof(struct attr_header) + at->size) > np->maxlen)
106 ah->attr = htons(at->attr);
107 ah->size = htons(at->size);
109 if (at->size > 0 && at->data != NULL)
110 memcpy(ah->data, at->data, at->size);
112 np->ah = (struct attr_header*)(ah->data + at->size);
116 struct attr* newAttr (unsigned short attr, unsigned short size, void *data)
121 at = malloc(sizeof(struct attr));
134 void freeAttr (struct attr *at)
143 int addPacketAttributes (struct ng_packet *np, const List* attr, unsigned char ports)
147 const struct attr_handler *ah;
153 for (ln = attr->first; ln != NULL; ln = ln->next) {
155 ah = getAttrHandler(at->attr);
157 if (ah->size > 0 && at->size > 0 && at->size != ah->size)
159 if (at->size > 0 && ah->encode != NULL && !ah->encode(at, ports))
163 addPacketAttr(np, at);
171 int extractPacketAttributes (struct ng_packet *np, List *attr, unsigned char ports)
174 const struct attr_handler *ah;
180 while (getPacketTotalSize(np) < np->maxlen) {
182 /* no room for an attribute header: error */
183 if (getPacketTotalSize(np) + (int)sizeof(struct attr_header) > np->maxlen) {
188 /* create new attribute */
189 size = ntohs(np->ah->size);
190 at = newAttr(ntohs(np->ah->attr), size, NULL);
196 /* attribute data bigger than the remaining size: error */
197 if (getPacketTotalSize(np) + (int)sizeof(struct attr_header) + size > np->maxlen) {
203 /* copy attribute raw data */
207 at->data = malloc(size * sizeof(char));
208 memcpy(at->data, np->ah->data, size);
211 /* decode attribute data */
214 ah = getAttrHandler(at->attr);
215 if (at->data == NULL || ah == NULL)
218 if (ah->size > 0 && size != ah->size) {
223 if (ah->decode != NULL)
224 valid = ah->decode(at, ports);
227 /* stop on an END attribute */
228 if (at->attr == ATTR_END) {
233 /* move to next attribute */
234 np->ah = (struct attr_header*)(np->ah->data + size);
237 pushBackList(attr, at);
247 void filterAttributes (List *attr, ...)
252 unsigned short attrcode;
263 while (!keep && attrcode != ATTR_END) {
264 attrcode = (unsigned short)va_arg(ap, unsigned int);
265 keep = keep || (at->attr == attrcode);
274 destroyElement(attr, pr, (void(*)(void*))freeAttr);