1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-10 05:18:17 +03:00

sd-lldp-rx: serialize LLDP neighbors to JSON format

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.
This commit is contained in:
Tomáš Pecka 2021-10-07 11:16:57 +02:00 committed by Yu Watanabe
parent 1e18e1aa11
commit 329146a9ac
4 changed files with 58 additions and 0 deletions

View 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))));
}

View 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);

View 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: ", \

View 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);