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