chiark / gitweb /
Makes up queries.
authorian <ian>
Sun, 27 Sep 1998 23:31:38 +0000 (23:31 +0000)
committerian <ian>
Sun, 27 Sep 1998 23:31:38 +0000 (23:31 +0000)
client/adnstest.c
src/adns-internal.h
src/adns.c
src/adns.h

index 6763babd28ff7d53075e29ecf934dc0bce6652e3..2ad2cc052bcd805766c5d42ca549debd8fee9afc 100644 (file)
@@ -1,14 +1,20 @@
 /**/
 
 #include <stdio.h>
 /**/
 
 #include <stdio.h>
+#include <unistd.h>
 
 #include "adns.h"
 
 int main(void) {
   adns_state ads;
 
 #include "adns.h"
 
 int main(void) {
   adns_state ads;
+  adns_query qu;
   int r;
 
   int r;
 
-  r= adns_init(&ads,adns_if_debug);
+  r= adns_init(&ads,adns_if_debug|adns_if_noautosys);
   if (r) { perror("init"); exit(2); }
   if (r) { perror("init"); exit(2); }
+
+  r= adns_submit(ads,"anarres.greenend.org.uk",adns_r_a,0,0,&qu);
+  if (r) { perror("submit"); exit(2); }
+  
   exit(0);
 }
   exit(0);
 }
