From 95a19e0c5c622e8e886ff822ea48d28f7bceced4 Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Thu, 30 Dec 2010 15:38:10 +0300 Subject: cli: removed old "show sessions" code shaper_tbf: add column "rate-limit" for "show sessions" --- accel-pptpd/cli/cli.h | 3 + accel-pptpd/cli/std_cmd.c | 172 ----------------------------------------- accel-pptpd/extra/shaper_tbf.c | 11 +++ 3 files changed, 14 insertions(+), 172 deletions(-) diff --git a/accel-pptpd/cli/cli.h b/accel-pptpd/cli/cli.h index ae452bc..cdceb2f 100644 --- a/accel-pptpd/cli/cli.h +++ b/accel-pptpd/cli/cli.h @@ -29,6 +29,8 @@ struct cli_regexp_cmd_t int (*help)(char * const *fields, int field_cnt, void *client); }; +struct ppp_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), @@ -37,6 +39,7 @@ void cli_register_simple_cmd2( ... ); void cli_register_regexp_cmd(struct cli_regexp_cmd_t *cmd); +void cli_show_ses_register(const char *name, const char *desc, void (*print)(const struct ppp_t *ppp, char *buf)); int cli_send(void *client, const char *data); int cli_sendv(void *client, const char *fmt, ...); diff --git a/accel-pptpd/cli/std_cmd.c b/accel-pptpd/cli/std_cmd.c index a5014f2..a49bbce 100644 --- a/accel-pptpd/cli/std_cmd.c +++ b/accel-pptpd/cli/std_cmd.c @@ -79,177 +79,6 @@ static void exit_help(char * const *fields, int fields_cnt, void *client) //============================= -static int show_ses_exec(const char *cmd, char * const *fields, int fields_cnt, void *client) -{ - struct row_t - { - struct list_head entry; - char buf[128]; - char *match_key; - char *order_key; - }; - char ip_str[17]; - char *state_str; - char time_str[12]; - time_t uptime; - int day,hour,min,sec; - struct ppp_t *ppp; - int i; - enum order_type {ORDER_NONE, ORDER_USERNAME}; - enum match_type {MATCH_NONE, MATCH_USERNAME}; - int order = ORDER_NONE; - int match = MATCH_NONE; - struct row_t *row, *row2, *row3; - pcre *re = NULL; - const char *pcre_err; - int pcre_offset; - LIST_HEAD(rows); - LIST_HEAD(temp_rows); - - for (i = 2; i < fields_cnt; i++) { - if (!strcmp(fields[i], "order")) { - if (i == fields_cnt - 1) - return CLI_CMD_SYNTAX; - i++; - if (!strcmp(fields[i], "username")) - order = ORDER_USERNAME; - else { - cli_send(client, "only order by username is supported yet\r\n"); - return CLI_CMD_OK; - } - } else if (!strcmp(fields[i], "match")) { - if (i == fields_cnt - 2) - return CLI_CMD_SYNTAX; - i++; - if (!strcmp(fields[i], "username")) - match = MATCH_USERNAME; - else { - cli_send(client, "only match by username is supported yet\r\n"); - return CLI_CMD_OK; - } - i++; - re = pcre_compile2(fields[i], 0, NULL, &pcre_err, &pcre_offset, NULL); - if (!re) { - cli_sendv(client, "match: %s at %i\r\n", pcre_err, pcre_offset); - return CLI_CMD_OK; - } - } else - return CLI_CMD_SYNTAX; - } - - - 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) { - row = _malloc(sizeof(*row)); - if (!row) { - log_emerg("out of memory\n"); - cli_send(client, "out of memory\r\n"); - break; - } - - if (order == ORDER_USERNAME) - row->order_key = ppp->username ? _strdup(ppp->username) : _strdup(""); - - if (match == MATCH_USERNAME) - row->match_key = ppp->username ? _strdup(ppp->username) : _strdup(""); - - u_inet_ntoa(ppp->peer_ipaddr, ip_str); - - switch (ppp->state) { - case PPP_STATE_STARTING: - state_str = "start"; - break; - case PPP_STATE_ACTIVE: - state_str = "active"; - break; - case PPP_STATE_FINISHING: - state_str = "finish"; - break; - default: - state_str = "unk"; - } - - if (ppp->stop_time) - uptime = ppp->stop_time - ppp->start_time; - else { - time(&uptime); - uptime -= ppp->start_time; - } - day = uptime/ (24*60*60); uptime %= (24*60*60); - hour = uptime / (60*60); uptime %= (60*60); - min = uptime / 60; - sec = uptime % 60; - if (day) - snprintf(time_str, 12, "%i.%02i:%02i:%02i", day, hour, min, sec); - else - snprintf(time_str, 12, "%02i:%02i:%02i", hour, min, sec); - - snprintf(row->buf, 128, "%9s %15s %16s %6s %6s %10s\r\n", ppp->ifname, ppp->username ? ppp->username : "", ip_str, ppp->ctrl->name, state_str, time_str); - if (order || match) - list_add_tail(&row->entry, &temp_rows); - else - list_add_tail(&row->entry, &rows); - //cli_send(client, buf); - } - pthread_rwlock_unlock(&ppp_lock); - - if (match || order) { - while (!list_empty(&temp_rows)) { - row = list_entry(temp_rows.next, typeof(*row), entry); - list_del(&row->entry); - if (match == MATCH_USERNAME) { - if (pcre_exec(re, NULL, row->match_key, strlen(row->match_key), 0, 0, NULL, 0) < 0) { - _free(row->match_key); - if (order) - _free(row->order_key); - _free(row); - continue; - } - } - if (order == ORDER_USERNAME) { - row3 = NULL; - list_for_each_entry(row2, &rows, entry) { - if (strcmp(row->order_key, row2->order_key) <= 0) { - row3 = row2; - break; - } - } - if (row3) - list_add_tail(&row->entry, &row3->entry); - else - list_add_tail(&row->entry, &rows); - } else - list_add_tail(&row->entry, &rows); - } - } - - while (!list_empty(&rows)) { - row = list_entry(rows.next, typeof(*row), entry); - list_del(&row->entry); - cli_send(client, row->buf); - if (match == MATCH_USERNAME) - _free(row->match_key); - if (order == ORDER_USERNAME) - _free(row->order_key); - _free(row); - } - - if (match == MATCH_USERNAME) - pcre_free(re); - - return CLI_CMD_OK; -} - -static void show_ses_help(char * const *fields, int fields_cnt, void *client) -{ - cli_send(client, "show sessions [order username] [match username ] - shows all sessions\r\n"); -} - -//============================= - static void ppp_terminate_soft(struct ppp_t *ppp) { ppp_terminate(ppp, TERM_NAS_REQUEST, 0); @@ -487,7 +316,6 @@ static void reload_help(char * const *fields, int fields_cnt, void *client) static void __init init(void) { 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_old"); cli_register_simple_cmd2(terminate_exec, terminate_help, 1, "terminate"); cli_register_simple_cmd2(reload_exec, reload_help, 1, "reload"); cli_register_simple_cmd2(shutdown_exec, shutdown_help, 1, "shutdown"); diff --git a/accel-pptpd/extra/shaper_tbf.c b/accel-pptpd/extra/shaper_tbf.c index 940fa29..bed43e2 100644 --- a/accel-pptpd/extra/shaper_tbf.c +++ b/accel-pptpd/extra/shaper_tbf.c @@ -823,6 +823,16 @@ static int shaper_restore_exec(const char *cmd, char * const *f, int f_cnt, void return CLI_CMD_OK; } +static void print_rate(const struct ppp_t *ppp, char *buf) +{ + struct shaper_pd_t *pd = find_pd((struct ppp_t *)ppp, 0); + + if (pd && (pd->down_speed || pd->up_speed)) + sprintf(buf, "%i/%i", pd->down_speed, pd->up_speed); + else + *buf = 0; +} + static int clock_init(void) { FILE *fp; @@ -968,5 +978,6 @@ static void __init init(void) cli_register_simple_cmd2(shaper_change_exec, shaper_change_help, 2, "shaper", "change"); cli_register_simple_cmd2(shaper_restore_exec, shaper_restore_help, 2, "shaper", "restore"); + cli_show_ses_register("rate-limit", "rate limit down-stream/up-stream (Kbit)", print_rate); } -- cgit v1.2.3