3 * - overall query management (allocation, completion)
4 * - per-query memory management
5 * - query submission and cancellation (user-visible and internal)
9 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
11 * It is part of adns, which is
12 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
13 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
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)
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.
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.
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. */
45 qu
= malloc(sizeof(*qu
)); if (!qu
) return 0;
46 qu
->answer
= malloc(sizeof(*qu
->answer
)); if (!qu
->answer
) { free(qu
); return 0; }
49 qu
->state
= query_tosend
;
50 qu
->back
= qu
->next
= qu
->parent
= 0;
51 LIST_INIT(qu
->children
);
52 LINK_INIT(qu
->siblings
);
53 LIST_INIT(qu
->allocations
);
54 qu
->interim_allocd
= 0;
55 qu
->preserved_allocd
= 0;
56 qu
->final_allocspace
= 0;
61 adns__vbuf_init(&qu
->vb
);
64 qu
->cname_dglen
= qu
->cname_begin
= 0;
66 adns__vbuf_init(&qu
->search_vb
);
67 qu
->search_origlen
= qu
->search_pos
= qu
->search_doneabs
= 0;
69 qu
->id
= -2; /* will be overwritten with real id before we leave adns */
74 timerclear(&qu
->timeout
);
75 qu
->expires
= now
.tv_sec
+ MAXTTLBELIEVE
;
77 memset(&qu
->ctx
,0,sizeof(qu
->ctx
));
79 qu
->answer
->status
= adns_s_ok
;
80 qu
->answer
->cname
= qu
->answer
->owner
= 0;
81 qu
->answer
->type
= typei
->type
;
82 qu
->answer
->expires
= -1;
84 qu
->answer
->rrs
.untyped
= 0;
85 qu
->answer
->rrsz
= typei
->rrsz
;
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,
94 * and submits it. Cannot fail. Takes over the memory for qumsg_vb.
98 adns__vbuf_init(qumsg_vb
);
100 qu
->query_dgram
= malloc(qu
->vb
.used
);
101 if (!qu
->query_dgram
) { adns__query_fail(qu
,adns_s_nomemory
); return; }
104 qu
->query_dglen
= qu
->vb
.used
;
105 memcpy(qu
->query_dgram
,qu
->vb
.buf
,qu
->vb
.used
);
107 adns__query_send(qu
,now
);
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
) {
116 qu
= query_alloc(ads
,typei
,flags
,now
);
117 if (!qu
) { adns__vbuf_free(qumsg_vb
); return adns_s_nomemory
; }
120 memcpy(&qu
->ctx
,ctx
,sizeof(qu
->ctx
));
121 query_submit(ads
,qu
, typei
,qumsg_vb
,id
,flags
,now
);
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
) {
134 stat
= adns__mkquery(ads
,&qu
->vb
,&id
, owner
,ol
, typei
,flags
);
136 if (stat
== adns_s_querydomaintoolong
&& (flags
& adns_qf_search
)) {
137 adns__search_next(ads
,qu
,now
);
140 adns__query_fail(qu
,stat
);
146 adns__vbuf_init(&qu
->vb
);
147 query_submit(ads
,qu
, typei
,&vb_new
,id
, flags
,now
);
150 void adns__search_next(adns_state ads
, adns_query qu
, struct timeval now
) {
151 const char *nextentry
;
154 if (qu
->search_doneabs
<0) {
156 qu
->search_doneabs
= 1;
158 if (qu
->search_pos
>= ads
->nsearchlist
) {
159 if (qu
->search_doneabs
) {
160 qu
->search_vb
.used
= qu
->search_origlen
;
161 stat
= adns_s_nxdomain
; goto x_fail
;
164 qu
->search_doneabs
= 1;
167 nextentry
= ads
->searchlist
[qu
->search_pos
++];
171 qu
->search_vb
.used
= qu
->search_origlen
;
173 if (!adns__vbuf_append(&qu
->search_vb
,".",1) ||
174 !adns__vbuf_appendstr(&qu
->search_vb
,nextentry
))
178 free(qu
->query_dgram
);
179 qu
->query_dgram
= 0; qu
->query_dglen
= 0;
181 query_simple(ads
,qu
, qu
->search_vb
.buf
, qu
->search_vb
.used
, qu
->typei
, qu
->flags
, now
);
185 stat
= adns_s_nomemory
;
187 adns__query_fail(qu
,stat
);
190 static int save_owner(adns_query qu
, const char *owner
, int ol
) {
191 /* Returns 1 if OK, otherwise there was no memory. */
194 if (!(qu
->flags
& adns_qf_owner
)) return 1;
199 ans
->owner
= adns__alloc_preserved(qu
,ol
+1); if (!ans
->owner
) return 0;
201 memcpy(ans
->owner
,owner
,ol
);
206 int adns_submit(adns_state ads
,
209 adns_queryflags flags
,
211 adns_query
*query_r
) {
214 const typeinfo
*typei
;
219 adns__consistency(ads
,0,cc_entex
);
221 typei
= adns__findtype(type
);
222 if (!typei
) return ENOSYS
;
224 r
= gettimeofday(&now
,0); if (r
) goto x_errno
;
225 qu
= query_alloc(ads
,typei
,flags
,now
); if (!qu
) goto x_errno
;
227 qu
->ctx
.ext
= context
;
229 memset(&qu
->ctx
.info
,0,sizeof(qu
->ctx
.info
));
234 if (!ol
) { stat
= adns_s_querydomaininvalid
; goto x_adnsfail
; }
235 if (ol
>DNS_MAXDOMAIN
+1) { stat
= adns_s_querydomaintoolong
; goto x_adnsfail
; }
237 if (ol
>=1 && owner
[ol
-1]=='.' && (ol
<2 || owner
[ol
-2]!='\\')) {
238 flags
&= ~adns_qf_search
;
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
; }
247 for (ndots
=0, p
=owner
; (p
= strchr(p
,'.')); p
++, ndots
++);
248 qu
->search_doneabs
= (ndots
>= ads
->searchndots
) ?
-1 : 0;
249 qu
->search_origlen
= ol
;
250 adns__search_next(ads
,qu
,now
);
252 if (flags
& adns_qf_owner
) {
253 if (!save_owner(qu
,owner
,ol
)) { stat
= adns_s_nomemory
; goto x_adnsfail
; }
255 query_simple(ads
,qu
, owner
,ol
, typei
,flags
, now
);
257 adns__autosys(ads
,now
);
258 adns__consistency(ads
,qu
,cc_entex
);
262 adns__query_fail(qu
,stat
);
263 adns__consistency(ads
,qu
,cc_entex
);
269 adns__consistency(ads
,0,cc_entex
);
273 int adns_submit_reverse_any(adns_state ads
,
274 const struct sockaddr
*addr
,
277 adns_queryflags flags
,
279 adns_query
*query_r
) {
280 const unsigned char *iaddr
;
281 char *buf
, *buf_free
;
285 flags
&= ~adns_qf_search
;
287 if (addr
->sa_family
!= AF_INET
) return ENOSYS
;
288 iaddr
= (const unsigned char*) &(((const struct sockaddr_in
*)addr
) -> sin_addr
);
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
;
299 sprintf(buf
, "%d.%d.%d.%d.%s", iaddr
[3], iaddr
[2], iaddr
[1], iaddr
[0], zone
);
301 r
= adns_submit(ads
,buf
,type
,flags
,context
,query_r
);
306 int adns_submit_reverse(adns_state ads
,
307 const struct sockaddr
*addr
,
309 adns_queryflags flags
,
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
);
316 int adns_synchronous(adns_state ads
,
319 adns_queryflags flags
,
320 adns_answer
**answer_r
) {
324 r
= adns_submit(ads
,owner
,type
,flags
,0,&qu
);
327 r
= adns_wait(ads
,&qu
,answer_r
,0);
328 if (r
) adns_cancel(qu
);
333 static void *alloc_common(adns_query qu
, size_t sz
) {
336 if (!sz
) return qu
; /* Any old pointer will do */
337 assert(!qu
->final_allocspace
);
338 an
= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an
)) + sz
));
340 LIST_LINK_TAIL(qu
->allocations
,an
);
341 return (byte
*)an
+ MEM_ROUND(sizeof(*an
));
344 void *adns__alloc_interim(adns_query qu
, size_t sz
) {
348 rv
= alloc_common(qu
,sz
);
350 qu
->interim_allocd
+= sz
;
354 void *adns__alloc_preserved(adns_query qu
, size_t sz
) {
358 rv
= adns__alloc_interim(qu
,sz
);
360 qu
->preserved_allocd
+= sz
;
364 void *adns__alloc_mine(adns_query qu
, size_t sz
) {
365 return alloc_common(qu
,MEM_ROUND(sz
));
368 void adns__transfer_interim(adns_query from
, adns_query to
, void *block
, size_t sz
) {
372 an
= (void*)((byte
*)block
- MEM_ROUND(sizeof(*an
)));
374 assert(!to
->final_allocspace
);
375 assert(!from
->final_allocspace
);
377 LIST_UNLINK(from
->allocations
,an
);
378 LIST_LINK_TAIL(to
->allocations
,an
);
381 from
->interim_allocd
-= sz
;
382 to
->interim_allocd
+= sz
;
384 if (to
->expires
> from
->expires
) to
->expires
= from
->expires
;
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
395 rp
= qu
->final_allocspace
;
397 qu
->interim_allocd
-= sz
;
398 assert(qu
->interim_allocd
>=0);
399 qu
->final_allocspace
= (byte
*)rp
+ sz
;
403 static void cancel_children(adns_query qu
) {
404 adns_query cqu
, ncqu
;
406 for (cqu
= qu
->children
.head
; cqu
; cqu
= ncqu
) {
407 ncqu
= cqu
->siblings
.next
;
412 void adns__reset_preserved(adns_query qu
) {
413 assert(!qu
->final_allocspace
);
416 qu
->answer
->rrs
.untyped
= 0;
417 qu
->interim_allocd
= qu
->preserved_allocd
;
420 static void free_query_allocs(adns_query qu
) {
424 for (an
= qu
->allocations
.head
; an
; an
= ann
) { ann
= an
->next
; free(an
); }
425 LIST_INIT(qu
->allocations
);
426 adns__vbuf_free(&qu
->vb
);
427 adns__vbuf_free(&qu
->search_vb
);
428 free(qu
->query_dgram
);
432 void adns_cancel(adns_query qu
) {
436 adns__consistency(ads
,qu
,cc_entex
);
437 if (qu
->parent
) LIST_UNLINK_PART(qu
->parent
->children
,qu
,siblings
.);
440 LIST_UNLINK(ads
->udpw
,qu
);
443 LIST_UNLINK(ads
->tcpw
,qu
);
446 LIST_UNLINK(ads
->childw
,qu
);
449 LIST_UNLINK(ads
->output
,qu
);
454 free_query_allocs(qu
);
457 adns__consistency(ads
,0,cc_entex
);
460 void adns__update_expires(adns_query qu
, unsigned long ttl
, struct timeval now
) {
463 assert(ttl
<= MAXTTLBELIEVE
);
464 max
= now
.tv_sec
+ ttl
;
465 if (qu
->expires
< max
) return;
469 static void makefinal_query(adns_query qu
) {
475 if (qu
->interim_allocd
) {
476 ans
= realloc(qu
->answer
, MEM_ROUND(MEM_ROUND(sizeof(*ans
)) + qu
->interim_allocd
));
477 if (!ans
) goto x_nomem
;
481 qu
->final_allocspace
= (byte
*)ans
+ MEM_ROUND(sizeof(*ans
));
482 adns__makefinal_str(qu
,&ans
->cname
);
483 adns__makefinal_str(qu
,&ans
->owner
);
486 adns__makefinal_block(qu
, &ans
->rrs
.untyped
, ans
->nrrs
*ans
->rrsz
);
488 for (rrn
=0; rrn
<ans
->nrrs
; rrn
++)
489 qu
->typei
->makefinal(qu
, ans
->rrs
.bytes
+ rrn
*ans
->rrsz
);
492 free_query_allocs(qu
);
496 qu
->preserved_allocd
= 0;
497 qu
->answer
->cname
= 0;
498 qu
->answer
->owner
= 0;
499 adns__reset_preserved(qu
); /* (but we just threw away the preserved stuff) */
501 qu
->answer
->status
= adns_s_nomemory
;
502 free_query_allocs(qu
);
505 void adns__query_done(adns_query qu
) {
514 if (qu
->flags
& adns_qf_search
&& ans
->status
!= adns_s_nomemory
) {
515 if (!save_owner(qu
, qu
->search_vb
.buf
, qu
->search_vb
.used
)) {
516 adns__query_fail(qu
,adns_s_nomemory
);
521 if (ans
->nrrs
&& qu
->typei
->diff_needswap
) {
522 if (!adns__vbuf_ensure(&qu
->vb
,qu
->typei
->rrsz
)) {
523 adns__query_fail(qu
,adns_s_nomemory
);
526 adns__isort(ans
->rrs
.bytes
, ans
->nrrs
, ans
->rrsz
,
528 (int(*)(void*, const void*, const void*))qu
->typei
->diff_needswap
,
532 ans
->expires
= qu
->expires
;
535 LIST_UNLINK_PART(parent
->children
,qu
,siblings
.);
536 LIST_UNLINK(qu
->ads
->childw
,parent
);
537 qu
->ctx
.callback(parent
,qu
);
538 free_query_allocs(qu
);
543 LIST_LINK_TAIL(qu
->ads
->output
,qu
);
544 qu
->state
= query_done
;
548 void adns__query_fail(adns_query qu
, adns_status stat
) {
549 adns__reset_preserved(qu
);
550 qu
->answer
->status
= stat
;
551 adns__query_done(qu
);
554 void adns__makefinal_str(adns_query qu
, char **strp
) {
556 char *before
, *after
;
561 after
= adns__alloc_final(qu
,l
);
562 memcpy(after
,before
,l
);
566 void adns__makefinal_block(adns_query qu
, void **blpp
, size_t sz
) {
567 void *before
, *after
;
571 after
= adns__alloc_final(qu
,sz
);
572 memcpy(after
,before
,sz
);