4 * adns lookup TYPE DOMAIN [QUERY-OPTIONS] => [list RDATA]
5 * if no or dontknow, throws an exception, with errorCode one of
6 * ADNS NO 300 nxdomain {No such domain}
7 * ADNS NO 301 nodata {No such data}
8 * ADNS FAILED 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 IdDataTable adnstcl_resolvers= { "adns-res" };
60 IdDataTable adnstcl_queries= { "adns-q" };
64 Tcl_Interp *interp; fixme what about interpreter deletion
73 Tcl_Obj *on_yes, *on_no, *on_fail, *xargs;
76 static adns_state *adnstcl_default;
77 static int default_auto;
79 #define RRTYPE_EXACTLY(t) { #t, adns_r_##t }
80 #define RRTYPE_RAW(t) { #t, adns_r_##t##_raw }
81 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
82 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
84 const AdnsTclRRTypeInfo adnstclrrtypeinfos[]= {
86 RRTYPE_EXACTLY(cname),
87 RRTYPE_EXACTLY(hinfo),
104 /* this struct type is used to hold both resolver and query options */
105 /* common to resolver and query: */
106 unsigned long *aflags;
107 unsigned long *sflags;
110 Tcl_Obj *errcallback;
111 const char *config_string;
113 adns_state *resolver;
114 const char *reverseany;
117 typedef struct OptionInfo OptionInfo;
120 int (*fn)(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
123 unsigned long flags_add, flags_remove;
127 oisf_makedefault= 0x0001,
131 static int oiufn_f(const OptionInfo *oi, unsigned long *flags) {
132 flags &= ~oi->flags_remove;
133 flags |= oi->flsgs_add;
135 static int oifn_fa(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
136 OptionParse *op) { oiufn_f(oi,op->aflags); }
137 static int oifn_fs(Tcl_Interp *ip, const OptionInfo *oi, Tcl_Obj *arg,
138 OptionParse *op) { oiufn_f(oi,op->sflags); }
140 static int oifn_errfile(Tcl_Interp *ip, const OptionInfo *oi,
141 Tcl_Obj *arg, OptionParse *op) {
145 rc= pat_string(ip,arg,&str); if (rc) return rc;
146 if (!strcmp(str,"stderr")) op.errfile= stderr;
147 else if (!strcmp(str,"stdout")) op.errfile= stdout;
148 else return staticerr(ip,"-errfile argument must be stderr or stdout",0);
150 op.aflags &= ~adns_if_noerrprint;
155 static int oifn_errcallback(Tcl_Interp *ip, const OptionInfo *oi,
156 Tcl_Obj *arg, OptionParse *op) {
158 op.aflags &= ~adns_if_noeerpring;
163 static int oifn_config(Tcl_Interp *ip, const OptionInfo *oi,
164 Tcl_Obj *arg, OptionParse *op) {
165 return pat_string(ip,arg,&op.config_string);
168 static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
169 Tcl_Obj *arg, OptionParse *op) {
171 rc= pat_iddata(ip,arg,&val_v,adnstcl_resolvers);
176 static int oifn_reverse_any(Tcl_Interp *ip, const OptionInfo *oi,
177 Tcl_Obj *arg, OptionParse *op) {
178 return pat_string(ip,arg,&op.reverseany);
181 #define OIFA1(t,f,r) { "-" #f, oifn_fa, 0, adns_##t##_##f, r }
182 #define OIFA2(t,f,g) { "-" #f "-" #g, oifn_fa, 0, adns_##t##_##f##_##g, 0 }
183 #define OIFS(f) { "-" #f, oifn_fs, 0, oisf_##f, 0 }
184 #define OICA(o) { "-" #o, oifn_##o, 1 }
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 const OptionInfo query_optioninfos[]= {
203 OIFA2(if,quoteok,query),
204 OIFA2(if,quoteok,anshost),
205 OIFA2(if,quotefail,cname),
206 OIFA2(if,cname,loose),
207 OIFA2(if,cname,forbid),
210 { "-reverse-any", oifn_reverse_any, 1 },
214 static int parse_options(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv,
215 const OptionInfo opttable[], OptionParse *op) {
216 const OptionInfo *oi;
222 rc= pat_enum(ip,*objv++, &oi,opttable, sizeof(OptionInfo));
227 setstringresult(ip,"missing value for option");
234 rc= oi->fn(ip,oi,arg,op);
240 int do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
241 adns_state *res= res_v;
243 if (res == adnstcl_default) {
247 adns_finish(res); fixme what about outstanding qs
251 static void asynch_check(Resolver *res);
253 static void asynch_timerhandler(void *res_v) {
254 Resolver *res= res_v;
256 adns_processtimeouts(res->ads,0);
257 checkqueries_updatehandlers(res);
260 static void asynch_filehandler(void *res_v, int mask) {
261 Resolver *res= res_v;
262 ec= adns_processany(res->ads);
263 if (ec) adns_globalsystemfailure(res->ads);
264 checkqueries_updatehandlers(res);
267 static void asynch_sethandlers(Resolver *res, int shutdown) {
270 struct timeval tvbuf, *timeout;
272 for (i=0; i<3; i++) FD_ZERO(&want[i]);
277 adns_beforeselect(res->ads,&maxfd,&want[0],&want[1],&want[2],
280 for (fd= 0; fd < maxfd || fd < res->maxfd; fd++)
282 if (!!FD_ISSET(fd, &res->handling[i])
283 != !!FD_ISSET(fd, &want[i])) {
285 if (FD->ISSET(fd, &want[0])) mask |= TCL_READABLE;
286 if (FD->ISSET(fd, &want[1])) mask |= TCL_WRITABLE;
287 if (FD->ISSET(fd, &want[2])) mask |= TCL_EXCEPTION;
288 if (mask) Tcl_CreateFileHandler(fd,mask,filehandler,res);
289 else Tcl_DeleteFileHandler(fd);
292 Tcl_DeleteTimerHandler(res->timertoken);
297 if (timeout->tv_sec >= INT_MAX/1000 - 1)
298 milliseconds= INT_MAX;
300 milliseconds= timeout->tv_sec * 1000 +
301 (timeout->tv_usec + 999) / 1000;
303 res->timertoken= Tcl_CreateTimerHandler(milliseconds,timerhandler,res);
307 int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
308 int objc, Tcl_Obj *const *objv,
314 op.aflags= adns_if_noautosys;
319 rc= parse_options(ip,objc,objv,resolver_optioninfos,&op);
322 res= TALLOC(sizeof(*res)); assert(res);
324 for (i=0; i<3; i++) FD_ZERO(&res->handling);
327 if (op.aflags & adns_if_noerrprint) {
332 ec= adns_init_logfn(&ads, op.aflags, op.configstring,
333 op.errcallback ? adnslogfn_callback : 0,
334 op.errcallback ? op.errcallback : op.errfile);
335 if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; }
339 Tcl_IncrRefCount(op.errcallback);
341 rc= update_handlers(&res); if (rc) goto x_rc;
347 static int query_submit(Tcl_Interp *ip, int objc, Tcl_Obj *const *objv,
348 const char *domain) {
350 static const int aftry[]= { AF_INET, AF_INET6 };
352 op.aflags= adns_qf_owner;
355 rc= parse_options(ip,objc,objv,query_optioninfos,&op);
358 if (op.reverseany || (op.sflags & oisf_reverse)) {
360 for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
361 memset(&sa,0,sizeof(sa));
363 r= inet_pton(*af,domain,&sa);
364 if (!r) goto af_found;
366 return staticerr(ip,ec,"invalid address for adns reverse submit","");
370 if (!adnstcl_default) {
371 ec= adns_init(&adnstcl_default,adns_if_noautosys,0);
372 if (ec) return posixerr(ip,ec,"create default adns resolver");
375 op.resolver= adnstcl_default;
379 ec= adns_submit_reverse_any(op.resolver, &sa, op.reverseany, rrtype,
380 op.aflags, 0, &query);
381 } else if (op.sflags & oisf_reverse) {
382 ec= adns_submit_reverse(op.resolver, &sa, rrtype, op.aflags, 0, &query);
384 ec= adns_submit(op.resolver, domain, rrtype, op.aflags, 0, &query);
387 return posixerr(ip,ec,"submit adns query");
392 int do_adns_lookup(ClientData cd, Tcl_Interp *ip,
393 const AdnsTclRRTypeInfo *rrtype,
395 int objc, Tcl_Obj *const *objv,
399 rc= query_submit(ip,domain,objc,objv,&op, ); if (rc) return rc;
401 ec= adns_wait(op.resolver,&query,&answer,0);
402 if (ec) return posixerr(ip,ec,"wait for adns lookup");
407 #define RESULTS_LLEN 7
408 static void make_results(Tcl_Interp *ip, adns_answer *answer,
409 Tcl_Obj results[RESULTS_LLEN]) {
412 results[0]= ret_string(ip, adns_errtypeabbrev(answer->status));
413 results[1]= ret_int(ip, answer->status);
414 results[2]= ret_string(ip, adns_errabbrev(answer->status));
415 results[3]= ret_string(ip, adns_strstatus(answer->status));
416 results[4]= ret_string(ip, answer->owner);
417 results[5]= ret_string(ip, answer->cname ? answer->cname : "");
419 rdata= TALLOC(sizeof(*rdata) * answer->nrrs);
420 for (i=0, datap=answer->rrs.untyped;
421 i<answer->nrrs; i++, datap += rrsz) {
422 st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
424 rdata[i]= ret_string(ip, rdatastring);
427 results[6]= Tcl_NewListObj(RESULTS_LLEN,rdata);
431 static void asynch_query_dispose(Query *query) {
432 scriptinv_cancel(&query->on_yes);
433 scriptinv_cancel(&query->on_no);
434 scriptinv_cancel(&query->on_fail);
435 if (query->xargs) Tcl_DecrRefCount(query->xargs);
436 if (query->aqu) adns_cancel(query->aqu);
440 static void asynch_check(Resolver *res) {
441 Tcl_Interp *interp= res->interp;
446 Tcl_Obj results[RESULTS_LLEN];
449 ec= adns_check(res->ads, &aqu, &answer, &qu);
450 if (ec==ESRCH || ec==EAGAIN) break;
455 si= (!answer->status ? si= &query->on_yes
456 : answer->status > adns_s_max_tempfail ? &query->on_no
459 adnstcl_queries[qu->ix].a= 0;
460 make_results(ip, answer, results);
461 scriptinv_invoke(si, RESULTS_LLEN, results);
462 asynch_query_dispose(query);
465 asynch_sethandlers(res,0);
468 int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
469 const AdnsTclRRTypeInfo *rrtype, const char *domain,
470 Tcl_Obj *on_yes, Tcl_Obj *on_no,
471 Tcl_Obj *on_fail, Tcl_Obj *xargs,
472 int objc, Tcl_Obj *const *objv, void **result) {
476 query= TALLOC(sizeof(*query));
478 scriptinv_init(&query->on_yes);
479 scriptinv_init(&query->on_no);
480 scriptinv_init(&query->on_fail);
483 rc= query_submit(ip,rrtype,domain, ...); if (rc) goto x_rc;
486 rc= scriptinv_set(&query->on_yes, ip,on_yes); if (rc) goto x_rc;
487 rc= scriptinv_set(&query->on_no, ip,on_no); if (rc) goto x_rc;
488 rc= scriptinv_set(&query->on_fail,ip,on_fail); if (rc) goto x_rc;
490 Tcl_IncrRefCount(xargs);
496 asynch_cancel(query);
500 int do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
505 int do_adns_synch(ClientData cd, Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype, const char *domain, int objc, Tcl_Obj *const *objv, adns_answer **result);