move duration to before cmd_show_counters
[l2tpns.git] / cli.c
diff --git a/cli.c b/cli.c
index 32fdd0d..963f220 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -2,7 +2,7 @@
 // vim: sw=8 ts=8
 
 char const *cvs_name = "$Name:  $";
-char const *cvs_id_cli = "$Id: cli.c,v 1.42 2004/12/16 23:40:30 bodea Exp $";
+char const *cvs_id_cli = "$Id: cli.c,v 1.50 2005/01/13 08:03:04 bodea Exp $";
 
 #include <stdio.h>
 #include <stdarg.h>
@@ -98,6 +98,7 @@ static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_load_plugin(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc);
+
 static int regular_stuff(struct cli_def *cli);
 static void parsemac(char *string, char mac[6]);
 
@@ -289,8 +290,9 @@ void cli_do(int sockfd)
        if (fork_and_close()) return;
        if (getpeername(sockfd, (struct sockaddr *)&addr, &l) == 0)
        {
-               LOG(3, 0, 0, "Accepted connection to CLI from %s\n", fmtaddr(addr.sin_addr.s_addr, 0));
                require_auth = addr.sin_addr.s_addr != inet_addr("127.0.0.1");
+               LOG(require_auth ? 3 : 4, 0, 0, "Accepted connection to CLI from %s\n",
+                       fmtaddr(addr.sin_addr.s_addr, 0));
        }
        else
                LOG(0, 0, 0, "getpeername() failed on cli socket.  Requiring authentication: %s\n", strerror(errno));
@@ -321,7 +323,9 @@ void cli_do(int sockfd)
        cli_loop(cli, sockfd);
 
        close(sockfd);
-       LOG(3, 0, 0, "Closed CLI connection from %s\n", fmtaddr(addr.sin_addr.s_addr, 0));
+       LOG(require_auth ? 3 : 4, 0, 0, "Closed CLI connection from %s\n",
+               fmtaddr(addr.sin_addr.s_addr, 0));
+
        exit(0);
 }
 
@@ -640,28 +644,62 @@ static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int a
        return CLI_OK;
 }
 
