]> git.sur5r.net Git - ngadmin/blob - cli/commands.c
b26d2fd44951d3a764ba4fc0b41fe4f287153225
[ngadmin] / cli / commands.c
1
2 #include "commands.h"
3
4
5
6
7 // =============================================================================
8 // bitrate
9
10
11 // helper function to analyse bitrate speed specifications
12 static int bitrate_analyse (int nb, const char **com, int *ports) {
13  
14  int i=0, s;
15  
16  
17  while ( i<nb-1 ) {
18   s=parseBitrate(com[i+1]);
19   if ( strcmp(com[i], "inout")==0 ) {
20    ports[0]=s;
21    ports[1]=s;
22   } else if ( strcmp(com[i], "in")==0 ) {
23    ports[0]=s;
24   } else if ( strcmp(com[i], "out")==0 ) {
25    ports[1]=s;
26   } else {
27    break;
28   }
29   i+=2;
30  }
31  
32  
33  return i;
34  
35 }
36
37
38 static bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga) {
39  
40  int i, k=0, defs[]={12, 12}, p, *ports=NULL;
41  const struct swi_attr *sa;
42  bool ret=true;
43  
44  
45  if ( nb<2 ) {
46   printf("Usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n");
47   printf("SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\n");
48   ret=false;
49   goto end;
50  }
51  
52  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
53   printf("must be logged\n");
54   ret=false;
55   goto end;
56  }
57  
58  ports=malloc(2*sa->ports*sizeof(int));
59  
60  // get defaults if present
61  if ( strcmp(com[k], "all")==0 ) {
62   ++k;
63   k+=bitrate_analyse(nb-k, &com[k], defs);
64  }
65  
66  // apply defaults
67  for (i=0; i<sa->ports; ++i) {
68   memcpy(&ports[2*i], defs, sizeof(defs));
69  }
70  
71  // get ports specifics
72  while ( k<nb ) {
73   p=strtol(com[k++], NULL, 0)-1;
74   if ( p>=0 && p<sa->ports ) {
75    k+=bitrate_analyse(nb-k, &com[k], &ports[2*p]);
76   }
77  }
78  
79  // send it to the switch
80  i=ngadmin_setBitrateLimits(nga, ports);
81  printErrCode(i);
82  
83  
84  end:
85  free(ports);
86  
87  return ret;
88  
89 }
90
91
92
93 static bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
94  
95  int i, ret=true, *ports=NULL;
96  const struct swi_attr *sa;
97  
98  
99  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
100   printf("must be logged\n");
101   ret=false;
102   goto end;
103  }
104  
105  
106  ports=malloc(2*sa->ports*sizeof(int));
107  if ( (i=ngadmin_getBitrateLimits(nga, ports))!=ERR_OK ) {
108   printErrCode(i);
109   ret=false;
110   goto end;
111  }
112  
113  for (i=0; i<sa->ports; ++i) {
114   printf("port %i: in %s, out %s\n", i+1, bitrates[ports[2*i+0]], bitrates[ports[2*i+1]]);
115  }
116  
117  end:
118  free(ports);
119  
120  return ret;
121  
122 }
123
124
125
126 // =============================================================================
127 // cabletest
128
129
130 static bool do_cabletest (int nb, const char **com, struct ngadmin *nga) {
131  
132  bool ret=true;
133  const struct swi_attr *sa;
134  struct cabletest *ct=NULL;
135  int i, j=0, k=0;
136  
137  
138  if ( nb<1 ) {
139   printf("Usage: cabletest <port1> [<port2> ...]\n");
140   goto end;
141  }
142  
143  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
144   printf("must be logged\n");
145   ret=false;
146   goto end;
147  }
148  
149  ct=malloc(sa->ports*sizeof(struct cabletest));
150  memset(ct, 0, sa->ports*sizeof(struct cabletest));
151  
152  while ( k<nb ) {
153   ct[j].port=strtol(com[k++], NULL, 0);
154   if ( ct[j].port>=1 && ct[j].port<=sa->ports ) ++j;
155  }
156  
157  i=ngadmin_cabletest(nga, ct, j);
158  if ( i<0 ) {
159   printErrCode(i);
160   ret=false;
161   goto end;
162  }
163  
164  
165  for (i=0; i<j; ++i) {
166   printf("port %i: %08X %08X\n", ct[i].port, ct[i].v1, ct[i].v2);
167  }
168  
169  
170  end:
171  free(ct);
172  
173  return ret;
174  
175 }
176
177
178
179 // =============================================================================
180 // defaults
181
182
183 static bool do_defaults (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
184  
185  int i, ret=true;
186  const struct swi_attr *sa;
187  char line[16];
188  
189  
190  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
191   printf("must be logged\n");
192   ret=false;
193   goto end;
194  }
195  
196  
197  printf("The switch settings will be CLEARED. Continue ? [y/N]: ");
198  fflush(stdout);
199  
200  if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) {
201   i=ngadmin_defaults(nga);
202   printErrCode(i);
203  }
204  
205  
206  end:
207  
208  return ret;
209  
210 }
211
212
213
214 // =============================================================================
215 // firmware
216
217
218 static bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
219  
220  const struct swi_attr *sa;
221  bool ret=true;
222  
223  
224  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
225   printf("must be logged\n");
226   ret=false;
227   goto end;
228  }
229  
230  puts(sa->firmware);
231  
232  
233  end:
234  
235  return ret;
236  
237 }
238
239
240
241 static bool do_firmware_upgrade (int nb, const char **com UNUSED, struct ngadmin *nga) {
242  
243  const struct swi_attr *sa;
244  bool ret=true;
245  
246  
247  if ( nb!=1 ) {
248   printf("Usage: firmware upgrade <file>\n");
249   ret=false;
250  }
251  
252  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
253   printf("must be logged\n");
254   ret=false;
255   goto end;
256  }
257  
258  printf("not implemented yet\n");
259  
260  
261  end:
262  
263  return ret;
264  
265 }
266
267
268
269 // =============================================================================
270 // igmp
271
272
273 static bool do_igmp_set (int nb, const char **com, struct ngadmin *nga) {
274  
275  int i;
276  struct igmp_conf ic;
277  
278  
279  if ( nb!=4 ) {
280   printf("Usage: igmp set <enable> <vlan> <validate> <block>\n");
281   return false;
282  }
283  
284  if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
285   printf("must be logged\n");
286   return false;
287  }
288  
289  ic.enable=strtol(com[0], NULL, 0);
290  ic.vlan=strtol(com[1], NULL, 0);
291  ic.validate=strtol(com[2], NULL, 0);
292  ic.block=strtol(com[3], NULL, 0);
293  
294  i=ngadmin_setIGMPConf(nga, &ic);
295  printErrCode(i);
296  
297  
298  return true;
299  
300 }
301
302
303
304 static bool do_igmp_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
305  
306  int i;
307  const struct swi_attr *sa;
308  struct igmp_conf ic;
309  bool ret=true;
310  
311  
312  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
313   printf("must be logged\n");
314   ret=false;
315   goto end;
316  }
317  
318  i=ngadmin_getIGMPConf(nga, &ic);
319  if ( i!=ERR_OK ) {
320   printErrCode(i);
321   ret=false;
322   goto end;
323  }
324  
325  
326  printf("IGMP snooping enabled: %s\n", ic.enable ? "yes" : "no" );
327  printf("IGMP snooping vlan: %u\n", ic.vlan);
328  printf("Validate IGMPv3 headers: %s\n", ic.validate ? "yes" : "no" );
329  printf("Block unknown multicast addresses: %s\n", ic.block ? "yes" : "no" );
330  
331  
332  
333  end:
334  
335  return ret;
336  
337 }
338
339
340
341 // =============================================================================
342 // list
343
344
345 static bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
346  
347  int n;
348  const struct swi_attr *sa;
349  
350  
351  sa=ngadmin_getSwitchTab(nga, &n);
352  displaySwitchTab(sa, n);
353  
354  
355  return true;
356  
357 }
358
359
360
361 // =============================================================================
362 // login
363
364
365 static bool do_login (int nb, const char **com, struct ngadmin *nga) {
366  
367  int i;
368  
369  
370  if ( nb!=1 ) {
371   printf("Usage: login <num>\n");
372   return false;
373  }
374  
375  
376  i=strtol(com[0], NULL, 0);
377  i=ngadmin_login(nga, i);
378  printErrCode(i);
379  
380  
381  return true;
382  
383 }
384
385
386
387 // =============================================================================
388 // mirror
389
390
391 static bool do_mirror_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
392  
393  int i;
394  
395  
396  if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
397   printf("must be logged\n");
398   return false;
399  }
400  
401  
402  i=ngadmin_setMirror(nga, NULL);
403  printErrCode(i);
404  
405  
406  return true;
407  
408 }
409
410
411
412 static bool do_mirror_set (int nb, const char **com, struct ngadmin *nga) {
413  
414  const struct swi_attr *sa;
415  char *ports=NULL;
416  bool ret=true;
417  int i, k=0;
418  
419  
420  if ( nb<3 ) {
421   printf("Usage: mirror set <destination port> clone <port1> [<port2> ...]\n");
422   goto end;
423  }
424  
425  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
426   printf("must be logged\n");
427   ret=false;
428   goto end;
429  }
430  
431  
432  ports=malloc((sa->ports+1)*sizeof(char));
433  memset(ports, 0, sa->ports+1);
434  
435  ports[0]=strtol(com[k++], NULL, 0);
436  if ( ports[0]<1 || ports[0]>sa->ports || strcasecmp(com[k++], "clone")!=0 ) {
437   printf("syntax error\n");
438   ret=false;
439   goto end;
440  }
441  
442  
443  while ( k<nb ) {
444   i=strtol(com[k++], NULL, 0);
445   if ( i<1 || i>sa->ports ) {
446    printf("port out of range\n");
447    ret=false;
448    goto end;
449   } else if ( i==ports[0] ) {
450    printf("destination port cannot be in port list\n");
451    ret=false;
452    goto end;
453   }
454   ports[i]=1;
455  }
456  
457  
458  i=ngadmin_setMirror(nga, ports);
459  printErrCode(i);
460  
461  
462  end:
463  free(ports);
464  
465  return ret;
466  
467 }
468
469
470
471 static bool do_mirror_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
472  
473  const struct swi_attr *sa;
474  char *ports=NULL;
475  int i;
476  
477  
478  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
479   printf("must be logged\n");
480   return false;
481  }
482  
483  
484  ports=malloc((sa->ports+1)*sizeof(char));
485  
486  i=ngadmin_getMirror(nga, ports);
487  if ( i!=ERR_OK ) {
488   printErrCode(i);
489   goto end;
490  }
491  
492  if ( ports[0]==0 ) {
493   printf("port mirroring is disabled\n");
494   goto end;
495  }
496  
497  printf("destination: %i\n", ports[0]);
498  printf("ports: ");
499  for (i=1; i<=sa->ports; ++i) {
500   if ( ports[i] ) {
501    printf("%i ", i);
502   }
503  }
504  printf("\n");
505  
506  
507  
508  end:
509  free(ports);
510  
511  return true;
512  
513 }
514
515
516
517 // =============================================================================
518 // name
519
520
521 static bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
522  
523  const struct swi_attr *sa;
524  
525  
526  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
527   printf("must be logged\n");
528   return false;
529  }
530  
531  puts(sa->name);
532  
533  
534  return true;
535  
536 }
537
538
539
540 static bool do_name_set (int nb, const char **com, struct ngadmin *nga) {
541  
542  int i;
543  const struct swi_attr *sa;
544  
545  
546  if ( nb!=1 ) {
547   printf("Usage: name set <value>\n");
548   return false;
549  }
550  
551  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
552   printf("must be logged\n");
553   return false;
554  }
555  
556  i=ngadmin_setName(nga, com[0]);
557  printErrCode(i);
558  
559  
560  return true;
561  
562 }
563
564
565
566 static bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
567  
568  int i;
569  const struct swi_attr *sa;
570  
571  
572  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
573   printf("must be logged\n");
574   return false;
575  }
576  
577  i=ngadmin_setName(nga, NULL);
578  printErrCode(i);
579  
580  
581  return true;
582  
583 }
584
585
586
587 // =============================================================================
588 // netconf
589
590
591 static bool do_netconf_set (int nb, const char **com, struct ngadmin *nga) {
592  
593  int i, k;
594  const struct swi_attr *sa;
595  struct net_conf nc;
596  bool ret=true;
597  
598  
599  if ( nb==0 ) {
600   printf("Usage: netconf set [dhcp yes|no] [ip <ip>] [mask <mask>] [gw <gw>]\n");
601   return false;
602  }
603  
604  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
605   printf("must be logged\n");
606   return false;
607  }
608  
609  
610  memset(&nc, 0, sizeof(struct net_conf));
611  
612  for (k=0; k<nb; k+=2) {
613   
614   if ( strcasecmp(com[k], "dhcp")==0 ) {
615    if ( strcasecmp(com[k+1], "yes")==0 ) {
616     nc.dhcp=true;
617    } else if ( strcasecmp(com[k+1], "no")==0 ) {
618     nc.dhcp=false;
619    } else {
620     printf("Incorrect DHCP value\n");
621     ret=false;
622     goto end;
623    }
624    
625   } else if ( strcasecmp(com[k], "ip")==0 ) {
626    if ( inet_aton(com[k+1], &nc.ip)==0 ) {
627     printf("Incorrect IP value\n");
628     ret=false;
629     goto end;
630    }
631    
632   } else if ( strcasecmp(com[k], "mask")==0 ) {
633    if ( inet_aton(com[k+1], &nc.netmask)==0 ) { // TODO: check if it is a correct mask
634     printf("Incorrect mask value\n");
635     ret=false;
636     goto end;
637    }
638    
639   } else if ( strcasecmp(com[k], "gw")==0 ) {
640    if ( inet_aton(com[k+1], &nc.gw)==0 ) {
641     printf("Incorrect gateway value\n");
642     ret=false;
643     goto end;
644    }
645    
646   }
647   
648  }
649  
650  
651  i=ngadmin_setNetConf(nga, &nc);
652  if ( i!=ERR_OK ) {
653   printErrCode(i);
654   ret=false;
655  }
656  
657  
658  end:
659  
660  return ret;
661  
662 }
663
664
665
666 // =============================================================================
667 // password
668
669
670 static bool do_password_change (int nb, const char **com, struct ngadmin *nga) {
671  
672  int i;
673  const struct swi_attr *sa;
674  
675  
676  if ( nb!=1 ) {
677   printf("Usage: password change <value>\n");
678   return false;
679  }
680  
681  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
682   printf("must be logged\n");
683   return false;
684  }
685  
686  i=ngadmin_changePassword(nga, com[0]);
687  printErrCode(i);
688  
689  
690  return true;
691  
692 }
693
694
695
696 static bool do_password_set (int nb, const char **com, struct ngadmin *nga) {
697  
698  int i;
699  
700  
701  if ( nb!=1 ) {
702   printf("Usage: password set <value>\n");
703   return false;
704  }
705  
706  i=ngadmin_setPassword(nga, com[0]);
707  printErrCode(i);
708  
709  
710  return true;
711  
712 }
713
714
715
716 // =============================================================================
717 // ports
718
719
720 static bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
721  
722  int i;
723  const struct swi_attr *sa;
724  unsigned char *ports=NULL;
725  bool ret=true;
726  
727  
728  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
729   printf("must be logged\n");
730   ret=false;
731   goto end;
732  }
733  
734  
735  ports=malloc(sa->ports*sizeof(unsigned char));
736  if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) {
737   printErrCode(i);
738   ret=false;
739   goto end;
740  }
741  
742  for (i=0; i<sa->ports; i++) {
743   printf("port %i: ", i+1);
744   switch ( ports[i] ) {
745    case 0: printf("down"); break;
746    case SPEED_10: printf("up, 10M"); break;
747    case SPEED_100: printf("up, 100M"); break;
748    case SPEED_1000: printf("up, 1000M"); break;
749    default: printf("unknown (%i)", ports[i]);
750   }
751   putchar('\n');
752  }
753  
754  end:
755  free(ports);
756  
757  
758  return ret;
759  
760 }
761
762
763
764 static bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
765  
766  int i;
767  
768  
769  if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
770   printf("must be logged\n");
771   return false;
772  }
773  
774  i=ngadmin_resetPortsStatistics(nga);
775  printErrCode(i);
776  
777  
778  return true;
779  
780 }
781
782
783
784 static bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
785  
786  int i;
787  const struct swi_attr *sa;
788  bool ret=true;
789  struct port_stats *ps=NULL;
790  
791  
792  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
793   printf("must be logged\n");
794   ret=false;
795   goto end;
796  }
797  
798  ps=calloc(sa->ports, sizeof(struct port_stats));
799  if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) {
800   printErrCode(i);
801   ret=false;
802   goto end;
803  }
804  
805  printf("Port\tReceived\tSent\tCRC errors\n");
806  for (i=0; i<sa->ports; ++i) {
807   printf("% 4i%12llu%12llu%14llu\n", i+1, ps[i].recv, ps[i].sent, ps[i].crc);
808  }
809  
810  end:
811  free(ps);
812  
813  
814  return ret;
815  
816 }
817
818
819
820
821 // =============================================================================
822 // qos
823
824
825
826 static bool do_qos_mode (int nb, const char **com, struct ngadmin *nga) {
827  
828  int i, s, ret=true;
829  const struct swi_attr *sa;
830  
831  
832  if ( nb==0 ) {
833   printf("Usage: qos mode port|802.1p\n");
834   goto end;
835  }
836  
837  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
838   printf("must be logged\n");
839   ret=false;
840   goto end;
841  }
842  
843  
844  if ( strcasecmp(com[0], "port")==0 ) {
845   s=QOS_PORT;
846  } else if ( strcasecmp(com[0], "802.1p")==0 ) {
847   s=QOS_DOT;
848  } else {
849   printf("Unknown QOS mode\n");
850   ret=false;
851   goto end;
852  }
853  
854  
855  i=ngadmin_setQOSMode(nga, s);
856  printErrCode(i);
857  
858  
859  end:
860  
861  return ret;
862  
863 }
864
865
866
867 static bool do_qos_set (int nb, const char **com, struct ngadmin *nga) {
868  
869  int i, p;
870  const struct swi_attr *sa;
871  bool ret=true;
872  char d=PRIO_UNSPEC, *ports=NULL;
873  
874  
875  if ( nb<2 ) {
876   printf("Usage: qos set (all <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\n");
877   ret=false;
878   goto end;
879  }
880  
881  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
882   printf("must be logged\n");
883   ret=false;
884   goto end;
885  }
886  
887  
888  ports=malloc(sa->ports*sizeof(char));
889  
890  if ( strcmp(com[0], "all")==0 ) {
891   d=parsePrio(com[1]);
892   com+=2;
893   nb-=2;
894  }
895  
896  for (i=0; i<sa->ports; ++i) {
897   ports[i]=d;
898  }
899  
900  for (i=0; i<nb; i+=2) {
901   if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
902   ports[p-1]=parsePrio(com[i+1]);
903  }
904  
905  
906  i=ngadmin_setQOSValues(nga, ports);
907  printErrCode(i);
908  
909  
910  end:
911  free(ports);
912  
913  return ret;
914  
915 }
916
917
918
919 static bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
920  
921  int i, s=0, ret=true;
922  const struct swi_attr *sa;
923  char *ports=NULL;
924  
925  
926  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
927   printf("must be logged\n");
928   ret=false;
929   goto end;
930  }
931  
932  if ( (i=ngadmin_getQOSMode(nga, &s))!=ERR_OK ) {
933   printErrCode(i);
934   ret=false;
935   goto end;
936  }
937  
938  
939  printf("QOS mode: ");
940  
941  if ( s==QOS_DOT ) {
942   printf("802.1p\n");
943   goto end;
944  } else if ( s!=QOS_PORT ) {
945   printf("unknown (%i)\n", s);
946   goto end;
947  }
948  
949  printf("port based\n");
950  
951  ports=malloc(sa->ports*sizeof(char));
952  
953  if ( (i=ngadmin_getQOSValues(nga, ports))!=ERR_OK ) {
954   printErrCode(i);
955   ret=false;
956   goto end;
957  }
958  
959  for (i=0; i<sa->ports; ++i) {
960   printf("port %i: %s\n", i+1, prio[(int)ports[i]]);
961  }
962  
963  
964  end:
965  free(ports);
966  
967  return ret;
968  
969 }
970
971
972
973 // =============================================================================
974 // quit
975
976
977 static bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
978  
979  cont=0;
980  
981  return true;
982  
983 }
984
985
986
987 // =============================================================================
988 // restart
989
990
991 static bool do_restart (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
992  
993  int i, ret=true;
994  const struct swi_attr *sa;
995  char line[16];
996  
997  
998  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
999   printf("must be logged\n");
1000   ret=false;
1001   goto end;
1002  }
1003  
1004  
1005  printf("The switch will be restarted. Continue ? [y/N]: ");
1006  fflush(stdout);
1007  
1008  if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) {
1009   i=ngadmin_restart(nga);
1010   printErrCode(i);
1011  }
1012  
1013  
1014  end:
1015  
1016  return ret;
1017  
1018 }
1019
1020
1021
1022 // =============================================================================
1023 // scan
1024
1025
1026 static bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1027  
1028  int i;
1029  const struct swi_attr *sa;
1030  
1031  
1032  if ( (i=ngadmin_scan(nga))<0 ) {
1033   printErrCode(i);
1034   return false;
1035  }
1036  
1037  sa=ngadmin_getSwitchTab(nga, &nb);
1038  displaySwitchTab(sa, nb);
1039  
1040  
1041  return true;
1042  
1043 }
1044
1045
1046
1047 // =============================================================================
1048 // stormfilter
1049
1050
1051 static bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1052  
1053  int i;
1054  const struct swi_attr *sa;
1055  
1056  
1057  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
1058   printf("must be logged\n");
1059   return false;
1060  }
1061  
1062  i=ngadmin_setStormFilterState(nga, 1);
1063  printErrCode(i);
1064  
1065  
1066  return true;
1067  
1068 }
1069
1070
1071
1072 static bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1073  
1074  int i;
1075  const struct swi_attr *sa;
1076  
1077  
1078  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
1079   printf("must be logged\n");
1080   return false;
1081  }
1082  
1083  i=ngadmin_setStormFilterState(nga, 0);
1084  printErrCode(i);
1085  
1086  
1087  return true;
1088  
1089 }
1090
1091
1092
1093 static bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) {
1094  
1095  int i, d=BITRATE_UNSPEC, p, *ports=NULL;
1096  const struct swi_attr *sa;
1097  bool ret=true;
1098  
1099  
1100  if ( nb<2 ) {
1101   printf("Usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
1102   ret=false;
1103   goto end;
1104  }
1105  
1106  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
1107   printf("must be logged\n");
1108   ret=false;
1109   goto end;
1110  }
1111  
1112  
1113  ports=malloc(sa->ports*sizeof(int));
1114  
1115  if ( strcmp(com[0], "all")==0 ) {
1116   d=parseBitrate(com[1]);
1117   com+=2;
1118   nb-=2;
1119  }
1120  
1121  for (i=0; i<sa->ports; ++i) {
1122   ports[i]=d;
1123  }
1124  
1125  for (i=0; i<nb; i+=2) {
1126   if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
1127   ports[p-1]=parseBitrate(com[i+1]);
1128  }
1129  
1130  
1131  i=ngadmin_setStormFilterValues(nga, ports);
1132  printErrCode(i);
1133  
1134  
1135  end:
1136  free(ports);
1137  
1138  return ret;
1139  
1140 }
1141
1142
1143
1144 static bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1145  
1146  int i, s, ret=true, *ports=NULL;
1147  const struct swi_attr *sa;
1148  
1149  
1150  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
1151   printf("must be logged\n");
1152   ret=false;
1153   goto end;
1154  }
1155  
1156  if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) {
1157   printErrCode(i);
1158   ret=false;
1159   goto end;
1160  }
1161  
1162  
1163  if ( !s ) {
1164   printf("storm filter is disabled\n");
1165   goto end;
1166  }
1167  
1168  printf("storm filter is enabled\n");
1169  
1170  ports=malloc(sa->ports*sizeof(int));
1171  if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) {
1172   printErrCode(i);
1173   ret=false;
1174   goto end;
1175  }
1176  
1177  for (i=0; i<sa->ports; ++i) {
1178   printf("port %i: %s\n", i+1, bitrates[ports[i]]);
1179  }
1180  
1181  end:
1182  free(ports);
1183  
1184  return ret;
1185  
1186 }
1187
1188
1189
1190 // =============================================================================
1191 // tree
1192
1193
1194 static void display_node (const struct TreeNode *tn, int depth) {
1195  
1196  int i;
1197  const struct TreeNode *s;
1198  
1199  
1200  for (i=0; i<depth; ++i) {
1201   putchar('\t');
1202  }
1203  
1204  puts(tn->name);
1205  
1206  if ( tn->sub==NULL ) return;
1207  
1208  for (s=tn->sub; s->name!=NULL; ++s) {
1209   display_node(s, depth+1);
1210  }
1211  
1212  
1213 }
1214
1215
1216 static bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
1217  
1218  
1219  display_node(&coms, 0);
1220  
1221  
1222  return true;
1223  
1224 }
1225
1226
1227
1228 // =============================================================================
1229 // vlan
1230
1231
1232 static char vlan_char (int t) {
1233  
1234  switch ( t ) {
1235   case VLAN_TAGGED: return 'T';
1236   case VLAN_UNTAGGED: return 'U'; 
1237   case VLAN_NO: return ' ';
1238   default: return '?';
1239  }
1240  
1241 }
1242
1243
1244 static bool print_vlan_pvid (struct ngadmin *nga) {
1245  
1246  unsigned short *ports=NULL;
1247  const struct swi_attr *sa;
1248  int i;
1249  bool ret=true;
1250  
1251  
1252  sa=ngadmin_getCurrentSwitch(nga);
1253  
1254  ports=malloc(sa->ports*sizeof(unsigned short));
1255  i=ngadmin_getPVID(nga, ports);
1256  if ( i!=ERR_OK ) {
1257   printErrCode(i);
1258   ret=false;
1259   goto end;
1260  }
1261  
1262  
1263  printf("PVID: \n");
1264  printf("Port\t");
1265  for (i=1; i<=sa->ports; ++i) {
1266   printf("%i\t", i);
1267  }
1268  putchar('\n');
1269  
1270  printf("VLAN\t");
1271  for (i=0; i<sa->ports; ++i) {
1272   printf("%u\t", ports[i]);
1273  }
1274  putchar('\n');
1275  
1276  
1277  end:
1278  free(ports);
1279  
1280  return ret;
1281  
1282 }
1283
1284
1285 static bool print_vlan_dot_adv (struct ngadmin *nga) {
1286  
1287  char buffer[512], *b=buffer;
1288  const struct swi_attr *sa;
1289  int i, t;
1290  
1291  
1292  sa=ngadmin_getCurrentSwitch(nga);
1293  
1294  t=sizeof(buffer);
1295  i=ngadmin_getVLANDotConf(nga, buffer, &t);
1296  if ( i!=ERR_OK ) {
1297   printErrCode(i);
1298   return false;
1299  }
1300  
1301  printf("Ports configuration: \n");
1302  printf("VLAN\t");
1303  for (i=1; i<=sa->ports; ++i) {
1304   printf("%i\t", i);
1305  }
1306  putchar('\n');
1307  
1308  while ( b-buffer<t ) {
1309   printf("%u\t", *(unsigned short*)b);b+=2;
1310   for (i=1; i<=sa->ports; ++i) {
1311    printf("%c\t", vlan_char(*b++));
1312   }
1313   putchar('\n');
1314  }
1315  
1316  
1317  return true;
1318  
1319 }
1320
1321
1322 static bool do_vlan_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1323  
1324  int i, t, ret=true;
1325  
1326  
1327  if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
1328   printf("must be logged\n");
1329   ret=false;
1330   goto end;
1331  }
1332  
1333  if ( (i=ngadmin_getVLANType(nga, &t))!=ERR_OK ) {
1334   printErrCode(i);
1335   ret=false;
1336   goto end;
1337  }
1338  
1339  
1340  printf("VLAN type: ");
1341  switch ( t ) {
1342   case VLAN_DISABLED: printf("disabled\n"); break;
1343   case VLAN_PORT_BASIC: printf("port basic\n"); break;
1344   case VLAN_PORT_ADV: printf("port advanced\n"); break;
1345   case VLAN_DOT_BASIC: printf("802.1Q basic\n"); break;
1346   
1347   case VLAN_DOT_ADV:
1348    printf("802.1Q advanced\n\n");
1349    ret=print_vlan_dot_adv(nga);
1350    putchar('\n');
1351    ret=print_vlan_pvid(nga);
1352   break;
1353   
1354   default: printf("unknown (%i)\n", t);
1355  }
1356  
1357  
1358  
1359  end:
1360  
1361  return ret;
1362  
1363 }
1364
1365
1366
1367 // =============================================================================
1368
1369
1370 COM_ROOT_START(coms)
1371  
1372  COM_START(bitrate)
1373   COM_TERM(set, do_bitrate_set, true)
1374   COM_TERM(show, do_bitrate_show, false)
1375  COM_END
1376  
1377  COM_TERM(cabletest, do_cabletest, true)
1378  
1379  COM_TERM(defaults, do_defaults, false)
1380  
1381  COM_START(firmware)
1382   COM_TERM(show, do_firmware_show, false)
1383   COM_TERM(upgrade, do_firmware_upgrade, true)
1384  COM_END
1385  
1386  COM_START(igmp)
1387   COM_TERM(set, do_igmp_set, true)
1388   COM_TERM(show, do_igmp_show, false)
1389  COM_END
1390  
1391  COM_TERM(list, do_list, false)
1392  
1393  COM_TERM(login, do_login, true)
1394  
1395  COM_START(mirror)
1396   COM_TERM(disable, do_mirror_disable, false)
1397   COM_TERM(set, do_mirror_set, true)
1398   COM_TERM(show, do_mirror_show, false)
1399  COM_END
1400  
1401  COM_START(name)
1402   COM_TERM(show, do_name_show, false)
1403   COM_TERM(set, do_name_set, true)
1404   COM_TERM(clear, do_name_clear, false)
1405  COM_END
1406  
1407  COM_START(netconf)
1408   COM_TERM(set, do_netconf_set, true)
1409  COM_END
1410  
1411  COM_START(password)
1412   COM_TERM(change, do_password_change, true)
1413   COM_TERM(set, do_password_set, true)
1414  COM_END
1415  
1416  COM_START(ports)
1417   COM_TERM(state, do_ports_state, false)
1418   COM_START(statistics)
1419    COM_TERM(reset, do_ports_statistics_reset, false)
1420    COM_TERM(show, do_ports_statistics_show, false)
1421   COM_END
1422  COM_END
1423  
1424  COM_START(qos)
1425   COM_TERM(mode, do_qos_mode, true)
1426   COM_TERM(set, do_qos_set, true)
1427   COM_TERM(show, do_qos_show, false)
1428  COM_END
1429  
1430  COM_TERM(quit, do_quit, false)
1431  
1432  COM_TERM(restart, do_restart, false)
1433  
1434  COM_TERM(scan, do_scan, false)
1435  
1436  COM_START(stormfilter)
1437   COM_TERM(enable, do_stormfilter_enable, false)
1438   COM_TERM(disable, do_stormfilter_disable, false)
1439   COM_TERM(set, do_stormfilter_set, true)
1440   COM_TERM(show, do_stormfilter_show, false)
1441  COM_END
1442  
1443  COM_TERM(tree, do_tree, false)
1444  
1445  COM_START(vlan)
1446   COM_TERM(show, do_vlan_show, false)
1447   COM_TERM(mode, NULL, true)
1448  COM_END
1449  
1450 COM_ROOT_END
1451
1452
1453