]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
cancel().
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Thu, 24 Jan 2008 09:14:07 +0000 (09:14 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Thu, 24 Jan 2008 09:14:07 +0000 (09:14 +0000)
git-svn-id: file:///svn/unbound/trunk@891 be551aaa-1e26-0410-a405-d3ace91eadb9

doc/Changelog
libunbound/unbound.c
testcode/asynclook.c

index 3a243bc58fcabba837cf6dcc429827feaa46e4bc..9ee6cb6e95c95d6dc7e59221a52a9bd34fd8dd86 100644 (file)
@@ -1,3 +1,9 @@
+24 January 2008: Wouter
+       - tested the cancel() function.
+       - asynclook -c (cancel) feature.
+       - fix fail to allocate context actions.
+       - make pipe nonblocking at start.
+
 23 January 2008: Wouter
        - removed debug prints from if-auto, verb-algo enables some.
        - libunbound QUIT setup, remove memory leaks, when using threads
index 1bec20d179cd7937e718ee0ab07f52aa35c89cee..148c22737503ee01f7963a755f76b0c0f978b957 100644 (file)
@@ -52,6 +52,7 @@
 #include "util/module.h"
 #include "util/regional.h"
 #include "util/log.h"
+#include "util/net_help.h"
 #include "services/modstack.h"
 #include "services/localzone.h"
 #include "services/cache/infra.h"
@@ -82,18 +83,40 @@ ub_val_ctx_create()
                errno = e;
                return NULL;
        }
+       if(!fd_set_nonblock(ctx->rrpipe[0]) ||
+          !fd_set_nonblock(ctx->rrpipe[1]) ||
+          !fd_set_nonblock(ctx->qqpipe[0]) ||
+          !fd_set_nonblock(ctx->qqpipe[1])) {
+               int e = errno;
+               close(ctx->rrpipe[0]);
+               close(ctx->rrpipe[1]);
+               close(ctx->qqpipe[0]);
+               close(ctx->qqpipe[1]);
+               free(ctx);
+               errno = e;
+               return NULL;
+       }
        lock_basic_init(&ctx->qqpipe_lock);
        lock_basic_init(&ctx->rrpipe_lock);
        lock_basic_init(&ctx->cfglock);
        ctx->env = (struct module_env*)calloc(1, sizeof(*ctx->env));
        if(!ctx->env) {
-               ub_val_ctx_delete(ctx);
+               close(ctx->rrpipe[0]);
+               close(ctx->rrpipe[1]);
+               close(ctx->qqpipe[0]);
+               close(ctx->qqpipe[1]);
+               free(ctx);
                errno = ENOMEM;
                return NULL;
        }
        ctx->env->cfg = config_create_forlib();
        if(!ctx->env->cfg) {
-               ub_val_ctx_delete(ctx);
+               close(ctx->rrpipe[0]);
+               close(ctx->rrpipe[1]);
+               close(ctx->qqpipe[0]);
+               close(ctx->qqpipe[1]);
+               free(ctx->env);
+               free(ctx);
                errno = ENOMEM;
                return NULL;
        }
index 70b8f2dd5e1f43d6d3ac46f37b08e46131fbaef9..53714275a73ab6e03145607564282610774bbf64 100644 (file)
@@ -48,7 +48,7 @@
  */
 struct lookinfo {
        /** name to look up */
-       char* qname;
+       char* name;
        /** tracking number that can be used to cancel the query */
        int async_id;
        /** error code from libunbound */
@@ -65,8 +65,10 @@ void usage(char* argv[])
 {
        printf("usage: %s name ...\n", argv[0]);
        printf("names are looked up at the same time, asynchronously.\n");
+       printf("options only in this order, before any domain names.\n");
        printf("-d : enable debug output\n");
        printf("-t : use a resolver thread instead of forking a process\n");
+       printf("-c : cancel the requests\n");
        exit(1);
 }
 
@@ -75,7 +77,7 @@ void lookup_is_done(void* mydata, int err, struct ub_val_result* result)
 {
        /* cast mydata back to the correct type */
        struct lookinfo* info = (struct lookinfo*)mydata;
-       fprintf(stderr, "name %s resolved\n", info->qname);
+       fprintf(stderr, "name %s resolved\n", info->name);
        info->err = err;
        info->result = result;
        /* one less to wait for */
@@ -87,7 +89,7 @@ int main(int argc, char** argv)
 {
        struct ub_val_ctx* ctx;
        struct lookinfo* lookups;
-       int i, r;
+       int i, r, cancel=0;
        if(argc == 1) {
                usage(argv);
        }
@@ -112,6 +114,11 @@ int main(int argc, char** argv)
                argc--;
                argv++;
        }
+       if(argc > 0 && strcmp(argv[0], "-c") == 0) {
+               cancel=1;
+               argc--;
+               argv++;
+       }
 
        /* allocate array for results. */
        lookups = (struct lookinfo*)calloc((size_t)argc, 
@@ -125,16 +132,23 @@ int main(int argc, char** argv)
        num_wait = argc;
        for(i=0; i<argc; i++) {
                fprintf(stderr, "start lookup %s\n", argv[i]);
-               lookups[i].qname = argv[i];
+               lookups[i].name = argv[i];
                r = ub_val_resolve_async(ctx, argv[i], LDNS_RR_TYPE_A,
                        LDNS_RR_CLASS_IN, &lookups[i], &lookup_is_done, 
                        &lookups[i].async_id);
        }
+       if(cancel) {
+               for(i=0; i<argc; i++) {
+                       fprintf(stderr, "cancel %s\n", argv[i]);
+                       ub_val_cancel(ctx, lookups[i].async_id);
+               }
+               num_wait = 0;
+       }
 
        /* wait while the hostnames are looked up. Do something useful here */
        for(i=0; i<1000; i++) {
                usleep(100000);
-               printf("%g seconds passed\n", 0.1*(double)i);
+               fprintf(stderr, "%g seconds passed\n", 0.1*(double)i);
                r = ub_val_ctx_process(ctx);
                if(r != 0) {
                        printf("ub_val_ctx_process error: %s\n",
@@ -154,21 +168,23 @@ int main(int argc, char** argv)
        for(i=0; i<argc; i++) {
                char buf[100];
                if(lookups[i].err) /* error (from libunbound) */
-                       printf("%s: error %s\n", lookups[i].qname,
+                       printf("%s: error %s\n", lookups[i].name,
                                ub_val_strerror(lookups[i].err));
+               else if(!lookups[i].result)
+                       printf("%s: cancelled\n", lookups[i].name);
                else if(lookups[i].result->havedata)
-                       printf("%s: %s\n", lookups[i].qname,
+                       printf("%s: %s\n", lookups[i].name,
                                inet_ntop(AF_INET, lookups[i].result->data[0],
                                buf, (socklen_t)sizeof(buf)));
                else {
                        /* there is no data, why that? */
                        if(lookups[i].result->rcode == 0 /*noerror*/ ||
                                lookups[i].result->nxdomain)
-                               printf("%s: no data %s\n", lookups[i].qname,
+                               printf("%s: no data %s\n", lookups[i].name,
                                lookups[i].result->nxdomain?"(no such host)":
                                "(no IP4 address)");
                        else    /* some error (from the server) */
-                               printf("%s: DNS error %d\n", lookups[i].qname,
+                               printf("%s: DNS error %d\n", lookups[i].name,
                                        lookups[i].result->rcode);
                }
        }