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 | /* |
d942707d |
8 | * This file is |
3d5cde09 |
9 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
d942707d |
10 | * |
11 | * It is part of adns, which is |
3d5cde09 |
12 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
bef232ae |
13 | * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at> |
e576be50 |
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 | */ |
4353a5c4 |
29 | |
4bec51a4 |
30 | #include "internal.h" |
656b2da9 |
31 | |
e576be50 |
32 | #include <stdlib.h> |
33 | #include <unistd.h> |
34 | #include <errno.h> |
656b2da9 |
35 | |
e576be50 |
36 | #include <sys/time.h> |
656b2da9 |
37 | |
e576be50 |
38 | #include "internal.h" |
ddfda861 |
39 | |
660d7d3b |
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. */ |
e576be50 |
43 | adns_query qu; |
660d7d3b |
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 | |
3955725c |
48 | qu->ads= ads; |
d8c062fa |
49 | qu->state= query_tosend; |
e576be50 |
50 | qu->back= qu->next= qu->parent= 0; |
51 | LIST_INIT(qu->children); |
660d7d3b |
52 | LINK_INIT(qu->siblings); |
551ff40f |
53 | LIST_INIT(qu->allocations); |
e576be50 |
54 | qu->interim_allocd= 0; |
8b3d55e3 |
55 | qu->preserved_allocd= 0; |
3955725c |
56 | qu->final_allocspace= 0; |
e576be50 |
57 | |
3955725c |
58 | qu->typei= typei; |
660d7d3b |
59 | qu->query_dgram= 0; |
60 | qu->query_dglen= 0; |
e576be50 |
61 | adns__vbuf_init(&qu->vb); |
62 | |
63 | qu->cname_dgram= 0; |
64 | qu->cname_dglen= qu->cname_begin= 0; |
660d7d3b |
65 | |
66 | adns__vbuf_init(&qu->search_vb); |
67 | qu->search_origlen= qu->search_pos= qu->search_doneabs= 0; |
68 | |
4fad263d |
69 | qu->id= -2; /* will be overwritten with real id before we leave adns */ |
e576be50 |
70 | qu->flags= flags; |
f7f83b4a |
71 | qu->retries= 0; |
e576be50 |
72 | qu->udpnextserver= 0; |
f7f83b4a |
73 | qu->udpsent= 0; |
e576be50 |
74 | timerclear(&qu->timeout); |
73dba56e |
75 | qu->expires= now.tv_sec + MAXTTLBELIEVE; |
e576be50 |
76 | |
660d7d3b |
77 | memset(&qu->ctx,0,sizeof(qu->ctx)); |
78 | |
e576be50 |
79 | qu->answer->status= adns_s_ok; |
22181a31 |
80 | qu->answer->cname= qu->answer->owner= 0; |
3955725c |
81 | qu->answer->type= typei->type; |
660d7d3b |
82 | qu->answer->expires= -1; |
e576be50 |
83 | qu->answer->nrrs= 0; |
71a6ff46 |
84 | qu->answer->rrs.untyped= 0; |
3955725c |
85 | qu->answer->rrsz= typei->rrsz; |
ddfda861 |
86 | |
660d7d3b |
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, |
914a5ff5 |
94 | * and submits it. Cannot fail. Takes over the memory for qumsg_vb. |
660d7d3b |
95 | */ |
96 | |
e576be50 |
97 | qu->vb= *qumsg_vb; |
98 | adns__vbuf_init(qumsg_vb); |
660d7d3b |
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); |
e576be50 |
106 | |
d8c062fa |
107 | adns__query_send(qu,now); |
660d7d3b |
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; |
e576be50 |
119 | |
660d7d3b |
120 | memcpy(&qu->ctx,ctx,sizeof(qu->ctx)); |
121 | query_submit(ads,qu, typei,qumsg_vb,id,flags,now); |
122 | |
3955725c |
123 | return adns_s_ok; |
660d7d3b |
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) { |
149dd923 |
130 | vbuf vb_new; |
660d7d3b |
131 | int id; |
132 | adns_status stat; |
e576be50 |
133 | |
149dd923 |
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); |
660d7d3b |
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) { |
ac015392 |
160 | qu->search_vb.used= qu->search_origlen; |
660d7d3b |
161 | stat= adns_s_nxdomain; goto x_fail; |
660d7d3b |
162 | } else { |
163 | nextentry= 0; |
164 | qu->search_doneabs= 1; |
165 | } |
166 | } else { |
167 | nextentry= ads->searchlist[qu->search_pos++]; |
168 | } |
169 | } |
170 | |
e2632eb9 |
171 | qu->search_vb.used= qu->search_origlen; |
660d7d3b |
172 | if (nextentry) { |
173 | if (!adns__vbuf_append(&qu->search_vb,".",1) || |
ac015392 |
174 | !adns__vbuf_appendstr(&qu->search_vb,nextentry)) |
175 | goto x_nomemory; |
660d7d3b |
176 | } |
177 | |
178 | free(qu->query_dgram); |
179 | qu->query_dgram= 0; qu->query_dglen= 0; |
180 | |
181 | query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now); |
182 | return; |
ac015392 |
183 | |
184 | x_nomemory: |
185 | stat= adns_s_nomemory; |
660d7d3b |
186 | x_fail: |
187 | adns__query_fail(qu,stat); |
4bec51a4 |
188 | } |
189 | |
22181a31 |
190 | static int save_owner(adns_query qu, const char *owner, int ol) { |
191 | /* Returns 1 if OK, otherwise there was no memory. */ |
192 | adns_answer *ans; |
193 | |
ac015392 |
194 | if (!(qu->flags & adns_qf_owner)) return 1; |
195 | |
22181a31 |
196 | ans= qu->answer; |
197 | assert(!ans->owner); |
198 | |
8b3d55e3 |
199 | ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0; |
22181a31 |
200 | |
201 | memcpy(ans->owner,owner,ol); |
202 | ans->owner[ol]= 0; |
203 | return 1; |
204 | } |
205 | |
e576be50 |
206 | int adns_submit(adns_state ads, |
207 | const char *owner, |
208 | adns_rrtype type, |
e563872a |
209 | adns_queryflags flags, |
e576be50 |
210 | void *context, |
211 | adns_query *query_r) { |
660d7d3b |
212 | int r, ol, ndots; |
3955725c |
213 | adns_status stat; |
214 | const typeinfo *typei; |
215 | struct timeval now; |
660d7d3b |
216 | adns_query qu; |
217 | const char *p; |
e576be50 |
218 | |
28de6442 |
219 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
220 | |
3955725c |
221 | typei= adns__findtype(type); |
636b69b1 |
222 | if (!typei) return ENOSYS; |
e576be50 |
223 | |
660d7d3b |
224 | r= gettimeofday(&now,0); if (r) goto x_errno; |
225 | qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno; |
226 | |
227 | qu->ctx.ext= context; |
228 | qu->ctx.callback= 0; |
229 | memset(&qu->ctx.info,0,sizeof(qu->ctx.info)); |
e576be50 |
230 | |
e2632eb9 |
231 | *query_r= qu; |
232 | |
e576be50 |
233 | ol= strlen(owner); |
660d7d3b |
234 | if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; } |
235 | if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; } |
e576be50 |
236 | |
e9e53c73 |
237 | if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) { |
238 | flags &= ~adns_qf_search; |
3d1ae6ea |
239 | qu->flags= flags; |
e9e53c73 |
240 | ol--; |
241 | } |
e576be50 |
242 | |
660d7d3b |
243 | if (flags & adns_qf_search) { |
244 | r= adns__vbuf_append(&qu->search_vb,owner,ol); |
245 | if (!r) { stat= adns_s_nomemory; goto x_adnsfail; } |
246 | |
247 | for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++); |
248 | qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0; |
660d7d3b |
249 | qu->search_origlen= ol; |
660d7d3b |
250 | adns__search_next(ads,qu,now); |
e2632eb9 |
251 | } else { |
22181a31 |
252 | if (flags & adns_qf_owner) { |
253 | if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; } |
254 | } |
e2632eb9 |
255 | query_simple(ads,qu, owner,ol, typei,flags, now); |
660d7d3b |
256 | } |
70ad7a2a |
257 | adns__autosys(ads,now); |
28de6442 |
258 | adns__consistency(ads,qu,cc_entex); |
660d7d3b |
259 | return 0; |
260 | |
261 | x_adnsfail: |
262 | adns__query_fail(qu,stat); |
28de6442 |
263 | adns__consistency(ads,qu,cc_entex); |
660d7d3b |
264 | return 0; |
265 | |
266 | x_errno: |
267 | r= errno; |
268 | assert(r); |
28de6442 |
269 | adns__consistency(ads,0,cc_entex); |
660d7d3b |
270 | return r; |
ddfda861 |
271 | } |
272 | |
2b1c6979 |
273 | int adns_submit_reverse_any(adns_state ads, |
274 | const struct sockaddr *addr, |
275 | const char *zone, |
276 | adns_rrtype type, |
277 | adns_queryflags flags, |
278 | void *context, |
279 | adns_query *query_r) { |
d7449548 |
280 | const unsigned char *iaddr; |
2b1c6979 |
281 | char *buf, *buf_free; |
282 | char shortbuf[100]; |
283 | int r, lreq; |
d7449548 |
284 | |
d7449548 |
285 | flags &= ~adns_qf_search; |
286 | |
287 | if (addr->sa_family != AF_INET) return ENOSYS; |
288 | iaddr= (const unsigned char*) &(((const struct sockaddr_in*)addr) -> sin_addr); |
289 | |
2b1c6979 |
290 | lreq= strlen(zone) + 4*4 + 1; |
291 | if (lreq > sizeof(shortbuf)) { |
292 | buf= malloc(strlen(zone) + 4*4 + 1); |
293 | if (!buf) return errno; |
294 | buf_free= buf; |
295 | } else { |
296 | buf= shortbuf; |
297 | buf_free= 0; |
298 | } |
299 | sprintf(buf, "%d.%d.%d.%d.%s", iaddr[3], iaddr[2], iaddr[1], iaddr[0], zone); |
d7449548 |
300 | |
2b1c6979 |
301 | r= adns_submit(ads,buf,type,flags,context,query_r); |
302 | free(buf_free); |
303 | return r; |
304 | } |
305 | |
306 | int adns_submit_reverse(adns_state ads, |
307 | const struct sockaddr *addr, |
308 | adns_rrtype type, |
309 | adns_queryflags flags, |
310 | void *context, |
311 | adns_query *query_r) { |
312 | if (type != adns_r_ptr && type != adns_r_ptr_raw) return EINVAL; |
313 | return adns_submit_reverse_any(ads,addr,"in-addr.arpa",type,flags,context,query_r); |
d7449548 |
314 | } |
315 | |
e576be50 |
316 | int adns_synchronous(adns_state ads, |
317 | const char *owner, |
318 | adns_rrtype type, |
e563872a |
319 | adns_queryflags flags, |
e576be50 |
320 | adns_answer **answer_r) { |
321 | adns_query qu; |
322 | int r; |
323 | |
324 | r= adns_submit(ads,owner,type,flags,0,&qu); |
325 | if (r) return r; |
4bec51a4 |
326 | |
88372443 |
327 | r= adns_wait(ads,&qu,answer_r,0); |
3955725c |
328 | if (r) adns_cancel(qu); |
ddfda861 |
329 | |
88372443 |
330 | return r; |
e576be50 |
331 | } |
332 | |
f759e52e |
333 | static void *alloc_common(adns_query qu, size_t sz) { |
e576be50 |
334 | allocnode *an; |
335 | |
a49a6d7b |
336 | if (!sz) return qu; /* Any old pointer will do */ |
e576be50 |
337 | assert(!qu->final_allocspace); |
e576be50 |
338 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
e062dcae |
339 | if (!an) return 0; |
551ff40f |
340 | LIST_LINK_TAIL(qu->allocations,an); |
e576be50 |
341 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
342 | } |
4bec51a4 |
343 | |
f759e52e |
344 | void *adns__alloc_interim(adns_query qu, size_t sz) { |
8b3d55e3 |
345 | void *rv; |
346 | |
f759e52e |
347 | sz= MEM_ROUND(sz); |
8b3d55e3 |
348 | rv= alloc_common(qu,sz); |
349 | if (!rv) return 0; |
f759e52e |
350 | qu->interim_allocd += sz; |
8b3d55e3 |
351 | return rv; |
352 | } |
353 | |
354 | void *adns__alloc_preserved(adns_query qu, size_t sz) { |
355 | void *rv; |
356 | |
357 | sz= MEM_ROUND(sz); |
358 | rv= adns__alloc_interim(qu,sz); |
359 | if (!rv) return 0; |
360 | qu->preserved_allocd += sz; |
361 | return rv; |
f759e52e |
362 | } |
363 | |
364 | void *adns__alloc_mine(adns_query qu, size_t sz) { |
365 | return alloc_common(qu,MEM_ROUND(sz)); |
366 | } |
367 | |
551ff40f |
368 | void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) { |
369 | allocnode *an; |
370 | |
371 | if (!block) return; |
372 | an= (void*)((byte*)block - MEM_ROUND(sizeof(*an))); |
373 | |
374 | assert(!to->final_allocspace); |
375 | assert(!from->final_allocspace); |
376 | |
377 | LIST_UNLINK(from->allocations,an); |
378 | LIST_LINK_TAIL(to->allocations,an); |
379 | |
3dbef075 |
380 | sz= MEM_ROUND(sz); |
551ff40f |
381 | from->interim_allocd -= sz; |
382 | to->interim_allocd += sz; |
73dba56e |
383 | |
384 | if (to->expires > from->expires) to->expires= from->expires; |
551ff40f |
385 | } |
386 | |
e576be50 |
387 | void *adns__alloc_final(adns_query qu, size_t sz) { |
388 | /* When we're in the _final stage, we _subtract_ from interim_alloc'd |
389 | * each allocation, and use final_allocspace to point to the next free |
390 | * bit. |
391 | */ |
392 | void *rp; |
393 | |
394 | sz= MEM_ROUND(sz); |
395 | rp= qu->final_allocspace; |
396 | assert(rp); |
397 | qu->interim_allocd -= sz; |
398 | assert(qu->interim_allocd>=0); |
399 | qu->final_allocspace= (byte*)rp + sz; |
400 | return rp; |
401 | } |
402 | |
9ec44266 |
403 | static void cancel_children(adns_query qu) { |
404 | adns_query cqu, ncqu; |
405 | |
406 | for (cqu= qu->children.head; cqu; cqu= ncqu) { |
407 | ncqu= cqu->siblings.next; |
408 | adns_cancel(cqu); |
409 | } |
9ec44266 |
410 | } |
411 | |
8b3d55e3 |
412 | void adns__reset_preserved(adns_query qu) { |
9557e604 |
413 | assert(!qu->final_allocspace); |
9ec44266 |
414 | cancel_children(qu); |
e576be50 |
415 | qu->answer->nrrs= 0; |
71a6ff46 |
416 | qu->answer->rrs.untyped= 0; |
8b3d55e3 |
417 | qu->interim_allocd= qu->preserved_allocd; |
4353a5c4 |
418 | } |
419 | |
88372443 |
420 | static void free_query_allocs(adns_query qu) { |
421 | allocnode *an, *ann; |
88372443 |
422 | |
9ec44266 |
423 | cancel_children(qu); |
551ff40f |
424 | for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); } |
bfc2c80e |
425 | LIST_INIT(qu->allocations); |
88372443 |
426 | adns__vbuf_free(&qu->vb); |
914a5ff5 |
427 | adns__vbuf_free(&qu->search_vb); |
eed63b97 |
428 | free(qu->query_dgram); |
d1cac7c0 |
429 | qu->query_dgram= 0; |
88372443 |
430 | } |
431 | |
1dfe95d8 |
432 | void adns_cancel(adns_query qu) { |
3e2e5fab |
433 | adns_state ads; |
434 | |
435 | ads= qu->ads; |
28de6442 |
436 | adns__consistency(ads,qu,cc_entex); |
33ae0ddd |
437 | if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.); |
88372443 |
438 | switch (qu->state) { |
f7f83b4a |
439 | case query_tosend: |
440 | LIST_UNLINK(ads->udpw,qu); |
88372443 |
441 | break; |
f7f83b4a |
442 | case query_tcpw: |
443 | LIST_UNLINK(ads->tcpw,qu); |
444 | break; |
445 | case query_childw: |
3e2e5fab |
446 | LIST_UNLINK(ads->childw,qu); |
88372443 |
447 | break; |
448 | case query_done: |
3e2e5fab |
449 | LIST_UNLINK(ads->output,qu); |
88372443 |
450 | break; |
451 | default: |
452 | abort(); |
453 | } |
454 | free_query_allocs(qu); |
455 | free(qu->answer); |
456 | free(qu); |
28de6442 |
457 | adns__consistency(ads,0,cc_entex); |
88372443 |
458 | } |
551ff40f |
459 | |
73dba56e |
460 | void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) { |
461 | time_t max; |
462 | |
463 | assert(ttl <= MAXTTLBELIEVE); |
464 | max= now.tv_sec + ttl; |
465 | if (qu->expires < max) return; |
466 | qu->expires= max; |
467 | } |
468 | |
551ff40f |
469 | static void makefinal_query(adns_query qu) { |
98a3f706 |
470 | adns_answer *ans; |
88372443 |
471 | int rrn; |
8e5b0abb |
472 | |
88372443 |
473 | ans= qu->answer; |
551ff40f |
474 | |
88372443 |
475 | if (qu->interim_allocd) { |
88372443 |
476 | ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd)); |
551ff40f |
477 | if (!ans) goto x_nomem; |
e062dcae |
478 | qu->answer= ans; |
479 | } |
8e5b0abb |
480 | |
88372443 |
481 | qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans)); |
8e5b0abb |
482 | adns__makefinal_str(qu,&ans->cname); |
22181a31 |
483 | adns__makefinal_str(qu,&ans->owner); |
88372443 |
484 | |
8e5b0abb |
485 | if (ans->nrrs) { |
88372443 |
486 | adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz); |
8e5b0abb |
487 | |
88372443 |
488 | for (rrn=0; rrn<ans->nrrs; rrn++) |
489 | qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz); |
88372443 |
490 | } |
73dba56e |
491 | |
88372443 |
492 | free_query_allocs(qu); |
551ff40f |
493 | return; |
8e5b0abb |
494 | |
551ff40f |
495 | x_nomem: |
8b3d55e3 |
496 | qu->preserved_allocd= 0; |
551ff40f |
497 | qu->answer->cname= 0; |
8b3d55e3 |
498 | qu->answer->owner= 0; |
499 | adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */ |
500 | |
501 | qu->answer->status= adns_s_nomemory; |
551ff40f |
502 | free_query_allocs(qu); |
503 | } |
504 | |
505 | void adns__query_done(adns_query qu) { |
506 | adns_answer *ans; |
507 | adns_query parent; |
508 | |
4218fb9a |
509 | cancel_children(qu); |
510 | |
4353a5c4 |
511 | qu->id= -1; |
551ff40f |
512 | ans= qu->answer; |
513 | |
ac015392 |
514 | if (qu->flags & adns_qf_search && ans->status != adns_s_nomemory) { |
22181a31 |
515 | if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) { |
516 | adns__query_fail(qu,adns_s_nomemory); |
517 | return; |
518 | } |
519 | } |
520 | |
551ff40f |
521 | if (ans->nrrs && qu->typei->diff_needswap) { |
522 | if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) { |
ea1e31e3 |
523 | adns__query_fail(qu,adns_s_nomemory); |
551ff40f |
524 | return; |
525 | } |
526 | adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz, |
09957b1c |
527 | qu->vb.buf, |
528 | (int(*)(void*, const void*, const void*))qu->typei->diff_needswap, |
529 | qu->ads); |
551ff40f |
530 | } |
e553d072 |
531 | |
73dba56e |
532 | ans->expires= qu->expires; |
551ff40f |
533 | parent= qu->parent; |
534 | if (parent) { |
535 | LIST_UNLINK_PART(parent->children,qu,siblings.); |
4218fb9a |
536 | LIST_UNLINK(qu->ads->childw,parent); |
a6536d8b |
537 | qu->ctx.callback(parent,qu); |
551ff40f |
538 | free_query_allocs(qu); |
eed63b97 |
539 | free(qu->answer); |
551ff40f |
540 | free(qu); |
551ff40f |
541 | } else { |
542 | makefinal_query(qu); |
543 | LIST_LINK_TAIL(qu->ads->output,qu); |
33ae0ddd |
544 | qu->state= query_done; |
551ff40f |
545 | } |
656b2da9 |
546 | } |
547 | |
3955725c |
548 | void adns__query_fail(adns_query qu, adns_status stat) { |
8b3d55e3 |
549 | adns__reset_preserved(qu); |
8e5b0abb |
550 | qu->answer->status= stat; |
3955725c |
551 | adns__query_done(qu); |
656b2da9 |
552 | } |
e576be50 |
553 | |
554 | void adns__makefinal_str(adns_query qu, char **strp) { |
555 | int l; |
556 | char *before, *after; |
557 | |
558 | before= *strp; |
9557e604 |
559 | if (!before) return; |
e576be50 |
560 | l= strlen(before)+1; |
561 | after= adns__alloc_final(qu,l); |
562 | memcpy(after,before,l); |
563 | *strp= after; |
564 | } |
565 | |
3955725c |
566 | void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) { |
9557e604 |
567 | void *before, *after; |
e576be50 |
568 | |
9557e604 |
569 | before= *blpp; |
570 | if (!before) return; |
e576be50 |
571 | after= adns__alloc_final(qu,sz); |
9557e604 |
572 | memcpy(after,before,sz); |
e576be50 |
573 | *blpp= after; |
574 | } |