]> git.sur5r.net Git - ngadmin/blob - cli/commands.c
CLI: added program options.
[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 // firmware
128
129
130 static bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
131  
132  const struct swi_attr *sa;
133  bool ret=true;
134  
135  
136  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
137   printf("must be logged\n");
138   ret=false;
139   goto end;
140  }
141  
142  puts(sa->firmware);
143  
144  
145  end:
146  
147  return ret;
148  
149 }
150
151
152
153 static bool do_firmware_upgrade (int nb, const char **com UNUSED, struct ngadmin *nga) {
154  
155  const struct swi_attr *sa;
156  bool ret=true;
157  
158  
159  if ( nb!=1 ) {
160   printf("Usage: firmware upgrade <file>\n");
161   ret=false;
162  }
163  
164  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
165   printf("must be logged\n");
166   ret=false;
167   goto end;
168  }
169  
170  printf("not implemented yet\n");
171  
172  
173  end:
174  
175  return ret;
176  
177 }
178
179
180
181 // =============================================================================
182 // list
183
184
185 static bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
186  
187  int n;
188  const struct swi_attr *sa;
189  
190  
191  sa=ngadmin_getSwitchTab(nga, &n);
192  displaySwitchTab(sa, n);
193  
194  
195  return true;
196  
197 }
198
199
200
201 // =============================================================================
202 // login
203
204
205 static bool do_login (int nb, const char **com, struct ngadmin *nga) {
206  
207  int i;
208  
209  
210  if ( nb!=1 ) {
211   printf("Usage: login <num>\n");
212   return false;
213  }
214  
215  
216  i=strtol(com[0], NULL, 0);
217  i=ngadmin_login(nga, i);
218  printErrCode(i);
219  
220  
221  return true;
222  
223 }
224
225
226
227 // =============================================================================
228 // name
229
230
231 static bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
232  
233  const struct swi_attr *sa;
234  
235  
236  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
237   printf("must be logged\n");
238   return false;
239  }
240  
241  puts(sa->name);
242  
243  
244  return true;
245  
246 }
247
248
249
250 static bool do_name_set (int nb, const char **com, struct ngadmin *nga) {
251  
252  int i;
253  const struct swi_attr *sa;
254  
255  
256  if ( nb!=1 ) {
257   printf("Usage: name set <value>\n");
258   return false;
259  }
260  
261  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
262   printf("must be logged\n");
263   return false;
264  }
265  
266  i=ngadmin_setName(nga, com[0]);
267  printErrCode(i);
268  
269  
270  return true;
271  
272 }
273
274
275
276 static bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
277  
278  int i;
279  const struct swi_attr *sa;
280  
281  
282  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
283   printf("must be logged\n");
284   return false;
285  }
286  
287  i=ngadmin_setName(nga, NULL);
288  printErrCode(i);
289  
290  
291  return true;
292  
293 }
294
295
296
297 // =============================================================================
298 // password
299
300
301 static bool do_password_change (int nb, const char **com, struct ngadmin *nga) {
302  
303  int i;
304  const struct swi_attr *sa;
305  
306  
307  if ( nb!=1 ) {
308   printf("Usage: password set <value>\n");
309   return false;
310  }
311  
312  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
313   printf("must be logged\n");
314   return false;
315  }
316  
317  i=ngadmin_changePassword(nga, com[0]);
318  printErrCode(i);
319  
320  
321  return true;
322  
323 }
324
325
326
327 static bool do_password_set (int nb, const char **com, struct ngadmin *nga) {
328  
329  int i;
330  
331  
332  if ( nb!=1 ) {
333   printf("Usage: password set <value>\n");
334   return false;
335  }
336  
337  i=ngadmin_setPassword(nga, com[0]);
338  printErrCode(i);
339  
340  
341  return true;
342  
343 }
344
345
346
347 // =============================================================================
348 // ports
349
350
351 static bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
352  
353  int i;
354  const struct swi_attr *sa;
355  unsigned char *ports=NULL;
356  bool ret=true;
357  
358  
359  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
360   printf("must be logged\n");
361   ret=false;
362   goto end;
363  }
364  
365  
366  ports=malloc(sa->ports*sizeof(unsigned char));
367  if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) {
368   printErrCode(i);
369   ret=false;
370   goto end;
371  }
372  
373  for (i=0; i<sa->ports; i++) {
374   printf("port %i: ", i+1);
375   switch ( ports[i] ) {
376    case 0: printf("down"); break;
377    case SPEED_10: printf("up, 10M"); break;
378    case SPEED_100: printf("up, 100M"); break;
379    case SPEED_1000: printf("up, 1000M"); break;
380    default: printf("unknown (%i)", ports[i]);
381   }
382   putchar('\n');
383  }
384  
385  end:
386  free(ports);
387  
388  
389  return ret;
390  
391 }
392
393
394
395 static bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
396  
397  int i;
398  
399  
400  if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
401   printf("must be logged\n");
402   return false;
403  }
404  
405  i=ngadmin_resetPortsStatistics(nga);
406  printErrCode(i);
407  
408  
409  return true;
410  
411 }
412
413
414
415 static bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
416  
417  int i;
418  const struct swi_attr *sa;
419  bool ret=true;
420  struct port_stats *ps=NULL;
421  
422  
423  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
424   printf("must be logged\n");
425   ret=false;
426   goto end;
427  }
428  
429  ps=calloc(sa->ports, sizeof(struct port_stats));
430  if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) {
431   printErrCode(i);
432   ret=false;
433   goto end;
434  }
435  
436  printf("Port\tReceived\tSent\tCRC errors\n");
437  for (i=0; i<sa->ports; ++i) {
438   printf("% 4i%12llu%12llu%14llu\n", i+1, ps[i].recv, ps[i].sent, ps[i].crc);
439  }
440  
441  end:
442  free(ps);
443  
444  
445  return ret;
446  
447 }
448
449
450
451 // =============================================================================
452 // quit
453
454
455 static bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
456  
457  cont=0;
458  
459  return true;
460  
461 }
462
463
464
465 // =============================================================================
466 // scan
467
468
469 static bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
470  
471  int i;
472  const struct swi_attr *sa;
473  
474  
475  if ( (i=ngadmin_scan(nga))<0 ) {
476   printErrCode(i);
477   return false;
478  }
479  
480  sa=ngadmin_getSwitchTab(nga, &nb);
481  displaySwitchTab(sa, nb);
482  
483  
484  return true;
485  
486 }
487
488
489
490 // =============================================================================
491 // stormfilter
492
493
494 static bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
495  
496  int i;
497  const struct swi_attr *sa;
498  
499  
500  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
501   printf("must be logged\n");
502   return false;
503  }
504  
505  i=ngadmin_setStormFilterState(nga, 1);
506  printErrCode(i);
507  
508  
509  return true;
510  
511 }
512
513
514
515 static bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
516  
517  int i;
518  const struct swi_attr *sa;
519  
520  
521  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
522   printf("must be logged\n");
523   return false;
524  }
525  
526  i=ngadmin_setStormFilterState(nga, 0);
527  printErrCode(i);
528  
529  
530  return true;
531  
532 }
533
534
535
536 static bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) {
537  
538  int i, d=BITRATE_UNSPEC, p, *ports=NULL;
539  const struct swi_attr *sa;
540  bool ret=true;
541  
542  
543  if ( nb<2 ) {
544   printf("Usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
545   ret=false;
546   goto end;
547  }
548  
549  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
550   printf("must be logged\n");
551   ret=false;
552   goto end;
553  }
554  
555  
556  ports=malloc(sa->ports*sizeof(int));
557  
558  if ( strcmp(com[0], "all")==0 ) {
559   d=parseBitrate(com[1]);
560   com+=2;
561   nb-=2;
562  }
563  
564  for (i=0; i<sa->ports; ++i) {
565   ports[i]=d;
566  }
567  
568  for (i=0; i<nb; i+=2) {
569   if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
570   ports[p-1]=parseBitrate(com[i+1]);
571  }
572  
573  
574  i=ngadmin_setStormFilterValues(nga, ports);
575  printErrCode(i);
576  
577  
578  end:
579  free(ports);
580  
581  return ret;
582  
583 }
584
585
586
587 static bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
588  
589  int i, s, ret=true, *ports=NULL;
590  const struct swi_attr *sa;
591  
592  
593  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
594   printf("must be logged\n");
595   ret=false;
596   goto end;
597  }
598  
599  if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) {
600   printErrCode(i);
601   ret=false;
602   goto end;
603  }
604  
605  
606  if ( !s ) {
607   printf("storm filter is disabled\n");
608   goto end;
609  }
610  
611  printf("storm filter is enabled\n");
612  
613  ports=malloc(sa->ports*sizeof(int));
614  if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) {
615   printErrCode(i);
616   ret=false;
617   goto end;
618  }
619  
620  for (i=0; i<sa->ports; ++i) {
621   printf("port %i: %s\n", i+1, bitrates[ports[i]]);
622  }
623  
624  end:
625  free(ports);
626  
627  return ret;
628  
629 }
630
631
632
633 // =============================================================================
634 // tree
635
636
637 static void display_node (const struct TreeNode *tn, int depth) {
638  
639  int i;
640  const struct TreeNode *s;
641  
642  
643  for (i=0; i<depth; ++i) {
644   putchar('\t');
645  }
646  
647  puts(tn->name);
648  
649  if ( tn->sub==NULL ) return;
650  
651  for (s=tn->sub; s->name!=NULL; ++s) {
652   display_node(s, depth+1);
653  }
654  
655  
656 }
657
658
659 static bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
660  
661  
662  display_node(&coms, 0);
663  
664  
665  return true;
666  
667 }
668
669
670
671 // =============================================================================
672
673
674 COM_ROOT_START(coms)
675  
676  COM_START(bitrate)
677   COM_TERM(set, do_bitrate_set, true)
678   COM_TERM(show, do_bitrate_show, false)
679  COM_END
680  
681  COM_TERM(cabletest, NULL, true)
682  
683  COM_TERM(defaults, NULL, false)
684  
685  COM_START(firmware)
686   COM_TERM(show, do_firmware_show, false)
687   COM_TERM(upgrade, do_firmware_upgrade, true)
688  COM_END
689  
690  COM_TERM(list, do_list, false)
691  
692  COM_TERM(login, do_login, true)
693  
694  COM_START(mirror)
695   COM_TERM(enable, NULL, false)
696   COM_TERM(disable, NULL, false)
697   COM_TERM(set, NULL, true)
698   COM_TERM(show, NULL, false)
699  COM_END
700  
701  COM_START(name)
702   COM_TERM(show, do_name_show, false)
703   COM_TERM(set, do_name_set, true)
704   COM_TERM(clear, do_name_clear, false)
705  COM_END
706  
707  COM_START(netconf)
708   COM_TERM(show, NULL, false)
709   COM_TERM(set, NULL, true)
710  COM_END
711  
712  COM_START(password)
713   COM_TERM(change, do_password_change, true)
714   COM_TERM(set, do_password_set, true)
715  COM_END
716  
717  COM_START(ports)
718   COM_TERM(state, do_ports_state, false)
719   COM_START(statistics)
720    COM_TERM(reset, do_ports_statistics_reset, false)
721    COM_TERM(show, do_ports_statistics_show, false)
722   COM_END
723  COM_END
724  
725  COM_START(qos)
726   COM_TERM(mode, NULL, true)
727   COM_TERM(set, NULL, true)
728   COM_TERM(show, NULL, false)
729  COM_END
730  
731  COM_TERM(quit, do_quit, false)
732  
733  COM_TERM(restart, NULL, false)
734  
735  COM_TERM(scan, do_scan, false)
736  
737  COM_START(stormfilter)
738   COM_TERM(enable, do_stormfilter_enable, false)
739   COM_TERM(disable, do_stormfilter_disable, false)
740   COM_TERM(set, do_stormfilter_set, true)
741   COM_TERM(show, do_stormfilter_show, false)
742  COM_END
743  
744  COM_TERM(tree, do_tree, false)
745  
746  COM_START(vlan)
747   COM_TERM(show, NULL, false)
748   COM_TERM(mode, NULL, true)
749  COM_END
750  
751 COM_ROOT_END
752
753
754