]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
target fetch policy setting from config structure.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Mon, 18 Jun 2007 15:24:14 +0000 (15:24 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Mon, 18 Jun 2007 15:24:14 +0000 (15:24 +0000)
git-svn-id: file:///svn/unbound/trunk@394 be551aaa-1e26-0410-a405-d3ace91eadb9

iterator/iter_utils.c
util/config_file.c
util/config_file.h

index 6a0234f48e183cf63cf42ff5b7fea71007056199..d728cf031fc379b08fb83c271cf7c45f695f21b7 100644 (file)
 #include "util/data/msgparse.h"
 #include "util/random.h"
 
+/** count number of integers in fetch policy string */
+static int
+fetch_count(const char* s)
+{
+       /* format ::= (sp num)+ sp      */
+       /* num ::= [-](0-9)+            */
+       /* sp ::= (space|tab)*          */
+       int num = 0;
+       while(*s) {
+               while(*s && isspace(*s))
+                       s++;
+               if(!*s) /* end of string */
+                       break;
+               if(*s == '-')
+                       s++;
+               if(!*s) /* only - not allowed */
+                       return 0;
+               if(!isdigit(*s)) /* bad character */
+                       return 0;
+               while(*s && isdigit(*s))
+                       s++;
+               num++;
+       }
+       return num;
+}
+
+/** fillup fetch policy array */
+static void
+fetch_fill(struct iter_env* ie, const char* str)
+{
+       char* s = (char*)str, *e;
+       int i;
+       for(i=0; i<ie->max_dependency_depth+1; i++) {
+               ie->target_fetch_policy[i] = strtol(s, &e, 10);
+               log_assert(s != e); /* parsed syntax already */
+               s = e;
+       }
+}
+
+/** Read config string that represents the target fetch policy */
+static int
+read_fetch_policy(struct iter_env* ie, const char* str)
+{
+       int count = fetch_count(str);
+       if(count < 1) {
+               log_err("Cannot parse target fetch policy: \"%s\"", str);
+               return 0;
+       }
+       ie->max_dependency_depth = count - 1;
+       ie->target_fetch_policy = (int*)calloc(
+               (size_t)ie->max_dependency_depth+1, sizeof(int));
+       if(!ie->target_fetch_policy) {
+               log_err("alloc fetch policy: out of memory");
+               return 0;
+       }
+       fetch_fill(ie, str);
+       return 1;
+}
+
 int 
 iter_apply_cfg(struct iter_env* iter_env, struct config_file* cfg)
 {
        int i;
        /* target fetch policy */
-       iter_env->max_dependency_depth = 4;
-       iter_env->target_fetch_policy = (int*)calloc(
-               (size_t)iter_env->max_dependency_depth+1, sizeof(int));
-       if(iter_env->max_dependency_depth >= 1)
-               iter_env->target_fetch_policy[1] = 3;
-       if(iter_env->max_dependency_depth >= 2)
-               iter_env->target_fetch_policy[2] = 1;
-       /* TODO read setting from config */
-       if(!iter_env->target_fetch_policy)
+       if(!read_fetch_policy(iter_env, cfg->target_fetch_policy))
                return 0;
        for(i=0; i<iter_env->max_dependency_depth+1; i++)
                verbose(VERB_DETAIL, "target fetch policy for level %d is %d",
index 7e7aa1c69613327f076a35df20f929db0c4f7d49..cf23a906e49c679947538a52a4947b42c5ebedb8 100644 (file)
@@ -94,6 +94,7 @@ config_create()
        if(!(cfg->directory = strdup("/etc/unbound"))) goto error_exit;
        if(!(cfg->logfile = strdup(""))) goto error_exit;
        if(!(cfg->pidfile = strdup("unbound.pid"))) goto error_exit;
+       if(!(cfg->target_fetch_policy = strdup("3 2 1 0 0"))) goto error_exit;
        cfg->fwd_port = UNBOUND_DNS_PORT;
        cfg->do_daemonize = 1;
        cfg->num_ifs = 0;
@@ -165,6 +166,7 @@ config_delete(struct config_file* cfg)
        free(cfg->directory);
        free(cfg->logfile);
        free(cfg->pidfile);
+       free(cfg->target_fetch_policy);
        if(cfg->ifs) {
                int i;
                for(i=0; i<cfg->num_ifs; i++)
index ecb12dc190f679980795cdf88ba5b551334bc0f4..866fd3cd42344f690c32498d218e398d6abdba50 100644 (file)
@@ -99,6 +99,9 @@ struct config_file {
        /** forwarder port */
        int fwd_port;
 
+       /** the target fetch policy for the iterator */
+       char* target_fetch_policy;
+
        /** number of interfaces to open. If 0 default all interfaces. */
        int num_ifs;
        /** interface description strings (IP addresses) */