From 994b3371b3b07227ac21a66de61923432a5032dd Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 21 Jun 2013 01:01:46 +0200 Subject: [PATCH] lldpcli: split `conf.c` into `conf-system.c` and `conf-lldp.c`. --- src/client/Makefile.am | 1 + src/client/client.h | 2 + src/client/conf-lldp.c | 105 ++++++++++++++++++++++++++++++ src/client/conf-system.c | 74 +++++++++++++++++++++ src/client/conf.c | 135 --------------------------------------- 5 files changed, 182 insertions(+), 135 deletions(-) create mode 100644 src/client/conf-lldp.c create mode 100644 src/client/conf-system.c diff --git a/src/client/Makefile.am b/src/client/Makefile.am index 44b6fefe..371d5b50 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -11,6 +11,7 @@ uninstall-local: lldpcli_SOURCES = client.h lldpcli.c display.c \ conf.c conf-med.c conf-dot3.c conf-power.c \ + conf-lldp.c conf-system.c \ commands.c show.c \ misc.c tokenizer.c \ writer.h text_writer.c kv_writer.c diff --git a/src/client/client.h b/src/client/client.h index 1b460d35..a2de3192 100644 --- a/src/client/client.h +++ b/src/client/client.h @@ -124,6 +124,8 @@ void register_commands_watch(struct cmd_node *); /* conf*.c */ void register_commands_configure(struct cmd_node *); +void register_commands_configure_system(struct cmd_node *); +void register_commands_configure_lldp(struct cmd_node *); void register_commands_configure_med(struct cmd_node *, struct cmd_node *); void register_commands_configure_dot3(struct cmd_node *); void register_commands_medpow(struct cmd_node *); diff --git a/src/client/conf-lldp.c b/src/client/conf-lldp.c new file mode 100644 index 00000000..e5389ad1 --- /dev/null +++ b/src/client/conf-lldp.c @@ -0,0 +1,105 @@ +/* -*- mode: c; c-file-style: "openbsd" -*- */ +/* + * Copyright (c) 2013 Vincent Bernat + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "client.h" +#include "../log.h" + +static int +cmd_txdelay(struct lldpctl_conn_t *conn, struct writer *w, + struct cmd_env *env, void *arg) +{ + log_debug("lldpctl", "set transmit delay"); + + lldpctl_atom_t *config = lldpctl_get_configuration(conn); + if (config == NULL) { + log_warnx("lldpctl", "unable to get configuration from lldpd. %s", + lldpctl_last_strerror(conn)); + return 0; + } + if (lldpctl_atom_set_str(config, + lldpctl_k_config_tx_interval, cmdenv_get(env, "tx-interval")) == NULL) { + log_warnx("lldpctl", "unable to set transmit delay. %s", + lldpctl_last_strerror(conn)); + lldpctl_atom_dec_ref(config); + return 0; + } + log_info("lldpctl", "transmit delay set to new value"); + lldpctl_atom_dec_ref(config); + return 1; +} + +static int +cmd_txhold(struct lldpctl_conn_t *conn, struct writer *w, + struct cmd_env *env, void *arg) +{ + log_debug("lldpctl", "set transmit hold"); + + lldpctl_atom_t *config = lldpctl_get_configuration(conn); + if (config == NULL) { + log_warnx("lldpctl", "unable to get configuration from lldpd. %s", + lldpctl_last_strerror(conn)); + return 0; + } + if (lldpctl_atom_set_str(config, + lldpctl_k_config_tx_hold, cmdenv_get(env, "tx-hold")) == NULL) { + log_warnx("lldpctl", "unable to set transmit hold. %s", + lldpctl_last_strerror(conn)); + lldpctl_atom_dec_ref(config); + return 0; + } + log_info("lldpctl", "transmit hold set to new value %s", cmdenv_get(env, "tx-hold")); + lldpctl_atom_dec_ref(config); + return 1; +} + +/** + * Register `configure lldp` commands. + * + * Those are the commands that are related to the LLDP protocol but not + * Dot1/Dot3/MED. Commands not related to LLDP should go in system instead. + */ +void +register_commands_configure_lldp(struct cmd_node *configure) +{ + struct cmd_node *configure_lldp = commands_new( + configure, + "lldp", "LLDP configuration", + NULL, NULL, NULL); + + commands_new( + commands_new( + commands_new(configure_lldp, + "tx-interval", "Set LLDP transmit delay", + cmd_check_no_env, NULL, "ports"), + NULL, "LLDP transmit delay in seconds", + NULL, cmd_store_env_value, "tx-interval"), + NEWLINE, "Set LLDP transmit delay", + NULL, cmd_txdelay, NULL); + + commands_new( + commands_new( + commands_new(configure_lldp, + "tx-hold", "Set LLDP transmit hold", + cmd_check_no_env, NULL, "ports"), + NULL, "LLDP transmit hold in seconds", + NULL, cmd_store_env_value, "tx-hold"), + NEWLINE, "Set LLDP transmit hold", + NULL, cmd_txhold, NULL); +} diff --git a/src/client/conf-system.c b/src/client/conf-system.c new file mode 100644 index 00000000..49728002 --- /dev/null +++ b/src/client/conf-system.c @@ -0,0 +1,74 @@ +/* -*- mode: c; c-file-style: "openbsd" -*- */ +/* + * Copyright (c) 2013 Vincent Bernat + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "client.h" +#include "../log.h" + +static int +cmd_iface_pattern(struct lldpctl_conn_t *conn, struct writer *w, + struct cmd_env *env, void *arg) +{ + log_debug("lldpctl", "set iface pattern"); + + lldpctl_atom_t *config = lldpctl_get_configuration(conn); + if (config == NULL) { + log_warnx("lldpctl", "unable to get configuration from lldpd. %s", + lldpctl_last_strerror(conn)); + return 0; + } + if (lldpctl_atom_set_str(config, + lldpctl_k_config_iface_pattern, cmdenv_get(env, "iface-pattern")) == NULL) { + log_warnx("lldpctl", "unable to set iface-pattern. %s", + lldpctl_last_strerror(conn)); + lldpctl_atom_dec_ref(config); + return 0; + } + log_info("lldpctl", "iface-pattern set to new value %s", cmdenv_get(env, "iface-pattern")); + lldpctl_atom_dec_ref(config); + return 1; +} + +/** + * Register `configure system` commands. + * + * Those are the commands to configure protocol-independant stuff. + */ +void +register_commands_configure_system(struct cmd_node *configure) +{ + struct cmd_node *configure_system = commands_new( + configure, + "system", "System configuration", + cmd_check_no_env, NULL, "ports"); + struct cmd_node *configure_interface = commands_new( + configure_system, + "interface", "Configure interface related items", + NULL, NULL, NULL); + + commands_new( + commands_new( + commands_new(configure_interface, + "pattern", "Set active interface pattern", + NULL, NULL, NULL), + NULL, "Interface pattern (comma separated list of wildcards)", + NULL, cmd_store_env_value, "iface-pattern"), + NEWLINE, "Set active interface pattern", + NULL, cmd_iface_pattern, NULL); +} diff --git a/src/client/conf.c b/src/client/conf.c index f11324d4..7b8e68ac 100644 --- a/src/client/conf.c +++ b/src/client/conf.c @@ -21,141 +21,6 @@ #include "client.h" #include "../log.h" -static int -cmd_txdelay(struct lldpctl_conn_t *conn, struct writer *w, - struct cmd_env *env, void *arg) -{ - log_debug("lldpctl", "set transmit delay"); - - lldpctl_atom_t *config = lldpctl_get_configuration(conn); - if (config == NULL) { - log_warnx("lldpctl", "unable to get configuration from lldpd. %s", - lldpctl_last_strerror(conn)); - return 0; - } - if (lldpctl_atom_set_str(config, - lldpctl_k_config_tx_interval, cmdenv_get(env, "tx-interval")) == NULL) { - log_warnx("lldpctl", "unable to set transmit delay. %s", - lldpctl_last_strerror(conn)); - lldpctl_atom_dec_ref(config); - return 0; - } - log_info("lldpctl", "transmit delay set to new value"); - lldpctl_atom_dec_ref(config); - return 1; -} - -static int -cmd_txhold(struct lldpctl_conn_t *conn, struct writer *w, - struct cmd_env *env, void *arg) -{ - log_debug("lldpctl", "set transmit hold"); - - lldpctl_atom_t *config = lldpctl_get_configuration(conn); - if (config == NULL) { - log_warnx("lldpctl", "unable to get configuration from lldpd. %s", - lldpctl_last_strerror(conn)); - return 0; - } - if (lldpctl_atom_set_str(config, - lldpctl_k_config_tx_hold, cmdenv_get(env, "tx-hold")) == NULL) { - log_warnx("lldpctl", "unable to set transmit hold. %s", - lldpctl_last_strerror(conn)); - lldpctl_atom_dec_ref(config); - return 0; - } - log_info("lldpctl", "transmit hold set to new value %s", cmdenv_get(env, "tx-hold")); - lldpctl_atom_dec_ref(config); - return 1; -} - -static int -cmd_iface_pattern(struct lldpctl_conn_t *conn, struct writer *w, - struct cmd_env *env, void *arg) -{ - log_debug("lldpctl", "set iface pattern"); - - lldpctl_atom_t *config = lldpctl_get_configuration(conn); - if (config == NULL) { - log_warnx("lldpctl", "unable to get configuration from lldpd. %s", - lldpctl_last_strerror(conn)); - return 0; - } - if (lldpctl_atom_set_str(config, - lldpctl_k_config_iface_pattern, cmdenv_get(env, "iface-pattern")) == NULL) { - log_warnx("lldpctl", "unable to set iface-pattern. %s", - lldpctl_last_strerror(conn)); - lldpctl_atom_dec_ref(config); - return 0; - } - log_info("lldpctl", "iface-pattern set to new value %s", cmdenv_get(env, "iface-pattern")); - lldpctl_atom_dec_ref(config); - return 1; -} - -/** - * Register `configure system` commands. - * - * Those are the commands to configure protocol-independant stuff. - */ -static void -register_commands_configure_system(struct cmd_node *configure) -{ - struct cmd_node *configure_system = commands_new( - configure, - "system", "System configuration", - cmd_check_no_env, NULL, "ports"); - struct cmd_node *configure_interface = commands_new( - configure_system, - "interface", "Configure interface related items", - NULL, NULL, NULL); - - commands_new( - commands_new( - commands_new(configure_interface, - "pattern", "Set active interface pattern", - NULL, NULL, NULL), - NULL, "Interface pattern (comma separated list of wildcards)", - NULL, cmd_store_env_value, "iface-pattern"), - NEWLINE, "Set active interface pattern", - NULL, cmd_iface_pattern, NULL); -} - -/** - * Register `configure lldp` commands. - * - * Those are the commands that are related to the LLDP protocol but not - * Dot1/Dot3/MED. Commands not related to LLDP should go in system instead. - */ -static void -register_commands_configure_lldp(struct cmd_node *configure) -{ - struct cmd_node *configure_lldp = commands_new( - configure, - "lldp", "LLDP configuration", - NULL, NULL, NULL); - - commands_new( - commands_new( - commands_new(configure_lldp, - "tx-interval", "Set LLDP transmit delay", - cmd_check_no_env, NULL, "ports"), - NULL, "LLDP transmit delay in seconds", - NULL, cmd_store_env_value, "tx-interval"), - NEWLINE, "Set LLDP transmit delay", - NULL, cmd_txdelay, NULL); - - commands_new( - commands_new( - commands_new(configure_lldp, - "tx-hold", "Set LLDP transmit hold", - cmd_check_no_env, NULL, "ports"), - NULL, "LLDP transmit hold in seconds", - NULL, cmd_store_env_value, "tx-hold"), - NEWLINE, "Set LLDP transmit hold", - NULL, cmd_txhold, NULL); -} - /** * Register `configure` and `no configure` commands. */ -- 2.39.5