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