]> git.ipfire.org Git - thirdparty/lldpd.git/blobdiff - src/lldpd-structs.c
tests: fix skip instruction
[thirdparty/lldpd.git] / src / lldpd-structs.c
index a14fe5da3e6d72214933901c5d8dca4037bc8139..95cef9f11a1169f6310e7fc1f9cba7fdfd9beb11 100644 (file)
@@ -27,7 +27,7 @@ lldpd_chassis_mgmt_cleanup(struct lldpd_chassis *chassis)
        struct lldpd_mgmt *mgmt, *mgmt_next;
 
        log_debug("alloc", "cleanup management addresses for chassis %s",
-           chassis->c_name ? chassis->c_name : "(unknwon)");
+           chassis->c_name ? chassis->c_name : "(unknown)");
 
        for (mgmt = TAILQ_FIRST(&chassis->c_mgmt);
             mgmt != NULL;
@@ -43,7 +43,7 @@ lldpd_chassis_cleanup(struct lldpd_chassis *chassis, int all)
 {
        lldpd_chassis_mgmt_cleanup(chassis);
        log_debug("alloc", "cleanup chassis %s",
-           chassis->c_name ? chassis->c_name : "(unknwon)");
+           chassis->c_name ? chassis->c_name : "(unknown)");
 #ifdef ENABLE_LLDPMED
        free(chassis->c_med_hw);
        free(chassis->c_med_sw);
@@ -103,12 +103,64 @@ lldpd_pi_cleanup(struct lldpd_port *port)
 }
 #endif
 
-/* Cleanup a remote port. The last argument, `expire` is a function that should
- * be called when expiration happens. If it is NULL, all remote ports are
- * removed. */
+#ifdef ENABLE_CUSTOM
+void
+lldpd_custom_tlv_add(struct lldpd_port *port, struct lldpd_custom *curr)
+{
+       struct lldpd_custom *custom;
+
+       if ((custom = malloc(sizeof(struct lldpd_custom)))) {
+               memcpy(custom, curr, sizeof(struct lldpd_custom));
+               if ((custom->oui_info = malloc(custom->oui_info_len))) {
+                       memcpy(custom->oui_info, curr->oui_info, custom->oui_info_len);
+                       TAILQ_INSERT_TAIL(&port->p_custom_list, custom, next);
+               } else {
+                       free(custom);
+                       log_warn("rpc", "could not allocate memory for custom TLV info");
+               }
+       }
+}
+
+void
+lldpd_custom_tlv_cleanup(struct lldpd_port *port, struct lldpd_custom *curr)
+{
+       struct lldpd_custom *custom, *custom_next;
+       for (custom = TAILQ_FIRST(&port->p_custom_list);
+           custom != NULL;
+           custom = custom_next) {
+               custom_next = TAILQ_NEXT(custom, next);
+               if (!memcmp(curr->oui, custom->oui, sizeof(curr->oui)) &&
+                   curr->subtype == custom->subtype) {
+                       TAILQ_REMOVE(&port->p_custom_list, custom, next);
+                       free(custom->oui_info);
+                       free(custom);
+               }
+       }
+}
+
+void
+lldpd_custom_list_cleanup(struct lldpd_port *port)
+{
+       struct lldpd_custom *custom, *custom_next;
+       for (custom = TAILQ_FIRST(&port->p_custom_list);
+           custom != NULL;
+           custom = custom_next) {
+               custom_next = TAILQ_NEXT(custom, next);
+               free(custom->oui_info);
+               free(custom);
+       }
+       TAILQ_INIT(&port->p_custom_list);
+}
+#endif
+
+/* Cleanup a remote port. The before last argument, `expire` is a function that
+ * should be called when a remote port is removed. If the last argument is 1,
+ * all remote ports are removed.
+ */
 void
 lldpd_remote_cleanup(struct lldpd_hardware *hardware,
-    void(*expire)(struct lldpd_hardware *, struct lldpd_port *))
+    void(*expire)(struct lldpd_hardware *, struct lldpd_port *),
+    int all)
 {
        struct lldpd_port *port, *port_next;
        int del;
@@ -120,22 +172,28 @@ lldpd_remote_cleanup(struct lldpd_hardware *hardware,
             port != NULL;
             port = port_next) {
                port_next = TAILQ_NEXT(port, p_entries);
-               del = (expire == NULL);
-               if (expire &&
-                   (now - port->p_lastupdate >= port->p_chassis->c_ttl)) {
-                       hardware->h_ageout_cnt++;
-                       hardware->h_delete_cnt++;
-                       expire(hardware, port);
+               del = all;
+               if (!all && expire &&
+                   (now >= port->p_lastupdate + port->p_ttl)) {
+                       if (port->p_ttl > 0) hardware->h_ageout_cnt++;
                        del = 1;
                }
                if (del) {
-                       if (expire)
-                               TAILQ_REMOVE(&hardware->h_rports, port, p_entries);
+                       if (expire) expire(hardware, port);
+                       /* This TAILQ_REMOVE is dangerous. It should not be
+                        * called while in liblldpctl because we don't have a
+                        * real list. It is only needed to be called when we
+                        * don't delete the entire list. */
+                       if (!all) TAILQ_REMOVE(&hardware->h_rports, port, p_entries);
+
+                       hardware->h_delete_cnt++;
+                       /* Register last removal to be able to report lldpStatsRemTablesLastChangeTime */
+                       hardware->h_lport.p_lastremove = time(NULL);
                        lldpd_port_cleanup(port, 1);
                        free(port);
                }
        }
-       if (!expire) TAILQ_INIT(&hardware->h_rports);
+       if (all) TAILQ_INIT(&hardware->h_rports);
 }
 
 /* If `all' is true, clear all information, including information that
@@ -154,14 +212,21 @@ lldpd_port_cleanup(struct lldpd_port *port, int all)
        lldpd_ppvid_cleanup(port);
        lldpd_pi_cleanup(port);
 #endif
-       free(port->p_id);
-       free(port->p_descr);
+       /* will set these to NULL so we don't free wrong memory */
+
        if (all) {
+               free(port->p_id);
+               port->p_id = NULL;
+               free(port->p_descr);
+               port->p_descr = NULL;
                free(port->p_lastframe);
                if (port->p_chassis) { /* chassis may not have been attributed, yet */
                        port->p_chassis->c_refcount--;
                        port->p_chassis = NULL;
                }
+#ifdef ENABLE_CUSTOM
+               lldpd_custom_list_cleanup(port);
+#endif
        }
 }
 
@@ -171,7 +236,10 @@ lldpd_config_cleanup(struct lldpd_config *config)
        log_debug("alloc", "general configuration cleanup");
        free(config->c_mgmt_pattern);
        free(config->c_cid_pattern);
+       free(config->c_cid_string);
        free(config->c_iface_pattern);
+       free(config->c_perm_ifaces);
+       free(config->c_hostname);
        free(config->c_platform);
        free(config->c_description);
 }