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.
30 #ifdef ADNS_JGAA_WIN32
31 # include "adns_win32.h"
36 # include <sys/time.h>
41 static adns_query
query_alloc(adns_state ads
, const typeinfo
*typei
,
42 adns_queryflags flags
, struct timeval now
) {
43 /* Allocate a virgin query and return it. */
46 qu
= malloc(sizeof(*qu
)); if (!qu
) return 0;
47 qu
->answer
= malloc(sizeof(*qu
->answer
)); if (!qu
->answer
) { free(qu
); return 0; }
50 qu
->state
= query_tosend
;
51 qu
->back
= qu
->next
= qu
->parent
= 0;
52 LIST_INIT(qu
->children
);
53 LINK_INIT(qu
->siblings
);
54 LIST_INIT(qu
->allocations
);
55 qu
->interim_allocd
= 0;
56 qu
->preserved_allocd
= 0;
57 qu
->final_allocspace
= 0;
62 adns__vbuf_init(&qu
->vb
);
65 qu
->cname_dglen
= qu
->cname_begin
= 0;
67 adns__vbuf_init(&qu
->search_vb
);
68 qu
->search_origlen
= qu
->search_pos
= qu
->search_doneabs
= 0;
70 qu
->id
= -2; /* will be overwritten with real id before we leave adns */
75 timerclear(&qu
->timeout
);
76 qu
->expires
= now
.tv_sec
+ MAXTTLBELIEVE
;
78 memset(&qu
->ctx
,0,sizeof(qu
->ctx
));
80 qu
->answer
->status
= adns_s_ok
;
81 qu
->answer
->cname
= qu
->answer
->owner
= 0;
82 qu
->answer
->type
= typei
->type
;
83 qu
->answer
->expires
= -1;
85 qu
->answer
->rrs
.untyped
= 0;
86 qu
->answer
->rrsz
= typei
->rrsz
;
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,
95 * and submits it. Cannot fail. Takes over the memory for qumsg_vb.
99 adns__vbuf_init(qumsg_vb
);
101 qu
->query_dgram
= malloc( (size_t) qu
->vb
.used
);
102 if (!qu
->query_dgram
) { adns__query_fail(qu
,adns_s_nomemory
); return; }
105 qu
->query_dglen
= qu
->vb
.used
;
106 memcpy(qu
->query_dgram
,qu
->vb
.buf
,(size_t) qu
->vb
.used
);
108 adns__query_send(qu
,now
);
111 adns_status
adns__internal_submit(adns_state ads
, adns_query
*query_r
,
112 const typeinfo
*typei
, vbuf
*qumsg_vb
, int id
,
113 adns_queryflags flags
, struct timeval now
,
114 const qcontext
*ctx
) {
117 qu
= query_alloc(ads
,typei
,flags
,now
);
118 if (!qu
) { adns__vbuf_free(qumsg_vb
); return adns_s_nomemory
; }
121 memcpy(&qu
->ctx
,ctx
,(size_t) sizeof(qu
->ctx
));
122 query_submit(ads
,qu
, typei
,qumsg_vb
,id
,flags
,now
);
127 static void query_simple(adns_state ads
, adns_query qu
,
128 const char *owner
, int ol
,
129 const typeinfo
*typei
, adns_queryflags flags
,
130 struct timeval now
) {
135 stat
= adns__mkquery(ads
,&qu
->vb
,&id
, owner
,ol
, typei
,flags
);
137 if (stat
== adns_s_querydomaintoolong
&& (flags
& adns_qf_search
)) {
138 adns__search_next(ads
,qu
,now
);
141 adns__query_fail(qu
,stat
);
147 adns__vbuf_init(&qu
->vb
);
148 query_submit(ads
,qu
, typei
,&vb_new
,id
, flags
,now
);
151 void adns__search_next(adns_state ads
, adns_query qu
, struct timeval now
) {
152 const char *nextentry
;
155 if (qu
->search_doneabs
<0) {
157 qu
->search_doneabs
= 1;
159 if (qu
->search_pos
>= ads
->nsearchlist
) {
160 if (qu
->search_doneabs
) {
161 stat
= adns_s_nxdomain
; goto x_fail
;
165 qu
->search_doneabs
= 1;
168 nextentry
= ads
->searchlist
[qu
->search_pos
++];
172 qu
->search_vb
.used
= qu
->search_origlen
;
174 if (!adns__vbuf_append(&qu
->search_vb
,(byte
*)".",1) ||
175 !adns__vbuf_appendstr(&qu
->search_vb
,nextentry
)) {
176 stat
= adns_s_nomemory
; goto x_fail
;
180 free(qu
->query_dgram
);
181 qu
->query_dgram
= 0; qu
->query_dglen
= 0;
183 query_simple(ads
,qu
, (char*)qu
->search_vb
.buf
, qu
->search_vb
.used
, qu
->typei
, qu
->flags
, now
);
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. */
197 ans
->owner
= adns__alloc_preserved(qu
, (size_t) ol
+1); if (!ans
->owner
) return 0;
199 memcpy(ans
->owner
,owner
, (size_t) ol
);
204 int adns_submit(adns_state ads
,
207 adns_queryflags flags
,
209 adns_query
*query_r
) {
212 const typeinfo
*typei
;
217 adns__consistency(ads
,0,cc_entex
);
219 typei
= adns__findtype(type
);
220 if (!typei
) return ENOSYS
;
222 r
= gettimeofday(&now
,0); if (r
) goto x_errno
;
223 qu
= query_alloc(ads
,typei
,flags
,now
); if (!qu
) goto x_errno
;
225 qu
->ctx
.ext
= context
;
227 memset(&qu
->ctx
.info
,0,sizeof(qu
->ctx
.info
));
232 if (!ol
) { stat
= adns_s_querydomaininvalid
; goto x_adnsfail
; }
233 if (ol
>DNS_MAXDOMAIN
+1) { stat
= adns_s_querydomaintoolong
; goto x_adnsfail
; }
235 if (ol
>=1 && owner
[ol
-1]=='.' && (ol
<2 || owner
[ol
-2]!='\\')) {
236 flags
&= ~adns_qf_search
;
241 if (flags
& adns_qf_search
) {
242 r
= adns__vbuf_append(&qu
->search_vb
,(byte
*)owner
,ol
);
243 if (!r
) { stat
= adns_s_nomemory
; goto x_adnsfail
; }
245 for (ndots
=0, p
=owner
; (p
= strchr(p
,'.')); p
++, ndots
++);
246 qu
->search_doneabs
= (ndots
>= ads
->searchndots
) ? -1 : 0;
247 qu
->search_origlen
= ol
;
248 adns__search_next(ads
,qu
,now
);
250 if (flags
& adns_qf_owner
) {
251 if (!save_owner(qu
,owner
,ol
)) { stat
= adns_s_nomemory
; goto x_adnsfail
; }
253 query_simple(ads
,qu
, owner
,ol
, typei
,flags
, now
);
255 adns__autosys(ads
,now
);
256 adns__consistency(ads
,qu
,cc_entex
);
260 adns__query_fail(qu
,stat
);
261 adns__consistency(ads
,qu
,cc_entex
);
267 adns__consistency(ads
,0,cc_entex
);
271 int adns_submit_reverse_any(adns_state ads
,
272 const struct sockaddr
*addr
,
275 adns_queryflags flags
,
277 adns_query
*query_r
) {
278 const unsigned char *iaddr
;
279 char *buf
, *buf_free
;
283 flags
&= ~adns_qf_search
;
285 if (addr
->sa_family
!= AF_INET
) return ENOSYS
;
286 iaddr
= (const unsigned char*) &(((const struct sockaddr_in
*)addr
) -> sin_addr
);
288 lreq
= strlen(zone
) + 4*4 + 1;
289 if (lreq
> sizeof(shortbuf
)) {
290 buf
= malloc( strlen(zone
) + 4*4 + 1 );
291 if (!buf
) return errno
;
297 sprintf(buf
, "%d.%d.%d.%d.%s", iaddr
[3], iaddr
[2], iaddr
[1], iaddr
[0], zone
);
299 r
= adns_submit(ads
,buf
,type
,flags
,context
,query_r
);
304 int adns_submit_reverse(adns_state ads
,
305 const struct sockaddr
*addr
,
307 adns_queryflags flags
,
309 adns_query
*query_r
) {
310 if (type
!= adns_r_ptr
&& type
!= adns_r_ptr_raw
) return EINVAL
;
311 return adns_submit_reverse_any(ads
,addr
,"in-addr.arpa",type
,flags
,context
,query_r
);
314 int adns_synchronous(adns_state ads
,
317 adns_queryflags flags
,
318 adns_answer
**answer_r
) {
322 r
= adns_submit(ads
,owner
,type
,flags
,0,&qu
);
325 r
= adns_wait(ads
,&qu
,answer_r
,0);
326 if (r
) adns_cancel(qu
);
331 static void *alloc_common(adns_query qu
, size_t sz
) {
334 if (!sz
) return qu
; /* Any old pointer will do */
335 assert(!qu
->final_allocspace
);
336 an
= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an
)) + sz
));
338 LIST_LINK_TAIL(qu
->allocations
,an
);
339 return (byte
*)an
+ MEM_ROUND(sizeof(*an
));
342 void *adns__alloc_interim(adns_query qu
, size_t sz
) {
346 rv
= alloc_common(qu
,sz
);
348 qu
->interim_allocd
+= sz
;
352 void *adns__alloc_preserved(adns_query qu
, size_t sz
) {
356 rv
= adns__alloc_interim(qu
,sz
);
358 qu
->preserved_allocd
+= sz
;
362 void *adns__alloc_mine(adns_query qu
, size_t sz
) {
363 return alloc_common(qu
,MEM_ROUND(sz
));
366 void adns__transfer_interim(adns_query from
, adns_query to
, void *block
, size_t sz
) {
370 an
= (void*)((byte
*)block
- MEM_ROUND(sizeof(*an
)));
372 assert(!to
->final_allocspace
);
373 assert(!from
->final_allocspace
);
375 LIST_UNLINK(from
->allocations
,an
);
376 LIST_LINK_TAIL(to
->allocations
,an
);
379 from
->interim_allocd
-= sz
;
380 to
->interim_allocd
+= sz
;
382 if (to
->expires
> from
->expires
) to
->expires
= from
->expires
;
385 void *adns__alloc_final(adns_query qu
, size_t sz
) {
386 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
387 * each allocation, and use final_allocspace to point to the next free
393 rp
= qu
->final_allocspace
;
395 qu
->interim_allocd
-= sz
;
396 assert(qu
->interim_allocd
>=0);
397 qu
->final_allocspace
= (byte
*)rp
+ sz
;
401 static void cancel_children(adns_query qu
) {
402 adns_query cqu
, ncqu
;
404 for (cqu
= qu
->children
.head
; cqu
; cqu
= ncqu
) {
405 ncqu
= cqu
->siblings
.next
;
410 void adns__reset_preserved(adns_query qu
) {
411 assert(!qu
->final_allocspace
);
414 qu
->answer
->rrs
.untyped
= 0;
415 qu
->interim_allocd
= qu
->preserved_allocd
;
418 static void free_query_allocs(adns_query qu
) {
422 for (an
= qu
->allocations
.head
; an
; an
= ann
) { ann
= an
->next
; free(an
); }
423 LIST_INIT(qu
->allocations
);
424 adns__vbuf_free(&qu
->vb
);
425 adns__vbuf_free(&qu
->search_vb
);
426 free(qu
->query_dgram
);
430 void adns_cancel(adns_query qu
) {
434 adns__consistency(ads
,qu
,cc_entex
);
435 if (qu
->parent
) LIST_UNLINK_PART(qu
->parent
->children
,qu
,siblings
.);
438 LIST_UNLINK(ads
->udpw
,qu
);
441 LIST_UNLINK(ads
->tcpw
,qu
);
444 LIST_UNLINK(ads
->childw
,qu
);
447 LIST_UNLINK(ads
->output
,qu
);
452 free_query_allocs(qu
);
455 adns__consistency(ads
,0,cc_entex
);
458 void adns__update_expires(adns_query qu
, unsigned long ttl
, struct timeval now
) {
461 assert(ttl
<= MAXTTLBELIEVE
);
462 max
= now
.tv_sec
+ ttl
;
463 if (qu
->expires
< max
) return;
467 static void makefinal_query(adns_query qu
) {
473 if (qu
->interim_allocd
) {
474 ans
= realloc(qu
->answer
, MEM_ROUND(MEM_ROUND(sizeof(*ans
)) + qu
->interim_allocd
));
475 if (!ans
) goto x_nomem
;
479 qu
->final_allocspace
= (byte
*)ans
+ MEM_ROUND(sizeof(*ans
));
480 adns__makefinal_str(qu
,&ans
->cname
);
481 adns__makefinal_str(qu
,&ans
->owner
);
484 adns__makefinal_block(qu
, &ans
->rrs
.untyped
, (size_t) ans
->nrrs
*ans
->rrsz
);
486 for (rrn
=0; rrn
<ans
->nrrs
; rrn
++)
487 qu
->typei
->makefinal(qu
, ans
->rrs
.bytes
+ rrn
*ans
->rrsz
);
490 free_query_allocs(qu
);
494 qu
->preserved_allocd
= 0;
495 qu
->answer
->cname
= 0;
496 qu
->answer
->owner
= 0;
497 adns__reset_preserved(qu
); /* (but we just threw away the preserved stuff) */
499 qu
->answer
->status
= adns_s_nomemory
;
500 free_query_allocs(qu
);
503 void adns__query_done(adns_query qu
) {
512 if (qu
->flags
& adns_qf_owner
&& qu
->flags
& adns_qf_search
&&
513 ans
->status
!= adns_s_nomemory
) {
514 if (!save_owner(qu
, (char*)qu
->search_vb
.buf
, qu
->search_vb
.used
)) {
515 adns__query_fail(qu
,adns_s_nomemory
);
520 if (ans
->nrrs
&& qu
->typei
->diff_needswap
) {
521 if (!adns__vbuf_ensure(&qu
->vb
,qu
->typei
->rrsz
)) {
522 adns__query_fail(qu
,adns_s_nomemory
);
525 adns__isort(ans
->rrs
.bytes
, ans
->nrrs
, ans
->rrsz
,
527 (int(*)(void*, const void*, const void*))qu
->typei
->diff_needswap
,
531 ans
->expires
= qu
->expires
;
534 LIST_UNLINK_PART(parent
->children
,qu
,siblings
.);
535 LIST_UNLINK(qu
->ads
->childw
,parent
);
536 qu
->ctx
.callback(parent
,qu
);
537 free_query_allocs(qu
);
542 LIST_LINK_TAIL(qu
->ads
->output
,qu
);
543 qu
->state
= query_done
;
547 void adns__query_fail(adns_query qu
, adns_status stat
) {
548 adns__reset_preserved(qu
);
549 qu
->answer
->status
= stat
;
550 adns__query_done(qu
);
553 void adns__makefinal_str(adns_query qu
, char **strp
) {
555 char *before
, *after
;
560 after
= adns__alloc_final(qu
, (size_t) l
);
561 memcpy(after
,before
,(size_t) l
);
565 void adns__makefinal_block(adns_query qu
, void **blpp
, size_t sz
) {
566 void *before
, *after
;
570 after
= adns__alloc_final(qu
,sz
);
571 memcpy(after
,before
, (size_t) sz
);