]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Add DHCPServer information to JSON output.
authorRene Hollander <mail@renehollander.at>
Sun, 30 Apr 2023 11:31:28 +0000 (13:31 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 26 May 2023 22:58:12 +0000 (07:58 +0900)
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

src/network/networkd-json.c

index c37f734d619428abcf3257dc7c178284874b2e95..0aeab985aa9af03096e4a119b11cc8590fac9d16 100644 (file)
@@ -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;
 }