chiark / gitweb /
Can set query flags, and static version.
[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/*
3ff64957 8 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
98db6da3 9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
d05cc330 24
48cb0b4b 25#include "internal.h"
6f17710a 26
98db6da3 27#include <stdlib.h>
28#include <unistd.h>
29#include <errno.h>
cd1bde2f 30#include <string.h>
6f17710a 31
98db6da3 32#include <sys/time.h>
6f17710a 33
98db6da3 34#include "internal.h"
96e79df5 35
7e6a84a1 36static adns_query query_alloc(adns_state ads, const typeinfo *typei,
37 adns_queryflags flags, struct timeval now) {
38 /* Allocate a virgin query and return it. */
98db6da3 39 adns_query qu;
7e6a84a1 40
41 qu= malloc(sizeof(*qu)); if (!qu) return 0;
42 qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) { free(qu); return 0; }
43
11c8bf9b 44 qu->ads= ads;
98db6da3 45 qu->state= query_udp;
46 qu->back= qu->next= qu->parent= 0;
47 LIST_INIT(qu->children);
7e6a84a1 48 LINK_INIT(qu->siblings);
bc01473e 49 LIST_INIT(qu->allocations);
98db6da3 50 qu->interim_allocd= 0;
11c8bf9b 51 qu->final_allocspace= 0;
98db6da3 52
11c8bf9b 53 qu->typei= typei;
7e6a84a1 54 qu->query_dgram= 0;
55 qu->query_dglen= 0;
98db6da3 56 adns__vbuf_init(&qu->vb);
57
58 qu->cname_dgram= 0;
59 qu->cname_dglen= qu->cname_begin= 0;
7e6a84a1 60
61 adns__vbuf_init(&qu->search_vb);
62 qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
63
64 qu->id= 0;
98db6da3 65 qu->flags= flags;
66 qu->udpretries= 0;
67 qu->udpnextserver= 0;
68 qu->udpsent= qu->tcpfailed= 0;
69 timerclear(&qu->timeout);
2c7b101b 70 qu->expires= now.tv_sec + MAXTTLBELIEVE;
98db6da3 71
7e6a84a1 72 memset(&qu->ctx,0,sizeof(qu->ctx));
73
98db6da3 74 qu->answer->status= adns_s_ok;
75 qu->answer->cname= 0;
11c8bf9b 76 qu->answer->type= typei->type;
7e6a84a1 77 qu->answer->expires= -1;
98db6da3 78 qu->answer->nrrs= 0;
79 qu->answer->rrs= 0;
11c8bf9b 80 qu->answer->rrsz= typei->rrsz;
96e79df5 81
7e6a84a1 82 return qu;
83}
84
85static void query_submit(adns_state ads, adns_query qu,
86 const typeinfo *typei, vbuf *qumsg_vb, int id,
87 adns_queryflags flags, struct timeval now) {
88 /* Fills in the query message in for a previously-allocated query,
89 * and submits it. Cannot fail.
90 */
91
98db6da3 92 qu->vb= *qumsg_vb;
93 adns__vbuf_init(qumsg_vb);
7e6a84a1 94
95 qu->query_dgram= malloc(qu->vb.used);
96 if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; }
97
98 qu->id= id;
99 qu->query_dglen= qu->vb.used;
100 memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
98db6da3 101
11c8bf9b 102 adns__query_udp(qu,now);
98db6da3 103 adns__autosys(ads,now);
7e6a84a1 104}
105
106adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
107 const typeinfo *typei, vbuf *qumsg_vb, int id,
108 adns_queryflags flags, struct timeval now,
109 const qcontext *ctx) {
110 adns_query qu;
111
112 qu= query_alloc(ads,typei,flags,now);
113 if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; }
114 *query_r= qu;
98db6da3 115
7e6a84a1 116 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
117 query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
118
11c8bf9b 119 return adns_s_ok;
7e6a84a1 120}
121
122static void query_simple(adns_state ads, adns_query qu,
123 const char *owner, int ol,
124 const typeinfo *typei, adns_queryflags flags,
125 struct timeval now) {
126 vbuf vb;
127 int id;
128 adns_status stat;
98db6da3 129
7e6a84a1 130 adns__vbuf_init(&vb);
131
132 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
133 if (stat) { adns__query_fail(qu,stat); return; }
134
135 query_submit(ads,qu, typei,&vb,id, flags,now);
136}
137
138void adns__search_next(adns_state ads, adns_query qu, struct timeval now) {
139 const char *nextentry;
140 adns_status stat;
141
142 if (qu->search_doneabs<0) {
143 nextentry= 0;
144 qu->search_doneabs= 1;
145 } else {
146 if (qu->search_pos >= ads->nsearchlist) {
147 if (qu->search_doneabs) {
148 stat= adns_s_nxdomain; goto x_fail;
149 return;
150 } else {
151 nextentry= 0;
152 qu->search_doneabs= 1;
153 }
154 } else {
155 nextentry= ads->searchlist[qu->search_pos++];
156 }
157 }
158
159 if (nextentry) {
160 if (!adns__vbuf_append(&qu->search_vb,".",1) ||
161 !adns__vbuf_appendstr(&qu->search_vb,nextentry)) {
162 stat= adns_s_nomemory; goto x_fail;
163 } else {
164 qu->search_vb.used= qu->search_origlen;
165 }
166 }
167
168 free(qu->query_dgram);
169 qu->query_dgram= 0; qu->query_dglen= 0;
170
171 query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now);
172 return;
173
174x_fail:
175 adns__query_fail(qu,stat);
48cb0b4b 176}
177
98db6da3 178int adns_submit(adns_state ads,
179 const char *owner,
180 adns_rrtype type,
181 adns_queryflags flags,
182 void *context,
183 adns_query *query_r) {
7e6a84a1 184 int r, ol, ndots;
11c8bf9b 185 adns_status stat;
186 const typeinfo *typei;
187 struct timeval now;
7e6a84a1 188 adns_query qu;
189 const char *p;
98db6da3 190
11c8bf9b 191 typei= adns__findtype(type);
9b86645c 192 if (!typei) return adns_s_unknownrrtype;
98db6da3 193
7e6a84a1 194 r= gettimeofday(&now,0); if (r) goto x_errno;
195 qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno;
196
197 qu->ctx.ext= context;
198 qu->ctx.callback= 0;
199 memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
98db6da3 200
201 ol= strlen(owner);
7e6a84a1 202 if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
203 if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
98db6da3 204
11f553d9 205 if (ol>=2 && owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
98db6da3 206
7e6a84a1 207 if (flags & adns_qf_search) {
208 r= adns__vbuf_append(&qu->search_vb,owner,ol);
209 if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
210
211 for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
212 qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
213
214 qu->search_origlen= ol;
215
216 adns__search_next(ads,qu,now);
217 return 0;
218 }
219
220 query_simple(ads,qu, owner,ol, typei,flags, now);
221 return 0;
222
223 x_adnsfail:
224 adns__query_fail(qu,stat);
225 return 0;
226
227 x_errno:
228 r= errno;
229 assert(r);
230 return r;
96e79df5 231}
232
98db6da3 233int adns_synchronous(adns_state ads,
234 const char *owner,
235 adns_rrtype type,
236 adns_queryflags flags,
237 adns_answer **answer_r) {
238 adns_query qu;
239 int r;
240
241 r= adns_submit(ads,owner,type,flags,0,&qu);
242 if (r) return r;
48cb0b4b 243
f318f883 244 r= adns_wait(ads,&qu,answer_r,0);
11c8bf9b 245 if (r) adns_cancel(qu);
96e79df5 246
f318f883 247 return r;
98db6da3 248}
249
c9afe7bb 250static void *alloc_common(adns_query qu, size_t sz) {
98db6da3 251 allocnode *an;
252
f47cdeec 253 if (!sz) return qu; /* Any old pointer will do */
98db6da3 254 assert(!qu->final_allocspace);
98db6da3 255 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
f2ad23ee 256 if (!an) return 0;
bc01473e 257 LIST_LINK_TAIL(qu->allocations,an);
98db6da3 258 return (byte*)an + MEM_ROUND(sizeof(*an));
259}
48cb0b4b 260
c9afe7bb 261void *adns__alloc_interim(adns_query qu, size_t sz) {
262 sz= MEM_ROUND(sz);
263 qu->interim_allocd += sz;
264 return alloc_common(qu,sz);
265}
266
267void *adns__alloc_mine(adns_query qu, size_t sz) {
268 return alloc_common(qu,MEM_ROUND(sz));
269}
270
bc01473e 271void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
272 allocnode *an;
273
274 if (!block) return;
275 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
276
277 assert(!to->final_allocspace);
278 assert(!from->final_allocspace);
279
280 LIST_UNLINK(from->allocations,an);
281 LIST_LINK_TAIL(to->allocations,an);
282
283 from->interim_allocd -= sz;
284 to->interim_allocd += sz;
2c7b101b 285
286 if (to->expires > from->expires) to->expires= from->expires;
bc01473e 287}
288
98db6da3 289void *adns__alloc_final(adns_query qu, size_t sz) {
290 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
291 * each allocation, and use final_allocspace to point to the next free
292 * bit.
293 */
294 void *rp;
295
296 sz= MEM_ROUND(sz);
297 rp= qu->final_allocspace;
298 assert(rp);
299 qu->interim_allocd -= sz;
300 assert(qu->interim_allocd>=0);
301 qu->final_allocspace= (byte*)rp + sz;
302 return rp;
303}
304
61093792 305static void cancel_children(adns_query qu) {
306 adns_query cqu, ncqu;
307
308 for (cqu= qu->children.head; cqu; cqu= ncqu) {
309 ncqu= cqu->siblings.next;
310 adns_cancel(cqu);
311 }
312 LIST_INIT(qu->children);
313}
314
11c8bf9b 315void adns__reset_cnameonly(adns_query qu) {
9a2b67d4 316 assert(!qu->final_allocspace);
61093792 317 cancel_children(qu);
98db6da3 318 qu->answer->nrrs= 0;
319 qu->answer->rrs= 0;
320 qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
d05cc330 321}
322
f318f883 323static void free_query_allocs(adns_query qu) {
324 allocnode *an, *ann;
f318f883 325
61093792 326 cancel_children(qu);
bc01473e 327 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
f318f883 328 adns__vbuf_free(&qu->vb);
329}
330
26eb6bdc 331void adns_cancel(adns_query qu) {
f318f883 332 switch (qu->state) {
333 case query_udp: case query_tcpwait: case query_tcpsent:
26eb6bdc 334 LIST_UNLINK(qu->ads->timew,qu);
f318f883 335 break;
336 case query_child:
26eb6bdc 337 LIST_UNLINK(qu->ads->childw,qu);
f318f883 338 break;
339 case query_done:
26eb6bdc 340 LIST_UNLINK(qu->ads->output,qu);
f318f883 341 break;
342 default:
343 abort();
344 }
345 free_query_allocs(qu);
346 free(qu->answer);
347 free(qu);
348}
bc01473e 349
2c7b101b 350void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
351 time_t max;
352
353 assert(ttl <= MAXTTLBELIEVE);
354 max= now.tv_sec + ttl;
355 if (qu->expires < max) return;
356 qu->expires= max;
357}
358
bc01473e 359static void makefinal_query(adns_query qu) {
5c596e4d 360 adns_answer *ans;
f318f883 361 int rrn;
965c9782 362
f318f883 363 ans= qu->answer;
bc01473e 364
f318f883 365 if (qu->interim_allocd) {
f318f883 366 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
bc01473e 367 if (!ans) goto x_nomem;
f2ad23ee 368 qu->answer= ans;
369 }
965c9782 370
f318f883 371 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
965c9782 372 adns__makefinal_str(qu,&ans->cname);
f318f883 373
965c9782 374 if (ans->nrrs) {
f318f883 375 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
965c9782 376
f318f883 377 for (rrn=0; rrn<ans->nrrs; rrn++)
378 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
f318f883 379 }
2c7b101b 380
f318f883 381 free_query_allocs(qu);
bc01473e 382 return;
965c9782 383
bc01473e 384 x_nomem:
9b86645c 385 qu->answer->status= adns_s_nomemory;
bc01473e 386 qu->answer->cname= 0;
387 adns__reset_cnameonly(qu);
388 free_query_allocs(qu);
389}
390
391void adns__query_done(adns_query qu) {
392 adns_answer *ans;
393 adns_query parent;
394
d05cc330 395 qu->id= -1;
bc01473e 396 ans= qu->answer;
397
398 if (ans->nrrs && qu->typei->diff_needswap) {
399 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
9b86645c 400 adns__query_fail(qu,adns_s_nomemory);
bc01473e 401 return;
402 }
403 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
8c3aa944 404 qu->vb.buf,
405 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
406 qu->ads);
bc01473e 407 }
a095483e 408
2c7b101b 409 ans->expires= qu->expires;
bc01473e 410 parent= qu->parent;
411 if (parent) {
412 LIST_UNLINK_PART(parent->children,qu,siblings.);
cd1bde2f 413 qu->ctx.callback(parent,qu);
bc01473e 414 free_query_allocs(qu);
415 free(qu);
bc01473e 416 } else {
417 makefinal_query(qu);
418 LIST_LINK_TAIL(qu->ads->output,qu);
419 }
6f17710a 420}
421
11c8bf9b 422void adns__query_fail(adns_query qu, adns_status stat) {
423 adns__reset_cnameonly(qu);
965c9782 424 qu->answer->status= stat;
11c8bf9b 425 adns__query_done(qu);
6f17710a 426}
98db6da3 427
428void adns__makefinal_str(adns_query qu, char **strp) {
429 int l;
430 char *before, *after;
431
432 before= *strp;
9a2b67d4 433 if (!before) return;
98db6da3 434 l= strlen(before)+1;
435 after= adns__alloc_final(qu,l);
436 memcpy(after,before,l);
437 *strp= after;
438}
439
11c8bf9b 440void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
9a2b67d4 441 void *before, *after;
98db6da3 442
9a2b67d4 443 before= *blpp;
444 if (!before) return;
98db6da3 445 after= adns__alloc_final(qu,sz);
9a2b67d4 446 memcpy(after,before,sz);
98db6da3 447 *blpp= after;
448}