install-sh
ltmain.sh
missing
+depcomp
build-aux/
libtool
stamp-h1
+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
-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
+++ /dev/null
-
-#include <stdio.h>
-#include <signal.h>
-#include <unistd.h>
-#include <setjmp.h>
-
-#include <getopt.h>
-#include <readline/readline.h>
-#include <readline/history.h>
-
-#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 <interface>] [-m <MAC>] [-p <password>]\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);
-}
-
-
+++ /dev/null
-
-#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] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n"
- "SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\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 <sa->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;
-}
-
-
-
+++ /dev/null
-
-#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 <port1> [<port2> ...]\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;
-}
-
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#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 <file>\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;
-}
-
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#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 <enable> <vlan> <validate> <block>\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;
-}
-
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#include "commands.h"
-
-
-bool do_login (int argc, const char **argv, struct ngadmin *nga)
-{
- int i;
-
-
- if (argc != 1) {
- printf("usage: login <num>\n");
- return 1;
- }
-
- i = strtol(argv[0], NULL, 0);
- i = ngadmin_login(nga, i);
- printErrCode(i);
-
-
- return 0;
-}
-
-
+++ /dev/null
-
-#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 <destination port> clone <port1> [<port2> ...]\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;
-}
-
-
+++ /dev/null
-
-#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 <value>\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;
-}
-
-
+++ /dev/null
-
-#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 <ip>] [mask <mask>] [gw <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;
-}
-
-
+++ /dev/null
-
-#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 <value>\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 [<value>]\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;
-}
-
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#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 <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\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;
-}
-
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#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 <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\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;
-}
-
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#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 <vlan>\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 <vlan>] [<port1> <vlan>] [<port2> <vlan>] [...]\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 <vlan> [all unspec|no|untagged|tagged] [<port1> 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 <mode>\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 <port> <vlan>\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;
-}
-
-
+++ /dev/null
-
-#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
-
-
+++ /dev/null
-
-#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 = "<root>", .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
-
+++ /dev/null
-
-#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;
-}
-
-
+++ /dev/null
-
-#ifndef DEF_COMMON
-#define DEF_COMMON
-
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-
-#include <ngadmin.h>
-
-
-#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
-
--- /dev/null
+
+man_MANS = ngadmin.1
+
--- /dev/null
+
+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
+
--- /dev/null
+
+#include <stdio.h>
+#include <signal.h>
+#include <unistd.h>
+#include <setjmp.h>
+
+#include <getopt.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#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 <interface>] [-m <MAC>] [-p <password>]\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);
+}
+
+
--- /dev/null
+
+#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] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n"
+ "SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\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 <sa->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;
+}
+
+
+
--- /dev/null
+
+#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 <port1> [<port2> ...]\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;
+}
+
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#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 <file>\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;
+}
+
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#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 <enable> <vlan> <validate> <block>\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;
+}
+
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#include "commands.h"
+
+
+bool do_login (int argc, const char **argv, struct ngadmin *nga)
+{
+ int i;
+
+
+ if (argc != 1) {
+ printf("usage: login <num>\n");
+ return 1;
+ }
+
+ i = strtol(argv[0], NULL, 0);
+ i = ngadmin_login(nga, i);
+ printErrCode(i);
+
+
+ return 0;
+}
+
+
--- /dev/null
+
+#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 <destination port> clone <port1> [<port2> ...]\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;
+}
+
+
--- /dev/null
+
+#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 <value>\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;
+}
+
+
--- /dev/null
+
+#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 <ip>] [mask <mask>] [gw <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;
+}
+
+
--- /dev/null
+
+#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 <value>\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 [<value>]\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;
+}
+
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#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 <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\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;
+}
+
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#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 <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\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;
+}
+
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#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 <vlan>\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 <vlan>] [<port1> <vlan>] [<port2> <vlan>] [...]\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 <vlan> [all unspec|no|untagged|tagged] [<port1> 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 <mode>\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 <port> <vlan>\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;
+}
+
+
--- /dev/null
+
+#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
+
+
--- /dev/null
+
+#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 = "<root>", .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
+
--- /dev/null
+
+#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;
+}
+
+
--- /dev/null
+
+#ifndef DEF_COMMON
+#define DEF_COMMON
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include <ngadmin.h>
+
+
+#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
+
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])
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
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
-SUBDIRS = src
+SUBDIRS = include src
--- /dev/null
+
+noinst_HEADERS = attr.h list.h protocol.h
+
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