index e3a013254cbc875fb76b012ad5cc76ef76887027..f4fe829c7368505f37dca783c11aaa9dc310b14c 100644 (file)
@@ -21,6 +21,8 @@ struct adns__query {
   unsigned long sentudp, senttcp; /* bitmaps indexed by server */
   struct timeval timeout;
   void *context;
   unsigned long sentudp, senttcp; /* bitmaps indexed by server */
   struct timeval timeout;
   void *context;
+  unsigned char *querymsg;
+  int querylen;
   char owner[1];
   /* Possible states:
    *  Queue   child  answer   nextserver  sentudp             senttcp
   char owner[1];
   /* Possible states:
    *  Queue   child  answer   nextserver  sentudp             senttcp
@@ -36,7 +38,7 @@ struct adns__state {
   struct { adns_query head, tail; } input, timew, childw, output;
   int nextid, udpsocket;
   int qbufavail, tcpbufavail, tcpbufused, tcpbufdone;
   struct { adns_query head, tail; } input, timew, childw, output;
   int nextid, udpsocket;
   int qbufavail, tcpbufavail, tcpbufused, tcpbufdone;
-  char *qbuf, *tcpbuf;
+  unsigned char *qbuf, *tcpbuf;
   int nservers;
   struct server {
     struct in_addr addr;
   int nservers;
   struct server {
     struct in_addr addr;
index 91b85d90377c8929135fbf6947645c9904e20a93..c4330a8864dad4bd12bb14ab5dbab8aad4f46ca7 100644 (file)
@@ -140,9 +140,8 @@ static const struct configcommandinfo {
   {  0                                   }
 };
 
   {  0                                   }
 };
 
-static int whitespace(int c) {
-  return c==' ' || c=='\n' || c=='\t';
-}
+static int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; }
+static int ctype_digit(int c) { return c>='0' && c<='9'; }
 
 static void readconfig(adns_state ads, const char *filename) {
   char linebuf[2000], *p, *q;
 
 static void readconfig(adns_state ads, const char *filename) {
   char linebuf[2000], *p, *q;
@@ -169,13 +168,13 @@ static void readconfig(adns_state ads, const char *filename) {
       if (c == EOF) break;
       continue;
     }
       if (c == EOF) break;
       continue;
     }
-    while (l>0 && whitespace(linebuf[l-1])) l--;
+    while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
     linebuf[l]= 0;
     p= linebuf;
     linebuf[l]= 0;
     p= linebuf;
-    while (whitespace(*p)) p++;
+    while (ctype_whitespace(*p)) p++;
     if (*p == '#' || *p == '\n') continue;
     q= p;
     if (*p == '#' || *p == '\n') continue;
     q= p;
-    while (*q && !whitespace(*q)) q++;
+    while (*q && !ctype_whitespace(*q)) q++;
     for (ccip=configcommandinfos;
         ccip->name && strncmp(ccip->name,p,q-p);
         ccip++);
     for (ccip=configcommandinfos;
         ccip->name && strncmp(ccip->name,p,q-p);
         ccip++);
@@ -183,7 +182,7 @@ static void readconfig(adns_state ads, const char *filename) {
       diag(ads,"%s:%d: unknown configuration directive `%.*s'",filename,lno,q-p,p);
       continue;
     }
       diag(ads,"%s:%d: unknown configuration directive `%.*s'",filename,lno,q-p,p);
       continue;
     }
-    while (whitespace(*q)) q++;
+    while (ctype_whitespace(*q)) q++;
     ccip->fn(ads,filename,lno,q);
   }
   if (ferror(file)) {
     ccip->fn(ads,filename,lno,q);
   }
   if (ferror(file)) {
@@ -216,8 +215,7 @@ int adns_init(adns_state *ads_r, adns_initflags flags) {
   adns_state ads;
   const char *res_options, *adns_res_options;
   struct protoent *proto;
   adns_state ads;
   const char *res_options, *adns_res_options;
   struct protoent *proto;
-  struct sockaddr_in udpaddr;
-  int udpaddrlen, r;
+  int r;
   
   ads= malloc(sizeof(*ads)); if (!ads) return errno;
   ads->input.head= ads->input.tail= 0;
   
   ads= malloc(sizeof(*ads)); if (!ads) return errno;
   ads->input.head= ads->input.tail= 0;
@@ -260,23 +258,6 @@ int adns_init(adns_state *ads_r, adns_initflags flags) {
   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
   ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
   if (!ads->udpsocket) { r= errno; goto x_closeudp; }
   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
   ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
   if (!ads->udpsocket) { r= errno; goto x_closeudp; }
-
-  memset(&udpaddr,0,sizeof(udpaddr));
-  udpaddr.sin_family= AF_INET;
-  udpaddr.sin_addr.s_addr= INADDR_ANY;
-  udpaddr.sin_port= 0;
-  r= bind(ads->udpsocket,&udpaddr,sizeof(udpaddr));
-  if (r) { r= errno; goto x_closeudp; }
-
-  udpaddrlen= sizeof(udpaddr);
-  r= getsockname(ads->udpsocket,&udpaddr,&udpaddrlen);
-  if (r) { r= errno; goto x_closeudp; }
-  if (udpaddr.sin_family != AF_INET) {
-    diag(ads,"network API error: UDP socket not AF_INET but %lu",
-        (unsigned long)udpaddr.sin_family);
-    r= EPROTOTYPE; goto x_closeudp;
-  }
-  debug(ads,"UDP socket is %s:%u",inet_ntoa(udpaddr.sin_addr),ntohs(udpaddr.sin_port));
   
   *ads_r= ads;
   return 0;
   
   *ads_r= ads;
   return 0;
@@ -382,29 +363,83 @@ int adns_synchronous(adns_state ads,
   return r;
 }
 
   return r;
 }
 
-static int mkquery(adns_state ads,
-                  const char *owner,
-                  adns_rrtype type) {
-  abort();
+static adns_status mkquery(adns_state ads, const char *owner, int ol, int id,
+                          adns_rrtype type, adns_queryflags flags, int *qml_r) {
+  int ll, c, nlabs, qbufreq;
+  unsigned char label[255], *nqbuf;
+  const char *p, *pe;
+
+#define MKQUERY_ADDB(b) *nqbuf++= (b)
+#define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
+
+  qbufreq= 12+strlen(owner)+3;
+  if (ads->qbufavail < qbufreq) {
+    nqbuf= realloc(ads->qbuf,qbufreq);
+    if (!nqbuf) return adns_s_nolocalmem;
+    ads->qbuf= nqbuf; ads->qbufavail= qbufreq;
+  }
+  nqbuf= ads->qbuf;
+  
+  MKQUERY_ADDW(id);
+  MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
+  MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
+  MKQUERY_ADDW(1); /* QDCOUNT=1 */
+  MKQUERY_ADDW(0); /* ANCOUNT=0 */
+  MKQUERY_ADDW(0); /* NSCOUNT=0 */
+  MKQUERY_ADDW(0); /* ARCOUNT=0 */
+  p= owner; pe= owner+ol;
+  nlabs= 0;
+  if (!*p) return adns_s_invaliddomain;
+  do {
+    ll= 0;
+    while (p!=pe && (c= *p++)!='.') {
+      if (c=='\\') {
+       if (!(flags & adns_f_anyquote)) return adns_s_invaliddomain;
+       if (ctype_digit(p[0])) {
+         if (ctype_digit(p[1]) && ctype_digit(p[2])) {
+           c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0');
+           if (c >= 256) return adns_s_invaliddomain;
+         } else {
+           return adns_s_invaliddomain;
+         }
+       } else if (!(c= *p++)) {
+         return adns_s_invaliddomain;
+       }
+      }
+      if (!(flags & adns_f_anyquote)) {
+       if ((c >= '0' && c <= '9') || c == '-') {
+         if (!ll) return adns_s_invaliddomain;
+       } else if ((c < 'a' || c > 'z') && (c < 'A' && c > 'Z')) {
+         return adns_s_invaliddomain;
+       }
+      }
+      if (ll == sizeof(label)) return adns_s_invaliddomain;
+      label[ll++]= c;
+    }
+    if (!ll) return adns_s_invaliddomain;
+    if (nlabs++ > 63) return adns_s_invaliddomain;
+    MKQUERY_ADDB(ll);
+    memcpy(nqbuf,label,ll); nqbuf+= ll;
+  } while (p!=pe);
+
+  MKQUERY_ADDB(0);
+  MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */
+  MKQUERY_ADDW(1); /* QCLASS=IN */
+
+  *qml_r= nqbuf - ads->qbuf;
+  
+  return adns_s_ok;
 }
 
 }
 
