From: Yu Watanabe Date: Sun, 15 Jun 2025 01:04:37 +0000 (+0900) Subject: sd-lldp-tx: introduce sd_lldp_tx_describe() to dump current settings X-Git-Tag: v258-rc1~296^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b6f96f635fd7b650465af80566bd614fc87792a8;p=thirdparty%2Fsystemd.git sd-lldp-tx: introduce sd_lldp_tx_describe() to dump current settings --- diff --git a/src/libsystemd-network/sd-lldp-tx.c b/src/libsystemd-network/sd-lldp-tx.c index 4c6f263f8d0..9b86558ebaa 100644 --- a/src/libsystemd-network/sd-lldp-tx.c +++ b/src/libsystemd-network/sd-lldp-tx.c @@ -10,6 +10,7 @@ #include "fd-util.h" #include "hostname-setup.h" #include "hostname-util.h" +#include "json-util.h" #include "network-common.h" #include "random-util.h" #include "socket-util.h" @@ -368,7 +369,7 @@ static int lldp_tx_create_packet(sd_lldp_tx *lldp_tx, size_t *ret_packet_size, u assert(ret_packet); /* If ifname is not set yet, set ifname from ifindex. */ - r = sd_lldp_tx_get_ifname(lldp_tx, NULL); + r = sd_lldp_tx_get_ifname(lldp_tx, /* ret = */ NULL); if (r < 0) return r; @@ -666,3 +667,60 @@ int sd_lldp_tx_start(sd_lldp_tx *lldp_tx) { return 0; } + +int sd_lldp_tx_describe(sd_lldp_tx *lldp_tx, sd_json_variant **ret) { + int r; + + assert(lldp_tx); + assert(ret); + + /* If ifname is not set yet, set ifname from ifindex. */ + const char *ifname; + r = sd_lldp_tx_get_ifname(lldp_tx, &ifname); + if (r < 0) + return r; + + size_t port_id_len = strlen(ifname) + 1; /* +1 for subtype */ + _cleanup_free_ uint8_t *port_id = new(uint8_t, port_id_len + 1); /* +1 for unused zero suffix for safety */ + if (!port_id) + return -ENOMEM; + + port_id[0] = SD_LLDP_PORT_SUBTYPE_INTERFACE_NAME; + memcpy(port_id + 1, ifname, port_id_len); /* also copy the final NUL for safety */ + assert(port_id[port_id_len] == 0); + + sd_id128_t machine_id; + r = lldp_tx_get_machine_id(&machine_id); + if (r < 0) + return r; + + size_t chassis_id_len = (SD_ID128_STRING_MAX - 1) + 1; + _cleanup_free_ uint8_t *chassis_id = new(uint8_t, chassis_id_len + 1); + if (!chassis_id) + return -ENOMEM; + + chassis_id[0] = SD_LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED; + memcpy(chassis_id + 1, SD_ID128_TO_STRING(machine_id), chassis_id_len); + assert(chassis_id[chassis_id_len] == 0); + + _cleanup_free_ char *hostname = NULL; + if (!lldp_tx->hostname) + (void) gethostname_strict(&hostname); + + _cleanup_free_ char *pretty_hostname = NULL; + if (!lldp_tx->pretty_hostname) + (void) get_pretty_hostname(&pretty_hostname); + + return sd_json_buildo( + ret, + SD_JSON_BUILD_PAIR_STRING("ChassisID", SD_ID128_TO_STRING(machine_id)), + SD_JSON_BUILD_PAIR_BYTE_ARRAY("RawChassisID", chassis_id, chassis_id_len), + SD_JSON_BUILD_PAIR_STRING("PortID", lldp_tx->ifname), + SD_JSON_BUILD_PAIR_BYTE_ARRAY("RawPortID", port_id, port_id_len), + JSON_BUILD_PAIR_STRING_NON_EMPTY("PortDescription", lldp_tx->port_description), + JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemName", lldp_tx->hostname ?: hostname), + JSON_BUILD_PAIR_STRING_NON_EMPTY("SystemDescription", lldp_tx->pretty_hostname ?: pretty_hostname), + SD_JSON_BUILD_PAIR_UNSIGNED("EnabledCapabilities", lldp_tx->enabled_capabilities), + JSON_BUILD_PAIR_STRING_NON_EMPTY("MUDURL", lldp_tx->mud_url), + JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("VlanID", lldp_tx->vlan_id)); +} diff --git a/src/systemd/sd-lldp-tx.h b/src/systemd/sd-lldp-tx.h index 4d92ca5ebed..3dfb481b555 100644 --- a/src/systemd/sd-lldp-tx.h +++ b/src/systemd/sd-lldp-tx.h @@ -18,6 +18,7 @@ ***/ #include "_sd-common.h" +#include "sd-json.h" #include "sd-lldp.h" /* IWYU pragma: export*/ _SD_BEGIN_DECLARATIONS; @@ -60,6 +61,8 @@ int sd_lldp_tx_set_capabilities(sd_lldp_tx *lldp_tx, uint16_t supported, uint16_ int sd_lldp_tx_set_mud_url(sd_lldp_tx *lldp_tx, const char *mud_url); int sd_lldp_tx_set_vlan_id(sd_lldp_tx *lldp_tx, uint16_t vlan_id); +int sd_lldp_tx_describe(sd_lldp_tx *lldp_tx, sd_json_variant **ret); + _SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_tx, sd_lldp_tx_unref); _SD_END_DECLARATIONS;