]>
Commit | Line | Data |
---|---|---|
4b292b55 VB |
1 | /* -*- mode: c; c-file-style: "openbsd" -*- */ |
2 | /* | |
3 | * Copyright (c) 2008 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 <stdlib.h> | |
19 | #include <unistd.h> | |
20 | #include <time.h> | |
21 | #include "lldpd-structs.h" | |
6f8925be | 22 | #include "log.h" |
4b292b55 VB |
23 | |
24 | void | |
25 | lldpd_chassis_mgmt_cleanup(struct lldpd_chassis *chassis) | |
26 | { | |
27 | struct lldpd_mgmt *mgmt, *mgmt_next; | |
6f8925be VB |
28 | |
29 | log_debug("alloc", "cleanup management addresses for chassis %s", | |
aed91d90 | 30 | chassis->c_name ? chassis->c_name : "(unknown)"); |
6f8925be | 31 | |
4b292b55 VB |
32 | for (mgmt = TAILQ_FIRST(&chassis->c_mgmt); |
33 | mgmt != NULL; | |
34 | mgmt = mgmt_next) { | |
35 | mgmt_next = TAILQ_NEXT(mgmt, m_entries); | |
36 | free(mgmt); | |
37 | } | |
38 | TAILQ_INIT(&chassis->c_mgmt); | |
39 | } | |
40 | ||
41 | void | |
42 | lldpd_chassis_cleanup(struct lldpd_chassis *chassis, int all) | |
43 | { | |
e12c2365 | 44 | lldpd_chassis_mgmt_cleanup(chassis); |
6f8925be | 45 | log_debug("alloc", "cleanup chassis %s", |
aed91d90 | 46 | chassis->c_name ? chassis->c_name : "(unknown)"); |
4b292b55 VB |
47 | #ifdef ENABLE_LLDPMED |
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); | |
55 | #endif | |
56 | free(chassis->c_id); | |
57 | free(chassis->c_name); | |
58 | free(chassis->c_descr); | |
4b292b55 VB |
59 | if (all) |
60 | free(chassis); | |
61 | } | |
62 | ||
63 | #ifdef ENABLE_DOT1 | |
64 | void | |
65 | lldpd_vlan_cleanup(struct lldpd_port *port) | |
66 | { | |
67 | struct lldpd_vlan *vlan, *vlan_next; | |
68 | for (vlan = TAILQ_FIRST(&port->p_vlans); | |
69 | vlan != NULL; | |
70 | vlan = vlan_next) { | |
71 | free(vlan->v_name); | |
72 | vlan_next = TAILQ_NEXT(vlan, v_entries); | |
73 | free(vlan); | |
74 | } | |
75 | TAILQ_INIT(&port->p_vlans); | |
76 | } | |
77 | ||
78 | void | |
79 | lldpd_ppvid_cleanup(struct lldpd_port *port) | |
80 | { | |
81 | struct lldpd_ppvid *ppvid, *ppvid_next; | |
82 | for (ppvid = TAILQ_FIRST(&port->p_ppvids); | |
83 | ppvid != NULL; | |
84 | ppvid = ppvid_next) { | |
85 | ppvid_next = TAILQ_NEXT(ppvid, p_entries); | |
86 | free(ppvid); | |
87 | } | |
88 | TAILQ_INIT(&port->p_ppvids); | |
89 | } | |
90 | ||
91 | void | |
92 | lldpd_pi_cleanup(struct lldpd_port *port) | |
93 | { | |
94 | struct lldpd_pi *pi, *pi_next; | |
95 | for (pi = TAILQ_FIRST(&port->p_pids); | |
96 | pi != NULL; | |
97 | pi = pi_next) { | |
98 | free(pi->p_pi); | |
99 | pi_next = TAILQ_NEXT(pi, p_entries); | |
100 | free(pi); | |
101 | } | |
102 | TAILQ_INIT(&port->p_pids); | |
103 | } | |
104 | #endif | |
105 | ||
fb1b78bb | 106 | #ifdef ENABLE_CUSTOM |
cd5de7a2 AA |
107 | void |
108 | lldpd_custom_list_cleanup(struct lldpd_port *port) | |
109 | { | |
110 | struct lldpd_custom *custom, *custom_next; | |
111 | for (custom = TAILQ_FIRST(&port->p_custom_list); | |
112 | custom != NULL; | |
113 | custom = custom_next) { | |
114 | custom_next = TAILQ_NEXT(custom, next); | |
8caf4341 | 115 | free(custom->oui_info); |
cd5de7a2 AA |
116 | free(custom); |
117 | } | |
118 | TAILQ_INIT(&port->p_custom_list); | |
119 | } | |
fb1b78bb | 120 | #endif |
cd5de7a2 | 121 | |
ef3707da VB |
122 | /* Cleanup a remote port. The before last argument, `expire` is a function that |
123 | * should be called when a remote port is removed. If the last argument is 1, | |
124 | * all remote ports are removed. | |
125 | */ | |
4b292b55 | 126 | void |
4e90a9e0 | 127 | lldpd_remote_cleanup(struct lldpd_hardware *hardware, |
ef3707da VB |
128 | void(*expire)(struct lldpd_hardware *, struct lldpd_port *), |
129 | int all) | |
4b292b55 VB |
130 | { |
131 | struct lldpd_port *port, *port_next; | |
132 | int del; | |
3333d2a8 | 133 | time_t now = time(NULL); |
6f8925be VB |
134 | |
135 | log_debug("alloc", "cleanup remote port on %s", | |
136 | hardware->h_ifname); | |
4b292b55 VB |
137 | for (port = TAILQ_FIRST(&hardware->h_rports); |
138 | port != NULL; | |
139 | port = port_next) { | |
140 | port_next = TAILQ_NEXT(port, p_entries); | |
ef3707da | 141 | del = all; |
42ea638d | 142 | if (!all && expire && |
5a215d4b | 143 | (now >= port->p_lastupdate + port->p_chassis->c_ttl)) { |
14052b61 VB |
144 | hardware->h_ageout_cnt++; |
145 | hardware->h_delete_cnt++; | |
4b292b55 VB |
146 | del = 1; |
147 | } | |
148 | if (del) { | |
41e57d5a | 149 | if (expire) expire(hardware, port); |
4e97a0bf VB |
150 | /* This TAILQ_REMOVE is dangerous. It should not be |
151 | * called while in liblldpctl because we don't have a | |
152 | * real list. It is only needed to be called when we | |
153 | * don't delete the entire list. */ | |
154 | if (!all) TAILQ_REMOVE(&hardware->h_rports, port, p_entries); | |
4b292b55 VB |
155 | lldpd_port_cleanup(port, 1); |
156 | free(port); | |
157 | } | |
158 | } | |
ef3707da | 159 | if (all) TAILQ_INIT(&hardware->h_rports); |
4b292b55 VB |
160 | } |
161 | ||
162 | /* If `all' is true, clear all information, including information that | |
163 | are not refreshed periodically. Port should be freed manually. */ | |
164 | void | |
165 | lldpd_port_cleanup(struct lldpd_port *port, int all) | |
166 | { | |
167 | #ifdef ENABLE_LLDPMED | |
168 | int i; | |
169 | if (all) | |
170 | for (i=0; i < LLDP_MED_LOCFORMAT_LAST; i++) | |
171 | free(port->p_med_location[i].data); | |
172 | #endif | |
173 | #ifdef ENABLE_DOT1 | |
174 | lldpd_vlan_cleanup(port); | |
175 | lldpd_ppvid_cleanup(port); | |
176 | lldpd_pi_cleanup(port); | |
177 | #endif | |
8fbd3195 ST |
178 | /* will set these to NULL so we don't free wrong memory */ |
179 | ||
4b292b55 | 180 | if (all) { |
8e46010c AA |
181 | free(port->p_id); |
182 | port->p_id = NULL; | |
c267d0f2 AA |
183 | free(port->p_descr); |
184 | port->p_descr = NULL; | |
4b292b55 VB |
185 | free(port->p_lastframe); |
186 | if (port->p_chassis) { /* chassis may not have been attributed, yet */ | |
187 | port->p_chassis->c_refcount--; | |
188 | port->p_chassis = NULL; | |
189 | } | |
fb1b78bb | 190 | #ifdef ENABLE_CUSTOM |
cd5de7a2 | 191 | lldpd_custom_list_cleanup(port); |
fb1b78bb | 192 | #endif |
4b292b55 VB |
193 | } |
194 | } | |
8ec333bd VB |
195 | |
196 | void | |
197 | lldpd_config_cleanup(struct lldpd_config *config) | |
198 | { | |
6f8925be | 199 | log_debug("alloc", "general configuration cleanup"); |
8ec333bd VB |
200 | free(config->c_mgmt_pattern); |
201 | free(config->c_cid_pattern); | |
202 | free(config->c_iface_pattern); | |
ce347d29 | 203 | free(config->c_hostname); |
8ec333bd VB |
204 | free(config->c_platform); |
205 | free(config->c_description); | |
206 | } |