98db6da3 |
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 | /* |
a79ac5ba |
8 | * This file is |
89435c42 |
9 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
a79ac5ba |
10 | * |
11 | * It is part of adns, which is |
89435c42 |
12 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
c6826df6 |
13 | * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at> |
98db6da3 |
14 | * |
15 | * This program is free software; you can redistribute it and/or modify |
16 | * it under the terms of the GNU General Public License as published by |
17 | * the Free Software Foundation; either version 2, or (at your option) |
18 | * any later version. |
19 | * |
20 | * This program is distributed in the hope that it will be useful, |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | * GNU General Public License for more details. |
24 | * |
25 | * You should have received a copy of the GNU General Public License |
26 | * along with this program; if not, write to the Free Software Foundation, |
27 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
28 | */ |
d05cc330 |
29 | |
48cb0b4b |
30 | #include "internal.h" |
6f17710a |
31 | |
98db6da3 |
32 | #include <stdlib.h> |
33 | #include <unistd.h> |
34 | #include <errno.h> |
6f17710a |
35 | |
98db6da3 |
36 | #include <sys/time.h> |
6f17710a |
37 | |
98db6da3 |
38 | #include "internal.h" |
96e79df5 |
39 | |
7e6a84a1 |
40 | static adns_query query_alloc(adns_state ads, const typeinfo *typei, |
41 | adns_queryflags flags, struct timeval now) { |
42 | /* Allocate a virgin query and return it. */ |
98db6da3 |
43 | adns_query qu; |
7e6a84a1 |
44 | |
45 | qu= malloc(sizeof(*qu)); if (!qu) return 0; |
9c344a42 |
46 | qu->answer= malloc(sizeof(*qu->answer)); |
47 | if (!qu->answer) { free(qu); return 0; } |
7e6a84a1 |
48 | |
11c8bf9b |
49 | qu->ads= ads; |
24d52b13 |
50 | qu->state= query_tosend; |
98db6da3 |
51 | qu->back= qu->next= qu->parent= 0; |
52 | LIST_INIT(qu->children); |
7e6a84a1 |
53 | LINK_INIT(qu->siblings); |
bc01473e |
54 | LIST_INIT(qu->allocations); |
98db6da3 |
55 | qu->interim_allocd= 0; |
1be24aef |
56 | qu->preserved_allocd= 0; |
11c8bf9b |
57 | qu->final_allocspace= 0; |
98db6da3 |
58 | |
11c8bf9b |
59 | qu->typei= typei; |
7e6a84a1 |
60 | qu->query_dgram= 0; |
61 | qu->query_dglen= 0; |
98db6da3 |
62 | adns__vbuf_init(&qu->vb); |
63 | |
64 | qu->cname_dgram= 0; |
65 | qu->cname_dglen= qu->cname_begin= 0; |
7e6a84a1 |
66 | |
67 | adns__vbuf_init(&qu->search_vb); |
68 | qu->search_origlen= qu->search_pos= qu->search_doneabs= 0; |
69 | |
8744cce8 |
70 | qu->id= -2; /* will be overwritten with real id before we leave adns */ |
98db6da3 |
71 | qu->flags= flags; |
d0a057ac |
72 | qu->retries= 0; |
98db6da3 |
73 | qu->udpnextserver= 0; |
d0a057ac |
74 | qu->udpsent= 0; |
98db6da3 |
75 | timerclear(&qu->timeout); |
2c7b101b |
76 | qu->expires= now.tv_sec + MAXTTLBELIEVE; |
98db6da3 |
77 | |
7e6a84a1 |
78 | memset(&qu->ctx,0,sizeof(qu->ctx)); |
79 | |
98db6da3 |
80 | qu->answer->status= adns_s_ok; |
b2988d3e |
81 | qu->answer->cname= qu->answer->owner= 0; |
11c8bf9b |
82 | qu->answer->type= typei->type; |
7e6a84a1 |
83 | qu->answer->expires= -1; |
98db6da3 |
84 | qu->answer->nrrs= 0; |
763d28b9 |
85 | qu->answer->rrs.untyped= 0; |
11c8bf9b |
86 | qu->answer->rrsz= typei->rrsz; |
96e79df5 |
87 | |
7e6a84a1 |
88 | return qu; |
89 | } |
90 | |
91 | static void query_submit(adns_state ads, adns_query qu, |
92 | const typeinfo *typei, vbuf *qumsg_vb, int id, |
93 | adns_queryflags flags, struct timeval now) { |
94 | /* Fills in the query message in for a previously-allocated query, |
fc86e61f |
95 | * and submits it. Cannot fail. Takes over the memory for qumsg_vb. |
7e6a84a1 |
96 | */ |
97 | |
98db6da3 |
98 | qu->vb= *qumsg_vb; |
99 | adns__vbuf_init(qumsg_vb); |
7e6a84a1 |
100 | |
101 | qu->query_dgram= malloc(qu->vb.used); |
102 | if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; } |
103 | |
104 | qu->id= id; |
105 | qu->query_dglen= qu->vb.used; |
106 | memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used); |
98db6da3 |
107 | |
24d52b13 |
108 | adns__query_send(qu,now); |
7e6a84a1 |
109 | } |
110 | |
111 | adns_status adns__internal_submit(adns_state ads, adns_query *query_r, |
9c344a42 |
112 | const typeinfo *typei, vbuf *qumsg_vb, |
113 | int id, |
7e6a84a1 |
114 | adns_queryflags flags, struct timeval now, |
115 | const qcontext *ctx) { |
116 | adns_query qu; |
117 | |
118 | qu= query_alloc(ads,typei,flags,now); |
119 | if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; } |
120 | *query_r= qu; |
98db6da3 |
121 | |
7e6a84a1 |
122 | memcpy(&qu->ctx,ctx,sizeof(qu->ctx)); |
123 | query_submit(ads,qu, typei,qumsg_vb,id,flags,now); |
124 | |
11c8bf9b |
125 | return adns_s_ok; |
7e6a84a1 |
126 | } |
127 | |
128 | static void query_simple(adns_state ads, adns_query qu, |
129 | const char *owner, int ol, |
130 | const typeinfo *typei, adns_queryflags flags, |
131 | struct timeval now) { |
a880d9ba |
132 | vbuf vb_new; |
7e6a84a1 |
133 | int id; |
134 | adns_status stat; |
98db6da3 |
135 | |
a880d9ba |
136 | stat= adns__mkquery(ads,&qu->vb,&id, owner,ol, typei,flags); |
137 | if (stat) { |
138 | if (stat == adns_s_querydomaintoolong && (flags & adns_qf_search)) { |
139 | adns__search_next(ads,qu,now); |
140 | return; |
141 | } else { |
142 | adns__query_fail(qu,stat); |
143 | return; |
144 | } |
145 | } |
146 | |
147 | vb_new= qu->vb; |
148 | adns__vbuf_init(&qu->vb); |
149 | query_submit(ads,qu, typei,&vb_new,id, flags,now); |
7e6a84a1 |
150 | } |
151 | |
152 | void adns__search_next(adns_state ads, adns_query qu, struct timeval now) { |
153 | const char *nextentry; |
154 | adns_status stat; |
155 | |
156 | if (qu->search_doneabs<0) { |
157 | nextentry= 0; |
158 | qu->search_doneabs= 1; |
159 | } else { |
160 | if (qu->search_pos >= ads->nsearchlist) { |
161 | if (qu->search_doneabs) { |
85021e18 |
162 | qu->search_vb.used= qu->search_origlen; |
7e6a84a1 |
163 | stat= adns_s_nxdomain; goto x_fail; |
7e6a84a1 |
164 | } else { |
165 | nextentry= 0; |
166 | qu->search_doneabs= 1; |
167 | } |
168 | } else { |
169 | nextentry= ads->searchlist[qu->search_pos++]; |
170 | } |
171 | } |
172 | |
a5da6822 |
173 | qu->search_vb.used= qu->search_origlen; |
7e6a84a1 |
174 | if (nextentry) { |
175 | if (!adns__vbuf_append(&qu->search_vb,".",1) || |
85021e18 |
176 | !adns__vbuf_appendstr(&qu->search_vb,nextentry)) |
177 | goto x_nomemory; |
7e6a84a1 |
178 | } |
179 | |
180 | free(qu->query_dgram); |
181 | qu->query_dgram= 0; qu->query_dglen= 0; |
182 | |
9c344a42 |
183 | query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, |
184 | qu->typei, qu->flags, now); |
7e6a84a1 |
185 | return; |
85021e18 |
186 | |
187 | x_nomemory: |
188 | stat= adns_s_nomemory; |
7e6a84a1 |
189 | x_fail: |
190 | adns__query_fail(qu,stat); |
48cb0b4b |
191 | } |
192 | |
b2988d3e |
193 | static int save_owner(adns_query qu, const char *owner, int ol) { |
194 | /* Returns 1 if OK, otherwise there was no memory. */ |
195 | adns_answer *ans; |
196 | |
85021e18 |
197 | if (!(qu->flags & adns_qf_owner)) return 1; |
198 | |
b2988d3e |
199 | ans= qu->answer; |
200 | assert(!ans->owner); |
201 | |
1be24aef |
202 | ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0; |
b2988d3e |
203 | |
204 | memcpy(ans->owner,owner,ol); |
205 | ans->owner[ol]= 0; |
206 | return 1; |
207 | } |
208 | |
98db6da3 |
209 | int adns_submit(adns_state ads, |
210 | const char *owner, |
211 | adns_rrtype type, |
edb95764 |
212 | adns_queryflags flags, |
98db6da3 |
213 | void *context, |
214 | adns_query *query_r) { |
7e6a84a1 |
215 | int r, ol, ndots; |
11c8bf9b |
216 | adns_status stat; |
217 | const typeinfo *typei; |
218 | struct timeval now; |
7e6a84a1 |
219 | adns_query qu; |
220 | const char *p; |
98db6da3 |
221 | |
2ac463bf |
222 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
223 | |
11c8bf9b |
224 | typei= adns__findtype(type); |
125de2a9 |
225 | if (!typei) return ENOSYS; |
98db6da3 |
226 | |
7e6a84a1 |
227 | r= gettimeofday(&now,0); if (r) goto x_errno; |
228 | qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno; |
229 | |
230 | qu->ctx.ext= context; |
231 | qu->ctx.callback= 0; |
232 | memset(&qu->ctx.info,0,sizeof(qu->ctx.info)); |
98db6da3 |
233 | |
a5da6822 |
234 | *query_r= qu; |
235 | |
98db6da3 |
236 | ol= strlen(owner); |
7e6a84a1 |
237 | if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; } |
238 | if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; } |
98db6da3 |
239 | |
7683abe2 |
240 | if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) { |
241 | flags &= ~adns_qf_search; |
30c317a4 |
242 | qu->flags= flags; |
7683abe2 |
243 | ol--; |
244 | } |
98db6da3 |
245 | |
7e6a84a1 |
246 | if (flags & adns_qf_search) { |
247 | r= adns__vbuf_append(&qu->search_vb,owner,ol); |
248 | if (!r) { stat= adns_s_nomemory; goto x_adnsfail; } |
249 | |
250 | for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++); |
251 | qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0; |
7e6a84a1 |
252 | qu->search_origlen= ol; |
7e6a84a1 |
253 | adns__search_next(ads,qu,now); |
a5da6822 |
254 | } else { |
b2988d3e |
255 | if (flags & adns_qf_owner) { |
256 | if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; } |
257 | } |
a5da6822 |
258 | query_simple(ads,qu, owner,ol, typei,flags, now); |
7e6a84a1 |
259 | } |
ab898cf4 |
260 | adns__autosys(ads,now); |
2ac463bf |
261 | adns__consistency(ads,qu,cc_entex); |
7e6a84a1 |
262 | return 0; |
263 | |
264 | x_adnsfail: |
265 | adns__query_fail(qu,stat); |
2ac463bf |
266 | adns__consistency(ads,qu,cc_entex); |
7e6a84a1 |
267 | return 0; |
268 | |
269 | x_errno: |
270 | r= errno; |
271 | assert(r); |
2ac463bf |
272 | adns__consistency(ads,0,cc_entex); |
7e6a84a1 |
273 | return r; |
96e79df5 |
274 | } |
275 | |
d2f6d877 |
276 | int adns_submit_reverse_any(adns_state ads, |
277 | const struct sockaddr *addr, |
278 | const char *zone, |
279 | adns_rrtype type, |
280 | adns_queryflags flags, |
281 | void *context, |
282 | adns_query *query_r) { |
5e6342f0 |
283 | const unsigned char *iaddr; |
d2f6d877 |
284 | char *buf, *buf_free; |
285 | char shortbuf[100]; |
286 | int r, lreq; |
5e6342f0 |
287 | |
5e6342f0 |
288 | flags &= ~adns_qf_search; |
289 | |
290 | if (addr->sa_family != AF_INET) return ENOSYS; |
9c344a42 |
291 | iaddr= (const unsigned char*) |
292 | &(((const struct sockaddr_in*)addr) -> sin_addr); |
5e6342f0 |
293 | |
d2f6d877 |
294 | lreq= strlen(zone) + 4*4 + 1; |
295 | if (lreq > sizeof(shortbuf)) { |
296 | buf= malloc(strlen(zone) + 4*4 + 1); |
297 | if (!buf) return errno; |
298 | buf_free= buf; |
299 | } else { |
300 | buf= shortbuf; |
301 | buf_free= 0; |
302 | } |
303 | sprintf(buf, "%d.%d.%d.%d.%s", iaddr[3], iaddr[2], iaddr[1], iaddr[0], zone); |
5e6342f0 |
304 | |
d2f6d877 |
305 | r= adns_submit(ads,buf,type,flags,context,query_r); |
306 | free(buf_free); |
307 | return r; |
308 | } |
309 | |
310 | int adns_submit_reverse(adns_state ads, |
311 | const struct sockaddr *addr, |
312 | adns_rrtype type, |
313 | adns_queryflags flags, |
314 | void *context, |
315 | adns_query *query_r) { |
316 | if (type != adns_r_ptr && type != adns_r_ptr_raw) return EINVAL; |
9c344a42 |
317 | return adns_submit_reverse_any(ads,addr,"in-addr.arpa", |
318 | type,flags,context,query_r); |
5e6342f0 |
319 | } |
320 | |
98db6da3 |
321 | int adns_synchronous(adns_state ads, |
322 | const char *owner, |
323 | adns_rrtype type, |
edb95764 |
324 | adns_queryflags flags, |
98db6da3 |
325 | adns_answer **answer_r) { |
326 | adns_query qu; |
327 | int r; |
328 | |
329 | r= adns_submit(ads,owner,type,flags,0,&qu); |
330 | if (r) return r; |
48cb0b4b |
331 | |
f318f883 |
332 | r= adns_wait(ads,&qu,answer_r,0); |
11c8bf9b |
333 | if (r) adns_cancel(qu); |
96e79df5 |
334 | |
f318f883 |
335 | return r; |
98db6da3 |
336 | } |
337 | |
c9afe7bb |
338 | static void *alloc_common(adns_query qu, size_t sz) { |
98db6da3 |
339 | allocnode *an; |
340 | |
f47cdeec |
341 | if (!sz) return qu; /* Any old pointer will do */ |
98db6da3 |
342 | assert(!qu->final_allocspace); |
98db6da3 |
343 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
f2ad23ee |
344 | if (!an) return 0; |
bc01473e |
345 | LIST_LINK_TAIL(qu->allocations,an); |
98db6da3 |
346 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
347 | } |
48cb0b4b |
348 | |
c9afe7bb |
349 | void *adns__alloc_interim(adns_query qu, size_t sz) { |
1be24aef |
350 | void *rv; |
351 | |
c9afe7bb |
352 | sz= MEM_ROUND(sz); |
1be24aef |
353 | rv= alloc_common(qu,sz); |
354 | if (!rv) return 0; |
c9afe7bb |
355 | qu->interim_allocd += sz; |
1be24aef |
356 | return rv; |
357 | } |
358 | |
359 | void *adns__alloc_preserved(adns_query qu, size_t sz) { |
360 | void *rv; |
361 | |
362 | sz= MEM_ROUND(sz); |
363 | rv= adns__alloc_interim(qu,sz); |
364 | if (!rv) return 0; |
365 | qu->preserved_allocd += sz; |
366 | return rv; |
c9afe7bb |
367 | } |
368 | |
369 | void *adns__alloc_mine(adns_query qu, size_t sz) { |
370 | return alloc_common(qu,MEM_ROUND(sz)); |
371 | } |
372 | |
9c344a42 |
373 | void adns__transfer_interim(adns_query from, adns_query to, |
374 | void *block, size_t sz) { |
bc01473e |
375 | allocnode *an; |
376 | |
377 | if (!block) return; |
378 | an= (void*)((byte*)block - MEM_ROUND(sizeof(*an))); |
379 | |
380 | assert(!to->final_allocspace); |
381 | assert(!from->final_allocspace); |
382 | |
383 | LIST_UNLINK(from->allocations,an); |
384 | LIST_LINK_TAIL(to->allocations,an); |
385 | |
647d5ec1 |
386 | sz= MEM_ROUND(sz); |
bc01473e |
387 | from->interim_allocd -= sz; |
388 | to->interim_allocd += sz; |
2c7b101b |
389 | |
390 | if (to->expires > from->expires) to->expires= from->expires; |
bc01473e |
391 | } |
392 | |
98db6da3 |
393 | void *adns__alloc_final(adns_query qu, size_t sz) { |
394 | /* When we're in the _final stage, we _subtract_ from interim_alloc'd |
395 | * each allocation, and use final_allocspace to point to the next free |
396 | * bit. |
397 | */ |
398 | void *rp; |
399 | |
400 | sz= MEM_ROUND(sz); |
401 | rp= qu->final_allocspace; |
402 | assert(rp); |
403 | qu->interim_allocd -= sz; |
404 | assert(qu->interim_allocd>=0); |
405 | qu->final_allocspace= (byte*)rp + sz; |
406 | return rp; |
407 | } |
408 | |
61093792 |
409 | static void cancel_children(adns_query qu) { |
410 | adns_query cqu, ncqu; |
411 | |
412 | for (cqu= qu->children.head; cqu; cqu= ncqu) { |
413 | ncqu= cqu->siblings.next; |
414 | adns_cancel(cqu); |
415 | } |
61093792 |
416 | } |
417 | |
1be24aef |
418 | void adns__reset_preserved(adns_query qu) { |
9a2b67d4 |
419 | assert(!qu->final_allocspace); |
61093792 |
420 | cancel_children(qu); |
98db6da3 |
421 | qu->answer->nrrs= 0; |
763d28b9 |
422 | qu->answer->rrs.untyped= 0; |
1be24aef |
423 | qu->interim_allocd= qu->preserved_allocd; |
d05cc330 |
424 | } |
425 | |
f318f883 |
426 | static void free_query_allocs(adns_query qu) { |
427 | allocnode *an, *ann; |
f318f883 |
428 | |
61093792 |
429 | cancel_children(qu); |
bc01473e |
430 | for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); } |
9f1041a6 |
431 | LIST_INIT(qu->allocations); |
f318f883 |
432 | adns__vbuf_free(&qu->vb); |
fc86e61f |
433 | adns__vbuf_free(&qu->search_vb); |
b8d9a971 |
434 | free(qu->query_dgram); |
d94fa362 |
435 | qu->query_dgram= 0; |
f318f883 |
436 | } |
437 | |
26eb6bdc |
438 | void adns_cancel(adns_query qu) { |
1389dc72 |
439 | adns_state ads; |
440 | |
441 | ads= qu->ads; |
2ac463bf |
442 | adns__consistency(ads,qu,cc_entex); |
0b772fb9 |
443 | if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.); |
f318f883 |
444 | switch (qu->state) { |
d0a057ac |
445 | case query_tosend: |
446 | LIST_UNLINK(ads->udpw,qu); |
f318f883 |
447 | break; |
d0a057ac |
448 | case query_tcpw: |
449 | LIST_UNLINK(ads->tcpw,qu); |
450 | break; |
451 | case query_childw: |
1389dc72 |
452 | LIST_UNLINK(ads->childw,qu); |
f318f883 |
453 | break; |
454 | case query_done: |
1389dc72 |
455 | LIST_UNLINK(ads->output,qu); |
f318f883 |
456 | break; |
457 | default: |
458 | abort(); |
459 | } |
460 | free_query_allocs(qu); |
461 | free(qu->answer); |
462 | free(qu); |
2ac463bf |
463 | adns__consistency(ads,0,cc_entex); |
f318f883 |
464 | } |
bc01473e |
465 | |
9c344a42 |
466 | void adns__update_expires(adns_query qu, unsigned long ttl, |
467 | struct timeval now) { |
2c7b101b |
468 | time_t max; |
469 | |
470 | assert(ttl <= MAXTTLBELIEVE); |
471 | max= now.tv_sec + ttl; |
472 | if (qu->expires < max) return; |
473 | qu->expires= max; |
474 | } |
475 | |
bc01473e |
476 | static void makefinal_query(adns_query qu) { |
5c596e4d |
477 | adns_answer *ans; |
f318f883 |
478 | int rrn; |
965c9782 |
479 | |
f318f883 |
480 | ans= qu->answer; |
bc01473e |
481 | |
f318f883 |
482 | if (qu->interim_allocd) { |
9c344a42 |
483 | ans= realloc(qu->answer, |
484 | MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd)); |
bc01473e |
485 | if (!ans) goto x_nomem; |
f2ad23ee |
486 | qu->answer= ans; |
487 | } |
965c9782 |
488 | |
f318f883 |
489 | qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans)); |
965c9782 |
490 | adns__makefinal_str(qu,&ans->cname); |
b2988d3e |
491 | adns__makefinal_str(qu,&ans->owner); |
f318f883 |
492 | |
965c9782 |
493 | if (ans->nrrs) { |
f318f883 |
494 | adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz); |
965c9782 |
495 | |
f318f883 |
496 | for (rrn=0; rrn<ans->nrrs; rrn++) |
497 | qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz); |
f318f883 |
498 | } |
2c7b101b |
499 | |
f318f883 |
500 | free_query_allocs(qu); |
bc01473e |
501 | return; |
965c9782 |
502 | |
bc01473e |
503 | x_nomem: |
1be24aef |
504 | qu->preserved_allocd= 0; |
bc01473e |
505 | qu->answer->cname= 0; |
1be24aef |
506 | qu->answer->owner= 0; |
507 | adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */ |
508 | |
509 | qu->answer->status= adns_s_nomemory; |
bc01473e |
510 | free_query_allocs(qu); |
511 | } |
512 | |
513 | void adns__query_done(adns_query qu) { |
514 | adns_answer *ans; |
515 | adns_query parent; |
516 | |
cb32030b |
517 | cancel_children(qu); |
518 | |
d05cc330 |
519 | qu->id= -1; |
bc01473e |
520 | ans= qu->answer; |
521 | |
85021e18 |
522 | if (qu->flags & adns_qf_search && ans->status != adns_s_nomemory) { |
b2988d3e |
523 | if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) { |
524 | adns__query_fail(qu,adns_s_nomemory); |
525 | return; |
526 | } |
527 | } |
528 | |
bc01473e |
529 | if (ans->nrrs && qu->typei->diff_needswap) { |
530 | if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { |
9b86645c |
531 | adns__query_fail(qu,adns_s_nomemory); |
bc01473e |
532 | return; |
533 | } |
534 | adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, |
8c3aa944 |
535 | qu->vb.buf, |
9c344a42 |
536 | (int(*)(void*, const void*, const void*)) |
537 | qu->typei->diff_needswap, |
8c3aa944 |
538 | qu->ads); |
bc01473e |
539 | } |
a095483e |
540 | |
2c7b101b |
541 | ans->expires= qu->expires; |
bc01473e |
542 | parent= qu->parent; |
543 | if (parent) { |
544 | LIST_UNLINK_PART(parent->children,qu,siblings.); |
cb32030b |
545 | LIST_UNLINK(qu->ads->childw,parent); |
cd1bde2f |
546 | qu->ctx.callback(parent,qu); |
bc01473e |
547 | free_query_allocs(qu); |
b8d9a971 |
548 | free(qu->answer); |
bc01473e |
549 | free(qu); |
bc01473e |
550 | } else { |
551 | makefinal_query(qu); |
552 | LIST_LINK_TAIL(qu->ads->output,qu); |
0b772fb9 |
553 | qu->state= query_done; |
bc01473e |
554 | } |
6f17710a |
555 | } |
556 | |
11c8bf9b |
557 | void adns__query_fail(adns_query qu, adns_status stat) { |
1be24aef |
558 | adns__reset_preserved(qu); |
965c9782 |
559 | qu->answer->status= stat; |
11c8bf9b |
560 | adns__query_done(qu); |
6f17710a |
561 | } |
98db6da3 |
562 | |
563 | void adns__makefinal_str(adns_query qu, char **strp) { |
564 | int l; |
565 | char *before, *after; |
566 | |
567 | before= *strp; |
9a2b67d4 |
568 | if (!before) return; |
98db6da3 |
569 | l= strlen(before)+1; |
570 | after= adns__alloc_final(qu,l); |
571 | memcpy(after,before,l); |
572 | *strp= after; |
573 | } |
574 | |
11c8bf9b |
575 | void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) { |
9a2b67d4 |
576 | void *before, *after; |
98db6da3 |
577 | |
9a2b67d4 |
578 | before= *blpp; |
579 | if (!before) return; |
98db6da3 |
580 | after= adns__alloc_final(qu,sz); |
9a2b67d4 |
581 | memcpy(after,before,sz); |
98db6da3 |
582 | *blpp= after; |
583 | } |