Author: Alex Badea <vampire@p16.pub.ro>
[ircu2.10.12-pk.git] / adns / 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-2000 Ian Jackson <ian@davenant.greenend.org.uk>
10  *
11  *  It is part of adns, which is
12  *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
13  *    Copyright (C) 1999-2000 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= -2; /* will be overwritten with real id before we leave adns */
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.  Takes over the memory for qumsg_vb.
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_new;
131   int id;
132   adns_status stat;
133
134   stat= adns__mkquery(ads,&qu->vb,&id, owner,ol, typei,flags);
135   if (stat) {
136     if (stat == adns_s_querydomaintoolong && (flags & adns_qf_search)) {
137       adns__search_next(ads,qu,now);
138       return;
139     } else {
140       adns__query_fail(qu,stat);
141       return;
142     }
143   }
144
145   vb_new= qu->vb;
146   adns__vbuf_init(&qu->vb);
147   query_submit(ads,qu, typei,&vb_new,id, flags,now);
148 }
149
150 void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
151   const char *nextentry;
152   adns_status stat;
153   
154   if (qu->search_doneabs<0) {
155     nextentry= 0;
156     qu->search_doneabs= 1;
157   } else {
158     if (qu->search_pos >= ads->nsearchlist) {
159       if (qu->search_doneabs) {
160         stat= adns_s_nxdomain; goto x_fail;
161         return;
162       } else {
163         nextentry= 0;
164         qu->search_doneabs= 1;
165       }
166     } else {
167       nextentry= ads->searchlist[qu->search_pos++];
168     }
169   }
170
171   qu->search_vb.used= qu->search_origlen;
172   if (nextentry) {
173     if (!adns__vbuf_append(&qu->search_vb,".",1) ||
174         !adns__vbuf_appendstr(&qu->search_vb,nextentry)) {
175       stat= adns_s_nomemory; goto x_fail;
176     }
177   }
178
179   free(qu->query_dgram);
180   qu->query_dgram= 0; qu->query_dglen= 0;
181
182   query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now);
183   return;
184   
185 x_fail:
186   adns__query_fail(qu,stat);
187 }
188
189 static int save_owner(adns_query qu, const char *owner, int ol) {
190   /* Returns 1 if OK, otherwise there was no memory. */
191   adns_answer *ans;
192
193   ans= qu->answer;
194   assert(!ans->owner);
195
196   ans->owner= adns__alloc_preserved(qu,ol+1);  if (!ans->owner) return 0;
197
198   memcpy(ans->owner,owner,ol);
199   ans->owner[ol]= 0;
200   return 1;
201 }
202
203 int adns_submit(adns_state ads,
204                 const char *owner,
205                 adns_rrtype type,
206                 adns_queryflags flags,
207                 void *context,
208                 adns_query *query_r) {
209   int r, ol, ndots;
210   adns_status stat;
211   const typeinfo *typei;
212   struct timeval now;
213   adns_query qu;
214   const char *p;
215
216   adns__consistency(ads,0,cc_entex);
217
218   typei= adns__findtype(type);
219   if (!typei) return ENOSYS;
220
221   r= gettimeofday(&now,0); if (r) goto x_errno;
222   qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno;
223   
224   qu->ctx.ext= context;
225   qu->ctx.callback= 0;
226   memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
227
228   *query_r= qu;
229
230   ol= strlen(owner);
231   if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
232   if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
233                                  
234   if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
235     flags &= ~adns_qf_search;
236     qu->flags= flags;
237     ol--;
238   }
239
240   if (flags & adns_qf_search) {
241     r= adns__vbuf_append(&qu->search_vb,owner,ol);
242     if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
243
244     for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
245     qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
246     qu->search_origlen= ol;
247     adns__search_next(ads,qu,now);
248   } else {
249     if (flags & adns_qf_owner) {
250       if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; }
251     }
252     query_simple(ads,qu, owner,ol, typei,flags, now);
253   }
254   adns__autosys(ads,now);
255   adns__consistency(ads,qu,cc_entex);
256   return 0;
257
258  x_adnsfail:
259   adns__query_fail(qu,stat);
260   adns__consistency(ads,qu,cc_entex);
261   return 0;
262
263  x_errno:
264   r= errno;
265   assert(r);
266   adns__consistency(ads,0,cc_entex);
267   return r;
268 }
269
270 int adns_submit_callback(adns_state ads,
271                 const char *owner,
272                 adns_rrtype type,
273                 adns_queryflags flags,
274                 void *context,
275                 adns_query *query_r,
276                 void (*callback) (adns_state, adns_query, void *))
277 {
278   int res = adns_submit(ads, owner, type, flags, context, query_r);
279   if (!res) {
280     assert(query_r);
281     (*query_r)->callback = callback;
282   }
283   return res;
284 }
285
286 int adns_submit_reverse_any(adns_state ads,
287                             const struct sockaddr *addr,
288                             const char *zone,
289                             adns_rrtype type,
290                             adns_queryflags flags,
291                             void *context,
292                             adns_query *query_r) {
293   const unsigned char *iaddr;
294   char *buf, *buf_free;
295   char shortbuf[100];
296   int r, lreq;
297
298   flags &= ~adns_qf_search;
299
300   if (addr->sa_family != AF_INET) return ENOSYS;
301   iaddr= (const unsigned char*) &(((const struct sockaddr_in*)addr) -> sin_addr);
302
303   lreq= strlen(zone) + 4*4 + 1;
304   if (lreq > sizeof(shortbuf)) {
305     buf= malloc(strlen(zone) + 4*4 + 1);
306     if (!buf) return errno;
307     buf_free= buf;
308   } else {
309     buf= shortbuf;
310     buf_free= 0;
311   }
312   sprintf(buf, "%d.%d.%d.%d.%s", iaddr[3], iaddr[2], iaddr[1], iaddr[0], zone);
313
314   r= adns_submit(ads,buf,type,flags,context,query_r);
315   free(buf_free);
316   return r;
317 }
318
319 int adns_submit_reverse(adns_state ads,
320                         const struct sockaddr *addr,
321                         adns_rrtype type,
322                         adns_queryflags flags,
323                         void *context,
324                         adns_query *query_r) {
325   if (type != adns_r_ptr && type != adns_r_ptr_raw) return EINVAL;
326   return adns_submit_reverse_any(ads,addr,"in-addr.arpa",type,flags,context,query_r);
327 }
328
329 int adns_synchronous(adns_state ads,
330                      const char *owner,
331                      adns_rrtype type,
332                      adns_queryflags flags,
333                      adns_answer **answer_r) {
334   adns_query qu;
335   int r;
336   
337   r= adns_submit(ads,owner,type,flags,0,&qu);
338   if (r) return r;
339
340   r= adns_wait(ads,&qu,answer_r,0);
341   if (r) adns_cancel(qu);
342
343   return r;
344 }
345
346 static void *alloc_common(adns_query qu, size_t sz) {
347   allocnode *an;
348
349   if (!sz) return qu; /* Any old pointer will do */
350   assert(!qu->final_allocspace);
351   an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
352   if (!an) return 0;
353   LIST_LINK_TAIL(qu->allocations,an);
354   return (byte*)an + MEM_ROUND(sizeof(*an));
355 }
356
357 void *adns__alloc_interim(adns_query qu, size_t sz) {
358   void *rv;
359   
360   sz= MEM_ROUND(sz);
361   rv= alloc_common(qu,sz);
362   if (!rv) return 0;
363   qu->interim_allocd += sz;
364   return rv;
365 }
366
367 void *adns__alloc_preserved(adns_query qu, size_t sz) {
368   void *rv;
369   
370   sz= MEM_ROUND(sz);
371   rv= adns__alloc_interim(qu,sz);
372   if (!rv) return 0;
373   qu->preserved_allocd += sz;
374   return rv;
375 }
376
377 void *adns__alloc_mine(adns_query qu, size_t sz) {
378   return alloc_common(qu,MEM_ROUND(sz));
379 }
380
381 void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
382   allocnode *an;
383
384   if (!block) return;
385   an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
386
387   assert(!to->final_allocspace);
388   assert(!from->final_allocspace);
389   
390   LIST_UNLINK(from->allocations,an);
391   LIST_LINK_TAIL(to->allocations,an);
392
393   sz= MEM_ROUND(sz);
394   from->interim_allocd -= sz;
395   to->interim_allocd += sz;
396
397   if (to->expires > from->expires) to->expires= from->expires;
398 }
399
400 void *adns__alloc_final(adns_query qu, size_t sz) {
401   /* When we're in the _final stage, we _subtract_ from interim_alloc'd
402    * each allocation, and use final_allocspace to point to the next free
403    * bit.
404    */
405   void *rp;
406
407   sz= MEM_ROUND(sz);
408   rp= qu->final_allocspace;
409   assert(rp);
410   qu->interim_allocd -= sz;
411   assert(qu->interim_allocd>=0);
412   qu->final_allocspace= (byte*)rp + sz;
413   return rp;
414 }
415
416 static void cancel_children(adns_query qu) {
417   adns_query cqu, ncqu;
418
419   for (cqu= qu->children.head; cqu; cqu= ncqu) {
420     ncqu= cqu->siblings.next;
421     adns_cancel(cqu);
422   }
423 }
424
425 void adns__reset_preserved(adns_query qu) {
426   assert(!qu->final_allocspace);
427   cancel_children(qu);
428   qu->answer->nrrs= 0;
429   qu->answer->rrs.untyped= 0;
430   qu->interim_allocd= qu->preserved_allocd;
431 }
432
433 static void free_query_allocs(adns_query qu) {
434   allocnode *an, *ann;
435
436   cancel_children(qu);
437   for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
438   LIST_INIT(qu->allocations);
439   adns__vbuf_free(&qu->vb);
440   adns__vbuf_free(&qu->search_vb);
441   free(qu->query_dgram);
442   qu->query_dgram= 0;
443 }
444
445 void adns_cancel(adns_query qu) {
446   adns_state ads;
447
448   ads= qu->ads;
449   adns__consistency(ads,qu,cc_entex);
450   if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.);
451   switch (qu->state) {
452   case query_tosend:
453     LIST_UNLINK(ads->udpw,qu);
454     break;
455   case query_tcpw:
456     LIST_UNLINK(ads->tcpw,qu);
457     break;
458   case query_childw:
459     LIST_UNLINK(ads->childw,qu);
460     break;
461   case query_done:
462     LIST_UNLINK(ads->output,qu);
463     break;
464   default:
465     abort();
466   }
467   free_query_allocs(qu);
468   free(qu->answer);
469   free(qu);
470   adns__consistency(ads,0,cc_entex);
471 }
472
473 void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
474   time_t max;
475
476   assert(ttl <= MAXTTLBELIEVE);
477   max= now.tv_sec + ttl;
478   if (qu->expires < max) return;
479   qu->expires= max;
480 }
481
482 static void makefinal_query(adns_query qu) {
483   adns_answer *ans;
484   int rrn;
485
486   ans= qu->answer;
487
488   if (qu->interim_allocd) {
489     ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
490     if (!ans) goto x_nomem;
491     qu->answer= ans;
492   }
493
494   qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
495   adns__makefinal_str(qu,&ans->cname);
496   adns__makefinal_str(qu,&ans->owner);
497   
498   if (ans->nrrs) {
499     adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
500
501     for (rrn=0; rrn<ans->nrrs; rrn++)
502       qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
503   }
504   
505   free_query_allocs(qu);
506   return;
507   
508  x_nomem:
509   qu->preserved_allocd= 0;
510   qu->answer->cname= 0;
511   qu->answer->owner= 0;
512   adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */
513
514   qu->answer->status= adns_s_nomemory;
515   free_query_allocs(qu);
516 }
517
518 void adns__query_done(adns_query qu) {
519   adns_answer *ans;
520   adns_query parent;
521
522   cancel_children(qu);
523
524   qu->id= -1;
525   ans= qu->answer;
526
527   if (qu->flags & adns_qf_owner && qu->flags & adns_qf_search &&
528       ans->status != adns_s_nomemory) {
529     if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
530       adns__query_fail(qu,adns_s_nomemory);
531       return;
532     }
533   }
534
535   if (ans->nrrs && qu->typei->diff_needswap) {
536     if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
537       adns__query_fail(qu,adns_s_nomemory);
538       return;
539     }
540     adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
541                 qu->vb.buf,
542                 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
543                 qu->ads);
544   }
545
546   ans->expires= qu->expires;
547   parent= qu->parent;
548   if (parent) {
549     LIST_UNLINK_PART(parent->children,qu,siblings.);
550     LIST_UNLINK(qu->ads->childw,parent);
551     qu->ctx.callback(parent,qu);
552     free_query_allocs(qu);
553     free(qu->answer);
554     free(qu);
555   } else {
556     makefinal_query(qu);
557     LIST_LINK_TAIL(qu->ads->output,qu);
558     qu->state= query_done;
559     if (qu->callback)
560       qu->callback(qu->ads, qu, qu->ctx.ext);
561   }
562 }
563
564 void adns__query_fail(adns_query qu, adns_status stat) {
565   adns__reset_preserved(qu);
566   qu->answer->status= stat;
567   adns__query_done(qu);
568 }
569
570 void adns__makefinal_str(adns_query qu, char **strp) {
571   int l;
572   char *before, *after;
573
574   before= *strp;
575   if (!before) return;
576   l= strlen(before)+1;
577   after= adns__alloc_final(qu,l);
578   memcpy(after,before,l);
579   *strp= after;  
580 }
581
582 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
583   void *before, *after;
584
585   before= *blpp;
586   if (!before) return;
587   after= adns__alloc_final(qu,sz);
588   memcpy(after,before,sz);
589   *blpp= after;
590 }