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