summaryrefslogtreecommitdiff
path: root/src/network/networkd-json.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-11-14 15:04:24 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-11-25 22:35:35 +0900
commit6e66b981d5bfbdb87d61eecb7b9bb8404377a476 (patch)
treee5f705f7d2e501dfb8daba56c7b9d52dbceb0bf5 /src/network/networkd-json.c
parent972f17c8f617e75b9052eadb5f7616074660db36 (diff)
downloadsystemd-6e66b981d5bfbdb87d61eecb7b9bb8404377a476.tar.gz
network: json: append neighbor information
Diffstat (limited to 'src/network/networkd-json.c')
-rw-r--r--src/network/networkd-json.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/network/networkd-json.c b/src/network/networkd-json.c
index 4f7824f2dd..e93e49d270 100644
--- a/src/network/networkd-json.c
+++ b/src/network/networkd-json.c
@@ -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;