9 #include <netinet/ether.h>
14 #define CLIENT_PORT 63321
15 #define SWITCH_PORT 63322
17 #define CODE_READ_REQ 1
18 #define CODE_READ_REP 2
19 #define CODE_WRITE_REQ 3
20 #define CODE_WRITE_REP 4
22 #define ERROR_READONLY 3
23 #define ERROR_INVALID_VALUE 5
24 #define ERROR_DENIED 7
28 char version; /* always 1, maybe version */
29 char code; /* request code: read request, read reply, write request, write reply */
30 unsigned char error; /* error code, 0 when no error */
31 unsigned char unk1; /* always 0, unknown */
32 unsigned short attr; /* attribute code which caused error, 0 when no error */
33 char unk2[2]; /* always 0, unknown */
34 char client_mac[ETH_ALEN]; /* client MAC address */
35 char switch_mac[ETH_ALEN]; /* switch MAC address */
36 unsigned int seqnum; /* sequence number */
37 char proto_id[4]; /* always "NSDP", maybe short for "Netgear Switch Description Protocol" */
38 char unk3[4]; /* always 0, unknown */
40 } __attribute__((packed));
47 } __attribute__((packed));
56 struct attr_header *ah;
68 int trim (char *txt, int start);
71 static inline int min (int a, int b)
77 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum);
80 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum);
83 static inline void initNgPacket (struct ng_packet *np)
85 np->ah = (struct attr_header*)np->nh->data;
89 void addPacketAttr (struct ng_packet *np, struct attr *at);
92 static inline int getPacketTotalSize (const struct ng_packet *np)
94 return ((char*)np->ah) - np->buffer;
98 struct attr* newAttr (unsigned short attr, unsigned short size, void *data);
101 static inline struct attr* newEmptyAttr (unsigned short attr)
103 return newAttr(attr, 0, NULL);
107 static inline struct attr* newByteAttr (unsigned short attr, unsigned char value)
109 char *v = malloc(sizeof(char));
113 return newAttr(attr, sizeof(char), v);
117 static inline struct attr* newShortAttr (unsigned short attr, short value)
119 short *v = malloc(sizeof(short));
123 return newAttr(attr, sizeof(short), v);
127 static inline struct attr* newIntAttr (unsigned short attr, int value)
129 int *v = malloc(sizeof(int));
133 return newAttr(attr, sizeof(int), v);
137 static inline struct attr* newAddrAttr (unsigned short attr, struct in_addr value)
139 struct in_addr *v = malloc(sizeof(struct in_addr));
143 return newAttr(attr, sizeof(struct in_addr), v);
147 void freeAttr (struct attr *at);
150 int addPacketAttributes (struct ng_packet *np, const List* attr, unsigned char ports);
153 int extractPacketAttributes (struct ng_packet *np, List *attr, unsigned char ports);
156 void filterAttributes (List *attr, ...);