]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-lldp-rx: serialize LLDP neighbors to JSON format
authorTomáš Pecka <tomas.pecka@cesnet.cz>
Thu, 7 Oct 2021 09:16:57 +0000 (11:16 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 1 Mar 2024 00:40:26 +0000 (09:40 +0900)
Add functions serializing LLDP neighbors to JSON (JsonVariant).

The entry contains a chassis id, system name and port id of the remote
neighbor. Also it possibly contains an integer coding the enabled system
capabilities and port description.

src/libsystemd-network/lldp-neighbor.c
src/libsystemd-network/lldp-neighbor.h
src/libsystemd-network/lldp-rx-internal.h
src/libsystemd-network/sd-lldp-rx.c

index 2cac77f4728fad9631925a9b08619c65012603fd..a296ac6c33fc6a643a0b569471b3812db2692a95 100644 (file)
@@ -793,3 +793,31 @@ int sd_lldp_neighbor_get_timestamp(sd_lldp_neighbor *n, clockid_t clock, uint64_
         *ret = triple_timestamp_by_clock(&n->timestamp, clock);
         return 0;
 }
+
+int lldp_neighbor_build_json(sd_lldp_neighbor *n, JsonVariant **ret) {
+        const char *chassis_id = NULL, *port_id = NULL, *port_description = NULL,
+                *system_name = NULL, *system_description = NULL;
+        uint16_t cc = 0;
+        bool valid_cc;
+
+        assert(n);
+        assert(ret);
+
+        (void) sd_lldp_neighbor_get_chassis_id_as_string(n, &chassis_id);
+        (void) sd_lldp_neighbor_get_port_id_as_string(n, &port_id);
+        (void) sd_lldp_neighbor_get_port_description(n, &port_description);
+        (void) sd_lldp_neighbor_get_system_name(n, &system_name);
+        (void) sd_lldp_neighbor_get_system_description(n, &system_description);
+
+        valid_cc = sd_lldp_neighbor_get_enabled_capabilities(n, &cc);
+
+        return json_build(ret, JSON_BUILD_OBJECT(
+                                JSON_BUILD_PAIR_STRING_NON_EMPTY("ChassisID", chassis_id),
+                                JSON_BUILD_PAIR_BYTE_ARRAY("RawChassisID", n->id.chassis_id, n->id.chassis_id_size),
+                                JSON_BUILD_PAIR_STRING_NON_EMPTY("PortID", port_id),
+                                JSON_BUILD_PAIR_BYTE_ARRAY("RawPortID", n->id.port_id, n->id.port_id_size),
+                                JSON_BUILD_PAIR_STRING_NON_EMPTY("PortDescription", port_description),
+                                JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemName", system_name),
+                                JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemDescription", system_description),
+                                JSON_BUILD_PAIR_CONDITION(valid_cc, "EnabledCapabilities", JSON_BUILD_UNSIGNED(cc))));
+}
index 016286b17d8710597bd5fbd53b860a475ccf472a..06ba4c74603034fa61cfba49802f732284ca833b 100644 (file)
@@ -8,6 +8,7 @@
 #include "sd-lldp-rx.h"
 
 #include "hash-funcs.h"
+#include "json.h"
 #include "lldp-rx-internal.h"
 #include "time-util.h"
 
@@ -90,3 +91,4 @@ sd_lldp_neighbor *lldp_neighbor_new(size_t raw_size);
 int lldp_neighbor_parse(sd_lldp_neighbor *n);
 void lldp_neighbor_start_ttl(sd_lldp_neighbor *n);
 bool lldp_neighbor_equal(const sd_lldp_neighbor *a, const sd_lldp_neighbor *b);
+int lldp_neighbor_build_json(sd_lldp_neighbor *n, JsonVariant **ret);
index 83d0bc460da018ddc0255734287069d9590c38ac..e914c6bfc62fd6dccb665ba62e6d66606473e520 100644 (file)
@@ -5,6 +5,7 @@
 #include "sd-lldp-rx.h"
 
 #include "hashmap.h"
+#include "json.h"
 #include "network-common.h"
 #include "prioq.h"
 
@@ -36,6 +37,8 @@ struct sd_lldp_rx {
 const char* lldp_rx_event_to_string(sd_lldp_rx_event_t e) _const_;
 sd_lldp_rx_event_t lldp_rx_event_from_string(const char *s) _pure_;
 
+int lldp_rx_build_neighbors_json(sd_lldp_rx *lldp_rx, JsonVariant **ret);
+
 #define log_lldp_rx_errno(lldp_rx, error, fmt, ...)     \
         log_interface_prefix_full_errno(                \
                 "LLDP Rx: ",                            \
index 2fc9a55323ef45fc69a38fcd5cda5783ea3ed6be..74000ffb8b52cbf3d93fbcbe88a95f1e605c5786 100644 (file)
@@ -10,6 +10,7 @@
 #include "ether-addr-util.h"
 #include "event-util.h"
 #include "fd-util.h"
+#include "json.h"
 #include "lldp-neighbor.h"
 #include "lldp-network.h"
 #include "lldp-rx-internal.h"
@@ -490,6 +491,30 @@ int sd_lldp_rx_get_neighbors(sd_lldp_rx *lldp_rx, sd_lldp_neighbor ***ret) {
         return k;
 }
 
+int lldp_rx_build_neighbors_json(sd_lldp_rx *lldp_rx, JsonVariant **ret) {
+        _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
+        int r;
+
+        assert(lldp_rx);
+        assert(ret);
+
+        sd_lldp_neighbor *n;
+        HASHMAP_FOREACH(n, lldp_rx->neighbor_by_id) {
+                _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
+
+                r = lldp_neighbor_build_json(n, &w);
+                if (r < 0)
+                        return r;
+
+                r = json_variant_append_array(&v, w);
+                if (r < 0)
+                        return r;
+        }
+
+        *ret = TAKE_PTR(v);
+        return 0;
+}
+
 int sd_lldp_rx_set_neighbors_max(sd_lldp_rx *lldp_rx, uint64_t m) {
         assert_return(lldp_rx, -EINVAL);
         assert_return(m > 0, -EINVAL);