From: Marek VavruĊĦa Date: Sun, 24 May 2015 21:04:21 +0000 (+0200) Subject: modules/hints: use json arrays for output X-Git-Tag: v1.0.0-beta1~142 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e9f3ea510713b808db0b5260f8556d3749fab04f;p=thirdparty%2Fknot-resolver.git modules/hints: use json arrays for output --- diff --git a/modules/hints/hints.c b/modules/hints/hints.c index 31ea6a0a9..563131ab3 100644 --- a/modules/hints/hints.c +++ b/modules/hints/hints.c @@ -21,6 +21,7 @@ * The module provides an override for queried address records. */ +#include #include #include #include @@ -215,7 +216,7 @@ static char* hint_set(void *env, struct kr_module *module, const char *args) } char *result = NULL; - asprintf(&result, "{ \"result\": %s }", ret == 0 ? "true" : "false"); + asprintf(&result, "{ \"result\": %s", ret == 0 ? "true" : "false"); return result; } @@ -230,7 +231,6 @@ static char* hint_get(void *env, struct kr_module *module, const char *args) struct kr_zonecut *hints = module->data; knot_dname_t key[KNOT_DNAME_MAXLEN]; pack_t *pack = NULL; - size_t bufsize = 4096; if (knot_dname_from_str(key, args, sizeof(key))) { pack = kr_zonecut_find(hints, key); } @@ -238,28 +238,21 @@ static char* hint_get(void *env, struct kr_module *module, const char *args) return NULL; } - auto_free char *hint_buf = malloc(bufsize); - if (hint_buf == NULL) { - return NULL; - } - char *p = hint_buf, *endp = hint_buf + bufsize; + char buf[SOCKADDR_STRLEN]; + JsonNode *root = json_mkarray(); uint8_t *addr = pack_head(*pack); while (addr != pack_tail(*pack)) { size_t len = pack_obj_len(addr); int family = len == sizeof(struct in_addr) ? AF_INET : AF_INET6; - if (!inet_ntop(family, pack_obj_val(addr), p, endp - p)) { + if (!inet_ntop(family, pack_obj_val(addr), buf, sizeof(buf))) { break; } - p += strlen(p); + json_append_element(root, json_mkstring(buf)); addr = pack_obj_next(addr); - if (p + 2 < endp && addr != pack_tail(*pack)) { - strcpy(p, " "); - p += 1; - } } - char *result = NULL; - asprintf(&result, "{ \"result\": [ %s ] }", hint_buf); + char *result = json_encode(root); + json_delete(root); return result; } diff --git a/modules/hints/hints.mk b/modules/hints/hints.mk index b3eb0b8a0..9d1dec1e2 100644 --- a/modules/hints/hints.mk +++ b/modules/hints/hints.mk @@ -1,4 +1,4 @@ -hints_SOURCES := modules/hints/hints.c +hints_SOURCES := modules/hints/hints.c contrib/ccan/json/json.c hints_DEPEND := $(libkresolve) hints_LIBS := $(libkresolve_TARGET) $(libkresolve_LIBS) $(call make_c_module,hints) \ No newline at end of file