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
.. 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"``
/* 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;
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;
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)
}
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. */
{ &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 }
};