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