From 0e75b5495117b9888b7a557fab1db40145a9a705 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 5 Nov 2022 19:17:17 +0100 Subject: Adding upstream version 1.2. Signed-off-by: Daniel Baumann --- src/nvme/json.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) (limited to 'src/nvme/json.c') diff --git a/src/nvme/json.c b/src/nvme/json.c index b42cd51..f0c2ab4 100644 --- a/src/nvme/json.c +++ b/src/nvme/json.c @@ -95,6 +95,9 @@ static void json_parse_port(nvme_subsystem_t s, struct json_object *port_obj) if (!c) return; json_update_attributes(c, port_obj); + attr_obj = json_object_object_get(port_obj, "dhchap_key"); + if (attr_obj) + nvme_ctrl_set_dhchap_host_key(c, json_object_get_string(attr_obj)); attr_obj = json_object_object_get(port_obj, "dhchap_ctrl_key"); if (attr_obj) nvme_ctrl_set_dhchap_key(c, json_object_get_string(attr_obj)); @@ -155,6 +158,43 @@ static void json_parse_host(nvme_root_t r, struct json_object *host_obj) } } +static struct json_object *parse_json(nvme_root_t r, int fd) +{ + char buf[JSON_FILE_BUF_SIZE]; + struct json_object *obj = NULL; + struct printbuf *pb; + json_tokener *tok = NULL; + int ret; + + pb = printbuf_new(); + if (!pb) + return NULL; + + while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) + printbuf_memappend(pb, buf, ret); + + if (ret < 0) + goto out; + + tok = json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH); + if (!tok) + goto out; + + /* Enforce correctly formatted JSON */ + tok->flags = JSON_TOKENER_STRICT; + + obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb)); + if (!obj) + nvme_msg(r, LOG_DEBUG, "JSON parsing failed: %s\n", + json_util_get_last_err()); +out: + if (tok) + json_tokener_free(tok); + printbuf_free(pb); + + return obj; +} + int json_read_config(nvme_root_t r, const char *config_file) { struct json_object *json_root, *host_obj; @@ -166,12 +206,16 @@ int json_read_config(nvme_root_t r, const char *config_file) config_file, strerror(errno)); return fd; } - json_root = json_object_from_fd(fd); + json_root = parse_json(r, fd); + close(fd); if (!json_root) { - nvme_msg(r, LOG_DEBUG, "Failed to read %s, %s\n", - config_file, json_util_get_last_err()); errno = EPROTO; - close(fd); + return -1; + } + if (!json_object_is_type(json_root, json_type_array)) { + nvme_msg(r, LOG_DEBUG, "Wrong format, expected array\n"); + json_object_put(json_root); + errno = EPROTO; return -1; } for (h = 0; h < json_object_array_length(json_root); h++) { @@ -180,7 +224,6 @@ int json_read_config(nvme_root_t r, const char *config_file) json_parse_host(r, host_obj); } json_object_put(json_root); - close(fd); return 0; } @@ -222,6 +265,10 @@ static void json_update_port(struct json_object *ctrl_array, nvme_ctrl_t c) if (value) json_object_object_add(port_obj, "trsvcid", json_object_new_string(value)); + value = nvme_ctrl_get_dhchap_host_key(c); + if (value) + json_object_object_add(port_obj, "dhchap_key", + json_object_new_string(value)); value = nvme_ctrl_get_dhchap_key(c); if (value) json_object_object_add(port_obj, "dhchap_ctrl_key", @@ -365,6 +412,10 @@ static void json_dump_ctrl(struct json_object *ctrl_array, nvme_ctrl_t c) if (value) json_object_object_add(ctrl_obj, "trsvcid", json_object_new_string(value)); + value = nvme_ctrl_get_dhchap_host_key(c); + if (value) + json_object_object_add(ctrl_obj, "dhchap_key", + json_object_new_string(value)); value = nvme_ctrl_get_dhchap_key(c); if (value) json_object_object_add(ctrl_obj, "dhchap_ctrl_key", -- cgit v1.2.3