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