From b92528a0a95714d8b738e619de5a1d80f2fbd413 Mon Sep 17 00:00:00 2001 From: ian Date: Sun, 19 Dec 2004 19:11:28 +0000 Subject: [PATCH] default resolvers --- adns/adns.c | 122 +++++++++++++++++++++++++++++---------- base/tables-examples.tct | 2 + 2 files changed, 93 insertions(+), 31 deletions(-) diff --git a/adns/adns.c b/adns/adns.c index d8b6e1c..e768cd6 100644 --- a/adns/adns.c +++ b/adns/adns.c @@ -50,7 +50,12 @@ * -reverse-any ZONE-A-LIKE * -config CONFIG-STRING * + * adns set-default-resolver RESOLVER + * cancels any outstanding queries from a previous anonymous + * default resolver + * * adns destroy-resolver RESOLVER + * cancels outstanding queries */ #include "tables.h" @@ -68,6 +73,8 @@ static void asynch_perturbed(Resolver *res); static void asynch_query_dispose(Tcl_Interp *interp, Query *query); +#define ASSOC_DEFAULTRES "adns-defaultresolver" + /*---------- common resolver/query option processing ----------*/ typedef struct { @@ -93,7 +100,6 @@ struct OptionInfo { }; enum { - oisf_makedefault= 0x0001, oisf_reverse= 0x0002 }; @@ -117,6 +123,13 @@ static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi, #define OIFS(f) { "-" #f, oifn_fs, 0, oisf_##f, 0 } #define OICA(o) { "-" #o, oifn_##o, 1 } +static void optparse_blank(OptionParse *op) { + memset(op,0,sizeof(*op)); + op->errfile= 0; + op->errcallback= 0; + op->config_string= 0; +} + static int parse_options(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv, const OptionInfo opttable[], OptionParse *op) { const OptionInfo *oi; @@ -156,8 +169,16 @@ struct Resolver { Tcl_TimerToken timertoken; int maxfd; fd_set handling[3]; + ScriptToInvoke errcallback; }; +/* The default resolver is recorded using Tcl_SetAssocData with key + * ASSOC_DEFAULTRES to record the Resolver*. If it was explicitly + * created with `adns new-resolver' then ix will be >=0, and the + * resolver will be destroyed - leaving no default - when explicitly + * requested. If it was implicitly created (by starting a query when + * there is no default) then ix will be -1. */ + static int oifn_errfile(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg, OptionParse *op) { int rc; @@ -193,7 +214,6 @@ static const OptionInfo resolver_optioninfos[]= { OIFA1(if,noerrprint, adns_if_debug), OIFA2(if,checkc,entex), OIFA2(if,checkc,freq), - OIFS(makedefault), OICA(errfile), OICA(errcallback), OICA(config), @@ -205,10 +225,17 @@ static void adnslogfn_callback(adns_state ads, void *logfndata, abort(); /* fixme implement adns_logcallbackfn */ } +static Resolver *default_resolver(Tcl_Interp *ip) { + return Tcl_GetAssocData(ip,ASSOC_DEFAULTRES,0); +} + static void destroy_resolver(Tcl_Interp *ip, Resolver *res) { void *query_v; adns_query aqu; - + + if (res == default_resolver(ip)) + Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES); + if (res->ads) { /* although adns would throw these away for us, we need to * destroy our own data too and only adns has a list of them */ @@ -222,12 +249,14 @@ static void destroy_resolver(Tcl_Interp *ip, Resolver *res) { } asynch_cancelhandlers(res); TFREE(res); - /* fixme what about the default resolver */ } static void destroy_resolver_idtabcb(Tcl_Interp *ip, void *resolver_v) { destroy_resolver(ip,resolver_v); } +static void destroy_resolver_defcb(ClientData resolver_v, Tcl_Interp *ip) { + destroy_resolver(ip,resolver_v); +} int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) { destroy_resolver(ip,res_v); @@ -235,44 +264,32 @@ int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) { return TCL_OK; } -int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip, - int objc, Tcl_Obj *const *objv, - void **result) { - OptionParse op; +static int create_resolver(Tcl_Interp *ip, const OptionParse *op, + Resolver **res_r) { Resolver *res=0; int rc, i, ec; - - op.aflags= adns_if_noautosys; - op.sflags= 0; - op.errfile= 0; - op.errcallback= 0; - op.config_string= 0; - rc= parse_options(ip,objc,objv,resolver_optioninfos,&op); - if (rc) goto x_rc; - - res= TALLOC(sizeof(*res)); assert(res); + + res= TALLOC(sizeof(*res)); assert(res); res->ix= -1; res->interp= ip; res->ads= 0; res->timertoken= 0; res->maxfd= 0; + scriptinv_init(&res->errcallback); for (i=0; i<3; i++) FD_ZERO(&res->handling[i]); - if (op.aflags & adns_if_noerrprint) { - op.errfile= 0; - op.errcallback= 0; - } - - ec= adns_init_logfn(&res->ads, op.aflags, op.config_string, - op.errcallback ? adnslogfn_callback : 0, - op.errcallback ? (void*)op.errcallback - : (void*)op.errfile); + ec= adns_init_logfn(&res->ads, + op->aflags | adns_if_noautosys, + op->config_string, + op->errcallback ? adnslogfn_callback : 0, + op->errcallback ? (void*)op->errcallback + : (void*)op->errfile); if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; } - if (op.errcallback) - Tcl_IncrRefCount(op.errcallback); + if (op->errcallback) + scriptinv_set(&res->errcallback, ip, op->errcallback, 0); - *result= res; + *res_r= res; return TCL_OK; x_rc: @@ -281,6 +298,36 @@ int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip, TFREE(res); } return rc; +} + +int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip, + int objc, Tcl_Obj *const *objv, + void **result) { + OptionParse op; + Resolver *res=0; + int rc; + + optparse_blank(&op); + rc= parse_options(ip,objc,objv,resolver_optioninfos,&op); + if (rc) return rc; + + if (op.aflags & adns_if_noerrprint) { + op.errfile= 0; + op.errcallback= 0; + } + + rc= create_resolver(ip, &op, &res); + if (rc) return rc; + + *result= res; + return TCL_OK; +} + +int do_adns_set_default_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) { + Resolver *res= res_v; + Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES); + Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, 0, res); + return TCL_OK; } const IdDataSpec adnstcl_resolvers= { @@ -344,16 +391,29 @@ static int query_submit(Tcl_Interp *ip, adns_query *aqu_r, void *context, OptionParse *op) { struct sockaddr sa; static const int aftry[]= { AF_INET, AF_INET6 }; + OptionParse res_op; int rc, r, ec; adns_state ads; op->aflags= adns_qf_owner; op->sflags= 0; - op->resolver= 0; /* fixme default */ + op->resolver= 0; op->reverseany= 0; rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,op); if (rc) return rc; + if (!op->resolver) { + op->resolver= default_resolver(ip); + if (!op->resolver) { + optparse_blank(&res_op); + rc= create_resolver(ip, &res_op, &op->resolver); + if (rc) return rc; + + Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, + destroy_resolver_defcb, op->resolver); + } + } + if (op->reverseany || (op->sflags & oisf_reverse)) { const int *af; for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) { diff --git a/base/tables-examples.tct b/base/tables-examples.tct index 98c6cd9..8a28c4f 100644 --- a/base/tables-examples.tct +++ b/base/tables-examples.tct @@ -69,6 +69,8 @@ Table adns Adns_SubCommand new-resolver ... obj => iddata(&adnstcl_resolvers) + set-default-resolver + res iddata(&adnstcl_resolvers) destroy-resolver res iddata(&adnstcl_resolvers) -- 2.30.2