From: Vladimír Čunát Date: Mon, 16 Jan 2017 13:16:42 +0000 (+0100) Subject: hints: do not load /etc/hosts by default X-Git-Tag: v1.2.0-rc1~19^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=487fc936ea38e1f04088a91c4201079ce5355fc3;p=thirdparty%2Fknot-resolver.git hints: do not load /etc/hosts by default And don't crash anymore if loaded without configuring. Users can now avoid loading any file and instead specify hints just via hints.set() calls. It's perhaps still strange that hints.config(...) will drop any hints set previously. --- diff --git a/NEWS b/NEWS index 5ba6ed8eb..d666becb1 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,8 @@ Knot Resolver 1.2.0 (2016-12-yy) - Support CD flag on incoming requests. - It now requires knot >= 2.3.1 to link successfully. - The API+ABI for modules changed slightly. +- hints module: previously /etc/hosts was loaded by default, but not anymore. + Users can now actually avoid loading any file. Knot Resolver 1.1.1 (2016-08-24) ================================ diff --git a/modules/hints/README.rst b/modules/hints/README.rst index 71f2e8eeb..88c259dd6 100644 --- a/modules/hints/README.rst +++ b/modules/hints/README.rst @@ -28,10 +28,11 @@ Properties .. function:: hints.config([path]) - :param string path: path to hosts file, default: ``"/etc/hosts"`` + :param string path: path to hosts file, default: no file :return: ``{ result: bool }`` - Load specified hosts file. + Clear any configured hints and load specified hosts file. + (Root hints are not touched.) .. function:: hints.get(hostname) diff --git a/modules/hints/hints.c b/modules/hints/hints.c index c1042adb3..0b9091d8f 100644 --- a/modules/hints/hints.c +++ b/modules/hints/hints.c @@ -34,7 +34,6 @@ #include "lib/layer.h" /* Defaults */ -#define DEFAULT_FILE "/etc/hosts" #define VERBOSE_MSG(qry, fmt...) QRVERBOSE(qry, "hint", fmt) /* Structure for reverse search (address to domain) */ @@ -301,7 +300,7 @@ static int load_map(struct kr_zonecut *hints, FILE *fp) return kr_ok(); } -static int load(struct kr_module *module, const char *path) +static int load_file(struct kr_module *module, const char *path) { auto_fclose FILE *fp = fopen(path, "r"); if (fp == NULL) { @@ -311,21 +310,8 @@ static int load(struct kr_module *module, const char *path) VERBOSE_MSG(NULL, "reading '%s'\n", path); } - /* Create pool and copy itself */ - knot_mm_t _pool = { - .ctx = mp_new(4096), - .alloc = (knot_mm_alloc_t) mp_alloc - }; - knot_mm_t *pool = mm_alloc(&_pool, sizeof(*pool)); - if (!pool) { - return kr_error(ENOMEM); - } - memcpy(pool, &_pool, sizeof(*pool)); - /* Load file to map */ - struct kr_zonecut *hints = mm_alloc(pool, sizeof(*hints)); - kr_zonecut_init(hints, (const uint8_t *)(""), pool); - module->data = hints; + struct kr_zonecut *hints = module->data; return load_map(hints, fp); } @@ -349,6 +335,8 @@ static void unload(struct kr_module *module) static char* hint_set(void *env, struct kr_module *module, const char *args) { struct kr_zonecut *hints = module->data; + if (!args) + return NULL; auto_free char *args_copy = strdup(args); int ret = -1; @@ -406,11 +394,15 @@ static char* pack_hints(struct kr_zonecut *hints); * Retrieve address hints, either for given name or for all names. * * Input: name - * Output: { address1, address2, ... } + * Output: NULL or "{ address1, address2, ... }" */ static char* hint_get(void *env, struct kr_module *module, const char *args) { struct kr_zonecut *hints = module->data; + if (!hints) { + assert(false); + return NULL; + } if (!args) { return pack_hints(hints); @@ -509,21 +501,51 @@ const kr_layer_api_t *hints_layer(struct kr_module *module) return &_layer; } + +/** Basic initialization: get a memory pool, etc. */ KR_EXPORT int hints_init(struct kr_module *module) { - module->data = NULL; - return 0; + /* Create pool and copy itself */ + knot_mm_t _pool = { + .ctx = mp_new(4096), + .alloc = (knot_mm_alloc_t) mp_alloc + }; + knot_mm_t *pool = mm_alloc(&_pool, sizeof(*pool)); + if (!pool) { + return kr_error(ENOMEM); + } + memcpy(pool, &_pool, sizeof(*pool)); + + struct kr_zonecut *hints = mm_alloc(pool, sizeof(*hints)); + if (!hints) { + mp_delete(pool->ctx); + return kr_error(ENOMEM); + } + hints->pool = pool; + kr_zonecut_init(hints, (const uint8_t *)(""), pool); + module->data = hints; + + return kr_ok(); } +/** Drop all hints, and load a hosts file if any was specified. + * + * It seems slightly strange to drop all, but keep doing that for now. + */ KR_EXPORT int hints_config(struct kr_module *module, const char *conf) { unload(module); - if (!conf || strlen(conf) < 1) { - conf = DEFAULT_FILE; + int err = hints_init(module); + if (err != kr_ok()) { + return err; } - return load(module, conf); + + if (conf && conf[0]) { + return load_file(module, conf); + } + return kr_ok(); } KR_EXPORT