+static char *duration(time_t secs)
+{
+       static char *buf = NULL;
+       int p = 0;
+
+       if (!buf) buf = calloc(64, 1);
+
+       if (secs >= 86400)
+       {
+               int days = secs / 86400;
+               p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
+               secs %= 86400;
+       }
+
+       if (secs >= 3600)
+       {
+               int mins = secs / 60;
+               int hrs = mins / 60;
+
+               mins %= 60;
+               sprintf(buf + p, "%d:%02d", hrs, mins);
+       }
+       else if (secs >= 60)
+       {
+               int mins = secs / 60;
+               sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
+       }
+       else
+               sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
+
+       return buf;
+}
+
 static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, int argc)
 {
        if (CLI_HELP_REQUESTED)
                return CLI_HELP_NO_ARGS;
 
-       cli_print(cli, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
-       cli_print(cli, "%-10s %8u %8u %8u", "RX",
+       cli_print(cli, "%-10s %10s %10s %10s %10s", "Ethernet", "Bytes", "Packets", "Errors", "Dropped");
+       cli_print(cli, "%-10s %10u %10u %10u %10u", "RX",
                        GET_STAT(tun_rx_bytes),
                        GET_STAT(tun_rx_packets),
-                       GET_STAT(tun_rx_errors));
-       cli_print(cli, "%-10s %8u %8u %8u", "TX",
+                       GET_STAT(tun_rx_errors),
+                       GET_STAT(tun_rx_dropped));
+       cli_print(cli, "%-10s %10u %10u %10u", "TX",
                        GET_STAT(tun_tx_bytes),
                        GET_STAT(tun_tx_packets),
                        GET_STAT(tun_tx_errors));
        cli_print(cli, "");
 
-       cli_print(cli, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
-       cli_print(cli, "%-10s %8u %8u %8u", "RX",
+       cli_print(cli, "%-10s %10s %10s %10s %10s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
+       cli_print(cli, "%-10s %10u %10u %10u", "RX",
                        GET_STAT(tunnel_rx_bytes),
                        GET_STAT(tunnel_rx_packets),
                        GET_STAT(tunnel_rx_errors));
-       cli_print(cli, "%-10s %8u %8u %8u %8u", "TX",
+       cli_print(cli, "%-10s %10u %10u %10u %10u", "TX",
                        GET_STAT(tunnel_tx_bytes),
                        GET_STAT(tunnel_tx_packets),
                        GET_STAT(tunnel_tx_errors),
@@ -686,8 +724,8 @@ static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, in
        cli_print(cli, "%-30s%u", "cluster_forwarded",          GET_STAT(c_forwarded));
        cli_print(cli, "%-30s%u", "recv_forward",               GET_STAT(recv_forward));
        cli_print(cli, "%-30s%u", "select_called",              GET_STAT(select_called));
-       cli_print(cli, "%-30s%u", "select_ready",               GET_STAT(select_ready));
-       cli_print(cli, "%-30s%u", "select_processed",           GET_STAT(select_processed));
+       cli_print(cli, "%-30s%u", "multi_read_used",            GET_STAT(multi_read_used));
+       cli_print(cli, "%-30s%u", "multi_read_exceeded",        GET_STAT(multi_read_exceeded));
 
 
 #ifdef STATISTICS
@@ -719,7 +757,12 @@ static int cmd_show_counters(struct cli_def *cli, char *command, char **argv, in
        cli_print(cli, "%-30s%u", "call_dump_acct_info",        GET_STAT(call_dump_acct_info));
        cli_print(cli, "%-30s%u", "call_radiussend",            GET_STAT(call_radiussend));
        cli_print(cli, "%-30s%u", "call_radiusretry",           GET_STAT(call_radiusretry));
+       cli_print(cli, "%-30s%u", "call_random_data",           GET_STAT(call_random_data));
 #endif
+
+       cli_print(cli, "");
+       cli_print(cli, "Counters last reset %s ago", duration(time_now - GET_STAT(last_reset)));
+
        return CLI_OK;
 }
 
@@ -889,6 +932,7 @@ static char const *show_access_list_rule(int extended, ip_filter_rulet *rule);
 static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int argc)
 {
        int i;
+       char ipv6addr[INET6_ADDRSTRLEN];
 
        if (CLI_HELP_REQUESTED)
                return CLI_HELP_NO_ARGS;
@@ -899,25 +943,27 @@ static int cmd_show_run(struct cli_def *cli, char *command, char **argv, int arg
        {
                void *value = ((void *)config) + config_values[i].offset;
                if (config_values[i].type == STRING)
-                       cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *)value);
-               else if (config_values[i].type == IP)
-                       cli_print(cli, "set %s %s", config_values[i].key, fmtaddr(*(unsigned *)value, 0));
+                       cli_print(cli, "set %s \"%.*s\"", config_values[i].key, config_values[i].size, (char *) value);
+               else if (config_values[i].type == IPv4)
+                       cli_print(cli, "set %s %s", config_values[i].key, fmtaddr(*(in_addr_t *) value, 0));
+               else if (config_values[i].type == IPv6)
+                       cli_print(cli, "set %s %s", config_values[i].key, inet_ntop(AF_INET6, value, ipv6addr, INET6_ADDRSTRLEN));
                else if (config_values[i].type == SHORT)
-                       cli_print(cli, "set %s %hu", config_values[i].key, *(short *)value);
+                       cli_print(cli, "set %s %hu", config_values[i].key, *(short *) value);
                else if (config_values[i].type == BOOL)
-                       cli_print(cli, "set %s %s", config_values[i].key, (*(int *)value) ? "yes" : "no");
+                       cli_print(cli, "set %s %s", config_values[i].key, (*(int *) value) ? "yes" : "no");
                else if (config_values[i].type == INT)
-                       cli_print(cli, "set %s %d", config_values[i].key, *(int *)value);
+                       cli_print(cli, "set %s %d", config_values[i].key, *(int *) value);
                else if (config_values[i].type == UNSIGNED_LONG)
-                       cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *)value);
+                       cli_print(cli, "set %s %lu", config_values[i].key, *(unsigned long *) value);
                else if (config_values[i].type == MAC)
                        cli_print(cli, "set %s %02x%02x.%02x%02x.%02x%02x", config_values[i].key,
-                                       *(unsigned short *)(value + 0),
-                                       *(unsigned short *)(value + 1),
-                                       *(unsigned short *)(value + 2),
-                                       *(unsigned short *)(value + 3),
-                                       *(unsigned short *)(value + 4),
-                                       *(unsigned short *)(value + 5));
+                                       *(unsigned short *) (value + 0),
+                                       *(unsigned short *) (value + 1),
+                                       *(unsigned short *) (value + 2),
+                                       *(unsigned short *) (value + 3),
+                                       *(unsigned short *) (value + 4),
+                                       *(unsigned short *) (value + 5));
        }
 
        cli_print(cli, "# Plugins");
@@ -1112,8 +1158,10 @@ static int cmd_clear_counters(struct cli_def *cli, char *command, char **argv, i
        if (CLI_HELP_REQUESTED)
                return CLI_HELP_NO_ARGS;
 
-       cli_print(cli, "Counters cleared");
+       memset(_statistics, 0, sizeof(struct Tstats));
        SET_STAT(last_reset, time(NULL));
+
+       cli_print(cli, "Counters cleared");
        return CLI_OK;
 }
 
@@ -1710,39 +1758,6 @@ static int cmd_remove_plugin(struct cli_def *cli, char *command, char **argv, in
        return CLI_OK;
 }
 
-static char *duration(time_t secs)
-{
-       static char *buf = NULL;
-       int p = 0;
-
-       if (!buf) buf = calloc(64, 1);
-
-       if (secs >= 86400)
-       {
-               int days = secs / 86400;
-               p = sprintf(buf, "%d day%s, ", days, days > 1 ? "s" : "");
-               secs %= 86400;
-       }
-
-       if (secs >= 3600)
-       {
-               int mins = secs / 60;
-               int hrs = mins / 60;
-
-               mins %= 60;
-               sprintf(buf + p, "%d:%02d", hrs, mins);
-       }
-       else if (secs >= 60)
-       {
-               int mins = secs / 60;
-               sprintf(buf + p, "%d min%s", mins, mins > 1 ? "s" : "");
-       }
-       else
-               sprintf(buf, "%ld sec%s", secs, secs > 1 ? "s" : "");
-
-       return buf;
-}
-
 static int cmd_uptime(struct cli_def *cli, char *command, char **argv, int argc)
 {
        FILE *fh;
@@ -1818,7 +1833,7 @@ static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
 
        for (i = 0; config_values[i].key; i++)
        {
-               void *value = ((void *)config) + config_values[i].offset;
+               void *value = ((void *) config) + config_values[i].offset;
                if (strcmp(config_values[i].key, argv[0]) == 0)
                {
                        // Found a value to set
@@ -1826,28 +1841,31 @@ static int cmd_set(struct cli_def *cli, char *command, char **argv, int argc)
                        switch (config_values[i].type)
                        {
                        case STRING:
-                               strncpy((char *)value, argv[1], config_values[i].size - 1);
+                               snprintf((char *) value, config_values[i].size, "%s", argv[1]);
                                break;
                        case INT:
-                               *(int *)value = atoi(argv[1]);
+                               *(int *) value = atoi(argv[1]);
                                break;
                        case UNSIGNED_LONG:
-                               *(unsigned long *)value = atol(argv[1]);
+                               *(unsigned long *) value = atol(argv[1]);
                                break;
                        case SHORT:
-                               *(short *)value = atoi(argv[1]);
+                               *(short *) value = atoi(argv[1]);
+                               break;
+                       case IPv4:
+                               *(in_addr_t *) value = inet_addr(argv[1]);
                                break;
-                       case IP:
-                               *(unsigned *)value = inet_addr(argv[1]);
+                       case IPv6:
+                               inet_pton(AF_INET6, argv[1], value);
                                break;
                        case MAC:
                                parsemac(argv[1], (char *)value);
                                break;
                        case BOOL:
                                if (strcasecmp(argv[1], "yes") == 0 || strcasecmp(argv[1], "true") == 0 || strcasecmp(argv[1], "1") == 0)
-                                       *(int *)value = 1;
+                                       *(int *) value = 1;
                                else
-                                       *(int *)value = 0;
+                                       *(int *) value = 0;
                                break;
                        default:
                                cli_print(cli, "Unknown variable type");
@@ -2045,7 +2063,7 @@ static int cmd_router_bgp_neighbour(struct cli_def *cli, char *command, char **a
 
                if (!config->neighbour[i].name[0])
                {
-                       snprintf(config->neighbour[i].name, sizeof(config->neighbour[i].name), argv[0]);
+                       snprintf(config->neighbour[i].name, sizeof(config->neighbour[i].name), "%s", argv[0]);
                        config->neighbour[i].keepalive = -1;
                        config->neighbour[i].hold = -1;
                }