]> git.sur5r.net Git - ngadmin/blob - lib/src/ngadmin.c
Added support for clearing the switch settings to the defaults.
[ngadmin] / lib / src / ngadmin.c
1
2 #include "lib.h"
3 #include "network.h"
4
5
6
7 static const struct timeval default_timeout={.tv_sec=3, .tv_usec=0};
8
9
10
11 // ---------------------------------------------
12 struct ngadmin* ngadmin_init (const char *iface) {
13  
14  struct ngadmin *nga;
15  
16  
17  // allocate main structure
18  nga=malloc(sizeof(struct ngadmin));
19  memset(nga, 0, sizeof(struct ngadmin));
20  
21  strncpy(nga->iface, iface, IFNAMSIZ-1);
22  
23  if ( startNetwork(nga)<0 ) {
24   free(nga);
25   return NULL;
26  }
27  
28  nga->timeout=default_timeout;
29  if ( updateTimeout(nga)<0 ) {
30   free(nga);
31   return NULL;
32  }
33  
34  
35  return nga;
36  
37 }
38
39
40
41 // ------------------------------------
42 int ngadmin_close (struct ngadmin *nga) {
43  
44  if ( nga==NULL ) {
45   return ERR_INVARG;
46  }
47  
48  
49  stopNetwork(nga);
50  free(nga->swi_tab);
51  free(nga);
52  
53  
54  return ERR_OK;
55  
56 }
57
58
59
60 // ---------------------------------------------
61 int ngadmin_forceInterface (struct ngadmin *nga) {
62  
63  
64  if ( nga==NULL ) {
65   return ERR_INVARG;
66  }
67  
68  
69  if ( forceInterface(nga)!=0 ) {
70   return ERR_NET;
71  } else {
72   return ERR_OK;
73  }
74  
75 }
76
77
78
79 // --------------------------------------------------------------
80 int ngadmin_setKeepBroadcasting (struct ngadmin *nga, bool value) {
81  
82  
83  if ( nga==NULL ) {
84   return ERR_INVARG;
85  }
86  
87  
88  nga->keepbroad=value;
89  
90  
91  return ERR_OK;
92  
93 }
94
95
96
97 // ------------------------------------------------------------
98 int ngadmin_setPassword (struct ngadmin *nga, const char *pass) {
99  
100  if ( nga==NULL ) {
101   return ERR_INVARG;
102  }
103  
104  
105  strncpy(nga->password, pass, PASSWORD_MAX);
106  
107  
108  return ERR_OK;
109  
110 }
111
112
113
114 // -------------------------------------------------------------------
115 int ngadmin_setTimeout (struct ngadmin *nga, const struct timeval *tv) {
116  
117  int ret=ERR_OK;
118  
119  
120  if ( nga==NULL || tv==NULL ) {
121   return ERR_INVARG;
122  }
123  
124  
125  nga->timeout=*tv;
126  if ( updateTimeout(nga)<0 ) {
127   ret=ERR_NET;
128  }
129  
130  
131  return ret;
132  
133 }
134
135
136
137 // -----------------------------------
138 int ngadmin_scan (struct ngadmin *nga) {
139  
140  int i;
141  List *attr, *swiList;
142  struct swi_attr *sa;
143  /*
144  sent by official win client:
145   ATTR_PRODUCT, 
146   ATTR_UNK2, 
147   ATTR_NAME, 
148   ATTR_MAC, 
149   ATTR_UNK5, 
150   ATTR_IP, 
151   ATTR_NETMASK, 
152   ATTR_GATEWAY, 
153   ATTR_DHCP, 
154   ATTR_UNK12, 
155   ATTR_FIRM_VER, 
156   ATTR_UNK14, 
157   ATTR_UNK15, 
158   ATTR_END
159  */
160  static const unsigned short hello[]={
161   ATTR_PRODUCT, 
162   ATTR_NAME, 
163   ATTR_MAC, 
164   ATTR_IP, 
165   ATTR_NETMASK, 
166   ATTR_GATEWAY, 
167   ATTR_DHCP, 
168   ATTR_FIRM_VER, 
169   ATTR_PORTS_COUNT, 
170   ATTR_END
171  };
172  
173  
174  if ( nga==NULL ) {
175   return ERR_INVARG;
176  }
177  
178  free(nga->swi_tab);
179  nga->swi_tab=NULL;
180  nga->swi_count=0;
181  nga->current=NULL;
182  
183  
184  // create attributes for an "hello" request
185  attr=createEmptyList();
186  for (i=0; ; ++i) {
187   pushBackList(attr, newEmptyAttr(hello[i]));
188   if ( hello[i]==ATTR_END ) break;
189  }
190  
191  // send request to all potential switches
192  i=sendNgPacket(nga, CODE_READ_REQ, attr);
193  destroyList(attr, (void(*)(void*))freeAttr);
194  if ( i<0 ) {
195   return ERR_NET;
196  }
197  
198  
199  // try to receive any packets until timeout
200  swiList=createEmptyList();
201  while ( (attr=recvNgPacket(nga, CODE_READ_REP, NULL, NULL))!=NULL ) {
202   sa=malloc(sizeof(struct swi_attr));
203   extractSwitchAttributes(sa, attr);
204   destroyList(attr, (void(*)(void*))freeAttr);
205   pushBackList(swiList, sa);
206  }
207  
208  nga->swi_count=swiList->count;
209  nga->swi_tab=convertToArray(swiList, sizeof(struct swi_attr));
210  
211  
212  return ERR_OK;
213  
214 }
215
216
217
218 // -----------------------------------------------------------------------
219 const struct swi_attr* ngadmin_getSwitchTab (struct ngadmin *nga, int *nb) {
220  
221  
222  if ( nga==NULL || nb==NULL ) {
223   return NULL;
224  }
225  
226  
227  *nb=nga->swi_count;
228  
229  
230  return nga->swi_tab;
231  
232 }
233
234
235
236 // ------------------------------------------------------------------
237 const struct swi_attr* ngadmin_getCurrentSwitch (struct ngadmin *nga) {
238  
239  
240  if ( nga==NULL ) {
241   return NULL;
242  }
243  
244  
245  return nga->current;
246  
247 }
248
249
250
251 // --------------------------------------------
252 int ngadmin_login (struct ngadmin *nga, int id) {
253  
254  List *attr;
255  int ret=ERR_OK, i;
256  struct swi_attr *sa;
257  char err;
258  unsigned short attr_error;
259  
260  
261  if ( nga==NULL ) {
262   return ERR_INVARG;
263  } else if ( id<0 || id>=nga->swi_count ) {
264   return ERR_BADID;
265  }
266  
267  
268  sa=&nga->swi_tab[id];
269  nga->current=sa;
270  
271  attr=createEmptyList();
272  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
273  pushBackList(attr, newEmptyAttr(ATTR_END));
274  i=sendNgPacket(nga, CODE_READ_REQ, attr);
275  destroyList(attr, (void(*)(void*))freeAttr);
276  if ( i<0 || (attr=recvNgPacket(nga, CODE_READ_REP, &err, &attr_error))==NULL ) {
277   ret=ERR_NET;
278   nga->current=NULL;
279   goto end;
280  }
281  
282  
283  destroyList(attr, (void(*)(void*))freeAttr);
284  if ( err==7 && attr_error==ATTR_PASSWORD ) {
285   ret=ERR_BADPASS;
286   nga->current=NULL;
287   goto end;
288  }
289  
290  
291  
292  end:
293  
294  return ret;
295  
296 }
297
298
299
300 // -------------------------------------------------------------------
301 int ngadmin_getPortsStatus (struct ngadmin *nga, unsigned char *ports) {
302  
303  List *attr;
304  ListNode *ln;
305  struct attr *at;
306  int ret=ERR_OK, i;
307  char *p;
308  
309  
310  if ( nga==NULL || ports==NULL ) {
311   return ERR_INVARG;
312  } else if ( nga->current==NULL ) {
313   return ERR_NOTLOG;
314  }
315  
316  
317  attr=createEmptyList();
318  pushBackList(attr, newEmptyAttr(ATTR_PORT_STATUS));
319  pushBackList(attr, newEmptyAttr(ATTR_END));
320  i=sendNgPacket(nga, CODE_READ_REQ, attr);
321  destroyList(attr, (void(*)(void*))freeAttr);
322  if ( i<0 || (attr=recvNgPacket(nga, CODE_READ_REP, NULL, NULL))==NULL ) {
323   ret=ERR_NET;
324   goto end;
325  }
326  
327  for (ln=attr->first; ln!=NULL; ln=ln->next) {
328   at=ln->data;
329   p=at->data;
330   if ( at->attr==ATTR_PORT_STATUS && at->size>=2 && (i=p[0]-1)>=0 && i<nga->current->ports ) {
331    ports[i]=p[1];
332   }
333  }
334  
335  destroyList(attr, (void(*)(void*))freeAttr);
336  
337  
338  end:
339  
340  return ret;
341  
342 }
343
344
345
346 // --------------------------------------------------------
347 int ngadmin_setName (struct ngadmin *nga, const char *name) {
348  
349  List *attr;
350  int ret=ERR_OK, i;
351  char err;
352  unsigned short attr_error;
353  
354  
355  if ( nga==NULL ) {
356   return ERR_INVARG;
357  } else if ( nga->current==NULL ) {
358   return ERR_NOTLOG;
359  }
360  
361  
362  attr=createEmptyList();
363  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
364  if ( name==NULL ) {
365   pushBackList(attr, newEmptyAttr(ATTR_NAME));
366  } else {
367   pushBackList(attr, newAttr(ATTR_NAME, strlen(name), strdup(name)));
368  }
369  pushBackList(attr, newEmptyAttr(ATTR_END));
370  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
371  destroyList(attr, (void(*)(void*))freeAttr);
372  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
373   ret=ERR_NET;
374   goto end;
375  }
376  
377  destroyList(attr, (void(*)(void*))freeAttr);
378  if ( err==7 && attr_error==ATTR_PASSWORD ) {
379   ret=ERR_BADPASS;
380   goto end;
381  }
382  
383  
384  // successful, also update local name
385  if ( name==NULL ) {
386   nga->current->name[0]=0;
387  } else {
388   strncpy(nga->current->name, name, NAME_SIZE);
389  }
390  
391  
392  end:
393  
394  return ret;
395  
396 }
397
398
399
400 // ------------------------------------------------------------------------
401 int ngadmin_getPortsStatistics (struct ngadmin *nga, struct port_stats *ps) {
402  
403  List *attr;
404  ListNode *ln;
405  struct attr *at;
406  int ret=ERR_OK, i;
407  int port;
408  
409  
410  if ( nga==NULL || ps==NULL ) {
411   return ERR_INVARG;
412  } else if ( nga->current==NULL ) {
413   return ERR_NOTLOG;
414  }
415  
416  
417  attr=createEmptyList();
418  pushBackList(attr, newEmptyAttr(ATTR_PORT_STATISTICS));
419  pushBackList(attr, newEmptyAttr(ATTR_END));
420  i=sendNgPacket(nga, CODE_READ_REQ, attr);
421  destroyList(attr, (void(*)(void*))freeAttr);
422  if ( i<0 || (attr=recvNgPacket(nga, CODE_READ_REP, NULL, NULL))==NULL ) {
423   ret=ERR_NET;
424   goto end;
425  }
426  
427  for (ln=attr->first; ln!=NULL; ln=ln->next) {
428   at=ln->data;
429   if ( at->attr==ATTR_PORT_STATISTICS && at->size>=49 && (port=(int)(*(char*)at->data)-1)>=0 && port<nga->current->ports ) {
430    ps[port].recv=be64toh(*(unsigned long long*)(at->data+1+8*0));
431    ps[port].sent=be64toh(*(unsigned long long*)(at->data+1+8*1));
432    ps[port].crc=be64toh(*(unsigned long long*)(at->data+1+8*5));
433    // all offsets between 2 and 4 inclusive are unknown values
434   }
435  }
436  
437  destroyList(attr, (void(*)(void*))freeAttr);
438  
439  
440  end:
441  
442  return ret;
443  
444 }
445
446
447
448 // ---------------------------------------------------
449 int ngadmin_resetPortsStatistics (struct ngadmin *nga) {
450  
451  List *attr;
452  int ret=ERR_OK, i;
453  char err;
454  unsigned short attr_error;
455  
456  
457  if ( nga==NULL ) {
458   return ERR_INVARG;
459  } else if ( nga->current==NULL ) {
460   return ERR_NOTLOG;
461  }
462  
463  
464  attr=createEmptyList();
465  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
466  pushBackList(attr, newByteAttr(ATTR_STATS_RESET, 1));
467  pushBackList(attr, newEmptyAttr(ATTR_END));
468  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
469  destroyList(attr, (void(*)(void*))freeAttr);
470  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
471   ret=ERR_NET;
472   goto end;
473  }
474  
475  destroyList(attr, (void(*)(void*))freeAttr);
476  
477  if ( err==7 && attr_error==ATTR_PASSWORD ) {
478   ret=ERR_BADPASS;
479   goto end;
480  }
481  
482  
483  
484  end:
485  
486  return ret;
487  
488 }
489
490
491
492 // ---------------------------------------------------------------
493 int ngadmin_changePassword (struct ngadmin *nga, const char* pass) {
494  
495  List *attr;
496  int ret=ERR_OK, i;
497  char err;
498  unsigned short attr_error;
499  
500  
501  if ( nga==NULL || pass==NULL ) {
502   return ERR_INVARG;
503  } else if ( nga->current==NULL ) {
504   return ERR_NOTLOG;
505  }
506  
507  
508  attr=createEmptyList();
509  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
510  pushBackList(attr, newAttr(ATTR_NEW_PASSWORD, strlen(pass), strdup(pass)));
511  pushBackList(attr, newEmptyAttr(ATTR_END));
512  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
513  destroyList(attr, (void(*)(void*))freeAttr);
514  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
515   ret=ERR_NET;
516   goto end;
517  }
518  
519  destroyList(attr, (void(*)(void*))freeAttr);
520  
521  if ( err==7 && attr_error==ATTR_PASSWORD ) {
522   ret=ERR_BADPASS;
523   goto end;
524  }
525  
526  
527  // successful, also update local password
528  strncpy(nga->password, pass, PASSWORD_MAX);
529  
530  
531  end:
532  
533  return ret;
534  
535 }
536
537
538
539 // ----------------------------------------------------------
540 int ngadmin_getStormFilterState (struct ngadmin *nga, int *s) {
541  
542  List *attr;
543  ListNode *ln;
544  struct attr *at;
545  int ret=ERR_OK, i;
546  
547  
548  if ( nga==NULL || s==NULL ) {
549   return ERR_INVARG;
550  } else if ( nga->current==NULL ) {
551   return ERR_NOTLOG;
552  }
553  
554  
555  attr=createEmptyList();
556  pushBackList(attr, newEmptyAttr(ATTR_STORM_ENABLE));
557  pushBackList(attr, newEmptyAttr(ATTR_END));
558  i=sendNgPacket(nga, CODE_READ_REQ, attr);
559  destroyList(attr, (void(*)(void*))freeAttr);
560  if ( i<0 || (attr=recvNgPacket(nga, CODE_READ_REP, NULL, NULL))==NULL ) {
561   ret=ERR_NET;
562   goto end;
563  }
564  
565  for (ln=attr->first; ln!=NULL; ln=ln->next) {
566   at=ln->data;
567   if ( at->attr==ATTR_STORM_ENABLE && at->size>=1 ) {
568    *s= *(char*)at->data!=0 ;
569    break;
570   }
571  }
572  
573  destroyList(attr, (void(*)(void*))freeAttr);
574  
575  
576  end:
577  
578  return ret;
579  
580 }
581
582
583
584 // ---------------------------------------------------------
585 int ngadmin_setStormFilterState (struct ngadmin *nga, int s) {
586  
587  List *attr;
588  int ret=ERR_OK, i;
589  char err;
590  unsigned short attr_error;
591  
592  
593  if ( nga==NULL ) {
594   return ERR_INVARG;
595  } else if ( nga->current==NULL ) {
596   return ERR_NOTLOG;
597  }
598  
599  
600  attr=createEmptyList();
601  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
602  pushBackList(attr, newByteAttr(ATTR_STORM_ENABLE, s!=0));
603  pushBackList(attr, newEmptyAttr(ATTR_END));
604  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
605  destroyList(attr, (void(*)(void*))freeAttr);
606  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
607   ret=ERR_NET;
608   goto end;
609  }
610  
611  destroyList(attr, (void(*)(void*))freeAttr);
612  
613  if ( err==7 && attr_error==ATTR_PASSWORD ) {
614   ret=ERR_BADPASS;
615   goto end;
616  }
617  
618  
619  end:
620  
621  return ret;
622  
623 }
624
625
626
627 // ---------------------------------------------------------------
628 int ngadmin_getStormFilterValues (struct ngadmin *nga, int *ports) {
629  
630  List *attr;
631  ListNode *ln;
632  struct attr *at;
633  int ret=ERR_OK, i;
634  
635  
636  if ( nga==NULL || ports==NULL ) {
637   return ERR_INVARG;
638  } else if ( nga->current==NULL ) {
639   return ERR_NOTLOG;
640  }
641  
642  
643  attr=createEmptyList();
644  pushBackList(attr, newEmptyAttr(ATTR_STORM_BITRATE));
645  pushBackList(attr, newEmptyAttr(ATTR_END));
646  i=sendNgPacket(nga, CODE_READ_REQ, attr);
647  destroyList(attr, (void(*)(void*))freeAttr);
648  if ( i<0 || (attr=recvNgPacket(nga, CODE_READ_REP, NULL, NULL))==NULL ) {
649   ret=ERR_NET;
650   goto end;
651  }
652  
653  for (ln=attr->first; ln!=NULL; ln=ln->next) {
654   at=ln->data;
655   if ( at->attr==ATTR_STORM_BITRATE && at->size>=5 && (i=*(char*)(at->data)-1)>=0 && i<nga->current->ports ) {
656    ports[i]=ntohl(*(int*)(1+(char*)at->data));
657   }
658  }
659  
660  destroyList(attr, (void(*)(void*))freeAttr);
661  
662  
663  end:
664  
665  return ret;
666  
667 }
668
669
670
671 // ---------------------------------------------------------------------
672 int ngadmin_setStormFilterValues (struct ngadmin *nga, const int *ports) {
673  
674  List *attr;
675  int ret=ERR_OK, i;
676  char err;
677  unsigned short attr_error;
678  char *p;
679  
680  
681  if ( nga==NULL || ports==NULL ) {
682   return ERR_INVARG;
683  } else if ( nga->current==NULL ) {
684   return ERR_NOTLOG;
685  }
686  
687  
688  attr=createEmptyList();
689  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
690  
691  for (i=0; i<nga->current->ports; ++i) {
692   if ( ports[i]>=0 && ports[i]<=11 ) {
693    p=malloc(5);
694    *p=i+1;
695    *(int*)(p+1)=htonl(ports[i]);
696    pushBackList(attr, newAttr(ATTR_STORM_BITRATE, 5, p));
697   }
698  }
699  
700  pushBackList(attr, newEmptyAttr(ATTR_END));
701  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
702  destroyList(attr, (void(*)(void*))freeAttr);
703  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
704   ret=ERR_NET;
705   goto end;
706  }
707  
708  destroyList(attr, (void(*)(void*))freeAttr);
709  
710  if ( err==7 && attr_error==ATTR_PASSWORD ) {
711   ret=ERR_BADPASS;
712   goto end;
713  }
714  
715  
716  end:
717  
718  return ret;
719  
720 }
721
722
723
724 // -----------------------------------------------------------
725 int ngadmin_getBitrateLimits (struct ngadmin *nga, int *ports) {
726  
727  List *attr;
728  ListNode *ln;
729  struct attr *at;
730  int ret=ERR_OK, i;
731  
732  
733  if ( nga==NULL || ports==NULL ) {
734   return ERR_INVARG;
735  } else if ( nga->current==NULL ) {
736   return ERR_NOTLOG;
737  }
738  
739  
740  attr=createEmptyList();
741  pushBackList(attr, newEmptyAttr(ATTR_BITRATE_INPUT));
742  pushBackList(attr, newEmptyAttr(ATTR_BITRATE_OUTPUT));
743  pushBackList(attr, newEmptyAttr(ATTR_END));
744  i=sendNgPacket(nga, CODE_READ_REQ, attr);
745  destroyList(attr, (void(*)(void*))freeAttr);
746  if ( i<0 || (attr=recvNgPacket(nga, CODE_READ_REP, NULL, NULL))==NULL ) {
747   ret=ERR_NET;
748   goto end;
749  }
750  
751  for (ln=attr->first; ln!=NULL; ln=ln->next) {
752   at=ln->data;
753   if ( at->attr==ATTR_BITRATE_INPUT && at->size>=5 && (i=*(char*)(at->data)-1)>=0 && i<nga->current->ports ) {
754    ports[i*2+0]=ntohl(*(int*)(1+(char*)at->data));
755   } else if ( at->attr==ATTR_BITRATE_OUTPUT && at->size>=5 && (i=*(char*)(at->data)-1)>=0 && i<nga->current->ports ) {
756    ports[i*2+1]=ntohl(*(int*)(1+(char*)at->data));
757   }
758  }
759  
760  destroyList(attr, (void(*)(void*))freeAttr);
761  
762  
763  end:
764  
765  return ret;
766  
767 }
768
769
770
771 // -----------------------------------------------------------------
772 int ngadmin_setBitrateLimits (struct ngadmin *nga, const int *ports) {
773  
774  List *attr;
775  int ret=ERR_OK, i;
776  char err;
777  unsigned short attr_error;
778  char *p;
779  
780  
781  if ( nga==NULL || ports==NULL ) {
782   return ERR_INVARG;
783  } else if ( nga->current==NULL ) {
784   return ERR_NOTLOG;
785  }
786  
787  
788  attr=createEmptyList();
789  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
790  
791  for (i=0; i<nga->current->ports; ++i) {
792   if ( ports[2*i+0]>=0 && ports[2*i+0]<=11 ) {
793    p=malloc(5);
794    *p=i+1;
795    *(int*)(p+1)=htonl(ports[2*i+0]);
796    pushBackList(attr, newAttr(ATTR_BITRATE_INPUT, 5, p));
797   }
798   if ( ports[2*i+1]>=0 && ports[2*i+1]<=11 ) {
799    p=malloc(5);
800    *p=i+1;
801    *(int*)(p+1)=htonl(ports[2*i+1]);
802    pushBackList(attr, newAttr(ATTR_BITRATE_OUTPUT, 5, p));
803   }
804  }
805  
806  pushBackList(attr, newEmptyAttr(ATTR_END));
807  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
808  destroyList(attr, (void(*)(void*))freeAttr);
809  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
810   ret=ERR_NET;
811   goto end;
812  }
813  
814  destroyList(attr, (void(*)(void*))freeAttr);
815  
816  if ( err==7 && attr_error==ATTR_PASSWORD ) {
817   ret=ERR_BADPASS;
818   goto end;
819  }
820  
821  
822  end:
823  
824  return ret;
825  
826 }
827
828
829
830 // -------------------------------------------------
831 int ngadmin_getQOSMode (struct ngadmin *nga, int *s) {
832  
833  List *attr;
834  ListNode *ln;
835  struct attr *at;
836  int ret=ERR_OK, i;
837  
838  
839  if ( nga==NULL || s==NULL ) {
840   return ERR_INVARG;
841  } else if ( nga->current==NULL ) {
842   return ERR_NOTLOG;
843  }
844  
845  
846  attr=createEmptyList();
847  pushBackList(attr, newEmptyAttr(ATTR_QOS_TYPE));
848  pushBackList(attr, newEmptyAttr(ATTR_END));
849  i=sendNgPacket(nga, CODE_READ_REQ, attr);
850  destroyList(attr, (void(*)(void*))freeAttr);
851  if ( i<0 || (attr=recvNgPacket(nga, CODE_READ_REP, NULL, NULL))==NULL ) {
852   ret=ERR_NET;
853   goto end;
854  }
855  
856  for (ln=attr->first; ln!=NULL; ln=ln->next) {
857   at=ln->data;
858   if ( at->attr==ATTR_QOS_TYPE && at->size>=1 ) {
859    *s= *(char*)at->data ;
860    break;
861   }
862  }
863  
864  destroyList(attr, (void(*)(void*))freeAttr);
865  
866  
867  end:
868  
869  return ret;
870  
871 }
872
873
874 // ------------------------------------------------
875 int ngadmin_setQOSMode (struct ngadmin *nga, int s) {
876  
877  List *attr;
878  int ret=ERR_OK, i;
879  char err;
880  unsigned short attr_error;
881  
882  
883  if ( nga==NULL || s<1 || s>2 ) {
884   return ERR_INVARG;
885  } else if ( nga->current==NULL ) {
886   return ERR_NOTLOG;
887  }
888  
889  
890  attr=createEmptyList();
891  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
892  pushBackList(attr, newByteAttr(ATTR_QOS_TYPE, s));
893  pushBackList(attr, newEmptyAttr(ATTR_END));
894  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
895  destroyList(attr, (void(*)(void*))freeAttr);
896  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
897   ret=ERR_NET;
898   goto end;
899  }
900  
901  destroyList(attr, (void(*)(void*))freeAttr);
902  
903  if ( err==7 && attr_error==ATTR_PASSWORD ) {
904   ret=ERR_BADPASS;
905   goto end;
906  }
907  
908  
909  end:
910  
911  return ret;
912  
913 }
914
915
916
917 // -------------------------------------------------------
918 int ngadmin_getQOSValues (struct ngadmin *nga, char *ports) {
919  
920  List *attr;
921  ListNode *ln;
922  struct attr *at;
923  int ret=ERR_OK, i;
924  char *p;
925  
926  
927  if ( nga==NULL || ports==NULL ) {
928   return ERR_INVARG;
929  } else if ( nga->current==NULL ) {
930   return ERR_NOTLOG;
931  }
932  
933  
934  attr=createEmptyList();
935  pushBackList(attr, newEmptyAttr(ATTR_QOS_CONFIG));
936  pushBackList(attr, newEmptyAttr(ATTR_END));
937  i=sendNgPacket(nga, CODE_READ_REQ, attr);
938  destroyList(attr, (void(*)(void*))freeAttr);
939  if ( i<0 || (attr=recvNgPacket(nga, CODE_READ_REP, NULL, NULL))==NULL ) {
940   ret=ERR_NET;
941   goto end;
942  }
943  
944  for (ln=attr->first; ln!=NULL; ln=ln->next) {
945   at=ln->data;
946   p=at->data;
947   if ( at->attr==ATTR_QOS_CONFIG && at->size>=2 && --p[0]>=0 && p[0]<nga->current->ports ) {
948    ports[(int)p[0]]=p[1];
949   }
950  }
951  
952  destroyList(attr, (void(*)(void*))freeAttr);
953  
954  
955  end:
956  
957  return ret;
958  
959 }
960
961
962
963 // --------------------------------------------------------------
964 int ngadmin_setQOSValues (struct ngadmin *nga, const char *ports) {
965  
966  List *attr;
967  int ret=ERR_OK, i;
968  char err;
969  unsigned short attr_error;
970  char *p;
971  
972  
973  if ( nga==NULL || ports==NULL ) {
974   return ERR_INVARG;
975  } else if ( nga->current==NULL ) {
976   return ERR_NOTLOG;
977  }
978  
979  
980  attr=createEmptyList();
981  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
982  
983  for (i=0; i<nga->current->ports; ++i) {
984   if ( ports[i]>=PRIO_HIGH && ports[i]<=PRIO_LOW ) {
985    p=malloc(2);
986    p[0]=i+1;
987    p[1]=ports[i];
988    pushBackList(attr, newAttr(ATTR_QOS_CONFIG, 2, p));
989   }
990  }
991  
992  pushBackList(attr, newEmptyAttr(ATTR_END));
993  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
994  destroyList(attr, (void(*)(void*))freeAttr);
995  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
996   ret=ERR_NET;
997   goto end;
998  }
999  
1000  destroyList(attr, (void(*)(void*))freeAttr);
1001  
1002  if ( err==7 && attr_error==ATTR_PASSWORD ) {
1003   ret=ERR_BADPASS;
1004   goto end;
1005  }
1006  
1007  
1008  end:
1009  
1010  return ret;
1011  
1012 }
1013
1014
1015
1016 // --------------------------------------
1017 int ngadmin_restart (struct ngadmin *nga) {
1018  
1019  List *attr;
1020  int ret=ERR_OK, i;
1021  char err;
1022  unsigned short attr_error;
1023  
1024  
1025  if ( nga==NULL ) {
1026   return ERR_INVARG;
1027  } else if ( nga->current==NULL ) {
1028   return ERR_NOTLOG;
1029  }
1030  
1031  
1032  attr=createEmptyList();
1033  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
1034  pushBackList(attr, newByteAttr(ATTR_RESTART, 1));
1035  pushBackList(attr, newEmptyAttr(ATTR_END));
1036  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
1037  destroyList(attr, (void(*)(void*))freeAttr);
1038  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
1039   ret=ERR_NET;
1040   goto end;
1041  }
1042  
1043  destroyList(attr, (void(*)(void*))freeAttr);
1044  
1045  if ( err==7 && attr_error==ATTR_PASSWORD ) {
1046   ret=ERR_BADPASS;
1047   goto end;
1048  }
1049  
1050  
1051  end:
1052  
1053  return ret;
1054  
1055 }
1056
1057
1058
1059 // ---------------------------------------
1060 int ngadmin_defaults (struct ngadmin *nga) {
1061  
1062  List *attr;
1063  int ret=ERR_OK, i;
1064  char err;
1065  unsigned short attr_error;
1066  
1067  
1068  if ( nga==NULL ) {
1069   return ERR_INVARG;
1070  } else if ( nga->current==NULL ) {
1071   return ERR_NOTLOG;
1072  }
1073  
1074  
1075  attr=createEmptyList();
1076  pushBackList(attr, newAttr(ATTR_PASSWORD, strlen(nga->password), strdup(nga->password)));
1077  pushBackList(attr, newByteAttr(ATTR_DEFAULTS, 1));
1078  pushBackList(attr, newEmptyAttr(ATTR_END));
1079  i=sendNgPacket(nga, CODE_WRITE_REQ, attr);
1080  destroyList(attr, (void(*)(void*))freeAttr);
1081  if ( i<0 || (attr=recvNgPacket(nga, CODE_WRITE_REP, &err, &attr_error))==NULL ) {
1082   ret=ERR_NET;
1083   goto end;
1084  }
1085  
1086  destroyList(attr, (void(*)(void*))freeAttr);
1087  
1088  if ( err==7 && attr_error==ATTR_PASSWORD ) {
1089   ret=ERR_BADPASS;
1090   goto end;
1091  }
1092  
1093  
1094  // successful: delog and clean list
1095  free(nga->swi_tab);
1096  nga->swi_tab=NULL;
1097  nga->swi_count=0;
1098  nga->current=NULL;
1099  
1100  
1101  end:
1102  
1103  return ret;
1104  
1105 }
1106
1107