chiark / gitweb /
copyright dates
[chiark-tcl.git] / adns / adns.c
index 5248c62efd37f830b1033c3b3470398d1c38c64f..201212c44b36106950f9a1a17ce4a054a1355fa2 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
 /*
  * 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 \
  *
  * 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
  *        calls, later,
  *           [concat ON-YES|ON-NO|ON-DONTKNOW XARGS RESULTS]
  * adns asynch-cancel QUERY-ID
  *
  * adns destroy-resolver RESOLVER
  *        cancels outstanding queries
  *
  * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
  */
 
 #define _GNU_SOURCE
  */
 
 #define _GNU_SOURCE
@@ -64,7 +84,7 @@
 
 #include <adns.h>
 
 
 #include <adns.h>
 
-#include "adnstcl.h"
+#include "chiark_tcl_adns.h"
 
 /*---------- important types and forward declarations ----------*/
 
 
 /*---------- important types and forward declarations ----------*/
 
@@ -178,6 +198,14 @@ struct Resolver {
   Tcl_Obj *errstring_accum;
 };
 
   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
 /* 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 +292,7 @@ static Resolver *default_resolver(Tcl_Interp *ip) {
 
 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
   void *query_v;
 
 static void destroy_resolver(Tcl_Interp *ip, Resolver *res) {
   void *query_v;
+  Query *query;
   int logstring_len;
   char *rstr;
   adns_query aqu;
   int logstring_len;
   char *rstr;
   adns_query aqu;
@@ -285,6 +314,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;
       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);
       asynch_query_dispose(ip, query_v);
     }
     adns_finish(res->ads);
@@ -303,7 +335,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) {
 }
 
 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;
 }
   destroy_resolver(ip,res_v);
   return TCL_OK;
 }
@@ -331,7 +363,7 @@ static int create_resolver(Tcl_Interp *ip, const OptionParse *op,
                      op->config_string,
                      op->errcallback ? adnslogfn_callback : 0,
                      op->errcallback ? (void*)res : (void*)op->errfile);
                      op->config_string,
                      op->errcallback ? adnslogfn_callback : 0,
                      op->errcallback ? (void*)res : (void*)op->errfile);
-  if (ec) { rc= posixerr(ip,ec,"create adns resolver"); goto x_rc; }
+  if (ec) { rc= cht_posixerr(ip,ec,"create adns resolver"); goto x_rc; }
 
   *res_r= res;
   return TCL_OK;
 
   *res_r= res;
   return TCL_OK;
@@ -344,7 +376,7 @@ static int create_resolver(Tcl_Interp *ip, const OptionParse *op,
   return rc;
 }  
 
   return rc;
 }  
 
-int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
+int cht_do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
                         int objc, Tcl_Obj *const *objv,
                         void **result) {
   OptionParse op;
                         int objc, Tcl_Obj *const *objv,
                         void **result) {
   OptionParse op;
@@ -367,14 +399,14 @@ int do_adns_new_resolver(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
   return TCL_OK;
 }
 
-int do_adns_set_default_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
+int cht_do_adns_set_default_resolver(ClientData cd, Tcl_Interp *ip, void *res_v) {
   Resolver *res= res_v;
   Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
   Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, 0, res);
   return TCL_OK;
 }
 
   Resolver *res= res_v;
   Tcl_DeleteAssocData(ip,ASSOC_DEFAULTRES);
   Tcl_SetAssocData(ip, ASSOC_DEFAULTRES, 0, res);
   return TCL_OK;
 }
 
-static const IdDataSpec adnstcl_resolvers= {
+const IdDataSpec cht_adnstcl_resolvers= {
   "adns-res", "adns-resolvers-table", destroy_resolver_idtabcb
 };
 
   "adns-res", "adns-resolvers-table", destroy_resolver_idtabcb
 };
 
@@ -385,7 +417,7 @@ static const IdDataSpec adnstcl_resolvers= {
 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
 
 #define RRTYPE_PLUS(t) { #t "+", adns_r_##t }
 #define RRTYPE_MINUS(t) { #t "-", adns_r_##t##_raw }
 
-const AdnsTclRRTypeInfo adnstclrrtypeinfos[]= {
+const AdnsTclRRTypeInfo cht_adnstclrrtypeinfo_entries[]= {
   RRTYPE_EXACTLY(a),
   RRTYPE_EXACTLY(cname),
   RRTYPE_EXACTLY(hinfo),
   RRTYPE_EXACTLY(a),
   RRTYPE_EXACTLY(cname),
   RRTYPE_EXACTLY(hinfo),
@@ -393,6 +425,7 @@ const AdnsTclRRTypeInfo adnstclrrtypeinfos[]= {
 
   RRTYPE_RAW(ns),
   RRTYPE_RAW(mx),
 
   RRTYPE_RAW(ns),
   RRTYPE_RAW(mx),
+  RRTYPE_EXACTLY(txt),
 
   RRTYPE_EXACTLY(soa),
   RRTYPE_EXACTLY(ptr),
 
   RRTYPE_EXACTLY(soa),
   RRTYPE_EXACTLY(ptr),
@@ -409,7 +442,7 @@ static int oifn_resolver(Tcl_Interp *ip, const OptionInfo *oi,
   void *val_v;
   int rc;
   
   void *val_v;
   int rc;
   
-  rc= 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;
   if (rc) return rc;
   op->resolver= val_v;
   return TCL_OK;
@@ -469,7 +502,8 @@ static int query_submit(Tcl_Interp *ip,
       r= inet_pton(*af,domain,&sa);
       if (!r) goto af_found;
     }
       r= inet_pton(*af,domain,&sa);
       if (!r) goto af_found;
     }
-    return staticerr(ip,"invalid address for adns reverse submit","");
+    return cht_staticerr(ip,"invalid address for adns reverse submit",
+                        "ADNS REVERSE INVALID");
   af_found:;
   }
 
   af_found:;
   }
 
@@ -486,7 +520,7 @@ static int query_submit(Tcl_Interp *ip,
                    type->number, op.aflags, context, aqu_r);
   }
   if (ec)
                    type->number, op.aflags, context, aqu_r);
   }
   if (ec)
-    return posixerr(ip,ec,"submit adns query");
+    return cht_posixerr(ip,ec,"submit adns query");
 
   return TCL_OK;
 }
 
   return TCL_OK;
 }
@@ -496,10 +530,11 @@ static int query_submit(Tcl_Interp *ip,
 
 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
                              Tcl_Obj *results[RESULTSTATUS_LLEN]) {
 
 static void make_resultstatus(Tcl_Interp *ip, adns_status status,
                              Tcl_Obj *results[RESULTSTATUS_LLEN]) {
-  results[0]= ret_string(ip, adns_errtypeabbrev(status));
-  results[1]= ret_int(ip, status);
-  results[2]= ret_string(ip, adns_errabbrev(status));
-  results[3]= ret_string(ip, adns_strerror(status));
+  results[0]= cht_ret_string(ip, adns_errtypeabbrev(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) {
 }
 
 static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
@@ -514,7 +549,7 @@ static Tcl_Obj *make_resultrdata(Tcl_Interp *ip, adns_answer *answer) {
        i++, datap += rrsz) {
     st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
     assert(!st);
        i++, datap += rrsz) {
     st= adns_rr_info(answer->type, 0,0, &rrsz, datap, &rdatastring);
     assert(!st);
-    rdata[i]= ret_string(ip, rdatastring);
+    rdata[i]= cht_ret_string(ip, rdatastring);
     free(rdatastring);
   }
   rl= Tcl_NewListObj(answer->nrrs, rdata);
     free(rdatastring);
   }
   rl= Tcl_NewListObj(answer->nrrs, rdata);
@@ -527,9 +562,10 @@ static void make_resultlist(Tcl_Interp *ip, adns_answer *answer,
 
   make_resultstatus(ip, answer->status, results);
   assert(RESULTSTATUS_LLEN==4);
 
   make_resultstatus(ip, answer->status, results);
   assert(RESULTSTATUS_LLEN==4);
-  results[4]= ret_string(ip, answer->owner);
-  results[5]= ret_string(ip, answer->cname ? answer->cname : "");
+  results[4]= cht_ret_string(ip, answer->owner);
+  results[5]= cht_ret_string(ip, answer->cname ? answer->cname : "");
   results[6]= make_resultrdata(ip, answer);
   results[6]= make_resultrdata(ip, answer);
+  assert(RESULTLIST_LLEN==7);
 }
 
 /*---------- synchronous query handling ----------*/
 }
 
 /*---------- synchronous query handling ----------*/
