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