chiark / gitweb /
changelog: finalise 1.3.5
[chiark-tcl.git] / adns / adns.c
index 30d036bd5f89e86f9eab09b7521a6dfb165eaaae..d0ee2955b94e09e26c36acde087faf81212173fd 100644 (file)
@@ -1,5 +1,3 @@
-/*
- */
 /*
  * adns lookup TYPE DOMAIN [QUERY-OPTIONS]                    => [list RDATA]
  *    if no or dontknow, throws an exception, with errorCode one of
@@ -22,7 +20,7 @@
  *
  * adns asynch ON-YES ON-NO ON-DONTKNOW XARGS \
  *             TYPE DOMAIN \
- *             [QUERY-OPTIONS]                               => QUERY-ID
+ *             [QUERY-OPTIONS...]                            => QUERY-ID
  *        calls, later,
  *           [concat ON-YES|ON-NO|ON-DONTKNOW XARGS RESULTS]
  * adns asynch-cancel QUERY-ID
  *
  * adns destroy-resolver RESOLVER
  *        cancels outstanding queries
+ *
+ */
+/* ---8<--- end of documentation comment --8<-- */
+
+/*
+ * adns.c - adns binding for Tcl
+ * Copyright 2006-2012 Ian Jackson
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #define _GNU_SOURCE
@@ -178,6 +196,14 @@ struct Resolver {
   Tcl_Obj *errstring_accum;
 };
 
+struct Query {
+  int ix; /* first! */
+  Resolver *res;
+  adns_query aqu;
+  ScriptToInvoke on_yes, on_no, on_fail;
+  Tcl_Obj *xargs;
+};
+
 /* The default resolver is recorded using Tcl_SetAssocData with key
  * ASSOC_DEFAULTRES to record the Resolver*.  If it was explicitly
  * created with `adns new-resolver' then ix will be >=0, and the
@@ -264,6 +290,7 @@ static Resolver *default_resolver(Tcl_Interp *ip) {
 
 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
   void *query_v;
+  Query *query;
   int logstring_len;
   char *rstr;
   adns_query aqu;
@@ -285,6 +312,9 @@ static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
       adns_forallqueries_begin(res->ads);
       aqu= adns_forallqueries_next(res->ads, &query_v);
       if (!aqu) break;
+      query= query_v;
+      assert(query->aqu == aqu);
+      query->aqu= 0; /* avoid disrupting the adns query list */
       asynch_query_dispose(ip, query_v);
     }
     adns_finish(res->ads);
@@ -303,7 +333,7 @@ static void destroy_resolver_defcb(ClientData resolver_v, Tcl_Interp *ip) {
 }
 
 int cht_do_adns_destroy_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
-  cht_tabledataid_disposing(ip,res_v,&adnstcl_resolvers);
+  cht_tabledataid_disposing(ip,res_v,&cht_adnstcl_resolvers);
   destroy_resolver(ip,res_v);
   return TCL_OK;
 }
@@ -393,6 +423,7 @@ const AdnsTclRRTypeInfo cht_adnstclrrtypeinfo_entries[]= {
 
   RRTYPE_RAW(ns),
   RRTYPE_RAW(mx),
+  RRTYPE_EXACTLY(txt),
 
   RRTYPE_EXACTLY(soa),
   RRTYPE_EXACTLY(ptr),
@@ -409,7 +440,7 @@ static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
   void *val_v;
   int rc;
   
-  rc= cht_pat_iddata(ip,arg,&val_v,&adnstcl_resolvers);
+  rc= cht_pat_iddata(ip,arg,&val_v,&cht_adnstcl_resolvers);
   if (rc) return rc;
   op->resolver= val_v;
   return TCL_OK;
@@ -463,7 +494,7 @@ static int query_submit(Tcl_Interp *ip,
   
   if (op.reverseany || (op.sflags & oisf_reverse)) {
     const int *af;
-    for (af=aftry; af < af + sizeof(af)/sizeof(*af); af++) {
+    for (af=aftry; af < af + sizeof(aftry)/sizeof(*aftry); af++) {
       memset(&sa,0,sizeof(sa));
       sa.sa_family= *af;
       r= inet_pton(*af,domain,&sa);
@@ -501,6 +532,7 @@ static void make_resultstatus(Tcl_Interp *ip, adns_status status,
   results[1]= cht_ret_int(ip, status);
   results[2]= cht_ret_string(ip, adns_errabbrev(status));
   results[3]= cht_ret_string(ip, adns_strerror(status));
+  assert(RESULTSTATUS_LLEN==4);
 }
 
 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
@@ -531,6 +563,7 @@ static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
   results[4]= cht_ret_string(ip, answer->owner);
   results[5]= cht_ret_string(ip, answer->cname ? answer->cname : "");
   results[6]= make_resultrdata(ip, answer);
+  assert(RESULTLIST_LLEN==7);
 }
 
 /*---------- synchronous query handling ----------*/
@@ -591,14 +624,6 @@ int cht_do_adns_synch(ClientData cd, Tcl_Interp *ip,
 
 /*---------- asynchronous query handling ----------*/
 
-struct Query {
-  int ix; /* first! */
-  Resolver *res;
-  adns_query aqu;
-  ScriptToInvoke on_yes, on_no, on_fail;
-  Tcl_Obj *xargs;
-};
-
 static void asynch_check_now(Resolver *res);
 
 static void asynch_timerhandler(void *res_v) {
@@ -704,7 +729,7 @@ static void asynch_check_now(Resolver *res) {
     query= query_v;
 
     query->aqu= 0;
-    cht_tabledataid_disposing(interp, query, &adnstcl_queries);
+    cht_tabledataid_disposing(interp, query, &cht_adnstcl_queries);
 
     si= (!answer->status ? &query->on_yes
         : answer->status > adns_s_max_tempfail ? &query->on_no
@@ -767,7 +792,7 @@ int cht_do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
 }
 
 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
-  cht_tabledataid_disposing(interp, query, &adnstcl_queries);
+  cht_tabledataid_disposing(interp, query, &cht_adnstcl_queries);
   cht_scriptinv_cancel(&query->on_yes);
   cht_scriptinv_cancel(&query->on_no);
   cht_scriptinv_cancel(&query->on_fail);
@@ -780,19 +805,10 @@ static void destroy_query_idtabcb(Tcl_Interp *interp, void *query_v) {
   asynch_query_dispose(interp, query_v);
 }
 
-const IdDataSpec adnstcl_queries= {
+const IdDataSpec cht_adnstcl_queries= {
   "adns", "adns-query-table", destroy_query_idtabcb
 };
 
 /*---------- main hooks for tcl ----------*/
 
-int cht_do_adnstoplevel_adns(ClientData cd, Tcl_Interp *ip,
-                     const Adns_SubCommand *subcmd,
-                     int objc, Tcl_Obj *const *objv) {
-  return subcmd->func(0,ip,objc,objv);
-}
-
-extern int Chiark_tcl_adns_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
-int Chiark_tcl_adns_Init(Tcl_Interp *ip) {
-  return cht_initextension(ip, cht_adnstoplevel_entries, 0);
-}
+CHT_INIT(adns, {}, CHTI_COMMANDS(cht_adnstoplevel_entries))