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
64 /*---------- important types and forward declarations ----------*/
66 typedef struct Query Query;
67 typedef struct Resolver Resolver;
68 typedef struct OptionInfo OptionInfo;
70 static void asynch_sethandlers(Resolver *res);
71 static void asynch_cancelhandlers(Resolver *res);
72 static void asynch_perturbed(Resolver *res);
74 static void asynch_query_dispose(Tcl_Interp *interp, Query *query);
76 #define ASSOC_DEFAULTRES "adns-defaultresolver"
78 /*---------- common resolver/query option processing ----------*/
81 /* this struct type is used to hold both resolver and query options */
82 /* common to resolver and query: */
88 const char *config_string;
91 const char *reverseany;
96 int (*fn)(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
99 unsigned long flags_add, flags_remove;
106 static int oiufn_f(const OptionInfo *oi, unsigned long *flags) {
107 *flags &= ~oi->flags_remove;
108 *flags |= oi->flags_add;
111 static int oifn_fa(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
112 OptionParse *op) { return oiufn_f(oi,&op->aflags); }
113 static int oifn_fs(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
114 OptionParse *op) { return oiufn_f(oi,&op->sflags); }
116 static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi,
117 Tcl_Obj *arg, OptionParse *op) {
118 return pat_string(ip,arg,&op->reverseany);
121 #define OIFA1(t,f,r) { "-" #f, oifn_fa, 0, adns_##t##_##f, r }
122 #define OIFA2(t,f,g) { "-" #f "-" #g, oifn_fa, 0, adns_##t##_##f##_##g, 0 }
123 #define OIFS(f) { "-" #f, oifn_fs, 0, oisf_##f, 0 }
124 #define OICA(o) { "-" #o, oifn_##o, 1 }
126 static void optparse_blank(OptionParse *op) {
127 memset(op,0,sizeof(*op));
130 op->config_string= 0;
133 static int parse_options(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv,
134 const OptionInfo opttable[], OptionParse *op) {
135 const OptionInfo *oi;
143 rc= pat_enum(ip, *objv++, &oi_v, opttable, sizeof(OptionInfo),
144 "query or resolver option");
150 setstringresult(ip,"missing value for option");
157 rc= oi->fn(ip,oi,arg,op);
163 /*---------- resolver management ----------*/
169 Tcl_TimerToken timertoken;
172 ScriptToInvoke errcallback;
175 /* The default resolver is recorded using Tcl_SetAssocData with key
176 * ASSOC_DEFAULTRES to record the Resolver*. If it was explicitly
177 * created with `adns new-resolver' then ix will be >=0, and the
178 * resolver will be destroyed - leaving no default - when explicitly
179 * requested. If it was implicitly created (by starting a query when
180 * there is no default) then ix will be -1. */
182 static int oifn_errfile(Tcl_Interp *ip, const OptionInfo *oi,
183 Tcl_Obj *arg, OptionParse *op) {
187 rc= pat_string(ip,arg,&str); if (rc) return rc;
188 if (!strcmp(str,"stderr")) op->errfile= stderr;
189 else if (!strcmp(str,"stdout")) op->errfile= stdout;
190 else return staticerr(ip,"-errfile argument must be stderr or stdout",0);
192 op->aflags &= ~adns_if_noerrprint;
197 static int oifn_errcallback(Tcl_Interp *ip, const OptionInfo *oi,
198 Tcl_Obj *arg, OptionParse *op) {
199 op->errcallback= arg;
200 op->aflags &= ~adns_if_noerrprint;
205 static int oifn_config(Tcl_Interp *ip, const OptionInfo *oi,
206 Tcl_Obj *arg, OptionParse *op) {
207 return pat_string(ip,arg,&op->config_string);
210 static const OptionInfo resolver_optioninfos[]= {
212 OIFA1(if,debug, adns_if_noerrprint),
213 OIFA1(if,logpid, adns_if_noerrprint),
214 OIFA1(if,noerrprint, adns_if_debug),
215 OIFA2(if,checkc,entex),
216 OIFA2(if,checkc,freq),
223 static void adnslogfn_callback(adns_state ads, void *logfndata,
224 const char *fmt, va_list al) {
225 abort(); /* fixme implement adns_logcallbackfn */
228 static Resolver *default_resolver(Tcl_Interp *ip) {
229 return Tcl_GetAssocData(ip,ASSOC_DEFAULTRES,0);
232 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
236 if (res == default_resolver(ip))
237 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
240 /* although adns would throw these away for us, we need to
241 * destroy our own data too and only adns has a list of them */
243 adns_forallqueries_begin(res->ads);
244 aqu= adns_forallqueries_next(res->ads, &query_v);
246 asynch_query_dispose(ip, query_v);
248 adns_finish(res->ads);
250 asynch_cancelhandlers(res);
254 static void destroy_resolver_idtabcb(Tcl_Interp *ip, void *resolver_v) {
255 destroy_resolver(ip,resolver_v);
257 static void destroy_resolver_defcb(ClientData resolver_v, Tcl_Interp *ip) {
258 destroy_resolver(ip,resolver_v);
261 int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
262 destroy_resolver(ip,res_v);
263 tabledataid_disposing(ip,res_v,&adnstcl_resolvers);
267 static int create_resolver(Tcl_Interp *ip, const OptionParse *op,
272 res= TALLOC(sizeof(*res)); assert(res);
278 scriptinv_init(&res->errcallback);
279 for (i=0; i<3; i++) FD_ZERO(&res->handling[i]);
281 ec= adns_init_logfn(&res->ads,
282 op->aflags | adns_if_noautosys,
284 op->errcallback ? adnslogfn_callback : 0,
285 op->errcallback ? (void*)op->errcallback
286 : (void*)op->errfile);
287 if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; }
290 scriptinv_set(&res->errcallback, ip, op->errcallback, 0);
297 if (res->ads) adns_finish(res->ads);
303 int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
304 int objc, Tcl_Obj *const *objv,
311 rc= parse_options(ip,objc,objv,resolver_optioninfos,&op);
314 if (op.aflags & adns_if_noerrprint) {
319 rc= create_resolver(ip, &op, &res);
326 int do_adns_set_default_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
327 Resolver *res= res_v;
328 Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
329 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, 0, res);
333 const IdDataSpec adnstcl_resolvers= {
334 "adns-res", "adns-resolvers-table", destroy_resolver_idtabcb
337 /*---------- query, query option and answers - common stuff ----------*/
339 #define RRTYPE_EXACTLY(t) { #t, adns_r_##t }
340 #define RRTYPE_RAW(t) { #t, adns_r_##t##_raw }
341 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
342 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
344 const AdnsTclRRTypeInfo adnstclrrtypeinfos[]= {
346 RRTYPE_EXACTLY(cname),
347 RRTYPE_EXACTLY(hinfo),
348 RRTYPE_EXACTLY(addr),
363 static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
364 Tcl_Obj *arg, OptionParse *op) {
368 rc= pat_iddata(ip,arg,&val_v,&adnstcl_resolvers);
374 static const OptionInfo query_optioninfos[]= {
377 OIFA2(qf,quoteok,query),
378 OIFA2(qf,quoteok,anshost),
379 OIFA2(qf,quotefail,cname),
380 OIFA2(qf,cname,loose),
381 OIFA2(qf,cname,forbid),
384 { "-reverse-any", oifn_reverse_any, 1 },
388 static int query_submit(Tcl_Interp *ip,
389 const AdnsTclRRTypeInfo *type, const char *domain,
390 int queryopts_objc, Tcl_Obj *const *queryopts_objv,
391 adns_query *aqu_r, void *context, OptionParse *op) {
393 static const int aftry[]= { AF_INET, AF_INET6 };
398 op->aflags= adns_qf_owner;
402 rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,op);
406 op->resolver= default_resolver(ip);
408 optparse_blank(&res_op);
409 rc= create_resolver(ip, &res_op, &op->resolver);
412 Tcl_SetAssocData(ip, ASSOC_DEFAULTRES,
413 destroy_resolver_defcb, op->resolver);
417 if (op->reverseany || (op->sflags & oisf_reverse)) {
419 for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
420 memset(&sa,0,sizeof(sa));
422 r= inet_pton(*af,domain,&sa);
423 if (!r) goto af_found;
425 return staticerr(ip,"invalid address for adns reverse submit","");
429 ads= op->resolver->ads;
431 if (op->reverseany) {
432 ec= adns_submit_reverse_any(ads, &sa, op->reverseany,
433 type->number, op->aflags, context, aqu_r);
434 } else if (op->sflags & oisf_reverse) {
435 ec= adns_submit_reverse(ads, &sa,
436 type->number, op->aflags, context, aqu_r);
438 ec= adns_submit(ads, domain,
439 type->number, op->aflags, context, aqu_r);
442 return posixerr(ip,ec,"submit adns query");
447 #define RESULTSTATUS_LLEN 4
448 #define RESULTLIST_LLEN 7
450 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
451 Tcl_Obj *results[RESULTSTATUS_LLEN]) {
452 results[0]= ret_string(ip, adns_errtypeabbrev(status));
453 results[1]= ret_int(ip, status);
454 results[2]= ret_string(ip, adns_errabbrev(status));
455 results[3]= ret_string(ip, adns_strerror(status));
458 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
459 Tcl_Obj **rdata, *rl;
462 char *datap, *rdatastring;
464 rdata= TALLOC(sizeof(*rdata) * answer->nrrs);
465 for (i=0, datap=answer->rrs.untyped;
467 i++, datap += rrsz) {
468 st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
470 rdata[i]= ret_string(ip, rdatastring);
473 rl= Tcl_NewListObj(answer->nrrs, rdata);
478 static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
479 Tcl_Obj *results[RESULTLIST_LLEN]) {
481 make_resultstatus(ip, answer->status, results);
482 assert(RESULTSTATUS_LLEN==4);
483 results[4]= ret_string(ip, answer->owner);
484 results[5]= ret_string(ip, answer->cname ? answer->cname : "");
485 results[6]= make_resultrdata(ip, answer);
488 /*---------- synchronous query handling ----------*/
490 static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
492 int objc, Tcl_Obj *const *objv, adns_answer **answer_r) {
498 rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,0,&op);
502 ec= adns_wait(res->ads,&aqu,answer_r,0);
505 asynch_perturbed(res);
509 int do_adns_lookup(ClientData cd, Tcl_Interp *ip,
510 const AdnsTclRRTypeInfo *rrtype,
512 int objc, Tcl_Obj *const *objv,
517 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
519 if (answer->status) {
520 Tcl_Obj *problem[RESULTSTATUS_LLEN];
521 make_resultstatus(ip, answer->status, problem);
522 *result= Tcl_NewListObj(RESULTSTATUS_LLEN, problem);
524 *result= make_resultrdata(ip, answer);
530 int do_adns_synch(ClientData cd, Tcl_Interp *ip,
531 const AdnsTclRRTypeInfo *rrtype,
533 int objc, Tcl_Obj *const *objv,
537 Tcl_Obj *results[RESULTLIST_LLEN];
539 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
540 make_resultlist(ip,answer,results);
542 *result= Tcl_NewListObj(RESULTLIST_LLEN, results);
546 /*---------- asynchronous query handling ----------*/
552 ScriptToInvoke on_yes, on_no, on_fail;
556 static void asynch_check_now(Resolver *res);
558 static void asynch_timerhandler(void *res_v) {
559 Resolver *res= res_v;
561 adns_processtimeouts(res->ads,0);
562 asynch_check_now(res);
565 static void asynch_filehandler(void *res_v, int mask) {
566 Resolver *res= res_v;
569 ec= adns_processany(res->ads);
570 if (ec) adns_globalsystemfailure(res->ads);
571 asynch_check_now(res);
574 static void asynch_sethandlers_generic(Resolver *res,
575 int shutdown /*from _cancelhandlers*/,
576 int immediate /*from _perturbed*/) {
579 struct timeval tv_buf, *timeout;
584 for (i=0; i<3; i++) FD_ZERO(&want[i]);
587 adns_beforeselect(res->ads,&maxfd,&want[0],&want[1],&want[2],
590 for (fd= 0; fd < maxfd || fd < res->maxfd; fd++)
592 if (!!FD_ISSET(fd, &res->handling[i])
593 != !!FD_ISSET(fd, &want[i])) {
595 if (FD_ISSET(fd, &want[0])) mask |= TCL_READABLE;
596 if (FD_ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
597 if (FD_ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
598 if (mask) Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
599 else Tcl_DeleteFileHandler(fd);
602 Tcl_DeleteTimerHandler(res->timertoken);
605 res->timertoken= Tcl_CreateTimerHandler(0,asynch_timerhandler,res);
606 } else if (timeout) {
609 if (timeout->tv_sec >= INT_MAX/1000 - 1)
610 milliseconds= INT_MAX;
612 milliseconds= timeout->tv_sec * 1000 +
613 (timeout->tv_usec + 999) / 1000;
616 Tcl_CreateTimerHandler(milliseconds,asynch_timerhandler,res);
620 static void asynch_sethandlers(Resolver *res) {
621 asynch_sethandlers_generic(res,0,0);
623 static void asynch_cancelhandlers(Resolver *res) {
624 asynch_sethandlers_generic(res,1,0);
626 static void asynch_perturbed(Resolver *res) {
627 asynch_sethandlers_generic(res,0,1);
630 static void asynch_check_now(Resolver *res) {
631 Tcl_Interp *interp= res->interp;
638 Tcl_Obj *results[RESULTLIST_LLEN];
642 ec= adns_check(res->ads, &aqu, &answer, &query_v);
643 if (ec==ESRCH || ec==EAGAIN) break;
648 tabledataid_disposing(interp, query, &adnstcl_queries);
650 si= (!answer->status ? &query->on_yes
651 : answer->status > adns_s_max_tempfail ? &query->on_no
654 make_resultlist(interp, answer, results);
656 scriptinv_invoke(si, RESULTLIST_LLEN, results);
657 asynch_query_dispose(interp, query);
660 asynch_sethandlers(res);
663 int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
664 Tcl_Obj *on_yes, Tcl_Obj *on_no,
665 Tcl_Obj *on_fail, Tcl_Obj *xargs,
666 const AdnsTclRRTypeInfo *rrtype, const char *domain,
667 int objc, Tcl_Obj *const *objv, void **result) {
673 query= TALLOC(sizeof(*query));
676 scriptinv_init(&query->on_yes);
677 scriptinv_init(&query->on_no);
678 scriptinv_init(&query->on_fail);
681 rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&op);
686 rc= scriptinv_set(&query->on_yes, ip,on_yes, xargs); if (rc) goto x_rc;
687 rc= scriptinv_set(&query->on_no, ip,on_no, xargs); if (rc) goto x_rc;
688 rc= scriptinv_set(&query->on_fail,ip,on_fail,xargs); if (rc) goto x_rc;
690 Tcl_IncrRefCount(xargs);
692 query= 0; /* do not dispose */
696 if (query) asynch_query_dispose(ip, query);
697 if (res) asynch_perturbed(res);
701 int do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
702 Query *query= query_v;
703 Resolver *res= query->res;
704 asynch_query_dispose(ip, query);
705 asynch_perturbed(res);
709 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
710 tabledataid_disposing(interp, query, &adnstcl_queries);
711 scriptinv_cancel(&query->on_yes);
712 scriptinv_cancel(&query->on_no);
713 scriptinv_cancel(&query->on_fail);
714 if (query->xargs) Tcl_DecrRefCount(query->xargs);
715 if (query->aqu) adns_cancel(query->aqu);
719 static void destroy_query_idtabcb(Tcl_Interp *interp, void *query_v) {
720 asynch_query_dispose(interp, query_v);
723 const IdDataSpec adnstcl_queries= {
724 "adns", "adns-query-table", destroy_query_idtabcb