2 * adns lookup TYPE DOMAIN [QUERY-OPTIONS] => [list RDATA]
3 * if no or dontknow, throws an exception, with errorCode one of
4 * ADNS permfail 300 nxdomain {No such domain}
5 * ADNS permfail 301 nodata {No such data}
6 * ADNS tempfail ERROR-CODE ERROR-NAME ERROR-STRING
8 * ERROR-CODE is the numerical adns status value
9 * ERROR-NAME is the symbolic adns status value (in lowercase)
10 * ERROR-STRING is the result of adns_strstatus
12 * adns synch TYPE DOMAIN [QUERY-OPTIONS] => RESULTS
13 * RESULTS is [list ok|permfail|tempfail
14 * ERROR-CODE ERROR-NAME ERROR-STRING \
17 * OWNER is the RR owner
18 * CNAME is the empty string or the canonical name if we went
21 * adns asynch ON-YES ON-NO ON-DONTKNOW XARGS \
23 * [QUERY-OPTIONS...] => QUERY-ID
25 * [concat ON-YES|ON-NO|ON-DONTKNOW XARGS RESULTS]
26 * adns asynch-cancel QUERY-ID
28 * QUERY-OPTIONS are zero or more of
29 * -resolver RESOLVER (see adns new-resolver)
30 * default is to use a default resolver
39 * -reverse-any ZONE-A-LIKE
41 * adns new-resolver [RES-OPTIONS...] => RESOLVER
43 * -errfile stdout|stderr (stderr is the default)
45 * -errcallback CALLBACK results in eval CALLBACK [list MESSAGE]
46 * -noenv|-debug|-logpid
49 * -config CONFIG-STRING
51 * adns set-default-resolver RESOLVER
52 * cancels any outstanding queries from a previous anonymous
55 * adns destroy-resolver RESOLVER
56 * cancels outstanding queries
59 /* ---8<--- end of documentation comment --8<-- */
62 * adns.c - adns binding for Tcl
63 * Copyright 2006 Ian Jackson
65 * This program is free software; you can redistribute it and/or
66 * modify it under the terms of the GNU General Public License as
67 * published by the Free Software Foundation; either version 2 of the
68 * License, or (at your option) any later version.
70 * This program is distributed in the hope that it will be useful, but
71 * WITHOUT ANY WARRANTY; without even the implied warranty of
72 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
73 * General Public License for more details.
75 * You should have received a copy of the GNU General Public License
76 * along with this library; if not, write to the Free Software
77 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
87 #include "chiark_tcl_adns.h"
89 /*---------- important types and forward declarations ----------*/
91 typedef struct Query Query;
92 typedef struct Resolver Resolver;
93 typedef struct OptionInfo OptionInfo;
95 static void asynch_sethandlers(Resolver *res);
96 static void asynch_cancelhandlers(Resolver *res);
97 static void asynch_perturbed(Resolver *res);
99 static void asynch_query_dispose(Tcl_Interp *interp, Query *query);
101 #define ASSOC_DEFAULTRES "adns-defaultresolver"
103 /*---------- common resolver/query option processing ----------*/
106 /* this struct type is used to hold both resolver and query options */
107 /* common to resolver and query: */
108 unsigned long aflags;
109 unsigned long sflags;
112 Tcl_Obj *errcallback;
113 const char *config_string;
116 const char *reverseany;
121 int (*fn)(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
124 unsigned long flags_add, flags_remove;
131 static int oiufn_f(const OptionInfo *oi, unsigned long *flags) {
132 *flags &= ~oi->flags_remove;
133 *flags |= oi->flags_add;
136 static int oifn_fa(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
137 OptionParse *op) { return oiufn_f(oi,&op->aflags); }
138 static int oifn_fs(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
139 OptionParse *op) { return oiufn_f(oi,&op->sflags); }
141 static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi,
142 Tcl_Obj *arg, OptionParse *op) {
143 return cht_pat_string(ip,arg,&op->reverseany);
146 #define OIFA1(t,f,r) { "-" #f, oifn_fa, 0, adns_##t##_##f, r }
147 #define OIFA2(t,f,g) { "-" #f "-" #g, oifn_fa, 0, adns_##t##_##f##_##g, 0 }
148 #define OIFS(f) { "-" #f, oifn_fs, 0, oisf_##f, 0 }
149 #define OICA(o) { "-" #o, oifn_##o, 1 }
151 static void optparse_blank(OptionParse *op) {
152 memset(op,0,sizeof(*op));
155 op->config_string= 0;
158 static int parse_options(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv,
159 const OptionInfo opttable[], OptionParse *op) {
160 const OptionInfo *oi;
168 rc= cht_pat_enum(ip, *objv++, &oi_v, opttable, sizeof(OptionInfo),
169 "query or resolver option");
175 cht_setstringresult(ip,"missing value for option");
182 rc= oi->fn(ip,oi,arg,op);
188 /*---------- resolver management ----------*/
194 Tcl_TimerToken timertoken;
197 ScriptToInvoke errcallback;
198 Tcl_Obj *errstring_accum;
201 /* The default resolver is recorded using Tcl_SetAssocData with key
202 * ASSOC_DEFAULTRES to record the Resolver*. If it was explicitly
203 * created with `adns new-resolver' then ix will be >=0, and the
204 * resolver will be destroyed - leaving no default - when explicitly
205 * requested. If it was implicitly created (by starting a query when
206 * there is no default) then ix will be -1. */
208 static int oifn_errfile(Tcl_Interp *ip, const OptionInfo *oi,
209 Tcl_Obj *arg, OptionParse *op) {
213 rc= cht_pat_string(ip,arg,&str); if (rc) return rc;
214 if (!strcmp(str,"stderr")) op->errfile= stderr;
215 else if (!strcmp(str,"stdout")) op->errfile= stdout;
216 else return cht_staticerr(ip,"-errfile argument must be stderr or stdout",0);
218 op->aflags &= ~adns_if_noerrprint;
223 static int oifn_errcallback(Tcl_Interp *ip, const OptionInfo *oi,
224 Tcl_Obj *arg, OptionParse *op) {
225 op->errcallback= arg;
226 op->aflags &= ~adns_if_noerrprint;
231 static int oifn_config(Tcl_Interp *ip, const OptionInfo *oi,
232 Tcl_Obj *arg, OptionParse *op) {
233 return cht_pat_string(ip,arg,&op->config_string);
236 static const OptionInfo resolver_optioninfos[]= {
238 OIFA1(if,debug, adns_if_noerrprint),
239 OIFA1(if,logpid, adns_if_noerrprint),
240 OIFA1(if,noerrprint, adns_if_debug),
241 OIFA2(if,checkc,entex),
242 OIFA2(if,checkc,freq),
249 static void adnslogfn_flushmessage(Resolver *res) {
250 cht_scriptinv_invoke(&res->errcallback, 1, &res->errstring_accum);
251 Tcl_SetObjLength(res->errstring_accum, 0);
254 static void adnslogfn_callback(adns_state ads, void *logfndata,
255 const char *fmt, va_list al) {
256 Resolver *res= logfndata;
260 l= vasprintf(&str,fmt,al);
262 cht_posixerr(res->interp,errno,"construct adns log callback string");
263 Tcl_BackgroundError(res->interp);
266 if (l==0) { free(str); return; }
267 if ((newline= l>0 && str[l-1]=='\n')) l--;
269 if (!res->errstring_accum) {
270 res->errstring_accum= Tcl_NewStringObj(str,l);
271 Tcl_IncrRefCount(res->errstring_accum);
273 Tcl_AppendToObj(res->errstring_accum,str,l);
278 adnslogfn_flushmessage(res);
281 static Resolver *default_resolver(Tcl_Interp *ip) {
282 return Tcl_GetAssocData(ip,ASSOC_DEFAULTRES,0);
285 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
291 if (res == default_resolver(ip))
292 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
294 if (res->errstring_accum) {
295 rstr= Tcl_GetStringFromObj(res->errstring_accum, &logstring_len);
298 adnslogfn_flushmessage(res);
302 /* although adns would throw these away for us, we need to
303 * destroy our own data too and only adns has a list of them */
305 adns_forallqueries_begin(res->ads);
306 aqu= adns_forallqueries_next(res->ads, &query_v);
308 assert(query_v->aqu == aqu);
309 query_v->aqu= 0; /* avoid disrupting the adns query list */
310 asynch_query_dispose(ip, query_v);
312 adns_finish(res->ads);
315 asynch_cancelhandlers(res);
316 cht_scriptinv_cancel(&res->errcallback);
317 Tcl_EventuallyFree(res, Tcl_Free);
320 static void destroy_resolver_idtabcb(Tcl_Interp *ip, void *resolver_v) {
321 destroy_resolver(ip,resolver_v);
323 static void destroy_resolver_defcb(ClientData resolver_v, Tcl_Interp *ip) {
324 destroy_resolver(ip,resolver_v);
327 int cht_do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
328 cht_tabledataid_disposing(ip,res_v,&cht_adnstcl_resolvers);
329 destroy_resolver(ip,res_v);
333 static int create_resolver(Tcl_Interp *ip, const OptionParse *op,
338 res= TALLOC(sizeof(*res)); assert(res);
344 for (i=0; i<3; i++) FD_ZERO(&res->handling[i]);
345 cht_scriptinv_init(&res->errcallback);
346 res->errstring_accum= 0;
349 cht_scriptinv_set(&res->errcallback, ip, op->errcallback, 0);
351 ec= adns_init_logfn(&res->ads,
352 op->aflags | adns_if_noautosys,
354 op->errcallback ? adnslogfn_callback : 0,
355 op->errcallback ? (void*)res : (void*)op->errfile);
356 if (ec) { rc= cht_posixerr(ip,ec,"create adns resolver"); goto x_rc; }
363 if (res->ads) adns_finish(res->ads);
369 int cht_do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
370 int objc, Tcl_Obj *const *objv,
377 rc= parse_options(ip,objc,objv,resolver_optioninfos,&op);
380 if (op.aflags & adns_if_noerrprint) {
385 rc= create_resolver(ip, &op, &res);
392 int cht_do_adns_set_default_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
393 Resolver *res= res_v;
394 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
395 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, 0, res);
399 const IdDataSpec cht_adnstcl_resolvers= {
400 "adns-res", "adns-resolvers-table", destroy_resolver_idtabcb
403 /*---------- query, query option and answers - common stuff ----------*/
405 #define RRTYPE_EXACTLY(t) { #t, adns_r_##t }
406 #define RRTYPE_RAW(t) { #t, adns_r_##t##_raw }
407 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
408 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
410 const AdnsTclRRTypeInfo cht_adnstclrrtypeinfo_entries[]= {
412 RRTYPE_EXACTLY(cname),
413 RRTYPE_EXACTLY(hinfo),
414 RRTYPE_EXACTLY(addr),
430 static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
431 Tcl_Obj *arg, OptionParse *op) {
435 rc= cht_pat_iddata(ip,arg,&val_v,&cht_adnstcl_resolvers);
441 static const OptionInfo query_optioninfos[]= {
444 OIFA2(qf,quoteok,query),
445 OIFA2(qf,quoteok,anshost),
446 OIFA2(qf,quotefail,cname),
447 OIFA2(qf,cname,loose),
448 OIFA2(qf,cname,forbid),
451 { "-reverse-any", oifn_reverse_any, 1 },
455 static int query_submit(Tcl_Interp *ip,
456 const AdnsTclRRTypeInfo *type, const char *domain,
457 int queryopts_objc, Tcl_Obj *const *queryopts_objv,
458 adns_query *aqu_r, void *context, Resolver **res_r) {
460 static const int aftry[]= { AF_INET, AF_INET6 };
466 op.aflags= adns_qf_owner;
470 rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,&op);
474 op.resolver= default_resolver(ip);
476 optparse_blank(&res_op);
477 rc= create_resolver(ip, &res_op, &op.resolver);
480 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES,
481 destroy_resolver_defcb, op.resolver);
487 if (op.reverseany || (op.sflags & oisf_reverse)) {
489 for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
490 memset(&sa,0,sizeof(sa));
492 r= inet_pton(*af,domain,&sa);
493 if (!r) goto af_found;
495 return cht_staticerr(ip,"invalid address for adns reverse submit",
496 "ADNS REVERSE INVALID");
500 ads= op.resolver->ads;
503 ec= adns_submit_reverse_any(ads, &sa, op.reverseany,
504 type->number, op.aflags, context, aqu_r);
505 } else if (op.sflags & oisf_reverse) {
506 ec= adns_submit_reverse(ads, &sa,
507 type->number, op.aflags, context, aqu_r);
509 ec= adns_submit(ads, domain,
510 type->number, op.aflags, context, aqu_r);
513 return cht_posixerr(ip,ec,"submit adns query");
518 #define RESULTSTATUS_LLEN 4
519 #define RESULTLIST_LLEN 7
521 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
522 Tcl_Obj *results[RESULTSTATUS_LLEN]) {
523 results[0]= cht_ret_string(ip, adns_errtypeabbrev(status));
524 results[1]= cht_ret_int(ip, status);
525 results[2]= cht_ret_string(ip, adns_errabbrev(status));
526 results[3]= cht_ret_string(ip, adns_strerror(status));
527 assert(RESULTSTATUS_LLEN==4);
530 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
531 Tcl_Obj **rdata, *rl;
534 char *datap, *rdatastring;
536 rdata= TALLOC(sizeof(*rdata) * answer->nrrs);
537 for (i=0, datap=answer->rrs.untyped;
539 i++, datap += rrsz) {
540 st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
542 rdata[i]= cht_ret_string(ip, rdatastring);
545 rl= Tcl_NewListObj(answer->nrrs, rdata);
550 static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
551 Tcl_Obj *results[RESULTLIST_LLEN]) {
553 make_resultstatus(ip, answer->status, results);
554 assert(RESULTSTATUS_LLEN==4);
555 results[4]= cht_ret_string(ip, answer->owner);
556 results[5]= cht_ret_string(ip, answer->cname ? answer->cname : "");
557 results[6]= make_resultrdata(ip, answer);
558 assert(RESULTLIST_LLEN==7);
561 /*---------- synchronous query handling ----------*/
563 static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
565 int objc, Tcl_Obj *const *objv, adns_answer **answer_r) {
570 rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,0,&res);
573 ec= adns_wait(res->ads,&aqu,answer_r,0);
576 asynch_perturbed(res);
580 int cht_do_adns_lookup(ClientData cd, Tcl_Interp *ip,
581 const AdnsTclRRTypeInfo *rrtype,
583 int objc, Tcl_Obj *const *objv,
588 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
590 if (answer->status) {
591 Tcl_Obj *problem[RESULTSTATUS_LLEN];
592 make_resultstatus(ip, answer->status, problem);
593 *result= Tcl_NewListObj(RESULTSTATUS_LLEN, problem);
595 *result= make_resultrdata(ip, answer);
601 int cht_do_adns_synch(ClientData cd, Tcl_Interp *ip,
602 const AdnsTclRRTypeInfo *rrtype,
604 int objc, Tcl_Obj *const *objv,
608 Tcl_Obj *results[RESULTLIST_LLEN];
610 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
611 make_resultlist(ip,answer,results);
613 *result= Tcl_NewListObj(RESULTLIST_LLEN, results);
617 /*---------- asynchronous query handling ----------*/
623 ScriptToInvoke on_yes, on_no, on_fail;
627 static void asynch_check_now(Resolver *res);
629 static void asynch_timerhandler(void *res_v) {
630 Resolver *res= res_v;
632 adns_processtimeouts(res->ads,0);
633 asynch_check_now(res);
636 static void asynch_filehandler(void *res_v, int mask) {
637 Resolver *res= res_v;
640 ec= adns_processany(res->ads);
641 if (ec) adns_globalsystemfailure(res->ads);
642 asynch_check_now(res);
645 static void asynch_sethandlers_generic(Resolver *res,
646 int shutdown /*from _cancelhandlers*/,
647 int immediate /*from _perturbed*/) {
650 struct timeval tv_buf, *timeout;
655 for (i=0; i<3; i++) FD_ZERO(&want[i]);
658 adns_beforeselect(res->ads,&maxfd,&want[0],&want[1],&want[2],
661 for (fd= 0; fd < maxfd || fd < res->maxfd; fd++)
663 if (!!FD_ISSET(fd, &res->handling[i])
664 != !!FD_ISSET(fd, &want[i])) {
666 if (FD_ISSET(fd, &want[0])) mask |= TCL_READABLE;
667 if (FD_ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
668 if (FD_ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
670 Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
671 FD_SET(fd, &res->handling[i]);
673 Tcl_DeleteFileHandler(fd);
674 FD_CLR(fd, &res->handling[i]);
679 Tcl_DeleteTimerHandler(res->timertoken);
682 res->timertoken= Tcl_CreateTimerHandler(0,asynch_timerhandler,res);
683 } else if (timeout) {
686 if (timeout->tv_sec >= INT_MAX/1000 - 1)
687 milliseconds= INT_MAX;
689 milliseconds= timeout->tv_sec * 1000 +
690 (timeout->tv_usec + 999) / 1000;
693 Tcl_CreateTimerHandler(milliseconds,asynch_timerhandler,res);
697 static void asynch_sethandlers(Resolver *res) {
698 asynch_sethandlers_generic(res,0,0);
700 static void asynch_cancelhandlers(Resolver *res) {
701 asynch_sethandlers_generic(res,1,0);
703 static void asynch_perturbed(Resolver *res) {
704 asynch_sethandlers_generic(res,0,1);
707 static void asynch_check_now(Resolver *res) {
708 Tcl_Interp *interp= res->interp;
715 Tcl_Obj *results[RESULTLIST_LLEN];
720 if (!res->ads) { /* oh, it has been destroyed! */
726 ec= adns_check(res->ads, &aqu, &answer, &query_v);
727 if (ec==ESRCH || ec==EAGAIN) break;
732 cht_tabledataid_disposing(interp, query, &cht_adnstcl_queries);
734 si= (!answer->status ? &query->on_yes
735 : answer->status > adns_s_max_tempfail ? &query->on_no
738 make_resultlist(interp, answer, results);
740 cht_scriptinv_invoke(si, RESULTLIST_LLEN, results);
741 asynch_query_dispose(interp, query);
744 asynch_sethandlers(res);
749 int cht_do_adns_asynch(ClientData cd, Tcl_Interp *ip,
750 Tcl_Obj *on_yes, Tcl_Obj *on_no,
751 Tcl_Obj *on_fail, Tcl_Obj *xargs,
752 const AdnsTclRRTypeInfo *rrtype, const char *domain,
753 int objc, Tcl_Obj *const *objv, void **result) {
758 query= TALLOC(sizeof(*query));
761 cht_scriptinv_init(&query->on_yes);
762 cht_scriptinv_init(&query->on_no);
763 cht_scriptinv_init(&query->on_fail);
766 rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&query->res);
771 rc= cht_scriptinv_set(&query->on_yes, ip,on_yes, xargs); if (rc) goto x_rc;
772 rc= cht_scriptinv_set(&query->on_no, ip,on_no, xargs); if (rc) goto x_rc;
773 rc= cht_scriptinv_set(&query->on_fail,ip,on_fail,xargs); if (rc) goto x_rc;
775 Tcl_IncrRefCount(xargs);
777 query= 0; /* do not dispose */
781 if (query) asynch_query_dispose(ip, query);
782 if (res) asynch_perturbed(res);
786 int cht_do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
787 Query *query= query_v;
788 Resolver *res= query->res;
789 asynch_query_dispose(ip, query);
790 asynch_perturbed(res);
794 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
795 cht_tabledataid_disposing(interp, query, &cht_adnstcl_queries);
796 cht_scriptinv_cancel(&query->on_yes);
797 cht_scriptinv_cancel(&query->on_no);
798 cht_scriptinv_cancel(&query->on_fail);
799 if (query->xargs) Tcl_DecrRefCount(query->xargs);
800 if (query->aqu) adns_cancel(query->aqu);
804 static void destroy_query_idtabcb(Tcl_Interp *interp, void *query_v) {
805 asynch_query_dispose(interp, query_v);
808 const IdDataSpec cht_adnstcl_queries= {
809 "adns", "adns-query-table", destroy_query_idtabcb
812 /*---------- main hooks for tcl ----------*/
814 CHT_INIT(adns, {}, CHTI_COMMANDS(cht_adnstoplevel_entries))