@@ -551,7 +587,7 @@ static int synch(Tcl_Interp *ip, const AdnsTclRRTypeInfo *rrtype,
   return TCL_OK;
 }
 
   return TCL_OK;
 }
 
-int do_adns_lookup(ClientData cd, Tcl_Interp *ip,
+int cht_do_adns_lookup(ClientData cd, Tcl_Interp *ip,
                   const AdnsTclRRTypeInfo *rrtype,
                   const char *domain,
                   int objc, Tcl_Obj *const *objv,
                   const AdnsTclRRTypeInfo *rrtype,
                   const char *domain,
                   int objc, Tcl_Obj *const *objv,
@@ -572,7 +608,7 @@ int do_adns_lookup(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
   return TCL_OK;
 }
 
-int do_adns_synch(ClientData cd, Tcl_Interp *ip,
+int cht_do_adns_synch(ClientData cd, Tcl_Interp *ip,
                  const AdnsTclRRTypeInfo *rrtype,
                  const char *domain,
                  int objc, Tcl_Obj *const *objv,
                  const AdnsTclRRTypeInfo *rrtype,
                  const char *domain,
                  int objc, Tcl_Obj *const *objv,
@@ -590,14 +626,6 @@ int do_adns_synch(ClientData cd, Tcl_Interp *ip,
 
 /*---------- asynchronous query handling ----------*/
 
 
 /*---------- 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) {
 static void asynch_check_now(Resolver *res);
 
 static void asynch_timerhandler(void *res_v) {
@@ -703,7 +731,7 @@ static void asynch_check_now(Resolver *res) {
     query= query_v;
 
     query->aqu= 0;
     query= query_v;
 
     query->aqu= 0;
-    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
 
     si= (!answer->status ? &query->on_yes
         : answer->status > adns_s_max_tempfail ? &query->on_no
@@ -711,7 +739,7 @@ static void asynch_check_now(Resolver *res) {
 
     make_resultlist(interp, answer, results);
     free(answer);
 
     make_resultlist(interp, answer, results);
     free(answer);
-    scriptinv_invoke(si, RESULTLIST_LLEN, results);
+    cht_scriptinv_invoke(si, RESULTLIST_LLEN, results);
     asynch_query_dispose(interp, query);
   }
 
     asynch_query_dispose(interp, query);
   }
 
@@ -720,7 +748,7 @@ static void asynch_check_now(Resolver *res) {
   Tcl_Release(res);
 }
     
   Tcl_Release(res);
 }
     
-int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
+int cht_do_adns_asynch(ClientData cd, Tcl_Interp *ip,
                   Tcl_Obj *on_yes, Tcl_Obj *on_no,
                   Tcl_Obj *on_fail, Tcl_Obj *xargs,
                   const AdnsTclRRTypeInfo *rrtype, const char *domain,
                   Tcl_Obj *on_yes, Tcl_Obj *on_no,
                   Tcl_Obj *on_fail, Tcl_Obj *xargs,
                   const AdnsTclRRTypeInfo *rrtype, const char *domain,
@@ -732,9 +760,9 @@ int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
   query= TALLOC(sizeof(*query));
   query->ix= -1;
   query->aqu= 0;
   query= TALLOC(sizeof(*query));
   query->ix= -1;
   query->aqu= 0;
-  scriptinv_init(&query->on_yes);
-  scriptinv_init(&query->on_no);
-  scriptinv_init(&query->on_fail);
+  cht_scriptinv_init(&query->on_yes);
+  cht_scriptinv_init(&query->on_no);
+  cht_scriptinv_init(&query->on_fail);
   query->xargs= 0;
 
   rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&query->res);
   query->xargs= 0;
 
   rc= query_submit(ip,rrtype,domain,objc,objv,&query->aqu,query,&query->res);
@@ -742,9 +770,9 @@ int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
 
   res= query->res;
 
 
   res= query->res;
 
-  rc= scriptinv_set(&query->on_yes, ip,on_yes, xargs);  if (rc) goto x_rc;
-  rc= scriptinv_set(&query->on_no,  ip,on_no,  xargs);  if (rc) goto x_rc;
-  rc= scriptinv_set(&query->on_fail,ip,on_fail,xargs);  if (rc) goto x_rc;
+  rc= cht_scriptinv_set(&query->on_yes, ip,on_yes, xargs);  if (rc) goto x_rc;
+  rc= cht_scriptinv_set(&query->on_no,  ip,on_no,  xargs);  if (rc) goto x_rc;
+  rc= cht_scriptinv_set(&query->on_fail,ip,on_fail,xargs);  if (rc) goto x_rc;
   query->xargs= xargs;
   Tcl_IncrRefCount(xargs);
   *result= query;
   query->xargs= xargs;
   Tcl_IncrRefCount(xargs);
   *result= query;
@@ -757,7 +785,7 @@ int do_adns_asynch(ClientData cd, Tcl_Interp *ip,
   return rc;
 }
 
   return rc;
 }
 
-int do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
+int cht_do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
   Query *query= query_v;
   Resolver *res= query->res;
   asynch_query_dispose(ip, query);
   Query *query= query_v;
   Resolver *res= query->res;
   asynch_query_dispose(ip, query);
@@ -766,10 +794,10 @@ int do_adns_asynch_cancel(ClientData cd, Tcl_Interp *ip, void *query_v) {
 }
 
 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
 }
 
 static void asynch_query_dispose(Tcl_Interp *interp, Query *query) {
-  tabledataid_disposing(interp, query, &adnstcl_queries);
-  scriptinv_cancel(&query->on_yes);
-  scriptinv_cancel(&query->on_no);
-  scriptinv_cancel(&query->on_fail);
+  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);
   if (query->xargs) Tcl_DecrRefCount(query->xargs);
   if (query->aqu) adns_cancel(query->aqu);
   TFREE(query);
   if (query->xargs) Tcl_DecrRefCount(query->xargs);
   if (query->aqu) adns_cancel(query->aqu);
   TFREE(query);
@@ -779,22 +807,10 @@ static void destroy_query_idtabcb(Tcl_Interp *interp, void *query_v) {
   asynch_query_dispose(interp, 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 ----------*/
 
   "adns", "adns-query-table", destroy_query_idtabcb
 };
 
 /*---------- main hooks for tcl ----------*/
 
-int do_toplevel_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 Adns_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
-int Adns_Init(
-  Tcl_RegisterObjType(&blockcipherkey_type);
-  Tcl_RegisterObjType(&sockaddr_type);
-  Tcl_RegisterObjType(&tabledataid_nearlytype);
-  Tcl_RegisterObjType(&ulong_type);
-  Tcl_RegisterObjType(&addrmap_type);
+CHT_INIT(adns, {}, CHTI_COMMANDS(cht_adnstoplevel_entries))