From 8d96dbb186138459117b82268407c0fb45339e8f Mon Sep 17 00:00:00 2001 From: darkcoven Date: Wed, 18 Sep 2013 22:30:15 +0200 Subject: [PATCH] Cli: refactor, change coding style --- cli/admin.c | 552 ++++++++++++++++----------------- cli/com_bitrate.c | 208 ++++++------- cli/com_cabletest.c | 90 +++--- cli/com_defaults.c | 53 ++-- cli/com_firmware.c | 94 +++--- cli/com_help.c | 30 +- cli/com_igmp.c | 118 ++++--- cli/com_list.c | 22 +- cli/com_login.c | 35 +-- cli/com_mirror.c | 225 +++++++------- cli/com_name.c | 111 ++++--- cli/com_netconf.c | 136 ++++---- cli/com_password.c | 117 ++++--- cli/com_ports.c | 190 ++++++------ cli/com_qos.c | 273 ++++++++-------- cli/com_quit.c | 12 +- cli/com_restart.c | 53 ++-- cli/com_scan.c | 35 +-- cli/com_stormfilter.c | 249 ++++++++------- cli/com_tree.c | 52 ++-- cli/com_vlan.c | 701 +++++++++++++++++++++--------------------- cli/commands.c | 236 +++++++------- cli/commands.h | 21 +- cli/common.c | 259 ++++++++-------- cli/common.h | 6 +- 25 files changed, 1862 insertions(+), 2016 deletions(-) diff --git a/cli/admin.c b/cli/admin.c index b23018d..e3fbec6 100644 --- a/cli/admin.c +++ b/cli/admin.c @@ -11,316 +11,280 @@ #include "commands.h" - - #define MAXCOM 32 -int cont=1; - - - - -const struct TreeNode* getSubCom (char **com, int n, int *t) { - - int i; - const struct TreeNode *cur, *next; - - - cur=&coms; - for (i=0; isub==NULL ) break; - - // search sub command in sub command array - for (next=cur->sub; next->name!=NULL && strcmp(next->name, com[i])!=0; ++next); - - // sub command not found, exit - if ( next->name==NULL ) break; - - // next command is now the current one - cur=next; - - } - - *t=i; - - - return cur; - +int cont = 1; + + +static const struct TreeNode* getSubCom (char **com, int n, int *t) +{ + int i; + const struct TreeNode *cur, *next; + + + cur = &coms; + for (i = 0; i < n; i++) { + /* we have reached a terminal command, exit */ + if (cur->sub == NULL) + break; + + /* search sub command in sub command array */ + for (next = cur->sub; next->name != NULL && strcmp(next->name, com[i]) != 0; next++); + + /* sub command not found, exit */ + if (next->name == NULL) + break; + + /* next command is now the current one */ + cur = next; + } + + *t = i; + + + return cur; } - - -const struct TreeNode *compcur; - - - -char* my_generator (const char* text, int state) { - - static int len; - static const struct TreeNode *tn; - const char *name; - - - if ( compcur==NULL ) { // sub command not found - return NULL; - } else if ( state==0 ) { - tn=compcur->sub; - len=strlen(text); - } - - if ( tn==NULL ) { // terminal command - return NULL; - } - - - while ( (name=tn++->name)!=NULL ) - if ( strncmp(name, text, len)==0 ) - return strdup(name); - - - return NULL; - +static const struct TreeNode *compcur; + + +static char* my_generator (const char* text, int state) +{ + static int len; + static const struct TreeNode *tn; + const char *name; + + + if (compcur == NULL) { + /* sub command not found */ + return NULL; + } else if (state == 0) { + tn = compcur->sub; + len = strlen(text); + } + + if (tn == NULL) /* terminal command */ + return NULL; + + while ((name = tn++->name) != NULL) { + if (strncmp(name, text, len) == 0) + return strdup(name); + } + + + return NULL; } - -char** my_completion (const char *text, int start, int end UNUSED) { - - char **matches=NULL; - char *line, *com[MAXCOM]; - int i, n; - - - memset(com, 0, MAXCOM*sizeof(char*)); - line=strdup(rl_line_buffer); - line[start]=0; - trim(line, start); - n=explode(line, com, MAXCOM); - free(line); - - compcur=getSubCom(com, n, &i); - - if ( i]\n", argv[0]); - goto end; - - case 't': - timeout=strtof(optarg, NULL); - break; - - case '?': - printf("Unknown option: \"%s\"\n", argv[optind-1]); - goto end; - - } - } - - argc-=optind; - argv+=optind; - - - if ( argc!=0 ) { - printf("Unknown trailing options\n"); - goto end; - } - - - memset(com, 0, MAXCOM*sizeof(char*)); - - if ( (nga=ngadmin_init(iface))==NULL ) { - fprintf(stderr, "Initialization error\n"); - goto end; - } - - // set timeout - if ( timeout>0.f ) { - tv.tv_sec=(int)timeout; - tv.tv_usec=(int)((timeout-(float)tv.tv_sec)*1.e6f); - ngadmin_setTimeout(nga, &tv); - } - - - if ( kb && ngadmin_setKeepBroadcasting(nga, true)!=ERR_OK ) goto end; - - if ( force && ngadmin_forceInterface(nga)!=ERR_OK ) goto end; - - if ( global && ngadmin_useGlobalBroadcast(nga, true)!=ERR_OK ) goto end; - - - - //rl_bind_key('\t', rl_abort); // disable auto completion - //rl_bind_key('\t', rl_complete); // enable auto-complete - rl_attempted_completion_function=my_completion; - rl_completion_entry_function=my_generator; - - - - signal(SIGTERM, handler); - signal(SIGINT, handler); - - sigsetjmp(jmpbuf, 1); - - - while ( cont ) { - - if ( (line=readline("> "))==NULL ) goto end; - trim(line, strlen(line)); - n=explode(line, com, MAXCOM); - - if ( n==0 ) { - free(line); - continue; - } else { - add_history(line); - free(line); - } - - cur=getSubCom(com, n, &i); - - if ( isub!=NULL ) { // intermediate command - printf("unknown %s subcommand\n", com[i-1]); - } else if ( !cur->hasArgs ) { // terminal command without arguments - printf("%s as no subcommand and takes no parameter\n", com[i-1]); - } else if ( cur->comfunc==NULL ) { // erroneous terminal command without function - printf("terminal command without function\n"); - } else { // terminal command with arguments, left "commands" are in fact parameters - cur->comfunc(n-i, (const char**)&com[i], nga); - } - - } else { // no command left - - if ( cur->sub!=NULL ) { // intermediate command - // print available subcommands - for (next=cur->sub; next->name!=NULL; ++next) { - printf("%s ", next->name); - } - printf("\n"); - } else if ( cur->comfunc==NULL ) { // erroneous terminal command without function - printf("terminal command without function\n"); - } else { // terminal command without arguments - cur->comfunc(0, NULL, nga); - } - - } - - - for (i=0; com[i]!=NULL; ++i) { - free(com[i]); - com[i]=NULL; - } - - } - - - end: - handler(0); - +int main (int argc, char **argv) +{ + static const struct option opts[] = { + {"keep-broadcasting", no_argument, NULL, 'b'}, + {"force-interface", no_argument, NULL, 'f'}, + {"global-broadcast", no_argument, NULL, 'g'}, + {"interface", required_argument, NULL, 'i'}, + {"help", no_argument, NULL, 'h'}, + {"timeout", required_argument, NULL, 't'}, + {0, 0, 0, 0} + }; + char *line, *com[MAXCOM]; + const char *iface = "eth0"; + float timeout = 0.f; + bool kb = false, force = false, global = false; + struct timeval tv; + const struct TreeNode *cur, *next; + int i, n; + + + tcgetattr(STDIN_FILENO, &orig_term); + current_term = orig_term; + + opterr = 0; + + while ((n = getopt_long(argc, argv, "bfgi:ht:", opts, NULL)) != -1) { + switch (n) { + + case 'b': + kb = true; + break; + + case 'f': + force = true; + break; + + case 'g': + global = true; + break; + + case 'i': + iface = optarg; + break; + + case 'h': + printf("Usage: %s [-b] [-f] [-g] [-i ]\n", argv[0]); + goto end; + + case 't': + timeout = strtof(optarg, NULL); + break; + + case '?': + printf("Unknown option: \"%s\"\n", argv[optind - 1]); + goto end; + } + } + + argc -= optind; + argv += optind; + + if (argc != 0) { + printf("Unknown trailing options\n"); + goto end; + } + + + memset(com, 0, MAXCOM * sizeof(char*)); + + nga = ngadmin_init(iface); + if (nga == NULL) { + fprintf(stderr, "Initialization error\n"); + goto end; + } + + /* set timeout */ + if (timeout > 0.f) { + tv.tv_sec = (int)timeout; + tv.tv_usec = (int)((timeout - (float)tv.tv_sec) * 1.e6f); + ngadmin_setTimeout(nga, &tv); + } + + + if (kb && ngadmin_setKeepBroadcasting(nga, true) != ERR_OK) + goto end; + + if (force && ngadmin_forceInterface(nga) != ERR_OK) + goto end; + + if (global && ngadmin_useGlobalBroadcast(nga, true) != ERR_OK) + goto end; + + rl_attempted_completion_function = my_completion; + rl_completion_entry_function = my_generator; + + signal(SIGTERM, handler); + signal(SIGINT, handler); + + sigsetjmp(jmpbuf, 1); + + while (cont) { + line = readline("> "); + if (line == NULL) + goto end; + trim(line, strlen(line)); + n = explode(line, com, MAXCOM); + + if (n == 0) { + free(line); + continue; + } else { + add_history(line); + free(line); + } + + cur = getSubCom(com, n, &i); + + if (i < n) { /* commands left unchecked */ + if (i == 0) { /* root command */ + printf("unknown command\n"); + } else if (cur->sub != NULL) { /* intermediate command */ + printf("unknown %s subcommand\n", com[i - 1]); + } else if (!cur->hasArgs) { /* terminal command without arguments */ + printf("%s as no subcommand and takes no parameter\n", com[i - 1]); + } else if (cur->comfunc == NULL) { /* erroneous terminal command without function */ + printf("terminal command without function\n"); + } else { /* terminal command with arguments, left "commands" are in fact parameters */ + cur->comfunc(n - i, (const char**)&com[i], nga); + } + } else { /* no command left */ + if (cur->sub != NULL) { /* intermediate command */ + /* print available subcommands */ + for (next = cur->sub; next->name != NULL; next++) + printf("%s ", next->name); + printf("\n"); + } else if (cur->comfunc == NULL) { /* erroneous terminal command without function */ + printf("terminal command without function\n"); + } else { /* terminal command without arguments */ + cur->comfunc(0, NULL, nga); + } + } + + for (i = 0; com[i] != NULL; i++) { + free(com[i]); + com[i] = NULL; + } + } + +end: + handler(0); } diff --git a/cli/com_bitrate.c b/cli/com_bitrate.c index 94afad8..2f7a5a9 100644 --- a/cli/com_bitrate.c +++ b/cli/com_bitrate.c @@ -2,119 +2,113 @@ #include "commands.h" - -// helper function to analyse bitrate speed specifications -static int bitrate_analyse (int nb, const char **com, int *ports) { - - int i=0, s; - - - while ( i SPEEDSPEC [ SPEEDSPEC ...]\n"); - printf("SPEEDSPEC: [inout ] [in ] [out ]\n"); - ret=false; - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - ports=malloc(2*sa->ports*sizeof(int)); - - // get defaults if present - if ( strcmp(com[k], "all")==0 ) { - ++k; - k+=bitrate_analyse(nb-k, &com[k], defs); - } - - // apply defaults - for (i=0; iports; ++i) { - memcpy(&ports[2*i], defs, sizeof(defs)); - } - - // get ports specifics - while ( k=0 && pports ) { - k+=bitrate_analyse(nb-k, &com[k], &ports[2*p]); - } - } - - // send it to the switch - i=ngadmin_setBitrateLimits(nga, ports); - printErrCode(i); - - - end: - free(ports); - - return ret; - +bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga) +{ + int i, k = 0, defs[] = {12, 12}, p, *ports = NULL; + const struct swi_attr *sa; + bool ret = true; + + if (nb < 2) { + printf( + "Usage: bitrate set [all SPEEDSPEC] SPEEDSPEC [ SPEEDSPEC ...]\n" + "SPEEDSPEC: [inout ] [in ] [out ]\n" + ); + ret = false; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ports = malloc(2 * sa->ports * sizeof(int)); + + /* get defaults if present */ + if (strcmp(com[k], "all") == 0) { + k++; + k += bitrate_analyse(nb-k, &com[k], defs); + } + + /* apply defaults */ + for (i = 0; i < sa->ports; i++) + memcpy(&ports[2 * i], defs, sizeof(defs)); + + /* get ports specifics */ + while (k < nb) { + p = strtol(com[k++], NULL, 0) - 1; + if (p >= 0 && p ports) + k += bitrate_analyse(nb - k, &com[k], &ports[2 * p]); + } + + /* send it to the switch */ + i = ngadmin_setBitrateLimits(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; } - -bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i, ret=true, *ports=NULL; - const struct swi_attr *sa; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - ports=malloc(2*sa->ports*sizeof(int)); - if ( (i=ngadmin_getBitrateLimits(nga, ports))!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - for (i=0; iports; ++i) { - printf("port %i: in %s, out %s\n", i+1, bitrates[ports[2*i+0]], bitrates[ports[2*i+1]]); - } - - end: - free(ports); - - return ret; - +bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i, ret = true, *ports = NULL; + const struct swi_attr *sa; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + + ports = malloc(2 * sa->ports * sizeof(int)); + i = ngadmin_getBitrateLimits(nga, ports); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + for (i = 0; i < sa->ports; i++) + printf("port %i: in %s, out %s\n", i + 1, bitrates[ports[2 * i + 0]], bitrates[ports[2 * i + 1]]); + +end: + free(ports); + + return ret; } diff --git a/cli/com_cabletest.c b/cli/com_cabletest.c index 521ada9..f23763f 100644 --- a/cli/com_cabletest.c +++ b/cli/com_cabletest.c @@ -2,52 +2,50 @@ #include "commands.h" - -bool do_cabletest (int nb, const char **com, struct ngadmin *nga) { - - bool ret=true; - const struct swi_attr *sa; - struct cabletest *ct=NULL; - int i, j=0, k=0; - - - if ( nb<1 ) { - printf("Usage: cabletest [ ...]\n"); - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - ct=malloc(sa->ports*sizeof(struct cabletest)); - memset(ct, 0, sa->ports*sizeof(struct cabletest)); - - while ( k=1 && ct[j].port<=sa->ports ) ++j; - } - - i=ngadmin_cabletest(nga, ct, j); - if ( i<0 ) { - printErrCode(i); - ret=false; - goto end; - } - - - for (i=0; i [ ...]\n"); + goto end; + } + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ct = malloc(sa->ports * sizeof(struct cabletest)); + memset(ct, 0, sa->ports * sizeof(struct cabletest)); + + while (k < nb) { + ct[j].port = strtol(com[k++], NULL, 0); + if (ct[j].port >= 1 && ct[j].port <= sa->ports) + j++; + } + + i = ngadmin_cabletest(nga, ct, j); + if (i < 0) { + printErrCode(i); + ret = false; + goto end; + } + + for (i = 0; i < j; i++) + printf("port %i: %08X %08X\n", ct[i].port, ct[i].v1, ct[i].v2); + +end: + free(ct); + + return ret; } diff --git a/cli/com_defaults.c b/cli/com_defaults.c index 5855332..a1c7107 100644 --- a/cli/com_defaults.c +++ b/cli/com_defaults.c @@ -2,34 +2,31 @@ #include "commands.h" - -bool do_defaults (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i, ret=true; - const struct swi_attr *sa; - char line[16]; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - printf("The switch settings will be CLEARED. Continue ? [y/N]: "); - fflush(stdout); - - if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) { - i=ngadmin_defaults(nga); - printErrCode(i); - } - - - end: - - return ret; - +bool do_defaults (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i, ret = true; + const struct swi_attr *sa; + char line[16]; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + printf("The switch settings will be CLEARED. Continue ? [y/N]: "); + fflush(stdout); + + if (fgets(line, sizeof(line), stdin) != NULL && strcasecmp(line, "y\n") == 0) { + i = ngadmin_defaults(nga); + printErrCode(i); + } + +end: + + return ret; } diff --git a/cli/com_firmware.c b/cli/com_firmware.c index f2b4732..cc931fe 100644 --- a/cli/com_firmware.c +++ b/cli/com_firmware.c @@ -2,59 +2,53 @@ #include "commands.h" - -bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - const struct swi_attr *sa; - bool ret=true; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - puts(sa->firmware); - - - end: - - return ret; - +bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + const struct swi_attr *sa; + bool ret = true; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + puts(sa->firmware); + +end: + + return ret; } - -bool do_firmware_upgrade (int nb, const char **com UNUSED, struct ngadmin *nga) { - - const struct swi_attr *sa; - bool ret=true; - int i; - - - if ( nb!=1 ) { - printf("Usage: firmware upgrade \n"); - ret=false; - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - i=ngadmin_upgradeFirmware(nga, com[0]); - printErrCode(i); - - - end: - - return ret; - +bool do_firmware_upgrade (int nb, const char **com, struct ngadmin *nga) +{ + const struct swi_attr *sa; + bool ret = true; + int i; + + + if (nb != 1) { + printf("Usage: firmware upgrade \n"); + ret = false; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + i = ngadmin_upgradeFirmware(nga, com[0]); + printErrCode(i); + +end: + + return ret; } - diff --git a/cli/com_help.c b/cli/com_help.c index 5bf84ee..e1bad0e 100644 --- a/cli/com_help.c +++ b/cli/com_help.c @@ -2,25 +2,17 @@ #include "commands.h" - - -bool do_help (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) { - - const struct TreeNode *s; - - - printf("Available commands: \n"); - - for (s=coms.sub; s->name!=NULL; ++s) { - printf("%s ", s->name); - } - putchar('\n'); - - - return true; - +bool do_help (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) +{ + const struct TreeNode *s; + + printf("Available commands: \n"); + + for (s = coms.sub; s->name != NULL; s++) + printf("%s ", s->name); + putchar('\n'); + + return true; } - - diff --git a/cli/com_igmp.c b/cli/com_igmp.c index efe39b8..3d57a03 100644 --- a/cli/com_igmp.c +++ b/cli/com_igmp.c @@ -2,71 +2,65 @@ #include "commands.h" - -bool do_igmp_set (int nb, const char **com, struct ngadmin *nga) { - - int i; - struct igmp_conf ic; - - - if ( nb!=4 ) { - printf("Usage: igmp set \n"); - return false; - } - - if ( ngadmin_getCurrentSwitch(nga)==NULL ) { - printf("must be logged\n"); - return false; - } - - ic.enable=strtol(com[0], NULL, 0); - ic.vlan=strtol(com[1], NULL, 0); - ic.validate=strtol(com[2], NULL, 0); - ic.block=strtol(com[3], NULL, 0); - - i=ngadmin_setIGMPConf(nga, &ic); - printErrCode(i); - - - return true; - +bool do_igmp_set (int nb, const char **com, struct ngadmin *nga) +{ + int i; + struct igmp_conf ic; + + + if (nb != 4) { + printf("Usage: igmp set \n"); + return false; + } + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + return false; + } + + ic.enable = strtol(com[0], NULL, 0); + ic.vlan = strtol(com[1], NULL, 0); + ic.validate = strtol(com[2], NULL, 0); + ic.block = strtol(com[3], NULL, 0); + + i = ngadmin_setIGMPConf(nga, &ic); + printErrCode(i); + + + return true; } - -bool do_igmp_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - struct igmp_conf ic; - bool ret=true; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - i=ngadmin_getIGMPConf(nga, &ic); - if ( i!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - - printf("IGMP snooping enabled: %s\n", ic.enable ? "yes" : "no" ); - printf("IGMP snooping vlan: %u\n", ic.vlan); - printf("Validate IGMPv3 headers: %s\n", ic.validate ? "yes" : "no" ); - printf("Block unknown multicast addresses: %s\n", ic.block ? "yes" : "no" ); - - - - end: - - return ret; - +bool do_igmp_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + struct igmp_conf ic; + bool ret = true; + + + sa=ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + i = ngadmin_getIGMPConf(nga, &ic); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + printf("IGMP snooping enabled: %s\n", ic.enable ? "yes" : "no" ); + printf("IGMP snooping vlan: %u\n", ic.vlan); + printf("Validate IGMPv3 headers: %s\n", ic.validate ? "yes" : "no" ); + printf("Block unknown multicast addresses: %s\n", ic.block ? "yes" : "no" ); + +end: + + return ret; } diff --git a/cli/com_list.c b/cli/com_list.c index 9630f76..820d698 100644 --- a/cli/com_list.c +++ b/cli/com_list.c @@ -2,19 +2,15 @@ #include "commands.h" - -bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int n; - const struct swi_attr *sa; - - - sa=ngadmin_getSwitchTab(nga, &n); - displaySwitchTab(sa, n); - - - return true; - +bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int n; + const struct swi_attr *sa; + + sa = ngadmin_getSwitchTab(nga, &n); + displaySwitchTab(sa, n); + + return true; } diff --git a/cli/com_login.c b/cli/com_login.c index dfd0a92..61b4f07 100644 --- a/cli/com_login.c +++ b/cli/com_login.c @@ -2,25 +2,22 @@ #include "commands.h" - -bool do_login (int nb, const char **com, struct ngadmin *nga) { - - int i; - - - if ( nb!=1 ) { - printf("Usage: login \n"); - return false; - } - - - i=strtol(com[0], NULL, 0); - i=ngadmin_login(nga, i); - printErrCode(i); - - - return true; - +bool do_login (int nb, const char **com, struct ngadmin *nga) +{ + int i; + + + if (nb != 1) { + printf("Usage: login \n"); + return false; + } + + i = strtol(com[0], NULL, 0); + i = ngadmin_login(nga, i); + printErrCode(i); + + + return true; } diff --git a/cli/com_mirror.c b/cli/com_mirror.c index 67cbb04..75ca75c 100644 --- a/cli/com_mirror.c +++ b/cli/com_mirror.c @@ -2,131 +2,118 @@ #include "commands.h" - - -bool do_mirror_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - - - if ( ngadmin_getCurrentSwitch(nga)==NULL ) { - printf("must be logged\n"); - return false; - } - - - i=ngadmin_setMirror(nga, NULL); - printErrCode(i); - - - return true; - +bool do_mirror_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + return false; + } + + + i = ngadmin_setMirror(nga, NULL); + printErrCode(i); + + + return true; } - -bool do_mirror_set (int nb, const char **com, struct ngadmin *nga) { - - const struct swi_attr *sa; - char *ports=NULL; - bool ret=true; - int i, k=0; - - - if ( nb<3 ) { - printf("Usage: mirror set clone [ ...]\n"); - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - ports=malloc((sa->ports+1)*sizeof(char)); - memset(ports, 0, sa->ports+1); - - ports[0]=strtol(com[k++], NULL, 0); - if ( ports[0]<1 || ports[0]>sa->ports || strcasecmp(com[k++], "clone")!=0 ) { - printf("syntax error\n"); - ret=false; - goto end; - } - - - while ( ksa->ports ) { - printf("port out of range\n"); - ret=false; - goto end; - } else if ( i==ports[0] ) { - printf("destination port cannot be in port list\n"); - ret=false; - goto end; - } - ports[i]=1; - } - - - i=ngadmin_setMirror(nga, ports); - printErrCode(i); - - - end: - free(ports); - - return ret; - +bool do_mirror_set (int nb, const char **com, struct ngadmin *nga) +{ + const struct swi_attr *sa; + char *ports = NULL; + bool ret = true; + int i, k = 0; + + + if (nb < 3) { + printf("Usage: mirror set clone [ ...]\n"); + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ports = malloc((sa->ports + 1) * sizeof(char)); + memset(ports, 0, sa->ports + 1); + + ports[0] = strtol(com[k++], NULL, 0); + if (ports[0] < 1 || ports[0] > sa->ports || strcasecmp(com[k++], "clone") != 0) { + printf("syntax error\n"); + ret = false; + goto end; + } + + while (k < nb) { + i = strtol(com[k++], NULL, 0); + if (i < 1 || i > sa->ports) { + printf("port out of range\n"); + ret = false; + goto end; + } else if (i == ports[0]) { + printf("destination port cannot be in port list\n"); + ret = false; + goto end; + } + ports[i] = 1; + } + + i = ngadmin_setMirror(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; } - -bool do_mirror_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - const struct swi_attr *sa; - char *ports=NULL; - int i; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - - ports=malloc((sa->ports+1)*sizeof(char)); - - i=ngadmin_getMirror(nga, ports); - if ( i!=ERR_OK ) { - printErrCode(i); - goto end; - } - - if ( ports[0]==0 ) { - printf("port mirroring is disabled\n"); - goto end; - } - - printf("destination: %i\n", ports[0]); - printf("ports: "); - for (i=1; i<=sa->ports; ++i) { - if ( ports[i] ) { - printf("%i ", i); - } - } - printf("\n"); - - - - end: - free(ports); - - return true; - +bool do_mirror_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + const struct swi_attr *sa; + char *ports = NULL; + int i; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + + ports = malloc((sa->ports + 1) * sizeof(char)); + i = ngadmin_getMirror(nga, ports); + if (i != ERR_OK) { + printErrCode(i); + goto end; + } + + if (ports[0] == 0) { + printf("port mirroring is disabled\n"); + goto end; + } + + printf("destination: %i\n", ports[0]); + printf("ports: "); + for (i = 1; i <= sa->ports; i++) { + if (ports[i]) + printf("%i ", i); + } + printf("\n"); + + +end: + free(ports); + + return true; } - diff --git a/cli/com_name.c b/cli/com_name.c index e561724..0e2524a 100644 --- a/cli/com_name.c +++ b/cli/com_name.c @@ -2,69 +2,66 @@ #include "commands.h" - -bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - const struct swi_attr *sa; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - puts(sa->name); - - - return true; - +bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + const struct swi_attr *sa; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + puts(sa->name); + + + return true; } - -bool do_name_set (int nb, const char **com, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - - - if ( nb!=1 ) { - printf("Usage: name set \n"); - return false; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - i=ngadmin_setName(nga, com[0]); - printErrCode(i); - - - return true; - +bool do_name_set (int nb, const char **com, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (nb != 1) { + printf("Usage: name set \n"); + return false; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + i = ngadmin_setName(nga, com[0]); + printErrCode(i); + + + return true; } - -bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - i=ngadmin_setName(nga, NULL); - printErrCode(i); - - - return true; - +bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + i = ngadmin_setName(nga, NULL); + printErrCode(i); + + + return true; } diff --git a/cli/com_netconf.c b/cli/com_netconf.c index e4838b8..5c165ae 100644 --- a/cli/com_netconf.c +++ b/cli/com_netconf.c @@ -2,79 +2,69 @@ #include "commands.h" - -bool do_netconf_set (int nb, const char **com, struct ngadmin *nga) { - - int i, k; - const struct swi_attr *sa; - struct net_conf nc; - bool ret=true; - - - if ( nb==0 ) { - printf("Usage: netconf set [dhcp yes|no] [ip ] [mask ] [gw ]\n"); - return false; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - - memset(&nc, 0, sizeof(struct net_conf)); - - for (k=0; k] [mask ] [gw ]\n"); + return false; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + memset(&nc, 0, sizeof(struct net_conf)); + + for (k = 0; k < nb; k += 2) { + if (strcasecmp(com[k], "dhcp") == 0) { + if (strcasecmp(com[k+1], "yes") == 0) { + nc.dhcp = true; + } else if (strcasecmp(com[k+1], "no") == 0) { + nc.dhcp = false; + } else { + printf("Incorrect DHCP value\n"); + ret = false; + goto end; + } + } else if (strcasecmp(com[k], "ip") == 0) { + if (inet_aton(com[k+1], &nc.ip) == 0) { + printf("Incorrect IP value\n"); + ret = false; + goto end; + } + } else if (strcasecmp(com[k], "mask") == 0) { + /* TODO: check if it is a correct mask */ + if (inet_aton(com[k+1], &nc.netmask) == 0) { + printf("Incorrect mask value\n"); + ret = false; + goto end; + } + } else if (strcasecmp(com[k], "gw") == 0) { + if (inet_aton(com[k+1], &nc.gw) == 0) { + printf("Incorrect gateway value\n"); + ret = false; + goto end; + } + } + } + + i = ngadmin_setNetConf(nga, &nc); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + } + +end: + + return ret; } - diff --git a/cli/com_password.c b/cli/com_password.c index 92e96c6..9264bb9 100644 --- a/cli/com_password.c +++ b/cli/com_password.c @@ -2,71 +2,64 @@ #include "commands.h" - -bool do_password_change (int nb, const char **com, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - - - if ( nb!=1 ) { - printf("Usage: password change \n"); - return false; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - i=ngadmin_changePassword(nga, com[0]); - printErrCode(i); - - - return true; - +bool do_password_change (int nb, const char **com, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (nb != 1) { + printf("Usage: password change \n"); + return false; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + i = ngadmin_changePassword(nga, com[0]); + printErrCode(i); + + + return true; } - -bool do_password_set (int nb, const char **com, struct ngadmin *nga) { - - int i; - char buf[64]; - const char *pass; - - - if ( nb>1 ) { - printf("Usage: password set []\n"); - return false; - } - - - if ( nb==0 ) { - printf("Enter password: "); - fflush(stdout); - current_term.c_lflag&=~ECHO; - tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); - pass=fgets(buf, sizeof(buf), stdin); - trim(buf, strlen(buf)); - current_term.c_lflag|=ECHO; - tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); - putchar('\n'); - } else { - pass=com[0]; - } - - - if ( pass!=NULL ) { - i=ngadmin_setPassword(nga, pass); - printErrCode(i); - } - - - - return true; - +bool do_password_set (int nb, const char **com, struct ngadmin *nga) +{ + int i; + char buf[64]; + const char *pass; + + + if (nb > 1) { + printf("Usage: password set []\n"); + return false; + } + + if (nb == 0) { + printf("Enter password: "); + fflush(stdout); + current_term.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); + pass = fgets(buf, sizeof(buf), stdin); + trim(buf, strlen(buf)); + current_term.c_lflag |= ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); + putchar('\n'); + } else { + pass = com[0]; + } + + if (pass != NULL) { + i = ngadmin_setPassword(nga, pass); + printErrCode(i); + } + + + return true; } - diff --git a/cli/com_ports.c b/cli/com_ports.c index baa8c8f..7c3fbb2 100644 --- a/cli/com_ports.c +++ b/cli/com_ports.c @@ -2,104 +2,110 @@ #include "commands.h" - -bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - unsigned char *ports=NULL; - bool ret=true; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - ports=malloc(sa->ports*sizeof(unsigned char)); - if ( (i=ngadmin_getPortsStatus(nga, ports))<0 ) { - printErrCode(i); - ret=false; - goto end; - } - - for (i=0; iports; i++) { - printf("port %i: ", i+1); - switch ( ports[i] ) { - case 0: printf("down"); break; - case SPEED_10: printf("up, 10M"); break; - case SPEED_100: printf("up, 100M"); break; - case SPEED_1000: printf("up, 1000M"); break; - default: printf("unknown (%i)", ports[i]); - } - putchar('\n'); - } - - end: - free(ports); - - - return ret; - +bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + unsigned char *ports = NULL; + bool ret = true; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ports = malloc(sa->ports * sizeof(unsigned char)); + i = ngadmin_getPortsStatus(nga, ports); + if (i < 0) { + printErrCode(i); + ret = false; + goto end; + } + + for (i = 0; i < sa->ports; i++) { + printf("port %i: ", i + 1); + switch (ports[i]) { + + case 0: + printf("down"); + break; + + case SPEED_10: + printf("up, 10M"); + break; + + case SPEED_100: + printf("up, 100M"); + break; + + case SPEED_1000: + printf("up, 1000M"); + break; + + default: + printf("unknown (%i)", ports[i]); + } + putchar('\n'); + } + +end: + free(ports); + + + return ret; } - -bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - - - if ( ngadmin_getCurrentSwitch(nga)==NULL ) { - printf("must be logged\n"); - return false; - } - - i=ngadmin_resetPortsStatistics(nga); - printErrCode(i); - - - return true; - +bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + return false; + } + + i = ngadmin_resetPortsStatistics(nga); + printErrCode(i); + + return true; } - -bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - bool ret=true; - struct port_stats *ps=NULL; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - ps=calloc(sa->ports, sizeof(struct port_stats)); - if ( (i=ngadmin_getPortsStatistics(nga, ps))<0 ) { - printErrCode(i); - ret=false; - goto end; - } - - printf("Port\tReceived\tSent\tCRC errors\n"); - for (i=0; iports; ++i) { - printf("% 4i%12llu%12llu%14llu\n", i+1, ps[i].recv, ps[i].sent, ps[i].crc); - } - - end: - free(ps); - - - return ret; - +bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + bool ret = true; + struct port_stats *ps = NULL; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ps = calloc(sa->ports, sizeof(struct port_stats)); + i = ngadmin_getPortsStatistics(nga, ps); + if (i < 0) { + printErrCode(i); + ret = false; + goto end; + } + + printf("Port\tReceived\tSent\tCRC errors\n"); + for (i = 0; i < sa->ports; i++) + printf("% 4i%12llu%12llu%14llu\n", i + 1, ps[i].recv, ps[i].sent, ps[i].crc); + +end: + free(ps); + + return ret; } - diff --git a/cli/com_qos.c b/cli/com_qos.c index b690424..4adf9b6 100644 --- a/cli/com_qos.c +++ b/cli/com_qos.c @@ -2,153 +2,144 @@ #include "commands.h" - - -bool do_qos_mode (int nb, const char **com, struct ngadmin *nga) { - - int i, s, ret=true; - const struct swi_attr *sa; - - - if ( nb==0 ) { - printf("Usage: qos mode port|802.1p\n"); - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - if ( strcasecmp(com[0], "port")==0 ) { - s=QOS_PORT; - } else if ( strcasecmp(com[0], "802.1p")==0 ) { - s=QOS_DOT; - } else { - printf("Unknown QOS mode\n"); - ret=false; - goto end; - } - - - i=ngadmin_setQOSMode(nga, s); - printErrCode(i); - - - end: - - return ret; - +bool do_qos_mode (int nb, const char **com, struct ngadmin *nga) +{ + int i, s, ret = true; + const struct swi_attr *sa; + + + if (nb == 0) { + printf("Usage: qos mode port|802.1p\n"); + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + if (strcasecmp(com[0], "port") == 0) { + s = QOS_PORT; + } else if (strcasecmp(com[0], "802.1p") == 0) { + s = QOS_DOT; + } else { + printf("Unknown QOS mode\n"); + ret = false; + goto end; + } + + i = ngadmin_setQOSMode(nga, s); + printErrCode(i); + +end: + + return ret; } - -bool do_qos_set (int nb, const char **com, struct ngadmin *nga) { - - int i, p; - const struct swi_attr *sa; - bool ret=true; - char d=PRIO_UNSPEC, *ports=NULL; - - - if ( nb<2 ) { - printf("Usage: qos set (all )|( [ ...])\n"); - ret=false; - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - ports=malloc(sa->ports*sizeof(char)); - - if ( strcmp(com[0], "all")==0 ) { - d=parsePrio(com[1]); - com+=2; - nb-=2; - } - - for (i=0; iports; ++i) { - ports[i]=d; - } - - for (i=0; isa->ports ) continue; - ports[p-1]=parsePrio(com[i+1]); - } - - - i=ngadmin_setQOSValues(nga, ports); - printErrCode(i); - - - end: - free(ports); - - return ret; - +bool do_qos_set (int nb, const char **com, struct ngadmin *nga) +{ + int i, p; + const struct swi_attr *sa; + bool ret = true; + char d = PRIO_UNSPEC, *ports = NULL; + + + if (nb < 2) { + printf("Usage: qos set (all )|( [ ...])\n"); + ret = false; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa ==NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ports = malloc(sa->ports * sizeof(char)); + + if (strcmp(com[0], "all") == 0) { + d = parsePrio(com[1]); + com += 2; + nb -= 2; + } + + for (i = 0; i < sa->ports; i++) + ports[i] = d; + + for (i = 0; i < nb; i += 2) { + p = strtol(com[i], NULL, 0); + if (p < 1 || p > sa->ports) + continue; + ports[p - 1] = parsePrio(com[i + 1]); + } + + i = ngadmin_setQOSValues(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; } - -bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i, s=0, ret=true; - const struct swi_attr *sa; - char *ports=NULL; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - if ( (i=ngadmin_getQOSMode(nga, &s))!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - - printf("QoS mode: "); - - if ( s==QOS_DOT ) { - printf("802.1p\n"); - goto end; - } else if ( s!=QOS_PORT ) { - printf("unknown (%i)\n", s); - goto end; - } - - printf("port based\n"); - - ports=malloc(sa->ports*sizeof(char)); - - if ( (i=ngadmin_getQOSValues(nga, ports))!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - for (i=0; iports; ++i) { - printf("port %i: %s\n", i+1, prio[(int)ports[i]]); - } - - - end: - free(ports); - - return ret; - +bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i, s = 0, ret = true; + const struct swi_attr *sa; + char *ports = NULL; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + i = ngadmin_getQOSMode(nga, &s); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + printf("QoS mode: "); + switch (s) { + + case QOS_DOT: + printf("802.1p\n"); + goto end; + + case QOS_PORT: + printf("port based\n"); + break; + + default: + printf("unknown (%i)\n", s); + goto end; + } + + ports = malloc(sa->ports * sizeof(char)); + i = ngadmin_getQOSValues(nga, ports); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + for (i = 0; i < sa->ports; i++) + printf("port %i: %s\n", i + 1, prio[(int)ports[i]]); + +end: + free(ports); + + return ret; } - - diff --git a/cli/com_quit.c b/cli/com_quit.c index 64c7824..96cb069 100644 --- a/cli/com_quit.c +++ b/cli/com_quit.c @@ -2,13 +2,11 @@ #include "commands.h" - -bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) { - - cont=0; - - return true; - +bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) +{ + cont = 0; + + return true; } diff --git a/cli/com_restart.c b/cli/com_restart.c index 8ad5845..9c001d1 100644 --- a/cli/com_restart.c +++ b/cli/com_restart.c @@ -2,34 +2,31 @@ #include "commands.h" - -bool do_restart (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) { - - int i, ret=true; - const struct swi_attr *sa; - char line[16]; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - printf("The switch will be restarted. Continue ? [y/N]: "); - fflush(stdout); - - if ( fgets(line, sizeof(line), stdin)!=NULL && strcasecmp(line, "y\n")==0 ) { - i=ngadmin_restart(nga); - printErrCode(i); - } - - - end: - - return ret; - +bool do_restart (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) +{ + int i, ret = true; + const struct swi_attr *sa; + char line[16]; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + printf("The switch will be restarted. Continue ? [y/N]: "); + fflush(stdout); + + if (fgets(line, sizeof(line), stdin) != NULL && strcasecmp(line, "y\n") == 0) { + i = ngadmin_restart(nga); + printErrCode(i); + } + +end: + + return ret; } diff --git a/cli/com_scan.c b/cli/com_scan.c index 98744ff..9d9bb13 100644 --- a/cli/com_scan.c +++ b/cli/com_scan.c @@ -2,24 +2,23 @@ #include "commands.h" - -bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - - - if ( (i=ngadmin_scan(nga))<0 ) { - printErrCode(i); - return false; - } - - sa=ngadmin_getSwitchTab(nga, &nb); - displaySwitchTab(sa, nb); - - - return true; - +bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + i = ngadmin_scan(nga); + if (i < 0) { + printErrCode(i); + return false; + } + + sa = ngadmin_getSwitchTab(nga, &nb); + displaySwitchTab(sa, nb); + + + return true; } diff --git a/cli/com_stormfilter.c b/cli/com_stormfilter.c index 88afdae..76076bf 100644 --- a/cli/com_stormfilter.c +++ b/cli/com_stormfilter.c @@ -2,143 +2,136 @@ #include "commands.h" - -bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - i=ngadmin_setStormFilterState(nga, 1); - printErrCode(i); - - - return true; - +bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + i = ngadmin_setStormFilterState(nga, 1); + printErrCode(i); + + + return true; } - -bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - i=ngadmin_setStormFilterState(nga, 0); - printErrCode(i); - - - return true; - +bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + i = ngadmin_setStormFilterState(nga, 0); + printErrCode(i); + + + return true; } - -bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) { - - int i, d=BITRATE_UNSPEC, p, *ports=NULL; - const struct swi_attr *sa; - bool ret=true; - - - if ( nb<2 ) { - printf("Usage: stormfilt set (all )|( [ ...])\n"); - ret=false; - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - - ports=malloc(sa->ports*sizeof(int)); - - if ( strcmp(com[0], "all")==0 ) { - d=parseBitrate(com[1]); - com+=2; - nb-=2; - } - - for (i=0; iports; ++i) { - ports[i]=d; - } - - for (i=0; isa->ports ) continue; - ports[p-1]=parseBitrate(com[i+1]); - } - - - i=ngadmin_setStormFilterValues(nga, ports); - printErrCode(i); - - - end: - free(ports); - - return ret; - +bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga) +{ + int i, d = BITRATE_UNSPEC, p, *ports = NULL; + const struct swi_attr *sa; + bool ret = true; + + + if (nb < 2) { + printf("Usage: stormfilt set (all )|( [ ...])\n"); + ret = false; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ports=malloc(sa->ports * sizeof(int)); + + if (strcmp(com[0], "all") == 0) { + d = parseBitrate(com[1]); + com += 2; + nb -= 2; + } + + for (i = 0; i < sa->ports; i++) + ports[i] = d; + + for (i = 0; i < nb; i += 2) { + p = strtol(com[i], NULL, 0); + if (p < 1 || p > sa->ports) + continue; + ports[p - 1] = parseBitrate(com[i + 1]); + } + + i = ngadmin_setStormFilterValues(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; } - -bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i, s, ret=true, *ports=NULL; - const struct swi_attr *sa; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - if ( (i=ngadmin_getStormFilterState(nga, &s))!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - - if ( !s ) { - printf("storm filter is disabled\n"); - goto end; - } - - printf("storm filter is enabled\n"); - - ports=malloc(sa->ports*sizeof(int)); - if ( (i=ngadmin_getStormFilterValues(nga, ports))!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - for (i=0; iports; ++i) { - printf("port %i: %s\n", i+1, bitrates[ports[i]]); - } - - end: - free(ports); - - return ret; - +bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i, s, ret = true, *ports = NULL; + const struct swi_attr *sa; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + i = ngadmin_getStormFilterState(nga, &s); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + if (!s) { + printf("storm filter is disabled\n"); + goto end; + } + + printf("storm filter is enabled\n"); + + ports = malloc(sa->ports * sizeof(int)); + i = ngadmin_getStormFilterValues(nga, ports); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + for (i = 0; i < sa->ports; i++) + printf("port %i: %s\n", i + 1, bitrates[ports[i]]); + +end: + free(ports); + + return ret; } - diff --git a/cli/com_tree.c b/cli/com_tree.c index 200ac41..cad5d57 100644 --- a/cli/com_tree.c +++ b/cli/com_tree.c @@ -2,41 +2,29 @@ #include "commands.h" - - -static void display_node (const struct TreeNode *tn, int depth) { - - int i; - const struct TreeNode *s; - - - for (i=0; iname); - - if ( tn->sub==NULL ) return; - - for (s=tn->sub; s->name!=NULL; ++s) { - display_node(s, depth+1); - } - - +static void display_node (const struct TreeNode *tn, int depth) +{ + int i; + const struct TreeNode *s; + + + for (i = 0; i < depth; i++) + putchar('\t'); + puts(tn->name); + + if (tn->sub == NULL) + return; + + for (s = tn->sub; s->name != NULL; s++) + display_node(s, depth + 1); } - -bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) { - - - display_node(&coms, 0); - - - return true; - +bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED) +{ + display_node(&coms, 0); + + return true; } - - diff --git a/cli/com_vlan.c b/cli/com_vlan.c index 90bbfca..0f7014e 100644 --- a/cli/com_vlan.c +++ b/cli/com_vlan.c @@ -3,376 +3,379 @@ - -static char vlan_char (int t) { - - switch ( t ) { - case VLAN_TAGGED: return 'T'; - case VLAN_UNTAGGED: return 'U'; - case VLAN_NO: return ' '; - default: return '?'; - } - +static char vlan_char (int t) +{ + switch (t) { + + case VLAN_TAGGED: + return 'T'; + + case VLAN_UNTAGGED: + return 'U'; + + case VLAN_NO: + return ' '; + + default: + return '?'; + } } - -bool do_vlan_8021q_del (int nb, const char **com, struct ngadmin *nga) { - - const struct swi_attr *sa; - unsigned short vlan; - int i; - - - if ( nb!=1 ) { - printf("Usage: vlan 8021q del \n"); - return false; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - vlan=strtoul(com[0], NULL, 0); - - if ( vlan<1 || vlan>VLAN_MAX ) { - printf("vlan out of range\n"); - return false; - } - - - i=ngadmin_VLANDestroy(nga, vlan); - printErrCode(i); - - - return true; - +bool do_vlan_8021q_del (int nb, const char **com, struct ngadmin *nga) +{ + const struct swi_attr *sa; + unsigned short vlan; + int i; + + + if (nb != 1) { + printf("Usage: vlan 8021q del \n"); + return false; + } + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + vlan=strtoul(com[0], NULL, 0); + if (vlan < 1 || vlan > VLAN_MAX) { + printf("vlan out of range\n"); + return false; + } + + i = ngadmin_VLANDestroy(nga, vlan); + printErrCode(i); + + + return true; } - -bool do_vlan_8021q_set (int nb, const char **com, struct ngadmin *nga) { - - unsigned char *ports=NULL, p, def=VLAN_UNSPEC; - const struct swi_attr *sa; - bool ret=true; - unsigned short vlan; - int i, k=0; - - - if ( nb==0 ) { - printf("Usage: vlan 802.1q set [all unspec|no|untagged|tagged] [ unspec|no|untagged|tagged ...]\n"); - ret=false; - goto end; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - // read vlan - vlan=strtoul(com[k++], NULL, 0); - - if ( vlan<1 || vlan>VLAN_MAX ) { - printf("vlan out of range\n"); - ret=false; - goto end; - } - - - // read defaults - if ( kports*sizeof(unsigned char)); - - // apply defaults - memset(ports, def, sa->ports); - - // apply port specifics - while ( k=sa->ports ) { - printf("port out of range\n"); - ret=false; - goto end; - } - if ( strcasecmp(com[k], "tagged")==0 ) ports[p]=VLAN_TAGGED; - else if ( strcasecmp(com[k], "untagged")==0 ) ports[p]=VLAN_UNTAGGED; - else if ( strcasecmp(com[k], "no")==0 ) ports[p]=VLAN_NO; - else if ( strcasecmp(com[k], "unspec")==0 ) ports[p]=VLAN_UNSPEC; - else { - printf("incorrect type\n"); - ret=false; - goto end; - } - ++k; - } - - - // set conf - i=ngadmin_setVLANDotConf(nga, vlan, ports); - printErrCode(i); - - - end: - free(ports); - - - return ret; - +bool do_vlan_8021q_set (int nb, const char **com, struct ngadmin *nga) +{ + unsigned char *ports = NULL, p, def = VLAN_UNSPEC; + const struct swi_attr *sa; + bool ret = true; + unsigned short vlan; + int i, k = 0; + + + if (nb == 0) { + printf("Usage: vlan 802.1q set [all unspec|no|untagged|tagged] [ unspec|no|untagged|tagged ...]\n"); + ret = false; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + /* read vlan */ + vlan = strtoul(com[k++], NULL, 0); + + if (vlan < 1 || vlan > VLAN_MAX) { + printf("vlan out of range\n"); + ret = false; + goto end; + } + + /* read defaults */ + if (k < nb - 1 && strcasecmp(com[k], "all") == 0) { + k++; + if (strcasecmp(com[k], "tagged") == 0) { + def = VLAN_TAGGED; + } else if (strcasecmp(com[k], "untagged") == 0) { + def = VLAN_UNTAGGED; + } else if (strcasecmp(com[k], "no") == 0) { + def = VLAN_NO; + } else if (strcasecmp(com[k], "unspec") == 0) { + def = VLAN_UNSPEC; + } else { + printf("incorrect type\n"); + ret = false; + goto end; + } + k++; + } + + ports = malloc(sa->ports * sizeof(unsigned char)); + + /* apply defaults */ + memset(ports, def, sa->ports); + + /* apply port specifics */ + while (k < nb - 1) { + p = strtoul(com[k++], NULL, 0) - 1; + if (p >= sa->ports) { + printf("port out of range\n"); + ret = false; + goto end; + } + if (strcasecmp(com[k], "tagged") ==0) { + ports[p] = VLAN_TAGGED; + } else if (strcasecmp(com[k], "untagged") == 0) { + ports[p] = VLAN_UNTAGGED; + } else if (strcasecmp(com[k], "no") == 0) { + ports[p] = VLAN_NO; + } else if (strcasecmp(com[k], "unspec") == 0) { + ports[p] = VLAN_UNSPEC; + } else { + printf("incorrect type\n"); + ret = false; + goto end; + } + k++; + } + + /* set conf */ + i = ngadmin_setVLANDotConf(nga, vlan, ports); + printErrCode(i); + +end: + free(ports); + + return ret; } - -bool do_vlan_8021q_show (int nb, const char **com, struct ngadmin *nga) { - - unsigned short vl=0, *vlans=NULL; - unsigned char *ports=NULL; - const struct swi_attr *sa; - int i, j, n=16; - bool ret=true; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - if ( nb>0 ) { - vl=strtoul(com[0], NULL, 0); - } - - - ports=malloc(sa->ports*n*sizeof(unsigned char)); - - if ( vl==0 ) { - vlans=malloc(n*sizeof(unsigned short)); - ports=malloc(sa->ports*n*sizeof(unsigned char)); - i=ngadmin_getVLANDotAllConf(nga, vlans, ports, &n); - } else { - ports=malloc(sa->ports*sizeof(unsigned char)); - i=ngadmin_getVLANDotConf(nga, vl, ports); - } - - if ( i!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - - printf("Ports configuration: \n"); - printf("VLAN\t"); - for (i=1; i<=sa->ports; ++i) { - printf("%i\t", i); - } - putchar('\n'); - - if ( vl==0 ) { - - for (i=0; iports; ++j) { - printf("%c\t", vlan_char(ports[i*sa->ports+j])); - } - putchar('\n'); - } - - } else { - - printf("%u\t", vl); - for (j=0; jports; ++j) { - printf("%c\t", vlan_char(ports[j])); - } - putchar('\n'); - - } - - - end: - free(vlans); - free(ports); - - - return ret; - +bool do_vlan_8021q_show (int nb, const char **com, struct ngadmin *nga) +{ + unsigned short vl = 0, *vlans = NULL; + unsigned char *ports = NULL; + const struct swi_attr *sa; + int i, j, n = 16; + bool ret = true; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + if (nb > 0) + vl = strtoul(com[0], NULL, 0); + + ports = malloc(sa->ports * n * sizeof(unsigned char)); + + if (vl == 0) { + /* request all VLANs config */ + vlans = malloc(n * sizeof(unsigned short)); + ports = malloc(sa->ports * n * sizeof(unsigned char)); + i = ngadmin_getVLANDotAllConf(nga, vlans, ports, &n); + } else { + /* request single VLAN config */ + ports = malloc(sa->ports * sizeof(unsigned char)); + i = ngadmin_getVLANDotConf(nga, vl, ports); + } + + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + printf("Ports configuration: \n"); + printf("VLAN\t"); + for (i = 1; i <= sa->ports; i++) + printf("%i\t", i); + putchar('\n'); + + if (vl == 0) { + /* show all VLANs */ + for (i = 0; i < n; i++) { + printf("%u\t", vlans[i]); + for (j = 0; j < sa->ports; j++) + printf("%c\t", vlan_char(ports[i * sa->ports + j])); + putchar('\n'); + } + } else { + /* show single VLAN config */ + printf("%u\t", vl); + for (j = 0; j < sa->ports; j++) + printf("%c\t", vlan_char(ports[j])); + putchar('\n'); + } + +end: + free(vlans); + free(ports); + + return ret; } - -bool do_vlan_mode_set (int nb, const char **com, struct ngadmin *nga) { - - int mode, i; - - - if ( nb==0 ) { - printf("Usage: vlan mode set \n"); - printf("1 - basic port based\n"); - printf("2 - advanced port based\n"); - printf("3 - basic 802.1Q\n"); - printf("4 - advanced 802.1Q\n"); - return true; - } - - if ( ngadmin_getCurrentSwitch(nga)==NULL ) { - printf("must be logged\n"); - return false; - } - - - mode=strtoul(com[0], NULL, 0); - - if ( mode<1 || mode>4 ) { - printf("mode out of range\n"); - return false; - } - - - i=ngadmin_setVLANType(nga, mode); - printErrCode(i); - - - return true; - +bool do_vlan_mode_set (int nb, const char **com, struct ngadmin *nga) +{ + int mode, i; + + + if (nb == 0) { + printf( + "Usage: vlan mode set \n" + "1 - basic port based\n" + "2 - advanced port based\n" + "3 - basic 802.1Q\n" + "4 - advanced 802.1Q\n" + ); + return true; + } + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + return false; + } + + mode = strtoul(com[0], NULL, 0); + if (mode < 1 || mode > 4) { + printf("mode out of range\n"); + return false; + } + + i = ngadmin_setVLANType(nga, mode); + printErrCode(i); + + + return true; } - -bool do_vlan_mode_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - int i, t, ret=true; - - - if ( ngadmin_getCurrentSwitch(nga)==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - if ( (i=ngadmin_getVLANType(nga, &t))!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - printf("VLAN type: "); - switch ( t ) { - case VLAN_DISABLED: printf("disabled\n"); break; - case VLAN_PORT_BASIC: printf("port basic\n"); break; - case VLAN_PORT_ADV: printf("port advanced\n"); break; - case VLAN_DOT_BASIC: printf("802.1Q basic\n"); break; - case VLAN_DOT_ADV:printf("802.1Q advanced\n");break; - default: printf("unknown (%i)\n", t); - } - - - end: - - return ret; - +bool do_vlan_mode_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + int i, t, ret = true; + + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + i = ngadmin_getVLANType(nga, &t); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + printf("VLAN type: "); + switch (t) { + + case VLAN_DISABLED: + printf("disabled\n"); + break; + + case VLAN_PORT_BASIC: + printf("port basic\n"); + break; + + case VLAN_PORT_ADV: + printf("port advanced\n"); + break; + + case VLAN_DOT_BASIC: + printf("802.1Q basic\n"); + break; + + case VLAN_DOT_ADV: + printf("802.1Q advanced\n"); + break; + + default: + printf("unknown (%i)\n", t); + } + +end: + + return ret; } - -bool do_vlan_pvid_set (int nb, const char **com, struct ngadmin *nga) { - - const struct swi_attr *sa; - unsigned char port; - unsigned short vlan; - int i; - - - if ( nb!=2 ) { - printf("Usage: vlan pvid set \n"); - return false; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - port=strtoul(com[0], NULL, 0); - vlan=strtoul(com[1], NULL, 0); - - if ( port<1 || port>sa->ports ) { - printf("port out of range\n"); - return false; - } - - if ( vlan<1 || vlan>VLAN_MAX ) { - printf("vlan out of range\n"); - return false; - } - - - i=ngadmin_setPVID(nga, port, vlan); - printErrCode(i); - - - return true; - +bool do_vlan_pvid_set (int nb, const char **com, struct ngadmin *nga) +{ + const struct swi_attr *sa; + unsigned char port; + unsigned short vlan; + int i; + + + if (nb != 2) { + printf("Usage: vlan pvid set \n"); + return false; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return false; + } + + port = strtoul(com[0], NULL, 0); + vlan = strtoul(com[1], NULL, 0); + + if (port < 1 || port > sa->ports) { + printf("port out of range\n"); + return false; + } + + if (vlan < 1 || vlan > VLAN_MAX) { + printf("vlan out of range\n"); + return false; + } + + i = ngadmin_setPVID(nga, port, vlan); + printErrCode(i); + + + return true; } - -bool do_vlan_pvid_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) { - - unsigned short *ports=NULL; - const struct swi_attr *sa; - int i; - bool ret=true; - - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - ret=false; - goto end; - } - - ports=malloc(sa->ports*sizeof(unsigned short)); - i=ngadmin_getAllPVID(nga, ports); - if ( i!=ERR_OK ) { - printErrCode(i); - ret=false; - goto end; - } - - - printf("Port\t"); - for (i=1; i<=sa->ports; ++i) { - printf("%i\t", i); - } - putchar('\n'); - - printf("VLAN\t"); - for (i=0; iports; ++i) { - printf("%u\t", ports[i]); - } - putchar('\n'); - - - end: - free(ports); - - return ret; - +bool do_vlan_pvid_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga) +{ + unsigned short *ports = NULL; + const struct swi_attr *sa; + int i; + bool ret = true; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = false; + goto end; + } + + ports = malloc(sa->ports * sizeof(unsigned short)); + i = ngadmin_getAllPVID(nga, ports); + if (i != ERR_OK) { + printErrCode(i); + ret = false; + goto end; + } + + printf("Port\t"); + for (i = 1; i <= sa->ports; i++) + printf("%i\t", i); + putchar('\n'); + + printf("VLAN\t"); + for (i = 0; i < sa->ports; i++) + printf("%u\t", ports[i]); + putchar('\n'); + +end: + free(ports); + + return ret; } diff --git a/cli/commands.c b/cli/commands.c index df13a58..f0bf9a3 100644 --- a/cli/commands.c +++ b/cli/commands.c @@ -2,100 +2,98 @@ #include "commands.h" - - -// bitrate +/* bitrate */ bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga); bool do_bitrate_show (int nb, const char **com, struct ngadmin *nga); -// cabletest +/* cabletest */ bool do_cabletest (int nb, const char **com, struct ngadmin *nga); -// defaults +/* defaults */ bool do_defaults (int nb, const char **com, struct ngadmin *nga); -// firmware +/* firmware */ bool do_firmware_show (int nb, const char **com, struct ngadmin *nga); bool do_firmware_upgrade (int nb, const char **com, struct ngadmin *nga); -// help +/* help */ bool do_help (int nb, const char **com, struct ngadmin *nga); -// igmp +/* igmp */ bool do_igmp_set (int nb, const char **com, struct ngadmin *nga); bool do_igmp_show (int nb, const char **com, struct ngadmin *nga); -// list +/* list */ bool do_list (int nb, const char **com, struct ngadmin *nga); -// login +/* login */ bool do_login (int nb, const char **com, struct ngadmin *nga); -// mirror +/* mirror */ bool do_mirror_disable (int nb, const char **com, struct ngadmin *nga); bool do_mirror_set (int nb, const char **com, struct ngadmin *nga); bool do_mirror_show (int nb, const char **com, struct ngadmin *nga); -// name +/* name */ bool do_name_show (int nb, const char **com, struct ngadmin *nga); bool do_name_set (int nb, const char **com, struct ngadmin *nga); bool do_name_clear (int nb, const char **com, struct ngadmin *nga); -// netconf +/* netconf */ bool do_netconf_set (int nb, const char **com, struct ngadmin *nga); -// password +/* password */ bool do_password_change (int nb, const char **com, struct ngadmin *nga); bool do_password_set (int nb, const char **com, struct ngadmin *nga); -// ports +/* ports */ bool do_ports_state (int nb, const char **com, struct ngadmin *nga); bool do_ports_statistics_reset (int nb, const char **com, struct ngadmin *nga); bool do_ports_statistics_show (int nb, const char **com, struct ngadmin *nga); -// qos +/* qos */ bool do_qos_mode (int nb, const char **com, struct ngadmin *nga); bool do_qos_set (int nb, const char **com, struct ngadmin *nga); bool do_qos_show (int nb, const char **com, struct ngadmin *nga); -// quit +/* quit */ bool do_quit (int nb, const char **com, struct ngadmin *nga); -// restart +/* restart */ bool do_restart (int nb, const char **com, struct ngadmin *nga); -// scan +/* scan */ bool do_scan (int nb, const char **com, struct ngadmin *nga); -// stormfilter +/* stormfilter */ bool do_stormfilter_enable (int nb, const char **com, struct ngadmin *nga); bool do_stormfilter_disable (int nb, const char **com, struct ngadmin *nga); bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga); bool do_stormfilter_show (int nb, const char **com, struct ngadmin *nga); -// tree +/* tree */ bool do_tree (int nb, const char **com, struct ngadmin *nga); -// vlan +/* vlan */ bool do_vlan_8021q_del (int nb, const char **com, struct ngadmin *nga); bool do_vlan_8021q_set (int nb, const char **com, struct ngadmin *nga); bool do_vlan_8021q_show (int nb, const char **com, struct ngadmin *nga); @@ -105,106 +103,102 @@ bool do_vlan_pvid_set (int nb, const char **com, struct ngadmin *nga); bool do_vlan_pvid_show (int nb, const char **com, struct ngadmin *nga); - -// commands structure +/* commands structure */ COM_ROOT_START(coms) - - COM_START(bitrate) - COM_TERM(set, do_bitrate_set, true) - COM_TERM(show, do_bitrate_show, false) - COM_END - - COM_TERM(cabletest, do_cabletest, true) - - COM_TERM(defaults, do_defaults, false) - - COM_START(firmware) - COM_TERM(show, do_firmware_show, false) - COM_TERM(upgrade, do_firmware_upgrade, true) - COM_END - - COM_TERM(help, do_help, false) - - COM_START(igmp) - COM_TERM(set, do_igmp_set, true) - COM_TERM(show, do_igmp_show, false) - COM_END - - COM_TERM(list, do_list, false) - - COM_TERM(login, do_login, true) - - COM_START(mirror) - COM_TERM(disable, do_mirror_disable, false) - COM_TERM(set, do_mirror_set, true) - COM_TERM(show, do_mirror_show, false) - COM_END - - COM_START(name) - COM_TERM(show, do_name_show, false) - COM_TERM(set, do_name_set, true) - COM_TERM(clear, do_name_clear, false) - COM_END - - COM_START(netconf) - COM_TERM(set, do_netconf_set, true) - COM_END - - COM_START(password) - COM_TERM(change, do_password_change, true) - COM_TERM(set, do_password_set, true) - COM_END - - COM_START(ports) - COM_TERM(state, do_ports_state, false) - COM_START(statistics) - COM_TERM(reset, do_ports_statistics_reset, false) - COM_TERM(show, do_ports_statistics_show, false) - COM_END - COM_END - - COM_START(qos) - COM_TERM(mode, do_qos_mode, true) - COM_TERM(set, do_qos_set, true) - COM_TERM(show, do_qos_show, false) - COM_END - - COM_TERM(quit, do_quit, false) - - COM_TERM(restart, do_restart, false) - - COM_TERM(scan, do_scan, false) - - COM_START(stormfilter) - COM_TERM(enable, do_stormfilter_enable, false) - COM_TERM(disable, do_stormfilter_disable, false) - COM_TERM(set, do_stormfilter_set, true) - COM_TERM(show, do_stormfilter_show, false) - COM_END - - COM_TERM(tree, do_tree, false) - - COM_START(vlan) - COM_START(802.1q) - COM_TERM(del, do_vlan_8021q_del, true) - COM_TERM(set, do_vlan_8021q_set, true) - COM_TERM(show, do_vlan_8021q_show, true) - COM_END - COM_START(mode) - COM_TERM(set, do_vlan_mode_set, true) - COM_TERM(show, do_vlan_mode_show, false) - COM_END - COM_START(port) - COM_TERM(set, NULL, true) - COM_TERM(show, NULL, false) - COM_END - COM_START(pvid) - COM_TERM(set, do_vlan_pvid_set, true) - COM_TERM(show, do_vlan_pvid_show, false) - COM_END - COM_END - + COM_START(bitrate) + COM_TERM(set, do_bitrate_set, true) + COM_TERM(show, do_bitrate_show, false) + COM_END + + COM_TERM(cabletest, do_cabletest, true) + + COM_TERM(defaults, do_defaults, false) + + COM_START(firmware) + COM_TERM(show, do_firmware_show, false) + COM_TERM(upgrade, do_firmware_upgrade, true) + COM_END + + COM_TERM(help, do_help, false) + + COM_START(igmp) + COM_TERM(set, do_igmp_set, true) + COM_TERM(show, do_igmp_show, false) + COM_END + + COM_TERM(list, do_list, false) + + COM_TERM(login, do_login, true) + + COM_START(mirror) + COM_TERM(disable, do_mirror_disable, false) + COM_TERM(set, do_mirror_set, true) + COM_TERM(show, do_mirror_show, false) + COM_END + + COM_START(name) + COM_TERM(show, do_name_show, false) + COM_TERM(set, do_name_set, true) + COM_TERM(clear, do_name_clear, false) + COM_END + + COM_START(netconf) + COM_TERM(set, do_netconf_set, true) + COM_END + + COM_START(password) + COM_TERM(change, do_password_change, true) + COM_TERM(set, do_password_set, true) + COM_END + + COM_START(ports) + COM_TERM(state, do_ports_state, false) + COM_START(statistics) + COM_TERM(reset, do_ports_statistics_reset, false) + COM_TERM(show, do_ports_statistics_show, false) + COM_END + COM_END + + COM_START(qos) + COM_TERM(mode, do_qos_mode, true) + COM_TERM(set, do_qos_set, true) + COM_TERM(show, do_qos_show, false) + COM_END + + COM_TERM(quit, do_quit, false) + + COM_TERM(restart, do_restart, false) + + COM_TERM(scan, do_scan, false) + + COM_START(stormfilter) + COM_TERM(enable, do_stormfilter_enable, false) + COM_TERM(disable, do_stormfilter_disable, false) + COM_TERM(set, do_stormfilter_set, true) + COM_TERM(show, do_stormfilter_show, false) + COM_END + + COM_TERM(tree, do_tree, false) + + COM_START(vlan) + COM_START(802.1q) + COM_TERM(del, do_vlan_8021q_del, true) + COM_TERM(set, do_vlan_8021q_set, true) + COM_TERM(show, do_vlan_8021q_show, true) + COM_END + COM_START(mode) + COM_TERM(set, do_vlan_mode_set, true) + COM_TERM(show, do_vlan_mode_show, false) + COM_END + COM_START(port) + COM_TERM(set, NULL, true) + COM_TERM(show, NULL, false) + COM_END + COM_START(pvid) + COM_TERM(set, do_vlan_pvid_set, true) + COM_TERM(show, do_vlan_pvid_show, false) + COM_END + COM_END COM_ROOT_END - diff --git a/cli/commands.h b/cli/commands.h index 3877d9d..fa2ec66 100644 --- a/cli/commands.h +++ b/cli/commands.h @@ -6,26 +6,23 @@ #include "common.h" - - struct TreeNode { - const char *name; - bool (* const comfunc)(int, const char**, struct ngadmin*); - bool hasArgs; - const struct TreeNode *sub; + const char *name; + bool (* const comfunc)(int, const char**, struct ngadmin*); + bool hasArgs; + const struct TreeNode *sub; }; -#define COM_ROOT_START(v) const struct TreeNode v={.name="", .comfunc=NULL, .hasArgs=false, .sub=(const struct TreeNode[]){ -#define COM_ROOT_END {.name=NULL, .comfunc=NULL, .hasArgs=false, .sub=NULL}}}; -#define COM_START(nam) {.name=#nam, .comfunc=NULL, .hasArgs=false, .sub=(const struct TreeNode[]){ -#define COM_END {.name=NULL, .comfunc=NULL, .hasArgs=false, .sub=NULL}}}, -#define COM_TERM(nam, func, args) {.name=#nam, .comfunc=func, .hasArgs=args, .sub=NULL}, +#define COM_ROOT_START(v) const struct TreeNode v = {.name = "", .comfunc = NULL, .hasArgs = false, .sub = (const struct TreeNode[]){ +#define COM_ROOT_END {.name = NULL, .comfunc = NULL, .hasArgs = false, .sub = NULL}}}; +#define COM_START(nam) {.name = #nam, .comfunc = NULL, .hasArgs = false, .sub = (const struct TreeNode[]){ +#define COM_END {.name = NULL, .comfunc = NULL, .hasArgs = false, .sub = NULL}}}, +#define COM_TERM(nam, func, args) {.name = #nam, .comfunc = func, .hasArgs = args, .sub = NULL}, extern const struct TreeNode coms; - #endif diff --git a/cli/common.c b/cli/common.c index 4a5315c..80f3f52 100644 --- a/cli/common.c +++ b/cli/common.c @@ -2,161 +2,152 @@ #include "common.h" - - -void printErrCode (int err) { - - - switch ( err ) { - case ERR_OK: /*printf("ok\n");*/ break; - case ERR_NET: printf("network error\n"); break; - case ERR_NOTLOG: printf("no switch selected\n"); break; - case ERR_DENIED: printf("access denied\n"); break; - case ERR_BADPASS: printf("wrong password\n"); break; - case ERR_BADID: printf("bad switch id\n"); break; - case ERR_INVARG: printf("invalid argument\n"); break; - case ERR_TIMEOUT: printf("timeout\n"); break; - case ERR_NOTIMPL: printf("not implemented\n"); break; - default: printf("unknown status code (%i)\n", err); - } - - +void printErrCode (int err) +{ + switch (err) { + case ERR_OK: + break; + + case ERR_NET: + printf("network error\n"); + break; + + case ERR_NOTLOG: + printf("no switch selected\n"); + break; + + case ERR_DENIED: + printf("access denied\n"); + break; + + case ERR_BADPASS: + printf("wrong password\n"); + break; + + case ERR_BADID: + printf("bad switch id\n"); + break; + + case ERR_INVARG: + printf("invalid argument\n"); + break; + + case ERR_TIMEOUT: + printf("timeout\n"); + break; + + case ERR_NOTIMPL: + printf("not implemented\n"); + break; + + default: + printf("unknown status code (%i)\n", err); + } } - - -const char* bitrates[]={ - "nl", - "512K", - "1M", - "2M", - "4M", - "8M", - "16M", - "32M", - "64M", - "128M", - "256M", - "512M", - NULL +const char* const bitrates[] = { + "nl", + "512K", + "1M", + "2M", + "4M", + "8M", + "16M", + "32M", + "64M", + "128M", + "256M", + "512M", + NULL }; -const char* prio[]={ - NULL, - "high", - "medium", - "normal", - "low", - NULL +const char* const prio[]={ + NULL, + "high", + "medium", + "normal", + "low", + NULL }; - - - - -int parseBitrate (const char *s) { - - int i; - - - for (i=0; bitrates[i]!=NULL && strcasecmp(bitrates[i], s)!=0; ++i); - - - return i; - +int parseBitrate (const char *s) +{ + int i; + + for (i = 0; bitrates[i] != NULL && strcasecmp(bitrates[i], s) != 0; i++); + + return i; } - -char parsePrio (const char *s) { - - int i; - - - for (i=1; prio[i]!=NULL && strcasecmp(prio[i], s)!=0; ++i); - - - return (char)i; - +char parsePrio (const char *s) +{ + int i; + + for (i = 1; prio[i] != NULL && strcasecmp(prio[i], s) != 0; i++); + + return (char)i; } - -void displaySwitchTab (const struct swi_attr *sa, int nb) { - - int i=0; - - - if ( nb==0 ) { - printf("no switch found\n"); - return; - } - - - printf("Num\tMac\t\t\tProduct\t\tName\t\t\tIP/mask\t\t\tDHCP\tPorts\tFirmware\n"); - - for (i=0; i=txt && ( (c=*p)==' ' || c=='\n' ); *p--=0); - - - return p-txt+1; - +int trim (char *txt, int start) +{ + char *p; + + if (txt == NULL) + return 0; + + p = txt + start; + for (p--; p >= txt && (*p == ' ' || *p == '\n'); *p-- = 0); + + return p - txt + 1; } - -// -------------------------------------------------------- -int explode (const char *commande, char** tab, int maximum) { - - const char *start, *end; - char c; - int n=0, len; - - - for (end=commande; ; n++) { - - for (start=end; (c=*start)==' ' && c!=0; start++); - for (end=start; ( (c=*end)!=' ' || n>=maximum-1 ) && c!=0; end++); - - if ( (len=end-start)==0 ) { - break; - } - - tab[n]=malloc(sizeof(char)*(len+1)); - memcpy(tab[n], start, len); - tab[n][len]=0; - - } - - - return n; - +int explode (const char *commande, char** tab, int maximum) +{ + const char *start, *end; + int n = 0, len; + + + for (end = commande; ; n++) { + for (start = end; *start == ' ' && *start != 0; start++); + for (end = start; (*end != ' ' || n >= maximum - 1 ) && *end != 0; end++); + + len = end - start; + if (len == 0) + break; + + tab[n] = malloc(sizeof(char) * (len + 1)); + memcpy(tab[n], start, len); + tab[n][len] = 0; + } + + + return n; } diff --git a/cli/common.h b/cli/common.h index c0e6cb8..6533f05 100644 --- a/cli/common.h +++ b/cli/common.h @@ -16,12 +16,11 @@ #define NORET __attribute__((noreturn)) - extern int cont; extern struct termios current_term; -extern const char *bitrates[], *prio[]; +extern const char * const bitrates[], * const prio[]; void displaySwitchTab (const struct swi_attr *sa, int nb); @@ -30,13 +29,10 @@ int parseBitrate (const char *s); char parsePrio (const char *s); -// int trim (char *txt, int start); -// int explode (const char *commande, char** tab, int maximum); - #endif -- 2.39.2