chiark / gitweb /
Do PTR checking (multiple PTRs allowed).
[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, 1998 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 int adns__internal_submit(adns_state ads, adns_query *query_r,
37                           const typeinfo *typei, vbuf *qumsg_vb, int id,
38                           adns_queryflags flags, struct timeval now,
39                           adns_status failstat, const qcontext *ctx) {
40   adns_query qu;
41
42   qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory;
43   qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory;
44
45   qu->ads= ads;
46   qu->state= query_udp;
47   qu->back= qu->next= qu->parent= 0;
48   LIST_INIT(qu->children);
49   qu->siblings.next= qu->siblings.back= 0;
50   LIST_INIT(qu->allocations);
51   qu->interim_allocd= 0;
52   qu->final_allocspace= 0;
53
54   qu->typei= typei;
55   adns__vbuf_init(&qu->vb);
56
57   qu->cname_dgram= 0;
58   qu->cname_dglen= qu->cname_begin= 0;
59   
60   qu->id= id;
61   qu->flags= flags;
62   qu->udpretries= 0;
63   qu->udpnextserver= 0;
64   qu->udpsent= qu->tcpfailed= 0;
65   timerclear(&qu->timeout);
66   memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
67
68   qu->answer->status= adns_s_ok;
69   qu->answer->cname= 0;
70   qu->answer->type= typei->type;
71   qu->answer->nrrs= 0;
72   qu->answer->rrs= 0;
73   qu->answer->rrsz= typei->rrsz;
74   
75   *query_r= qu;
76
77   qu->query_dglen= qumsg_vb->used;
78   if (qumsg_vb->used) {
79     qu->query_dgram= malloc(qumsg_vb->used);
80     if (!qu->query_dgram) {
81       adns__query_fail(qu,adns_s_nolocalmem);
82       return adns_s_ok;
83     }
84     memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
85   } else {
86     qu->query_dgram= 0;
87   }
88   qu->vb= *qumsg_vb;
89   adns__vbuf_init(qumsg_vb);
90   
91   if (failstat) {
92     adns__query_fail(qu,failstat);
93     return adns_s_ok;
94   }
95   adns__query_udp(qu,now);
96   adns__autosys(ads,now);
97
98   return adns_s_ok;
99
100  x_freequ_nomemory:
101   free(qu);
102  x_nomemory:
103   adns__vbuf_free(qumsg_vb);
104   return adns_s_nolocalmem;
105 }
106
107 int adns_submit(adns_state ads,
108                 const char *owner,
109                 adns_rrtype type,
110                 adns_queryflags flags,
111                 void *context,
112                 adns_query *query_r) {
113   qcontext ctx;
114   int id, r, ol;
115   vbuf vb;
116   adns_status stat;
117   const typeinfo *typei;
118   struct timeval now;
119
120   typei= adns__findtype(type);
121   if (!typei) return adns_s_notimplemented;
122   
123   ctx.ext= context;
124   ctx.callback= 0;
125   memset(&ctx.info,0,sizeof(ctx.info));
126   
127   r= gettimeofday(&now,0); if (r) return errno;
128   id= 0;
129
130   adns__vbuf_init(&vb);
131
132   ol= strlen(owner);
133   if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_domaintoolong; goto xit; }
134                                  
135   if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
136
137   stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
138                         
139  xit:
140   return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx);        
141 }
142
143 int adns_synchronous(adns_state ads,
144                      const char *owner,
145                      adns_rrtype type,
146                      adns_queryflags flags,
147                      adns_answer **answer_r) {
148   adns_query qu;
149   int r;
150   
151   r= adns_submit(ads,owner,type,flags,0,&qu);
152   if (r) return r;
153
154   r= adns_wait(ads,&qu,answer_r,0);
155   if (r) adns_cancel(qu);
156
157   return r;
158 }
159
160 static void *alloc_common(adns_query qu, size_t sz) {
161   allocnode *an;
162
163   if (!sz) return qu; /* Any old pointer will do */
164   assert(!qu->final_allocspace);
165   an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
166   if (!an) return 0;
167   LIST_LINK_TAIL(qu->allocations,an);
168   return (byte*)an + MEM_ROUND(sizeof(*an));
169 }
170
171 void *adns__alloc_interim(adns_query qu, size_t sz) {
172   sz= MEM_ROUND(sz);
173   qu->interim_allocd += sz;
174   return alloc_common(qu,sz);
175 }
176
177 void *adns__alloc_mine(adns_query qu, size_t sz) {
178   return alloc_common(qu,MEM_ROUND(sz));
179 }
180
181 void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
182   allocnode *an;
183
184   if (!block) return;
185   an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
186
187   assert(!to->final_allocspace);
188   assert(!from->final_allocspace);
189   
190   LIST_UNLINK(from->allocations,an);
191   LIST_LINK_TAIL(to->allocations,an);
192
193   from->interim_allocd -= sz;
194   to->interim_allocd += sz;
195 }
196
197 void *adns__alloc_final(adns_query qu, size_t sz) {
198   /* When we're in the _final stage, we _subtract_ from interim_alloc'd
199    * each allocation, and use final_allocspace to point to the next free
200    * bit.
201    */
202   void *rp;
203
204   sz= MEM_ROUND(sz);
205   rp= qu->final_allocspace;
206   assert(rp);
207   qu->interim_allocd -= sz;
208   assert(qu->interim_allocd>=0);
209   qu->final_allocspace= (byte*)rp + sz;
210   return rp;
211 }
212
213 void adns__reset_cnameonly(adns_query qu) {
214   /* fixme: cancel children */
215   assert(!qu->final_allocspace);
216   qu->answer->nrrs= 0;
217   qu->answer->rrs= 0;
218   qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
219 }
220
221 static void free_query_allocs(adns_query qu) {
222   allocnode *an, *ann;
223   adns_query cqu, ncqu;
224
225   for (cqu= qu->children.head; cqu; cqu= ncqu) {
226     ncqu= cqu->siblings.next;
227     adns_cancel(cqu);
228   }
229   for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
230   adns__vbuf_free(&qu->vb);
231 }
232
233 void adns_cancel(adns_query qu) {
234   switch (qu->state) {
235   case query_udp: case query_tcpwait: case query_tcpsent:
236     LIST_UNLINK(qu->ads->timew,qu);
237     break;
238   case query_child:
239     LIST_UNLINK(qu->ads->childw,qu);
240     break;
241   case query_done:
242     LIST_UNLINK(qu->ads->output,qu);
243     break;
244   default:
245     abort();
246   }
247   free_query_allocs(qu);
248   free(qu->answer);
249   free(qu);
250 }
251
252 static void makefinal_query(adns_query qu) {
253   adns_answer *ans;
254   int rrn;
255
256   ans= qu->answer;
257
258   if (qu->interim_allocd) {
259     ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
260     if (!ans) goto x_nomem;
261     qu->answer= ans;
262   }
263
264   qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
265   adns__makefinal_str(qu,&ans->cname);
266   
267   if (ans->nrrs) {
268     adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
269
270     for (rrn=0; rrn<ans->nrrs; rrn++)
271       qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
272   }
273     
274   free_query_allocs(qu);
275   return;
276   
277  x_nomem:
278   qu->answer->status= adns_s_nolocalmem;
279   qu->answer->cname= 0;
280   adns__reset_cnameonly(qu);
281   free_query_allocs(qu);
282 }
283
284 void adns__query_done(adns_query qu) {
285   adns_answer *ans;
286   adns_query parent;
287
288   qu->id= -1;
289   ans= qu->answer;
290
291   if (ans->nrrs && qu->typei->diff_needswap) {
292     if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
293       adns__query_fail(qu,adns_s_nolocalmem);
294       return;
295     }
296     adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
297                 qu->vb.buf, qu->typei->diff_needswap);
298   }
299
300   parent= qu->parent;
301   if (parent) {
302     LIST_UNLINK_PART(parent->children,qu,siblings.);
303     qu->ctx.callback(parent,qu);
304     free_query_allocs(qu);
305     free(qu);
306   } else {
307     makefinal_query(qu);
308     LIST_LINK_TAIL(qu->ads->output,qu);
309   }
310 }
311
312 void adns__query_fail(adns_query qu, adns_status stat) {
313   adns__reset_cnameonly(qu);
314   qu->answer->status= stat;
315   adns__query_done(qu);
316 }
317
318 void adns__makefinal_str(adns_query qu, char **strp) {
319   int l;
320   char *before, *after;
321
322   before= *strp;
323   if (!before) return;
324   l= strlen(before)+1;
325   after= adns__alloc_final(qu,l);
326   memcpy(after,before,l);
327   *strp= after;  
328 }
329
330 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
331   void *before, *after;
332
333   before= *blpp;
334   if (!before) return;
335   after= adns__alloc_final(qu,sz);
336   memcpy(after,before,sz);
337   *blpp= after;
338 }