Portability fixes (missing struct in_addr, INADDR_LOOPBACK defs, rrs=0
[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/*
a719a4be 8 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
e576be50 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 */
4353a5c4 24
4bec51a4 25#include "internal.h"
656b2da9 26
e576be50 27#include <stdlib.h>
28#include <unistd.h>
29#include <errno.h>
a6536d8b 30#include <string.h>
656b2da9 31
e576be50 32#include <sys/time.h>
656b2da9 33
e576be50 34#include "internal.h"
ddfda861 35
660d7d3b 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. */
e576be50 39 adns_query qu;
660d7d3b 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
3955725c 44 qu->ads= ads;
e576be50 45 qu->state= query_udp;
46 qu->back= qu->next= qu->parent= 0;
47 LIST_INIT(qu->children);
660d7d3b 48 LINK_INIT(qu->siblings);
551ff40f 49 LIST_INIT(qu->allocations);
e576be50 50 qu->interim_allocd= 0;
3955725c 51 qu->final_allocspace= 0;
e576be50 52
3955725c 53 qu->typei= typei;
660d7d3b 54 qu->query_dgram= 0;
55 qu->query_dglen= 0;
e576be50 56 adns__vbuf_init(&qu->vb);
57
58 qu->cname_dgram= 0;
59 qu->cname_dglen= qu->cname_begin= 0;
660d7d3b 60
61 adns__vbuf_init(&qu->search_vb);
62 qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
63
64 qu->id= 0;
e576be50 65 qu->flags= flags;
66 qu->udpretries= 0;
67 qu->udpnextserver= 0;
68 qu->udpsent= qu->tcpfailed= 0;
69 timerclear(&qu->timeout);
73dba56e 70 qu->expires= now.tv_sec + MAXTTLBELIEVE;
e576be50 71
660d7d3b 72 memset(&qu->ctx,0,sizeof(qu->ctx));
73
e576be50 74 qu->answer->status= adns_s_ok;
22181a31 75 qu->answer->cname= qu->answer->owner= 0;
3955725c 76 qu->answer->type= typei->type;
660d7d3b 77 qu->answer->expires= -1;
e576be50 78 qu->answer->nrrs= 0;
79 qu->answer->rrs= 0;
3955725c 80 qu->answer->rrsz= typei->rrsz;
ddfda861 81
660d7d3b 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
e576be50 92 qu->vb= *qumsg_vb;
93 adns__vbuf_init(qumsg_vb);
660d7d3b 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);
e576be50 101
3955725c 102 adns__query_udp(qu,now);
e576be50 103 adns__autosys(ads,now);
660d7d3b 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;
e576be50 115
660d7d3b 116 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
117 query_submit(ads,qu, typei,qumsg_vb,id,flags,now);
118
3955725c 119 return adns_s_ok;
660d7d3b 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;
e576be50 129
660d7d3b 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
e2632eb9 159 qu->search_vb.used= qu->search_origlen;
660d7d3b 160 if (nextentry) {
161 if (!adns__vbuf_append(&qu->search_vb,".",1) ||
162 !adns__vbuf_appendstr(&qu->search_vb,nextentry)) {
163 stat= adns_s_nomemory; goto x_fail;
660d7d3b 164 }
165 }
166
167 free(qu->query_dgram);
168 qu->query_dgram= 0; qu->query_dglen= 0;
169
170 query_simple(ads,qu, qu->search_vb.buf, qu->search_vb.used, qu->typei, qu->flags, now);
171 return;
172
173x_fail:
174 adns__query_fail(qu,stat);
4bec51a4 175}
176
22181a31 177static int save_owner(adns_query qu, const char *owner, int ol) {
178 /* Returns 1 if OK, otherwise there was no memory. */
179 adns_answer *ans;
180
181 ans= qu->answer;
182 assert(!ans->owner);
183
184 ans->owner= adns__alloc_interim(qu,ol+1); if (!ans->owner) return 0;
185
186 memcpy(ans->owner,owner,ol);
187 ans->owner[ol]= 0;
188 return 1;
189}
190
e576be50 191int adns_submit(adns_state ads,
192 const char *owner,
193 adns_rrtype type,
194 adns_queryflags flags,
195 void *context,
196 adns_query *query_r) {
660d7d3b 197 int r, ol, ndots;
3955725c 198 adns_status stat;
199 const typeinfo *typei;
200 struct timeval now;
660d7d3b 201 adns_query qu;
202 const char *p;
e576be50 203
3955725c 204 typei= adns__findtype(type);
ea1e31e3 205 if (!typei) return adns_s_unknownrrtype;
e576be50 206
660d7d3b 207 r= gettimeofday(&now,0); if (r) goto x_errno;
208 qu= query_alloc(ads,typei,flags,now); if (!qu) goto x_errno;
209
210 qu->ctx.ext= context;
211 qu->ctx.callback= 0;
212 memset(&qu->ctx.info,0,sizeof(qu->ctx.info));
e576be50 213
e2632eb9 214 *query_r= qu;
215
e576be50 216 ol= strlen(owner);
660d7d3b 217 if (!ol) { stat= adns_s_querydomaininvalid; goto x_adnsfail; }
218 if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto x_adnsfail; }
e576be50 219
e9e53c73 220 if (ol>=1 && owner[ol-1]=='.' && (ol<2 || owner[ol-2]!='\\')) {
221 flags &= ~adns_qf_search;
222 ol--;
223 }
e576be50 224
660d7d3b 225 if (flags & adns_qf_search) {
226 r= adns__vbuf_append(&qu->search_vb,owner,ol);
227 if (!r) { stat= adns_s_nomemory; goto x_adnsfail; }
228
229 for (ndots=0, p=owner; (p= strchr(p,'.')); p++, ndots++);
230 qu->search_doneabs= (ndots >= ads->searchndots) ? -1 : 0;
660d7d3b 231 qu->search_origlen= ol;
660d7d3b 232 adns__search_next(ads,qu,now);
e2632eb9 233 } else {
22181a31 234 if (flags & adns_qf_owner) {
235 if (!save_owner(qu,owner,ol)) { stat= adns_s_nomemory; goto x_adnsfail; }
236 }
e2632eb9 237 query_simple(ads,qu, owner,ol, typei,flags, now);
660d7d3b 238 }
660d7d3b 239 return 0;
240
241 x_adnsfail:
242 adns__query_fail(qu,stat);
243 return 0;
244
245 x_errno:
246 r= errno;
247 assert(r);
248 return r;
ddfda861 249}
250
e576be50 251int adns_synchronous(adns_state ads,
252 const char *owner,
253 adns_rrtype type,
254 adns_queryflags flags,
255 adns_answer **answer_r) {
256 adns_query qu;
257 int r;
258
259 r= adns_submit(ads,owner,type,flags,0,&qu);
260 if (r) return r;
4bec51a4 261
88372443 262 r= adns_wait(ads,&qu,answer_r,0);
3955725c 263 if (r) adns_cancel(qu);
ddfda861 264
88372443 265 return r;
e576be50 266}
267
f759e52e 268static void *alloc_common(adns_query qu, size_t sz) {
e576be50 269 allocnode *an;
270
a49a6d7b 271 if (!sz) return qu; /* Any old pointer will do */
e576be50 272 assert(!qu->final_allocspace);
e576be50 273 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
e062dcae 274 if (!an) return 0;
551ff40f 275 LIST_LINK_TAIL(qu->allocations,an);
e576be50 276 return (byte*)an + MEM_ROUND(sizeof(*an));
277}
4bec51a4 278
f759e52e 279void *adns__alloc_interim(adns_query qu, size_t sz) {
280 sz= MEM_ROUND(sz);
281 qu->interim_allocd += sz;
282 return alloc_common(qu,sz);
283}
284
285void *adns__alloc_mine(adns_query qu, size_t sz) {
286 return alloc_common(qu,MEM_ROUND(sz));
287}
288
551ff40f 289void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
290 allocnode *an;
291
292 if (!block) return;
293 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
294
295 assert(!to->final_allocspace);
296 assert(!from->final_allocspace);
297
298 LIST_UNLINK(from->allocations,an);
299 LIST_LINK_TAIL(to->allocations,an);
300
301 from->interim_allocd -= sz;
302 to->interim_allocd += sz;
73dba56e 303
304 if (to->expires > from->expires) to->expires= from->expires;
551ff40f 305}
306
e576be50 307void *adns__alloc_final(adns_query qu, size_t sz) {
308 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
309 * each allocation, and use final_allocspace to point to the next free
310 * bit.
311 */
312 void *rp;
313
314 sz= MEM_ROUND(sz);
315 rp= qu->final_allocspace;
316 assert(rp);
317 qu->interim_allocd -= sz;
318 assert(qu->interim_allocd>=0);
319 qu->final_allocspace= (byte*)rp + sz;
320 return rp;
321}
322
9ec44266 323static void cancel_children(adns_query qu) {
324 adns_query cqu, ncqu;
325
326 for (cqu= qu->children.head; cqu; cqu= ncqu) {
327 ncqu= cqu->siblings.next;
328 adns_cancel(cqu);
329 }
330 LIST_INIT(qu->children);
331}
332
3955725c 333void adns__reset_cnameonly(adns_query qu) {
9557e604 334 assert(!qu->final_allocspace);
9ec44266 335 cancel_children(qu);
e576be50 336 qu->answer->nrrs= 0;
337 qu->answer->rrs= 0;
338 qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
4353a5c4 339}
340
88372443 341static void free_query_allocs(adns_query qu) {
342 allocnode *an, *ann;
88372443 343
9ec44266 344 cancel_children(qu);
551ff40f 345 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
88372443 346 adns__vbuf_free(&qu->vb);
347}
348
1dfe95d8 349void adns_cancel(adns_query qu) {
88372443 350 switch (qu->state) {
351 case query_udp: case query_tcpwait: case query_tcpsent:
1dfe95d8 352 LIST_UNLINK(qu->ads->timew,qu);
88372443 353 break;
354 case query_child:
1dfe95d8 355 LIST_UNLINK(qu->ads->childw,qu);
88372443 356 break;
357 case query_done:
1dfe95d8 358 LIST_UNLINK(qu->ads->output,qu);
88372443 359 break;
360 default:
361 abort();
362 }
363 free_query_allocs(qu);
364 free(qu->answer);
365 free(qu);
366}
551ff40f 367
73dba56e 368void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
369 time_t max;
370
371 assert(ttl <= MAXTTLBELIEVE);
372 max= now.tv_sec + ttl;
373 if (qu->expires < max) return;
374 qu->expires= max;
375}
376
551ff40f 377static void makefinal_query(adns_query qu) {
98a3f706 378 adns_answer *ans;
88372443 379 int rrn;
8e5b0abb 380
88372443 381 ans= qu->answer;
551ff40f 382
88372443 383 if (qu->interim_allocd) {
88372443 384 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
551ff40f 385 if (!ans) goto x_nomem;
e062dcae 386 qu->answer= ans;
387 }
8e5b0abb 388
88372443 389 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
8e5b0abb 390 adns__makefinal_str(qu,&ans->cname);
22181a31 391 adns__makefinal_str(qu,&ans->owner);
88372443 392
8e5b0abb 393 if (ans->nrrs) {
88372443 394 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
8e5b0abb 395
88372443 396 for (rrn=0; rrn<ans->nrrs; rrn++)
397 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
88372443 398 }
73dba56e 399
88372443 400 free_query_allocs(qu);
551ff40f 401 return;
8e5b0abb 402
551ff40f 403 x_nomem:
ea1e31e3 404 qu->answer->status= adns_s_nomemory;
551ff40f 405 qu->answer->cname= 0;
406 adns__reset_cnameonly(qu);
407 free_query_allocs(qu);
408}
409
410void adns__query_done(adns_query qu) {
411 adns_answer *ans;
412 adns_query parent;
413
4353a5c4 414 qu->id= -1;
551ff40f 415 ans= qu->answer;
416
22181a31 417 if (qu->flags & adns_qf_owner && qu->flags & adns_qf_search &&
418 ans->status != adns_s_nomemory) {
419 if (!save_owner(qu, qu->search_vb.buf, qu->search_vb.used)) {
420 adns__query_fail(qu,adns_s_nomemory);
421 return;
422 }
423 }
424
551ff40f 425 if (ans->nrrs && qu->typei->diff_needswap) {
426 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
ea1e31e3 427 adns__query_fail(qu,adns_s_nomemory);
551ff40f 428 return;
429 }
430 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
09957b1c 431 qu->vb.buf,
432 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
433 qu->ads);
551ff40f 434 }
e553d072 435
73dba56e 436 ans->expires= qu->expires;
551ff40f 437 parent= qu->parent;
438 if (parent) {
439 LIST_UNLINK_PART(parent->children,qu,siblings.);
a6536d8b 440 qu->ctx.callback(parent,qu);
551ff40f 441 free_query_allocs(qu);
442 free(qu);
551ff40f 443 } else {
444 makefinal_query(qu);
445 LIST_LINK_TAIL(qu->ads->output,qu);
446 }
656b2da9 447}
448
3955725c 449void adns__query_fail(adns_query qu, adns_status stat) {
450 adns__reset_cnameonly(qu);
8e5b0abb 451 qu->answer->status= stat;
3955725c 452 adns__query_done(qu);
656b2da9 453}
e576be50 454
455void adns__makefinal_str(adns_query qu, char **strp) {
456 int l;
457 char *before, *after;
458
459 before= *strp;
9557e604 460 if (!before) return;
e576be50 461 l= strlen(before)+1;
462 after= adns__alloc_final(qu,l);
463 memcpy(after,before,l);
464 *strp= after;
465}
466
3955725c 467void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
9557e604 468 void *before, *after;
e576be50 469
9557e604 470 before= *blpp;
471 if (!before) return;
e576be50 472 after= adns__alloc_final(qu,sz);
9557e604 473 memcpy(after,before,sz);
e576be50 474 *blpp= after;
475}