]> git.sur5r.net Git - ngadmin/blob - cli/com_vlan.c
Cli: refactor, change coding style
[ngadmin] / cli / com_vlan.c
1
2 #include "commands.h"
3
4
5
6 static char vlan_char (int t)
7 {
8         switch (t) {
9         
10         case VLAN_TAGGED:
11                 return 'T';
12         
13         case VLAN_UNTAGGED:
14                 return 'U';
15         
16         case VLAN_NO:
17                 return ' ';
18         
19         default:
20                 return '?';
21         }
22 }
23
24
25 bool do_vlan_8021q_del (int nb, const char **com, struct ngadmin *nga)
26 {
27         const struct swi_attr *sa;
28         unsigned short vlan;
29         int i;
30         
31         
32         if (nb != 1) {
33                 printf("Usage: vlan 8021q del <vlan>\n");
34                 return false;
35         }
36         
37         
38         sa = ngadmin_getCurrentSwitch(nga);
39         if (sa == NULL) {
40                 printf("must be logged\n");
41                 return false;
42         }
43         
44         vlan=strtoul(com[0], NULL, 0);
45         if (vlan < 1 || vlan > VLAN_MAX) {
46                 printf("vlan out of range\n");
47                 return false;
48         }
49         
50         i = ngadmin_VLANDestroy(nga, vlan);
51         printErrCode(i);
52         
53         
54         return true;
55 }
56
57
58 bool do_vlan_8021q_set (int nb, const char **com, struct ngadmin *nga)
59 {
60         unsigned char *ports = NULL, p, def = VLAN_UNSPEC;
61         const struct swi_attr *sa;
62         bool ret = true;
63         unsigned short vlan;
64         int i, k = 0;
65         
66         
67         if (nb == 0) {
68                 printf("Usage: vlan 802.1q set <vlan> [all unspec|no|untagged|tagged] [<port1> unspec|no|untagged|tagged ...]\n");
69                 ret = false;
70                 goto end;
71         }
72         
73         sa = ngadmin_getCurrentSwitch(nga);
74         if (sa == NULL) {
75                 printf("must be logged\n");
76                 ret = false;
77                 goto end;
78         }
79         
80         /* read vlan */
81         vlan = strtoul(com[k++], NULL, 0);
82         
83         if (vlan < 1 || vlan > VLAN_MAX) {
84                 printf("vlan out of range\n");
85                 ret = false;
86                 goto end;
87         }
88         
89         /* read defaults */
90         if (k < nb - 1 && strcasecmp(com[k], "all") == 0) {
91                 k++;
92                 if (strcasecmp(com[k], "tagged") == 0) {
93                         def = VLAN_TAGGED;
94                 } else if (strcasecmp(com[k], "untagged") == 0) {
95                         def = VLAN_UNTAGGED;
96                 } else if (strcasecmp(com[k], "no") == 0) {
97                         def = VLAN_NO;
98                 } else if (strcasecmp(com[k], "unspec") == 0) {
99                         def = VLAN_UNSPEC;
100                 } else {
101                         printf("incorrect type\n");
102                         ret = false;
103                         goto end;
104                 }
105                 k++;
106         }
107         
108         ports = malloc(sa->ports * sizeof(unsigned char));
109         
110         /* apply defaults */
111         memset(ports, def, sa->ports);
112         
113         /* apply port specifics */
114         while (k < nb - 1) {
115                 p = strtoul(com[k++], NULL, 0) - 1;
116                 if (p >= sa->ports) {
117                         printf("port out of range\n");
118                         ret = false;
119                         goto end;
120                 }
121                 if (strcasecmp(com[k], "tagged") ==0) {
122                         ports[p] = VLAN_TAGGED;
123                 } else if (strcasecmp(com[k], "untagged") == 0) {
124                         ports[p] = VLAN_UNTAGGED;
125                 } else if (strcasecmp(com[k], "no") == 0) {
126                         ports[p] = VLAN_NO;
127                 } else if (strcasecmp(com[k], "unspec") == 0) {
128                         ports[p] = VLAN_UNSPEC;
129                 } else {
130                         printf("incorrect type\n");
131                         ret = false;
132                         goto end;
133                 }
134                 k++;
135         }
136         
137         /* set conf */
138         i = ngadmin_setVLANDotConf(nga, vlan, ports);
139         printErrCode(i);
140         
141 end:
142         free(ports);
143         
144         return ret;
145 }
146
147
148 bool do_vlan_8021q_show (int nb, const char **com, struct ngadmin *nga)
149 {
150         unsigned short vl = 0, *vlans = NULL;
151         unsigned char *ports = NULL;
152         const struct swi_attr *sa;
153         int i, j, n = 16;
154         bool ret = true;
155         
156         
157         sa = ngadmin_getCurrentSwitch(nga);
158         if (sa == NULL) {
159                 printf("must be logged\n");
160                 ret = false;
161                 goto end;
162         }
163         
164         if (nb > 0)
165                 vl = strtoul(com[0], NULL, 0);
166         
167         ports = malloc(sa->ports * n * sizeof(unsigned char));
168         
169         if (vl == 0) {
170                 /* request all VLANs config */
171                 vlans = malloc(n * sizeof(unsigned short));
172                 ports = malloc(sa->ports * n * sizeof(unsigned char));
173                 i = ngadmin_getVLANDotAllConf(nga, vlans, ports, &n);
174         } else {
175                 /* request single VLAN config */
176                 ports = malloc(sa->ports * sizeof(unsigned char));
177                 i = ngadmin_getVLANDotConf(nga, vl, ports);
178         }
179         
180         if (i != ERR_OK) {
181                 printErrCode(i);
182                 ret = false;
183                 goto end;
184         }
185         
186         printf("Ports configuration: \n");
187         printf("VLAN\t");
188         for (i = 1; i <= sa->ports; i++)
189                 printf("%i\t", i);
190         putchar('\n');
191         
192         if (vl == 0) {
193                 /* show all VLANs */
194                 for (i = 0; i < n; i++) {
195                         printf("%u\t", vlans[i]);
196                         for (j = 0; j < sa->ports; j++)
197                                 printf("%c\t", vlan_char(ports[i * sa->ports + j]));
198                         putchar('\n');
199                 }
200         } else {
201                 /* show single VLAN config */
202                 printf("%u\t", vl);
203                 for (j = 0; j < sa->ports; j++)
204                         printf("%c\t", vlan_char(ports[j]));
205                 putchar('\n');
206         }
207         
208 end:
209         free(vlans);
210         free(ports);
211         
212         return ret;
213 }
214
215
216 bool do_vlan_mode_set (int nb, const char **com, struct ngadmin *nga)
217 {
218         int mode, i;
219         
220         
221         if (nb == 0) {
222                 printf(
223                 "Usage: vlan mode set <mode>\n"
224                 "1 - basic port based\n"
225                 "2 - advanced port based\n"
226                 "3 - basic 802.1Q\n"
227                 "4 - advanced 802.1Q\n"
228                 );
229                 return true;
230         }
231         
232         if (ngadmin_getCurrentSwitch(nga) == NULL) {
233                 printf("must be logged\n");
234                 return false;
235         }
236         
237         mode = strtoul(com[0], NULL, 0);
238         if (mode < 1 || mode > 4) {
239                 printf("mode out of range\n");
240                 return false;
241         }
242         
243         i = ngadmin_setVLANType(nga, mode);
244         printErrCode(i);
245         
246         
247         return true;
248 }
249
250
251 bool do_vlan_mode_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
252 {
253         int i, t, ret = true;
254         
255         
256         if (ngadmin_getCurrentSwitch(nga) == NULL) {
257                 printf("must be logged\n");
258                 ret = false;
259                 goto end;
260         }
261         
262         i = ngadmin_getVLANType(nga, &t);
263         if (i != ERR_OK) {
264                 printErrCode(i);
265                 ret = false;
266                 goto end;
267         }
268         
269         printf("VLAN type: ");
270         switch (t) {
271         
272         case VLAN_DISABLED:
273                 printf("disabled\n");
274                 break;
275         
276         case VLAN_PORT_BASIC:
277                 printf("port basic\n");
278                 break;
279         
280         case VLAN_PORT_ADV:
281                 printf("port advanced\n");
282                 break;
283         
284         case VLAN_DOT_BASIC:
285                 printf("802.1Q basic\n");
286                 break;
287         
288         case VLAN_DOT_ADV:
289                 printf("802.1Q advanced\n");
290                 break;
291         
292         default:
293                 printf("unknown (%i)\n", t);
294         }
295         
296 end:
297         
298         return ret;
299 }
300
301
302 bool do_vlan_pvid_set (int nb, const char **com, struct ngadmin *nga)
303 {
304         const struct swi_attr *sa;
305         unsigned char port;
306         unsigned short vlan;
307         int i;
308         
309         
310         if (nb != 2) {
311                 printf("Usage: vlan pvid set <port> <vlan>\n");
312                 return false;
313         }
314         
315         sa = ngadmin_getCurrentSwitch(nga);
316         if (sa == NULL) {
317                 printf("must be logged\n");
318                 return false;
319         }
320         
321         port = strtoul(com[0], NULL, 0);
322         vlan = strtoul(com[1], NULL, 0);
323         
324         if (port < 1 || port > sa->ports) {
325                 printf("port out of range\n");
326                 return false;
327         }
328         
329         if (vlan < 1 || vlan > VLAN_MAX) {
330                 printf("vlan out of range\n");
331                 return false;
332         }
333         
334         i = ngadmin_setPVID(nga, port, vlan);
335         printErrCode(i);
336         
337         
338         return true;
339 }
340
341
342 bool do_vlan_pvid_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
343 {
344         unsigned short *ports = NULL;
345         const struct swi_attr *sa;
346         int i;
347         bool ret = true;
348         
349         
350         sa = ngadmin_getCurrentSwitch(nga);
351         if (sa == NULL) {
352                 printf("must be logged\n");
353                 ret = false;
354                 goto end;
355         }
356         
357         ports = malloc(sa->ports * sizeof(unsigned short));
358         i = ngadmin_getAllPVID(nga, ports);
359         if (i != ERR_OK) {
360                 printErrCode(i);
361                 ret = false;
362                 goto end;
363         }
364         
365         printf("Port\t");
366         for (i = 1; i <= sa->ports; i++)
367                 printf("%i\t", i);
368         putchar('\n');
369         
370         printf("VLAN\t");
371         for (i = 0; i < sa->ports; i++)
372                 printf("%u\t", ports[i]);
373         putchar('\n');
374         
375 end:
376         free(ports);
377         
378         return ret;
379 }
380
381