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