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
67 #include "chiark_tcl_adns.h"
69 /*---------- important types and forward declarations ----------*/
71 typedef struct Query Query;
72 typedef struct Resolver Resolver;
73 typedef struct OptionInfo OptionInfo;
75 static void asynch_sethandlers(Resolver *res);
76 static void asynch_cancelhandlers(Resolver *res);
77 static void asynch_perturbed(Resolver *res);
79 static void asynch_query_dispose(Tcl_Interp *interp, Query *query);
81 #define ASSOC_DEFAULTRES "adns-defaultresolver"
83 /*---------- common resolver/query option processing ----------*/
86 /* this struct type is used to hold both resolver and query options */
87 /* common to resolver and query: */
93 const char *config_string;
96 const char *reverseany;
101 int (*fn)(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
104 unsigned long flags_add, flags_remove;
111 static int oiufn_f(const OptionInfo *oi, unsigned long *flags) {
112 *flags &= ~oi->flags_remove;
113 *flags |= oi->flags_add;
116 static int oifn_fa(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
117 OptionParse *op) { return oiufn_f(oi,&op->aflags); }
118 static int oifn_fs(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
119 OptionParse *op) { return oiufn_f(oi,&op->sflags); }
121 static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi,
122 Tcl_Obj *arg, OptionParse *op) {
123 return cht_pat_string(ip,arg,&op->reverseany);
126 #define OIFA1(t,f,r) { "-" #f, oifn_fa, 0, adns_##t##_##f, r }
127 #define OIFA2(t,f,g) { "-" #f "-" #g, oifn_fa, 0, adns_##t##_##f##_##g, 0 }
128 #define OIFS(f) { "-" #f, oifn_fs, 0, oisf_##f, 0 }
129 #define OICA(o) { "-" #o, oifn_##o, 1 }
131 static void optparse_blank(OptionParse *op) {
132 memset(op,0,sizeof(*op));
135 op->config_string= 0;
138 static int parse_options(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv,
139 const OptionInfo opttable[], OptionParse *op) {
140 const OptionInfo *oi;
148 rc= cht_pat_enum(ip, *objv++, &oi_v, opttable, sizeof(OptionInfo),
149 "query or resolver option");
155 cht_setstringresult(ip,"missing value for option");
162 rc= oi->fn(ip,oi,arg,op);
168 /*---------- resolver management ----------*/
174 Tcl_TimerToken timertoken;
177 ScriptToInvoke errcallback;
178 Tcl_Obj *errstring_accum;
181 /* The default resolver is recorded using Tcl_SetAssocData with key
182 * ASSOC_DEFAULTRES to record the Resolver*. If it was explicitly
183 * created with `adns new-resolver' then ix will be >=0, and the
184 * resolver will be destroyed - leaving no default - when explicitly
185 * requested. If it was implicitly created (by starting a query when
186 * there is no default) then ix will be -1. */
188 static int oifn_errfile(Tcl_Interp *ip, const OptionInfo *oi,
189 Tcl_Obj *arg, OptionParse *op) {
193 rc= cht_pat_string(ip,arg,&str); if (rc) return rc;
194 if (!strcmp(str,"stderr")) op->errfile= stderr;
195 else if (!strcmp(str,"stdout")) op->errfile= stdout;
196 else return cht_staticerr(ip,"-errfile argument must be stderr or stdout",0);
198 op->aflags &= ~adns_if_noerrprint;
203 static int oifn_errcallback(Tcl_Interp *ip, const OptionInfo *oi,
204 Tcl_Obj *arg, OptionParse *op) {
205 op->errcallback= arg;
206 op->aflags &= ~adns_if_noerrprint;
211 static int oifn_config(Tcl_Interp *ip, const OptionInfo *oi,
212 Tcl_Obj *arg, OptionParse *op) {
213 return cht_pat_string(ip,arg,&op->config_string);
216 static const OptionInfo resolver_optioninfos[]= {
218 OIFA1(if,debug, adns_if_noerrprint),
219 OIFA1(if,logpid, adns_if_noerrprint),
220 OIFA1(if,noerrprint, adns_if_debug),
221 OIFA2(if,checkc,entex),
222 OIFA2(if,checkc,freq),
229 static void adnslogfn_flushmessage(Resolver *res) {
230 cht_scriptinv_invoke(&res->errcallback, 1, &res->errstring_accum);
231 Tcl_SetObjLength(res->errstring_accum, 0);
234 static void adnslogfn_callback(adns_state ads, void *logfndata,
235 const char *fmt, va_list al) {
236 Resolver *res= logfndata;
240 l= vasprintf(&str,fmt,al);
242 cht_posixerr(res->interp,errno,"construct adns log callback string");
243 Tcl_BackgroundError(res->interp);
246 if (l==0) { free(str); return; }
247 if ((newline= l>0 && str[l-1]=='\n')) l--;
249 if (!res->errstring_accum) {
250 res->errstring_accum= Tcl_NewStringObj(str,l);
251 Tcl_IncrRefCount(res->errstring_accum);
253 Tcl_AppendToObj(res->errstring_accum,str,l);
258 adnslogfn_flushmessage(res);
261 static Resolver *default_resolver(Tcl_Interp *ip) {
262 return Tcl_GetAssocData(ip,ASSOC_DEFAULTRES,0);
265 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
271 if (res == default_resolver(ip))
272 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
274 if (res->errstring_accum) {
275 rstr= Tcl_GetStringFromObj(res->errstring_accum, &logstring_len);
278 adnslogfn_flushmessage(res);
282 /* although adns would throw these away for us, we need to
283 * destroy our own data too and only adns has a list of them */
285 adns_forallqueries_begin(res->ads);
286 aqu= adns_forallqueries_next(res->ads, &query_v);
288 asynch_query_dispose(ip, query_v);
290 adns_finish(res->ads);
293 asynch_cancelhandlers(res);
294 cht_scriptinv_cancel(&res->errcallback);
295 Tcl_EventuallyFree(res, Tcl_Free);
298 static void destroy_resolver_idtabcb(Tcl_Interp *ip, void *resolver_v) {
299 destroy_resolver(ip,resolver_v);
301 static void destroy_resolver_defcb(ClientData resolver_v, Tcl_Interp *ip) {
302 destroy_resolver(ip,resolver_v);
305 int cht_do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
306 cht_tabledataid_disposing(ip,res_v,&adnstcl_resolvers);
307 destroy_resolver(ip,res_v);
311 static int create_resolver(Tcl_Interp *ip, const OptionParse *op,
316 res= TALLOC(sizeof(*res)); assert(res);
322 for (i=0; i<3; i++) FD_ZERO(&res->handling[i]);
323 cht_scriptinv_init(&res->errcallback);
324 res->errstring_accum= 0;
327 cht_scriptinv_set(&res->errcallback, ip, op->errcallback, 0);
329 ec= adns_init_logfn(&res->ads,
330 op->aflags | adns_if_noautosys,
332 op->errcallback ? adnslogfn_callback : 0,
333 op->errcallback ? (void*)res : (void*)op->errfile);
334 if (ec) { rc= cht_posixerr(ip,ec,"create adns resolver"); goto x_rc; }
341 if (res->ads) adns_finish(res->ads);
347 int cht_do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
348 int objc, Tcl_Obj *const *objv,
355 rc= parse_options(ip,objc,objv,resolver_optioninfos,&op);
358 if (op.aflags & adns_if_noerrprint) {
363 rc= create_resolver(ip, &op, &res);
370 int cht_do_adns_set_default_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
371 Resolver *res= res_v;
372 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
373 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, 0, res);
377 const IdDataSpec cht_adnstcl_resolvers= {
378 "adns-res", "adns-resolvers-table", destroy_resolver_idtabcb
381 /*---------- query, query option and answers - common stuff ----------*/
383 #define RRTYPE_EXACTLY(t) { #t, adns_r_##t }
384 #define RRTYPE_RAW(t) { #t, adns_r_##t##_raw }
385 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
386 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
388 const AdnsTclRRTypeInfo cht_adnstclrrtypeinfo_entries[]= {
390 RRTYPE_EXACTLY(cname),
391 RRTYPE_EXACTLY(hinfo),
392 RRTYPE_EXACTLY(addr),
407 static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
408 Tcl_Obj *arg, OptionParse *op) {
412 rc= cht_pat_iddata(ip,arg,&val_v,&adnstcl_resolvers);
418 static const OptionInfo query_optioninfos[]= {
421 OIFA2(qf,quoteok,query),
422 OIFA2(qf,quoteok,anshost),
423 OIFA2(qf,quotefail,cname),
424 OIFA2(qf,cname,loose),
425 OIFA2(qf,cname,forbid),
428 { "-reverse-any", oifn_reverse_any, 1 },
432 static int query_submit(Tcl_Interp *ip,
433 const AdnsTclRRTypeInfo *type, const char *domain,
434 int queryopts_objc, Tcl_Obj *const *queryopts_objv,
435 adns_query *aqu_r, void *context, Resolver **res_r) {
437 static const int aftry[]= { AF_INET, AF_INET6 };
443 op.aflags= adns_qf_owner;
447 rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,&op);
451 op.resolver= default_resolver(ip);
453 optparse_blank(&res_op);
454 rc= create_resolver(ip, &res_op, &op.resolver);
457 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES,
458 destroy_resolver_defcb, op.resolver);
464 if (op.reverseany || (op.sflags & oisf_reverse)) {
466 for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
467 memset(&sa,0,sizeof(sa));
469 r= inet_pton(*af,domain,&sa);
470 if (!r) goto af_found;
472 return cht_staticerr(ip,"invalid address for adns reverse submit",
473 "ADNS REVERSE INVALID");
477 ads= op.resolver->ads;
480 ec= adns_submit_reverse_any(ads, &sa, op.reverseany,
481 type->number, op.aflags, context, aqu_r);
482 } else if (op.sflags & oisf_reverse) {
483 ec= adns_submit_reverse(ads, &sa,
484 type->number, op.aflags, context, aqu_r);
486 ec= adns_submit(ads, domain,
487 type->number, op.aflags, context, aqu_r);
490 return cht_posixerr(ip,ec,"submit adns query");
495 #define RESULTSTATUS_LLEN 4
496 #define RESULTLIST_LLEN 7
498 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
499 Tcl_Obj *results[RESULTSTATUS_LLEN]) {
500 results[0]= cht_ret_string(ip, adns_errtypeabbrev(status));
501 results[1]= cht_ret_int(ip, status);
502 results[2]= cht_ret_string(ip, adns_errabbrev(status));
503 results[3]= cht_ret_string(ip, adns_strerror(status));
506 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
507 Tcl_Obj **rdata, *rl;
510 char *datap, *rdatastring;
512 rdata= TALLOC(sizeof(*rdata) * answer->nrrs);
513 for (i=0, datap=answer->rrs.untyped;
515 i++, datap += rrsz) {
516 st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
518 rdata[i]= cht_ret_string(ip, rdatastring);
521 rl= Tcl_NewListObj(answer->nrrs, rdata);
526 static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
527 Tcl_Obj *results[RESULTLIST_LLEN]) {
529 make_resultstatus(ip, answer->status, results);
530 assert(RESULTSTATUS_LLEN==4);
531 results[4]= cht_ret_string(ip, answer->owner);
532 results[5]= cht_ret_string(ip, answer->cname ? answer->cname : "");
533 results[6]= make_resultrdata(ip, answer);
536 /*---------- synchronous query handling ----------*/
538 static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
540 int objc, Tcl_Obj *const *objv, adns_answer **answer_r) {
545 rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,0,&res);
548 ec= adns_wait(res->ads,&aqu,answer_r,0);
551 asynch_perturbed(res);
555 int cht_do_adns_lookup(ClientData cd, Tcl_Interp *ip,
556 const AdnsTclRRTypeInfo *rrtype,
558 int objc, Tcl_Obj *const *objv,
563 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
565 if (answer->status) {
566 Tcl_Obj *problem[RESULTSTATUS_LLEN];
567 make_resultstatus(ip, answer->status, problem);
568 *result= Tcl_NewListObj(RESULTSTATUS_LLEN, problem);
570 *result= make_resultrdata(ip, answer);
576 int cht_do_adns_synch(ClientData cd, Tcl_Interp *ip,
577 const AdnsTclRRTypeInfo *rrtype,
579 int objc, Tcl_Obj *const *objv,
583 Tcl_Obj *results[RESULTLIST_LLEN];
585 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
586 make_resultlist(ip,answer,results);
588 *result= Tcl_NewListObj(RESULTLIST_LLEN, results);
592 /*---------- asynchronous query handling ----------*/
598 ScriptToInvoke on_yes, on_no, on_fail;
602 static void asynch_check_now(Resolver *res);
604 static void asynch_timerhandler(void *res_v) {
605 Resolver *res= res_v;
607 adns_processtimeouts(res->ads,0);
608 asynch_check_now(res);
611 static void asynch_filehandler(void *res_v, int mask) {
612 Resolver *res= res_v;
615 ec= adns_processany(res->ads);
616 if (ec) adns_globalsystemfailure(res->ads);
617 asynch_check_now(res);
620 static void asynch_sethandlers_generic(Resolver *res,
621 int shutdown /*from _cancelhandlers*/,
622 int immediate /*from _perturbed*/) {
625 struct timeval tv_buf, *timeout;
630 for (i=0; i<3; i++) FD_ZERO(&want[i]);
633 adns_beforeselect(res->ads,&maxfd,&want[0],&want[1],&want[2],
636 for (fd= 0; fd < maxfd || fd < res->maxfd; fd++)
638 if (!!FD_ISSET(fd, &res->handling[i])
639 != !!FD_ISSET(fd, &want[i])) {
641 if (FD_ISSET(fd, &want[0])) mask |= TCL_READABLE;
642 if (FD_ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
643 if (FD_ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
645 Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
646 FD_SET(fd, &res->handling[i]);
648 Tcl_DeleteFileHandler(fd);
649 FD_CLR(fd, &res->handling[i]);
654 Tcl_DeleteTimerHandler(res->timertoken);
657 res->timertoken= Tcl_CreateTimerHandler(0,asynch_timerhandler,res);
658 } else if (timeout) {
661 if (timeout->tv_sec >= INT_MAX/1000 - 1)
662 milliseconds= INT_MAX;
664 milliseconds= timeout->tv_sec * 1000 +
665 (timeout->tv_usec + 999) / 1000;
668 Tcl_CreateTimerHandler(milliseconds,asynch_timerhandler,res);
672 static void asynch_sethandlers(Resolver *res) {
673 asynch_sethandlers_generic(res,0,0);
675 static void asynch_cancelhandlers(Resolver *res) {
676 asynch_sethandlers_generic(res,1,0);
678 static void asynch_perturbed(Resolver *res) {
679 asynch_sethandlers_generic(res,0,1);
682 static void asynch_check_now(Resolver *res) {
683 Tcl_Interp *interp= res->interp;
690 Tcl_Obj *results[RESULTLIST_LLEN];
695 if (!res->ads) { /* oh, it has been destroyed! */
701 ec= adns_check(res->ads, &aqu, &answer, &query_v);
702 if (ec==ESRCH || ec==EAGAIN) break;
707 cht_tabledataid_disposing(interp, query, &adnstcl_queries);
709 si= (!answer->status ? &query->on_yes
710 : answer->status > adns_s_max_tempfail ? &query->on_no
713 make_resultlist(interp, answer, results);
715 cht_scriptinv_invoke(si, RESULTLIST_LLEN, results);
716 asynch_query_dispose(interp, query);
719 asynch_sethandlers(res);
724 int cht_do_adns_asynch(ClientData cd, Tcl_Interp *ip,
725 Tcl_Obj *on_yes, Tcl_Obj *on_no,
726 Tcl_Obj *on_fail, Tcl_Obj *xargs,
727 const AdnsTclRRTypeInfo *rrtype, const char *domain,
728 int objc, Tcl_Obj *const *objv, void **result) {
733 query= TALLOC(sizeof(*query));
736 cht_scriptinv_init(&query->on_yes);
737 cht_scriptinv_init(&query->on_no);
738 cht_scriptinv_init(&query->on_fail);
741 rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&query->res);
746 rc= cht_scriptinv_set(&query->on_yes, ip,on_yes, xargs); if (rc) goto x_rc;
747 rc= cht_scriptinv_set(&query->on_no, ip,on_no, xargs); if (rc) goto x_rc;
748 rc= cht_scriptinv_set(&query->on_fail,ip,on_fail,xargs); if (rc) goto x_rc;
750 Tcl_IncrRefCount(xargs);
752 query= 0; /* do not dispose */
756 if (query) asynch_query_dispose(ip, query);
757 if (res) asynch_perturbed(res);
761 int cht_do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
762 Query *query= query_v;
763 Resolver *res= query->res;
764 asynch_query_dispose(ip, query);
765 asynch_perturbed(res);
769 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
770 cht_tabledataid_disposing(interp, query, &adnstcl_queries);
771 cht_scriptinv_cancel(&query->on_yes);
772 cht_scriptinv_cancel(&query->on_no);
773 cht_scriptinv_cancel(&query->on_fail);
774 if (query->xargs) Tcl_DecrRefCount(query->xargs);
775 if (query->aqu) adns_cancel(query->aqu);
779 static void destroy_query_idtabcb(Tcl_Interp *interp, void *query_v) {
780 asynch_query_dispose(interp, query_v);
783 const IdDataSpec adnstcl_queries= {
784 "adns", "adns-query-table", destroy_query_idtabcb
787 /*---------- main hooks for tcl ----------*/
789 int cht_do_adnstoplevel_adns(ClientData cd, Tcl_Interp *ip,
790 const Adns_SubCommand *subcmd,
791 int objc, Tcl_Obj *const *objv) {
792 return subcmd->func(0,ip,objc,objv);
795 extern int Chiark_tcl_adns_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
796 int Chiark_tcl_adns_Init(Tcl_Interp *ip) {
797 return cht_initextension(ip, cht_adnstoplevel_entries, 0);