chiark / gitweb /
+ * Referrals with RD+RA set, or RCODE=Refused, don't generate warnings,
[adns.git] / src / query.c
index e6bc7a0bf862e5c9a3e6b8321e7b290bf517b889..61ae7bec79a9a60aab9593e6a1e7d387ca973dc4 100644 (file)
@@ -6,10 +6,10 @@
  */
 /*
  *  This file is
- *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
+ *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
  *
  *  It is part of adns, which is
- *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
+ *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
  *    Copyright (C) 1999 Tony Finch <dot@dotat.at>
  *  
  *  This program is free software; you can redistribute it and/or modify
@@ -66,7 +66,7 @@ static adns_query query_alloc(adns_state ads, const typeinfo *typei,
   adns__vbuf_init(&qu->search_vb);
   qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
 
-  qu->id= 0;
+  qu->id= -2; /* will be overwritten with real id before we leave adns */
   qu->flags= flags;
   qu->retries= 0;
   qu->udpnextserver= 0;
@@ -91,7 +91,7 @@ static void query_submit(adns_state ads, adns_query qu,
                         const typeinfo *typei, vbuf *qumsg_vb, int id,
                         adns_queryflags flags, struct timeval now) {
   /* Fills in the query message in for a previously-allocated query,
-   * and submits it.  Cannot fail.
+   * and submits it.  Cannot fail.  Takes over the memory for qumsg_vb.
    */
 
   qu->vb= *qumsg_vb;
@@ -127,16 +127,24 @@ static void query_simple(adns_state ads, adns_query qu,
                         const char *owner, int ol,
                         const typeinfo *typei, adns_queryflags flags,
                         struct timeval now) {
-  vbuf vb;
+  vbuf vb_new;
   int id;
   adns_status stat;
 
-  adns__vbuf_init(&vb);
-  
-  stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
-  if (stat) { adns__query_fail(qu,stat); return; }
+  stat= adns__mkquery(ads,&qu->vb,&id, owner,ol, typei,flags);
+  if (stat) {
+    if (stat == adns_s_querydomaintoolong && (flags & adns_qf_search)) {
+      adns__search_next(ads,qu,now);
+      return;
+    } else {
+      adns__query_fail(qu,stat);
+      return;
+    }
+  }
 
-  query_submit(ads,qu, typei,&vb,id, flags,now);
+  vb_new= qu->vb;
+  adns__vbuf_init(&qu->vb);
+  query_submit(ads,qu, typei,&vb_new,id, flags,now);
 }
 
 void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
@@ -259,25 +267,47 @@ int adns_submit(adns_state ads,
   return r;
 }
 
-int adns_submit_reverse(adns_state ads,
-                       const struct sockaddr *addr,
-                       adns_rrtype type,
-                       adns_queryflags flags,
-                       void *context,
-                       adns_query *query_r) {
+int adns_submit_reverse_any(adns_state ads,
+                           const struct sockaddr *addr,
+                           const char *zone,
+                           adns_rrtype type,
+                           adns_queryflags flags,
+                           void *context,
+                           adns_query *query_r) {
   const unsigned char *iaddr;
-  char buf[30];
+  char *buf, *buf_free;
+  char shortbuf[100];
+  int r, lreq;
 
-  if (type != adns_r_ptr && type != adns_r_ptr_raw) return EINVAL;
   flags &= ~adns_qf_search;
 
   if (addr->sa_family != AF_INET) return ENOSYS;
   iaddr= (const unsigned char*) &(((const struct sockaddr_in*)addr) -> sin_addr);
 
-  sprintf(buf, "%d.%d.%d.%d.in-addr.arpa",
-         iaddr[3], iaddr[2], iaddr[1], iaddr[0]);
+  lreq= strlen(zone) + 4*4 + 1;
+  if (lreq > sizeof(shortbuf)) {
+    buf= malloc(strlen(zone) + 4*4 + 1);
+    if (!buf) return errno;
+    buf_free= buf;
+  } else {
+    buf= shortbuf;
+    buf_free= 0;
+  }
+  sprintf(buf, "%d.%d.%d.%d.%s", iaddr[3], iaddr[2], iaddr[1], iaddr[0], zone);
 
-  return adns_submit(ads,buf,type,flags,context,query_r);
+  r= adns_submit(ads,buf,type,flags,context,query_r);
+  free(buf_free);
+  return r;
+}
+
+int adns_submit_reverse(adns_state ads,
+                       const struct sockaddr *addr,
+                       adns_rrtype type,
+                       adns_queryflags flags,
+                       void *context,
+                       adns_query *query_r) {
+  if (type != adns_r_ptr && type != adns_r_ptr_raw) return EINVAL;
+  return adns_submit_reverse_any(ads,addr,"in-addr.arpa",type,flags,context,query_r);
 }
 
 int adns_synchronous(adns_state ads,
@@ -344,6 +374,7 @@ void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t
   LIST_UNLINK(from->allocations,an);
   LIST_LINK_TAIL(to->allocations,an);
 
+  sz= MEM_ROUND(sz);
   from->interim_allocd -= sz;
   to->interim_allocd += sz;
 
@@ -390,7 +421,9 @@ static void free_query_allocs(adns_query qu) {
   for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
   LIST_INIT(qu->allocations);
   adns__vbuf_free(&qu->vb);
+  adns__vbuf_free(&qu->search_vb);
   free(qu->query_dgram);
+  qu->query_dgram= 0;
 }
 
 void adns_cancel(adns_query qu) {