8 const struct ether_addr nullMac={.ether_addr_octet={0, 0, 0, 0, 0, 0}};
13 // ----------------------------
14 int trim (char *txt, int start) {
23 //for (p=txt; *p!=0; p++);
25 for (p--; p>=txt && ( (c=*p)==' ' || c=='\n' ); *p--=0);
34 // -------------------
35 int min (int a, int b) {
41 // -----------------------------------------------------------------------------------------------------------------------------------------------
42 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
45 memset(nh, 0, sizeof(struct ng_header));
49 memcpy(nh->client_mac, client_mac, ETH_ALEN);
51 if ( switch_mac!=NULL ) {
52 memcpy(nh->switch_mac, switch_mac, ETH_ALEN);
55 nh->seqnum=htonl(seqnum);
56 strcpy(nh->proto_id, "NSDP");
63 // ---------------------------------------------------------------------------------------------------------------------------------------------------------
64 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
71 if ( code>0 && nh->code!=code ) {
75 if ( *(unsigned short*)nh->unk2!=0 ) {
79 if ( client_mac!=NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN)!=0 ) {
83 if ( switch_mac!=NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN)!=0 ) {
87 if ( seqnum>0 && ntohl(nh->seqnum)!=seqnum ) {
91 if ( memcmp(nh->proto_id, "NSDP", 4)!=0 ) {
95 if ( *(unsigned int*)nh->unk3!=0 ) {
106 // -------------------------------------
107 void initNgPacket (struct ng_packet *np) {
109 np->ah=(struct attr_header*)np->nh->data;
115 // --------------------------------------------------------------------------------------------
116 void addPacketAttr (struct ng_packet *np, unsigned short attr, unsigned short size, void* data) {
118 struct attr_header *ah=np->ah;
121 if ( (int)(getPacketTotalSize(np)+sizeof(struct attr_header)+size)>(np->maxlen) ) {
125 ah->attr=htons(attr);
126 ah->size=htons(size);
128 if ( size>0 && data!=NULL ) {
129 memcpy(ah->data, data, size);
132 np->ah=(struct attr_header*)(ah->data+size);
138 // ----------------------------------------------------------------
139 void addPacketEmptyAttr (struct ng_packet *np, unsigned short attr) {
140 addPacketAttr(np, attr, 0, NULL);
145 // -------------------------------------------------------------------------
146 void addPacketByteAttr (struct ng_packet *np, unsigned short attr, char val) {
147 addPacketAttr(np, attr, 1, &val);
152 // ---------------------------------------------------------------------------
153 void addPacketShortAttr (struct ng_packet *np, unsigned short attr, short val) {
158 addPacketAttr(np, attr, 2, &s);
164 // ------------------------------------------------
165 int getPacketTotalSize (const struct ng_packet *np) {
166 return ((char*)np->ah)-np->buffer;
171 // --------------------------------------------
172 struct attr* newEmptyAttr (unsigned short attr) {
173 return newAttr(attr, 0, NULL);
178 // ------------------------------------------------------------------------
179 struct attr* newAttr (unsigned short attr, unsigned short size, void *data) {
184 at=malloc(sizeof(struct attr));
196 // ----------------------------------------------------------------
197 struct attr* newByteAttr (unsigned short attr, unsigned char value) {
199 char *v=malloc(sizeof(char));
203 return newAttr(attr, sizeof(char), v);
209 // ---------------------------------------------------------
210 struct attr* newShortAttr (unsigned short attr, short value) {
212 short *v=malloc(sizeof(short));
216 return newAttr(attr, sizeof(short), v);
222 // -----------------------------------------------------
223 struct attr* newIntAttr (unsigned short attr, int value) {
225 int *v=malloc(sizeof(int));
229 return newAttr(attr, sizeof(int), v);
235 // -----------------------------------------------------------------
236 struct attr* newAddrAttr (unsigned short attr, struct in_addr value) {
238 struct in_addr *v=malloc(sizeof(struct in_addr));
242 return newAttr(attr, sizeof(struct in_addr), v);
248 // ----------------------------
249 void freeAttr (struct attr *at) {
260 // ---------------------------------------------------------------------------------------------------------------
261 void extractPacketAttributes (struct ng_packet *np, unsigned short *error, unsigned short *attr_error, List *attr) {
266 if ( error!=NULL ) *error=ntohs(np->nh->error);
267 if ( attr_error!=NULL ) *attr_error=ntohs(np->nh->attr);
269 while ( getPacketTotalSize(np)<np->maxlen ) {
271 at=malloc(sizeof(struct attr));
272 at->attr=ntohs(np->ah->attr);
273 at->size=ntohs(np->ah->size);
275 if ( getPacketTotalSize(np)+at->size>np->maxlen ) {
283 at->data=malloc(at->size*sizeof(char));
284 memcpy(at->data, np->ah->data, at->size);
287 pushBackList(attr, at);
289 if ( at->attr==ATTR_END ) {
293 np->ah=(struct attr_header*)(np->ah->data+at->size);
302 // --------------------------------------------------------------
303 void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
306 const struct attr *at;
310 memset(sa, 0, sizeof(struct swi_attr));
312 for (ln=l->first; ln!=NULL; ln=ln->next) {
315 switch ( at->attr ) {
318 len=min(at->size, PRODUCT_SIZE);
319 memcpy(sa->product, at->data, len);
320 trim(sa->product, len);
324 len=min(at->size, NAME_SIZE);
325 memcpy(sa->name, at->data, len);
330 memcpy(&sa->mac, at->data, ETH_ALEN);
334 sa->nc.ip=*(struct in_addr*)at->data;
338 sa->nc.netmask=*(struct in_addr*)at->data;
342 sa->nc.gw=*(struct in_addr*)at->data;
346 sa->nc.dhcp=( ntohs(*(unsigned short*)at->data)==1 );
350 len=min(at->size, FIRMWARE_SIZE-1);
351 memcpy(sa->firmware, at->data, len);
355 case ATTR_PORTS_COUNT:
356 sa->ports=*(unsigned char*)at->data;