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, sizeof(char), v);
187 // -----------------------------------------------------
188 struct attr* newIntAttr (unsigned short attr, int value) {
190 int *v=malloc(sizeof(int));
194 return newAttr(attr, sizeof(int), v);
200 // ----------------------------
201 void freeAttr (struct attr *at) {
212 // -----------------------------------------------------------------------------------------------------
213 void extractPacketAttributes (struct ng_packet *np, char *error, unsigned short *attr_error, List *attr) {
219 *error=np->nh->error;
222 if ( attr_error!=NULL ) {
223 *attr_error=ntohs(np->nh->attr);
226 while ( getPacketTotalSize(np)<np->maxlen ) {
228 at=malloc(sizeof(struct attr));
229 at->attr=ntohs(np->ah->attr);
230 at->size=ntohs(np->ah->size);
232 if ( getPacketTotalSize(np)+at->size>np->maxlen ) {
240 at->data=malloc(at->size*sizeof(char));
241 memcpy(at->data, np->ah->data, at->size);
244 pushBackList(attr, at);
246 if ( at->attr==ATTR_END ) {
250 np->ah=(struct attr_header*)(np->ah->data+at->size);
259 // --------------------------------------------------------------
260 void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
263 const struct attr *at;
267 memset(sa, 0, sizeof(struct swi_attr));
269 // FIXME: mutex lock ?
271 for (ln=l->first; ln!=NULL; ln=ln->next) {
274 switch ( at->attr ) {
277 len=min(at->size, PRODUCT_SIZE);
278 memcpy(sa->product, at->data, len);
279 //trim(sa->product, len); // FIXME
283 len=min(at->size, NAME_SIZE);
284 memcpy(sa->name, at->data, len);
285 //trim(sa->name, len); // FIXME
289 memcpy(&sa->mac, at->data, ETH_ALEN);
293 sa->nc.ip=*(struct in_addr*)at->data;
297 sa->nc.netmask=*(struct in_addr*)at->data;
301 sa->nc.gw=*(struct in_addr*)at->data;
305 sa->nc.dhcp=( ntohs(*(unsigned short*)at->data)==1 );
309 len=min(at->size, FIRMWARE_SIZE-1);
310 memcpy(sa->firmware, at->data, len);
314 case ATTR_PORTS_COUNT:
315 sa->ports=*(unsigned char*)at->data;