Clean spec file for Yocto compatibility
[platform/upstream/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
9  *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
10  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
11  *    Copyright (C) 1991 Massachusetts Institute of Technology
12  *  (See the file INSTALL for full details.)
13  *  
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2, or (at your option)
17  *  any later version.
18  *  
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *  
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software Foundation,
26  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
27  */
28
29 #include "internal.h"
30
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
34
35 #include <sys/time.h>
36
37 #include "internal.h"
38
39 #if DMALLOC
40 # include <dmalloc.h>
41 #endif
42
43 static adns_query query_alloc(adns_state ads,
44                               const typeinfo *typei, adns_rrtype type,
45                               adns_queryflags flags, struct timeval now) {
46   /* Allocate a virgin query and return it. */
47   adns_query qu;
48   
49   qu= malloc(sizeof(*qu));  if (!qu) return 0;
50   qu->answer= malloc(sizeof(*qu->answer));
51   if (!qu->answer) { free(qu); return 0; }
52   
53   qu->ads= ads;
54   qu->state= query_tosend;
55   qu->back= qu->next= qu->parent= 0;
56   LIST_INIT(qu->children);
57   LINK_INIT(qu->siblings);
58   LIST_INIT(qu->allocations);
59   qu->interim_allocd= 0;
60   qu->preserved_allocd= 0;
61   qu->final_allocspace= 0;
62
63   qu->typei= typei;
64   qu->query_dgram= 0;
65   qu->query_dglen= 0;
66   adns__vbuf_init(&qu->vb);
67
68   qu->cname_dgram= 0;
69   qu->cname_dglen= qu->cname_begin= 0;
70
71   adns__vbuf_init(&qu->search_vb);
72   qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
73
74   qu->id= -2; /* will be overwritten with real id before we leave adns */
75   qu->flags= flags;
76   qu->retries= 0;
77   qu->udpnextserver= 0;
78   qu->udpsent= 0;
79   timerclear(&qu->timeout);
80   qu->expires= now.tv_sec + MAXTTLBELIEVE;
81
82   memset(&qu->ctx,0,sizeof(qu->ctx));
83   memset(&qu->extra,0,sizeof(qu->extra));
84
85   qu->answer->status= adns_s_ok;
86   qu->answer->cname= qu->answer->owner= 0;
87   qu->answer->type= type;
88   qu->answer->expires= -1;
89   qu->answer->nrrs= 0;
90   qu->answer->rrs.untyped= 0;
91   qu->answer->rrsz= typei->rrsz;
92
93   return qu;
94 }
95
96 static adns_queryflags default_ip6_flags(adns_state ads)
97 {
98   adns_queryflags flags = 0;
99
100   if (!(ads->iflags & adns_if_ip4only))
101     flags |= adns_qf_ip4;
102   if (!(ads->iflags & adns_if_ip6only))
103     flags |= adns_qf_ip6;
104   if (ads->iflags & adns_if_ip6mapped)
105     flags |= adns_qf_ip6mapped;
106
107   return flags;
108 }
109
110 static void query_submit(adns_state ads, adns_query qu,
111                          const typeinfo *typei, vbuf *qumsg_vb, int id,
112                          adns_queryflags flags, struct timeval now) {
113   /* Fills in the query message in for a previously-allocated query,
114    * and submits it.  Cannot fail.  Takes over the memory for qumsg_vb.
115    */
116
117   qu->vb= *qumsg_vb;
118   adns__vbuf_init(qumsg_vb);
119
120   qu->query_dgram= malloc(qu->vb.used);
121   if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; }
122   
123   qu->id= id;
124   qu->query_dglen= qu->vb.used;
125   memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
126   
127   adns__query_send(qu,now);
128 }
129
130 /* FIXME: Take a adns_rrtype type artument? */
131 adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
132                                   const typeinfo *typei, vbuf *qumsg_vb,
133                                   int id,
134                                   adns_queryflags flags, struct timeval now,
135                                   const qcontext *ctx) {
136   adns_query qu;
137
138   if (!(flags & adns__qf_ip_mask))
139     flags |= default_ip6_flags(ads);
140   
141   qu= query_alloc(ads,typei,typei->typekey,flags,now);
142   if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; }
143   *query_r= qu;
144
145   memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
146
147   if (typei->submithook) {
148     qu->vb = *qumsg_vb;
149     adns__vbuf_init(qumsg_vb);
150
151     typei->submithook(qu, flags, now); 
152     if (qu->children.head) {
153       qu->state= query_childw;
154       LIST_LINK_TAIL(ads->childw,qu);
155     }
156   }
157   else query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
158   
159   return adns_s_ok;
160 }
161
162 static void query_simple(adns_state ads, adns_query qu,
163                          const char *owner, int ol,
164                          const typeinfo *typei, adns_queryflags flags,
165                          struct timeval now) {
166   vbuf vb_new;
167   int id;
168   adns_status stat;
169
170   if (typei->submithook) {
171     stat= adns__mkquery_labels(ads, &qu->vb, owner, ol, typei, flags);
172     if (stat) goto fail;
173
174     typei->submithook(qu, flags, now); 
175     if (qu->children.head) {
176       qu->state= query_childw;
177       LIST_LINK_TAIL(ads->childw,qu);
178     }
179     return;
180   }
181   else {
182     stat= adns__mkquery(ads,&qu->vb,&id, owner,ol,
183                         typei,qu->answer->type,flags);
184     if (stat) goto fail;
185
186   vb_new= qu->vb;
187   adns__vbuf_init(&qu->vb);
188   query_submit(ads,qu, typei,&vb_new,id, flags,now);
189     return;
190   }
191  fail:
192   if (stat == adns_s_querydomaintoolong && (flags & adns_qf_search)) 
193     adns__search_next(ads,qu,now);
194   else
195     adns__query_fail(qu,stat);
196 }
197
198 void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
199   const char *nextentry;
200   adns_status stat;
201   
202   if (qu->search_doneabs<0) {
203     nextentry= 0;
204     qu->search_doneabs= 1;
205   } else {
206     if (qu->search_pos >= ads->nsearchlist) {
207       if (qu->search_doneabs) {
208         qu->search_vb.used= qu->search_origlen;
209         stat= adns_s_nxdomain; goto x_fail;
210       } else {
211         nextentry= 0;
212         qu->search_doneabs= 1;
213       }
214     } else {
215       nextentry= ads->searchlist[qu->search_pos++];
216     }
217   }
218
219   qu->search_vb.used= qu->search_origlen;
220   if (nextentry) {
221     if (!adns__vbuf_append(&qu->search_vb,".",1) ||
222         !adns__vbuf_appendstr(&qu->search_vb,nextentry))
223       goto x_nomemory;
224   }
225
226   free(qu->query_dgram);
227   qu->query_dgram= 0; qu->query_dglen= 0;
228
229   query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used,
230                qu->typei, qu->flags, now);
231   return;
232
233 x_nomemory:
234   stat= adns_s_nomemory;
235 x_fail:
236   adns__query_fail(qu,stat);
237 }
238
239 static int save_owner(adns_query qu, const char *owner, int ol) {
240   /* Returns 1 if OK, otherwise there was no memory. */
241   adns_answer *ans;
242
243   if (!(qu->flags & adns_qf_owner)) return 1;
244
245   ans= qu->answer;
246   assert(!ans->owner);
247
248   ans->owner= adns__alloc_preserved(qu,ol+1);  if (!ans->owner) return 0;
249
250   memcpy(ans->owner,owner,ol);
251   ans->owner[ol]= 0;
252   return 1;
253 }
254
255 int adns_submit(adns_state ads,
256                 const char *owner,
257                 adns_rrtype type,
258                 adns_queryflags flags,
259                 void *context,
260                 adns_query *query_r) {
261   int r, ol, ndots;
262   adns_status stat;
263   const typeinfo *typei;
264   struct timeval now;
265   adns_query qu;
266   const char *p;
267
268   adns__consistency(ads,0,cc_entex);
269
270   if (!(flags & adns__qf_ip_mask))
271     flags |= default_ip6_flags(ads);
272
273   typei= adns__findtype(type);
274   if (!typei) return ENOSYS;
275
276   r= gettimeofday(&now,0); if (r) goto x_errno;
277   qu= query_alloc(ads,typei,type,flags,now); if (!qu) goto x_errno;
278   
279   qu->ctx.ext= context;
280   qu->ctx.callback= 0;
281   memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
282
283   *query_r= qu;
284
285   ol= strlen(owner);
286   if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
287   if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
288                                  
289   if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
290     flags &= ~adns_qf_search;
291     qu->flags= flags;
292     ol--;
293   }
294
295   if (flags & adns_qf_search) {
296     r= adns__vbuf_append(&qu->search_vb,owner,ol);
297     if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
298
299     for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
300     qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
301     qu->search_origlen= ol;
302     adns__search_next(ads,qu,now);
303   } else {
304     if (flags & adns_qf_owner) {
305       if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; }
306     }
307     query_simple(ads,qu, owner,ol, typei,flags, now);
308   }
309   adns__autosys(ads,now);
310   adns__consistency(ads,qu,cc_entex);
311   return 0;
312
313  x_adnsfail:
314   adns__query_fail(qu,stat);
315   adns__consistency(ads,qu,cc_entex);
316   return 0;
317
318  x_errno:
319   r= errno;
320   assert(r);
321   adns__consistency(ads,0,cc_entex);
322   return r;
323 }
324
325 int adns_submit_reverse_any(adns_state ads,
326                             const struct sockaddr *addr,
327                             const char *zone,
328                             adns_rrtype type,
329                             adns_queryflags flags,
330                             void *context,
331                             adns_query *query_r) {
332   const unsigned char *iaddr;
333   char *buf, *buf_free;
334   char shortbuf[100];
335   int r, lreq;
336
337   flags &= ~adns_qf_search;
338
339   switch (addr->sa_family) {
340   default: return ENOSYS;
341   case AF_INET: 
342     iaddr= (const unsigned char*) &((const struct sockaddr_in*)addr)->sin_addr;      
343   lreq= strlen(zone) + 4*4 + 1;
344   if (lreq > sizeof(shortbuf)) {
345       buf= malloc(lreq);
346     if (!buf) return errno;
347     buf_free= buf;
348   } else {
349     buf= shortbuf;
350     buf_free= 0;
351   }
352   sprintf(buf, "%d.%d.%d.%d.%s", iaddr[3], iaddr[2], iaddr[1], iaddr[0], zone);
353     break;
354   case AF_INET6:
355     iaddr= (const unsigned char*) &((const struct sockaddr_in6*)addr)->sin6_addr;      
356     lreq = strlen(zone) + 2*32 + 1;
357     if (lreq > sizeof(shortbuf)) {
358       buf= malloc(lreq);
359       if (!buf) return errno;
360       buf_free= buf;
361     }
362     else {
363       buf= shortbuf;
364       buf_free= 0;
365     }
366     strcpy(buf + 2*32, zone);
367     {
368       int i;
369       const unsigned char *p;
370       static const unsigned char hex[16] = "0123456789abcdef";
371       for (i = 0, p = iaddr + 15; i < 2*32; p--) {
372         buf[i++] = hex[*p & 0xf];
373         buf[i++] = '.';
374         buf[i++] = hex[*p / 0x10];
375         buf[i++] = '.';
376       }
377     }
378   }
379   r= adns_submit(ads,buf,type,flags,context,query_r);
380   free(buf_free);
381   return r;
382 }
383
384 int adns_submit_reverse(adns_state ads,
385                         const struct sockaddr *addr,
386                         adns_rrtype type,
387                         adns_queryflags flags,
388                         void *context,
389                         adns_query *query_r) {
390   int r;
391   /* Address record used for forward lookup and consistency check */
392   adns_rr_addr rr;
393   const char *zone;
394   
395   if (type != adns_r_ptr && type != adns_r_ptr_raw) return EINVAL;
396   memset(&rr, 0, sizeof(rr));
397   rr.addr.sa.sa_family = addr->sa_family;
398   
399   switch (addr->sa_family) {
400   default: return ENOSYS;
401   case AF_INET:
402     zone = "in-addr.arpa";
403     rr.len = sizeof(rr.addr.inet);
404     rr.addr.inet.sin_addr = ((const struct sockaddr_in *)addr)->sin_addr;
405     break;
406   case AF_INET6:
407     zone = "ip6.arpa";
408     rr.len = sizeof(rr.addr.inet6);
409     rr.addr.inet6.sin6_addr = ((const struct sockaddr_in6 *)addr)->sin6_addr;
410     break;
411   }
412   
413   r= adns_submit_reverse_any(ads,addr,zone,
414                                  type,flags,context,query_r);
415   if (r) return r;
416   (*query_r)->extra.info.ptr_addr = rr;
417   return 0;
418 }
419
420 int adns_synchronous(adns_state ads,
421                      const char *owner,
422                      adns_rrtype type,
423                      adns_queryflags flags,
424                      adns_answer **answer_r) {
425   adns_query qu;
426   int r;
427   
428   r= adns_submit(ads,owner,type,flags,0,&qu);
429   if (r) return r;
430
431   r= adns_wait(ads,&qu,answer_r,0);
432   if (r) adns_cancel(qu);
433
434   return r;
435 }
436
437 static void *alloc_common(adns_query qu, size_t sz) {
438   allocnode *an;
439
440   if (!sz) return qu; /* Any old pointer will do */
441   assert(!qu->final_allocspace);
442   an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
443   if (!an) return 0;
444   LIST_LINK_TAIL(qu->allocations,an);
445   an->size = sz;
446   return (byte*)an + MEM_ROUND(sizeof(*an));
447 }
448
449 void *adns__realloc_interim(adns_query qu, void *p, size_t sz) {
450   allocnode *an; 
451   allocnode *nan;
452
453   sz = MEM_ROUND(sz);
454   assert(sz); /* Freeing via realloc not supported */
455   assert(!qu->final_allocspace);
456
457   an = (allocnode *) ((byte *) p - MEM_ROUND(sizeof(*an)));
458   assert(an->size <= qu->interim_allocd);
459
460   nan = realloc(an, MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
461   if (!nan) return 0;
462
463   qu->interim_allocd -= nan->size;
464   qu->interim_allocd += sz;
465   nan->size = sz;
466   
467   if (nan->next) nan->next->back = nan;
468   else qu->allocations.tail = nan;
469   if (nan->back) nan->back->next = nan;
470   else qu->allocations.head = nan;
471   
472   return (byte*)nan + MEM_ROUND(sizeof(*nan));
473 }
474
475 void *adns__alloc_interim(adns_query qu, size_t sz) {
476   void *rv;
477   
478   sz= MEM_ROUND(sz);
479   rv= alloc_common(qu,sz);
480   if (!rv) return 0;
481   qu->interim_allocd += sz;
482   return rv;
483 }
484
485 void *adns__alloc_preserved(adns_query qu, size_t sz) {
486   void *rv;
487   
488   sz= MEM_ROUND(sz);
489   rv= adns__alloc_interim(qu,sz);
490   if (!rv) return 0;
491   qu->preserved_allocd += sz;
492   return rv;
493 }
494
495 void *adns__alloc_mine(adns_query qu, size_t sz) {
496   return alloc_common(qu,MEM_ROUND(sz));
497 }
498
499 void adns__transfer_interim(adns_query from, adns_query to,
500                             void *block, size_t sz) {
501   allocnode *an;
502
503   if (!block) return;
504   an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
505
506   assert(!to->final_allocspace);
507   assert(!from->final_allocspace);
508   
509   LIST_UNLINK(from->allocations,an);
510   LIST_LINK_TAIL(to->allocations,an);
511
512   sz= MEM_ROUND(sz);
513   from->interim_allocd -= sz;
514   to->interim_allocd += sz;
515
516   if (to->expires > from->expires) to->expires= from->expires;
517 }
518
519 void *adns__alloc_final(adns_query qu, size_t sz) {
520   /* When we're in the _final stage, we _subtract_ from interim_alloc'd
521    * each allocation, and use final_allocspace to point to the next free
522    * bit.
523    */
524   void *rp;
525
526   sz= MEM_ROUND(sz);
527   rp= qu->final_allocspace;
528   assert(rp);
529   qu->interim_allocd -= sz;
530   assert(qu->interim_allocd>=0);
531   qu->final_allocspace= (byte*)rp + sz;
532   return rp;
533 }
534
535 static void cancel_children(adns_query qu) {
536   adns_query cqu, ncqu;
537
538   for (cqu= qu->children.head; cqu; cqu= ncqu) {
539     ncqu= cqu->siblings.next;
540     adns_cancel(cqu);
541   }
542 }
543
544 void adns__reset_preserved(adns_query qu) {
545   assert(!qu->final_allocspace);
546   cancel_children(qu);
547   qu->answer->nrrs= 0;
548   qu->answer->rrs.untyped= 0;
549   qu->interim_allocd= qu->preserved_allocd;
550 }
551
552 static void free_query_allocs(adns_query qu) {
553   allocnode *an, *ann;
554
555   cancel_children(qu);
556   for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
557   LIST_INIT(qu->allocations);
558   adns__vbuf_free(&qu->vb);
559   adns__vbuf_free(&qu->search_vb);
560   free(qu->query_dgram);
561   qu->query_dgram= 0;
562 }
563
564 void adns_cancel(adns_query qu) {
565   adns_state ads;
566
567   ads= qu->ads;
568   adns__consistency(ads,qu,cc_entex);
569   if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.);
570   switch (qu->state) {
571   case query_tosend:
572     LIST_UNLINK(ads->udpw,qu);
573     break;
574   case query_tcpw:
575     LIST_UNLINK(ads->tcpw,qu);
576     break;
577   case query_childw:
578     LIST_UNLINK(ads->childw,qu);
579     break;
580   case query_done:
581     LIST_UNLINK(ads->output,qu);
582     break;
583   default:
584     abort();
585   }
586   free_query_allocs(qu);
587   free(qu->answer);
588   free(qu);
589   adns__consistency(ads,0,cc_entex);
590 }
591
592 void adns__update_expires(adns_query qu, unsigned long ttl,
593                           struct timeval now) {
594   time_t max;
595
596   assert(ttl <= MAXTTLBELIEVE);
597   max= now.tv_sec + ttl;
598   if (qu->expires < max) return;
599   qu->expires= max;
600 }
601
602 static void makefinal_query(adns_query qu) {
603   adns_answer *ans;
604   int rrn;
605
606   ans= qu->answer;
607
608   if (qu->interim_allocd) {
609     ans= realloc(qu->answer,
610                  MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
611     if (!ans) goto x_nomem;
612     qu->answer= ans;
613   }
614
615   qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
616   adns__makefinal_str(qu,&ans->cname);
617   adns__makefinal_str(qu,&ans->owner);
618   
619   if (ans->nrrs) {
620     adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
621
622     for (rrn=0; rrn<ans->nrrs; rrn++)
623       qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
624   }
625   
626   free_query_allocs(qu);
627   return;
628   
629  x_nomem:
630   qu->preserved_allocd= 0;
631   qu->answer->cname= 0;
632   qu->answer->owner= 0;
633   adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */
634
635   qu->answer->status= adns_s_nomemory;
636   free_query_allocs(qu);
637 }
638
639 void adns__query_done(adns_query qu) {
640   adns_answer *ans;
641   adns_query parent;
642
643   cancel_children(qu);
644
645   qu->id= -1;
646   ans= qu->answer;
647
648   if (qu->flags & adns_qf_search && ans->status != adns_s_nomemory) {
649     if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
650       adns__query_fail(qu,adns_s_nomemory);
651       return;
652     }
653   }
654
655   if (ans->nrrs && qu->typei->diff_needswap) {
656     if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
657       adns__query_fail(qu,adns_s_nomemory);
658       return;
659     }
660     adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
661                 qu->vb.buf,
662                 (int(*)(void*, const void*, const void*))
663                   qu->typei->diff_needswap,
664                 qu->ads);
665   }
666   if (ans->nrrs && qu->typei->postsort) {
667     qu->typei->postsort(qu->ads, ans->rrs.bytes, ans->nrrs, qu->typei);
668   }
669
670   ans->expires= qu->expires;
671   parent= qu->parent;
672   if (parent) {
673     LIST_UNLINK_PART(parent->children,qu,siblings.);
674     LIST_UNLINK(qu->ads->childw,parent);
675     qu->ctx.callback(parent,qu);
676     free_query_allocs(qu);
677     free(qu->answer);
678     free(qu);
679   } else {
680     makefinal_query(qu);
681     LIST_LINK_TAIL(qu->ads->output,qu);
682     qu->state= query_done;
683   }
684 }
685
686 void adns__query_fail(adns_query qu, adns_status stat) {
687   adns__reset_preserved(qu);
688   qu->answer->status= stat;
689   adns__query_done(qu);
690 }
691
692 void adns__makefinal_str(adns_query qu, char **strp) {
693   int l;
694   char *before, *after;
695
696   before= *strp;
697   if (!before) return;
698   l= strlen(before)+1;
699   after= adns__alloc_final(qu,l);
700   memcpy(after,before,l);
701   *strp= after;  
702 }
703
704 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
705   void *before, *after;
706
707   before= *blpp;
708   if (!before) return;
709   after= adns__alloc_final(qu,sz);
710   memcpy(after,before,sz);
711   *blpp= after;
712 }