diff options
author | Kozlov Dmitry <dima@server> | 2010-11-11 14:35:47 +0300 |
---|---|---|
committer | Kozlov Dmitry <dima@server> | 2010-11-11 14:35:47 +0300 |
commit | 15339d1df496dc3f9225157e97b07f96fa9bdf0b (patch) | |
tree | a430e16d9cada376e50bfcadd810aca10e7da3a6 /accel-pptpd/ctrl/pppoe | |
parent | dda81cb1d1ba0229aa4c9a90a6f2b2012a4e9326 (diff) | |
download | accel-ppp-15339d1df496dc3f9225157e97b07f96fa9bdf0b.tar.gz accel-ppp-15339d1df496dc3f9225157e97b07f96fa9bdf0b.zip |
cli: simplified cli api
Diffstat (limited to 'accel-pptpd/ctrl/pppoe')
-rw-r--r-- | accel-pptpd/ctrl/pppoe/mac_filter.c | 103 | ||||
-rw-r--r-- | accel-pptpd/ctrl/pppoe/pppoe.c | 27 |
2 files changed, 37 insertions, 93 deletions
diff --git a/accel-pptpd/ctrl/pppoe/mac_filter.c b/accel-pptpd/ctrl/pppoe/mac_filter.c index 537c2e51..03f62811 100644 --- a/accel-pptpd/ctrl/pppoe/mac_filter.c +++ b/accel-pptpd/ctrl/pppoe/mac_filter.c @@ -119,7 +119,7 @@ err: return -1; } -static int mac_filter_add(const char *addr, void *client) +static void mac_filter_add(const char *addr, void *client) { int n[ETH_ALEN]; struct mac_t *mac; @@ -127,14 +127,16 @@ static int mac_filter_add(const char *addr, void *client) if (sscanf(addr, "%x:%x:%x:%x:%x:%x", n + 0, n + 1, n + 2, n + 3, n + 4, n + 5) != 6) { - return cli_send(client, "invalid format\r\n"); + cli_send(client, "invalid format\r\n"); + return; } mac = _malloc(sizeof(*mac)); for (i = 0; i < ETH_ALEN; i++) { if (n[i] > 255) { _free(mac); - return cli_send(client, "invalid format\r\n"); + cli_send(client, "invalid format\r\n"); + return; } mac->addr[i] = n[i]; } @@ -142,11 +144,9 @@ static int mac_filter_add(const char *addr, void *client) pthread_rwlock_wrlock(&lock); list_add_tail(&mac->entry, &mac_list); pthread_rwlock_unlock(&lock); - - return 0; } -static int mac_filter_del(const char *addr, void *client) +static void mac_filter_del(const char *addr, void *client) { int n[ETH_ALEN]; uint8_t a[ETH_ALEN]; @@ -156,12 +156,14 @@ static int mac_filter_del(const char *addr, void *client) if (sscanf(addr, "%x:%x:%x:%x:%x:%x", n + 0, n + 1, n + 2, n + 3, n + 4, n + 5) != 6) { - return cli_send(client, "invalid format\r\n"); + cli_send(client, "invalid format\r\n"); + return; } for (i = 0; i < ETH_ALEN; i++) { if (n[i] > 255) { - return cli_send(client, "invalid format\r\n"); + cli_send(client, "invalid format\r\n"); + return; } a[i] = n[i]; } @@ -178,16 +180,13 @@ static int mac_filter_del(const char *addr, void *client) pthread_rwlock_unlock(&lock); if (!found) - return cli_send(client, "not found\r\n"); - - return 0; + cli_send(client, "not found\r\n"); } -static int mac_filter_show(void *client) +static void mac_filter_show(void *client) { struct mac_t *mac; const char *filter_type; - char buf[64]; if (type == 0) filter_type = "deny"; @@ -196,88 +195,60 @@ static int mac_filter_show(void *client) else filter_type = "disabled"; - sprintf(buf, "filter type: %s\r\n", filter_type); - - if (cli_send(client, buf)) - return -1; + cli_sendv(client, "filter type: %s\r\n", filter_type); pthread_rwlock_rdlock(&lock); list_for_each_entry(mac, &mac_list, entry) { - sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\r\n", + cli_sendv(client, "%02x:%02x:%02x:%02x:%02x:%02x\r\n", mac->addr[0], mac->addr[1], mac->addr[2], mac->addr[3], mac->addr[4], mac->addr[5]); - if (cli_send(client, buf)) { - pthread_rwlock_unlock(&lock); - return -1; - } } pthread_rwlock_unlock(&lock); - - return 0; } -static int cmd_help(char * const *fields, int fields_cnt, void *client); -int cmd_exec(const char *cmd, char * const *fields, int fields_cnt, void *client) +static void cmd_help(char * const *fields, int fields_cnt, void *client); +static int cmd_exec(const char *cmd, char * const *fields, int fields_cnt, void *client) { if (fields_cnt == 2) - return cmd_help(fields, fields_cnt, client); - + goto help; + if (!strcmp(fields[2], "reload")) { - if (!conf_mac_filter) { - if (cli_send(client, "error: mac-filter was not specified in the config\r\n")) - return CLI_CMD_FAILED; - } else if (mac_filter_load(conf_mac_filter)) { - if (cli_send(client, "error: check logs\r\n")) - return CLI_CMD_FAILED; - } + if (!conf_mac_filter) + cli_send(client, "error: mac-filter was not specified in the config\r\n"); + else if (mac_filter_load(conf_mac_filter)) + cli_send(client, "error: check logs\r\n"); } else if (!strcmp(fields[2], "add")) { if (fields_cnt != 4) - return cmd_help(fields, fields_cnt, client); - if (mac_filter_add(fields[3], client)) - return CLI_CMD_FAILED; + goto help; + mac_filter_add(fields[3], client); } else if (!strcmp(fields[2], "del")) { if (fields_cnt != 4) - return cmd_help(fields, fields_cnt, client); - if (mac_filter_del(fields[3], client)) - return CLI_CMD_FAILED; + goto help; + mac_filter_del(fields[3], client); } else if (!strcmp(fields[2], "show")) { - if (mac_filter_show(client)) - return CLI_CMD_FAILED; + mac_filter_show(client); } + + return CLI_CMD_OK; +help: + cmd_help(fields, fields_cnt, client); return CLI_CMD_OK; } -static int cmd_help(char * const *fields, int fields_cnt, void *client) +static void cmd_help(char * const *fields, int fields_cnt, void *client) { - if (cli_send(client, "pppoe mac-filter reload - reload mac-filter file\r\n")) - return -1; - - if (cli_send(client, "pppoe mac-filter add <address> - add address to mac-filter list\r\n")) - return -1; - - if (cli_send(client, "pppoe mac-filter del <address> - delete address from mac-filter list\r\n")) - return -1; - - if (cli_send(client, "pppoe mac-filter show - show current mac-filter list\r\n")) - return -1; - - return 0; + cli_send(client, "pppoe mac-filter reload - reload mac-filter file\r\n"); + cli_send(client, "pppoe mac-filter add <address> - add address to mac-filter list\r\n"); + cli_send(client, "pppoe mac-filter del <address> - delete address from mac-filter list\r\n"); + cli_send(client, "pppoe mac-filter show - show current mac-filter list\r\n"); } -const char *cmd_hdr[] = {"pppoe", "mac-filter"}; -static struct cli_simple_cmd_t cmd = { - .hdr_len = 2, - .hdr = cmd_hdr, - .exec = cmd_exec, - .help = cmd_help, -}; - static void __init init(void) { const char *opt = conf_get_opt("pppoe", "mac-filter"); if (!opt || mac_filter_load(opt)) type = -1; - cli_register_simple_cmd(&cmd); + cli_register_simple_cmd2(cmd_exec, cmd_help, 2, "pppoe", "mac-filter"); } diff --git a/accel-pptpd/ctrl/pppoe/pppoe.c b/accel-pptpd/ctrl/pppoe/pppoe.c index 73752920..f90d3704 100644 --- a/accel-pptpd/ctrl/pppoe/pppoe.c +++ b/accel-pptpd/ctrl/pppoe/pppoe.c @@ -943,31 +943,6 @@ out_err: _free(serv); } -static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt, void *client) -{ - char buf[128]; - - if (cli_send(client, "pppoe:\r\n")) - return CLI_CMD_FAILED; - - sprintf(buf, " active: %u\r\n", stat_active); - if (cli_send(client, buf)) - return CLI_CMD_FAILED; - - sprintf(buf, " delayed PADO: %u\r\n", stat_delayed_pado); - if (cli_send(client, buf)) - return CLI_CMD_FAILED; - - return CLI_CMD_OK; -} - -static 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, -}; - static int init_secret(void) { int fd; @@ -1024,7 +999,5 @@ static void __init pppoe_init(void) conf_pado_delay = atoi(opt->val); } } - - cli_register_simple_cmd(&show_stat_cmd); } |