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 ) {
53 if ( *(unsigned short*)nh->unk2!=0 ) {
57 if ( client_mac!=NULL && memcmp(nh->client_mac, client_mac, ETH_ALEN)!=0 ) {
61 if ( switch_mac!=NULL && memcmp(nh->switch_mac, switch_mac, ETH_ALEN)!=0 ) {
65 if ( seqnum>0 && ntohl(nh->seqnum)!=seqnum ) {
69 if ( memcmp(nh->proto_id, "NSDP", 4)!=0 ) {
73 if ( *(unsigned int*)nh->unk3!=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* newShortAttr (unsigned short attr, short value) {
190 short *v=malloc(sizeof(short));
194 return newAttr(attr, sizeof(short), v);
200 // -----------------------------------------------------
201 struct attr* newIntAttr (unsigned short attr, int value) {
203 int *v=malloc(sizeof(int));
207 return newAttr(attr, sizeof(int), v);
213 // -----------------------------------------------------------------
214 struct attr* newAddrAttr (unsigned short attr, struct in_addr value) {
216 struct in_addr *v=malloc(sizeof(struct in_addr));
220 return newAttr(attr, sizeof(struct in_addr), v);
226 // ----------------------------
227 void freeAttr (struct attr *at) {
238 // ---------------------------------------------------------------------------------------------------------------
239 void extractPacketAttributes (struct ng_packet *np, unsigned short *error, unsigned short *attr_error, List *attr) {
244 if ( error!=NULL ) *error=ntohs(np->nh->error);
245 if ( attr_error!=NULL ) *attr_error=ntohs(np->nh->attr);
247 while ( getPacketTotalSize(np)<np->maxlen ) {
249 at=malloc(sizeof(struct attr));
250 at->attr=ntohs(np->ah->attr);
251 at->size=ntohs(np->ah->size);
253 if ( getPacketTotalSize(np)+at->size>np->maxlen ) {
261 at->data=malloc(at->size*sizeof(char));
262 memcpy(at->data, np->ah->data, at->size);
265 pushBackList(attr, at);
267 if ( at->attr==ATTR_END ) {
271 np->ah=(struct attr_header*)(np->ah->data+at->size);
280 // --------------------------------------------------------------
281 void extractSwitchAttributes (struct swi_attr *sa, const List *l) {
284 const struct attr *at;
288 memset(sa, 0, sizeof(struct swi_attr));
290 // FIXME: mutex lock ?
292 for (ln=l->first; ln!=NULL; ln=ln->next) {
295 switch ( at->attr ) {
298 len=min(at->size, PRODUCT_SIZE);
299 memcpy(sa->product, at->data, len);
300 //trim(sa->product, len); // FIXME
304 len=min(at->size, NAME_SIZE);
305 memcpy(sa->name, at->data, len);
306 //trim(sa->name, len); // FIXME
310 memcpy(&sa->mac, at->data, ETH_ALEN);
314 sa->nc.ip=*(struct in_addr*)at->data;
318 sa->nc.netmask=*(struct in_addr*)at->data;
322 sa->nc.gw=*(struct in_addr*)at->data;
326 sa->nc.dhcp=( ntohs(*(unsigned short*)at->data)==1 );
330 len=min(at->size, FIRMWARE_SIZE-1);
331 memcpy(sa->firmware, at->data, len);
335 case ATTR_PORTS_COUNT:
336 sa->ports=*(unsigned char*)at->data;