]>
git.ipfire.org Git - thirdparty/lldpd.git/blob - src/lldpd-structs.c
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
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.
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.
21 #include "lldpd-structs.h"
25 lldpd_chassis_mgmt_cleanup(struct lldpd_chassis
*chassis
)
27 struct lldpd_mgmt
*mgmt
, *mgmt_next
;
29 log_debug("alloc", "cleanup management addresses for chassis %s",
30 chassis
->c_name
? chassis
->c_name
: "(unknown)");
32 for (mgmt
= TAILQ_FIRST(&chassis
->c_mgmt
);
35 mgmt_next
= TAILQ_NEXT(mgmt
, m_entries
);
38 TAILQ_INIT(&chassis
->c_mgmt
);
42 lldpd_chassis_cleanup(struct lldpd_chassis
*chassis
, int all
)
44 lldpd_chassis_mgmt_cleanup(chassis
);
45 log_debug("alloc", "cleanup chassis %s",
46 chassis
->c_name
? chassis
->c_name
: "(unknown)");
48 free(chassis
->c_med_hw
);
49 free(chassis
->c_med_sw
);
50 free(chassis
->c_med_fw
);
51 free(chassis
->c_med_sn
);
52 free(chassis
->c_med_manuf
);
53 free(chassis
->c_med_model
);
54 free(chassis
->c_med_asset
);
57 free(chassis
->c_name
);
58 free(chassis
->c_descr
);
65 lldpd_vlan_cleanup(struct lldpd_port
*port
)
67 struct lldpd_vlan
*vlan
, *vlan_next
;
68 for (vlan
= TAILQ_FIRST(&port
->p_vlans
);
72 vlan_next
= TAILQ_NEXT(vlan
, v_entries
);
75 TAILQ_INIT(&port
->p_vlans
);
79 lldpd_ppvid_cleanup(struct lldpd_port
*port
)
81 struct lldpd_ppvid
*ppvid
, *ppvid_next
;
82 for (ppvid
= TAILQ_FIRST(&port
->p_ppvids
);
85 ppvid_next
= TAILQ_NEXT(ppvid
, p_entries
);
88 TAILQ_INIT(&port
->p_ppvids
);
92 lldpd_pi_cleanup(struct lldpd_port
*port
)
94 struct lldpd_pi
*pi
, *pi_next
;
95 for (pi
= TAILQ_FIRST(&port
->p_pids
);
99 pi_next
= TAILQ_NEXT(pi
, p_entries
);
102 TAILQ_INIT(&port
->p_pids
);
108 lldpd_custom_tlv_add(struct lldpd_port
*port
, struct lldpd_custom
*curr
)
110 struct lldpd_custom
*custom
;
112 if ((custom
= malloc(sizeof(struct lldpd_custom
)))) {
113 memcpy(custom
, curr
, sizeof(struct lldpd_custom
));
114 if ((custom
->oui_info
= malloc(custom
->oui_info_len
))) {
115 memcpy(custom
->oui_info
, curr
->oui_info
, custom
->oui_info_len
);
116 TAILQ_INSERT_TAIL(&port
->p_custom_list
, custom
, next
);
119 log_warn("rpc", "could not allocate memory for custom TLV info");
125 lldpd_custom_tlv_cleanup(struct lldpd_port
*port
, struct lldpd_custom
*curr
)
127 struct lldpd_custom
*custom
, *custom_next
;
128 for (custom
= TAILQ_FIRST(&port
->p_custom_list
);
130 custom
= custom_next
) {
131 custom_next
= TAILQ_NEXT(custom
, next
);
132 if (!memcmp(curr
->oui
, custom
->oui
, sizeof(curr
->oui
)) &&
133 curr
->subtype
== custom
->subtype
) {
134 TAILQ_REMOVE(&port
->p_custom_list
, custom
, next
);
135 free(custom
->oui_info
);
142 lldpd_custom_list_cleanup(struct lldpd_port
*port
)
144 struct lldpd_custom
*custom
, *custom_next
;
145 for (custom
= TAILQ_FIRST(&port
->p_custom_list
);
147 custom
= custom_next
) {
148 custom_next
= TAILQ_NEXT(custom
, next
);
149 free(custom
->oui_info
);
152 TAILQ_INIT(&port
->p_custom_list
);
156 /* Cleanup a remote port. The before last argument, `expire` is a function that
157 * should be called when a remote port is removed. If the last argument is 1,
158 * all remote ports are removed.
161 lldpd_remote_cleanup(struct lldpd_hardware
*hardware
,
162 void(*expire
)(struct lldpd_hardware
*, struct lldpd_port
*),
165 struct lldpd_port
*port
, *port_next
;
167 time_t now
= time(NULL
);
169 log_debug("alloc", "cleanup remote port on %s",
171 for (port
= TAILQ_FIRST(&hardware
->h_rports
);
174 port_next
= TAILQ_NEXT(port
, p_entries
);
176 if (!all
&& expire
&&
177 (now
>= port
->p_lastupdate
+ port
->p_ttl
)) {
178 hardware
->h_ageout_cnt
++;
179 hardware
->h_delete_cnt
++;
183 if (expire
) expire(hardware
, port
);
184 /* This TAILQ_REMOVE is dangerous. It should not be
185 * called while in liblldpctl because we don't have a
186 * real list. It is only needed to be called when we
187 * don't delete the entire list. */
188 if (!all
) TAILQ_REMOVE(&hardware
->h_rports
, port
, p_entries
);
189 lldpd_port_cleanup(port
, 1);
193 if (all
) TAILQ_INIT(&hardware
->h_rports
);
196 /* If `all' is true, clear all information, including information that
197 are not refreshed periodically. Port should be freed manually. */
199 lldpd_port_cleanup(struct lldpd_port
*port
, int all
)
201 #ifdef ENABLE_LLDPMED
204 for (i
=0; i
< LLDP_MED_LOCFORMAT_LAST
; i
++)
205 free(port
->p_med_location
[i
].data
);
208 lldpd_vlan_cleanup(port
);
209 lldpd_ppvid_cleanup(port
);
210 lldpd_pi_cleanup(port
);
212 /* will set these to NULL so we don't free wrong memory */
218 port
->p_descr
= NULL
;
219 free(port
->p_lastframe
);
220 if (port
->p_chassis
) { /* chassis may not have been attributed, yet */
221 port
->p_chassis
->c_refcount
--;
222 port
->p_chassis
= NULL
;
225 lldpd_custom_list_cleanup(port
);
231 lldpd_config_cleanup(struct lldpd_config
*config
)
233 log_debug("alloc", "general configuration cleanup");
234 free(config
->c_mgmt_pattern
);
235 free(config
->c_cid_pattern
);
236 free(config
->c_cid_string
);
237 free(config
->c_iface_pattern
);
238 free(config
->c_perm_ifaces
);
239 free(config
->c_hostname
);
240 free(config
->c_platform
);
241 free(config
->c_description
);