4 * adns lookup TYPE DOMAIN [QUERY-OPTIONS] => [list RDATA]
5 * if no or dontknow, throws an exception, with errorCode one of
6 * ADNS permfail 300 nxdomain {No such domain}
7 * ADNS permfail 301 nodata {No such data}
8 * ADNS tempfail ERROR-CODE ERROR-NAME ERROR-STRING
10 * ERROR-CODE is the numerical adns status value
11 * ERROR-NAME is the symbolic adns status value (in lowercase)
12 * ERROR-STRING is the result of adns_strstatus
14 * adns synch TYPE DOMAIN [QUERY-OPTIONS] => RESULTS
15 * RESULTS is [list ok|permfail|tempfail
16 * ERROR-CODE ERROR-NAME ERROR-STRING \
19 * OWNER is the RR owner
20 * CNAME is the empty string or the canonical name if we went
23 * adns asynch ON-YES ON-NO ON-DONTKNOW XARGS \
25 * [QUERY-OPTIONS] => QUERY-ID
27 * [concat ON-YES|ON-NO|ON-DONTKNOW XARGS RESULTS]
28 * adns asynch-cancel QUERY-ID
30 * QUERY-OPTIONS are zero or more of
31 * -resolver RESOLVER (see adns new-resolver)
32 * default is to use a default resolver
41 * -reverse-any ZONE-A-LIKE
43 * adns new-resolver [RES-OPTIONS...] => RESOLVER
45 * -errfile stdout|stderr (stderr is the default)
47 * -errcallback CALLBACK results in eval CALLBACK [list MESSAGE]
48 * -noenv|-debug|-logpid
51 * -config CONFIG-STRING
53 * adns set-default-resolver RESOLVER
54 * cancels any outstanding queries from a previous anonymous
57 * adns destroy-resolver RESOLVER
58 * cancels outstanding queries
68 /*---------- important types and forward declarations ----------*/
70 typedef struct Query Query;
71 typedef struct Resolver Resolver;
72 typedef struct OptionInfo OptionInfo;
74 static void asynch_sethandlers(Resolver *res);
75 static void asynch_cancelhandlers(Resolver *res);
76 static void asynch_perturbed(Resolver *res);
78 static void asynch_query_dispose(Tcl_Interp *interp, Query *query);
80 #define ASSOC_DEFAULTRES "adns-defaultresolver"
82 /*---------- common resolver/query option processing ----------*/
85 /* this struct type is used to hold both resolver and query options */
86 /* common to resolver and query: */
92 const char *config_string;
95 const char *reverseany;
100 int (*fn)(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
103 unsigned long flags_add, flags_remove;
110 static int oiufn_f(const OptionInfo *oi, unsigned long *flags) {
111 *flags &= ~oi->flags_remove;
112 *flags |= oi->flags_add;
115 static int oifn_fa(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
116 OptionParse *op) { return oiufn_f(oi,&op->aflags); }
117 static int oifn_fs(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
118 OptionParse *op) { return oiufn_f(oi,&op->sflags); }
120 static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi,
121 Tcl_Obj *arg, OptionParse *op) {
122 return pat_string(ip,arg,&op->reverseany);
125 #define OIFA1(t,f,r) { "-" #f, oifn_fa, 0, adns_##t##_##f, r }
126 #define OIFA2(t,f,g) { "-" #f "-" #g, oifn_fa, 0, adns_##t##_##f##_##g, 0 }
127 #define OIFS(f) { "-" #f, oifn_fs, 0, oisf_##f, 0 }
128 #define OICA(o) { "-" #o, oifn_##o, 1 }
130 static void optparse_blank(OptionParse *op) {
131 memset(op,0,sizeof(*op));
134 op->config_string= 0;
137 static int parse_options(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv,
138 const OptionInfo opttable[], OptionParse *op) {
139 const OptionInfo *oi;
147 rc= pat_enum(ip, *objv++, &oi_v, opttable, sizeof(OptionInfo),
148 "query or resolver option");
154 setstringresult(ip,"missing value for option");
161 rc= oi->fn(ip,oi,arg,op);
167 /*---------- resolver management ----------*/
173 Tcl_TimerToken timertoken;
176 ScriptToInvoke errcallback;
177 Tcl_Obj *errstring_accum;
180 /* The default resolver is recorded using Tcl_SetAssocData with key
181 * ASSOC_DEFAULTRES to record the Resolver*. If it was explicitly
182 * created with `adns new-resolver' then ix will be >=0, and the
183 * resolver will be destroyed - leaving no default - when explicitly
184 * requested. If it was implicitly created (by starting a query when
185 * there is no default) then ix will be -1. */
187 static int oifn_errfile(Tcl_Interp *ip, const OptionInfo *oi,
188 Tcl_Obj *arg, OptionParse *op) {
192 rc= pat_string(ip,arg,&str); if (rc) return rc;
193 if (!strcmp(str,"stderr")) op->errfile= stderr;
194 else if (!strcmp(str,"stdout")) op->errfile= stdout;
195 else return staticerr(ip,"-errfile argument must be stderr or stdout",0);
197 op->aflags &= ~adns_if_noerrprint;
202 static int oifn_errcallback(Tcl_Interp *ip, const OptionInfo *oi,
203 Tcl_Obj *arg, OptionParse *op) {
204 op->errcallback= arg;
205 op->aflags &= ~adns_if_noerrprint;
210 static int oifn_config(Tcl_Interp *ip, const OptionInfo *oi,
211 Tcl_Obj *arg, OptionParse *op) {
212 return pat_string(ip,arg,&op->config_string);
215 static const OptionInfo resolver_optioninfos[]= {
217 OIFA1(if,debug, adns_if_noerrprint),
218 OIFA1(if,logpid, adns_if_noerrprint),
219 OIFA1(if,noerrprint, adns_if_debug),
220 OIFA2(if,checkc,entex),
221 OIFA2(if,checkc,freq),
228 static void adnslogfn_flushmessage(Resolver *res) {
229 scriptinv_invoke(&res->errcallback, 1, &res->errstring_accum);
230 Tcl_SetObjLength(res->errstring_accum, 0);
233 static void adnslogfn_callback(adns_state ads, void *logfndata,
234 const char *fmt, va_list al) {
235 Resolver *res= logfndata;
239 l= vasprintf(&str,fmt,al);
241 posixerr(res->interp,errno,"construct adns log callback string");
242 Tcl_BackgroundError(res->interp);
245 if (l==0) { free(str); return; }
246 if ((newline= l>0 && str[l-1]=='\n')) l--;
248 if (!res->errstring_accum) {
249 res->errstring_accum= Tcl_NewStringObj(str,l);
250 Tcl_IncrRefCount(res->errstring_accum);
252 Tcl_AppendToObj(res->errstring_accum,str,l);
257 adnslogfn_flushmessage(res);
260 static Resolver *default_resolver(Tcl_Interp *ip) {
261 return Tcl_GetAssocData(ip,ASSOC_DEFAULTRES,0);
264 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
270 if (res == default_resolver(ip))
271 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
273 if (res->errstring_accum) {
274 rstr= Tcl_GetStringFromObj(res->errstring_accum, &logstring_len);
277 adnslogfn_flushmessage(res);
281 /* although adns would throw these away for us, we need to
282 * destroy our own data too and only adns has a list of them */
284 adns_forallqueries_begin(res->ads);
285 aqu= adns_forallqueries_next(res->ads, &query_v);
287 asynch_query_dispose(ip, query_v);
289 adns_finish(res->ads);
292 asynch_cancelhandlers(res);
293 scriptinv_cancel(&res->errcallback);
294 Tcl_EventuallyFree(res, Tcl_Free);
297 static void destroy_resolver_idtabcb(Tcl_Interp *ip, void *resolver_v) {
298 destroy_resolver(ip,resolver_v);
300 static void destroy_resolver_defcb(ClientData resolver_v, Tcl_Interp *ip) {
301 destroy_resolver(ip,resolver_v);
304 int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
305 tabledataid_disposing(ip,res_v,&adnstcl_resolvers);
306 destroy_resolver(ip,res_v);
310 static int create_resolver(Tcl_Interp *ip, const OptionParse *op,
315 res= TALLOC(sizeof(*res)); assert(res);
321 for (i=0; i<3; i++) FD_ZERO(&res->handling[i]);
322 scriptinv_init(&res->errcallback);
323 res->errstring_accum= 0;
326 scriptinv_set(&res->errcallback, ip, op->errcallback, 0);
328 ec= adns_init_logfn(&res->ads,
329 op->aflags | adns_if_noautosys,
331 op->errcallback ? adnslogfn_callback : 0,
332 op->errcallback ? (void*)res : (void*)op->errfile);
333 if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; }
340 if (res->ads) adns_finish(res->ads);
346 int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
347 int objc, Tcl_Obj *const *objv,
354 rc= parse_options(ip,objc,objv,resolver_optioninfos,&op);
357 if (op.aflags & adns_if_noerrprint) {
362 rc= create_resolver(ip, &op, &res);
369 int do_adns_set_default_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
370 Resolver *res= res_v;
371 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
372 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, 0, res);
376 const IdDataSpec adnstcl_resolvers= {
377 "adns-res", "adns-resolvers-table", destroy_resolver_idtabcb
380 /*---------- query, query option and answers - common stuff ----------*/
382 #define RRTYPE_EXACTLY(t) { #t, adns_r_##t }
383 #define RRTYPE_RAW(t) { #t, adns_r_##t##_raw }
384 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
385 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
387 const AdnsTclRRTypeInfo adnstclrrtypeinfos[]= {
389 RRTYPE_EXACTLY(cname),
390 RRTYPE_EXACTLY(hinfo),
391 RRTYPE_EXACTLY(addr),
406 static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
407 Tcl_Obj *arg, OptionParse *op) {
411 rc= pat_iddata(ip,arg,&val_v,&adnstcl_resolvers);
417 static const OptionInfo query_optioninfos[]= {
420 OIFA2(qf,quoteok,query),
421 OIFA2(qf,quoteok,anshost),
422 OIFA2(qf,quotefail,cname),
423 OIFA2(qf,cname,loose),
424 OIFA2(qf,cname,forbid),
427 { "-reverse-any", oifn_reverse_any, 1 },
431 static int query_submit(Tcl_Interp *ip,
432 const AdnsTclRRTypeInfo *type, const char *domain,
433 int queryopts_objc, Tcl_Obj *const *queryopts_objv,
434 adns_query *aqu_r, void *context, Resolver **res_r) {
436 static const int aftry[]= { AF_INET, AF_INET6 };
442 op.aflags= adns_qf_owner;
446 rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,&op);
450 op.resolver= default_resolver(ip);
452 optparse_blank(&res_op);
453 rc= create_resolver(ip, &res_op, &op.resolver);
456 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES,
457 destroy_resolver_defcb, op.resolver);
463 if (op.reverseany || (op.sflags & oisf_reverse)) {
465 for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
466 memset(&sa,0,sizeof(sa));
468 r= inet_pton(*af,domain,&sa);
469 if (!r) goto af_found;
471 return staticerr(ip,"invalid address for adns reverse submit","");
475 ads= op.resolver->ads;
478 ec= adns_submit_reverse_any(ads, &sa, op.reverseany,
479 type->number, op.aflags, context, aqu_r);
480 } else if (op.sflags & oisf_reverse) {
481 ec= adns_submit_reverse(ads, &sa,
482 type->number, op.aflags, context, aqu_r);
484 ec= adns_submit(ads, domain,
485 type->number, op.aflags, context, aqu_r);
488 return posixerr(ip,ec,"submit adns query");
493 #define RESULTSTATUS_LLEN 4
494 #define RESULTLIST_LLEN 7
496 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
497 Tcl_Obj *results[RESULTSTATUS_LLEN]) {
498 results[0]= ret_string(ip, adns_errtypeabbrev(status));
499 results[1]= ret_int(ip, status);
500 results[2]= ret_string(ip, adns_errabbrev(status));
501 results[3]= ret_string(ip, adns_strerror(status));
504 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
505 Tcl_Obj **rdata, *rl;
508 char *datap, *rdatastring;
510 rdata= TALLOC(sizeof(*rdata) * answer->nrrs);
511 for (i=0, datap=answer->rrs.untyped;
513 i++, datap += rrsz) {
514 st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
516 rdata[i]= ret_string(ip, rdatastring);
519 rl= Tcl_NewListObj(answer->nrrs, rdata);
524 static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
525 Tcl_Obj *results[RESULTLIST_LLEN]) {
527 make_resultstatus(ip, answer->status, results);
528 assert(RESULTSTATUS_LLEN==4);
529 results[4]= ret_string(ip, answer->owner);
530 results[5]= ret_string(ip, answer->cname ? answer->cname : "");
531 results[6]= make_resultrdata(ip, answer);
534 /*---------- synchronous query handling ----------*/
536 static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
538 int objc, Tcl_Obj *const *objv, adns_answer **answer_r) {
543 rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,0,&res);
546 ec= adns_wait(res->ads,&aqu,answer_r,0);
549 asynch_perturbed(res);
553 int do_adns_lookup(ClientData cd, Tcl_Interp *ip,
554 const AdnsTclRRTypeInfo *rrtype,
556 int objc, Tcl_Obj *const *objv,
561 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
563 if (answer->status) {
564 Tcl_Obj *problem[RESULTSTATUS_LLEN];
565 make_resultstatus(ip, answer->status, problem);
566 *result= Tcl_NewListObj(RESULTSTATUS_LLEN, problem);
568 *result= make_resultrdata(ip, answer);
574 int do_adns_synch(ClientData cd, Tcl_Interp *ip,
575 const AdnsTclRRTypeInfo *rrtype,
577 int objc, Tcl_Obj *const *objv,
581 Tcl_Obj *results[RESULTLIST_LLEN];
583 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
584 make_resultlist(ip,answer,results);
586 *result= Tcl_NewListObj(RESULTLIST_LLEN, results);
590 /*---------- asynchronous query handling ----------*/
596 ScriptToInvoke on_yes, on_no, on_fail;
600 static void asynch_check_now(Resolver *res);
602 static void asynch_timerhandler(void *res_v) {
603 Resolver *res= res_v;
605 adns_processtimeouts(res->ads,0);
606 asynch_check_now(res);
609 static void asynch_filehandler(void *res_v, int mask) {
610 Resolver *res= res_v;
613 ec= adns_processany(res->ads);
614 if (ec) adns_globalsystemfailure(res->ads);
615 asynch_check_now(res);
618 static void asynch_sethandlers_generic(Resolver *res,
619 int shutdown /*from _cancelhandlers*/,
620 int immediate /*from _perturbed*/) {
623 struct timeval tv_buf, *timeout;
628 for (i=0; i<3; i++) FD_ZERO(&want[i]);
631 adns_beforeselect(res->ads,&maxfd,&want[0],&want[1],&want[2],
634 for (fd= 0; fd < maxfd || fd < res->maxfd; fd++)
636 if (!!FD_ISSET(fd, &res->handling[i])
637 != !!FD_ISSET(fd, &want[i])) {
639 if (FD_ISSET(fd, &want[0])) mask |= TCL_READABLE;
640 if (FD_ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
641 if (FD_ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
643 Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
644 FD_SET(fd, &res->handling[i]);
646 Tcl_DeleteFileHandler(fd);
647 FD_CLR(fd, &res->handling[i]);
652 Tcl_DeleteTimerHandler(res->timertoken);
655 res->timertoken= Tcl_CreateTimerHandler(0,asynch_timerhandler,res);
656 } else if (timeout) {
659 if (timeout->tv_sec >= INT_MAX/1000 - 1)
660 milliseconds= INT_MAX;
662 milliseconds= timeout->tv_sec * 1000 +
663 (timeout->tv_usec + 999) / 1000;
666 Tcl_CreateTimerHandler(milliseconds,asynch_timerhandler,res);
670 static void asynch_sethandlers(Resolver *res) {
671 asynch_sethandlers_generic(res,0,0);
673 static void asynch_cancelhandlers(Resolver *res) {
674 asynch_sethandlers_generic(res,1,0);
676 static void asynch_perturbed(Resolver *res) {
677 asynch_sethandlers_generic(res,0,1);
680 static void asynch_check_now(Resolver *res) {
681 Tcl_Interp *interp= res->interp;
688 Tcl_Obj *results[RESULTLIST_LLEN];
693 if (!res->ads) { /* oh, it has been destroyed! */
699 ec= adns_check(res->ads, &aqu, &answer, &query_v);
700 if (ec==ESRCH || ec==EAGAIN) break;
705 tabledataid_disposing(interp, query, &adnstcl_queries);
707 si= (!answer->status ? &query->on_yes
708 : answer->status > adns_s_max_tempfail ? &query->on_no
711 make_resultlist(interp, answer, results);
713 scriptinv_invoke(si, RESULTLIST_LLEN, results);
714 asynch_query_dispose(interp, query);
717 asynch_sethandlers(res);
722 int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
723 Tcl_Obj *on_yes, Tcl_Obj *on_no,
724 Tcl_Obj *on_fail, Tcl_Obj *xargs,
725 const AdnsTclRRTypeInfo *rrtype, const char *domain,
726 int objc, Tcl_Obj *const *objv, void **result) {
731 query= TALLOC(sizeof(*query));
734 scriptinv_init(&query->on_yes);
735 scriptinv_init(&query->on_no);
736 scriptinv_init(&query->on_fail);
739 rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&query->res);
744 rc= scriptinv_set(&query->on_yes, ip,on_yes, xargs); if (rc) goto x_rc;
745 rc= scriptinv_set(&query->on_no, ip,on_no, xargs); if (rc) goto x_rc;
746 rc= scriptinv_set(&query->on_fail,ip,on_fail,xargs); if (rc) goto x_rc;
748 Tcl_IncrRefCount(xargs);
750 query= 0; /* do not dispose */
754 if (query) asynch_query_dispose(ip, query);
755 if (res) asynch_perturbed(res);
759 int do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
760 Query *query= query_v;
761 Resolver *res= query->res;
762 asynch_query_dispose(ip, query);
763 asynch_perturbed(res);
767 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
768 tabledataid_disposing(interp, query, &adnstcl_queries);
769 scriptinv_cancel(&query->on_yes);
770 scriptinv_cancel(&query->on_no);
771 scriptinv_cancel(&query->on_fail);
772 if (query->xargs) Tcl_DecrRefCount(query->xargs);
773 if (query->aqu) adns_cancel(query->aqu);
777 static void destroy_query_idtabcb(Tcl_Interp *interp, void *query_v) {
778 asynch_query_dispose(interp, query_v);
781 const IdDataSpec adnstcl_queries= {
782 "adns", "adns-query-table", destroy_query_idtabcb