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 Resolver Resolver;
62 typedef struct OptionInfo OptionInfo;
63 static void asynch_check(Resolver *res);
64 static void asynch_sethandlers(Resolver *res, int shutdown);
66 /*---------- common resolver/query option processing ----------*/
69 /* this struct type is used to hold both resolver and query options */
70 /* common to resolver and query: */
76 const char *config_string;
79 const char *reverseany;
84 int (*fn)(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
87 unsigned long flags_add, flags_remove;
91 oisf_makedefault= 0x0001,
95 static int oiufn_f(const OptionInfo *oi, unsigned long *flags) {
96 *flags &= ~oi->flags_remove;
97 *flags |= oi->flags_add;
100 static int oifn_fa(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
101 OptionParse *op) { return oiufn_f(oi,&op->aflags); }
102 static int oifn_fs(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
103 OptionParse *op) { return oiufn_f(oi,&op->sflags); }
105 static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi,
106 Tcl_Obj *arg, OptionParse *op) {
107 return pat_string(ip,arg,&op->reverseany);
110 #define OIFA1(t,f,r) { "-" #f, oifn_fa, 0, adns_##t##_##f, r }
111 #define OIFA2(t,f,g) { "-" #f "-" #g, oifn_fa, 0, adns_##t##_##f##_##g, 0 }
112 #define OIFS(f) { "-" #f, oifn_fs, 0, oisf_##f, 0 }
113 #define OICA(o) { "-" #o, oifn_##o, 1 }
115 static int parse_options(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv,
116 const OptionInfo opttable[], OptionParse *op) {
117 const OptionInfo *oi;
125 rc= pat_enum(ip, *objv++, &oi_v, opttable, sizeof(OptionInfo),
126 "query or resolver option");
132 setstringresult(ip,"missing value for option");
139 rc= oi->fn(ip,oi,arg,op);
145 /*---------- resolver management ----------*/
147 IdDataTable adnstcl_resolvers= { "adns-res" };
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 int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
206 Resolver *res= res_v;
207 tabledataid_disposing(res,&adnstcl_resolvers);
208 if (res->ads) adns_finish(res->ads);
209 asynch_sethandlers(res,1);
211 /* fixme what about outstanding queries */
212 /* fixme what about the default resolver */
213 /* fixme what if the tcl interpreter gets deleted */
217 int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
218 int objc, Tcl_Obj *const *objv,
224 op.aflags= adns_if_noautosys;
229 rc= parse_options(ip,objc,objv,resolver_optioninfos,&op);
232 res= TALLOC(sizeof(*res)); assert(res);
238 for (i=0; i<3; i++) FD_ZERO(&res->handling[i]);
240 if (op.aflags & adns_if_noerrprint) {
245 ec= adns_init_logfn(&res->ads, op.aflags, op.config_string,
246 op.errcallback ? adnslogfn_callback : 0,
247 op.errcallback ? (void*)op.errcallback
248 : (void*)op.errfile);
249 if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; }
252 Tcl_IncrRefCount(op.errcallback);
259 if (res->ads) adns_finish(res->ads);
265 /*---------- query, query option and answers - common stuff ----------*/
267 #define RRTYPE_EXACTLY(t) { #t, adns_r_##t }
268 #define RRTYPE_RAW(t) { #t, adns_r_##t##_raw }
269 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
270 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
272 const AdnsTclRRTypeInfo adnstclrrtypeinfos[]= {
274 RRTYPE_EXACTLY(cname),
275 RRTYPE_EXACTLY(hinfo),
276 RRTYPE_EXACTLY(addr),
291 static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
292 Tcl_Obj *arg, OptionParse *op) {
296 rc= pat_iddata(ip,arg,&val_v,&adnstcl_resolvers);
302 static const OptionInfo query_optioninfos[]= {
305 OIFA2(qf,quoteok,query),
306 OIFA2(qf,quoteok,anshost),
307 OIFA2(qf,quotefail,cname),
308 OIFA2(qf,cname,loose),
309 OIFA2(qf,cname,forbid),
312 { "-reverse-any", oifn_reverse_any, 1 },
316 static int query_submit(Tcl_Interp *ip,
317 const AdnsTclRRTypeInfo *type, const char *domain,
318 int queryopts_objc, Tcl_Obj *const *queryopts_objv,
319 adns_query *aqu_r, OptionParse *op) {
321 static const int aftry[]= { AF_INET, AF_INET6 };
325 op->aflags= adns_qf_owner;
328 rc= parse_options(ip, queryopts_objc,queryopts_objv, query_optioninfos,op);
331 if (op->reverseany || (op->sflags & oisf_reverse)) {
333 for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
334 memset(&sa,0,sizeof(sa));
336 r= inet_pton(*af,domain,&sa);
337 if (!r) goto af_found;
339 return staticerr(ip,"invalid address for adns reverse submit","");
343 ads= op->resolver->ads;
345 if (op->reverseany) {
346 ec= adns_submit_reverse_any(ads, &sa, op->reverseany,
347 type->number, op->aflags, 0, aqu_r);
348 } else if (op->sflags & oisf_reverse) {
349 ec= adns_submit_reverse(ads, &sa,
350 type->number, op->aflags, 0, aqu_r);
352 ec= adns_submit(ads, domain,
353 type->number, op->aflags, 0, aqu_r);
356 return posixerr(ip,ec,"submit adns query");
361 #define RESULTSTATUS_LLEN 4
362 #define RESULTLIST_LLEN 7
364 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
365 Tcl_Obj *results[RESULTSTATUS_LLEN]) {
366 results[0]= ret_string(ip, adns_errtypeabbrev(status));
367 results[1]= ret_int(ip, status);
368 results[2]= ret_string(ip, adns_errabbrev(status));
369 results[3]= ret_string(ip, adns_strerror(status));
372 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
376 char *datap, *rdatastring;
378 rdata= TALLOC(sizeof(*rdata) * answer->nrrs);
379 for (i=0, datap=answer->rrs.untyped;
381 i++, datap += rrsz) {
382 st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
384 rdata[i]= ret_string(ip, rdatastring);
388 return Tcl_NewListObj(answer->nrrs, rdata);
391 static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
392 Tcl_Obj *results[RESULTLIST_LLEN]) {
394 make_resultstatus(ip, answer->status, results);
395 assert(RESULTSTATUS_LLEN==4);
396 results[4]= ret_string(ip, answer->owner);
397 results[5]= ret_string(ip, answer->cname ? answer->cname : "");
398 results[6]= make_resultrdata(ip, answer);
401 /*---------- synchronous query handling ----------*/
403 static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
405 int objc, Tcl_Obj *const *objv, adns_answer **answer_r) {
411 rc= query_submit(ip,rrtype,domain,objc,objv,&aqu,&op);
415 ec= adns_wait(res->ads,&aqu,answer_r,0);
422 int do_adns_lookup(ClientData cd, Tcl_Interp *ip,
423 const AdnsTclRRTypeInfo *rrtype,
425 int objc, Tcl_Obj *const *objv,
430 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
432 if (answer->status) {
433 Tcl_Obj *problem[RESULTSTATUS_LLEN];
434 make_resultstatus(ip, answer->status, problem);
435 *result= Tcl_NewListObj(RESULTSTATUS_LLEN, problem);
437 *result= make_resultrdata(ip, answer);
443 int do_adns_synch(ClientData cd, Tcl_Interp *ip,
444 const AdnsTclRRTypeInfo *rrtype,
446 int objc, Tcl_Obj *const *objv,
450 Tcl_Obj *results[RESULTLIST_LLEN];
452 rc= synch(ip,rrtype,domain,objc,objv,&answer); if (rc) return rc;
453 make_resultlist(ip,answer,results);
455 *result= Tcl_NewListObj(RESULTLIST_LLEN, results);
459 /*---------- asynchronous query handling ----------*/
465 ScriptToInvoke on_yes, on_no, on_fail;
469 IdDataTable adnstcl_queries= { "adns" };
471 static void asynch_timerhandler(void *res_v) {
472 Resolver *res= res_v;
474 adns_processtimeouts(res->ads,0);
478 static void asynch_filehandler(void *res_v, int mask) {
479 Resolver *res= res_v;
482 ec= adns_processany(res->ads);
483 if (ec) adns_globalsystemfailure(res->ads);
487 static void asynch_sethandlers(Resolver *res, int shutdown) {
490 struct timeval tv_buf, *timeout;
495 for (i=0; i<3; i++) FD_ZERO(&want[i]);
498 adns_beforeselect(res->ads,&maxfd,&want[0],&want[1],&want[2],
501 for (fd= 0; fd < maxfd || fd < res->maxfd; fd++)
503 if (!!FD_ISSET(fd, &res->handling[i])
504 != !!FD_ISSET(fd, &want[i])) {
506 if (FD_ISSET(fd, &want[0])) mask |= TCL_READABLE;
507 if (FD_ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
508 if (FD_ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
509 if (mask) Tcl_CreateFileHandler(fd,mask,asynch_filehandler,res);
510 else Tcl_DeleteFileHandler(fd);
513 Tcl_DeleteTimerHandler(res->timertoken);
518 if (timeout->tv_sec >= INT_MAX/1000 - 1)
519 milliseconds= INT_MAX;
521 milliseconds= timeout->tv_sec * 1000 +
522 (timeout->tv_usec + 999) / 1000;
525 Tcl_CreateTimerHandler(milliseconds,asynch_timerhandler,res);
529 static void asynch_query_dispose(Query *query) {
530 tabledataid_disposing(query, &adnstcl_queries);
531 scriptinv_cancel(&query->on_yes);
532 scriptinv_cancel(&query->on_no);
533 scriptinv_cancel(&query->on_fail);
534 if (query->xargs) Tcl_DecrRefCount(query->xargs);
535 if (query->aqu) adns_cancel(query->aqu);
539 static void asynch_check(Resolver *res) {
540 Tcl_Interp *interp= res->interp;
547 Tcl_Obj *results[RESULTLIST_LLEN];
550 ec= adns_check(res->ads, &aqu, &answer, &query_v);
551 if (ec==ESRCH || ec==EAGAIN) break;
556 tabledataid_disposing(query, &adnstcl_queries);
558 si= (!answer->status ? si= &query->on_yes
559 : answer->status > adns_s_max_tempfail ? &query->on_no
562 make_resultlist(interp, answer, results);
564 scriptinv_invoke(si, RESULTLIST_LLEN, results);
565 asynch_query_dispose(query);
568 asynch_sethandlers(res,0);
571 int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
572 Tcl_Obj *on_yes, Tcl_Obj *on_no,
573 Tcl_Obj *on_fail, Tcl_Obj *xargs,
574 const AdnsTclRRTypeInfo *rrtype, const char *domain,
575 int objc, Tcl_Obj *const *objv, void **result) {
581 query= TALLOC(sizeof(*query));
584 scriptinv_init(&query->on_yes);
585 scriptinv_init(&query->on_no);
586 scriptinv_init(&query->on_fail);
589 rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,&op);
594 rc= scriptinv_set(&query->on_yes, ip,on_yes); if (rc) goto x_rc;
595 rc= scriptinv_set(&query->on_no, ip,on_no); if (rc) goto x_rc;
596 rc= scriptinv_set(&query->on_fail,ip,on_fail); if (rc) goto x_rc;
598 Tcl_IncrRefCount(xargs);
604 if (query) asynch_query_dispose(query);
605 if (res) asynch_sethandlers(res,0);
609 int do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
610 Query *query= query_v;
611 Resolver *res= query->res;
613 asynch_query_dispose(query);
614 asynch_sethandlers(res,0);