chiark / gitweb /
make _qf_owner work if _qf_search not specified, and test it (oops!)
[adns.git] / src / query.c
1 /*
2  * query.c
3  * - overall query management (allocation, completion)
4  * - per-query memory management
5  * - query submission and cancellation (user-visible and internal)
6  */
7 /*
8  *  This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2, or (at your option)
13  *  any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software Foundation,
22  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
23  */
24
25 #include "internal.h"
26
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31
32 #include <sys/time.h>
33
34 #include "internal.h"
35
36 static adns_query query_alloc(adns_state ads, const typeinfo *typei,
37                               adns_queryflags flags, struct timeval now) {
38   /* Allocate a virgin query and return it. */
39   adns_query qu;
40   
41   qu= malloc(sizeof(*qu));  if (!qu) return 0;
42   qu->answer= malloc(sizeof(*qu->answer));  if (!qu->answer) { free(qu); return 0; }
43   
44   qu->ads= ads;
45   qu->state= query_udp;
46   qu->back= qu->next= qu->parent= 0;
47   LIST_INIT(qu->children);
48   LINK_INIT(qu->siblings);
49   LIST_INIT(qu->allocations);
50   qu->interim_allocd= 0;
51   qu->preserved_allocd= 0;
52   qu->final_allocspace= 0;
53
54   qu->typei= typei;
55   qu->query_dgram= 0;
56   qu->query_dglen= 0;
57   adns__vbuf_init(&qu->vb);
58
59   qu->cname_dgram= 0;
60   qu->cname_dglen= qu->cname_begin= 0;
61
62   adns__vbuf_init(&qu->search_vb);
63   qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
64
65   qu->id= 0;
66   qu->flags= flags;
67   qu->udpretries= 0;
68   qu->udpnextserver= 0;
69   qu->udpsent= qu->tcpfailed= 0;
70   timerclear(&qu->timeout);
71   qu->expires= now.tv_sec + MAXTTLBELIEVE;
72
73   memset(&qu->ctx,0,sizeof(qu->ctx));
74
75   qu->answer->status= adns_s_ok;
76   qu->answer->cname= qu->answer->owner= 0;
77   qu->answer->type= typei->type;
78   qu->answer->expires= -1;
79   qu->answer->nrrs= 0;
80   qu->answer->rrs.untyped= 0;
81   qu->answer->rrsz= typei->rrsz;
82
83   return qu;
84 }
85
86 static void query_submit(adns_state ads, adns_query qu,
87                          const typeinfo *typei, vbuf *qumsg_vb, int id,
88                          adns_queryflags flags, struct timeval now) {
89   /* Fills in the query message in for a previously-allocated query,
90    * and submits it.  Cannot fail.
91    */
92
93   qu->vb= *qumsg_vb;
94   adns__vbuf_init(qumsg_vb);
95
96   qu->query_dgram= malloc(qu->vb.used);
97   if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; }
98   
99   qu->id= id;
100   qu->query_dglen= qu->vb.used;
101   memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
102   
103   adns__query_udp(qu,now);
104   adns__autosys(ads,now);
105 }
106
107 adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
108                                   const typeinfo *typei, vbuf *qumsg_vb, int id,
109                                   adns_queryflags flags, struct timeval now,
110                                   const qcontext *ctx) {
111   adns_query qu;
112
113   qu= query_alloc(ads,typei,flags,now);
114   if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; }
115   *query_r= qu;
116
117   memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
118   query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
119   
120   return adns_s_ok;
121 }
122
123 static void query_simple(adns_state ads, adns_query qu,
124                          const char *owner, int ol,
125                          const typeinfo *typei, adns_queryflags flags,
126                          struct timeval now) {
127   vbuf vb;
128   int id;
129   adns_status stat;
130
131   adns__vbuf_init(&vb);
132   
133   stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
134   if (stat) { adns__query_fail(qu,stat); return; }
135
136   query_submit(ads,qu, typei,&vb,id, flags,now);
137 }
138
139 void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
140   const char *nextentry;
141   adns_status stat;
142   
143   if (qu->search_doneabs<0) {
144     nextentry= 0;
145     qu->search_doneabs= 1;
146   } else {
147     if (qu->search_pos >= ads->nsearchlist) {
148       if (qu->search_doneabs) {
149         stat= adns_s_nxdomain; goto x_fail;
150         return;
151       } else {
152         nextentry= 0;
153         qu->search_doneabs= 1;
154       }
155     } else {
156       nextentry= ads->searchlist[qu->search_pos++];
157     }
158   }
159
160   qu->search_vb.used= qu->search_origlen;
161   if (nextentry) {
162     if (!adns__vbuf_append(&qu->search_vb,".",1) ||
163         !adns__vbuf_appendstr(&qu->search_vb,nextentry)) {
164       stat= adns_s_nomemory; goto x_fail;
165     }
166   }
167
168   free(qu->query_dgram);
169   qu->query_dgram= 0; qu->query_dglen= 0;
170
171   query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now);
172   return;
173   
174 x_fail:
175   adns__query_fail(qu,stat);
176 }
177
178 static int save_owner(adns_query qu, const char *owner, int ol) {
179   /* Returns 1 if OK, otherwise there was no memory. */
180   adns_answer *ans;
181
182   ans= qu->answer;
183   assert(!ans->owner);
184
185   ans->owner= adns__alloc_preserved(qu,ol+1);  if (!ans->owner) return 0;
186
187   memcpy(ans->owner,owner,ol);
188   ans->owner[ol]= 0;
189   return 1;
190 }
191
192 int adns_submit(adns_state ads,
193                 const char *owner,
194                 adns_rrtype type,
195                 adns_queryflags flags,
196                 void *context,
197                 adns_query *query_r) {
198   int r, ol, ndots;
199   adns_status stat;
200   const typeinfo *typei;
201   struct timeval now;
202   adns_query qu;
203   const char *p;
204
205   typei= adns__findtype(type);
206   if (!typei) return adns_s_unknownrrtype;
207
208   r= gettimeofday(&now,0); if (r) goto x_errno;
209   qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno;
210   
211   qu->ctx.ext= context;
212   qu->ctx.callback= 0;
213   memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
214
215   *query_r= qu;
216
217   ol= strlen(owner);
218   if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
219   if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
220                                  
221   if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
222     flags &= ~adns_qf_search;
223     ol--;
224   }
225
226   if (flags & adns_qf_search) {
227     r= adns__vbuf_append(&qu->search_vb,owner,ol);
228     if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
229
230     for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
231     qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
232     qu->search_origlen= ol;
233     adns__search_next(ads,qu,now);
234   } else {
235     if (flags & adns_qf_owner) {
236       if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; }
237     }
238     query_simple(ads,qu, owner,ol, typei,flags, now);
239   }
240   return 0;
241
242  x_adnsfail:
243   adns__query_fail(qu,stat);
244   return 0;
245
246  x_errno:
247   r= errno;
248   assert(r);
249   return r;
250 }
251
252 int adns_synchronous(adns_state ads,
253                      const char *owner,
254                      adns_rrtype type,
255                      adns_queryflags flags,
256                      adns_answer **answer_r) {
257   adns_query qu;
258   int r;
259   
260   r= adns_submit(ads,owner,type,flags,0,&qu);
261   if (r) return r;
262
263   r= adns_wait(ads,&qu,answer_r,0);
264   if (r) adns_cancel(qu);
265
266   return r;
267 }
268
269 static void *alloc_common(adns_query qu, size_t sz) {
270   allocnode *an;
271
272   if (!sz) return qu; /* Any old pointer will do */
273   assert(!qu->final_allocspace);
274   an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
275   if (!an) return 0;
276   LIST_LINK_TAIL(qu->allocations,an);
277   return (byte*)an + MEM_ROUND(sizeof(*an));
278 }
279
280 void *adns__alloc_interim(adns_query qu, size_t sz) {
281   void *rv;
282   
283   sz= MEM_ROUND(sz);
284   rv= alloc_common(qu,sz);
285   if (!rv) return 0;
286   qu->interim_allocd += sz;
287   return rv;
288 }
289
290 void *adns__alloc_preserved(adns_query qu, size_t sz) {
291   void *rv;
292   
293   sz= MEM_ROUND(sz);
294   rv= adns__alloc_interim(qu,sz);
295   if (!rv) return 0;
296   qu->preserved_allocd += sz;
297   return rv;
298 }
299
300 void *adns__alloc_mine(adns_query qu, size_t sz) {
301   return alloc_common(qu,MEM_ROUND(sz));
302 }
303
304 void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
305   allocnode *an;
306
307   if (!block) return;
308   an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
309
310   assert(!to->final_allocspace);
311   assert(!from->final_allocspace);
312   
313   LIST_UNLINK(from->allocations,an);
314   LIST_LINK_TAIL(to->allocations,an);
315
316   from->interim_allocd -= sz;
317   to->interim_allocd += sz;
318
319   if (to->expires > from->expires) to->expires= from->expires;
320 }
321
322 void *adns__alloc_final(adns_query qu, size_t sz) {
323   /* When we're in the _final stage, we _subtract_ from interim_alloc'd
324    * each allocation, and use final_allocspace to point to the next free
325    * bit.
326    */
327   void *rp;
328
329   sz= MEM_ROUND(sz);
330   rp= qu->final_allocspace;
331   assert(rp);
332   qu->interim_allocd -= sz;
333   assert(qu->interim_allocd>=0);
334   qu->final_allocspace= (byte*)rp + sz;
335   return rp;
336 }
337
338 static void cancel_children(adns_query qu) {
339   adns_query cqu, ncqu;
340
341   for (cqu= qu->children.head; cqu; cqu= ncqu) {
342     ncqu= cqu->siblings.next;
343     adns_cancel(cqu);
344   }
345   LIST_INIT(qu->children);
346 }
347
348 void adns__reset_preserved(adns_query qu) {
349   assert(!qu->final_allocspace);
350   cancel_children(qu);
351   qu->answer->nrrs= 0;
352   qu->answer->rrs.untyped= 0;
353   qu->interim_allocd= qu->preserved_allocd;
354 }
355
356 static void free_query_allocs(adns_query qu) {
357   allocnode *an, *ann;
358
359   cancel_children(qu);
360   for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
361   adns__vbuf_free(&qu->vb);
362 }
363
364 void adns_cancel(adns_query qu) {
365   switch (qu->state) {
366   case query_udp: case query_tcpwait: case query_tcpsent:
367     LIST_UNLINK(qu->ads->timew,qu);
368     break;
369   case query_child:
370     LIST_UNLINK(qu->ads->childw,qu);
371     break;
372   case query_done:
373     LIST_UNLINK(qu->ads->output,qu);
374     break;
375   default:
376     abort();
377   }
378   free_query_allocs(qu);
379   free(qu->answer);
380   free(qu);
381 }
382
383 void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
384   time_t max;
385
386   assert(ttl <= MAXTTLBELIEVE);
387   max= now.tv_sec + ttl;
388   if (qu->expires < max) return;
389   qu->expires= max;
390 }
391
392 static void makefinal_query(adns_query qu) {
393   adns_answer *ans;
394   int rrn;
395
396   ans= qu->answer;
397
398   if (qu->interim_allocd) {
399     ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
400     if (!ans) goto x_nomem;
401     qu->answer= ans;
402   }
403
404   qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
405   adns__makefinal_str(qu,&ans->cname);
406   adns__makefinal_str(qu,&ans->owner);
407   
408   if (ans->nrrs) {
409     adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
410
411     for (rrn=0; rrn<ans->nrrs; rrn++)
412       qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
413   }
414   
415   free_query_allocs(qu);
416   return;
417   
418  x_nomem:
419   qu->preserved_allocd= 0;
420   qu->answer->cname= 0;
421   qu->answer->owner= 0;
422   adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */
423
424   qu->answer->status= adns_s_nomemory;
425   free_query_allocs(qu);
426 }
427
428 void adns__query_done(adns_query qu) {
429   adns_answer *ans;
430   adns_query parent;
431
432   qu->id= -1;
433   ans= qu->answer;
434
435   if (qu->flags & adns_qf_owner && qu->flags & adns_qf_search &&
436       ans->status != adns_s_nomemory) {
437     if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
438       adns__query_fail(qu,adns_s_nomemory);
439       return;
440     }
441   }
442
443   if (ans->nrrs && qu->typei->diff_needswap) {
444     if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
445       adns__query_fail(qu,adns_s_nomemory);
446       return;
447     }
448     adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
449                 qu->vb.buf,
450                 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
451                 qu->ads);
452   }
453
454   ans->expires= qu->expires;
455   parent= qu->parent;
456   if (parent) {
457     LIST_UNLINK_PART(parent->children,qu,siblings.);
458     if (!parent->children.head) LIST_UNLINK(qu->ads->childw,parent);
459     qu->ctx.callback(parent,qu);
460     free_query_allocs(qu);
461     free(qu);
462   } else {
463     makefinal_query(qu);
464     LIST_LINK_TAIL(qu->ads->output,qu);
465   }
466 }
467
468 void adns__query_fail(adns_query qu, adns_status stat) {
469   adns__reset_preserved(qu);
470   qu->answer->status= stat;
471   adns__query_done(qu);
472 }
473
474 void adns__makefinal_str(adns_query qu, char **strp) {
475   int l;
476   char *before, *after;
477
478   before= *strp;
479   if (!before) return;
480   l= strlen(before)+1;
481   after= adns__alloc_final(qu,l);
482   memcpy(after,before,l);
483   *strp= after;  
484 }
485
486 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
487   void *before, *after;
488
489   before= *blpp;
490   if (!before) return;
491   after= adns__alloc_final(qu,sz);
492   memcpy(after,before,sz);
493   *blpp= after;
494 }