+ * In answers, quote everything except alphanums and - _ / + and document
[adns] / src / query.c
CommitLineData
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
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>
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 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. */
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
69 qu->id= 0;
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
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,
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
110adns_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
126static 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
150void 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) {
160 stat= adns_s_nxdomain; goto x_fail;
161 return;
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) ||
174 !adns__vbuf_appendstr(&qu->search_vb,nextentry)) {
175 stat= adns_s_nomemory; goto x_fail;
660d7d3b 176 }
177 }
178
179 free(qu->query_dgram);
180 qu->query_dgram= 0; qu->query_dglen= 0;
181
182 query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now);
183 return;
184
185x_fail:
186 adns__query_fail(qu,stat);
4bec51a4 187}
188
22181a31 189static int save_owner(adns_query qu, const char *owner, int ol) {
190 /* Returns 1 if OK, otherwise there was no memory. */
191 adns_answer *ans;
192
193 ans= qu->answer;
194 assert(!ans->owner);
195
8b3d55e3 196 ans->owner= adns__alloc_preserved(qu,ol+1); if (!ans->owner) return 0;
22181a31 197
198 memcpy(ans->owner,owner,ol);
199 ans->owner[ol]= 0;
200 return 1;
201}
202
e576be50 203int adns_submit(adns_state ads,
204 const char *owner,
205 adns_rrtype type,
e563872a 206 adns_queryflags flags,
e576be50 207 void *context,
208 adns_query *query_r) {
660d7d3b 209 int r, ol, ndots;
3955725c 210 adns_status stat;
211 const typeinfo *typei;
212 struct timeval now;
660d7d3b 213 adns_query qu;
214 const char *p;
e576be50 215
28de6442 216 adns__consistency(ads,0,cc_entex);
3e2e5fab 217
3955725c 218 typei= adns__findtype(type);
636b69b1 219 if (!typei) return ENOSYS;
e576be50 220
660d7d3b 221 r= gettimeofday(&now,0); if (r) goto x_errno;
222 qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno;
223
224 qu->ctx.ext= context;
225 qu->ctx.callback= 0;
226 memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
e576be50 227
e2632eb9 228 *query_r= qu;
229
e576be50 230 ol= strlen(owner);
660d7d3b 231 if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
232 if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
e576be50 233
e9e53c73 234 if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
235 flags &= ~adns_qf_search;
3d1ae6ea 236 qu->flags= flags;
e9e53c73 237 ol--;
238 }
e576be50 239
660d7d3b 240 if (flags & adns_qf_search) {
241 r= adns__vbuf_append(&qu->search_vb,owner,ol);
242 if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
243
244 for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
245 qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
660d7d3b 246 qu->search_origlen= ol;
660d7d3b 247 adns__search_next(ads,qu,now);
e2632eb9 248 } else {
22181a31 249 if (flags & adns_qf_owner) {
250 if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; }
251 }
e2632eb9 252 query_simple(ads,qu, owner,ol, typei,flags, now);
660d7d3b 253 }
70ad7a2a 254 adns__autosys(ads,now);
28de6442 255 adns__consistency(ads,qu,cc_entex);
660d7d3b 256 return 0;
257
258 x_adnsfail:
259 adns__query_fail(qu,stat);
28de6442 260 adns__consistency(ads,qu,cc_entex);
660d7d3b 261 return 0;
262
263 x_errno:
264 r= errno;
265 assert(r);
28de6442 266 adns__consistency(ads,0,cc_entex);
660d7d3b 267 return r;
ddfda861 268}
269
d7449548 270int adns_submit_reverse(adns_state ads,
271 const struct sockaddr *addr,
272 adns_rrtype type,
e563872a 273 adns_queryflags flags,
d7449548 274 void *context,
275 adns_query *query_r) {
276 const unsigned char *iaddr;
277 char buf[30];
278
279 if (type != adns_r_ptr && type != adns_r_ptr_raw) return EINVAL;
280 flags &= ~adns_qf_search;
281
282 if (addr->sa_family != AF_INET) return ENOSYS;
283 iaddr= (const unsigned char*) &(((const struct sockaddr_in*)addr) -> sin_addr);
284
285 sprintf(buf, "%d.%d.%d.%d.in-addr.arpa",
286 iaddr[3], iaddr[2], iaddr[1], iaddr[0]);
287
288 return adns_submit(ads,buf,type,flags,context,query_r);
289}
290
e576be50 291int adns_synchronous(adns_state ads,
292 const char *owner,
293 adns_rrtype type,
e563872a 294 adns_queryflags flags,
e576be50 295 adns_answer **answer_r) {
296 adns_query qu;
297 int r;
298
299 r= adns_submit(ads,owner,type,flags,0,&qu);
300 if (r) return r;
4bec51a4 301
88372443 302 r= adns_wait(ads,&qu,answer_r,0);
3955725c 303 if (r) adns_cancel(qu);
ddfda861 304
88372443 305 return r;
e576be50 306}
307
f759e52e 308static void *alloc_common(adns_query qu, size_t sz) {
e576be50 309 allocnode *an;
310
a49a6d7b 311 if (!sz) return qu; /* Any old pointer will do */
e576be50 312 assert(!qu->final_allocspace);
e576be50 313 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
e062dcae 314 if (!an) return 0;
551ff40f 315 LIST_LINK_TAIL(qu->allocations,an);
e576be50 316 return (byte*)an + MEM_ROUND(sizeof(*an));
317}
4bec51a4 318
f759e52e 319void *adns__alloc_interim(adns_query qu, size_t sz) {
8b3d55e3 320 void *rv;
321
f759e52e 322 sz= MEM_ROUND(sz);
8b3d55e3 323 rv= alloc_common(qu,sz);
324 if (!rv) return 0;
f759e52e 325 qu->interim_allocd += sz;
8b3d55e3 326 return rv;
327}
328
329void *adns__alloc_preserved(adns_query qu, size_t sz) {
330 void *rv;
331
332 sz= MEM_ROUND(sz);
333 rv= adns__alloc_interim(qu,sz);
334 if (!rv) return 0;
335 qu->preserved_allocd += sz;
336 return rv;
f759e52e 337}
338
339void *adns__alloc_mine(adns_query qu, size_t sz) {
340 return alloc_common(qu,MEM_ROUND(sz));
341}
342
551ff40f 343void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
344 allocnode *an;
345
346 if (!block) return;
347 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
348
349 assert(!to->final_allocspace);
350 assert(!from->final_allocspace);
351
352 LIST_UNLINK(from->allocations,an);
353 LIST_LINK_TAIL(to->allocations,an);
354
355 from->interim_allocd -= sz;
356 to->interim_allocd += sz;
73dba56e 357
358 if (to->expires > from->expires) to->expires= from->expires;
551ff40f 359}
360
e576be50 361void *adns__alloc_final(adns_query qu, size_t sz) {
362 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
363 * each allocation, and use final_allocspace to point to the next free
364 * bit.
365 */
366 void *rp;
367
368 sz= MEM_ROUND(sz);
369 rp= qu->final_allocspace;
370 assert(rp);
371 qu->interim_allocd -= sz;
372 assert(qu->interim_allocd>=0);
373 qu->final_allocspace= (byte*)rp + sz;
374 return rp;
375}
376
9ec44266 377static void cancel_children(adns_query qu) {
378 adns_query cqu, ncqu;
379
380 for (cqu= qu->children.head; cqu; cqu= ncqu) {
381 ncqu= cqu->siblings.next;
382 adns_cancel(cqu);
383 }
9ec44266 384}
385
8b3d55e3 386void adns__reset_preserved(adns_query qu) {
9557e604 387 assert(!qu->final_allocspace);
9ec44266 388 cancel_children(qu);
e576be50 389 qu->answer->nrrs= 0;
71a6ff46 390 qu->answer->rrs.untyped= 0;
8b3d55e3 391 qu->interim_allocd= qu->preserved_allocd;
4353a5c4 392}
393
88372443 394static void free_query_allocs(adns_query qu) {
395 allocnode *an, *ann;
88372443 396
9ec44266 397 cancel_children(qu);
551ff40f 398 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
bfc2c80e 399 LIST_INIT(qu->allocations);
88372443 400 adns__vbuf_free(&qu->vb);
914a5ff5 401 adns__vbuf_free(&qu->search_vb);
eed63b97 402 free(qu->query_dgram);
88372443 403}
404
1dfe95d8 405void adns_cancel(adns_query qu) {
3e2e5fab 406 adns_state ads;
407
408 ads= qu->ads;
28de6442 409 adns__consistency(ads,qu,cc_entex);
33ae0ddd 410 if (qu->parent) LIST_UNLINK_PART(qu->parent->children,qu,siblings.);
88372443 411 switch (qu->state) {
f7f83b4a 412 case query_tosend:
413 LIST_UNLINK(ads->udpw,qu);
88372443 414 break;
f7f83b4a 415 case query_tcpw:
416 LIST_UNLINK(ads->tcpw,qu);
417 break;
418 case query_childw:
3e2e5fab 419 LIST_UNLINK(ads->childw,qu);
88372443 420 break;
421 case query_done:
3e2e5fab 422 LIST_UNLINK(ads->output,qu);
88372443 423 break;
424 default:
425 abort();
426 }
427 free_query_allocs(qu);
428 free(qu->answer);
429 free(qu);
28de6442 430 adns__consistency(ads,0,cc_entex);
88372443 431}
551ff40f 432
73dba56e 433void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
434 time_t max;
435
436 assert(ttl <= MAXTTLBELIEVE);
437 max= now.tv_sec + ttl;
438 if (qu->expires < max) return;
439 qu->expires= max;
440}
441
551ff40f 442static void makefinal_query(adns_query qu) {
98a3f706 443 adns_answer *ans;
88372443 444 int rrn;
8e5b0abb 445
88372443 446 ans= qu->answer;
551ff40f 447
88372443 448 if (qu->interim_allocd) {
88372443 449 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
551ff40f 450 if (!ans) goto x_nomem;
e062dcae 451 qu->answer= ans;
452 }
8e5b0abb 453
88372443 454 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
8e5b0abb 455 adns__makefinal_str(qu,&ans->cname);
22181a31 456 adns__makefinal_str(qu,&ans->owner);
88372443 457
8e5b0abb 458 if (ans->nrrs) {
88372443 459 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
8e5b0abb 460
88372443 461 for (rrn=0; rrn<ans->nrrs; rrn++)
462 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
88372443 463 }
73dba56e 464
88372443 465 free_query_allocs(qu);
551ff40f 466 return;
8e5b0abb 467
551ff40f 468 x_nomem:
8b3d55e3 469 qu->preserved_allocd= 0;
551ff40f 470 qu->answer->cname= 0;
8b3d55e3 471 qu->answer->owner= 0;
472 adns__reset_preserved(qu); /* (but we just threw away the preserved stuff) */
473
474 qu->answer->status= adns_s_nomemory;
551ff40f 475 free_query_allocs(qu);
476}
477
478void adns__query_done(adns_query qu) {
479 adns_answer *ans;
480 adns_query parent;
481
4218fb9a 482 cancel_children(qu);
483
4353a5c4 484 qu->id= -1;
551ff40f 485 ans= qu->answer;
486
22181a31 487 if (qu->flags & adns_qf_owner && qu->flags & adns_qf_search &&
488 ans->status != adns_s_nomemory) {
489 if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
490 adns__query_fail(qu,adns_s_nomemory);
491 return;
492 }
493 }
494
551ff40f 495 if (ans->nrrs && qu->typei->diff_needswap) {
496 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
ea1e31e3 497 adns__query_fail(qu,adns_s_nomemory);
551ff40f 498 return;
499 }
500 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
09957b1c 501 qu->vb.buf,
502 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
503 qu->ads);
551ff40f 504 }
e553d072 505
73dba56e 506 ans->expires= qu->expires;
551ff40f 507 parent= qu->parent;
508 if (parent) {
509 LIST_UNLINK_PART(parent->children,qu,siblings.);
4218fb9a 510 LIST_UNLINK(qu->ads->childw,parent);
a6536d8b 511 qu->ctx.callback(parent,qu);
551ff40f 512 free_query_allocs(qu);
eed63b97 513 free(qu->answer);
551ff40f 514 free(qu);
551ff40f 515 } else {
516 makefinal_query(qu);
517 LIST_LINK_TAIL(qu->ads->output,qu);
33ae0ddd 518 qu->state= query_done;
551ff40f 519 }
656b2da9 520}
521
3955725c 522void adns__query_fail(adns_query qu, adns_status stat) {
8b3d55e3 523 adns__reset_preserved(qu);
8e5b0abb 524 qu->answer->status= stat;
3955725c 525 adns__query_done(qu);
656b2da9 526}
e576be50 527
528void adns__makefinal_str(adns_query qu, char **strp) {
529 int l;
530 char *before, *after;
531
532 before= *strp;
9557e604 533 if (!before) return;
e576be50 534 l= strlen(before)+1;
535 after= adns__alloc_final(qu,l);
536 memcpy(after,before,l);
537 *strp= after;
538}
539
3955725c 540void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
9557e604 541 void *before, *after;
e576be50 542
9557e604 543 before= *blpp;
544 if (!before) return;
e576be50 545 after= adns__alloc_final(qu,sz);
9557e604 546 memcpy(after,before,sz);
e576be50 547 *blpp= after;
548}