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