]>
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 | ||
8e46010c AA |
72 | static int |
73 | cmd_portid_type_local(struct lldpctl_conn_t *conn, struct writer *w, | |
74 | struct cmd_env *env, void *arg) | |
75 | { | |
76 | lldpctl_atom_t *iface; | |
77 | const char *id = cmdenv_get(env, "port-id"); | |
78 | ||
79 | if (!id || !strlen(id)) { | |
80 | log_warnx("lldpctl", "no id speficied"); | |
81 | return 0; | |
82 | } | |
83 | ||
84 | while ((iface = cmd_iterate_on_interfaces(conn, env))) { | |
85 | lldpctl_atom_t *port = lldpctl_get_port(iface); | |
86 | if (lldpctl_atom_set_str(port, lldpctl_k_port_id, id) == NULL) { | |
87 | log_warnx("lldpctl", "unable to set LLDP PortID." | |
88 | " %s", lldpctl_last_strerror(conn)); | |
89 | } | |
90 | lldpctl_atom_dec_ref(port); | |
91 | } | |
92 | ||
93 | return 1; | |
94 | } | |
95 | ||
8fbd3195 ST |
96 | static int |
97 | cmd_portid_type(struct lldpctl_conn_t *conn, struct writer *w, | |
98 | struct cmd_env *env, void *arg) | |
99 | { | |
100 | char *value_str; | |
cf719ce8 | 101 | int value = -1; |
8fbd3195 ST |
102 | |
103 | log_debug("lldpctl", "lldp PortID TLV Subtype"); | |
104 | ||
105 | lldpctl_atom_t *config = lldpctl_get_configuration(conn); | |
106 | if (config == NULL) { | |
107 | log_warnx("lldpctl", | |
108 | "unable to get configuration from lldpd. %s", | |
109 | lldpctl_last_strerror(conn)); | |
110 | return 0; | |
111 | } | |
112 | ||
113 | value_str = arg; | |
114 | for (lldpctl_map_t *b_map = | |
115 | lldpctl_key_get_map(lldpctl_k_config_lldp_portid_type); | |
116 | b_map->string; b_map++) { | |
117 | if (!strcmp(b_map->string, value_str)) { | |
118 | value = b_map->value; | |
119 | break; | |
120 | } | |
121 | } | |
122 | ||
123 | if (value == -1) { | |
124 | log_warnx("lldpctl", "invalid value"); | |
125 | lldpctl_atom_dec_ref(config); | |
126 | return 0; | |
127 | } | |
128 | ||
129 | if (lldpctl_atom_set_int(config, | |
130 | lldpctl_k_config_lldp_portid_type, value) == NULL) { | |
131 | log_warnx("lldpctl", "unable to set LLDP PortID type." | |
132 | " %s", lldpctl_last_strerror(conn)); | |
133 | lldpctl_atom_dec_ref(config); | |
134 | return 0; | |
135 | } | |
136 | ||
ff0a278f | 137 | log_info("lldpctl", "LLDP PortID TLV type set to new value : %s", value_str); |
8fbd3195 ST |
138 | lldpctl_atom_dec_ref(config); |
139 | ||
140 | return 1; | |
141 | } | |
142 | ||
994b3371 VB |
143 | /** |
144 | * Register `configure lldp` commands. | |
145 | * | |
146 | * Those are the commands that are related to the LLDP protocol but not | |
147 | * Dot1/Dot3/MED. Commands not related to LLDP should go in system instead. | |
148 | */ | |
149 | void | |
150 | register_commands_configure_lldp(struct cmd_node *configure) | |
151 | { | |
152 | struct cmd_node *configure_lldp = commands_new( | |
153 | configure, | |
154 | "lldp", "LLDP configuration", | |
155 | NULL, NULL, NULL); | |
156 | ||
157 | commands_new( | |
158 | commands_new( | |
159 | commands_new(configure_lldp, | |
160 | "tx-interval", "Set LLDP transmit delay", | |
161 | cmd_check_no_env, NULL, "ports"), | |
162 | NULL, "LLDP transmit delay in seconds", | |
163 | NULL, cmd_store_env_value, "tx-interval"), | |
164 | NEWLINE, "Set LLDP transmit delay", | |
165 | NULL, cmd_txdelay, NULL); | |
166 | ||
167 | commands_new( | |
168 | commands_new( | |
169 | commands_new(configure_lldp, | |
170 | "tx-hold", "Set LLDP transmit hold", | |
171 | cmd_check_no_env, NULL, "ports"), | |
172 | NULL, "LLDP transmit hold in seconds", | |
173 | NULL, cmd_store_env_value, "tx-hold"), | |
174 | NEWLINE, "Set LLDP transmit hold", | |
175 | NULL, cmd_txhold, NULL); | |
8fbd3195 ST |
176 | |
177 | /* Now handle the various portid subtypes we can configure. */ | |
178 | struct cmd_node *configure_lldp_portid_type = commands_new( | |
179 | configure_lldp, | |
180 | "portidsubtype", "LLDP PortID TLV Subtype ", | |
181 | NULL, NULL, NULL); | |
182 | ||
183 | for (lldpctl_map_t *b_map = | |
184 | lldpctl_key_get_map(lldpctl_k_config_lldp_portid_type); | |
185 | b_map->string; b_map++) { | |
186 | if (!strcmp(b_map->string, "ifname")) { | |
187 | commands_new( | |
188 | commands_new(configure_lldp_portid_type, | |
189 | b_map->string, "Interface Name", | |
190 | NULL, NULL, NULL), | |
191 | NEWLINE, NULL, | |
192 | NULL, cmd_portid_type, | |
193 | b_map->string); | |
8e46010c AA |
194 | } else if (!strcmp(b_map->string, "local")) { |
195 | commands_new( | |
196 | commands_new( | |
197 | commands_new(configure_lldp_portid_type, | |
198 | b_map->string, "Local", | |
199 | NULL, NULL, NULL), | |
200 | NULL, "PortID", | |
201 | NULL, cmd_store_env_value, "port-id"), | |
202 | NEWLINE, NULL, | |
203 | NULL, cmd_portid_type_local, | |
204 | b_map->string); | |
8fbd3195 ST |
205 | } else if (!strcmp(b_map->string, "macaddress")) { |
206 | commands_new( | |
207 | commands_new(configure_lldp_portid_type, | |
208 | b_map->string, "MAC Address", | |
209 | NULL, NULL, NULL), | |
210 | NEWLINE, NULL, | |
211 | NULL, cmd_portid_type, | |
212 | b_map->string); | |
213 | } | |
214 | } | |
994b3371 | 215 | } |