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 * 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
50 * -reverse-any ZONE-A-LIKE
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);
291 asynch_cancelhandlers(res);
292 scriptinv_cancel(&res->errcallback);
296 static void destroy_resolver_idtabcb(Tcl_Interp *ip, void *resolver_v) {
297 destroy_resolver(ip,resolver_v);
299 static void destroy_resolver_defcb(ClientData resolver_v, Tcl_Interp *ip) {
300 destroy_resolver(ip,resolver_v);
303 int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
304 destroy_resolver(ip,res_v);
305 tabledataid_disposing(ip,res_v,&adnstcl_resolvers);
309 static int create_resolver(Tcl_Interp *ip, const OptionParse *op,
314 res= TALLOC(sizeof(*res)); assert(res);
320 for (i=0; i<3; i++) FD_ZERO(&res->handling[i]);
321 scriptinv_init(&res->errcallback);
322 res->errstring_accum= 0;
325 scriptinv_set(&res->errcallback, ip, op->errcallback, 0);
327 ec= adns_init_logfn(&res->ads,
328 op->aflags | adns_if_noautosys,
330 op->errcallback ? adnslogfn_callback : 0,
331 op->errcallback ? (void*)res : (void*)op->errfile);
332 if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; }
339 if (res->ads) adns_finish(res->ads);
345 int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
346 int objc, Tcl_Obj *const *objv,
353 rc= parse_options(ip,objc,objv,resolver_optioninfos,&op);
356 if (op.aflags & adns_if_noerrprint) {
361 rc= create_resolver(ip, &op, &res);
368 int do_adns_set_default_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
369 Resolver *res= res_v;
370 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
371 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, 0, res);
375 const IdDataSpec adnstcl_resolvers= {
376 "adns-res", "adns-resolvers-table", destroy_resolver_idtabcb
379 /*---------- query, query option and answers - common stuff ----------*/
381 #define RRTYPE_EXACTLY(t) { #t, adns_r_##t }
382 #define RRTYPE_RAW(t) { #t, adns_r_##t##_raw }
383 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
384 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
386 const AdnsTclRRTypeInfo adnstclrrtypeinfos[]= {
388 RRTYPE_EXACTLY(cname),
389 RRTYPE_EXACTLY(hinfo),
390 RRTYPE_EXACTLY(addr),
405 static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
406 Tcl_Obj *arg, OptionParse *op) {
410 rc= pat_iddata(ip,arg,&val_v,&adnstcl_resolvers);
416 static const OptionInfo query_optioninfos[]= {
419 OIFA2(qf,quoteok,query),
420 OIFA2(qf,quoteok,anshost),
421 OIFA2(qf,quotefail,cname),
422 OIFA2(qf,cname,loose),
423 OIFA2(qf,cname,forbid),
426 { "-reverse-any", oifn_reverse_any, 1 },
430 static int query_submit(Tcl_Interp *ip,
431 const AdnsTclRRTypeInfo *type, const char *domain,
432 int queryopts_objc, Tcl_Obj *const *queryopts_objv,
433 adns_query *aqu_r, void *context, OptionParse *op) {
435 static const int aftry[]= { AF_INET, AF_INET6 };
440 op->aflags= adns_qf_owner;
444 rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,op);
448 op->resolver= default_resolver(ip);
450 optparse_blank(&res_op);
451 rc= create_resolver(ip, &res_op, &op->resolver);
454 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES,
455 destroy_resolver_defcb, op->resolver);
459 if (op->reverseany || (op->sflags & oisf_reverse)) {
461 for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
462 memset(&sa,0,sizeof(sa));
464 r= inet_pton(*af,domain,&sa);
465 if (!r) goto af_found;
467 return staticerr(ip,"invalid address for adns reverse submit","");
471 ads= op->resolver->ads;
473 if (op->reverseany) {
474 ec= adns_submit_reverse_any(ads, &sa, op->reverseany,
475 type->number, op->aflags, context, aqu_r);
476 } else if (op->sflags & oisf_reverse) {
477 ec= adns_submit_reverse(ads, &sa,
478 type->number, op->aflags, context, aqu_r);
480 ec= adns_submit(ads, domain,
481 type->number, op->aflags, context, aqu_r);
484 return posixerr(ip,ec,"submit adns query");
489 #define RESULTSTATUS_LLEN 4
490 #define RESULTLIST_LLEN 7
492 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
493 Tcl_Obj *results[RESULTSTATUS_LLEN]) {
494 results[0]= ret_string(ip, adns_errtypeabbrev(status));
495 results[1]= ret_int(ip, status);
496 results[2]= ret_string(ip, adns_errabbrev(status));
497 results[3]= ret_string(ip, adns_strerror(status));
500 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
501 Tcl_Obj **rdata, *rl;
504 char *datap, *rdatastring;
506 rdata= TALLOC(sizeof(*rdata) * answer->nrrs);
507 for (i=0, datap=answer->rrs.untyped;
509 i++, datap += rrsz) {
510 st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
512 rdata[i]= ret_string(ip, rdatastring);
515 rl= Tcl_NewListObj(answer->nrrs, rdata);
520 static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
521 Tcl_Obj *results[RESULTLIST_LLEN]) {
523 make_resultstatus(ip, answer->status, results);
524 assert(RESULTSTATUS_LLEN==4);
525 results[4]= ret_string(ip, answer->owner);
526 results[5]= ret_string(ip, answer->cname ? answer->cname : "");
527 results[6]= make_resultrdata(ip, answer);
530 /*---------- synchronous query handling ----------*/
532 static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
534 int objc, Tcl_Obj *const *objv, adns_answer **answer_r) {
540 rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,0,&op);
544 ec= adns_wait(res->ads,&aqu,answer_r,0);
547 asynch_perturbed(res);
551 int do_adns_lookup(ClientData cd, Tcl_Interp *ip,
552 const AdnsTclRRTypeInfo *rrtype,
554 int objc, Tcl_Obj *const *objv,
559 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
561 if (answer->status) {
562 Tcl_Obj *problem[RESULTSTATUS_LLEN];
563 make_resultstatus(ip, answer->status, problem);
564 *result= Tcl_NewListObj(RESULTSTATUS_LLEN, problem);
566 *result= make_resultrdata(ip, answer);
572 int do_adns_synch(ClientData cd, Tcl_Interp *ip,
573 const AdnsTclRRTypeInfo *rrtype,
575 int objc, Tcl_Obj *const *objv,
579 Tcl_Obj *results[RESULTLIST_LLEN];
581 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
582 make_resultlist(ip,answer,results);
584 *result= Tcl_NewListObj(RESULTLIST_LLEN, results);
588 /*---------- asynchronous query handling ----------*/
594 ScriptToInvoke on_yes, on_no, on_fail;
598 static void asynch_check_now(Resolver *res);
600 static void asynch_timerhandler(void *res_v) {
601 Resolver *res= res_v;
603 adns_processtimeouts(res->ads,0);
604 asynch_check_now(res);
607 static void asynch_filehandler(void *res_v, int mask) {
608 Resolver *res= res_v;
611 ec= adns_processany(res->ads);
612 if (ec) adns_globalsystemfailure(res->ads);
613 asynch_check_now(res);
616 static void asynch_sethandlers_generic(Resolver *res,
617 int shutdown /*from _cancelhandlers*/,
618 int immediate /*from _perturbed*/) {
621 struct timeval tv_buf, *timeout;
626 for (i=0; i<3; i++) FD_ZERO(&want[i]);
629 adns_beforeselect(res->ads,&maxfd,&want[0],&want[1],&want[2],
632 for (fd= 0; fd < maxfd || fd < res->maxfd; fd++)
634 if (!!FD_ISSET(fd, &res->handling[i])
635 != !!FD_ISSET(fd, &want[i])) {
637 if (FD_ISSET(fd, &want[0])) mask |= TCL_READABLE;
638 if (FD_ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
639 if (FD_ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
640 if (mask) Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
641 else Tcl_DeleteFileHandler(fd);
644 Tcl_DeleteTimerHandler(res->timertoken);
647 res->timertoken= Tcl_CreateTimerHandler(0,asynch_timerhandler,res);
648 } else if (timeout) {
651 if (timeout->tv_sec >= INT_MAX/1000 - 1)
652 milliseconds= INT_MAX;
654 milliseconds= timeout->tv_sec * 1000 +
655 (timeout->tv_usec + 999) / 1000;
658 Tcl_CreateTimerHandler(milliseconds,asynch_timerhandler,res);
662 static void asynch_sethandlers(Resolver *res) {
663 asynch_sethandlers_generic(res,0,0);
665 static void asynch_cancelhandlers(Resolver *res) {
666 asynch_sethandlers_generic(res,1,0);
668 static void asynch_perturbed(Resolver *res) {
669 asynch_sethandlers_generic(res,0,1);
672 static void asynch_check_now(Resolver *res) {
673 Tcl_Interp *interp= res->interp;
680 Tcl_Obj *results[RESULTLIST_LLEN];
684 ec= adns_check(res->ads, &aqu, &answer, &query_v);
685 if (ec==ESRCH || ec==EAGAIN) break;
690 tabledataid_disposing(interp, query, &adnstcl_queries);
692 si= (!answer->status ? &query->on_yes
693 : answer->status > adns_s_max_tempfail ? &query->on_no
696 make_resultlist(interp, answer, results);
698 scriptinv_invoke(si, RESULTLIST_LLEN, results);
699 asynch_query_dispose(interp, query);
702 asynch_sethandlers(res);
705 int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
706 Tcl_Obj *on_yes, Tcl_Obj *on_no,
707 Tcl_Obj *on_fail, Tcl_Obj *xargs,
708 const AdnsTclRRTypeInfo *rrtype, const char *domain,
709 int objc, Tcl_Obj *const *objv, void **result) {
715 query= TALLOC(sizeof(*query));
718 scriptinv_init(&query->on_yes);
719 scriptinv_init(&query->on_no);
720 scriptinv_init(&query->on_fail);
723 rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&op);
728 rc= scriptinv_set(&query->on_yes, ip,on_yes, xargs); if (rc) goto x_rc;
729 rc= scriptinv_set(&query->on_no, ip,on_no, xargs); if (rc) goto x_rc;
730 rc= scriptinv_set(&query->on_fail,ip,on_fail,xargs); if (rc) goto x_rc;
732 Tcl_IncrRefCount(xargs);
734 query= 0; /* do not dispose */
738 if (query) asynch_query_dispose(ip, query);
739 if (res) asynch_perturbed(res);
743 int do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
744 Query *query= query_v;
745 Resolver *res= query->res;
746 asynch_query_dispose(ip, query);
747 asynch_perturbed(res);
751 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
752 tabledataid_disposing(interp, query, &adnstcl_queries);
753 scriptinv_cancel(&query->on_yes);
754 scriptinv_cancel(&query->on_no);
755 scriptinv_cancel(&query->on_fail);
756 if (query->xargs) Tcl_DecrRefCount(query->xargs);
757 if (query->aqu) adns_cancel(query->aqu);
761 static void destroy_query_idtabcb(Tcl_Interp *interp, void *query_v) {
762 asynch_query_dispose(interp, query_v);
765 const IdDataSpec adnstcl_queries= {
766 "adns", "adns-query-table", destroy_query_idtabcb