network: append DHCP message in json output
Also, get private options from sd_dhcp_message object if sd_dhcp_lease has it. With this change, networkctl will be able to use the serialized DHCP message object. Preparation for later changes.
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include "dhcp6-lease-internal.h"
|
||||
#include "extract-word.h"
|
||||
#include "in-addr-util.h"
|
||||
#include "iovec-util.h"
|
||||
#include "ip-protocol-list.h"
|
||||
#include "json-util.h"
|
||||
#include "netif-util.h"
|
||||
@@ -1352,12 +1353,20 @@ static int dhcp_client_lease_append_json(Link *link, sd_json_variant **v) {
|
||||
if (r < 0 && r != -ENODATA)
|
||||
return r;
|
||||
|
||||
_cleanup_(sd_json_variant_unrefp) sd_json_variant *m = NULL;
|
||||
if (link->dhcp_lease->message) {
|
||||
r = dhcp_message_build_json(link->dhcp_lease->message, &m);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
r = sd_json_buildo(
|
||||
&w,
|
||||
JSON_BUILD_PAIR_FINITE_USEC("LeaseTimestampUSec", lease_timestamp_usec),
|
||||
JSON_BUILD_PAIR_FINITE_USEC("Timeout1USec", t1),
|
||||
JSON_BUILD_PAIR_FINITE_USEC("Timeout2USec", t2),
|
||||
JSON_BUILD_PAIR_STRING_NON_EMPTY("Hostname", hostname));
|
||||
JSON_BUILD_PAIR_STRING_NON_EMPTY("Hostname", hostname),
|
||||
JSON_BUILD_PAIR_VARIANT_NON_NULL("Message", m));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -1419,14 +1428,35 @@ static int dhcp_client_private_options_append_json(Link *link, sd_json_variant *
|
||||
if (!link->dhcp_lease)
|
||||
return 0;
|
||||
|
||||
LIST_FOREACH(options, option, link->dhcp_lease->private_options) {
|
||||
if (!link->dhcp_lease->message) {
|
||||
LIST_FOREACH(options, option, link->dhcp_lease->private_options) {
|
||||
r = sd_json_variant_append_arraybo(
|
||||
&array,
|
||||
SD_JSON_BUILD_PAIR_UNSIGNED("Option", option->tag),
|
||||
SD_JSON_BUILD_PAIR_HEX("PrivateOptionData", option->data, option->length));
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
return json_variant_set_field_non_null(v, "PrivateOptions", array);
|
||||
}
|
||||
|
||||
for (uint8_t i = SD_DHCP_OPTION_PRIVATE_BASE; i <= SD_DHCP_OPTION_PRIVATE_LAST; i++) {
|
||||
_cleanup_(iovec_done) struct iovec iov = {};
|
||||
r = dhcp_message_get_option_alloc(link->dhcp_lease->message, i, &iov);
|
||||
if (r == -ENODATA)
|
||||
continue;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_json_variant_append_arraybo(
|
||||
&array,
|
||||
SD_JSON_BUILD_PAIR_UNSIGNED("Option", option->tag),
|
||||
SD_JSON_BUILD_PAIR_HEX("PrivateOptionData", option->data, option->length));
|
||||
SD_JSON_BUILD_PAIR_UNSIGNED("Option", i),
|
||||
SD_JSON_BUILD_PAIR_HEX("PrivateOptionData", iov.iov_base, iov.iov_len));
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
return json_variant_set_field_non_null(v, "PrivateOptions", array);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user