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