]> git.sur5r.net Git - ngadmin/blob - cli/com_stormfilter.c
88afdae51c8c7a7ce9ba20b021d328a5e8114feb
[ngadmin] / cli / com_stormfilter.c
1
2 #include "commands.h"
3
4
5
6 bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
7  
8  int i;
9  const struct swi_attr *sa;
10  
11  
12  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
13   printf("must be logged\n");
14   return false;
15  }
16  
17  i=ngadmin_setStormFilterState(nga, 1);
18  printErrCode(i);
19  
20  
21  return true;
22  
23 }
24
25
26
27 bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
28  
29  int i;
30  const struct swi_attr *sa;
31  
32  
33  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
34   printf("must be logged\n");
35   return false;
36  }
37  
38  i=ngadmin_setStormFilterState(nga, 0);
39  printErrCode(i);
40  
41  
42  return true;
43  
44 }
45
46
47
48 bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) {
49  
50  int i, d=BITRATE_UNSPEC, p, *ports=NULL;
51  const struct swi_attr *sa;
52  bool ret=true;
53  
54  
55  if ( nb<2 ) {
56   printf("Usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
57   ret=false;
58   goto end;
59  }
60  
61  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
62   printf("must be logged\n");
63   ret=false;
64   goto end;
65  }
66  
67  
68  ports=malloc(sa->ports*sizeof(int));
69  
70  if ( strcmp(com[0], "all")==0 ) {
71   d=parseBitrate(com[1]);
72   com+=2;
73   nb-=2;
74  }
75  
76  for (i=0; i<sa->ports; ++i) {
77   ports[i]=d;
78  }
79  
80  for (i=0; i<nb; i+=2) {
81   if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
82   ports[p-1]=parseBitrate(com[i+1]);
83  }
84  
85  
86  i=ngadmin_setStormFilterValues(nga, ports);
87  printErrCode(i);
88  
89  
90  end:
91  free(ports);
92  
93  return ret;
94  
95 }
96
97
98
99 bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
100  
101  int i, s, ret=true, *ports=NULL;
102  const struct swi_attr *sa;
103  
104  
105  if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
106   printf("must be logged\n");
107   ret=false;
108   goto end;
109  }
110  
111  if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) {
112   printErrCode(i);
113   ret=false;
114   goto end;
115  }
116  
117  
118  if ( !s ) {
119   printf("storm filter is disabled\n");
120   goto end;
121  }
122  
123  printf("storm filter is enabled\n");
124  
125  ports=malloc(sa->ports*sizeof(int));
126  if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) {
127   printErrCode(i);
128   ret=false;
129   goto end;
130  }
131  
132  for (i=0; i<sa->ports; ++i) {
133   printf("port %i: %s\n", i+1, bitrates[ports[i]]);
134  }
135  
136  end:
137  free(ports);
138  
139  return ret;
140  
141 }
142
143
144