From 49e0f1dc6e52e791f5e0ba21097aea17d5950d38 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Wed, 29 Aug 2018 16:23:28 +0200 Subject: JSON: Add metainfo object to all output Right now this object merely contains the nftables version and release name as well as a JSON schema version, but it could be extended arbitrarily. In the future, this will also allow for non-compatible schema changes should the need for this arise. Adjust the parser to accept metainfo objects and make it verify json_schema_version to be less than or equal to the one hard-coded in the library. Signed-off-by: Phil Sutter Signed-off-by: Pablo Neira Ayuso --- src/json.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/json.c') diff --git a/src/json.c b/src/json.c index 2a13dfe7..84bdaa39 100644 --- a/src/json.c +++ b/src/json.c @@ -1530,6 +1530,14 @@ static json_t *do_list_flowtables_json(struct netlink_ctx *ctx, struct cmd *cmd) return root; } +static json_t *generate_json_metainfo(void) +{ + return json_pack("{s: {s:s, s:s, s:i}}", "metainfo", + "version", PACKAGE_VERSION, + "release_name", RELEASE_NAME, + "json_schema_version", JSON_SCHEMA_VERSION); +} + int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd) { struct table *table = NULL; @@ -1596,10 +1604,15 @@ int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd) BUG("invalid command object type %u\n", cmd->obj); } - if (json_is_array(root) && !json_array_size(root)) { - json_decref(root); - root = json_null(); + if (!json_is_array(root)) { + json_t *tmp = json_array(); + + json_array_append_new(tmp, root); + root = tmp; } + + json_array_insert_new(root, 0, generate_json_metainfo()); + root = json_pack("{s:o}", "nftables", root); json_dumpf(root, ctx->octx->output_fp, 0); json_decref(root); -- cgit v1.2.3