]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
hints.add_hosts(path): a new property
authorVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 26 Jan 2017 15:02:41 +0000 (16:02 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Thu, 26 Jan 2017 15:07:22 +0000 (16:07 +0100)
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
modules/hints/hints.c

index 88c259dd65451142114a3d421fcf1d62c1cf3bfc..26486fbbc4d18db352e480db70774a6783a934f2 100644 (file)
@@ -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"``
index ff6e1f8adf753df2fc7692c9f26250691aed9de2..cbac5effb1675eb1dcea73988fa49abf9319edda 100644 (file)
 /* 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 }
        };