]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: json: append neighbor information
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 14 Nov 2021 06:04:24 +0000 (15:04 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 25 Nov 2021 13:35:35 +0000 (22:35 +0900)
src/network/networkd-json.c

index 4f7824f2dd4843466547ffffdb097833ddd9a5b7..e93e49d2703e86203b8dc509a860844122bc1383 100644 (file)
@@ -7,6 +7,7 @@
 #include "networkd-json.h"
 #include "networkd-link.h"
 #include "networkd-manager.h"
+#include "networkd-neighbor.h"
 #include "networkd-nexthop.h"
 #include "networkd-network.h"
 #include "networkd-route-util.h"
@@ -88,6 +89,63 @@ finalize:
         return r;
 }
 
+static int neighbor_build_json(Neighbor *n, JsonVariant **ret) {
+        _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
+        _cleanup_free_ char *state = NULL;
+        int r;
+
+        assert(n);
+        assert(ret);
+
+        r = network_config_state_to_string_alloc(n->state, &state);
+        if (r < 0)
+                return r;
+
+        r = json_build(&v, JSON_BUILD_OBJECT(
+                                JSON_BUILD_PAIR_INTEGER("Family", n->family),
+                                JSON_BUILD_PAIR_IN_ADDR("Destination", &n->in_addr, n->family),
+                                JSON_BUILD_PAIR_HW_ADDR("LinkLayerAddress", &n->ll_addr),
+                                JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(n->source)),
+                                JSON_BUILD_PAIR_STRING("ConfigState", state)));
+        if (r < 0)
+                return r;
+
+        *ret = TAKE_PTR(v);
+        return 0;
+}
+
+static int neighbors_build_json(Set *neighbors, JsonVariant **ret) {
+        JsonVariant **elements;
+        Neighbor *neighbor;
+        size_t n = 0;
+        int r;
+
+        assert(ret);
+
+        if (set_isempty(neighbors)) {
+                *ret = NULL;
+                return 0;
+        }
+
+        elements = new(JsonVariant*, set_size(neighbors));
+        if (!elements)
+                return -ENOMEM;
+
+        SET_FOREACH(neighbor, neighbors) {
+                r = neighbor_build_json(neighbor, elements + n);
+                if (r < 0)
+                        goto finalize;
+                n++;
+        }
+
+        r = json_build(ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("Neighbors", JSON_BUILD_VARIANT_ARRAY(elements, n))));
+
+finalize:
+        json_variant_unref_many(elements, n);
+        free(elements);
+        return r;
+}
+
 static int nexthop_group_build_json(NextHop *nexthop, JsonVariant **ret) {
         JsonVariant **elements;
         struct nexthop_grp *g;
@@ -392,6 +450,16 @@ int link_build_json(Link *link, JsonVariant **ret) {
 
         w = json_variant_unref(w);
 
+        r = neighbors_build_json(link->neighbors, &w);
+        if (r < 0)
+                return r;
+
+        r = json_variant_merge(&v, w);
+        if (r < 0)
+                return r;
+
+        w = json_variant_unref(w);
+
         r = nexthops_build_json(link->nexthops, &w);
         if (r < 0)
                 return r;