]>
Commit | Line | Data |
---|---|---|
994b3371 VB |
1 | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | /* | |
3 | * Copyright (c) 2013 Vincent Bernat <bernat@luffy.cx> | |
4 | * | |
5 | * Permission to use, copy, modify, and/or distribute this software for any | |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice appear in all copies. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
16 | */ | |
17 | ||
18 | #include <unistd.h> | |
19 | #include <string.h> | |
20 | ||
21 | #include "client.h" | |
22 | #include "../log.h" | |
23 | ||
24 | static int | |
25 | cmd_txdelay(struct lldpctl_conn_t *conn, struct writer *w, | |
26 | struct cmd_env *env, void *arg) | |
27 | { | |
28 | log_debug("lldpctl", "set transmit delay"); | |
29 | ||
30 | lldpctl_atom_t *config = lldpctl_get_configuration(conn); | |
31 | if (config == NULL) { | |
32 | log_warnx("lldpctl", "unable to get configuration from lldpd. %s", | |
33 | lldpctl_last_strerror(conn)); | |
34 | return 0; | |
35 | } | |
36 | if (lldpctl_atom_set_str(config, | |
37 | lldpctl_k_config_tx_interval, cmdenv_get(env, "tx-interval")) == NULL) { | |
38 | log_warnx("lldpctl", "unable to set transmit delay. %s", | |
39 | lldpctl_last_strerror(conn)); | |
40 | lldpctl_atom_dec_ref(config); | |
41 | return 0; | |
42 | } | |
43 | log_info("lldpctl", "transmit delay set to new value"); | |
44 | lldpctl_atom_dec_ref(config); | |
45 | return 1; | |
46 | } | |
47 | ||
48 | static int | |
49 | cmd_txhold(struct lldpctl_conn_t *conn, struct writer *w, | |
50 | struct cmd_env *env, void *arg) | |
51 | { | |
52 | log_debug("lldpctl", "set transmit hold"); | |
53 | ||
54 | lldpctl_atom_t *config = lldpctl_get_configuration(conn); | |
55 | if (config == NULL) { | |
56 | log_warnx("lldpctl", "unable to get configuration from lldpd. %s", | |
57 | lldpctl_last_strerror(conn)); | |
58 | return 0; | |
59 | } | |
60 | if (lldpctl_atom_set_str(config, | |
61 | lldpctl_k_config_tx_hold, cmdenv_get(env, "tx-hold")) == NULL) { | |
62 | log_warnx("lldpctl", "unable to set transmit hold. %s", | |
63 | lldpctl_last_strerror(conn)); | |
64 | lldpctl_atom_dec_ref(config); | |
65 | return 0; | |
66 | } | |
67 | log_info("lldpctl", "transmit hold set to new value %s", cmdenv_get(env, "tx-hold")); | |
68 | lldpctl_atom_dec_ref(config); | |
69 | return 1; | |
70 | } | |
71 | ||
8fbd3195 ST |
72 | static int |
73 | cmd_portid_type(struct lldpctl_conn_t *conn, struct writer *w, | |
74 | struct cmd_env *env, void *arg) | |
75 | { | |
76 | char *value_str; | |
cf719ce8 | 77 | int value = -1; |
8fbd3195 ST |
78 | |
79 | log_debug("lldpctl", "lldp PortID TLV Subtype"); | |
80 | ||
81 | lldpctl_atom_t *config = lldpctl_get_configuration(conn); | |
82 | if (config == NULL) { | |
83 | log_warnx("lldpctl", | |
84 | "unable to get configuration from lldpd. %s", | |
85 | lldpctl_last_strerror(conn)); | |
86 | return 0; | |
87 | } | |
88 | ||
89 | value_str = arg; | |
90 | for (lldpctl_map_t *b_map = | |
91 | lldpctl_key_get_map(lldpctl_k_config_lldp_portid_type); | |
92 | b_map->string; b_map++) { | |
93 | if (!strcmp(b_map->string, value_str)) { | |
94 | value = b_map->value; | |
95 | break; | |
96 | } | |
97 | } | |
98 | ||
99 | if (value == -1) { | |
100 | log_warnx("lldpctl", "invalid value"); | |
101 | lldpctl_atom_dec_ref(config); | |
102 | return 0; | |
103 | } | |
104 | ||
105 | if (lldpctl_atom_set_int(config, | |
106 | lldpctl_k_config_lldp_portid_type, value) == NULL) { | |
107 | log_warnx("lldpctl", "unable to set LLDP PortID type." | |
108 | " %s", lldpctl_last_strerror(conn)); | |
109 | lldpctl_atom_dec_ref(config); | |
110 | return 0; | |
111 | } | |
112 | ||
ff0a278f | 113 | log_info("lldpctl", "LLDP PortID TLV type set to new value : %s", value_str); |
8fbd3195 ST |
114 | lldpctl_atom_dec_ref(config); |
115 | ||
116 | return 1; | |
117 | } | |
118 | ||
994b3371 VB |
119 | /** |
120 | * Register `configure lldp` commands. | |
121 | * | |
122 | * Those are the commands that are related to the LLDP protocol but not | |
123 | * Dot1/Dot3/MED. Commands not related to LLDP should go in system instead. | |
124 | */ | |
125 | void | |
126 | register_commands_configure_lldp(struct cmd_node *configure) | |
127 | { | |
128 | struct cmd_node *configure_lldp = commands_new( | |
129 | configure, | |
130 | "lldp", "LLDP configuration", | |
131 | NULL, NULL, NULL); | |
132 | ||
133 | commands_new( | |
134 | commands_new( | |
135 | commands_new(configure_lldp, | |
136 | "tx-interval", "Set LLDP transmit delay", | |
137 | cmd_check_no_env, NULL, "ports"), | |
138 | NULL, "LLDP transmit delay in seconds", | |
139 | NULL, cmd_store_env_value, "tx-interval"), | |
140 | NEWLINE, "Set LLDP transmit delay", | |
141 | NULL, cmd_txdelay, NULL); | |
142 | ||
143 | commands_new( | |
144 | commands_new( | |
145 | commands_new(configure_lldp, | |
146 | "tx-hold", "Set LLDP transmit hold", | |
147 | cmd_check_no_env, NULL, "ports"), | |
148 | NULL, "LLDP transmit hold in seconds", | |
149 | NULL, cmd_store_env_value, "tx-hold"), | |
150 | NEWLINE, "Set LLDP transmit hold", | |
151 | NULL, cmd_txhold, NULL); | |
8fbd3195 ST |
152 | |
153 | /* Now handle the various portid subtypes we can configure. */ | |
154 | struct cmd_node *configure_lldp_portid_type = commands_new( | |
155 | configure_lldp, | |
156 | "portidsubtype", "LLDP PortID TLV Subtype ", | |
157 | NULL, NULL, NULL); | |
158 | ||
159 | for (lldpctl_map_t *b_map = | |
160 | lldpctl_key_get_map(lldpctl_k_config_lldp_portid_type); | |
161 | b_map->string; b_map++) { | |
162 | if (!strcmp(b_map->string, "ifname")) { | |
163 | commands_new( | |
164 | commands_new(configure_lldp_portid_type, | |
165 | b_map->string, "Interface Name", | |
166 | NULL, NULL, NULL), | |
167 | NEWLINE, NULL, | |
168 | NULL, cmd_portid_type, | |
169 | b_map->string); | |
170 | } else if (!strcmp(b_map->string, "macaddress")) { | |
171 | commands_new( | |
172 | commands_new(configure_lldp_portid_type, | |
173 | b_map->string, "MAC Address", | |
174 | NULL, NULL, NULL), | |
175 | NEWLINE, NULL, | |
176 | NULL, cmd_portid_type, | |
177 | b_map->string); | |
178 | } | |
179 | } | |
994b3371 | 180 | } |