Add DHCPServer information to JSON output.
Adds a new JSON object called DHCPServer for each interface that has a DHCPServer configured. It has the following attributes: - PoolSize and PoolOffset from the configuration - List of offered leases - List of static leases from the configuration
This commit is contained in:
committed by
Yu Watanabe
parent
837773add4
commit
58fa558ae4
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <linux/nexthop.h>
|
||||
|
||||
#include "dhcp-server-internal.h"
|
||||
#include "dns-domain.h"
|
||||
#include "ip-protocol-list.h"
|
||||
#include "netif-util.h"
|
||||
@@ -1161,6 +1162,128 @@ finalize:
|
||||
return r;
|
||||
}
|
||||
|
||||
static int dhcp_server_offered_leases_build_json(Link *link, JsonVariant **ret) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *elements = NULL;
|
||||
int r;
|
||||
DHCPLease *lease;
|
||||
|
||||
assert(link);
|
||||
assert(ret);
|
||||
|
||||
if (!link->dhcp_server) {
|
||||
*ret = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (hashmap_isempty(link->dhcp_server->bound_leases_by_client_id)) {
|
||||
*ret = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
HASHMAP_FOREACH(lease, link->dhcp_server->bound_leases_by_client_id) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *e = NULL;
|
||||
struct in_addr address = { .s_addr = lease->address };
|
||||
|
||||
r = json_build(&e,
|
||||
JSON_BUILD_OBJECT(
|
||||
JSON_BUILD_PAIR_BYTE_ARRAY(
|
||||
"ClientId",
|
||||
lease->client_id.data,
|
||||
lease->client_id.length),
|
||||
JSON_BUILD_PAIR_IN4_ADDR_NON_NULL("Address", &address),
|
||||
JSON_BUILD_PAIR_FINITE_USEC(
|
||||
"ExpirationUSec", lease->expiration)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = json_variant_append_array(&elements, e);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
return json_build(ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_VARIANT("Leases", elements)));
|
||||
}
|
||||
|
||||
static int dhcp_server_static_leases_build_json(Link *link, JsonVariant **ret) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *elements = NULL;
|
||||
int r;
|
||||
DHCPLease *lease;
|
||||
|
||||
assert(link);
|
||||
assert(ret);
|
||||
|
||||
if (!link->dhcp_server) {
|
||||
*ret = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (hashmap_isempty(link->dhcp_server->static_leases_by_client_id)) {
|
||||
*ret = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
HASHMAP_FOREACH(lease, link->dhcp_server->static_leases_by_client_id) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *e = NULL;
|
||||
struct in_addr address = { .s_addr = lease->address };
|
||||
|
||||
r = json_build(&e,
|
||||
JSON_BUILD_OBJECT(
|
||||
JSON_BUILD_PAIR_BYTE_ARRAY(
|
||||
"ClientId",
|
||||
lease->client_id.data,
|
||||
lease->client_id.length),
|
||||
JSON_BUILD_PAIR_IN4_ADDR_NON_NULL("Address", &address)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = json_variant_append_array(&elements, e);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
return json_build(ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_VARIANT("StaticLeases", elements)));
|
||||
}
|
||||
|
||||
static int dhcp_server_build_json(Link *link, JsonVariant **ret) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
|
||||
int r;
|
||||
|
||||
assert(link);
|
||||
assert(ret);
|
||||
|
||||
if (!link->dhcp_server) {
|
||||
*ret = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = json_build(&v,
|
||||
JSON_BUILD_OBJECT(
|
||||
JSON_BUILD_PAIR_UNSIGNED("PoolOffset", link->dhcp_server->pool_offset),
|
||||
JSON_BUILD_PAIR_UNSIGNED("PoolSize", link->dhcp_server->pool_size)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = dhcp_server_offered_leases_build_json(link, &w);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = json_variant_merge(&v, w);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
w = json_variant_unref(w);
|
||||
|
||||
r = dhcp_server_static_leases_build_json(link, &w);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = json_variant_merge(&v, w);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return json_build(ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_VARIANT("DHCPServer", v)));
|
||||
}
|
||||
|
||||
int link_build_json(Link *link, JsonVariant **ret) {
|
||||
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
|
||||
_cleanup_free_ char *type = NULL, *flags = NULL;
|
||||
@@ -1344,6 +1467,16 @@ int link_build_json(Link *link, JsonVariant **ret) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
w = json_variant_unref(w);
|
||||
|
||||
r = dhcp_server_build_json(link, &w);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = json_variant_merge(&v, w);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = TAKE_PTR(v);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user