summaryrefslogtreecommitdiff
path: root/accel-pptpd/cli
diff options
context:
space:
mode:
authorKozlov Dmitry <dima@server>2010-11-11 14:35:47 +0300
committerKozlov Dmitry <dima@server>2010-11-11 14:35:47 +0300
commit15339d1df496dc3f9225157e97b07f96fa9bdf0b (patch)
treea430e16d9cada376e50bfcadd810aca10e7da3a6 /accel-pptpd/cli
parentdda81cb1d1ba0229aa4c9a90a6f2b2012a4e9326 (diff)
downloadaccel-ppp-xebd-15339d1df496dc3f9225157e97b07f96fa9bdf0b.tar.gz
accel-ppp-xebd-15339d1df496dc3f9225157e97b07f96fa9bdf0b.zip
cli: simplified cli api
Diffstat (limited to 'accel-pptpd/cli')
-rw-r--r--accel-pptpd/cli/cli.c56
-rw-r--r--accel-pptpd/cli/cli.h9
-rw-r--r--accel-pptpd/cli/std_cmd.c181
-rw-r--r--accel-pptpd/cli/telnet.c22
-rw-r--r--accel-pptpd/cli/telnet.h4
5 files changed, 124 insertions, 148 deletions
diff --git a/accel-pptpd/cli/cli.c b/accel-pptpd/cli/cli.c
index 28416e1..06360c5 100644
--- a/accel-pptpd/cli/cli.c
+++ b/accel-pptpd/cli/cli.c
@@ -1,4 +1,5 @@
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <unistd.h>
@@ -20,6 +21,33 @@ void __export cli_register_simple_cmd(struct cli_simple_cmd_t *cmd)
list_add_tail(&cmd->entry, &simple_cmd_list);
}
+void __export cli_register_simple_cmd2(
+ int (*exec)(const char *cmd, char * const *fields, int fields_cnt, void *client),
+ void (*help)(char * const *fields, int fields_cnt, void *client),
+ int hdr_len,
+ ...
+ )
+{
+ struct cli_simple_cmd_t *c;
+ int i;
+ va_list ap;
+
+ va_start(ap, hdr_len);
+
+ c = malloc(sizeof(*c));
+ memset(c, 0, sizeof(*c));
+
+ c->exec = exec;
+ c->help = help;
+ c->hdr_len = hdr_len;
+ c->hdr = malloc(hdr_len * sizeof(void*));
+
+ for (i = 0; i < hdr_len; i++)
+ c->hdr[i] = va_arg(ap, char *);
+
+ list_add_tail(&c->entry, &simple_cmd_list);
+}
+
void __export cli_register_regexp_cmd(struct cli_regexp_cmd_t *cmd)
{
int err;
@@ -38,6 +66,19 @@ int __export cli_send(void *client, const char *data)
return telnet_send(cln, data, strlen(data));
}
+int __export cli_sendv(void *client, const char *fmt, ...)
+{
+ struct client_t *cln = (struct client_t *)client;
+ int r;
+
+ va_list ap;
+ va_start(ap, fmt);
+ r = telnet_sendv(cln, fmt, ap);
+ va_end(ap);
+
+ return r;
+}
+
static char *skip_word(char *ptr)
{
@@ -89,14 +130,13 @@ int process_cmd(struct client_t *cln)
n = split((char *)cln->cmdline, f);
if (n >= 1 && !strcmp(f[0], "help")) {
- list_for_each_entry(cmd1, &simple_cmd_list, entry) {
- if (cmd1->help && cmd1->help(f, n, cln))
- return -1;
- }
- list_for_each_entry(cmd2, &regexp_cmd_list, entry) {
- if (cmd2->help && cmd1->help(f, n, cln))
- return -1;
- }
+ list_for_each_entry(cmd1, &simple_cmd_list, entry)
+ if (cmd1->help)
+ cmd1->help(f, n, cln);
+
+ list_for_each_entry(cmd2, &regexp_cmd_list, entry)
+ if (cmd2->help)
+ cmd1->help(f, n, cln);
return 0;
}
diff --git a/accel-pptpd/cli/cli.h b/accel-pptpd/cli/cli.h
index aec62fd..af6fd98 100644
--- a/accel-pptpd/cli/cli.h
+++ b/accel-pptpd/cli/cli.h
@@ -15,7 +15,7 @@ struct cli_simple_cmd_t
int hdr_len;
const char **hdr;
int (*exec)(const char *cmd, char * const *fields, int fields_cnt, void *client);
- int (*help)(char * const *fields, int field_cnt, void *client);
+ void (*help)(char * const *fields, int field_cnt, void *client);
};
struct cli_regexp_cmd_t
@@ -29,9 +29,16 @@ struct cli_regexp_cmd_t
};
void cli_register_simple_cmd(struct cli_simple_cmd_t *cmd);
+void cli_register_simple_cmd2(
+ int (*exec)(const char *cmd, char * const *fields, int fields_cnt, void *client),
+ void (*help)(char * const *fields, int fields_cnt, void *client),
+ int hdr_len,
+ ...
+ );
void cli_register_regexp_cmd(struct cli_regexp_cmd_t *cmd);
int cli_send(void *client, const char *data);
+int cli_sendv(void *client, const char *fmt, ...);
#endif
diff --git a/accel-pptpd/cli/std_cmd.c b/accel-pptpd/cli/std_cmd.c
index 491caf7..9a5747e 100644
--- a/accel-pptpd/cli/std_cmd.c
+++ b/accel-pptpd/cli/std_cmd.c
@@ -7,118 +7,49 @@
#include "cli.h"
#include "utils.h"
-int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
+static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
{
- char buf[128];
-
- if (cli_send(client, "core:\r\n"))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " mempool_allocated: %u\r\n", triton_stat.mempool_allocated);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " mempool_available: %u\r\n", triton_stat.mempool_available);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " thread_count: %u\r\n", triton_stat.thread_count);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " thread_active: %u\r\n", triton_stat.thread_active);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " context_count: %u\r\n", triton_stat.context_count);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " context_sleeping: %u\r\n", triton_stat.context_sleeping);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " context_pending: %u\r\n", triton_stat.context_pending);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " md_handler_count: %u\r\n", triton_stat.md_handler_count);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " md_handler_pending: %u\r\n", triton_stat.md_handler_pending);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " timer_count: %u\r\n", triton_stat.timer_count);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " timer_pending: %u\r\n", triton_stat.timer_pending);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
+ cli_send(client, "core:\r\n");
+ cli_sendv(client, " mempool_allocated: %u\r\n", triton_stat.mempool_allocated);
+ cli_sendv(client, " mempool_available: %u\r\n", triton_stat.mempool_available);
+ cli_sendv(client, " thread_count: %u\r\n", triton_stat.thread_count);
+ cli_sendv(client, " thread_active: %u\r\n", triton_stat.thread_active);
+ cli_sendv(client, " context_count: %u\r\n", triton_stat.context_count);
+ cli_sendv(client, " context_sleeping: %u\r\n", triton_stat.context_sleeping);
+ cli_sendv(client, " context_pending: %u\r\n", triton_stat.context_pending);
+ cli_sendv(client, " md_handler_count: %u\r\n", triton_stat.md_handler_count);
+ cli_sendv(client, " md_handler_pending: %u\r\n", triton_stat.md_handler_pending);
+ cli_sendv(client, " timer_count: %u\r\n", triton_stat.timer_count);
+ cli_sendv(client, " timer_pending: %u\r\n", triton_stat.timer_pending);
//===========
- if (cli_send(client, "ppp:\r\n"))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " staring: %u\r\n", ppp_stat.starting);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " active: %u\r\n", ppp_stat.active);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
-
- sprintf(buf, " finishing: %u\r\n", ppp_stat.finishing);
- if (cli_send(client, buf))
- return CLI_CMD_FAILED;
+ cli_send(client, "ppp:\r\n");
+ cli_sendv(client, " staring: %u\r\n", ppp_stat.starting);
+ cli_sendv(client, " active: %u\r\n", ppp_stat.active);
+ cli_sendv(client, " finishing: %u\r\n", ppp_stat.finishing);
return CLI_CMD_OK;
}
-int show_stat_help(char * const *fields, int fields_cnt, void *client)
+static void show_stat_help(char * const *fields, int fields_cnt, void *client)
{
- if (cli_send(client, "show stat - shows various statistics information\r\n"))
- return -1;
-
- return 0;
+ cli_send(client, "show stat - shows various statistics information\r\n");
}
-
-const char *show_stat_hdr[] = {"show","stat"};
-static struct cli_simple_cmd_t show_stat_cmd = {
- .hdr_len = 2,
- .hdr = show_stat_hdr,
- .exec = show_stat_exec,
- .help = show_stat_help,
-};
-
//=============================
-int exit_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
+static int exit_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
{
return CLI_CMD_EXIT;
}
-int exit_help(char * const *fields, int fields_cnt, void *client)
+static void exit_help(char * const *fields, int fields_cnt, void *client)
{
- if (cli_send(client, "exit - exit cli\r\n"))
- return -1;
-
- return 0;
+ cli_send(client, "exit - exit cli\r\n");
}
-const char *exit_hdr[] = {"exit"};
-static struct cli_simple_cmd_t exit_cmd = {
- .hdr_len = 1,
- .hdr = exit_hdr,
- .exec = exit_exec,
- .help = exit_help,
-};
-
//=============================
-int show_ses_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
+static int show_ses_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
{
char buf[128];
char ip_str[17];
@@ -128,11 +59,8 @@ int show_ses_exec(const char *cmd, char * const *fields, int fields_cnt, void *c
int day,hour,min,sec;
struct ppp_t *ppp;
- if (cli_send(client, "interface: username: address: type: state: uptime:\r\n"))
- return CLI_CMD_FAILED;
-
- if (cli_send(client, "------------------------------------------------------------------------\r\n"))
- return CLI_CMD_FAILED;
+ cli_send(client, "interface: username: address: type: state: uptime:\r\n");
+ cli_send(client, "------------------------------------------------------------------------\r\n");
pthread_rwlock_rdlock(&ppp_lock);
list_for_each_entry(ppp, &ppp_list, entry) {
@@ -168,32 +96,18 @@ int show_ses_exec(const char *cmd, char * const *fields, int fields_cnt, void *c
sprintf(time_str, "%02i:%02i:%02i", hour, min, sec);
sprintf(buf, "%9s %15s %16s %6s %6s %10s\r\n", ppp->ifname, ppp->username ? ppp->username : "", ip_str, ppp->ctrl->name, state_str, time_str);
- if (cli_send(client, buf)) {
- pthread_rwlock_unlock(&ppp_lock);
- return CLI_CMD_FAILED;
- }
+ cli_send(client, buf);
}
pthread_rwlock_unlock(&ppp_lock);
return CLI_CMD_OK;
}
-int show_ses_help(char * const *fields, int fields_cnt, void *client)
+static void show_ses_help(char * const *fields, int fields_cnt, void *client)
{
- if (cli_send(client, "show sessions - shows all sessions\r\n"))
- return -1;
-
- return 0;
+ cli_send(client, "show sessions - shows all sessions\r\n");
}
-const char *show_ses_hdr[] = {"show", "sessions"};
-static struct cli_simple_cmd_t show_ses_cmd = {
- .hdr_len = 2,
- .hdr = show_ses_hdr,
- .exec = show_ses_exec,
- .help = show_ses_help,
-};
-
//=============================
static void ppp_terminate_soft(struct ppp_t *ppp)
@@ -206,20 +120,20 @@ static void ppp_terminate_hard(struct ppp_t *ppp)
ppp_terminate(ppp, 1, TERM_ADMIN_RESET);
}
-int terminate_help(char * const *fields, int fields_cnt, void *client);
-int terminate_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
+static void terminate_help(char * const *fields, int fields_cnt, void *client);
+static int terminate_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
{
struct ppp_t *ppp;
int hard = 0;
if (fields_cnt == 1)
- return terminate_help(NULL, 0, client);
+ goto help;
if (fields_cnt == 3) {
if (!strcmp(fields[2], "hard"))
hard = 1;
else if (strcmp(fields[2], "soft"))
- return terminate_help(NULL, 0, client);
+ goto help;
}
pthread_rwlock_rdlock(&ppp_lock);
@@ -244,31 +158,22 @@ int terminate_exec(const char *cmd, char * const *fields, int fields_cnt, void *
pthread_rwlock_unlock(&ppp_lock);
return CLI_CMD_OK;
+help:
+ terminate_help(fields, fields_cnt, client);
+ return CLI_CMD_OK;
}
-int terminate_help(char * const *fields, int fields_cnt, void *client)
+static void terminate_help(char * const *fields, int fields_cnt, void *client)
{
- if (cli_send(client, "terminate <interface> [soft|hard]- terminate session\r\n"))
- return -1;
-
- if (cli_send(client, "terminate all [soft|hard]- terminate all session\r\n"))
- return -1;
-
- return 0;
+ cli_send(client, "terminate <interface> [soft|hard]- terminate session\r\n");
+ cli_send(client, "terminate all [soft|hard]- terminate all session\r\n");
}
-const char *terminate_hdr[] = {"terminate"};
-static struct cli_simple_cmd_t terminate_cmd = {
- .hdr_len = 1,
- .hdr = terminate_hdr,
- .exec = terminate_exec,
- .help = terminate_help,
-};
-
static void __init init(void)
{
- cli_register_simple_cmd(&show_stat_cmd);
- cli_register_simple_cmd(&show_ses_cmd);
- cli_register_simple_cmd(&terminate_cmd);
- cli_register_simple_cmd(&exit_cmd);
+ cli_register_simple_cmd2(show_stat_exec, show_stat_help, 2, "show", "stat");
+ cli_register_simple_cmd2(show_ses_exec, show_ses_help, 2, "show", "sessions");
+ cli_register_simple_cmd2(terminate_exec, terminate_help, 1, "terminate");
+ cli_register_simple_cmd2(exit_exec, exit_help, 1, "exit");
}
+
diff --git a/accel-pptpd/cli/telnet.c b/accel-pptpd/cli/telnet.c
index c7b5f15..97e85d0 100644
--- a/accel-pptpd/cli/telnet.c
+++ b/accel-pptpd/cli/telnet.c
@@ -91,6 +91,9 @@ int telnet_send(struct client_t *cln, const void *_buf, int size)
struct buffer_t *b;
const uint8_t *buf = (const uint8_t *)_buf;
+ if (cln->disconnect)
+ return -1;
+
for (n = 0; n < size; n += k) {
k = write(cln->hnd.fd, buf + n, size - n);
if (k < 0) {
@@ -105,13 +108,26 @@ int telnet_send(struct client_t *cln, const void *_buf, int size)
}
if (errno != EPIPE)
log_error("cli: write: %s\n", strerror(errno));
- disconnect(cln);
+ //disconnect(cln);
+ cln->disconnect = 1;
return -1;
}
}
return 0;
}
+int telnet_sendv(struct client_t *cln, const char *fmt, va_list ap)
+{
+ int r = vsnprintf((char *)temp_buf, RECV_BUF_SIZE, fmt, ap);
+
+ if (r >= RECV_BUF_SIZE) {
+ strcpy((char *)temp_buf + RECV_BUF_SIZE - 6, "...\r\n");
+ r = RECV_BUF_SIZE;
+ }
+
+ return telnet_send(cln, temp_buf, r);
+}
+
static int send_banner(struct client_t *cln)
{
return telnet_send(cln, BANNER, sizeof(BANNER));
@@ -398,6 +414,10 @@ static int cln_read(struct triton_md_handler_t *h)
if (telnet_input_char(cln, recv_buf[i]))
return -1;
}
+ if (cln->disconnect) {
+ disconnect(cln);
+ return 0;
+ }
}
return 0;
diff --git a/accel-pptpd/cli/telnet.h b/accel-pptpd/cli/telnet.h
index adbb7b9..24515d3 100644
--- a/accel-pptpd/cli/telnet.h
+++ b/accel-pptpd/cli/telnet.h
@@ -1,6 +1,8 @@
#ifndef __TELNET_H
#define __TELNET_H
+#include <stdarg.h>
+
struct client_t
{
struct list_head entry;
@@ -18,9 +20,11 @@ struct client_t
int echo:1;
int telcmd:1;
int esc:1;
+ int disconnect:1;
};
int telnet_send(struct client_t *cln, const void *buf, int size);
+int telnet_sendv(struct client_t *cln, const char *fmt, va_list ap);
void telnet_disconnect(struct client_t *cln);
int process_cmd(struct client_t *cln);