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 destroy-resolver RESOLVER
59 /*---------- important types and forward declarations ----------*/
61 typedef struct Query Query;
62 typedef struct Resolver Resolver;
63 typedef struct OptionInfo OptionInfo;
64 static void asynch_check(Resolver *res);
65 static void asynch_sethandlers(Resolver *res, int shutdown);
66 static void asynch_query_dispose(Tcl_Interp *interp, Query *query);
68 /*---------- common resolver/query option processing ----------*/
71 /* this struct type is used to hold both resolver and query options */
72 /* common to resolver and query: */
78 const char *config_string;
81 const char *reverseany;
86 int (*fn)(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
89 unsigned long flags_add, flags_remove;
93 oisf_makedefault= 0x0001,
97 static int oiufn_f(const OptionInfo *oi, unsigned long *flags) {
98 *flags &= ~oi->flags_remove;
99 *flags |= oi->flags_add;
102 static int oifn_fa(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
103 OptionParse *op) { return oiufn_f(oi,&op->aflags); }
104 static int oifn_fs(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
105 OptionParse *op) { return oiufn_f(oi,&op->sflags); }
107 static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi,
108 Tcl_Obj *arg, OptionParse *op) {
109 return pat_string(ip,arg,&op->reverseany);
112 #define OIFA1(t,f,r) { "-" #f, oifn_fa, 0, adns_##t##_##f, r }
113 #define OIFA2(t,f,g) { "-" #f "-" #g, oifn_fa, 0, adns_##t##_##f##_##g, 0 }
114 #define OIFS(f) { "-" #f, oifn_fs, 0, oisf_##f, 0 }
115 #define OICA(o) { "-" #o, oifn_##o, 1 }
117 static int parse_options(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv,
118 const OptionInfo opttable[], OptionParse *op) {
119 const OptionInfo *oi;
127 rc= pat_enum(ip, *objv++, &oi_v, opttable, sizeof(OptionInfo),
128 "query or resolver option");
134 setstringresult(ip,"missing value for option");
141 rc= oi->fn(ip,oi,arg,op);
147 /*---------- resolver management ----------*/
153 Tcl_TimerToken timertoken;
158 static int oifn_errfile(Tcl_Interp *ip, const OptionInfo *oi,
159 Tcl_Obj *arg, OptionParse *op) {
163 rc= pat_string(ip,arg,&str); if (rc) return rc;
164 if (!strcmp(str,"stderr")) op->errfile= stderr;
165 else if (!strcmp(str,"stdout")) op->errfile= stdout;
166 else return staticerr(ip,"-errfile argument must be stderr or stdout",0);
168 op->aflags &= ~adns_if_noerrprint;
173 static int oifn_errcallback(Tcl_Interp *ip, const OptionInfo *oi,
174 Tcl_Obj *arg, OptionParse *op) {
175 op->errcallback= arg;
176 op->aflags &= ~adns_if_noerrprint;
181 static int oifn_config(Tcl_Interp *ip, const OptionInfo *oi,
182 Tcl_Obj *arg, OptionParse *op) {
183 return pat_string(ip,arg,&op->config_string);
186 static const OptionInfo resolver_optioninfos[]= {
188 OIFA1(if,debug, adns_if_noerrprint),
189 OIFA1(if,logpid, adns_if_noerrprint),
190 OIFA1(if,noerrprint, adns_if_debug),
191 OIFA2(if,checkc,entex),
192 OIFA2(if,checkc,freq),
200 static void adnslogfn_callback(adns_state ads, void *logfndata,
201 const char *fmt, va_list al) {
202 abort(); /* fixme implement adns_logcallbackfn */
205 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
210 /* although adns would throw these away for us, we need to
211 * destroy our own data too and only adns has a list of them */
213 adns_forallqueries_begin(res->ads);
214 aqu= adns_forallqueries_next(res->ads, &query_v);
216 asynch_query_dispose(ip, query_v);
218 adns_finish(res->ads);
220 asynch_sethandlers(res,1);
222 /* fixme what about the default resolver */
225 static void destroy_resolver_idtabcb(Tcl_Interp *ip, void *resolver_v) {
226 destroy_resolver(ip,resolver_v);
229 int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
230 destroy_resolver(ip,res_v);
231 tabledataid_disposing(ip,res_v,&adnstcl_resolvers);
235 int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
236 int objc, Tcl_Obj *const *objv,
242 op.aflags= adns_if_noautosys;
247 rc= parse_options(ip,objc,objv,resolver_optioninfos,&op);
250 res= TALLOC(sizeof(*res)); assert(res);
256 for (i=0; i<3; i++) FD_ZERO(&res->handling[i]);
258 if (op.aflags & adns_if_noerrprint) {
263 ec= adns_init_logfn(&res->ads, op.aflags, op.config_string,
264 op.errcallback ? adnslogfn_callback : 0,
265 op.errcallback ? (void*)op.errcallback
266 : (void*)op.errfile);
267 if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; }
270 Tcl_IncrRefCount(op.errcallback);
277 if (res->ads) adns_finish(res->ads);
283 const IdDataSpec adnstcl_resolvers= {
284 "adns-res", "adns-resolvers-table", destroy_resolver_idtabcb
287 /*---------- query, query option and answers - common stuff ----------*/
289 #define RRTYPE_EXACTLY(t) { #t, adns_r_##t }
290 #define RRTYPE_RAW(t) { #t, adns_r_##t##_raw }
291 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
292 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
294 const AdnsTclRRTypeInfo adnstclrrtypeinfos[]= {
296 RRTYPE_EXACTLY(cname),
297 RRTYPE_EXACTLY(hinfo),
298 RRTYPE_EXACTLY(addr),
313 static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
314 Tcl_Obj *arg, OptionParse *op) {
318 rc= pat_iddata(ip,arg,&val_v,&adnstcl_resolvers);
324 static const OptionInfo query_optioninfos[]= {
327 OIFA2(qf,quoteok,query),
328 OIFA2(qf,quoteok,anshost),
329 OIFA2(qf,quotefail,cname),
330 OIFA2(qf,cname,loose),
331 OIFA2(qf,cname,forbid),
334 { "-reverse-any", oifn_reverse_any, 1 },
338 static int query_submit(Tcl_Interp *ip,
339 const AdnsTclRRTypeInfo *type, const char *domain,
340 int queryopts_objc, Tcl_Obj *const *queryopts_objv,
341 adns_query *aqu_r, void *context, OptionParse *op) {
343 static const int aftry[]= { AF_INET, AF_INET6 };
347 op->aflags= adns_qf_owner;
349 op->resolver= 0; /* fixme default */
351 rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,op);
354 if (op->reverseany || (op->sflags & oisf_reverse)) {
356 for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
357 memset(&sa,0,sizeof(sa));
359 r= inet_pton(*af,domain,&sa);
360 if (!r) goto af_found;
362 return staticerr(ip,"invalid address for adns reverse submit","");
366 ads= op->resolver->ads;
368 if (op->reverseany) {
369 ec= adns_submit_reverse_any(ads, &sa, op->reverseany,
370 type->number, op->aflags, context, aqu_r);
371 } else if (op->sflags & oisf_reverse) {
372 ec= adns_submit_reverse(ads, &sa,
373 type->number, op->aflags, context, aqu_r);
375 ec= adns_submit(ads, domain,
376 type->number, op->aflags, context, aqu_r);
379 return posixerr(ip,ec,"submit adns query");
384 #define RESULTSTATUS_LLEN 4
385 #define RESULTLIST_LLEN 7
387 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
388 Tcl_Obj *results[RESULTSTATUS_LLEN]) {
389 results[0]= ret_string(ip, adns_errtypeabbrev(status));
390 results[1]= ret_int(ip, status);
391 results[2]= ret_string(ip, adns_errabbrev(status));
392 results[3]= ret_string(ip, adns_strerror(status));
395 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
396 Tcl_Obj **rdata, *rl;
399 char *datap, *rdatastring;
401 rdata= TALLOC(sizeof(*rdata) * answer->nrrs);
402 for (i=0, datap=answer->rrs.untyped;
404 i++, datap += rrsz) {
405 st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
407 rdata[i]= ret_string(ip, rdatastring);
410 rl= Tcl_NewListObj(answer->nrrs, rdata);
415 static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
416 Tcl_Obj *results[RESULTLIST_LLEN]) {
418 make_resultstatus(ip, answer->status, results);
419 assert(RESULTSTATUS_LLEN==4);
420 results[4]= ret_string(ip, answer->owner);
421 results[5]= ret_string(ip, answer->cname ? answer->cname : "");
422 results[6]= make_resultrdata(ip, answer);
425 /*---------- synchronous query handling ----------*/
427 static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
429 int objc, Tcl_Obj *const *objv, adns_answer **answer_r) {
435 rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,0,&op);
439 ec= adns_wait(res->ads,&aqu,answer_r,0);
446 int do_adns_lookup(ClientData cd, Tcl_Interp *ip,
447 const AdnsTclRRTypeInfo *rrtype,
449 int objc, Tcl_Obj *const *objv,
454 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
456 if (answer->status) {
457 Tcl_Obj *problem[RESULTSTATUS_LLEN];
458 make_resultstatus(ip, answer->status, problem);
459 *result= Tcl_NewListObj(RESULTSTATUS_LLEN, problem);
461 *result= make_resultrdata(ip, answer);
467 int do_adns_synch(ClientData cd, Tcl_Interp *ip,
468 const AdnsTclRRTypeInfo *rrtype,
470 int objc, Tcl_Obj *const *objv,
474 Tcl_Obj *results[RESULTLIST_LLEN];
476 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
477 make_resultlist(ip,answer,results);
479 *result= Tcl_NewListObj(RESULTLIST_LLEN, results);
483 /*---------- asynchronous query handling ----------*/
489 ScriptToInvoke on_yes, on_no, on_fail;
493 static void asynch_timerhandler(void *res_v) {
494 Resolver *res= res_v;
496 adns_processtimeouts(res->ads,0);
500 static void asynch_filehandler(void *res_v, int mask) {
501 Resolver *res= res_v;
504 ec= adns_processany(res->ads);
505 if (ec) adns_globalsystemfailure(res->ads);
509 static void asynch_sethandlers(Resolver *res, int shutdown) {
512 struct timeval tv_buf, *timeout;
517 for (i=0; i<3; i++) FD_ZERO(&want[i]);
520 adns_beforeselect(res->ads,&maxfd,&want[0],&want[1],&want[2],
523 for (fd= 0; fd < maxfd || fd < res->maxfd; fd++)
525 if (!!FD_ISSET(fd, &res->handling[i])
526 != !!FD_ISSET(fd, &want[i])) {
528 if (FD_ISSET(fd, &want[0])) mask |= TCL_READABLE;
529 if (FD_ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
530 if (FD_ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
531 if (mask) Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
532 else Tcl_DeleteFileHandler(fd);
535 Tcl_DeleteTimerHandler(res->timertoken);
540 if (timeout->tv_sec >= INT_MAX/1000 - 1)
541 milliseconds= INT_MAX;
543 milliseconds= timeout->tv_sec * 1000 +
544 (timeout->tv_usec + 999) / 1000;
547 Tcl_CreateTimerHandler(milliseconds,asynch_timerhandler,res);
551 static void asynch_check(Resolver *res) {
552 Tcl_Interp *interp= res->interp;
559 Tcl_Obj *results[RESULTLIST_LLEN];
563 ec= adns_check(res->ads, &aqu, &answer, &query_v);
564 if (ec==ESRCH || ec==EAGAIN) break;
569 tabledataid_disposing(interp, query, &adnstcl_queries);
571 si= (!answer->status ? &query->on_yes
572 : answer->status > adns_s_max_tempfail ? &query->on_no
575 make_resultlist(interp, answer, results);
577 scriptinv_invoke(si, RESULTLIST_LLEN, results);
578 asynch_query_dispose(interp, query);
581 asynch_sethandlers(res,0);
584 int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
585 Tcl_Obj *on_yes, Tcl_Obj *on_no,
586 Tcl_Obj *on_fail, Tcl_Obj *xargs,
587 const AdnsTclRRTypeInfo *rrtype, const char *domain,
588 int objc, Tcl_Obj *const *objv, void **result) {
594 query= TALLOC(sizeof(*query));
597 scriptinv_init(&query->on_yes);
598 scriptinv_init(&query->on_no);
599 scriptinv_init(&query->on_fail);
602 rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&op);
607 rc= scriptinv_set(&query->on_yes, ip,on_yes); if (rc) goto x_rc;
608 rc= scriptinv_set(&query->on_no, ip,on_no); if (rc) goto x_rc;
609 rc= scriptinv_set(&query->on_fail,ip,on_fail); if (rc) goto x_rc;
611 Tcl_IncrRefCount(xargs);
613 query= 0; /* do not dispose */
617 if (query) asynch_query_dispose(ip, query);
618 if (res) asynch_sethandlers(res,0);
622 int do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
623 Query *query= query_v;
624 Resolver *res= query->res;
625 asynch_query_dispose(ip, query);
626 asynch_sethandlers(res,0);
630 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
631 tabledataid_disposing(interp, query, &adnstcl_queries);
632 scriptinv_cancel(&query->on_yes);
633 scriptinv_cancel(&query->on_no);
634 scriptinv_cancel(&query->on_fail);
635 if (query->xargs) Tcl_DecrRefCount(query->xargs);
636 if (query->aqu) adns_cancel(query->aqu);
640 static void destroy_query_idtabcb(Tcl_Interp *interp, void *query_v) {
641 asynch_query_dispose(interp, query_v);
644 const IdDataSpec adnstcl_queries= {
645 "adns", "adns-query-table", destroy_query_idtabcb