From 920ecd7bd6f5fd55bbd9dc1c5f5cc062eaa0fcca Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Thu, 26 Jan 2017 16:02:41 +0100 Subject: [PATCH] hints.add_hosts(path): a new property So far it wasn't possible to load multiple files (!). Real use case: https://forum.turris.cz/t/how-to-configure-local-address-dns-resoultion-on-omnia/1000/14 --- modules/hints/README.rst | 16 ++++++++++++---- modules/hints/hints.c | 28 ++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/modules/hints/README.rst b/modules/hints/README.rst index 88c259dd6..26486fbbc 100644 --- a/modules/hints/README.rst +++ b/modules/hints/README.rst @@ -3,8 +3,10 @@ Static hints ------------ -This is a module providing static hints from ``/etc/hosts`` like file for forward records (A/AAAA) and reverse records (PTR). -You can also use it to change root hints that are used as a safety belt, or if the root NS +This is a module providing static hints for forward records (A/AAAA) and reverse records (PTR). +The records can be loaded from ``/etc/hosts``-like files and/or added directly. + +You can also use the module to change root hints that are used as a safety belt, or if the root NS drops out of cache. Examples @@ -28,12 +30,18 @@ Properties .. function:: hints.config([path]) - :param string path: path to hosts file, default: no file + :param string path: path to hosts-like file, default: no file :return: ``{ result: bool }`` - Clear any configured hints and load specified hosts file. + Clear any configured hints, and optionally load a hosts-like file as in ``hints.add_hosts(path)``. (Root hints are not touched.) +.. function:: hints.add_hosts([path]) + + :param string path: path to hosts-like file, default: `/etc/hosts` + + Add hints from a host-like file. + .. function:: hints.get(hostname) :param string hostname: i.e. ``"localhost"`` diff --git a/modules/hints/hints.c b/modules/hints/hints.c index ff6e1f8ad..cbac5effb 100644 --- a/modules/hints/hints.c +++ b/modules/hints/hints.c @@ -36,6 +36,15 @@ /* Defaults */ #define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "hint", fmt) +/** Useful for returning from module properties. */ +static char * bool2jsonstr(bool val) +{ + char *result = NULL; + if (-1 == asprintf(&result, "{ \"result\": %s }", val ? "true" : "false")) + result = NULL; + return result; +} + /* Structure for reverse search (address to domain) */ struct rev_search_baton { knot_pkt_t *pkt; @@ -315,6 +324,14 @@ static int load_file(struct kr_module *module, const char *path) return load_map(hints, fp); } +static char* hint_add_hosts(void *env, struct kr_module *module, const char *args) +{ + if (!args) + args = "/etc/hosts"; + int err = load_file(module, args); + return bool2jsonstr(err == kr_ok()); +} + static void unload(struct kr_module *module) { struct kr_zonecut *hints = module->data; @@ -348,10 +365,7 @@ static char* hint_set(void *env, struct kr_module *module, const char *args) ret = add_pair(hints, args_copy, addr + 1); } - char *result = NULL; - if (-1 == asprintf(&result, "{ \"result\": %s }", ret == 0 ? "true" : "false")) - result = NULL; - return result; + return bool2jsonstr(ret == 0); } static char* hint_del(void *env, struct kr_module *module, const char *args) @@ -371,10 +385,7 @@ static char* hint_del(void *env, struct kr_module *module, const char *args) } ret = del_pair(hints, args_copy, addr); - char *result = NULL; - if (-1 == asprintf(&result, "{ \"result\": %s }", ret == 0 ? "true" : "false")) - result = NULL; - return result; + return bool2jsonstr(ret == 0); } /** @internal Pack address list into JSON array. */ @@ -568,6 +579,7 @@ struct kr_prop *hints_props(void) { &hint_set, "set", "Set {name, address} hint.", }, { &hint_del, "del", "Delete one {name, address} hint or all addresses for the name.", }, { &hint_get, "get", "Retrieve hint for given name.", }, + { &hint_add_hosts, "add_hosts", "Load a file with hosts-like formatting and add contents into hints.", }, { &hint_root, "root", "Replace root hints set (empty value to return current list).", }, { NULL, NULL, NULL } }; -- 2.47.2