]> git.sur5r.net Git - ngadmin/blob - cli/commands.c
CLI: reunited commands in one file.
[ngadmin] / cli / commands.c
1
2 #include "commands.h"
3
4
5
6
7 // =============================================================================
8 // firmware
9
10
11 static bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
12  
13  const struct swi_attr *sa;
14  bool ret=true;
15  
16  
17  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
18   printf("must be logged\n");
19   ret=false;
20   goto end;
21  }
22  
23  puts(sa->firmware);
24  
25  
26  end:
27  
28  return ret;
29  
30 }
31
32
33
34 static bool do_firmware_upgrade (int nb, const char **com UNUSED, struct ngadmin *nga) {
35  
36  const struct swi_attr *sa;
37  bool ret=true;
38  
39  
40  if ( nb!=1 ) {
41   printf("Usage: firmware upgrade <file>\n");
42   ret=false;
43  }
44  
45  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
46   printf("must be logged\n");
47   ret=false;
48   goto end;
49  }
50  
51  printf("not implemented yet\n");
52  
53  
54  end:
55  
56  return ret;
57  
58 }
59
60
61
62 // =============================================================================
63 // list
64
65
66 static bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
67  
68  int n;
69  const struct swi_attr *sa;
70  
71  
72  sa=ngadmin_getSwitchTab(nga, &n);
73  displaySwitchTab(sa, n);
74  
75  
76  return true;
77  
78 }
79
80
81
82 // =============================================================================
83 // login
84
85
86 static bool do_login (int nb, const char **com, struct ngadmin *nga) {
87  
88  int i;
89  
90  
91  if ( nb!=1 ) {
92   printf("Usage: login <num>\n");
93   return false;
94  }
95  
96  
97  i=strtol(com[0], NULL, 0);
98  i=ngadmin_login(nga, i);
99  printErrCode(i);
100  
101  
102  return true;
103  
104 }
105
106
107
108 // =============================================================================
109 // name
110
111
112 static bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
113  
114  const struct swi_attr *sa;
115  
116  
117  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
118   printf("must be logged\n");
119   return false;
120  }
121  
122  puts(sa->name);
123  
124  
125  return true;
126  
127 }
128
129
130
131 static bool do_name_set (int nb, const char **com, struct ngadmin *nga) {
132  
133  int i;
134  const struct swi_attr *sa;
135  
136  
137  if ( nb!=1 ) {
138   printf("Usage: name set <value>\n");
139   return false;
140  }
141  
142  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
143   printf("must be logged\n");
144   return false;
145  }
146  
147  i=ngadmin_setName(nga, com[0]);
148  printErrCode(i);
149  
150  
151  return true;
152  
153 }
154
155
156
157 static bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
158  
159  int i;
160  const struct swi_attr *sa;
161  
162  
163  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
164   printf("must be logged\n");
165   return false;
166  }
167  
168  i=ngadmin_setName(nga, NULL);
169  printErrCode(i);
170  
171  
172  return true;
173  
174 }
175
176
177
178 // =============================================================================
179 // password
180
181
182 static bool do_password_change (int nb, const char **com, struct ngadmin *nga) {
183  
184  int i;
185  const struct swi_attr *sa;
186  
187  
188  if ( nb!=1 ) {
189   printf("Usage: password set <value>\n");
190   return false;
191  }
192  
193  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
194   printf("must be logged\n");
195   return false;
196  }
197  
198  i=ngadmin_changePassword(nga, com[0]);
199  printErrCode(i);
200  
201  
202  return true;
203  
204 }
205
206
207
208 static bool do_password_set (int nb, const char **com, struct ngadmin *nga) {
209  
210  int i;
211  
212  
213  if ( nb!=1 ) {
214   printf("Usage: password set <value>\n");
215   return false;
216  }
217  
218  i=ngadmin_setPassword(nga, com[0]);
219  printErrCode(i);
220  
221  
222  return true;
223  
224 }
225
226
227
228 // =============================================================================
229 // ports
230
231
232 static bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
233  
234  int i;
235  const struct swi_attr *sa;
236  unsigned char *ports=NULL;
237  bool ret=true;
238  
239  
240  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
241   printf("must be logged\n");
242   ret=false;
243   goto end;
244  }
245  
246  
247  ports=malloc(sa->ports*sizeof(unsigned char));
248  if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) {
249   printErrCode(i);
250   ret=false;
251   goto end;
252  }
253  
254  for (i=0; i<sa->ports; i++) {
255   printf("port %i: ", i+1);
256   switch ( ports[i] ) {
257    case 0: printf("down"); break;
258    case SPEED_10: printf("up, 10M"); break;
259    case SPEED_100: printf("up, 100M"); break;
260    case SPEED_1000: printf("up, 1000M"); break;
261    default: printf("unknown (%i)", ports[i]);
262   }
263   putchar('\n');
264  }
265  
266  end:
267  free(ports);
268  
269  
270  return ret;
271  
272 }
273
274
275
276 static bool do_ports_stats_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
277  
278  int i;
279  
280  
281  if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
282   printf("must be logged\n");
283   return false;
284  }
285  
286  i=ngadmin_resetPortsStatistics(nga);
287  printErrCode(i);
288  
289  
290  return true;
291  
292 }
293
294
295
296 static bool do_ports_stats_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
297  
298  int i;
299  const struct swi_attr *sa;
300  bool ret=true;
301  struct port_stats *ps=NULL;
302  
303  
304  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
305   printf("must be logged\n");
306   ret=false;
307   goto end;
308  }
309   
310  ps=calloc(sa->ports, sizeof(struct port_stats));
311  if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) {
312   printErrCode(i);
313   ret=false;
314   goto end;
315  }
316  
317  printf("Port\tReceived\tSent\t\tCRC errors\n");
318  for (i=0; i<sa->ports; ++i) {
319   printf("%i\t%8llu\t%8llu\t%8llu\n", i+1, ps[i].recv, ps[i].sent, ps[i].crc);
320  }
321  
322  end:
323  free(ps);
324  
325  
326  return ret;
327  
328 }
329
330
331
332 // =============================================================================
333 // quit
334
335
336 static bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
337  
338  cont=0;
339  
340  return true;
341  
342 }
343
344
345
346 // =============================================================================
347 // scan
348
349
350 static bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
351  
352  int i;
353  const struct swi_attr *sa;
354  
355  
356  if ( (i=ngadmin_scan(nga))<0 ) {
357   printErrCode(i);
358   return false;
359  }
360  
361  sa=ngadmin_getSwitchTab(nga, &nb);
362  displaySwitchTab(sa, nb);
363  
364  
365  return true;
366  
367 }
368
369
370
371 // =============================================================================
372 // stormfilt
373
374
375 static bool do_stormfilt_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
376  
377  int i;
378  const struct swi_attr *sa;
379  
380  
381  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
382   printf("must be logged\n");
383   return false;
384  }
385  
386  i=ngadmin_setStormFilterState(nga, 1);
387  printErrCode(i);
388  
389  
390  return true;
391  
392 }
393
394
395
396 static bool do_stormfilt_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
397  
398  int i;
399  const struct swi_attr *sa;
400  
401  
402  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
403   printf("must be logged\n");
404   return false;
405  }
406  
407  i=ngadmin_setStormFilterState(nga, 0);
408  printErrCode(i);
409  
410  
411  return true;
412  
413 }
414
415
416
417 static bool do_stormfilt_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
418  
419  int i, *ports=NULL;
420  const struct swi_attr *sa;
421  bool ret=true;
422  
423  
424  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
425   printf("must be logged\n");
426   ret=false;
427   goto end;
428  }
429  
430  
431  ports=malloc(sa->ports*sizeof(int));
432  for (i=0; i<sa->ports; ++i) {
433   ports[i]=BITRATE_NOLIMIT;
434  }
435  
436  i=ngadmin_setStormFilterValues(nga, ports);
437  printErrCode(i);
438  
439  
440  end:
441  free(ports);
442  
443  return ret;
444  
445 }
446
447
448
449 static bool do_stormfilt_set (int nb, const char **com, struct ngadmin *nga) {
450  
451  int i, p, *ports=NULL;
452  const struct swi_attr *sa;
453  bool ret=true;
454  
455  
456  if ( nb==0 ) {
457   printf("Usage: stormfilt set <port1> <speed1> [<port2> <speed2> ...]\n");
458   ret=false;
459   goto end;
460  }
461  
462  
463  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
464   printf("must be logged\n");
465   ret=false;
466   goto end;
467  }
468  
469  
470  ports=malloc(sa->ports*sizeof(int));
471  for (i=0; i<sa->ports; ++i) {
472   ports[i]=BITRATE_UNSPEC;
473  }
474  
475  for (i=0; i<nb; i+=2) {
476   if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
477   ports[p-1]=parseBitrate(com[i+1]);
478  }
479  
480  
481  i=ngadmin_setStormFilterValues(nga, ports);
482  printErrCode(i);
483  
484  
485  end:
486  free(ports);
487  
488  return ret;
489  
490 }
491
492
493
494 static bool do_stormfilt_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
495  
496  int i, s, ret=true, *ports=NULL;
497  const struct swi_attr *sa;
498  
499  
500  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
501   printf("must be logged\n");
502   ret=false;
503   goto end;
504  }
505  
506  if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) {
507   printErrCode(i);
508   ret=false;
509   goto end;
510  }
511  
512  
513  if ( !s ) {
514   printf("storm filter is disabled\n");
515   goto end;
516  }
517  
518  printf("storm filter is enabled\n");
519  
520  ports=malloc(sa->ports*sizeof(int));
521  if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) {
522   printErrCode(i);
523   ret=false;
524   goto end;
525  }
526  
527  for (i=0; i<sa->ports; ++i) {
528   printf("port %i: ", i+1);
529   printBitrate(ports[i]);
530  }
531  
532  end:
533  free(ports);
534  
535  return ret;
536  
537 }
538
539
540
541 // =============================================================================
542
543
544 COM_ROOT_START(coms)
545  
546  COM_START(firmware)
547   COM_TERM(show, do_firmware_show, false)
548   COM_TERM(upgrade, do_firmware_upgrade, true)
549  COM_END
550  
551  COM_TERM(list, do_list, false)
552  
553  COM_TERM(login, do_login, true)
554  
555  COM_START(name)
556   COM_TERM(show, do_name_show, false)
557   COM_TERM(set, do_name_set, true)
558   COM_TERM(clear, do_name_clear, false)
559  COM_END
560  
561  COM_START(password)
562   COM_TERM(change, do_password_change, true)
563   COM_TERM(set, do_password_set, true)
564  COM_END
565  
566  COM_START(ports)
567   COM_TERM(state, do_ports_state, false)
568   COM_START(stats)
569    COM_TERM(reset, do_ports_stats_reset, false)
570    COM_TERM(show, do_ports_stats_show, false)
571   COM_END
572  COM_END
573  
574  COM_TERM(quit, do_quit, false)
575  
576  COM_TERM(scan, do_scan, false)
577  
578  COM_START(stormfilt)
579   COM_TERM(enable, do_stormfilt_enable, false)
580   COM_TERM(disable, do_stormfilt_disable, false)
581   COM_TERM(reset, do_stormfilt_reset, false)
582   COM_TERM(set, do_stormfilt_set, true)
583   COM_TERM(show, do_stormfilt_show, false)
584  COM_END
585  
586 COM_ROOT_END
587
588
589