From: darkcoven Date: Fri, 27 Sep 2013 22:22:06 +0000 (+0200) Subject: Move cli code to src subdir X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=70f6914469f40ee063b190e40ca82c8150dfba88;p=ngadmin Move cli code to src subdir Shared build works --- diff --git a/.gitignore b/.gitignore index 603185b..0ad50c4 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ compile install-sh ltmain.sh missing +depcomp build-aux/ libtool stamp-h1 diff --git a/Makefile.am b/Makefile.am index 8519d09..939d0ff 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,15 +1,17 @@ +ACLOCAL_AMFLAGS = -I . + SUBDIRS = raw lib cli -if ENABLE_SPY -SUBDIRS += dump -endif +#if ENABLE_SPY +#SUBDIRS += dump +#endif -if ENABLE_DEBUG -CFLAGS += -g -else -CFLAGS += -fomit-frame-pointer -LDFLAGS+= -s -endif +#if ENABLE_DEBUG +#CFLAGS += -g +#else +#CFLAGS += -fomit-frame-pointer +#LDFLAGS+= -s +#endif diff --git a/cli/Makefile.am b/cli/Makefile.am index 66d771d..1c0c975 100644 --- a/cli/Makefile.am +++ b/cli/Makefile.am @@ -1,11 +1,3 @@ -bin_PROGRAMS = admin - -admin_SOURCES = admin.c com_bitrate.c com_cabletest.c com_defaults.c com_firmware.c \ - com_help.c com_igmp.c com_list.c com_login.c commands.c com_mirror.c \ - common.c com_name.c com_netconf.c com_password.c com_ports.c com_qos.c \ - com_quit.c com_restart.c com_scan.c com_stormfilter.c com_tree.c com_vlan.c -admin_CFLAGS = -I../lib/include/ -admin_LDADD = libngadmin.la - +SUBDIRS = man src diff --git a/cli/admin.c b/cli/admin.c deleted file mode 100644 index bb4b310..0000000 --- a/cli/admin.c +++ /dev/null @@ -1,393 +0,0 @@ - -#include -#include -#include -#include - -#include -#include -#include - -#include "common.h" -#include "commands.h" - - -#define MAXCOM 32 - - -int main_loop_continue = 1; - - -static const struct TreeNode* getSubCom (char **com, int n, int *t) -{ - int i; - const struct TreeNode *cur, *next; - - - cur = &commands; - 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; -} - - -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; -} - - -static 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) - compcur = NULL; - matches = rl_completion_matches(text, my_generator); - - for (i = 0; com[i] != NULL; i++) - free(com[i]); - - - return matches; -} - - -static struct ngadmin *nga; -static sigjmp_buf jmpbuf; -static struct termios orig_term; -struct termios current_term; -static bool batch; - - -NORET static void handler (int sig) -{ - switch (sig) { - - case SIGTERM: - case SIGINT: - printf("interrupt\n"); - - current_term.c_lflag |= ECHO; - tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); - - if (!batch) - siglongjmp(jmpbuf, 1); - - default: - ngadmin_close(nga); - - tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); - - exit(0); - } -} - - -static int pre_login (const struct ether_addr *mac, int retries) -{ - const struct swi_attr *sa; - int i, n, err; - - - for (i = 1; retries <= 0 || i <= retries; i++) { - /* scan */ - printf("scan... "); - fflush(stdout); - err = ngadmin_scan(nga); - if (err < 0) { - printErrCode(err); - continue; - } - - /* search switch with requested MAC */ - sa = ngadmin_getSwitchTab(nga, &n); - while (--n >= 0) { - if (memcmp(mac, &sa[n].mac, ETH_ALEN) == 0) - break; - } - - if (n < 0) { - printf("no switch found\n"); - } else { - printf("done\n"); - break; - } - } - - if (n < 0) - return 1; - - /* login */ - printf("login... "); - fflush(stdout); - err = ngadmin_login(nga, n); - if (err < 0) - printErrCode(err); - else - printf("done\n"); - - return err; -} - - -int main (int argc, char **argv) -{ - static const struct option opts[] = { - {"batch", no_argument, NULL, 'a'}, - {"keep-broadcasting", no_argument, NULL, 'b'}, - {"force-interface", no_argument, NULL, 'f'}, - {"global-broadcast", no_argument, NULL, 'g'}, - {"help", no_argument, NULL, 'h'}, - {"interface", required_argument, NULL, 'i'}, - {"mac", required_argument, NULL, 'm'}, - {"password", required_argument, NULL, 'p'}, - {"retries", required_argument, NULL, 'r'}, - {"timeout", required_argument, NULL, 't'}, - {0, 0, 0, 0} - }; - char *line, *com[MAXCOM]; - const char *iface = "eth0", *password = NULL; - float timeout = 0.f; - bool kb = false, force = false, global = false; - struct timeval tv; - const struct TreeNode *cur, *next; - struct ether_addr *mac = NULL; - int i, n, retries = 3; - - - tcgetattr(STDIN_FILENO, &orig_term); - current_term = orig_term; - batch = false; - - opterr = 0; - - while ((n = getopt_long(argc, argv, "abfghi:m:p:r:t:", opts, NULL)) != -1) { - switch (n) { - - case 'a': - batch = true; - break; - - case 'b': - kb = true; - break; - - case 'f': - force = true; - break; - - case 'g': - global = true; - break; - - case 'h': - printf("usage: %s [-a] [-b] [-f] [-g] [-i ] [-m ] [-p ]\n", argv[0]); - goto end; - - case 'i': - iface = optarg; - break; - - case 'm': - mac = ether_aton(optarg); - if (mac == NULL) { - printf("invalid MAC\n"); - goto end; - } - break; - - case 'p': - password = optarg; - break; - - case 'r': - retries = strtol(optarg, NULL, 0); - break; - - 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; - - /* non-TTY inputs are automatically set to batch mode */ - if (!isatty(STDIN_FILENO)) - batch = true; - - if (password != NULL) - ngadmin_setPassword(nga, password); - - signal(SIGTERM, handler); - signal(SIGINT, handler); - - /* automatic scan & login when switch MAC is specified on the command line */ - if (mac != NULL && pre_login(mac, retries) != 0) - goto end; - - if (batch) { - /* in batch mode, we must be logged to continue */ - if (ngadmin_getCurrentSwitch(nga) == NULL) { - printf("must be logged\n"); - goto end; - } - } else { - /* initialize readline functions */ - rl_attempted_completion_function = my_completion; - rl_completion_entry_function = my_generator; - - sigsetjmp(jmpbuf, 1); - } - - while (main_loop_continue) { - /* read user input */ - line = NULL; - n = 0; - if (batch) - n = getline(&line, (size_t*)&i, stdin); - else - line = readline("> "); - if (n < 0 || line == NULL) - goto end; - - /* split string into words */ - trim(line, strlen(line)); - n = explode(line, com, MAXCOM); - - if (n == 0) { - free(line); - continue; - } else { - if (!batch) - add_history(line); - free(line); - } - - cur = getSubCom(com, n, &i); - - if (cur->sub != NULL) { - /* not terminal command */ - if (i == 0) { - /* root command */ - printf("unknown command: %s\n", com[i]); - } else if (i < n) { - /* intermediate command, remaining string */ - printf("unknown %s subcommand: %s\n", com[i - 1], com[i]); - } else { - /* intermediate command, no remaining string */ - /* print available subcommands */ - for (next = cur->sub; next->name != NULL; next++) - printf("%s ", next->name); - putchar('\n'); - } - } else if (cur->comfunc == NULL) { - /* erroneous terminal command without function */ - printf("terminal command without function\n"); - } else { - /* execute terminal command */ - cur->comfunc(n - i, (const char**)&com[i], 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 deleted file mode 100644 index 7085b82..0000000 --- a/cli/com_bitrate.c +++ /dev/null @@ -1,121 +0,0 @@ - -#include "commands.h" - - -/* helper function to analyse bitrate speed specifications */ -static int bitrate_analyse (int argc, const char **argv, int *ports) -{ - int i = 0, s; - - - while (i < argc - 1) { - s = parseBitrate(argv[i + 1]); - if (strcmp(argv[i], "inout") == 0) { - ports[0] = s; - ports[1] = s; - } else if (strcmp(argv[i], "in") == 0) { - ports[0] = s; - } else if (strcmp(argv[i], "out") == 0) { - ports[1] = s; - } else { - break; - } - i += 2; - } - - - return i; -} - - -int do_bitrate_set (int argc, const char **argv, struct ngadmin *nga) -{ - int i, k = 0, defs[] = {12, 12}, p, *ports = NULL, ret = 0; - const struct swi_attr *sa; - - - if (argc < 2) { - printf( - "usage: bitrate set [all SPEEDSPEC] SPEEDSPEC [ SPEEDSPEC ...]\n" - "SPEEDSPEC: [inout ] [in ] [out ]\n" - ); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ports = malloc(2 * sa->ports * sizeof(int)); - - /* get defaults if present */ - if (strcmp(argv[k], "all") == 0) { - k++; - k += bitrate_analyse(argc - k, &argv[k], defs); - } - - /* apply defaults */ - for (i = 0; i < sa->ports; i++) - memcpy(&ports[2 * i], defs, sizeof(defs)); - - /* get ports specifics */ - while (k < argc) { - p = strtol(argv[k++], NULL, 0) - 1; - if (p >= 0 && p ports) - k += bitrate_analyse(argc - k, &argv[k], &ports[2 * p]); - } - - /* send it to the switch */ - i = ngadmin_setBitrateLimits(nga, ports); - printErrCode(i); - -end: - free(ports); - - return ret; -} - - -int do_bitrate_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i, ret = 0, *ports = NULL; - const struct swi_attr *sa; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - - ports = malloc(2 * sa->ports * sizeof(int)); - i = ngadmin_getBitrateLimits(nga, ports); - if (i != ERR_OK) { - printErrCode(i); - ret = 1; - 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 deleted file mode 100644 index b5dfab3..0000000 --- a/cli/com_cabletest.c +++ /dev/null @@ -1,49 +0,0 @@ - -#include "commands.h" - - -int do_cabletest (int argc, const char **argv, struct ngadmin *nga) -{ - const struct swi_attr *sa; - struct cabletest *ct = NULL; - int i, j = 0, k = 0, ret = 0; - - - if (argc < 1) { - printf("usage: cabletest [ ...]\n"); - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ct = malloc(sa->ports * sizeof(struct cabletest)); - memset(ct, 0, sa->ports * sizeof(struct cabletest)); - - while (k < argc) { - ct[j].port = strtol(argv[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 = 1; - 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 deleted file mode 100644 index 74ecfa5..0000000 --- a/cli/com_defaults.c +++ /dev/null @@ -1,38 +0,0 @@ - -#include "commands.h" - - -int do_defaults (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i, ret = 0; - const struct swi_attr *sa; - char line[16]; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - 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 deleted file mode 100644 index 3d5e76c..0000000 --- a/cli/com_firmware.c +++ /dev/null @@ -1,59 +0,0 @@ - -#include "commands.h" - - -int do_firmware_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - const struct swi_attr *sa; - int ret = 0; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - puts(sa->firmware); - -end: - - return ret; -} - - -int do_firmware_upgrade (int argc, const char **argv, struct ngadmin *nga) -{ - const struct swi_attr *sa; - int i, ret = 0; - - - if (argc != 1) { - printf("usage: firmware upgrade \n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - i = ngadmin_upgradeFirmware(nga, argv[0]); - printErrCode(i); - -end: - - return ret; -} - - diff --git a/cli/com_help.c b/cli/com_help.c deleted file mode 100644 index 34fc2bc..0000000 --- a/cli/com_help.c +++ /dev/null @@ -1,25 +0,0 @@ - -#include "commands.h" - - -bool do_help (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED) -{ - const struct TreeNode *s; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - printf("Available commands: \n"); - - for (s = commands.sub; s->name != NULL; s++) - printf("%s ", s->name); - putchar('\n'); - - - return 0; -} - - diff --git a/cli/com_igmp.c b/cli/com_igmp.c deleted file mode 100644 index 55e12da..0000000 --- a/cli/com_igmp.c +++ /dev/null @@ -1,71 +0,0 @@ - -#include "commands.h" - - -int do_igmp_set (int argc, const char **argv, struct ngadmin *nga) -{ - int i; - struct igmp_conf ic; - - - if (argc != 4) { - printf("usage: igmp set \n"); - return 1; - } - - if (ngadmin_getCurrentSwitch(nga) == NULL) { - printf("must be logged\n"); - return 1; - } - - ic.enable = strtol(argv[0], NULL, 0); - ic.vlan = strtol(argv[1], NULL, 0); - ic.validate = strtol(argv[2], NULL, 0); - ic.block = strtol(argv[3], NULL, 0); - - i = ngadmin_setIGMPConf(nga, &ic); - printErrCode(i); - - - return 0; -} - - -int do_igmp_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i, ret = 0; - const struct swi_attr *sa; - struct igmp_conf ic; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - i = ngadmin_getIGMPConf(nga, &ic); - if (i != ERR_OK) { - printErrCode(i); - ret = 1; - 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 deleted file mode 100644 index 853f8f3..0000000 --- a/cli/com_list.c +++ /dev/null @@ -1,23 +0,0 @@ - -#include "commands.h" - - -bool do_list (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int n; - const struct swi_attr *sa; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - sa = ngadmin_getSwitchTab(nga, &n); - displaySwitchTab(sa, n); - - - return 0; -} - - diff --git a/cli/com_login.c b/cli/com_login.c deleted file mode 100644 index a6ee7e8..0000000 --- a/cli/com_login.c +++ /dev/null @@ -1,23 +0,0 @@ - -#include "commands.h" - - -bool do_login (int argc, const char **argv, struct ngadmin *nga) -{ - int i; - - - if (argc != 1) { - printf("usage: login \n"); - return 1; - } - - i = strtol(argv[0], NULL, 0); - i = ngadmin_login(nga, i); - printErrCode(i); - - - return 0; -} - - diff --git a/cli/com_mirror.c b/cli/com_mirror.c deleted file mode 100644 index 5ebe040..0000000 --- a/cli/com_mirror.c +++ /dev/null @@ -1,126 +0,0 @@ - -#include "commands.h" - - -int do_mirror_disable (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - if (ngadmin_getCurrentSwitch(nga) == NULL) { - printf("must be logged\n"); - return 1; - } - - i = ngadmin_setMirror(nga, NULL); - printErrCode(i); - - - return 0; -} - - -int do_mirror_set (int argc, const char **argv, struct ngadmin *nga) -{ - const struct swi_attr *sa; - char *ports = NULL; - int i, k = 0, ret = 0; - - - if (argc < 3) { - printf("usage: mirror set clone [ ...]\n"); - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ports = malloc((sa->ports + 1) * sizeof(char)); - memset(ports, 0, sa->ports + 1); - - ports[0] = strtol(argv[k++], NULL, 0); - if (ports[0] < 1 || ports[0] > sa->ports || strcasecmp(argv[k++], "clone") != 0) { - printf("syntax error\n"); - ret = 1; - goto end; - } - - while (k < argc) { - i = strtol(argv[k++], NULL, 0); - if (i < 1 || i > sa->ports) { - printf("port out of range\n"); - ret = 1; - goto end; - } else if (i == ports[0]) { - printf("destination port cannot be in port list\n"); - ret = 1; - goto end; - } - ports[i] = 1; - } - - i = ngadmin_setMirror(nga, ports); - printErrCode(i); - -end: - free(ports); - - return ret; -} - - -int do_mirror_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - const struct swi_attr *sa; - char *ports = NULL; - int i; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - 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 0; -} - - diff --git a/cli/com_name.c b/cli/com_name.c deleted file mode 100644 index dd7b048..0000000 --- a/cli/com_name.c +++ /dev/null @@ -1,77 +0,0 @@ - -#include "commands.h" - - -int do_name_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - const struct swi_attr *sa; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - puts(sa->name); - - - return 0; -} - - -int do_name_set (int argc, const char **argv, struct ngadmin *nga) -{ - int i; - const struct swi_attr *sa; - - - if (argc != 1) { - printf("usage: name set \n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - i = ngadmin_setName(nga, argv[0]); - printErrCode(i); - - - return 0; -} - - -int do_name_clear (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i; - const struct swi_attr *sa; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - i = ngadmin_setName(nga, NULL); - printErrCode(i); - - - return 0; -} - - diff --git a/cli/com_netconf.c b/cli/com_netconf.c deleted file mode 100644 index d44b83a..0000000 --- a/cli/com_netconf.c +++ /dev/null @@ -1,69 +0,0 @@ - -#include "commands.h" - - -int do_netconf_set (int argc, const char **argv, struct ngadmin *nga) -{ - int i, k, ret = 0; - const struct swi_attr *sa; - struct net_conf nc; - - - if (argc == 0) { - printf("usage: netconf set [dhcp yes|no] [ip ] [mask ] [gw ]\n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - memset(&nc, 0, sizeof(struct net_conf)); - - for (k = 0; k < argc; k += 2) { - if (strcasecmp(argv[k], "dhcp") == 0) { - if (strcasecmp(argv[k + 1], "yes") == 0) { - nc.dhcp = true; - } else if (strcasecmp(argv[k + 1], "no") == 0) { - nc.dhcp = 1; - } else { - printf("Incorrect DHCP value\n"); - ret = 1; - goto end; - } - } else if (strcasecmp(argv[k], "ip") == 0) { - if (inet_aton(argv[k + 1], &nc.ip) == 0) { - printf("Incorrect IP value\n"); - ret = 1; - goto end; - } - } else if (strcasecmp(argv[k], "mask") == 0) { - /* TODO: check if it is a correct mask */ - if (inet_aton(argv[k + 1], &nc.netmask) == 0) { - printf("Incorrect mask value\n"); - ret = 1; - goto end; - } - } else if (strcasecmp(argv[k], "gw") == 0) { - if (inet_aton(argv[k + 1], &nc.gw) == 0) { - printf("Incorrect gateway value\n"); - ret = 1; - goto end; - } - } - } - - i = ngadmin_setNetConf(nga, &nc); - if (i != ERR_OK) { - printErrCode(i); - ret = 1; - } - -end: - - return ret; -} - - diff --git a/cli/com_password.c b/cli/com_password.c deleted file mode 100644 index 0303b2b..0000000 --- a/cli/com_password.c +++ /dev/null @@ -1,65 +0,0 @@ - -#include "commands.h" - - -int do_password_change (int argc, const char **argv, struct ngadmin *nga) -{ - int i; - const struct swi_attr *sa; - - - if (argc != 1) { - printf("usage: password change \n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - i = ngadmin_changePassword(nga, argv[0]); - printErrCode(i); - - - return 0; -} - - -int do_password_set (int argc, const char **argv, struct ngadmin *nga) -{ - int i; - char buf[64]; - const char *pass; - - - if (argc > 1) { - printf("usage: password set []\n"); - return 1; - } - - if (argc == 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 = argv[0]; - } - - if (pass != NULL) { - i = ngadmin_setPassword(nga, pass); - printErrCode(i); - } - - - return 0; -} - - diff --git a/cli/com_ports.c b/cli/com_ports.c deleted file mode 100644 index 3337b43..0000000 --- a/cli/com_ports.c +++ /dev/null @@ -1,127 +0,0 @@ - -#include "commands.h" - - -int do_ports_state (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i, ret = 0; - const struct swi_attr *sa; - unsigned char *ports = NULL; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ports = malloc(sa->ports * sizeof(unsigned char)); - i = ngadmin_getPortsStatus(nga, ports); - if (i < 0) { - printErrCode(i); - ret = 1; - 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; -} - - -int do_ports_statistics_reset (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - if (ngadmin_getCurrentSwitch(nga) == NULL) { - printf("must be logged\n"); - return 1; - } - - i = ngadmin_resetPortsStatistics(nga); - printErrCode(i); - - return 0; -} - - -int do_ports_statistics_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i, ret = 0; - const struct swi_attr *sa; - struct port_stats *ps = NULL; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ps = calloc(sa->ports, sizeof(struct port_stats)); - i = ngadmin_getPortsStatistics(nga, ps); - if (i < 0) { - printErrCode(i); - ret = 1; - 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 deleted file mode 100644 index 7b51bd2..0000000 --- a/cli/com_qos.c +++ /dev/null @@ -1,154 +0,0 @@ - -#include "commands.h" - - -int do_qos_mode (int argc, const char **argv, struct ngadmin *nga) -{ - int i, s, ret = 0; - const struct swi_attr *sa; - - - if (argc == 0) { - printf("usage: qos mode port|802.1p\n"); - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - if (strcasecmp(argv[0], "port") == 0) { - s = QOS_PORT; - } else if (strcasecmp(argv[0], "802.1p") == 0) { - s = QOS_DOT; - } else { - printf("Unknown QOS mode\n"); - ret = 1; - goto end; - } - - i = ngadmin_setQOSMode(nga, s); - printErrCode(i); - -end: - - return ret; -} - - -int do_qos_set (int argc, const char **argv, struct ngadmin *nga) -{ - int i, p, ret = 0; - const struct swi_attr *sa; - char d = PRIO_UNSPEC, *ports = NULL; - - - if (argc < 2) { - printf("usage: qos set (all )|( [ ...])\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa ==NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ports = malloc(sa->ports * sizeof(char)); - - /* read defaults */ - if (strcmp(argv[0], "all") == 0) { - d = parsePrio(argv[1]); - argv += 2; - argc -= 2; - } - - /* apply defaults */ - for (i = 0; i < sa->ports; i++) - ports[i] = d; - - /* read and apply port specifics */ - for (i = 0; i < argc; i += 2) { - p = strtol(argv[i], NULL, 0); - if (p < 1 || p > sa->ports) - continue; - ports[p - 1] = parsePrio(argv[i + 1]); - } - - /* send the new configuration to the switch */ - i = ngadmin_setQOSValues(nga, ports); - printErrCode(i); - -end: - free(ports); - - return ret; -} - - -int do_qos_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i, s = 0, ret = 0; - const struct swi_attr *sa; - char *ports = NULL; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - i = ngadmin_getQOSMode(nga, &s); - if (i != ERR_OK) { - printErrCode(i); - ret = 1; - 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 = 1; - 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 deleted file mode 100644 index f27620b..0000000 --- a/cli/com_quit.c +++ /dev/null @@ -1,17 +0,0 @@ - -#include "commands.h" - - -int do_quit (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED) -{ - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - main_loop_continue = 0; - - return 0; -} - - diff --git a/cli/com_restart.c b/cli/com_restart.c deleted file mode 100644 index c464828..0000000 --- a/cli/com_restart.c +++ /dev/null @@ -1,38 +0,0 @@ - -#include "commands.h" - - -int do_restart (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED) -{ - int i, ret = 0; - const struct swi_attr *sa; - char line[16]; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - 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 deleted file mode 100644 index f374d18..0000000 --- a/cli/com_scan.c +++ /dev/null @@ -1,29 +0,0 @@ - -#include "commands.h" - - -int do_scan (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i; - const struct swi_attr *sa; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - i = ngadmin_scan(nga); - if (i < 0) { - printErrCode(i); - return 1; - } - - sa = ngadmin_getSwitchTab(nga, &i); - displaySwitchTab(sa, i); - - - return 0; -} - - diff --git a/cli/com_stormfilter.c b/cli/com_stormfilter.c deleted file mode 100644 index dbd91f8..0000000 --- a/cli/com_stormfilter.c +++ /dev/null @@ -1,156 +0,0 @@ - -#include "commands.h" - - -int do_stormfilter_enable (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i; - const struct swi_attr *sa; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - i = ngadmin_setStormFilterState(nga, 1); - printErrCode(i); - - - return 0; -} - - -int do_stormfilter_disable (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i; - const struct swi_attr *sa; - - - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - i = ngadmin_setStormFilterState(nga, 0); - printErrCode(i); - - - return 0; -} - - -int do_stormfilter_set (int argc, const char **argv, struct ngadmin *nga) -{ - int i, d = BITRATE_UNSPEC, p, *ports = NULL, ret = 0; - const struct swi_attr *sa; - - - if (argc < 2) { - printf("usage: stormfilt set (all )|( [ ...])\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ports = malloc(sa->ports * sizeof(int)); - - /* read defaults */ - if (strcmp(argv[0], "all") == 0) { - d = parseBitrate(argv[1]); - argv += 2; - argc -= 2; - } - - /* apply defaults */ - for (i = 0; i < sa->ports; i++) - ports[i] = d; - - /* read and apply port specifics */ - for (i = 0; i < argc - 1; i += 2) { - p = strtol(argv[i], NULL, 0); - if (p < 1 || p > sa->ports) - continue; - ports[p - 1] = parseBitrate(argv[i + 1]); - } - - /* send the new configuration to the switch */ - i = ngadmin_setStormFilterValues(nga, ports); - printErrCode(i); - -end: - free(ports); - - return ret; -} - - -int do_stormfilter_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i, s, ret = 0, *ports = NULL; - const struct swi_attr *sa; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - i = ngadmin_getStormFilterState(nga, &s); - if (i != ERR_OK) { - printErrCode(i); - ret = 1; - 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 = 1; - 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 deleted file mode 100644 index b1dadff..0000000 --- a/cli/com_tree.c +++ /dev/null @@ -1,35 +0,0 @@ - -#include "commands.h" - - -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); -} - - -int do_tree (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED) -{ - if (argc > 0) { - printf("this command takes no argument\n"); - return 1; - } - - display_node(&commands, 0); - - return 0; -} - - diff --git a/cli/com_vlan.c b/cli/com_vlan.c deleted file mode 100644 index 3496815..0000000 --- a/cli/com_vlan.c +++ /dev/null @@ -1,510 +0,0 @@ - -#include "commands.h" - - - -static char vlan_char (int t) -{ - switch (t) { - - case VLAN_TAGGED: - return 'T'; - - case VLAN_UNTAGGED: - return 'U'; - - case VLAN_NO: - return ' '; - - default: - return '?'; - } -} - - -int do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga) -{ - const struct swi_attr *sa; - unsigned short vlan; - int i; - - - if (argc != 1) { - printf("usage: vlan 8021q del \n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - vlan=strtoul(argv[0], NULL, 0); - if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) { - printf("vlan out of range\n"); - return 1; - } - - i = ngadmin_VLANDestroy(nga, vlan); - printErrCode(i); - - - return 0; -} - - -int do_vlan_port_set (int argc, const char **argv, struct ngadmin *nga) -{ - unsigned char vlan, port, *ports = NULL; - const struct swi_attr *sa; - int i, k = 0, ret = 0; - - - if (argc < 2) { - printf("usage: vlan port set [all ] [ ] [ ] [...]\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ports = malloc(sa->ports * sizeof(unsigned char)); - - /* read defaults */ - vlan = 0; - if (strcmp(argv[k], "all") == 0) { - k++; - vlan = strtoul(argv[k++], NULL, 0); - /* VLAN 0 is allowed and means no change */ - if (vlan > VLAN_PORT_MAX) { - printf("vlan out of range\n"); - ret = 1; - goto end; - } - } - - /* apply defaults */ - memset(ports, vlan, sa->ports); - - /* read and apply port specifics */ - while (k < argc - 1) { - /* read port */ - port = strtoul(argv[k++], NULL, 0); - if (port < 1 || port > sa->ports) { - printf("port out of range\n"); - ret = 1; - goto end; - } - - /* read vlan */ - vlan = strtoul(argv[k++], NULL, 0); - /* VLAN 0 is allowed and means no change */ - if (vlan > VLAN_PORT_MAX) { - printf("vlan out of range\n"); - ret = 1; - goto end; - } - - ports[port - 1] = vlan; - } - - /* set conf */ - i = ngadmin_setVLANPortConf(nga, ports); - printErrCode(i); - -end: - free(ports); - - return ret; -} - - -int do_vlan_port_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - unsigned char *ports = NULL; - const struct swi_attr *sa; - int i, ret = 0; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ports = malloc(sa->ports * sizeof(unsigned char)); - - /* request all VLANs config */ - i = ngadmin_getVLANPortConf(nga, ports); - - if (i != ERR_OK) { - printErrCode(i); - ret = 1; - goto end; - } - - printf("Ports configuration: \n"); - printf("Port\t"); - for (i = 1; i <= sa->ports; i++) - printf("%i\t", i); - putchar('\n'); - - /* show all VLANs */ - printf("VLAN\t"); - for (i = 0; i < sa->ports; i++) - printf("%u\t", ports[i]); - putchar('\n'); - -end: - free(ports); - - return ret; -} - - -int do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga) -{ - unsigned char *ports = NULL, p, def = VLAN_UNSPEC; - const struct swi_attr *sa; - unsigned short vlan; - int i, k = 0, ret = 0; - - - if (argc == 0) { - printf("usage: vlan 802.1q set [all unspec|no|untagged|tagged] [ unspec|no|untagged|tagged ...]\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - /* read vlan */ - vlan = strtoul(argv[k++], NULL, 0); - - if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) { - printf("vlan out of range\n"); - ret = 1; - goto end; - } - - /* read defaults */ - if (k < argc - 1 && strcasecmp(argv[k], "all") == 0) { - k++; - if (strcasecmp(argv[k], "tagged") == 0) { - def = VLAN_TAGGED; - } else if (strcasecmp(argv[k], "untagged") == 0) { - def = VLAN_UNTAGGED; - } else if (strcasecmp(argv[k], "no") == 0) { - def = VLAN_NO; - } else if (strcasecmp(argv[k], "unspec") == 0) { - def = VLAN_UNSPEC; - } else { - printf("incorrect type\n"); - ret = 1; - goto end; - } - k++; - } - - ports = malloc(sa->ports * sizeof(unsigned char)); - - /* apply defaults */ - memset(ports, def, sa->ports); - - /* read and apply port specifics */ - while (k < argc - 1) { - p = strtoul(argv[k++], NULL, 0) - 1; - if (p >= sa->ports) { - printf("port out of range\n"); - ret = 1; - goto end; - } - if (strcasecmp(argv[k], "tagged") ==0) { - ports[p] = VLAN_TAGGED; - } else if (strcasecmp(argv[k], "untagged") == 0) { - ports[p] = VLAN_UNTAGGED; - } else if (strcasecmp(argv[k], "no") == 0) { - ports[p] = VLAN_NO; - } else if (strcasecmp(argv[k], "unspec") == 0) { - ports[p] = VLAN_UNSPEC; - } else { - printf("incorrect type\n"); - ret = 1; - goto end; - } - k++; - } - - /* set conf */ - i = ngadmin_setVLANDotConf(nga, vlan, ports); - printErrCode(i); - -end: - free(ports); - - return ret; -} - - -int do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga) -{ - unsigned short vl = 0, *vlans = NULL; - unsigned char *ports = NULL; - const struct swi_attr *sa; - int i, j, n = 16, ret = 0; - - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - if (argc > 0) - vl = strtoul(argv[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 = 1; - 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; -} - - -int do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga) -{ - int mode, i; - - - if (argc == 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 0; - } - - if (ngadmin_getCurrentSwitch(nga) == NULL) { - printf("must be logged\n"); - return 1; - } - - mode = strtoul(argv[0], NULL, 0); - if (mode < 1 || mode > 4) { - printf("mode out of range\n"); - return 1; - } - - i = ngadmin_setVLANType(nga, mode); - printErrCode(i); - - - return 0; -} - - -int do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - int i, t, ret = 0; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - if (ngadmin_getCurrentSwitch(nga) == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - i = ngadmin_getVLANType(nga, &t); - if (i != ERR_OK) { - printErrCode(i); - ret = 1; - 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; -} - - -int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga) -{ - const struct swi_attr *sa; - unsigned char port; - unsigned short vlan; - int i; - - - if (argc != 2) { - printf("usage: vlan pvid set \n"); - return 1; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - return 1; - } - - port = strtoul(argv[0], NULL, 0); - vlan = strtoul(argv[1], NULL, 0); - - if (port < 1 || port > sa->ports) { - printf("port out of range\n"); - return 1; - } - - if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) { - printf("vlan out of range\n"); - return 1; - } - - i = ngadmin_setPVID(nga, port, vlan); - printErrCode(i); - - - return 0; -} - - -int do_vlan_pvid_show (int argc, const char **argv UNUSED, struct ngadmin *nga) -{ - unsigned short *ports = NULL; - const struct swi_attr *sa; - int i, ret = 0; - - - if (argc > 0) { - printf("this command takes no argument\n"); - ret = 1; - goto end; - } - - sa = ngadmin_getCurrentSwitch(nga); - if (sa == NULL) { - printf("must be logged\n"); - ret = 1; - goto end; - } - - ports = malloc(sa->ports * sizeof(unsigned short)); - i = ngadmin_getAllPVID(nga, ports); - if (i != ERR_OK) { - printErrCode(i); - ret = 1; - 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 deleted file mode 100644 index 2d37ea8..0000000 --- a/cli/commands.c +++ /dev/null @@ -1,206 +0,0 @@ - -#include "commands.h" - - -/* bitrate */ -int do_bitrate_set (int argc, const char **argv, struct ngadmin *nga); -int do_bitrate_show (int argc, const char **argv, struct ngadmin *nga); - - -/* cabletest */ -int do_cabletest (int argc, const char **argv, struct ngadmin *nga); - - -/* defaults */ -int do_defaults (int argc, const char **argv, struct ngadmin *nga); - - -/* firmware */ -int do_firmware_show (int argc, const char **argv, struct ngadmin *nga); -int do_firmware_upgrade (int argc, const char **argv, struct ngadmin *nga); - - -/* help */ -int do_help (int argc, const char **argv, struct ngadmin *nga); - - -/* igmp */ -int do_igmp_set (int argc, const char **argv, struct ngadmin *nga); -int do_igmp_show (int argc, const char **argv, struct ngadmin *nga); - - -/* list */ -int do_list (int argc, const char **argv, struct ngadmin *nga); - - -/* login */ -int do_login (int argc, const char **argv, struct ngadmin *nga); - - -/* mirror */ -int do_mirror_disable (int argc, const char **argv, struct ngadmin *nga); -int do_mirror_set (int argc, const char **argv, struct ngadmin *nga); -int do_mirror_show (int argc, const char **argv, struct ngadmin *nga); - - -/* name */ -int do_name_show (int argc, const char **argv, struct ngadmin *nga); -int do_name_set (int argc, const char **argv, struct ngadmin *nga); -int do_name_clear (int argc, const char **argv, struct ngadmin *nga); - - -/* netconf */ -int do_netconf_set (int argc, const char **argv, struct ngadmin *nga); - - -/* password */ -int do_password_change (int argc, const char **argv, struct ngadmin *nga); -int do_password_set (int argc, const char **argv, struct ngadmin *nga); - - -/* ports */ -int do_ports_state (int argc, const char **argv, struct ngadmin *nga); -int do_ports_statistics_reset (int argc, const char **argv, struct ngadmin *nga); -int do_ports_statistics_show (int argc, const char **argv, struct ngadmin *nga); - - -/* qos */ -int do_qos_mode (int argc, const char **argv, struct ngadmin *nga); -int do_qos_set (int argc, const char **argv, struct ngadmin *nga); -int do_qos_show (int argc, const char **argv, struct ngadmin *nga); - - -/* quit */ -int do_quit (int argc, const char **argv, struct ngadmin *nga); - - -/* restart */ -int do_restart (int argc, const char **argv, struct ngadmin *nga); - - -/* scan */ -int do_scan (int argc, const char **argv, struct ngadmin *nga); - - -/* stormfilter */ -int do_stormfilter_enable (int argc, const char **argv, struct ngadmin *nga); -int do_stormfilter_disable (int argc, const char **argv, struct ngadmin *nga); -int do_stormfilter_set (int argc, const char **argv, struct ngadmin *nga); -int do_stormfilter_show (int argc, const char **argv, struct ngadmin *nga); - - -/* tree */ -int do_tree (int argc, const char **argv, struct ngadmin *nga); - - -/* vlan */ -int do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga); -int do_vlan_port_set (int argc, const char **argv, struct ngadmin *nga); -int do_vlan_port_show (int argc, const char **argv, struct ngadmin *nga); -int do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga); -int do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga); -int do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga); -int do_vlan_mode_show (int argc, const char **argv, struct ngadmin *nga); -int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga); -int do_vlan_pvid_show (int argc, const char **argv, struct ngadmin *nga); - - -/* commands structure */ -COM_ROOT_START(commands) - COM_START(bitrate) - COM_TERM(set, do_bitrate_set) - COM_TERM(show, do_bitrate_show) - COM_END - - COM_TERM(cabletest, do_cabletest) - - COM_TERM(defaults, do_defaults) - - COM_START(firmware) - COM_TERM(show, do_firmware_show) - COM_TERM(upgrade, do_firmware_upgrade) - COM_END - - COM_TERM(help, do_help) - - COM_START(igmp) - COM_TERM(set, do_igmp_set) - COM_TERM(show, do_igmp_show) - COM_END - - COM_TERM(list, do_list) - - COM_TERM(login, do_login) - - COM_START(mirror) - COM_TERM(disable, do_mirror_disable) - COM_TERM(set, do_mirror_set) - COM_TERM(show, do_mirror_show) - COM_END - - COM_START(name) - COM_TERM(show, do_name_show) - COM_TERM(set, do_name_set) - COM_TERM(clear, do_name_clear) - COM_END - - COM_START(netconf) - COM_TERM(set, do_netconf_set) - COM_END - - COM_START(password) - COM_TERM(change, do_password_change) - COM_TERM(set, do_password_set) - COM_END - - COM_START(ports) - COM_TERM(state, do_ports_state) - COM_START(statistics) - COM_TERM(reset, do_ports_statistics_reset) - COM_TERM(show, do_ports_statistics_show) - COM_END - COM_END - - COM_START(qos) - COM_TERM(mode, do_qos_mode) - COM_TERM(set, do_qos_set) - COM_TERM(show, do_qos_show) - COM_END - - COM_TERM(quit, do_quit) - - COM_TERM(restart, do_restart) - - COM_TERM(scan, do_scan) - - COM_START(stormfilter) - COM_TERM(enable, do_stormfilter_enable) - COM_TERM(disable, do_stormfilter_disable) - COM_TERM(set, do_stormfilter_set) - COM_TERM(show, do_stormfilter_show) - COM_END - - COM_TERM(tree, do_tree) - - COM_START(vlan) - COM_START(802.1q) - COM_TERM(del, do_vlan_8021q_del) - COM_TERM(set, do_vlan_8021q_set) - COM_TERM(show, do_vlan_8021q_show) - COM_END - COM_START(mode) - COM_TERM(set, do_vlan_mode_set) - COM_TERM(show, do_vlan_mode_show) - COM_END - COM_START(port) - COM_TERM(set, do_vlan_port_set) - COM_TERM(show, do_vlan_port_show) - COM_END - COM_START(pvid) - COM_TERM(set, do_vlan_pvid_set) - COM_TERM(show, do_vlan_pvid_show) - COM_END - COM_END -COM_ROOT_END - - diff --git a/cli/commands.h b/cli/commands.h deleted file mode 100644 index 2aeada5..0000000 --- a/cli/commands.h +++ /dev/null @@ -1,27 +0,0 @@ - -#ifndef DEF_COMMANDS -#define DEF_COMMANDS - - -#include "common.h" - - -struct TreeNode { - const char *name; - int (*comfunc)(int, const char**, struct ngadmin*); - const struct TreeNode *sub; -}; - - -#define COM_ROOT_START(v) const struct TreeNode v = {.name = "", .comfunc = NULL, .sub = (const struct TreeNode[]){ -#define COM_ROOT_END {.name = NULL, .comfunc = NULL, .sub = NULL}}}; -#define COM_START(nam) {.name = #nam, .comfunc = NULL, .sub = (const struct TreeNode[]){ -#define COM_END {.name = NULL, .comfunc = NULL, .sub = NULL}}}, -#define COM_TERM(nam, func) {.name = #nam, .comfunc = func, .sub = NULL}, - - -extern const struct TreeNode commands; - - -#endif - diff --git a/cli/common.c b/cli/common.c deleted file mode 100644 index 80f3f52..0000000 --- a/cli/common.c +++ /dev/null @@ -1,153 +0,0 @@ - -#include "common.h" - - -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* const bitrates[] = { - "nl", - "512K", - "1M", - "2M", - "4M", - "8M", - "16M", - "32M", - "64M", - "128M", - "256M", - "512M", - 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; -} - - -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 < nb; i++) { - printf("%i\t%s\t%s\t%s\t\t%s/", i, ether_ntoa(&sa[i].mac), sa[i].product, sa[i].name, inet_ntoa(sa[i].nc.ip)); - printf("%s\t%s\t%i\t%s\n", inet_ntoa(sa[i].nc.netmask), ( sa[i].nc.dhcp ? "Yes" : "No" ), sa[i].ports, sa[i].firmware); - } - - printf("\nfound %i switch(es)\n", nb); -} - - -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; - 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 deleted file mode 100644 index e50ae1e..0000000 --- a/cli/common.h +++ /dev/null @@ -1,38 +0,0 @@ - -#ifndef DEF_COMMON -#define DEF_COMMON - - -#include -#include -#include -#include -#include - -#include - - -#define UNUSED __attribute__((unused)) -#define NORET __attribute__((noreturn)) - - -extern int main_loop_continue; -extern struct termios current_term; - - -extern const char * const bitrates[], * const prio[]; - - -void displaySwitchTab (const struct swi_attr *sa, int nb); -void printErrCode (int err); -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 - diff --git a/cli/man/Makefile.am b/cli/man/Makefile.am new file mode 100644 index 0000000..cea1b3d --- /dev/null +++ b/cli/man/Makefile.am @@ -0,0 +1,3 @@ + +man_MANS = ngadmin.1 + diff --git a/cli/src/Makefile.am b/cli/src/Makefile.am new file mode 100644 index 0000000..9cb55f9 --- /dev/null +++ b/cli/src/Makefile.am @@ -0,0 +1,10 @@ + +bin_PROGRAMS = admin + +admin_SOURCES = admin.c com_bitrate.c com_cabletest.c com_defaults.c com_firmware.c \ + com_help.c com_igmp.c com_list.c com_login.c commands.c com_mirror.c \ + common.c com_name.c com_netconf.c com_password.c com_ports.c com_qos.c \ + com_quit.c com_restart.c com_scan.c com_stormfilter.c com_tree.c com_vlan.c +admin_CPPFLAGS = -I$(top_srcdir)/lib/include/ +admin_LDADD = $(top_builddir)/lib/src/libngadmin.la + diff --git a/cli/src/admin.c b/cli/src/admin.c new file mode 100644 index 0000000..bb4b310 --- /dev/null +++ b/cli/src/admin.c @@ -0,0 +1,393 @@ + +#include +#include +#include +#include + +#include +#include +#include + +#include "common.h" +#include "commands.h" + + +#define MAXCOM 32 + + +int main_loop_continue = 1; + + +static const struct TreeNode* getSubCom (char **com, int n, int *t) +{ + int i; + const struct TreeNode *cur, *next; + + + cur = &commands; + 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; +} + + +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; +} + + +static 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) + compcur = NULL; + matches = rl_completion_matches(text, my_generator); + + for (i = 0; com[i] != NULL; i++) + free(com[i]); + + + return matches; +} + + +static struct ngadmin *nga; +static sigjmp_buf jmpbuf; +static struct termios orig_term; +struct termios current_term; +static bool batch; + + +NORET static void handler (int sig) +{ + switch (sig) { + + case SIGTERM: + case SIGINT: + printf("interrupt\n"); + + current_term.c_lflag |= ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); + + if (!batch) + siglongjmp(jmpbuf, 1); + + default: + ngadmin_close(nga); + + tcsetattr(STDIN_FILENO, TCSANOW, &orig_term); + + exit(0); + } +} + + +static int pre_login (const struct ether_addr *mac, int retries) +{ + const struct swi_attr *sa; + int i, n, err; + + + for (i = 1; retries <= 0 || i <= retries; i++) { + /* scan */ + printf("scan... "); + fflush(stdout); + err = ngadmin_scan(nga); + if (err < 0) { + printErrCode(err); + continue; + } + + /* search switch with requested MAC */ + sa = ngadmin_getSwitchTab(nga, &n); + while (--n >= 0) { + if (memcmp(mac, &sa[n].mac, ETH_ALEN) == 0) + break; + } + + if (n < 0) { + printf("no switch found\n"); + } else { + printf("done\n"); + break; + } + } + + if (n < 0) + return 1; + + /* login */ + printf("login... "); + fflush(stdout); + err = ngadmin_login(nga, n); + if (err < 0) + printErrCode(err); + else + printf("done\n"); + + return err; +} + + +int main (int argc, char **argv) +{ + static const struct option opts[] = { + {"batch", no_argument, NULL, 'a'}, + {"keep-broadcasting", no_argument, NULL, 'b'}, + {"force-interface", no_argument, NULL, 'f'}, + {"global-broadcast", no_argument, NULL, 'g'}, + {"help", no_argument, NULL, 'h'}, + {"interface", required_argument, NULL, 'i'}, + {"mac", required_argument, NULL, 'm'}, + {"password", required_argument, NULL, 'p'}, + {"retries", required_argument, NULL, 'r'}, + {"timeout", required_argument, NULL, 't'}, + {0, 0, 0, 0} + }; + char *line, *com[MAXCOM]; + const char *iface = "eth0", *password = NULL; + float timeout = 0.f; + bool kb = false, force = false, global = false; + struct timeval tv; + const struct TreeNode *cur, *next; + struct ether_addr *mac = NULL; + int i, n, retries = 3; + + + tcgetattr(STDIN_FILENO, &orig_term); + current_term = orig_term; + batch = false; + + opterr = 0; + + while ((n = getopt_long(argc, argv, "abfghi:m:p:r:t:", opts, NULL)) != -1) { + switch (n) { + + case 'a': + batch = true; + break; + + case 'b': + kb = true; + break; + + case 'f': + force = true; + break; + + case 'g': + global = true; + break; + + case 'h': + printf("usage: %s [-a] [-b] [-f] [-g] [-i ] [-m ] [-p ]\n", argv[0]); + goto end; + + case 'i': + iface = optarg; + break; + + case 'm': + mac = ether_aton(optarg); + if (mac == NULL) { + printf("invalid MAC\n"); + goto end; + } + break; + + case 'p': + password = optarg; + break; + + case 'r': + retries = strtol(optarg, NULL, 0); + break; + + 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; + + /* non-TTY inputs are automatically set to batch mode */ + if (!isatty(STDIN_FILENO)) + batch = true; + + if (password != NULL) + ngadmin_setPassword(nga, password); + + signal(SIGTERM, handler); + signal(SIGINT, handler); + + /* automatic scan & login when switch MAC is specified on the command line */ + if (mac != NULL && pre_login(mac, retries) != 0) + goto end; + + if (batch) { + /* in batch mode, we must be logged to continue */ + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + goto end; + } + } else { + /* initialize readline functions */ + rl_attempted_completion_function = my_completion; + rl_completion_entry_function = my_generator; + + sigsetjmp(jmpbuf, 1); + } + + while (main_loop_continue) { + /* read user input */ + line = NULL; + n = 0; + if (batch) + n = getline(&line, (size_t*)&i, stdin); + else + line = readline("> "); + if (n < 0 || line == NULL) + goto end; + + /* split string into words */ + trim(line, strlen(line)); + n = explode(line, com, MAXCOM); + + if (n == 0) { + free(line); + continue; + } else { + if (!batch) + add_history(line); + free(line); + } + + cur = getSubCom(com, n, &i); + + if (cur->sub != NULL) { + /* not terminal command */ + if (i == 0) { + /* root command */ + printf("unknown command: %s\n", com[i]); + } else if (i < n) { + /* intermediate command, remaining string */ + printf("unknown %s subcommand: %s\n", com[i - 1], com[i]); + } else { + /* intermediate command, no remaining string */ + /* print available subcommands */ + for (next = cur->sub; next->name != NULL; next++) + printf("%s ", next->name); + putchar('\n'); + } + } else if (cur->comfunc == NULL) { + /* erroneous terminal command without function */ + printf("terminal command without function\n"); + } else { + /* execute terminal command */ + cur->comfunc(n - i, (const char**)&com[i], nga); + } + + for (i = 0; com[i] != NULL; i++) { + free(com[i]); + com[i] = NULL; + } + } + +end: + handler(0); +} + + diff --git a/cli/src/com_bitrate.c b/cli/src/com_bitrate.c new file mode 100644 index 0000000..7085b82 --- /dev/null +++ b/cli/src/com_bitrate.c @@ -0,0 +1,121 @@ + +#include "commands.h" + + +/* helper function to analyse bitrate speed specifications */ +static int bitrate_analyse (int argc, const char **argv, int *ports) +{ + int i = 0, s; + + + while (i < argc - 1) { + s = parseBitrate(argv[i + 1]); + if (strcmp(argv[i], "inout") == 0) { + ports[0] = s; + ports[1] = s; + } else if (strcmp(argv[i], "in") == 0) { + ports[0] = s; + } else if (strcmp(argv[i], "out") == 0) { + ports[1] = s; + } else { + break; + } + i += 2; + } + + + return i; +} + + +int do_bitrate_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i, k = 0, defs[] = {12, 12}, p, *ports = NULL, ret = 0; + const struct swi_attr *sa; + + + if (argc < 2) { + printf( + "usage: bitrate set [all SPEEDSPEC] SPEEDSPEC [ SPEEDSPEC ...]\n" + "SPEEDSPEC: [inout ] [in ] [out ]\n" + ); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ports = malloc(2 * sa->ports * sizeof(int)); + + /* get defaults if present */ + if (strcmp(argv[k], "all") == 0) { + k++; + k += bitrate_analyse(argc - k, &argv[k], defs); + } + + /* apply defaults */ + for (i = 0; i < sa->ports; i++) + memcpy(&ports[2 * i], defs, sizeof(defs)); + + /* get ports specifics */ + while (k < argc) { + p = strtol(argv[k++], NULL, 0) - 1; + if (p >= 0 && p ports) + k += bitrate_analyse(argc - k, &argv[k], &ports[2 * p]); + } + + /* send it to the switch */ + i = ngadmin_setBitrateLimits(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; +} + + +int do_bitrate_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, ret = 0, *ports = NULL; + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + + ports = malloc(2 * sa->ports * sizeof(int)); + i = ngadmin_getBitrateLimits(nga, ports); + if (i != ERR_OK) { + printErrCode(i); + ret = 1; + 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/src/com_cabletest.c b/cli/src/com_cabletest.c new file mode 100644 index 0000000..b5dfab3 --- /dev/null +++ b/cli/src/com_cabletest.c @@ -0,0 +1,49 @@ + +#include "commands.h" + + +int do_cabletest (int argc, const char **argv, struct ngadmin *nga) +{ + const struct swi_attr *sa; + struct cabletest *ct = NULL; + int i, j = 0, k = 0, ret = 0; + + + if (argc < 1) { + printf("usage: cabletest [ ...]\n"); + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ct = malloc(sa->ports * sizeof(struct cabletest)); + memset(ct, 0, sa->ports * sizeof(struct cabletest)); + + while (k < argc) { + ct[j].port = strtol(argv[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 = 1; + 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/src/com_defaults.c b/cli/src/com_defaults.c new file mode 100644 index 0000000..74ecfa5 --- /dev/null +++ b/cli/src/com_defaults.c @@ -0,0 +1,38 @@ + +#include "commands.h" + + +int do_defaults (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, ret = 0; + const struct swi_attr *sa; + char line[16]; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + 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/src/com_firmware.c b/cli/src/com_firmware.c new file mode 100644 index 0000000..3d5e76c --- /dev/null +++ b/cli/src/com_firmware.c @@ -0,0 +1,59 @@ + +#include "commands.h" + + +int do_firmware_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + const struct swi_attr *sa; + int ret = 0; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + puts(sa->firmware); + +end: + + return ret; +} + + +int do_firmware_upgrade (int argc, const char **argv, struct ngadmin *nga) +{ + const struct swi_attr *sa; + int i, ret = 0; + + + if (argc != 1) { + printf("usage: firmware upgrade \n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + i = ngadmin_upgradeFirmware(nga, argv[0]); + printErrCode(i); + +end: + + return ret; +} + + diff --git a/cli/src/com_help.c b/cli/src/com_help.c new file mode 100644 index 0000000..34fc2bc --- /dev/null +++ b/cli/src/com_help.c @@ -0,0 +1,25 @@ + +#include "commands.h" + + +bool do_help (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED) +{ + const struct TreeNode *s; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + printf("Available commands: \n"); + + for (s = commands.sub; s->name != NULL; s++) + printf("%s ", s->name); + putchar('\n'); + + + return 0; +} + + diff --git a/cli/src/com_igmp.c b/cli/src/com_igmp.c new file mode 100644 index 0000000..55e12da --- /dev/null +++ b/cli/src/com_igmp.c @@ -0,0 +1,71 @@ + +#include "commands.h" + + +int do_igmp_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i; + struct igmp_conf ic; + + + if (argc != 4) { + printf("usage: igmp set \n"); + return 1; + } + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + return 1; + } + + ic.enable = strtol(argv[0], NULL, 0); + ic.vlan = strtol(argv[1], NULL, 0); + ic.validate = strtol(argv[2], NULL, 0); + ic.block = strtol(argv[3], NULL, 0); + + i = ngadmin_setIGMPConf(nga, &ic); + printErrCode(i); + + + return 0; +} + + +int do_igmp_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, ret = 0; + const struct swi_attr *sa; + struct igmp_conf ic; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + i = ngadmin_getIGMPConf(nga, &ic); + if (i != ERR_OK) { + printErrCode(i); + ret = 1; + 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/src/com_list.c b/cli/src/com_list.c new file mode 100644 index 0000000..853f8f3 --- /dev/null +++ b/cli/src/com_list.c @@ -0,0 +1,23 @@ + +#include "commands.h" + + +bool do_list (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int n; + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + sa = ngadmin_getSwitchTab(nga, &n); + displaySwitchTab(sa, n); + + + return 0; +} + + diff --git a/cli/src/com_login.c b/cli/src/com_login.c new file mode 100644 index 0000000..a6ee7e8 --- /dev/null +++ b/cli/src/com_login.c @@ -0,0 +1,23 @@ + +#include "commands.h" + + +bool do_login (int argc, const char **argv, struct ngadmin *nga) +{ + int i; + + + if (argc != 1) { + printf("usage: login \n"); + return 1; + } + + i = strtol(argv[0], NULL, 0); + i = ngadmin_login(nga, i); + printErrCode(i); + + + return 0; +} + + diff --git a/cli/src/com_mirror.c b/cli/src/com_mirror.c new file mode 100644 index 0000000..5ebe040 --- /dev/null +++ b/cli/src/com_mirror.c @@ -0,0 +1,126 @@ + +#include "commands.h" + + +int do_mirror_disable (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + return 1; + } + + i = ngadmin_setMirror(nga, NULL); + printErrCode(i); + + + return 0; +} + + +int do_mirror_set (int argc, const char **argv, struct ngadmin *nga) +{ + const struct swi_attr *sa; + char *ports = NULL; + int i, k = 0, ret = 0; + + + if (argc < 3) { + printf("usage: mirror set clone [ ...]\n"); + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ports = malloc((sa->ports + 1) * sizeof(char)); + memset(ports, 0, sa->ports + 1); + + ports[0] = strtol(argv[k++], NULL, 0); + if (ports[0] < 1 || ports[0] > sa->ports || strcasecmp(argv[k++], "clone") != 0) { + printf("syntax error\n"); + ret = 1; + goto end; + } + + while (k < argc) { + i = strtol(argv[k++], NULL, 0); + if (i < 1 || i > sa->ports) { + printf("port out of range\n"); + ret = 1; + goto end; + } else if (i == ports[0]) { + printf("destination port cannot be in port list\n"); + ret = 1; + goto end; + } + ports[i] = 1; + } + + i = ngadmin_setMirror(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; +} + + +int do_mirror_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + const struct swi_attr *sa; + char *ports = NULL; + int i; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + 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 0; +} + + diff --git a/cli/src/com_name.c b/cli/src/com_name.c new file mode 100644 index 0000000..dd7b048 --- /dev/null +++ b/cli/src/com_name.c @@ -0,0 +1,77 @@ + +#include "commands.h" + + +int do_name_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + puts(sa->name); + + + return 0; +} + + +int do_name_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (argc != 1) { + printf("usage: name set \n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + i = ngadmin_setName(nga, argv[0]); + printErrCode(i); + + + return 0; +} + + +int do_name_clear (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + i = ngadmin_setName(nga, NULL); + printErrCode(i); + + + return 0; +} + + diff --git a/cli/src/com_netconf.c b/cli/src/com_netconf.c new file mode 100644 index 0000000..d44b83a --- /dev/null +++ b/cli/src/com_netconf.c @@ -0,0 +1,69 @@ + +#include "commands.h" + + +int do_netconf_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i, k, ret = 0; + const struct swi_attr *sa; + struct net_conf nc; + + + if (argc == 0) { + printf("usage: netconf set [dhcp yes|no] [ip ] [mask ] [gw ]\n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + memset(&nc, 0, sizeof(struct net_conf)); + + for (k = 0; k < argc; k += 2) { + if (strcasecmp(argv[k], "dhcp") == 0) { + if (strcasecmp(argv[k + 1], "yes") == 0) { + nc.dhcp = true; + } else if (strcasecmp(argv[k + 1], "no") == 0) { + nc.dhcp = 1; + } else { + printf("Incorrect DHCP value\n"); + ret = 1; + goto end; + } + } else if (strcasecmp(argv[k], "ip") == 0) { + if (inet_aton(argv[k + 1], &nc.ip) == 0) { + printf("Incorrect IP value\n"); + ret = 1; + goto end; + } + } else if (strcasecmp(argv[k], "mask") == 0) { + /* TODO: check if it is a correct mask */ + if (inet_aton(argv[k + 1], &nc.netmask) == 0) { + printf("Incorrect mask value\n"); + ret = 1; + goto end; + } + } else if (strcasecmp(argv[k], "gw") == 0) { + if (inet_aton(argv[k + 1], &nc.gw) == 0) { + printf("Incorrect gateway value\n"); + ret = 1; + goto end; + } + } + } + + i = ngadmin_setNetConf(nga, &nc); + if (i != ERR_OK) { + printErrCode(i); + ret = 1; + } + +end: + + return ret; +} + + diff --git a/cli/src/com_password.c b/cli/src/com_password.c new file mode 100644 index 0000000..0303b2b --- /dev/null +++ b/cli/src/com_password.c @@ -0,0 +1,65 @@ + +#include "commands.h" + + +int do_password_change (int argc, const char **argv, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (argc != 1) { + printf("usage: password change \n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + i = ngadmin_changePassword(nga, argv[0]); + printErrCode(i); + + + return 0; +} + + +int do_password_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i; + char buf[64]; + const char *pass; + + + if (argc > 1) { + printf("usage: password set []\n"); + return 1; + } + + if (argc == 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 = argv[0]; + } + + if (pass != NULL) { + i = ngadmin_setPassword(nga, pass); + printErrCode(i); + } + + + return 0; +} + + diff --git a/cli/src/com_ports.c b/cli/src/com_ports.c new file mode 100644 index 0000000..3337b43 --- /dev/null +++ b/cli/src/com_ports.c @@ -0,0 +1,127 @@ + +#include "commands.h" + + +int do_ports_state (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, ret = 0; + const struct swi_attr *sa; + unsigned char *ports = NULL; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ports = malloc(sa->ports * sizeof(unsigned char)); + i = ngadmin_getPortsStatus(nga, ports); + if (i < 0) { + printErrCode(i); + ret = 1; + 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; +} + + +int do_ports_statistics_reset (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + return 1; + } + + i = ngadmin_resetPortsStatistics(nga); + printErrCode(i); + + return 0; +} + + +int do_ports_statistics_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, ret = 0; + const struct swi_attr *sa; + struct port_stats *ps = NULL; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ps = calloc(sa->ports, sizeof(struct port_stats)); + i = ngadmin_getPortsStatistics(nga, ps); + if (i < 0) { + printErrCode(i); + ret = 1; + 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/src/com_qos.c b/cli/src/com_qos.c new file mode 100644 index 0000000..7b51bd2 --- /dev/null +++ b/cli/src/com_qos.c @@ -0,0 +1,154 @@ + +#include "commands.h" + + +int do_qos_mode (int argc, const char **argv, struct ngadmin *nga) +{ + int i, s, ret = 0; + const struct swi_attr *sa; + + + if (argc == 0) { + printf("usage: qos mode port|802.1p\n"); + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + if (strcasecmp(argv[0], "port") == 0) { + s = QOS_PORT; + } else if (strcasecmp(argv[0], "802.1p") == 0) { + s = QOS_DOT; + } else { + printf("Unknown QOS mode\n"); + ret = 1; + goto end; + } + + i = ngadmin_setQOSMode(nga, s); + printErrCode(i); + +end: + + return ret; +} + + +int do_qos_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i, p, ret = 0; + const struct swi_attr *sa; + char d = PRIO_UNSPEC, *ports = NULL; + + + if (argc < 2) { + printf("usage: qos set (all )|( [ ...])\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa ==NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ports = malloc(sa->ports * sizeof(char)); + + /* read defaults */ + if (strcmp(argv[0], "all") == 0) { + d = parsePrio(argv[1]); + argv += 2; + argc -= 2; + } + + /* apply defaults */ + for (i = 0; i < sa->ports; i++) + ports[i] = d; + + /* read and apply port specifics */ + for (i = 0; i < argc; i += 2) { + p = strtol(argv[i], NULL, 0); + if (p < 1 || p > sa->ports) + continue; + ports[p - 1] = parsePrio(argv[i + 1]); + } + + /* send the new configuration to the switch */ + i = ngadmin_setQOSValues(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; +} + + +int do_qos_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, s = 0, ret = 0; + const struct swi_attr *sa; + char *ports = NULL; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + i = ngadmin_getQOSMode(nga, &s); + if (i != ERR_OK) { + printErrCode(i); + ret = 1; + 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 = 1; + 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/src/com_quit.c b/cli/src/com_quit.c new file mode 100644 index 0000000..f27620b --- /dev/null +++ b/cli/src/com_quit.c @@ -0,0 +1,17 @@ + +#include "commands.h" + + +int do_quit (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED) +{ + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + main_loop_continue = 0; + + return 0; +} + + diff --git a/cli/src/com_restart.c b/cli/src/com_restart.c new file mode 100644 index 0000000..c464828 --- /dev/null +++ b/cli/src/com_restart.c @@ -0,0 +1,38 @@ + +#include "commands.h" + + +int do_restart (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED) +{ + int i, ret = 0; + const struct swi_attr *sa; + char line[16]; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + 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/src/com_scan.c b/cli/src/com_scan.c new file mode 100644 index 0000000..f374d18 --- /dev/null +++ b/cli/src/com_scan.c @@ -0,0 +1,29 @@ + +#include "commands.h" + + +int do_scan (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + i = ngadmin_scan(nga); + if (i < 0) { + printErrCode(i); + return 1; + } + + sa = ngadmin_getSwitchTab(nga, &i); + displaySwitchTab(sa, i); + + + return 0; +} + + diff --git a/cli/src/com_stormfilter.c b/cli/src/com_stormfilter.c new file mode 100644 index 0000000..dbd91f8 --- /dev/null +++ b/cli/src/com_stormfilter.c @@ -0,0 +1,156 @@ + +#include "commands.h" + + +int do_stormfilter_enable (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + i = ngadmin_setStormFilterState(nga, 1); + printErrCode(i); + + + return 0; +} + + +int do_stormfilter_disable (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + i = ngadmin_setStormFilterState(nga, 0); + printErrCode(i); + + + return 0; +} + + +int do_stormfilter_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i, d = BITRATE_UNSPEC, p, *ports = NULL, ret = 0; + const struct swi_attr *sa; + + + if (argc < 2) { + printf("usage: stormfilt set (all )|( [ ...])\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ports = malloc(sa->ports * sizeof(int)); + + /* read defaults */ + if (strcmp(argv[0], "all") == 0) { + d = parseBitrate(argv[1]); + argv += 2; + argc -= 2; + } + + /* apply defaults */ + for (i = 0; i < sa->ports; i++) + ports[i] = d; + + /* read and apply port specifics */ + for (i = 0; i < argc - 1; i += 2) { + p = strtol(argv[i], NULL, 0); + if (p < 1 || p > sa->ports) + continue; + ports[p - 1] = parseBitrate(argv[i + 1]); + } + + /* send the new configuration to the switch */ + i = ngadmin_setStormFilterValues(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; +} + + +int do_stormfilter_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, s, ret = 0, *ports = NULL; + const struct swi_attr *sa; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + i = ngadmin_getStormFilterState(nga, &s); + if (i != ERR_OK) { + printErrCode(i); + ret = 1; + 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 = 1; + 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/src/com_tree.c b/cli/src/com_tree.c new file mode 100644 index 0000000..b1dadff --- /dev/null +++ b/cli/src/com_tree.c @@ -0,0 +1,35 @@ + +#include "commands.h" + + +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); +} + + +int do_tree (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED) +{ + if (argc > 0) { + printf("this command takes no argument\n"); + return 1; + } + + display_node(&commands, 0); + + return 0; +} + + diff --git a/cli/src/com_vlan.c b/cli/src/com_vlan.c new file mode 100644 index 0000000..3496815 --- /dev/null +++ b/cli/src/com_vlan.c @@ -0,0 +1,510 @@ + +#include "commands.h" + + + +static char vlan_char (int t) +{ + switch (t) { + + case VLAN_TAGGED: + return 'T'; + + case VLAN_UNTAGGED: + return 'U'; + + case VLAN_NO: + return ' '; + + default: + return '?'; + } +} + + +int do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga) +{ + const struct swi_attr *sa; + unsigned short vlan; + int i; + + + if (argc != 1) { + printf("usage: vlan 8021q del \n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + vlan=strtoul(argv[0], NULL, 0); + if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) { + printf("vlan out of range\n"); + return 1; + } + + i = ngadmin_VLANDestroy(nga, vlan); + printErrCode(i); + + + return 0; +} + + +int do_vlan_port_set (int argc, const char **argv, struct ngadmin *nga) +{ + unsigned char vlan, port, *ports = NULL; + const struct swi_attr *sa; + int i, k = 0, ret = 0; + + + if (argc < 2) { + printf("usage: vlan port set [all ] [ ] [ ] [...]\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ports = malloc(sa->ports * sizeof(unsigned char)); + + /* read defaults */ + vlan = 0; + if (strcmp(argv[k], "all") == 0) { + k++; + vlan = strtoul(argv[k++], NULL, 0); + /* VLAN 0 is allowed and means no change */ + if (vlan > VLAN_PORT_MAX) { + printf("vlan out of range\n"); + ret = 1; + goto end; + } + } + + /* apply defaults */ + memset(ports, vlan, sa->ports); + + /* read and apply port specifics */ + while (k < argc - 1) { + /* read port */ + port = strtoul(argv[k++], NULL, 0); + if (port < 1 || port > sa->ports) { + printf("port out of range\n"); + ret = 1; + goto end; + } + + /* read vlan */ + vlan = strtoul(argv[k++], NULL, 0); + /* VLAN 0 is allowed and means no change */ + if (vlan > VLAN_PORT_MAX) { + printf("vlan out of range\n"); + ret = 1; + goto end; + } + + ports[port - 1] = vlan; + } + + /* set conf */ + i = ngadmin_setVLANPortConf(nga, ports); + printErrCode(i); + +end: + free(ports); + + return ret; +} + + +int do_vlan_port_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + unsigned char *ports = NULL; + const struct swi_attr *sa; + int i, ret = 0; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ports = malloc(sa->ports * sizeof(unsigned char)); + + /* request all VLANs config */ + i = ngadmin_getVLANPortConf(nga, ports); + + if (i != ERR_OK) { + printErrCode(i); + ret = 1; + goto end; + } + + printf("Ports configuration: \n"); + printf("Port\t"); + for (i = 1; i <= sa->ports; i++) + printf("%i\t", i); + putchar('\n'); + + /* show all VLANs */ + printf("VLAN\t"); + for (i = 0; i < sa->ports; i++) + printf("%u\t", ports[i]); + putchar('\n'); + +end: + free(ports); + + return ret; +} + + +int do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga) +{ + unsigned char *ports = NULL, p, def = VLAN_UNSPEC; + const struct swi_attr *sa; + unsigned short vlan; + int i, k = 0, ret = 0; + + + if (argc == 0) { + printf("usage: vlan 802.1q set [all unspec|no|untagged|tagged] [ unspec|no|untagged|tagged ...]\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + /* read vlan */ + vlan = strtoul(argv[k++], NULL, 0); + + if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) { + printf("vlan out of range\n"); + ret = 1; + goto end; + } + + /* read defaults */ + if (k < argc - 1 && strcasecmp(argv[k], "all") == 0) { + k++; + if (strcasecmp(argv[k], "tagged") == 0) { + def = VLAN_TAGGED; + } else if (strcasecmp(argv[k], "untagged") == 0) { + def = VLAN_UNTAGGED; + } else if (strcasecmp(argv[k], "no") == 0) { + def = VLAN_NO; + } else if (strcasecmp(argv[k], "unspec") == 0) { + def = VLAN_UNSPEC; + } else { + printf("incorrect type\n"); + ret = 1; + goto end; + } + k++; + } + + ports = malloc(sa->ports * sizeof(unsigned char)); + + /* apply defaults */ + memset(ports, def, sa->ports); + + /* read and apply port specifics */ + while (k < argc - 1) { + p = strtoul(argv[k++], NULL, 0) - 1; + if (p >= sa->ports) { + printf("port out of range\n"); + ret = 1; + goto end; + } + if (strcasecmp(argv[k], "tagged") ==0) { + ports[p] = VLAN_TAGGED; + } else if (strcasecmp(argv[k], "untagged") == 0) { + ports[p] = VLAN_UNTAGGED; + } else if (strcasecmp(argv[k], "no") == 0) { + ports[p] = VLAN_NO; + } else if (strcasecmp(argv[k], "unspec") == 0) { + ports[p] = VLAN_UNSPEC; + } else { + printf("incorrect type\n"); + ret = 1; + goto end; + } + k++; + } + + /* set conf */ + i = ngadmin_setVLANDotConf(nga, vlan, ports); + printErrCode(i); + +end: + free(ports); + + return ret; +} + + +int do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga) +{ + unsigned short vl = 0, *vlans = NULL; + unsigned char *ports = NULL; + const struct swi_attr *sa; + int i, j, n = 16, ret = 0; + + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + if (argc > 0) + vl = strtoul(argv[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 = 1; + 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; +} + + +int do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga) +{ + int mode, i; + + + if (argc == 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 0; + } + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + return 1; + } + + mode = strtoul(argv[0], NULL, 0); + if (mode < 1 || mode > 4) { + printf("mode out of range\n"); + return 1; + } + + i = ngadmin_setVLANType(nga, mode); + printErrCode(i); + + + return 0; +} + + +int do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + int i, t, ret = 0; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + if (ngadmin_getCurrentSwitch(nga) == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + i = ngadmin_getVLANType(nga, &t); + if (i != ERR_OK) { + printErrCode(i); + ret = 1; + 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; +} + + +int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga) +{ + const struct swi_attr *sa; + unsigned char port; + unsigned short vlan; + int i; + + + if (argc != 2) { + printf("usage: vlan pvid set \n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + port = strtoul(argv[0], NULL, 0); + vlan = strtoul(argv[1], NULL, 0); + + if (port < 1 || port > sa->ports) { + printf("port out of range\n"); + return 1; + } + + if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) { + printf("vlan out of range\n"); + return 1; + } + + i = ngadmin_setPVID(nga, port, vlan); + printErrCode(i); + + + return 0; +} + + +int do_vlan_pvid_show (int argc, const char **argv UNUSED, struct ngadmin *nga) +{ + unsigned short *ports = NULL; + const struct swi_attr *sa; + int i, ret = 0; + + + if (argc > 0) { + printf("this command takes no argument\n"); + ret = 1; + goto end; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + ret = 1; + goto end; + } + + ports = malloc(sa->ports * sizeof(unsigned short)); + i = ngadmin_getAllPVID(nga, ports); + if (i != ERR_OK) { + printErrCode(i); + ret = 1; + 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/src/commands.c b/cli/src/commands.c new file mode 100644 index 0000000..2d37ea8 --- /dev/null +++ b/cli/src/commands.c @@ -0,0 +1,206 @@ + +#include "commands.h" + + +/* bitrate */ +int do_bitrate_set (int argc, const char **argv, struct ngadmin *nga); +int do_bitrate_show (int argc, const char **argv, struct ngadmin *nga); + + +/* cabletest */ +int do_cabletest (int argc, const char **argv, struct ngadmin *nga); + + +/* defaults */ +int do_defaults (int argc, const char **argv, struct ngadmin *nga); + + +/* firmware */ +int do_firmware_show (int argc, const char **argv, struct ngadmin *nga); +int do_firmware_upgrade (int argc, const char **argv, struct ngadmin *nga); + + +/* help */ +int do_help (int argc, const char **argv, struct ngadmin *nga); + + +/* igmp */ +int do_igmp_set (int argc, const char **argv, struct ngadmin *nga); +int do_igmp_show (int argc, const char **argv, struct ngadmin *nga); + + +/* list */ +int do_list (int argc, const char **argv, struct ngadmin *nga); + + +/* login */ +int do_login (int argc, const char **argv, struct ngadmin *nga); + + +/* mirror */ +int do_mirror_disable (int argc, const char **argv, struct ngadmin *nga); +int do_mirror_set (int argc, const char **argv, struct ngadmin *nga); +int do_mirror_show (int argc, const char **argv, struct ngadmin *nga); + + +/* name */ +int do_name_show (int argc, const char **argv, struct ngadmin *nga); +int do_name_set (int argc, const char **argv, struct ngadmin *nga); +int do_name_clear (int argc, const char **argv, struct ngadmin *nga); + + +/* netconf */ +int do_netconf_set (int argc, const char **argv, struct ngadmin *nga); + + +/* password */ +int do_password_change (int argc, const char **argv, struct ngadmin *nga); +int do_password_set (int argc, const char **argv, struct ngadmin *nga); + + +/* ports */ +int do_ports_state (int argc, const char **argv, struct ngadmin *nga); +int do_ports_statistics_reset (int argc, const char **argv, struct ngadmin *nga); +int do_ports_statistics_show (int argc, const char **argv, struct ngadmin *nga); + + +/* qos */ +int do_qos_mode (int argc, const char **argv, struct ngadmin *nga); +int do_qos_set (int argc, const char **argv, struct ngadmin *nga); +int do_qos_show (int argc, const char **argv, struct ngadmin *nga); + + +/* quit */ +int do_quit (int argc, const char **argv, struct ngadmin *nga); + + +/* restart */ +int do_restart (int argc, const char **argv, struct ngadmin *nga); + + +/* scan */ +int do_scan (int argc, const char **argv, struct ngadmin *nga); + + +/* stormfilter */ +int do_stormfilter_enable (int argc, const char **argv, struct ngadmin *nga); +int do_stormfilter_disable (int argc, const char **argv, struct ngadmin *nga); +int do_stormfilter_set (int argc, const char **argv, struct ngadmin *nga); +int do_stormfilter_show (int argc, const char **argv, struct ngadmin *nga); + + +/* tree */ +int do_tree (int argc, const char **argv, struct ngadmin *nga); + + +/* vlan */ +int do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga); +int do_vlan_port_set (int argc, const char **argv, struct ngadmin *nga); +int do_vlan_port_show (int argc, const char **argv, struct ngadmin *nga); +int do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga); +int do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga); +int do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga); +int do_vlan_mode_show (int argc, const char **argv, struct ngadmin *nga); +int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga); +int do_vlan_pvid_show (int argc, const char **argv, struct ngadmin *nga); + + +/* commands structure */ +COM_ROOT_START(commands) + COM_START(bitrate) + COM_TERM(set, do_bitrate_set) + COM_TERM(show, do_bitrate_show) + COM_END + + COM_TERM(cabletest, do_cabletest) + + COM_TERM(defaults, do_defaults) + + COM_START(firmware) + COM_TERM(show, do_firmware_show) + COM_TERM(upgrade, do_firmware_upgrade) + COM_END + + COM_TERM(help, do_help) + + COM_START(igmp) + COM_TERM(set, do_igmp_set) + COM_TERM(show, do_igmp_show) + COM_END + + COM_TERM(list, do_list) + + COM_TERM(login, do_login) + + COM_START(mirror) + COM_TERM(disable, do_mirror_disable) + COM_TERM(set, do_mirror_set) + COM_TERM(show, do_mirror_show) + COM_END + + COM_START(name) + COM_TERM(show, do_name_show) + COM_TERM(set, do_name_set) + COM_TERM(clear, do_name_clear) + COM_END + + COM_START(netconf) + COM_TERM(set, do_netconf_set) + COM_END + + COM_START(password) + COM_TERM(change, do_password_change) + COM_TERM(set, do_password_set) + COM_END + + COM_START(ports) + COM_TERM(state, do_ports_state) + COM_START(statistics) + COM_TERM(reset, do_ports_statistics_reset) + COM_TERM(show, do_ports_statistics_show) + COM_END + COM_END + + COM_START(qos) + COM_TERM(mode, do_qos_mode) + COM_TERM(set, do_qos_set) + COM_TERM(show, do_qos_show) + COM_END + + COM_TERM(quit, do_quit) + + COM_TERM(restart, do_restart) + + COM_TERM(scan, do_scan) + + COM_START(stormfilter) + COM_TERM(enable, do_stormfilter_enable) + COM_TERM(disable, do_stormfilter_disable) + COM_TERM(set, do_stormfilter_set) + COM_TERM(show, do_stormfilter_show) + COM_END + + COM_TERM(tree, do_tree) + + COM_START(vlan) + COM_START(802.1q) + COM_TERM(del, do_vlan_8021q_del) + COM_TERM(set, do_vlan_8021q_set) + COM_TERM(show, do_vlan_8021q_show) + COM_END + COM_START(mode) + COM_TERM(set, do_vlan_mode_set) + COM_TERM(show, do_vlan_mode_show) + COM_END + COM_START(port) + COM_TERM(set, do_vlan_port_set) + COM_TERM(show, do_vlan_port_show) + COM_END + COM_START(pvid) + COM_TERM(set, do_vlan_pvid_set) + COM_TERM(show, do_vlan_pvid_show) + COM_END + COM_END +COM_ROOT_END + + diff --git a/cli/src/commands.h b/cli/src/commands.h new file mode 100644 index 0000000..2aeada5 --- /dev/null +++ b/cli/src/commands.h @@ -0,0 +1,27 @@ + +#ifndef DEF_COMMANDS +#define DEF_COMMANDS + + +#include "common.h" + + +struct TreeNode { + const char *name; + int (*comfunc)(int, const char**, struct ngadmin*); + const struct TreeNode *sub; +}; + + +#define COM_ROOT_START(v) const struct TreeNode v = {.name = "", .comfunc = NULL, .sub = (const struct TreeNode[]){ +#define COM_ROOT_END {.name = NULL, .comfunc = NULL, .sub = NULL}}}; +#define COM_START(nam) {.name = #nam, .comfunc = NULL, .sub = (const struct TreeNode[]){ +#define COM_END {.name = NULL, .comfunc = NULL, .sub = NULL}}}, +#define COM_TERM(nam, func) {.name = #nam, .comfunc = func, .sub = NULL}, + + +extern const struct TreeNode commands; + + +#endif + diff --git a/cli/src/common.c b/cli/src/common.c new file mode 100644 index 0000000..80f3f52 --- /dev/null +++ b/cli/src/common.c @@ -0,0 +1,153 @@ + +#include "common.h" + + +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* const bitrates[] = { + "nl", + "512K", + "1M", + "2M", + "4M", + "8M", + "16M", + "32M", + "64M", + "128M", + "256M", + "512M", + 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; +} + + +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 < nb; i++) { + printf("%i\t%s\t%s\t%s\t\t%s/", i, ether_ntoa(&sa[i].mac), sa[i].product, sa[i].name, inet_ntoa(sa[i].nc.ip)); + printf("%s\t%s\t%i\t%s\n", inet_ntoa(sa[i].nc.netmask), ( sa[i].nc.dhcp ? "Yes" : "No" ), sa[i].ports, sa[i].firmware); + } + + printf("\nfound %i switch(es)\n", nb); +} + + +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; + 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/src/common.h b/cli/src/common.h new file mode 100644 index 0000000..e50ae1e --- /dev/null +++ b/cli/src/common.h @@ -0,0 +1,38 @@ + +#ifndef DEF_COMMON +#define DEF_COMMON + + +#include +#include +#include +#include +#include + +#include + + +#define UNUSED __attribute__((unused)) +#define NORET __attribute__((noreturn)) + + +extern int main_loop_continue; +extern struct termios current_term; + + +extern const char * const bitrates[], * const prio[]; + + +void displaySwitchTab (const struct swi_attr *sa, int nb); +void printErrCode (int err); +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 + diff --git a/configure.ac b/configure.ac index 0e759eb..d063ae5 100644 --- a/configure.ac +++ b/configure.ac @@ -1,15 +1,21 @@ AC_PREREQ([2.68]) AC_INIT([ngadmin], [0.1], [admin@darkcoven.tk]) +AC_CONFIG_MACRO_DIR([.]) AC_CONFIG_SRCDIR([raw/src/attr.c]) AC_CONFIG_HEADERS([config.h]) AM_INIT_AUTOMAKE +LT_PREREQ([2.4]) +LT_INIT + # Checks for programs AC_PROG_CC +AM_PROG_CC_C_O # Checks for libraries +AC_CHECK_LIB([readline], [readline]) # Checks for header files AC_CHECK_HEADERS([arpa/inet.h stdlib.h string.h sys/ioctl.h termios.h unistd.h]) @@ -27,11 +33,14 @@ AC_CHECK_FUNCS([inet_ntoa memchr memset select socket strcasecmp strdup strtol s AC_CONFIG_FILES([ Makefile raw/Makefile + raw/include/Makefile raw/src/Makefile lib/Makefile lib/include/Makefile lib/src/Makefile cli/Makefile + cli/man/Makefile + cli/src/Makefile ]) AC_OUTPUT diff --git a/lib/src/Makefile.am b/lib/src/Makefile.am index c2a1d50..bc789c4 100644 --- a/lib/src/Makefile.am +++ b/lib/src/Makefile.am @@ -3,7 +3,7 @@ lib_LTLIBRARIES = libngadmin.la libngadmin_la_SOURCES = network.c bitrate.c firmware.c libconf.c mirror.c misc.c \ netconf.c ports.c qos.c session.c vlan.c -libngadmin_la_CPPFLAGS = -DBUILD_LIB -I../include/ -I../../raw/include/ +libngadmin_la_CPPFLAGS = -DBUILD_LIB -I$(top_srcdir)/raw/include/ -I$(top_srcdir)/lib/include/ libngadmin_la_CFLAGS = -fno-strict-aliasing -libngadmin_la_LIBADD = ../../raw/src/librawnsdp.la +libngadmin_la_LIBADD = $(top_builddir)/raw/src/librawnsdp.la diff --git a/raw/Makefile.am b/raw/Makefile.am index 6b75694..acfa633 100644 --- a/raw/Makefile.am +++ b/raw/Makefile.am @@ -1,3 +1,3 @@ -SUBDIRS = src +SUBDIRS = include src diff --git a/raw/include/Makefile.am b/raw/include/Makefile.am new file mode 100644 index 0000000..2183ae6 --- /dev/null +++ b/raw/include/Makefile.am @@ -0,0 +1,3 @@ + +noinst_HEADERS = attr.h list.h protocol.h + diff --git a/raw/src/Makefile.am b/raw/src/Makefile.am index 562d502..bf354b0 100644 --- a/raw/src/Makefile.am +++ b/raw/src/Makefile.am @@ -2,6 +2,6 @@ noinst_LTLIBRARIES = librawnsdp.la librawnsdp_la_SOURCES = attr.c list.c protocol.c -librawnsdp_la_CPPFLAGS = -I../include/ -I../../lib/include/ +librawnsdp_la_CPPFLAGS = -I$(top_srcdir)/raw/include/ -I$(top_srcdir)/lib/include/ librawnsdp_la_CFLAGS = -fno-strict-aliasing