-int adns_submit(adns_state ads,
-               const char *owner,
-               adns_rrtype type,
-               adns_queryflags flags,
-               void *context,
-               adns_query *query_r) {
+static adns_query allocquery(adns_state ads, const char *owner, int ol,
+                            int qml, int id, adns_rrtype type,
+                            adns_queryflags flags, void *context) {
   adns_query qu;
   adns_query qu;
-  adns_status stat;
-  int ol;
-
-  stat= 0;
-  ol= strlen(owner);
-  if (ol>MAXDNAME+1) { stat= adns_s_invaliddomain; ol= 0; }
-  if (ol>0 && owner[ol-1]=='.') { flags &= ~adns_f_search; ol--; }
-  qu= malloc(sizeof(*qu)+ol+1); if (!qu) return errno;
+  unsigned char *qm;
+  
+  qu= malloc(sizeof(*qu)+ol+1+qml); if (!qu) return 0;
   qu->next= qu->back= qu->parent= qu->child= 0;
   qu->next= qu->back= qu->parent= qu->child= 0;
-  qu->id= ads->nextid++;
+  qu->id= id;
   qu->type= type;
   qu->answer= 0;
   qu->flags= flags;
   qu->type= type;
   qu->answer= 0;
   qu->flags= flags;
@@ -413,16 +448,48 @@ int adns_submit(adns_state ads,
   qu->sentudp= qu->senttcp= 0;
   qu->nextserver= 0;
   memcpy(qu->owner,owner,ol); qu->owner[ol]= 0;
   qu->sentudp= qu->senttcp= 0;
   qu->nextserver= 0;
   memcpy(qu->owner,owner,ol); qu->owner[ol]= 0;
+  qu->querymsg= qm= qu->owner+ol+1;
+  memcpy(qm,ads->qbuf,qml);
+  qu->querylen= qml;
+  return qu;
+}
 
 
+static int failsubmit(adns_state ads, void *context, adns_query *query_r,
+                     adns_rrtype type, adns_queryflags flags,
+                     int id, adns_status stat) {
+  adns_query qu;
 
 
-  mkquery(ads,owner,type);
+  qu= allocquery(ads,0,0,0,id,type,flags,context); if (!qu) return errno;
+  query_fail(ads,qu,stat);
+  *query_r= qu;
+  return 0;
+}
+
+int adns_submit(adns_state ads,
+               const char *owner,
+               adns_rrtype type,
+               adns_queryflags flags,
+               void *context,
+               adns_query *query_r) {
+  adns_query qu;
+  adns_status stat;
+  int ol, id, qml;
+
+  id= ads->nextid++;
+
+  ol= strlen(owner);
+  if (ol<=1 || ol>MAXDNAME+1)
+    return failsubmit(ads,context,query_r,type,flags,id,adns_s_invaliddomain);
+  if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_f_search; ol--; }
+
+  stat= mkquery(ads,owner,ol,id,type,flags,&qml);
+  if (stat) return failsubmit(ads,context,query_r,type,flags,id,stat);
+
+  qu= allocquery(ads,owner,ol,qml,id,type,flags,context); if (!qu) return errno;
+
+  LIST_LINK_TAIL(ads->input,qu);
+  autosys(ads);
 
 
-  if (stat) {
-    query_fail(ads,qu,stat);
-  } else {
-    LIST_LINK_TAIL(ads->input,qu);
-    autosys(ads);
-  }
   *query_r= qu;
   return 0;
 }
   *query_r= qu;
   return 0;
 }
