chiark / gitweb /
Copyrights now acknowledge fanf for adns. Individual file copyrights too.
[adns] / src / query.c
CommitLineData
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
9 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
10 *
11 * It is part of adns, which is
12 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
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 40static 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
69 qu->id= 0;
98db6da3 70 qu->flags= flags;
71 qu->udpretries= 0;
72 qu->udpnextserver= 0;
73 qu->udpsent= qu->tcpfailed= 0;
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
90static 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.
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);
98db6da3 108 adns__autosys(ads,now);
7e6a84a1 109}
110
111adns_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) {
115 adns_query qu;
116
117 qu= query_alloc(ads,typei,flags,now);
118 if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; }
119 *query_r= qu;
98db6da3 120
7e6a84a1 121 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
122 query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
123
11c8bf9b 124 return adns_s_ok;
7e6a84a1 125}
126
127static 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) {
131 vbuf vb;
132 int id;
133 adns_status stat;
98db6da3 134
7e6a84a1 135 adns__vbuf_init(&vb);
136
137 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
138 if (stat) { adns__query_fail(qu,stat); return; }
139
140 query_submit(ads,qu, typei,&vb,id, flags,now);
141}
142
143void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
144 const char *nextentry;
145 adns_status stat;
146
147 if (qu->search_doneabs<0) {
148 nextentry= 0;
149 qu->search_doneabs= 1;
150 } else {
151 if (qu->search_pos >= ads->nsearchlist) {
152 if (qu->search_doneabs) {
153 stat= adns_s_nxdomain; goto x_fail;
154 return;
155 } else {
156 nextentry= 0;
157 qu->search_doneabs= 1;
158 }
159 } else {
160 nextentry= ads->searchlist[qu->search_pos++];
161 }
162 }
163
a5da6822 164 qu->search_vb.used= qu->search_origlen;
7e6a84a1 165 if (nextentry) {
166 if (!adns__vbuf_append(&qu->search_vb,".",1) ||
167 !adns__vbuf_appendstr(&qu->search_vb,nextentry)) {
168 stat= adns_s_nomemory; goto x_fail;
7e6a84a1 169 }
170 }
171
172 free(qu->query_dgram);
173 qu->query_dgram= 0; qu->query_dglen= 0;
174
175 query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now);
176 return;
177
178x_fail:
179 adns__query_fail(qu,stat);
48cb0b4b 180}
181
b2988d3e 182static int save_owner(adns_query qu, const char *owner, int ol) {
183 /* Returns 1 if OK, otherwise there was no memory. */
184 adns_answer *ans;
185
186 ans= qu->answer;
187 assert(!ans->owner);
188
1be24aef 189 ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0;
b2988d3e 190
191 memcpy(ans->owner,owner,ol);
192 ans->owner[ol]= 0;
193 return 1;
194}
195
98db6da3 196int adns_submit(adns_state ads,
197 const char *owner,
198 adns_rrtype type,
199 adns_queryflags flags,
200 void *context,
201 adns_query *query_r) {
7e6a84a1 202 int r, ol, ndots;
11c8bf9b 203 adns_status stat;
204 const typeinfo *typei;
205 struct timeval now;
7e6a84a1 206 adns_query qu;
207 const char *p;
98db6da3 208
2ac463bf 209 adns__consistency(ads,0,cc_entex);
1389dc72 210
11c8bf9b 211 typei= adns__findtype(type);
125de2a9 212 if (!typei) return ENOSYS;
98db6da3 213
7e6a84a1 214 r= gettimeofday(&now,0); if (r) goto x_errno;
215 qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno;
216
217 qu->ctx.ext= context;
218 qu->ctx.callback= 0;
219 memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
98db6da3 220
a5da6822 221 *query_r= qu;
222
98db6da3 223 ol= strlen(owner);
7e6a84a1 224 if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
225 if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
98db6da3 226
7683abe2 227 if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
228 flags &= ~adns_qf_search;
229 ol--;
230 }
98db6da3 231
7e6a84a1 232 if (flags & adns_qf_search) {
233 r= adns__vbuf_append(&qu->search_vb,owner,ol);
234 if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
235
236 for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
237 qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
7e6a84a1 238 qu->search_origlen= ol;
7e6a84a1 239 adns__search_next(ads,qu,now);
a5da6822 240 } else {
b2988d3e 241 if (flags & adns_qf_owner) {
242 if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; }
243 }
a5da6822 244 query_simple(ads,qu, owner,ol, typei,flags, now);
7e6a84a1 245 }
2ac463bf 246 adns__consistency(ads,qu,cc_entex);
7e6a84a1 247 return 0;
248
249 x_adnsfail:
250 adns__query_fail(qu,stat);
2ac463bf 251 adns__consistency(ads,qu,cc_entex);
7e6a84a1 252 return 0;
253
254 x_errno:
255 r= errno;
256 assert(r);
2ac463bf 257 adns__consistency(ads,0,cc_entex);
7e6a84a1 258 return r;
96e79df5 259}
260
98db6da3 261int adns_synchronous(adns_state ads,
262 const char *owner,
263 adns_rrtype type,
264 adns_queryflags flags,
265 adns_answer **answer_r) {
266 adns_query qu;
267 int r;
268
269 r= adns_submit(ads,owner,type,flags,0,&qu);
270 if (r) return r;
48cb0b4b 271
f318f883 272 r= adns_wait(ads,&qu,answer_r,0);
11c8bf9b 273 if (r) adns_cancel(qu);
96e79df5 274
f318f883 275 return r;
98db6da3 276}
277
c9afe7bb 278static void *alloc_common(adns_query qu, size_t sz) {
98db6da3 279 allocnode *an;
280
f47cdeec 281 if (!sz) return qu; /* Any old pointer will do */
98db6da3 282 assert(!qu->final_allocspace);
98db6da3 283 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
f2ad23ee 284 if (!an) return 0;
bc01473e 285 LIST_LINK_TAIL(qu->allocations,an);
98db6da3 286 return (byte*)an + MEM_ROUND(sizeof(*an));
287}
48cb0b4b 288
c9afe7bb 289void *adns__alloc_interim(adns_query qu, size_t sz) {
1be24aef 290 void *rv;
291
c9afe7bb 292 sz= MEM_ROUND(sz);
1be24aef 293 rv= alloc_common(qu,sz);
294 if (!rv) return 0;
c9afe7bb 295 qu->interim_allocd += sz;
1be24aef 296 return rv;
297}
298
299void *adns__alloc_preserved(adns_query qu, size_t sz) {
300 void *rv;
301
302 sz= MEM_ROUND(sz);
303 rv= adns__alloc_interim(qu,sz);
304 if (!rv) return 0;
305 qu->preserved_allocd += sz;
306 return rv;
c9afe7bb 307}
308
309void *adns__alloc_mine(adns_query qu, size_t sz) {
310 return alloc_common(qu,MEM_ROUND(sz));
311}
312
bc01473e 313void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
314 allocnode *an;
315
316 if (!block) return;
317 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
318
319 assert(!to->final_allocspace);
320 assert(!from->final_allocspace);
321
322 LIST_UNLINK(from->allocations,an);
323 LIST_LINK_TAIL(to->allocations,an);
324
325 from->interim_allocd -= sz;
326 to->interim_allocd += sz;
2c7b101b 327
328 if (to->expires > from->expires) to->expires= from->expires;
bc01473e 329}
330
98db6da3 331void *adns__alloc_final(adns_query qu, size_t sz) {
332 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
333 * each allocation, and use final_allocspace to point to the next free
334 * bit.
335 */
336 void *rp;
337
338 sz= MEM_ROUND(sz);
339 rp= qu->final_allocspace;
340 assert(rp);
341 qu->interim_allocd -= sz;
342 assert(qu->interim_allocd>=0);
343 qu->final_allocspace= (byte*)rp + sz;
344 return rp;
345}
346
61093792 347static void cancel_children(adns_query qu) {
348 adns_query cqu, ncqu;
349
350 for (cqu= qu->children.head; cqu; cqu= ncqu) {
351 ncqu= cqu->siblings.next;
352 adns_cancel(cqu);
353 }
61093792 354}
355
1be24aef 356void adns__reset_preserved(adns_query qu) {
9a2b67d4 357 assert(!qu->final_allocspace);
61093792 358 cancel_children(qu);
98db6da3 359 qu->answer->nrrs= 0;
763d28b9 360 qu->answer->rrs.untyped= 0;
1be24aef 361 qu->interim_allocd= qu->preserved_allocd;
d05cc330 362}
363
f318f883 364static void free_query_allocs(adns_query qu) {
365 allocnode *an, *ann;
f318f883 366
61093792 367 cancel_children(qu);
bc01473e 368 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
9f1041a6 369 LIST_INIT(qu->allocations);
f318f883 370 adns__vbuf_free(&qu->vb);
371}
372
26eb6bdc 373void adns_cancel(adns_query qu) {
1389dc72 374 adns_state ads;
375
376 ads= qu->ads;
2ac463bf 377 adns__consistency(ads,qu,cc_entex);
0b772fb9 378 if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.);
f318f883 379 switch (qu->state) {
24d52b13 380 case query_tosend: case query_tcpwait: case query_tcpsent:
1389dc72 381 LIST_UNLINK(ads->timew,qu);
f318f883 382 break;
383 case query_child:
1389dc72 384 LIST_UNLINK(ads->childw,qu);
f318f883 385 break;
386 case query_done:
1389dc72 387 LIST_UNLINK(ads->output,qu);
f318f883 388 break;
389 default:
390 abort();
391 }
392 free_query_allocs(qu);
393 free(qu->answer);
394 free(qu);
2ac463bf 395 adns__consistency(ads,0,cc_entex);
f318f883 396}
bc01473e 397
2c7b101b 398void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
399 time_t max;
400
401 assert(ttl <= MAXTTLBELIEVE);
402 max= now.tv_sec + ttl;
403 if (qu->expires < max) return;
404 qu->expires= max;
405}
406
bc01473e 407static void makefinal_query(adns_query qu) {
5c596e4d 408 adns_answer *ans;
f318f883 409 int rrn;
965c9782 410
f318f883 411 ans= qu->answer;
bc01473e 412
f318f883 413 if (qu->interim_allocd) {
f318f883 414 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
bc01473e 415 if (!ans) goto x_nomem;
f2ad23ee 416 qu->answer= ans;
417 }
965c9782 418
f318f883 419 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
965c9782 420 adns__makefinal_str(qu,&ans->cname);
b2988d3e 421 adns__makefinal_str(qu,&ans->owner);
f318f883 422
965c9782 423 if (ans->nrrs) {
f318f883 424 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
965c9782 425
f318f883 426 for (rrn=0; rrn<ans->nrrs; rrn++)
427 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
f318f883 428 }
2c7b101b 429
f318f883 430 free_query_allocs(qu);
bc01473e 431 return;
965c9782 432
bc01473e 433 x_nomem:
1be24aef 434 qu->preserved_allocd= 0;
bc01473e 435 qu->answer->cname= 0;
1be24aef 436 qu->answer->owner= 0;
437 adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */
438
439 qu->answer->status= adns_s_nomemory;
bc01473e 440 free_query_allocs(qu);
441}
442
443void adns__query_done(adns_query qu) {
444 adns_answer *ans;
445 adns_query parent;
446
cb32030b 447 cancel_children(qu);
448
d05cc330 449 qu->id= -1;
bc01473e 450 ans= qu->answer;
451
b2988d3e 452 if (qu->flags & adns_qf_owner && qu->flags & adns_qf_search &&
453 ans->status != adns_s_nomemory) {
454 if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
455 adns__query_fail(qu,adns_s_nomemory);
456 return;
457 }
458 }
459
bc01473e 460 if (ans->nrrs && qu->typei->diff_needswap) {
461 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
9b86645c 462 adns__query_fail(qu,adns_s_nomemory);
bc01473e 463 return;
464 }
465 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
8c3aa944 466 qu->vb.buf,
467 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
468 qu->ads);
bc01473e 469 }
a095483e 470
2c7b101b 471 ans->expires= qu->expires;
bc01473e 472 parent= qu->parent;
473 if (parent) {
474 LIST_UNLINK_PART(parent->children,qu,siblings.);
cb32030b 475 LIST_UNLINK(qu->ads->childw,parent);
cd1bde2f 476 qu->ctx.callback(parent,qu);
bc01473e 477 free_query_allocs(qu);
478 free(qu);
bc01473e 479 } else {
480 makefinal_query(qu);
481 LIST_LINK_TAIL(qu->ads->output,qu);
0b772fb9 482 qu->state= query_done;
bc01473e 483 }
6f17710a 484}
485
11c8bf9b 486void adns__query_fail(adns_query qu, adns_status stat) {
1be24aef 487 adns__reset_preserved(qu);
965c9782 488 qu->answer->status= stat;
11c8bf9b 489 adns__query_done(qu);
6f17710a 490}
98db6da3 491
492void adns__makefinal_str(adns_query qu, char **strp) {
493 int l;
494 char *before, *after;
495
496 before= *strp;
9a2b67d4 497 if (!before) return;
98db6da3 498 l= strlen(before)+1;
499 after= adns__alloc_final(qu,l);
500 memcpy(after,before,l);
501 *strp= after;
502}
503
11c8bf9b 504void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
9a2b67d4 505 void *before, *after;
98db6da3 506
9a2b67d4 507 before= *blpp;
508 if (!before) return;
98db6da3 509 after= adns__alloc_final(qu,sz);
9a2b67d4 510 memcpy(after,before,sz);
98db6da3 511 *blpp= after;
512}