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