]> git.sur5r.net Git - ngadmin/blob - cli/src/com_vlan.c
Merge branch 'autotools'
[ngadmin] / cli / src / 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 int do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga)
26 {
27         const struct swi_attr *sa;
28         unsigned short vlan;
29         int i;
30         
31         
32         if (argc != 1) {
33                 printf("usage: vlan 8021q del <vlan>\n");
34                 return 1;
35         }
36         
37         sa = ngadmin_getCurrentSwitch(nga);
38         if (sa == NULL) {
39                 printf("must be logged\n");
40                 return 1;
41         }
42         
43         vlan=strtoul(argv[0], NULL, 0);
44         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
45                 printf("vlan out of range\n");
46                 return 1;
47         }
48         
49         i = ngadmin_VLANDestroy(nga, vlan);
50         printErrCode(i);
51         
52         
53         return 0;
54 }
55
56
57 int do_vlan_port_set (int argc, const char **argv, struct ngadmin *nga)
58 {
59         unsigned char vlan, port, *ports = NULL;
60         const struct swi_attr *sa;
61         int i, k = 0, ret = 0;
62         
63         
64         if (argc < 2) {
65                 printf("usage: vlan port set [all <vlan>] [<port1> <vlan>] [<port2> <vlan>] [...]\n");
66                 ret = 1;
67                 goto end;
68         }
69         
70         sa = ngadmin_getCurrentSwitch(nga);
71         if (sa == NULL) {
72                 printf("must be logged\n");
73                 ret = 1;
74                 goto end;
75         }
76         
77         ports = malloc(sa->ports * sizeof(unsigned char));
78
79         /* read defaults */
80         vlan = 0;
81         if (strcmp(argv[k], "all") == 0) {
82                 k++;
83                 vlan = strtoul(argv[k++], NULL, 0);
84                 /* VLAN 0 is allowed and means no change */
85                 if (vlan > VLAN_PORT_MAX) {
86                         printf("vlan out of range\n");
87                         ret = 1;
88                         goto end;
89                 }
90         }
91         
92         /* apply defaults */
93         memset(ports, vlan, sa->ports);
94         
95         /* read and apply port specifics */
96         while (k < argc - 1) {
97                 /* read port */
98                 port = strtoul(argv[k++], NULL, 0);
99                 if (port < 1 || port > sa->ports) {
100                         printf("port out of range\n");
101                         ret = 1;
102                         goto end;
103                 }
104                 
105                 /* read vlan */
106                 vlan = strtoul(argv[k++], NULL, 0);
107                 /* VLAN 0 is allowed and means no change */
108                 if (vlan > VLAN_PORT_MAX) {
109                         printf("vlan out of range\n");
110                         ret = 1;
111                         goto end;
112                 }
113                 
114                 ports[port - 1] = vlan;
115         }
116         
117         /* set conf */
118         i = ngadmin_setVLANPortConf(nga, ports);
119         printErrCode(i);
120         
121 end:
122         free(ports);
123         
124         return ret;
125 }
126
127
128 int do_vlan_port_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
129 {
130         unsigned char *ports = NULL;
131         const struct swi_attr *sa;
132         int i, ret = 0;
133         
134         
135         if (argc > 0) {
136                 printf("this command takes no argument\n");
137                 ret = 1;
138                 goto end;
139         }
140         
141         sa = ngadmin_getCurrentSwitch(nga);
142         if (sa == NULL) {
143                 printf("must be logged\n");
144                 ret = 1;
145                 goto end;
146         }
147         
148         ports = malloc(sa->ports * sizeof(unsigned char));
149         
150         /* request all VLANs config */
151         i = ngadmin_getVLANPortConf(nga, ports);
152         
153         if (i != ERR_OK) {
154                 printErrCode(i);
155                 ret = 1;
156                 goto end;
157         }
158         
159         printf("Ports configuration: \n");
160         printf("Port\t");
161         for (i = 1; i <= sa->ports; i++)
162                 printf("%i\t", i);
163         putchar('\n');
164         
165         /* show all VLANs */
166         printf("VLAN\t");
167         for (i = 0; i < sa->ports; i++)
168                 printf("%u\t", ports[i]);
169         putchar('\n');
170         
171 end:
172         free(ports);
173         
174         return ret;
175 }
176
177
178 int do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga)
179 {
180         unsigned char *ports = NULL, p, def = VLAN_UNSPEC;
181         const struct swi_attr *sa;
182         unsigned short vlan;
183         int i, k = 0, ret = 0;
184         
185         
186         if (argc == 0) {
187                 printf("usage: vlan 802.1q set <vlan> [all unspec|no|untagged|tagged] [<port1> unspec|no|untagged|tagged ...]\n");
188                 ret = 1;
189                 goto end;
190         }
191         
192         sa = ngadmin_getCurrentSwitch(nga);
193         if (sa == NULL) {
194                 printf("must be logged\n");
195                 ret = 1;
196                 goto end;
197         }
198         
199         /* read vlan */
200         vlan = strtoul(argv[k++], NULL, 0);
201         
202         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
203                 printf("vlan out of range\n");
204                 ret = 1;
205                 goto end;
206         }
207         
208         /* read defaults */
209         if (k < argc - 1 && strcasecmp(argv[k], "all") == 0) {
210                 k++;
211                 if (strcasecmp(argv[k], "tagged") == 0) {
212                         def = VLAN_TAGGED;
213                 } else if (strcasecmp(argv[k], "untagged") == 0) {
214                         def = VLAN_UNTAGGED;
215                 } else if (strcasecmp(argv[k], "no") == 0) {
216                         def = VLAN_NO;
217                 } else if (strcasecmp(argv[k], "unspec") == 0) {
218                         def = VLAN_UNSPEC;
219                 } else {
220                         printf("incorrect type\n");
221                         ret = 1;
222                         goto end;
223                 }
224                 k++;
225         }
226         
227         ports = malloc(sa->ports * sizeof(unsigned char));
228         
229         /* apply defaults */
230         memset(ports, def, sa->ports);
231         
232         /* read and apply port specifics */
233         while (k < argc - 1) {
234                 p = strtoul(argv[k++], NULL, 0) - 1;
235                 if (p >= sa->ports) {
236                         printf("port out of range\n");
237                         ret = 1;
238                         goto end;
239                 }
240                 if (strcasecmp(argv[k], "tagged") ==0) {
241                         ports[p] = VLAN_TAGGED;
242                 } else if (strcasecmp(argv[k], "untagged") == 0) {
243                         ports[p] = VLAN_UNTAGGED;
244                 } else if (strcasecmp(argv[k], "no") == 0) {
245                         ports[p] = VLAN_NO;
246                 } else if (strcasecmp(argv[k], "unspec") == 0) {
247                         ports[p] = VLAN_UNSPEC;
248                 } else {
249                         printf("incorrect type\n");
250                         ret = 1;
251                         goto end;
252                 }
253                 k++;
254         }
255         
256         /* set conf */
257         i = ngadmin_setVLANDotConf(nga, vlan, ports);
258         printErrCode(i);
259         
260 end:
261         free(ports);
262         
263         return ret;
264 }
265
266
267 int do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga)
268 {
269         unsigned short vl = 0, *vlans = NULL;
270         unsigned char *ports = NULL;
271         const struct swi_attr *sa;
272         int i, j, n = 16, ret = 0;
273         
274         
275         sa = ngadmin_getCurrentSwitch(nga);
276         if (sa == NULL) {
277                 printf("must be logged\n");
278                 ret = 1;
279                 goto end;
280         }
281         
282         if (argc > 0)
283                 vl = strtoul(argv[0], NULL, 0);
284         
285         ports = malloc(sa->ports * n * sizeof(unsigned char));
286         
287         if (vl == 0) {
288                 /* request all VLANs config */
289                 vlans = malloc(n * sizeof(unsigned short));
290                 ports = malloc(sa->ports * n * sizeof(unsigned char));
291                 i = ngadmin_getVLANDotAllConf(nga, vlans, ports, &n);
292         } else {
293                 /* request single VLAN config */
294                 ports = malloc(sa->ports * sizeof(unsigned char));
295                 i = ngadmin_getVLANDotConf(nga, vl, ports);
296         }
297         
298         if (i != ERR_OK) {
299                 printErrCode(i);
300                 ret = 1;
301                 goto end;
302         }
303         
304         printf("Ports configuration: \n");
305         printf("VLAN\t");
306         for (i = 1; i <= sa->ports; i++)
307                 printf("%i\t", i);
308         putchar('\n');
309         
310         if (vl == 0) {
311                 /* show all VLANs */
312                 for (i = 0; i < n; i++) {
313                         printf("%u\t", vlans[i]);
314                         for (j = 0; j < sa->ports; j++)
315                                 printf("%c\t", vlan_char(ports[i * sa->ports + j]));
316                         putchar('\n');
317                 }
318         } else {
319                 /* show single VLAN config */
320                 printf("%u\t", vl);
321                 for (j = 0; j < sa->ports; j++)
322                         printf("%c\t", vlan_char(ports[j]));
323                 putchar('\n');
324         }
325         
326 end:
327         free(vlans);
328         free(ports);
329         
330         return ret;
331 }
332
333
334 int do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga)
335 {
336         int mode, i;
337         
338         
339         if (argc == 0) {
340                 printf(
341                 "usage: vlan mode set <mode>\n"
342                 "0 - disabled\n"
343                 "1 - basic port based\n"
344                 "2 - advanced port based\n"
345                 "3 - basic 802.1Q\n"
346                 "4 - advanced 802.1Q\n"
347                 );
348                 return 0;
349         }
350         
351         if (ngadmin_getCurrentSwitch(nga) == NULL) {
352                 printf("must be logged\n");
353                 return 1;
354         }
355         
356         mode = strtoul(argv[0], NULL, 0);
357         if (mode < VLAN_DISABLED || mode > VLAN_DOT_ADV) {
358                 printf("mode out of range\n");
359                 return 1;
360         }
361         
362         i = ngadmin_setVLANType(nga, mode);
363         printErrCode(i);
364         
365         
366         return 0;
367 }
368
369
370 int do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
371 {
372         int i, t, ret = 0;
373         
374         
375         if (argc > 0) {
376                 printf("this command takes no argument\n");
377                 ret = 1;
378                 goto end;
379         }
380         
381         if (ngadmin_getCurrentSwitch(nga) == NULL) {
382                 printf("must be logged\n");
383                 ret = 1;
384                 goto end;
385         }
386         
387         i = ngadmin_getVLANType(nga, &t);
388         if (i != ERR_OK) {
389                 printErrCode(i);
390                 ret = 1;
391                 goto end;
392         }
393         
394         printf("VLAN type: ");
395         switch (t) {
396         
397         case VLAN_DISABLED:
398                 printf("disabled\n");
399                 break;
400         
401         case VLAN_PORT_BASIC:
402                 printf("port basic\n");
403                 break;
404         
405         case VLAN_PORT_ADV:
406                 printf("port advanced\n");
407                 break;
408         
409         case VLAN_DOT_BASIC:
410                 printf("802.1Q basic\n");
411                 break;
412         
413         case VLAN_DOT_ADV:
414                 printf("802.1Q advanced\n");
415                 break;
416         
417         default:
418                 printf("unknown (%i)\n", t);
419         }
420         
421 end:
422         
423         return ret;
424 }
425
426
427 int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga)
428 {
429         const struct swi_attr *sa;
430         unsigned char port;
431         unsigned short vlan;
432         int i;
433         
434         
435         if (argc != 2) {
436                 printf("usage: vlan pvid set <port> <vlan>\n");
437                 return 1;
438         }
439         
440         sa = ngadmin_getCurrentSwitch(nga);
441         if (sa == NULL) {
442                 printf("must be logged\n");
443                 return 1;
444         }
445         
446         port = strtoul(argv[0], NULL, 0);
447         vlan = strtoul(argv[1], NULL, 0);
448         
449         if (port < 1 || port > sa->ports) {
450                 printf("port out of range\n");
451                 return 1;
452         }
453         
454         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
455                 printf("vlan out of range\n");
456                 return 1;
457         }
458         
459         i = ngadmin_setPVID(nga, port, vlan);
460         printErrCode(i);
461         
462         
463         return 0;
464 }
465
466
467 int do_vlan_pvid_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
468 {
469         unsigned short *ports = NULL;
470         const struct swi_attr *sa;
471         int i, ret = 0;
472         
473         
474         if (argc > 0) {
475                 printf("this command takes no argument\n");
476                 ret = 1;
477                 goto end;
478         }
479         
480         sa = ngadmin_getCurrentSwitch(nga);
481         if (sa == NULL) {
482                 printf("must be logged\n");
483                 ret = 1;
484                 goto end;
485         }
486         
487         ports = malloc(sa->ports * sizeof(unsigned short));
488         i = ngadmin_getAllPVID(nga, ports);
489         if (i != ERR_OK) {
490                 printErrCode(i);
491                 ret = 1;
492                 goto end;
493         }
494         
495         printf("Port\t");
496         for (i = 1; i <= sa->ports; i++)
497                 printf("%i\t", i);
498         putchar('\n');
499         
500         printf("VLAN\t");
501         for (i = 0; i < sa->ports; i++)
502                 printf("%u\t", ports[i]);
503         putchar('\n');
504         
505 end:
506         free(ports);
507         
508         return ret;
509 }
510
511