8 const struct ether_addr nullMac={.ether_addr_octet={0, 0, 0, 0, 0, 0}};
12 // -------------------
13 int min (int a, int b) {
19 // -----------------------------------------------------------------------------------------------------------------------------------------------
20 void initNgHeader (struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
23 memset(nh, 0, sizeof(struct ng_header));
27 memcpy(nh->client_mac, client_mac, ETH_ALEN);
29 if ( switch_mac!=NULL ) {
30 memcpy(nh->switch_mac, switch_mac, ETH_ALEN);
33 nh->seqnum=htonl(seqnum);
34 strcpy(nh->proto_id, "NSDP");
41 // ---------------------------------------------------------------------------------------------------------------------------------------------------------
42 bool validateNgHeader (const struct ng_header *nh, char code, const struct ether_addr *client_mac, const struct ether_addr *switch_mac, unsigned int seqnum) {
49 if ( code>0 && nh->code!=code ) {
57 if ( *(unsigned short*)nh->unk3!=0 ) {
61 if ( client_mac!=NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN)!=0 ) {
65 if ( switch_mac!=NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN)!=0 ) {
69 if ( seqnum>0 && ntohl(nh->seqnum)!=seqnum ) {
73 if ( *(unsigned int*)nh->unk4!=0 ) {
84 // -------------------------------------
85 void initNgPacket (struct ng_packet *np) {
87 np->ah=(struct attr_header*)np->nh->data;
93 // --------------------------------------------------------------------------------------------
94 void addPacketAttr (struct ng_packet *np, unsigned short attr, unsigned short size, void* data) {
96 struct attr_header *ah=np->ah;
99 if ( (int)(getPacketTotalSize(np)+sizeof(struct attr_header)+size)>(np->maxlen) ) {
103 ah->attr=htons(attr);
104 ah->size=htons(size);
106 if ( size>0 && data!=NULL ) {
107 memcpy(ah->data, data, size);
110 np->ah=(struct attr_header*)(ah->data+size);
116 // ----------------------------------------------------------------
117 void addPacketEmptyAttr (struct ng_packet *np, unsigned short attr) {
118 addPacketAttr(np, attr, 0, NULL);
123 // -------------------------------------------------------------------------
124 void addPacketByteAttr (struct ng_packet *np, unsigned short attr, char val) {
125 addPacketAttr(np, attr, 1, &val);
130 // ---------------------------------------------------------------------------
131 void addPacketShortAttr (struct ng_packet *np, unsigned short attr, short val) {
136 addPacketAttr(np, attr, 2, &s);
142 // ------------------------------------------------
143 int getPacketTotalSize (const struct ng_packet *np) {
144 return ((char*)np->ah)-np->buffer;
149 // --------------------------------------------
150 struct attr* newEmptyAttr (unsigned short attr) {
151 return newAttr(attr, 0, NULL);
156 // ------------------------------------------------------------------------
157 struct attr* newAttr (unsigned short attr, unsigned short size, void *data) {
162 at=malloc(sizeof(struct attr));
174 // ----------------------------------------------------------------
175 struct attr* newByteAttr (unsigned short attr, unsigned char value) {
177 char *v=malloc(sizeof(char));
181 return newAttr(attr, 1, v);
187 // ----------------------------
188 void freeAttr (struct attr *at) {
199 // ------------------------------------------------------------------------------------------
200 List* extractPacketAttributes (struct ng_packet *np, char *error, unsigned short *attr_error) {
207 *error=np->nh->error;
210 if ( attr_error!=NULL ) {
211 *attr_error=ntohs(np->nh->attr);
216 while ( getPacketTotalSize(np)<np->maxlen ) {
218 at=malloc(sizeof(struct attr));
219 at->attr=ntohs(np->ah->attr);
220 at->size=ntohs(np->ah->size);
222 if ( getPacketTotalSize(np)+at->size>np->maxlen ) {
230 at->data=malloc(at->size*sizeof(char));
231 memcpy(at->data, np->ah->data, at->size);
236 if ( at->attr==ATTR_END ) {
240 np->ah=(struct attr_header*)(np->ah->data+at->size);
251 // --------------------------------------------------------------
252 void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
255 const struct attr *at;
259 memset(sa, 0, sizeof(struct swi_attr));
261 // FIXME: mutex lock ?
263 for (ln=l->first; ln!=NULL; ln=ln->next) {
266 switch ( at->attr ) {
269 len=min(at->size, PRODUCT_SIZE);
270 memcpy(sa->product, at->data, len);
271 //trim(sa->product, len); // FIXME
275 len=min(at->size, NAME_SIZE);
276 memcpy(sa->name, at->data, len);
277 //trim(sa->name, len); // FIXME
281 memcpy(&sa->mac, at->data, ETH_ALEN);
285 sa->nc.ip=*(struct in_addr*)at->data;
289 sa->nc.netmask=*(struct in_addr*)at->data;
293 sa->nc.gw=*(struct in_addr*)at->data;
297 sa->nc.dhcp=( ntohs(*(unsigned short*)at->data)==1 );
301 len=min(at->size, FIRMWARE_SIZE-1);
302 memcpy(sa->firmware, at->data, len);
306 case ATTR_PORTS_COUNT:
307 sa->ports=*(unsigned char*)at->data;