7 // =============================================================================
11 // helper function to analyse bitrate speed specifications
12 static int bitrate_analyse (int nb, const char **com, int *ports) {
18 s=parseBitrate(com[i+1]);
19 if ( strcmp(com[i], "inout")==0 ) {
22 } else if ( strcmp(com[i], "in")==0 ) {
24 } else if ( strcmp(com[i], "out")==0 ) {
38 static bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga) {
40 int i, k=0, defs[]={12, 12}, p, *ports=NULL;
41 const struct swi_attr *sa;
46 printf("Usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n");
47 printf("SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\n");
52 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
53 printf("must be logged\n");
58 ports=malloc(2*sa->ports*sizeof(int));
60 // get defaults if present
61 if ( strcmp(com[k], "all")==0 ) {
63 k+=bitrate_analyse(nb-k, &com[k], defs);
67 for (i=0; i<sa->ports; ++i) {
68 memcpy(&ports[2*i], defs, sizeof(defs));
71 // get ports specifics
73 p=strtol(com[k++], NULL, 0)-1;
74 if ( p>=0 && p<sa->ports ) {
75 k+=bitrate_analyse(nb-k, &com[k], &ports[2*p]);
79 // send it to the switch
80 i=ngadmin_setBitrateLimits(nga, ports);
93 static bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
95 int i, ret=true, *ports=NULL;
96 const struct swi_attr *sa;
99 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
100 printf("must be logged\n");
106 ports=malloc(2*sa->ports*sizeof(int));
107 if ( (i=ngadmin_getBitrateLimits(nga, ports))!=ERR_OK ) {
113 for (i=0; i<sa->ports; ++i) {
114 printf("port %i: in %s, out %s\n", i+1, bitrates[ports[2*i+0]], bitrates[ports[2*i+1]]);
126 // =============================================================================
130 static bool do_cabletest (int nb, const char **com, struct ngadmin *nga) {
133 const struct swi_attr *sa;
134 struct cabletest *ct=NULL;
139 printf("Usage: cabletest <port1> [<port2> ...]\n");
143 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
144 printf("must be logged\n");
149 ct=malloc(sa->ports*sizeof(struct cabletest));
150 memset(ct, 0, sa->ports*sizeof(struct cabletest));
153 ct[j].port=strtol(com[k++], NULL, 0);
154 if ( ct[j].port>=1 && ct[j].port<=sa->ports ) ++j;
157 i=ngadmin_cabletest(nga, ct, j);
165 for (i=0; i<j; ++i) {
166 printf("port %i: %08X %08X\n", ct[i].port, ct[i].v1, ct[i].v2);
179 // =============================================================================
183 static bool do_defaults (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
186 const struct swi_attr *sa;
190 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
191 printf("must be logged\n");
197 printf("The switch settings will be CLEARED. Continue ? [y/N]: ");
200 if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) {
201 i=ngadmin_defaults(nga);
214 // =============================================================================
218 static bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
220 const struct swi_attr *sa;
224 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
225 printf("must be logged\n");
241 static bool do_firmware_upgrade (int nb, const char **com UNUSED, struct ngadmin *nga) {
243 const struct swi_attr *sa;
248 printf("Usage: firmware upgrade <file>\n");
252 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
253 printf("must be logged\n");
258 printf("not implemented yet\n");
269 // =============================================================================
273 static bool do_igmp_set (int nb, const char **com, struct ngadmin *nga) {
280 printf("Usage: igmp set <enable> <vlan> <validate> <block>\n");
284 if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
285 printf("must be logged\n");
289 ic.enable=strtol(com[0], NULL, 0);
290 ic.vlan=strtol(com[1], NULL, 0);
291 ic.validate=strtol(com[2], NULL, 0);
292 ic.block=strtol(com[3], NULL, 0);
294 i=ngadmin_setIGMPConf(nga, &ic);
304 static bool do_igmp_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
307 const struct swi_attr *sa;
312 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
313 printf("must be logged\n");
318 i=ngadmin_getIGMPConf(nga, &ic);
326 printf("IGMP snooping enabled: %s\n", ic.enable ? "yes" : "no" );
327 printf("IGMP snooping vlan: %u\n", ic.vlan);
328 printf("Validate IGMPv3 headers: %s\n", ic.validate ? "yes" : "no" );
329 printf("Block unknown multicast addresses: %s\n", ic.block ? "yes" : "no" );
341 // =============================================================================
345 static bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
348 const struct swi_attr *sa;
351 sa=ngadmin_getSwitchTab(nga, &n);
352 displaySwitchTab(sa, n);
361 // =============================================================================
365 static bool do_login (int nb, const char **com, struct ngadmin *nga) {
371 printf("Usage: login <num>\n");
376 i=strtol(com[0], NULL, 0);
377 i=ngadmin_login(nga, i);
387 // =============================================================================
391 static bool do_mirror_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
396 if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
397 printf("must be logged\n");
402 i=ngadmin_setMirror(nga, NULL);
412 static bool do_mirror_set (int nb, const char **com, struct ngadmin *nga) {
414 const struct swi_attr *sa;
421 printf("Usage: mirror set <destination port> clone <port1> [<port2> ...]\n");
425 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
426 printf("must be logged\n");
432 ports=malloc((sa->ports+1)*sizeof(char));
433 memset(ports, 0, sa->ports+1);
435 ports[0]=strtol(com[k++], NULL, 0);
436 if ( ports[0]<1 || ports[0]>sa->ports || strcasecmp(com[k++], "clone")!=0 ) {
437 printf("syntax error\n");
444 i=strtol(com[k++], NULL, 0);
445 if ( i<1 || i>sa->ports ) {
446 printf("port out of range\n");
449 } else if ( i==ports[0] ) {
450 printf("destination port cannot be in port list\n");
458 i=ngadmin_setMirror(nga, ports);
471 static bool do_mirror_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
473 const struct swi_attr *sa;
478 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
479 printf("must be logged\n");
484 ports=malloc((sa->ports+1)*sizeof(char));
486 i=ngadmin_getMirror(nga, ports);
493 printf("port mirroring is disabled\n");
497 printf("destination: %i\n", ports[0]);
499 for (i=1; i<=sa->ports; ++i) {
517 // =============================================================================
521 static bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
523 const struct swi_attr *sa;
526 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
527 printf("must be logged\n");
540 static bool do_name_set (int nb, const char **com, struct ngadmin *nga) {
543 const struct swi_attr *sa;
547 printf("Usage: name set <value>\n");
551 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
552 printf("must be logged\n");
556 i=ngadmin_setName(nga, com[0]);
566 static bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
569 const struct swi_attr *sa;
572 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
573 printf("must be logged\n");
577 i=ngadmin_setName(nga, NULL);
587 // =============================================================================
591 static bool do_netconf_set (int nb, const char **com, struct ngadmin *nga) {
594 const struct swi_attr *sa;
600 printf("Usage: netconf set [dhcp yes|no] [ip <ip>] [mask <mask>] [gw <gw>]\n");
604 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
605 printf("must be logged\n");
610 memset(&nc, 0, sizeof(struct net_conf));
612 for (k=0; k<nb; k+=2) {
614 if ( strcasecmp(com[k], "dhcp")==0 ) {
615 if ( strcasecmp(com[k+1], "yes")==0 ) {
617 } else if ( strcasecmp(com[k+1], "no")==0 ) {
620 printf("Incorrect DHCP value\n");
625 } else if ( strcasecmp(com[k], "ip")==0 ) {
626 if ( inet_aton(com[k+1], &nc.ip)==0 ) {
627 printf("Incorrect IP value\n");
632 } else if ( strcasecmp(com[k], "mask")==0 ) {
633 if ( inet_aton(com[k+1], &nc.netmask)==0 ) { // TODO: check if it is a correct mask
634 printf("Incorrect mask value\n");
639 } else if ( strcasecmp(com[k], "gw")==0 ) {
640 if ( inet_aton(com[k+1], &nc.gw)==0 ) {
641 printf("Incorrect gateway value\n");
651 i=ngadmin_setNetConf(nga, &nc);
666 // =============================================================================
670 static bool do_password_change (int nb, const char **com, struct ngadmin *nga) {
673 const struct swi_attr *sa;
677 printf("Usage: password change <value>\n");
681 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
682 printf("must be logged\n");
686 i=ngadmin_changePassword(nga, com[0]);
696 static bool do_password_set (int nb, const char **com, struct ngadmin *nga) {
702 printf("Usage: password set <value>\n");
706 i=ngadmin_setPassword(nga, com[0]);
716 // =============================================================================
720 static bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
723 const struct swi_attr *sa;
724 unsigned char *ports=NULL;
728 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
729 printf("must be logged\n");
735 ports=malloc(sa->ports*sizeof(unsigned char));
736 if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) {
742 for (i=0; i<sa->ports; i++) {
743 printf("port %i: ", i+1);
744 switch ( ports[i] ) {
745 case 0: printf("down"); break;
746 case SPEED_10: printf("up, 10M"); break;
747 case SPEED_100: printf("up, 100M"); break;
748 case SPEED_1000: printf("up, 1000M"); break;
749 default: printf("unknown (%i)", ports[i]);
764 static bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
769 if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
770 printf("must be logged\n");
774 i=ngadmin_resetPortsStatistics(nga);
784 static bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
787 const struct swi_attr *sa;
789 struct port_stats *ps=NULL;
792 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
793 printf("must be logged\n");
798 ps=calloc(sa->ports, sizeof(struct port_stats));
799 if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) {
805 printf("Port\tReceived\tSent\tCRC errors\n");
806 for (i=0; i<sa->ports; ++i) {
807 printf("% 4i%12llu%12llu%14llu\n", i+1, ps[i].recv, ps[i].sent, ps[i].crc);
821 // =============================================================================
826 static bool do_qos_mode (int nb, const char **com, struct ngadmin *nga) {
829 const struct swi_attr *sa;
833 printf("Usage: qos mode port|802.1p\n");
837 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
838 printf("must be logged\n");
844 if ( strcasecmp(com[0], "port")==0 ) {
846 } else if ( strcasecmp(com[0], "802.1p")==0 ) {
849 printf("Unknown QOS mode\n");
855 i=ngadmin_setQOSMode(nga, s);
867 static bool do_qos_set (int nb, const char **com, struct ngadmin *nga) {
870 const struct swi_attr *sa;
872 char d=PRIO_UNSPEC, *ports=NULL;
876 printf("Usage: qos set (all <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\n");
881 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
882 printf("must be logged\n");
888 ports=malloc(sa->ports*sizeof(char));
890 if ( strcmp(com[0], "all")==0 ) {
896 for (i=0; i<sa->ports; ++i) {
900 for (i=0; i<nb; i+=2) {
901 if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
902 ports[p-1]=parsePrio(com[i+1]);
906 i=ngadmin_setQOSValues(nga, ports);
919 static bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
921 int i, s=0, ret=true;
922 const struct swi_attr *sa;
926 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
927 printf("must be logged\n");
932 if ( (i=ngadmin_getQOSMode(nga, &s))!=ERR_OK ) {
939 printf("QOS mode: ");
944 } else if ( s!=QOS_PORT ) {
945 printf("unknown (%i)\n", s);
949 printf("port based\n");
951 ports=malloc(sa->ports*sizeof(char));
953 if ( (i=ngadmin_getQOSValues(nga, ports))!=ERR_OK ) {
959 for (i=0; i<sa->ports; ++i) {
960 printf("port %i: %s\n", i+1, prio[(int)ports[i]]);
973 // =============================================================================
977 static bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
987 // =============================================================================
991 static bool do_restart (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
994 const struct swi_attr *sa;
998 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
999 printf("must be logged\n");
1005 printf("The switch will be restarted. Continue ? [y/N]: ");
1008 if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) {
1009 i=ngadmin_restart(nga);
1022 // =============================================================================
1026 static bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1029 const struct swi_attr *sa;
1032 if ( (i=ngadmin_scan(nga))<0 ) {
1037 sa=ngadmin_getSwitchTab(nga, &nb);
1038 displaySwitchTab(sa, nb);
1047 // =============================================================================
1051 static bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1054 const struct swi_attr *sa;
1057 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
1058 printf("must be logged\n");
1062 i=ngadmin_setStormFilterState(nga, 1);
1072 static bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1075 const struct swi_attr *sa;
1078 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
1079 printf("must be logged\n");
1083 i=ngadmin_setStormFilterState(nga, 0);
1093 static bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) {
1095 int i, d=BITRATE_UNSPEC, p, *ports=NULL;
1096 const struct swi_attr *sa;
1101 printf("Usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
1106 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
1107 printf("must be logged\n");
1113 ports=malloc(sa->ports*sizeof(int));
1115 if ( strcmp(com[0], "all")==0 ) {
1116 d=parseBitrate(com[1]);
1121 for (i=0; i<sa->ports; ++i) {
1125 for (i=0; i<nb; i+=2) {
1126 if ( (p=strtol(com[i], NULL, 0))<1 || p>sa->ports ) continue;
1127 ports[p-1]=parseBitrate(com[i+1]);
1131 i=ngadmin_setStormFilterValues(nga, ports);
1144 static bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1146 int i, s, ret=true, *ports=NULL;
1147 const struct swi_attr *sa;
1150 if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) {
1151 printf("must be logged\n");
1156 if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) {
1164 printf("storm filter is disabled\n");
1168 printf("storm filter is enabled\n");
1170 ports=malloc(sa->ports*sizeof(int));
1171 if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) {
1177 for (i=0; i<sa->ports; ++i) {
1178 printf("port %i: %s\n", i+1, bitrates[ports[i]]);
1190 // =============================================================================
1194 static void display_node (const struct TreeNode *tn, int depth) {
1197 const struct TreeNode *s;
1200 for (i=0; i<depth; ++i) {
1206 if ( tn->sub==NULL ) return;
1208 for (s=tn->sub; s->name!=NULL; ++s) {
1209 display_node(s, depth+1);
1216 static bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) {
1219 display_node(&coms, 0);
1228 // =============================================================================
1232 static char vlan_char (int t) {
1235 case VLAN_TAGGED: return 'T';
1236 case VLAN_UNTAGGED: return 'U';
1237 case VLAN_NO: return ' ';
1238 default: return '?';
1244 static bool print_vlan_pvid (struct ngadmin *nga) {
1246 unsigned short *ports=NULL;
1247 const struct swi_attr *sa;
1252 sa=ngadmin_getCurrentSwitch(nga);
1254 ports=malloc(sa->ports*sizeof(unsigned short));
1255 i=ngadmin_getPVID(nga, ports);
1265 for (i=1; i<=sa->ports; ++i) {
1271 for (i=0; i<sa->ports; ++i) {
1272 printf("%u\t", ports[i]);
1285 static bool print_vlan_dot_adv (struct ngadmin *nga) {
1287 char buffer[512], *b=buffer;
1288 const struct swi_attr *sa;
1292 sa=ngadmin_getCurrentSwitch(nga);
1295 i=ngadmin_getVLANDotConf(nga, buffer, &t);
1301 printf("Ports configuration: \n");
1303 for (i=1; i<=sa->ports; ++i) {
1308 while ( b-buffer<t ) {
1309 printf("%u\t", *(unsigned short*)b);b+=2;
1310 for (i=1; i<=sa->ports; ++i) {
1311 printf("%c\t", vlan_char(*b++));
1322 static bool do_vlan_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) {
1327 if ( ngadmin_getCurrentSwitch(nga)==NULL ) {
1328 printf("must be logged\n");
1333 if ( (i=ngadmin_getVLANType(nga, &t))!=ERR_OK ) {
1340 printf("VLAN type: ");
1342 case VLAN_DISABLED: printf("disabled\n"); break;
1343 case VLAN_PORT_BASIC: printf("port basic\n"); break;
1344 case VLAN_PORT_ADV: printf("port advanced\n"); break;
1345 case VLAN_DOT_BASIC: printf("802.1Q basic\n"); break;
1348 printf("802.1Q advanced\n\n");
1349 ret=print_vlan_dot_adv(nga);
1351 ret=print_vlan_pvid(nga);
1354 default: printf("unknown (%i)\n", t);
1367 // =============================================================================
1370 COM_ROOT_START(coms)
1373 COM_TERM(set, do_bitrate_set, true)
1374 COM_TERM(show, do_bitrate_show, false)
1377 COM_TERM(cabletest, do_cabletest, true)
1379 COM_TERM(defaults, do_defaults, false)
1382 COM_TERM(show, do_firmware_show, false)
1383 COM_TERM(upgrade, do_firmware_upgrade, true)
1387 COM_TERM(set, do_igmp_set, true)
1388 COM_TERM(show, do_igmp_show, false)
1391 COM_TERM(list, do_list, false)
1393 COM_TERM(login, do_login, true)
1396 COM_TERM(disable, do_mirror_disable, false)
1397 COM_TERM(set, do_mirror_set, true)
1398 COM_TERM(show, do_mirror_show, false)
1402 COM_TERM(show, do_name_show, false)
1403 COM_TERM(set, do_name_set, true)
1404 COM_TERM(clear, do_name_clear, false)
1408 COM_TERM(set, do_netconf_set, true)
1412 COM_TERM(change, do_password_change, true)
1413 COM_TERM(set, do_password_set, true)
1417 COM_TERM(state, do_ports_state, false)
1418 COM_START(statistics)
1419 COM_TERM(reset, do_ports_statistics_reset, false)
1420 COM_TERM(show, do_ports_statistics_show, false)
1425 COM_TERM(mode, do_qos_mode, true)
1426 COM_TERM(set, do_qos_set, true)
1427 COM_TERM(show, do_qos_show, false)
1430 COM_TERM(quit, do_quit, false)
1432 COM_TERM(restart, do_restart, false)
1434 COM_TERM(scan, do_scan, false)
1436 COM_START(stormfilter)
1437 COM_TERM(enable, do_stormfilter_enable, false)
1438 COM_TERM(disable, do_stormfilter_disable, false)
1439 COM_TERM(set, do_stormfilter_set, true)
1440 COM_TERM(show, do_stormfilter_show, false)
1443 COM_TERM(tree, do_tree, false)
1446 COM_TERM(show, do_vlan_show, false)
1447 COM_TERM(mode, NULL, true)