7 // ----------------------------
8 int trim (char *txt, int start) {
17 //for (p=txt; *p!=0; p++);
19 for (p--; p>=txt && ( (c=*p)==' ' || c=='\n' ); *p--=0);
28 // -------------------
29 int min (int a, int b) {
35 // -----------------------------------------------------------------------------------------------------------------------------------------------
36 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
39 memset(nh, 0, sizeof(struct ng_header));
43 memcpy(nh->client_mac, client_mac, ETH_ALEN);
45 if ( switch_mac!=NULL ) {
46 memcpy(nh->switch_mac, switch_mac, ETH_ALEN);
49 nh->seqnum=htonl(seqnum);
50 strcpy(nh->proto_id, "NSDP");
57 // ---------------------------------------------------------------------------------------------------------------------------------------------------------
58 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
61 if ( nh->version!=1 ) {
65 if ( code>0 && nh->code!=code ) {
73 if ( *(unsigned short*)nh->unk2!=0 ) {
77 if ( client_mac!=NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN)!=0 ) {
81 if ( switch_mac!=NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN)!=0 ) {
85 if ( seqnum>0 && ntohl(nh->seqnum)!=seqnum ) {
89 if ( memcmp(nh->proto_id, "NSDP", 4)!=0 ) {
93 if ( *(unsigned int*)nh->unk3!=0 ) {
104 // -------------------------------------
105 void initNgPacket (struct ng_packet *np) {
107 np->ah=(struct attr_header*)np->nh->data;
113 // --------------------------------------------------------------------------------------------
114 void addPacketAttr (struct ng_packet *np, unsigned short attr, unsigned short size, void* data) {
116 struct attr_header *ah=np->ah;
119 if ( (int)(getPacketTotalSize(np)+sizeof(struct attr_header)+size)>(np->maxlen) ) {
123 ah->attr=htons(attr);
124 ah->size=htons(size);
126 if ( size>0 && data!=NULL ) {
127 memcpy(ah->data, data, size);
130 np->ah=(struct attr_header*)(ah->data+size);
136 // ----------------------------------------------------------------
137 void addPacketEmptyAttr (struct ng_packet *np, unsigned short attr) {
138 addPacketAttr(np, attr, 0, NULL);
143 // -------------------------------------------------------------------------
144 void addPacketByteAttr (struct ng_packet *np, unsigned short attr, char val) {
145 addPacketAttr(np, attr, 1, &val);
150 // ---------------------------------------------------------------------------
151 void addPacketShortAttr (struct ng_packet *np, unsigned short attr, short val) {
156 addPacketAttr(np, attr, 2, &s);
162 // ------------------------------------------------
163 int getPacketTotalSize (const struct ng_packet *np) {
164 return ((char*)np->ah)-np->buffer;
169 // --------------------------------------------
170 struct attr* newEmptyAttr (unsigned short attr) {
171 return newAttr(attr, 0, NULL);
176 // ------------------------------------------------------------------------
177 struct attr* newAttr (unsigned short attr, unsigned short size, void *data) {
182 at=malloc(sizeof(struct attr));
194 // ----------------------------------------------------------------
195 struct attr* newByteAttr (unsigned short attr, unsigned char value) {
197 char *v=malloc(sizeof(char));
201 return newAttr(attr, sizeof(char), v);
207 // ---------------------------------------------------------
208 struct attr* newShortAttr (unsigned short attr, short value) {
210 short *v=malloc(sizeof(short));
214 return newAttr(attr, sizeof(short), v);
220 // -----------------------------------------------------
221 struct attr* newIntAttr (unsigned short attr, int value) {
223 int *v=malloc(sizeof(int));
227 return newAttr(attr, sizeof(int), v);
233 // -----------------------------------------------------------------
234 struct attr* newAddrAttr (unsigned short attr, struct in_addr value) {
236 struct in_addr *v=malloc(sizeof(struct in_addr));
240 return newAttr(attr, sizeof(struct in_addr), v);
246 // ----------------------------
247 void freeAttr (struct attr *at) {
258 // --------------------------------------------------------------------------------------------------------------
259 int extractPacketAttributes (struct ng_packet *np, unsigned char *error, unsigned short *attr_error, List *attr) {
265 if ( error!=NULL ) *error=np->nh->error;
266 if ( attr_error!=NULL ) *attr_error=ntohs(np->nh->attr);
268 while ( getPacketTotalSize(np)<np->maxlen ) {
270 // no room for an attribute header: error
271 if ( getPacketTotalSize(np)+(int)sizeof(struct attr_header)>np->maxlen ) {
276 at=malloc(sizeof(struct attr));
277 at->attr=ntohs(np->ah->attr);
278 at->size=ntohs(np->ah->size);
280 // attribute data bigger than the remaining size: error
281 if ( getPacketTotalSize(np)+(int)sizeof(struct attr_header)+at->size>np->maxlen ) {
290 at->data=malloc(at->size*sizeof(char));
291 memcpy(at->data, np->ah->data, at->size);
294 pushBackList(attr, at);
296 // stop on an END attribute
297 if ( at->attr==ATTR_END ) break;
299 // move to next attribute
300 np->ah=(struct attr_header*)(np->ah->data+at->size);
311 // --------------------------------------------------------------
312 void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
315 const struct attr *at;
319 memset(sa, 0, sizeof(struct swi_attr));
321 for (ln=l->first; ln!=NULL; ln=ln->next) {
324 switch ( at->attr ) {
327 len=min(at->size, PRODUCT_SIZE);
328 memcpy(sa->product, at->data, len);
329 trim(sa->product, len);
333 len=min(at->size, NAME_SIZE);
334 memcpy(sa->name, at->data, len);
339 memcpy(&sa->mac, at->data, ETH_ALEN);
343 sa->nc.ip=*(struct in_addr*)at->data;
347 sa->nc.netmask=*(struct in_addr*)at->data;
351 sa->nc.gw=*(struct in_addr*)at->data;
355 sa->nc.dhcp=( ntohs(*(unsigned short*)at->data)==1 );
359 len=min(at->size, FIRMWARE_SIZE-1);
360 memcpy(sa->firmware, at->data, len);
364 case ATTR_PORTS_COUNT:
365 sa->ports=*(unsigned char*)at->data;