index 553c3205afcec23de02e422375dd2c323d76b615..70e743ba9a9649b6aa3017d72623aee479e1ba2e 100644 (file)
@@ -23,25 +23,25 @@ typedef enum {
 } adns_queryflags;
 
 typedef enum {
 } adns_queryflags;
 
 typedef enum {
-  adns__rrttype_mask=  0x0fff,
-  adns__qtf_deref=     0x1000, /* dereference domains and produce extra data */
-  adns__qtf_mailconv=  0x2000, /* put @ between first and second labels */
-  adns_r_none=              0,
-  adns_r_a=                 1,
-  adns_r_ns_raw=            2,
-  adns_r_ns=                   adns_r_ns_raw|adns__qtf_deref,
-  adns_r_cname=             5,
-  adns_r_soa_raw=           6,
-  adns_r_soa=                  adns_r_soa_raw|adns__qtf_mailconv,
-  adns_r_null=             10,
-  adns_r_ptr_raw=          12,
-  adns_r_ptr=                  adns_r_ptr_raw|adns__qtf_deref,
-  adns_r_hinfo=            13,  
-  adns_r_mx_raw=           15,
-  adns_r_mx=                   adns_r_mx_raw|adns__qtf_deref,
-  adns_r_txt=              16,
-  adns_r_rp_raw=           17,
-  adns_r_rp=                   adns_r_rp_raw|adns__qtf_mailconv
+  adns__rrt_typemask=  0x0ffff,
+  adns__qtf_deref=     0x10000, /* dereference domains and produce extra data */
+  adns__qtf_mailconv=  0x20000, /* put @ between first and second labels */
+  adns_r_none=               0,
+  adns_r_a=                  1,
+  adns_r_ns_raw=             2,
+  adns_r_ns=                    adns_r_ns_raw|adns__qtf_deref,
+  adns_r_cname=              5,
+  adns_r_soa_raw=            6,
+  adns_r_soa=                   adns_r_soa_raw|adns__qtf_mailconv,
+  adns_r_null=              10,
+  adns_r_ptr_raw=           12,
+  adns_r_ptr=                   adns_r_ptr_raw|adns__qtf_deref,
+  adns_r_hinfo=             13,  
+  adns_r_mx_raw=            15,
+  adns_r_mx=                    adns_r_mx_raw|adns__qtf_deref,
+  adns_r_txt=               16,
+  adns_r_rp_raw=            17,
+  adns_r_rp=                    adns_r_rp_raw|adns__qtf_mailconv
 } adns_rrtype;
 
 /* In queries without qtf_anyquote, all domains must have standard
 } adns_rrtype;
 
 /* In queries without qtf_anyquote, all domains must have standard
@@ -64,6 +64,7 @@ typedef enum {
   adns_s_serverfailure,
   adns_s_unknownqtype,
   adns_s_remoteerror,
   adns_s_serverfailure,
   adns_s_unknownqtype,
   adns_s_remoteerror,
+  adns_s_nolocalmem,
   adns_s_max_tempfail= 99,
   adns_s_nxdomain,
   adns_s_norecord,
   adns_s_max_tempfail= 99,
   adns_s_nxdomain,
   adns_s_norecord,