chiark / gitweb /
+ * Do not spin if TCP connection blocks for writing (and add test case).
[adns.git] / src / query.c
index 5e345ae72f5f4101e356c367be6489509b8b26f0..966f40974f31d6d55655901340caff7bcb5a90d3 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;
@@ -267,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);
+
+  r= adns_submit(ads,buf,type,flags,context,query_r);
+  free(buf_free);
+  return r;
+}
 
-  return adns_submit(ads,buf,type,flags,context,query_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,
@@ -352,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;