e576be50 |
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 | /* |
a719a4be |
8 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
e576be50 |
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 | */ |
4353a5c4 |
24 | |
4bec51a4 |
25 | #include "internal.h" |
656b2da9 |
26 | |
e576be50 |
27 | #include <stdlib.h> |
28 | #include <unistd.h> |
29 | #include <errno.h> |
656b2da9 |
30 | |
e576be50 |
31 | #include <sys/time.h> |
656b2da9 |
32 | |
e576be50 |
33 | #include "internal.h" |
ddfda861 |
34 | |
660d7d3b |
35 | static adns_query query_alloc(adns_state ads, const typeinfo *typei, |
36 | adns_queryflags flags, struct timeval now) { |
37 | /* Allocate a virgin query and return it. */ |
e576be50 |
38 | adns_query qu; |
660d7d3b |
39 | |
40 | qu= malloc(sizeof(*qu)); if (!qu) return 0; |
41 | qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) { free(qu); return 0; } |
42 | |
3955725c |
43 | qu->ads= ads; |
d8c062fa |
44 | qu->state= query_tosend; |
e576be50 |
45 | qu->back= qu->next= qu->parent= 0; |
46 | LIST_INIT(qu->children); |
660d7d3b |
47 | LINK_INIT(qu->siblings); |
551ff40f |
48 | LIST_INIT(qu->allocations); |
e576be50 |
49 | qu->interim_allocd= 0; |
8b3d55e3 |
50 | qu->preserved_allocd= 0; |
3955725c |
51 | qu->final_allocspace= 0; |
e576be50 |
52 | |
3955725c |
53 | qu->typei= typei; |
660d7d3b |
54 | qu->query_dgram= 0; |
55 | qu->query_dglen= 0; |
e576be50 |
56 | adns__vbuf_init(&qu->vb); |
57 | |
58 | qu->cname_dgram= 0; |
59 | qu->cname_dglen= qu->cname_begin= 0; |
660d7d3b |
60 | |
61 | adns__vbuf_init(&qu->search_vb); |
62 | qu->search_origlen= qu->search_pos= qu->search_doneabs= 0; |
63 | |
64 | qu->id= 0; |
e576be50 |
65 | qu->flags= flags; |
66 | qu->udpretries= 0; |
67 | qu->udpnextserver= 0; |
68 | qu->udpsent= qu->tcpfailed= 0; |
69 | timerclear(&qu->timeout); |
73dba56e |
70 | qu->expires= now.tv_sec + MAXTTLBELIEVE; |
e576be50 |
71 | |
660d7d3b |
72 | memset(&qu->ctx,0,sizeof(qu->ctx)); |
73 | |
e576be50 |
74 | qu->answer->status= adns_s_ok; |
22181a31 |
75 | qu->answer->cname= qu->answer->owner= 0; |
3955725c |
76 | qu->answer->type= typei->type; |
660d7d3b |
77 | qu->answer->expires= -1; |
e576be50 |
78 | qu->answer->nrrs= 0; |
71a6ff46 |
79 | qu->answer->rrs.untyped= 0; |
3955725c |
80 | qu->answer->rrsz= typei->rrsz; |
ddfda861 |
81 | |
660d7d3b |
82 | return qu; |
83 | } |
84 | |
85 | static void query_submit(adns_state ads, adns_query qu, |
86 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
87 | adns_queryflags flags, struct timeval now) { |
88 | /* Fills in the query message in for a previously-allocated query, |
89 | * and submits it. Cannot fail. |
90 | */ |
91 | |
e576be50 |
92 | qu->vb= *qumsg_vb; |
93 | adns__vbuf_init(qumsg_vb); |
660d7d3b |
94 | |
95 | qu->query_dgram= malloc(qu->vb.used); |
96 | if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; } |
97 | |
98 | qu->id= id; |
99 | qu->query_dglen= qu->vb.used; |
100 | memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used); |
e576be50 |
101 | |
d8c062fa |
102 | adns__query_send(qu,now); |
e576be50 |
103 | adns__autosys(ads,now); |
660d7d3b |
104 | } |
105 | |
106 | adns_status adns__internal_submit(adns_state ads, adns_query *query_r, |
107 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
108 | adns_queryflags flags, struct timeval now, |
109 | const qcontext *ctx) { |
110 | adns_query qu; |
111 | |
112 | qu= query_alloc(ads,typei,flags,now); |
113 | if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; } |
114 | *query_r= qu; |
e576be50 |
115 | |
660d7d3b |
116 | memcpy(&qu->ctx,ctx,sizeof(qu->ctx)); |
117 | query_submit(ads,qu, typei,qumsg_vb,id,flags,now); |
118 | |
3955725c |
119 | return adns_s_ok; |
660d7d3b |
120 | } |
121 | |
122 | static void query_simple(adns_state ads, adns_query qu, |
123 | const char *owner, int ol, |
124 | const typeinfo *typei, adns_queryflags flags, |
125 | struct timeval now) { |
126 | vbuf vb; |
127 | int id; |
128 | adns_status stat; |
e576be50 |
129 | |
660d7d3b |
130 | adns__vbuf_init(&vb); |
131 | |
132 | stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags); |
133 | if (stat) { adns__query_fail(qu,stat); return; } |
134 | |
135 | query_submit(ads,qu, typei,&vb,id, flags,now); |
136 | } |
137 | |
138 | void adns__search_next(adns_state ads, adns_query qu, struct timeval now) { |
139 | const char *nextentry; |
140 | adns_status stat; |
141 | |
142 | if (qu->search_doneabs<0) { |
143 | nextentry= 0; |
144 | qu->search_doneabs= 1; |
145 | } else { |
146 | if (qu->search_pos >= ads->nsearchlist) { |
147 | if (qu->search_doneabs) { |
148 | stat= adns_s_nxdomain; goto x_fail; |
149 | return; |
150 | } else { |
151 | nextentry= 0; |
152 | qu->search_doneabs= 1; |
153 | } |
154 | } else { |
155 | nextentry= ads->searchlist[qu->search_pos++]; |
156 | } |
157 | } |
158 | |
e2632eb9 |
159 | qu->search_vb.used= qu->search_origlen; |
660d7d3b |
160 | if (nextentry) { |
161 | if (!adns__vbuf_append(&qu->search_vb,".",1) || |
162 | !adns__vbuf_appendstr(&qu->search_vb,nextentry)) { |
163 | stat= adns_s_nomemory; goto x_fail; |
660d7d3b |
164 | } |
165 | } |
166 | |
167 | free(qu->query_dgram); |
168 | qu->query_dgram= 0; qu->query_dglen= 0; |
169 | |
170 | query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now); |
171 | return; |
172 | |
173 | x_fail: |
174 | adns__query_fail(qu,stat); |
4bec51a4 |
175 | } |
176 | |
22181a31 |
177 | static int save_owner(adns_query qu, const char *owner, int ol) { |
178 | /* Returns 1 if OK, otherwise there was no memory. */ |
179 | adns_answer *ans; |
180 | |
181 | ans= qu->answer; |
182 | assert(!ans->owner); |
183 | |
8b3d55e3 |
184 | ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0; |
22181a31 |
185 | |
186 | memcpy(ans->owner,owner,ol); |
187 | ans->owner[ol]= 0; |
188 | return 1; |
189 | } |
190 | |
e576be50 |
191 | int adns_submit(adns_state ads, |
192 | const char *owner, |
193 | adns_rrtype type, |
194 | adns_queryflags flags, |
195 | void *context, |
196 | adns_query *query_r) { |
660d7d3b |
197 | int r, ol, ndots; |
3955725c |
198 | adns_status stat; |
199 | const typeinfo *typei; |
200 | struct timeval now; |
660d7d3b |
201 | adns_query qu; |
202 | const char *p; |
e576be50 |
203 | |
3e2e5fab |
204 | adns__consistency(ads,cc_entex); |
205 | |
3955725c |
206 | typei= adns__findtype(type); |
636b69b1 |
207 | if (!typei) return ENOSYS; |
e576be50 |
208 | |
660d7d3b |
209 | r= gettimeofday(&now,0); if (r) goto x_errno; |
210 | qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno; |
211 | |
212 | qu->ctx.ext= context; |
213 | qu->ctx.callback= 0; |
214 | memset(&qu->ctx.info,0,sizeof(qu->ctx.info)); |
e576be50 |
215 | |
e2632eb9 |
216 | *query_r= qu; |
217 | |
e576be50 |
218 | ol= strlen(owner); |
660d7d3b |
219 | if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; } |
220 | if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; } |
e576be50 |
221 | |
e9e53c73 |
222 | if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) { |
223 | flags &= ~adns_qf_search; |
224 | ol--; |
225 | } |
e576be50 |
226 | |
660d7d3b |
227 | if (flags & adns_qf_search) { |
228 | r= adns__vbuf_append(&qu->search_vb,owner,ol); |
229 | if (!r) { stat= adns_s_nomemory; goto x_adnsfail; } |
230 | |
231 | for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++); |
232 | qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0; |
660d7d3b |
233 | qu->search_origlen= ol; |
660d7d3b |
234 | adns__search_next(ads,qu,now); |
e2632eb9 |
235 | } else { |
22181a31 |
236 | if (flags & adns_qf_owner) { |
237 | if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; } |
238 | } |
e2632eb9 |
239 | query_simple(ads,qu, owner,ol, typei,flags, now); |
660d7d3b |
240 | } |
3e2e5fab |
241 | adns__consistency(ads,cc_entex); |
660d7d3b |
242 | return 0; |
243 | |
244 | x_adnsfail: |
245 | adns__query_fail(qu,stat); |
3e2e5fab |
246 | adns__consistency(ads,cc_entex); |
660d7d3b |
247 | return 0; |
248 | |
249 | x_errno: |
250 | r= errno; |
251 | assert(r); |
3e2e5fab |
252 | adns__consistency(ads,cc_entex); |
660d7d3b |
253 | return r; |
ddfda861 |
254 | } |
255 | |
e576be50 |
256 | int adns_synchronous(adns_state ads, |
257 | const char *owner, |
258 | adns_rrtype type, |
259 | adns_queryflags flags, |
260 | adns_answer **answer_r) { |
261 | adns_query qu; |
262 | int r; |
263 | |
264 | r= adns_submit(ads,owner,type,flags,0,&qu); |
265 | if (r) return r; |
4bec51a4 |
266 | |
88372443 |
267 | r= adns_wait(ads,&qu,answer_r,0); |
3955725c |
268 | if (r) adns_cancel(qu); |
ddfda861 |
269 | |
88372443 |
270 | return r; |
e576be50 |
271 | } |
272 | |
f759e52e |
273 | static void *alloc_common(adns_query qu, size_t sz) { |
e576be50 |
274 | allocnode *an; |
275 | |
a49a6d7b |
276 | if (!sz) return qu; /* Any old pointer will do */ |
e576be50 |
277 | assert(!qu->final_allocspace); |
e576be50 |
278 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
e062dcae |
279 | if (!an) return 0; |
551ff40f |
280 | LIST_LINK_TAIL(qu->allocations,an); |
e576be50 |
281 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
282 | } |
4bec51a4 |
283 | |
f759e52e |
284 | void *adns__alloc_interim(adns_query qu, size_t sz) { |
8b3d55e3 |
285 | void *rv; |
286 | |
f759e52e |
287 | sz= MEM_ROUND(sz); |
8b3d55e3 |
288 | rv= alloc_common(qu,sz); |
289 | if (!rv) return 0; |
f759e52e |
290 | qu->interim_allocd += sz; |
8b3d55e3 |
291 | return rv; |
292 | } |
293 | |
294 | void *adns__alloc_preserved(adns_query qu, size_t sz) { |
295 | void *rv; |
296 | |
297 | sz= MEM_ROUND(sz); |
298 | rv= adns__alloc_interim(qu,sz); |
299 | if (!rv) return 0; |
300 | qu->preserved_allocd += sz; |
301 | return rv; |
f759e52e |
302 | } |
303 | |
304 | void *adns__alloc_mine(adns_query qu, size_t sz) { |
305 | return alloc_common(qu,MEM_ROUND(sz)); |
306 | } |
307 | |
551ff40f |
308 | void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) { |
309 | allocnode *an; |
310 | |
311 | if (!block) return; |
312 | an= (void*)((byte*)block - MEM_ROUND(sizeof(*an))); |
313 | |
314 | assert(!to->final_allocspace); |
315 | assert(!from->final_allocspace); |
316 | |
317 | LIST_UNLINK(from->allocations,an); |
318 | LIST_LINK_TAIL(to->allocations,an); |
319 | |
320 | from->interim_allocd -= sz; |
321 | to->interim_allocd += sz; |
73dba56e |
322 | |
323 | if (to->expires > from->expires) to->expires= from->expires; |
551ff40f |
324 | } |
325 | |
e576be50 |
326 | void *adns__alloc_final(adns_query qu, size_t sz) { |
327 | /* When we're in the _final stage, we _subtract_ from interim_alloc'd |
328 | * each allocation, and use final_allocspace to point to the next free |
329 | * bit. |
330 | */ |
331 | void *rp; |
332 | |
333 | sz= MEM_ROUND(sz); |
334 | rp= qu->final_allocspace; |
335 | assert(rp); |
336 | qu->interim_allocd -= sz; |
337 | assert(qu->interim_allocd>=0); |
338 | qu->final_allocspace= (byte*)rp + sz; |
339 | return rp; |
340 | } |
341 | |
9ec44266 |
342 | static void cancel_children(adns_query qu) { |
343 | adns_query cqu, ncqu; |
344 | |
345 | for (cqu= qu->children.head; cqu; cqu= ncqu) { |
346 | ncqu= cqu->siblings.next; |
347 | adns_cancel(cqu); |
348 | } |
349 | LIST_INIT(qu->children); |
350 | } |
351 | |
8b3d55e3 |
352 | void adns__reset_preserved(adns_query qu) { |
9557e604 |
353 | assert(!qu->final_allocspace); |
9ec44266 |
354 | cancel_children(qu); |
e576be50 |
355 | qu->answer->nrrs= 0; |
71a6ff46 |
356 | qu->answer->rrs.untyped= 0; |
8b3d55e3 |
357 | qu->interim_allocd= qu->preserved_allocd; |
4353a5c4 |
358 | } |
359 | |
88372443 |
360 | static void free_query_allocs(adns_query qu) { |
361 | allocnode *an, *ann; |
88372443 |
362 | |
9ec44266 |
363 | cancel_children(qu); |
551ff40f |
364 | for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); } |
88372443 |
365 | adns__vbuf_free(&qu->vb); |
366 | } |
367 | |
1dfe95d8 |
368 | void adns_cancel(adns_query qu) { |
3e2e5fab |
369 | adns_state ads; |
370 | |
371 | ads= qu->ads; |
372 | adns__consistency(ads,cc_entex); |
88372443 |
373 | switch (qu->state) { |
d8c062fa |
374 | case query_tosend: case query_tcpwait: case query_tcpsent: |
3e2e5fab |
375 | LIST_UNLINK(ads->timew,qu); |
88372443 |
376 | break; |
377 | case query_child: |
3e2e5fab |
378 | LIST_UNLINK(ads->childw,qu); |
88372443 |
379 | break; |
380 | case query_done: |
3e2e5fab |
381 | LIST_UNLINK(ads->output,qu); |
88372443 |
382 | break; |
383 | default: |
384 | abort(); |
385 | } |
386 | free_query_allocs(qu); |
387 | free(qu->answer); |
388 | free(qu); |
3e2e5fab |
389 | adns__consistency(ads,cc_entex); |
88372443 |
390 | } |
551ff40f |
391 | |
73dba56e |
392 | void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) { |
393 | time_t max; |
394 | |
395 | assert(ttl <= MAXTTLBELIEVE); |
396 | max= now.tv_sec + ttl; |
397 | if (qu->expires < max) return; |
398 | qu->expires= max; |
399 | } |
400 | |
551ff40f |
401 | static void makefinal_query(adns_query qu) { |
98a3f706 |
402 | adns_answer *ans; |
88372443 |
403 | int rrn; |
8e5b0abb |
404 | |
88372443 |
405 | ans= qu->answer; |
551ff40f |
406 | |
88372443 |
407 | if (qu->interim_allocd) { |
88372443 |
408 | ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd)); |
551ff40f |
409 | if (!ans) goto x_nomem; |
e062dcae |
410 | qu->answer= ans; |
411 | } |
8e5b0abb |
412 | |
88372443 |
413 | qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans)); |
8e5b0abb |
414 | adns__makefinal_str(qu,&ans->cname); |
22181a31 |
415 | adns__makefinal_str(qu,&ans->owner); |
88372443 |
416 | |
8e5b0abb |
417 | if (ans->nrrs) { |
88372443 |
418 | adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz); |
8e5b0abb |
419 | |
88372443 |
420 | for (rrn=0; rrn<ans->nrrs; rrn++) |
421 | qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz); |
88372443 |
422 | } |
73dba56e |
423 | |
88372443 |
424 | free_query_allocs(qu); |
551ff40f |
425 | return; |
8e5b0abb |
426 | |
551ff40f |
427 | x_nomem: |
8b3d55e3 |
428 | qu->preserved_allocd= 0; |
551ff40f |
429 | qu->answer->cname= 0; |
8b3d55e3 |
430 | qu->answer->owner= 0; |
431 | adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */ |
432 | |
433 | qu->answer->status= adns_s_nomemory; |
551ff40f |
434 | free_query_allocs(qu); |
435 | } |
436 | |
437 | void adns__query_done(adns_query qu) { |
438 | adns_answer *ans; |
439 | adns_query parent; |
440 | |
4218fb9a |
441 | cancel_children(qu); |
442 | |
4353a5c4 |
443 | qu->id= -1; |
551ff40f |
444 | ans= qu->answer; |
445 | |
22181a31 |
446 | if (qu->flags & adns_qf_owner && qu->flags & adns_qf_search && |
447 | ans->status != adns_s_nomemory) { |
448 | if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) { |
449 | adns__query_fail(qu,adns_s_nomemory); |
450 | return; |
451 | } |
452 | } |
453 | |
551ff40f |
454 | if (ans->nrrs && qu->typei->diff_needswap) { |
455 | if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { |
ea1e31e3 |
456 | adns__query_fail(qu,adns_s_nomemory); |
551ff40f |
457 | return; |
458 | } |
459 | adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, |
09957b1c |
460 | qu->vb.buf, |
461 | (int(*)(void*, const void*, const void*))qu->typei->diff_needswap, |
462 | qu->ads); |
551ff40f |
463 | } |
e553d072 |
464 | |
73dba56e |
465 | ans->expires= qu->expires; |
551ff40f |
466 | parent= qu->parent; |
467 | if (parent) { |
468 | LIST_UNLINK_PART(parent->children,qu,siblings.); |
4218fb9a |
469 | LIST_UNLINK(qu->ads->childw,parent); |
a6536d8b |
470 | qu->ctx.callback(parent,qu); |
551ff40f |
471 | free_query_allocs(qu); |
472 | free(qu); |
551ff40f |
473 | } else { |
474 | makefinal_query(qu); |
475 | LIST_LINK_TAIL(qu->ads->output,qu); |
476 | } |
3e2e5fab |
477 | qu->state= query_done; |
656b2da9 |
478 | } |
479 | |
3955725c |
480 | void adns__query_fail(adns_query qu, adns_status stat) { |
8b3d55e3 |
481 | adns__reset_preserved(qu); |
8e5b0abb |
482 | qu->answer->status= stat; |
3955725c |
483 | adns__query_done(qu); |
656b2da9 |
484 | } |
e576be50 |
485 | |
486 | void adns__makefinal_str(adns_query qu, char **strp) { |
487 | int l; |
488 | char *before, *after; |
489 | |
490 | before= *strp; |
9557e604 |
491 | if (!before) return; |
e576be50 |
492 | l= strlen(before)+1; |
493 | after= adns__alloc_final(qu,l); |
494 | memcpy(after,before,l); |
495 | *strp= after; |
496 | } |
497 | |
3955725c |
498 | void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) { |
9557e604 |
499 | void *before, *after; |
e576be50 |
500 | |
9557e604 |
501 | before= *blpp; |
502 | if (!before) return; |
e576be50 |
503 | after= adns__alloc_final(qu,sz); |
9557e604 |
504 | memcpy(after,before,sz); |
e576be50 |
505 | *blpp= after; |
506 | } |