Parse searchlist; beginnings of paying attention.
[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
98db6da3 36int adns__internal_submit(adns_state ads, adns_query *query_r,
11c8bf9b 37 const typeinfo *typei, vbuf *qumsg_vb, int id,
98db6da3 38 adns_queryflags flags, struct timeval now,
39 adns_status failstat, const qcontext *ctx) {
40 adns_query qu;
98db6da3 41
42 qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory;
43 qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory;
44
11c8bf9b 45 qu->ads= ads;
98db6da3 46 qu->state= query_udp;
47 qu->back= qu->next= qu->parent= 0;
48 LIST_INIT(qu->children);
49 qu->siblings.next= qu->siblings.back= 0;
bc01473e 50 LIST_INIT(qu->allocations);
98db6da3 51 qu->interim_allocd= 0;
11c8bf9b 52 qu->final_allocspace= 0;
98db6da3 53
11c8bf9b 54 qu->typei= typei;
98db6da3 55 adns__vbuf_init(&qu->vb);
56
57 qu->cname_dgram= 0;
58 qu->cname_dglen= qu->cname_begin= 0;
48cb0b4b 59
98db6da3 60 qu->id= id;
61 qu->flags= flags;
62 qu->udpretries= 0;
63 qu->udpnextserver= 0;
64 qu->udpsent= qu->tcpfailed= 0;
65 timerclear(&qu->timeout);
cd1bde2f 66 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
2c7b101b 67 qu->expires= now.tv_sec + MAXTTLBELIEVE;
98db6da3 68
69 qu->answer->status= adns_s_ok;
70 qu->answer->cname= 0;
11c8bf9b 71 qu->answer->type= typei->type;
98db6da3 72 qu->answer->nrrs= 0;
73 qu->answer->rrs= 0;
11c8bf9b 74 qu->answer->rrsz= typei->rrsz;
98db6da3 75
76 *query_r= qu;
96e79df5 77
f47cdeec 78 qu->query_dglen= qumsg_vb->used;
79 if (qumsg_vb->used) {
80 qu->query_dgram= malloc(qumsg_vb->used);
81 if (!qu->query_dgram) {
9b86645c 82 adns__query_fail(qu,adns_s_nomemory);
f47cdeec 83 return adns_s_ok;
84 }
85 memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
86 } else {
87 qu->query_dgram= 0;
96e79df5 88 }
98db6da3 89 qu->vb= *qumsg_vb;
90 adns__vbuf_init(qumsg_vb);
91
92 if (failstat) {
11c8bf9b 93 adns__query_fail(qu,failstat);
94 return adns_s_ok;
6f17710a 95 }
11c8bf9b 96 adns__query_udp(qu,now);
98db6da3 97 adns__autosys(ads,now);
98
11c8bf9b 99 return adns_s_ok;
98db6da3 100
101 x_freequ_nomemory:
102 free(qu);
103 x_nomemory:
11c8bf9b 104 adns__vbuf_free(qumsg_vb);
9b86645c 105 return adns_s_nomemory;
48cb0b4b 106}
107
98db6da3 108int adns_submit(adns_state ads,
109 const char *owner,
110 adns_rrtype type,
111 adns_queryflags flags,
112 void *context,
113 adns_query *query_r) {
114 qcontext ctx;
11c8bf9b 115 int id, r, ol;
11f553d9 116 vbuf vb, search_vb;
11c8bf9b 117 adns_status stat;
118 const typeinfo *typei;
119 struct timeval now;
98db6da3 120
11c8bf9b 121 typei= adns__findtype(type);
9b86645c 122 if (!typei) return adns_s_unknownrrtype;
11c8bf9b 123
98db6da3 124 ctx.ext= context;
cd1bde2f 125 ctx.callback= 0;
126 memset(&ctx.info,0,sizeof(ctx.info));
127
98db6da3 128 r= gettimeofday(&now,0); if (r) return errno;
129 id= 0;
130
131 adns__vbuf_init(&vb);
132
133 ol= strlen(owner);
11f553d9 134 if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto xit; }
98db6da3 135
11f553d9 136 if (ol>=2 && owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
98db6da3 137
11c8bf9b 138 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
98db6da3 139
140 xit:
11c8bf9b 141 return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx);
96e79df5 142}
143
98db6da3 144int adns_synchronous(adns_state ads,
145 const char *owner,
146 adns_rrtype type,
147 adns_queryflags flags,
148 adns_answer **answer_r) {
149 adns_query qu;
150 int r;
151
152 r= adns_submit(ads,owner,type,flags,0,&qu);
153 if (r) return r;
48cb0b4b 154
f318f883 155 r= adns_wait(ads,&qu,answer_r,0);
11c8bf9b 156 if (r) adns_cancel(qu);
96e79df5 157
f318f883 158 return r;
98db6da3 159}
160
c9afe7bb 161static void *alloc_common(adns_query qu, size_t sz) {
98db6da3 162 allocnode *an;
163
f47cdeec 164 if (!sz) return qu; /* Any old pointer will do */
98db6da3 165 assert(!qu->final_allocspace);
98db6da3 166 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
f2ad23ee 167 if (!an) return 0;
bc01473e 168 LIST_LINK_TAIL(qu->allocations,an);
98db6da3 169 return (byte*)an + MEM_ROUND(sizeof(*an));
170}
48cb0b4b 171
c9afe7bb 172void *adns__alloc_interim(adns_query qu, size_t sz) {
173 sz= MEM_ROUND(sz);
174 qu->interim_allocd += sz;
175 return alloc_common(qu,sz);
176}
177
178void *adns__alloc_mine(adns_query qu, size_t sz) {
179 return alloc_common(qu,MEM_ROUND(sz));
180}
181
bc01473e 182void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
183 allocnode *an;
184
185 if (!block) return;
186 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
187
188 assert(!to->final_allocspace);
189 assert(!from->final_allocspace);
190
191 LIST_UNLINK(from->allocations,an);
192 LIST_LINK_TAIL(to->allocations,an);
193
194 from->interim_allocd -= sz;
195 to->interim_allocd += sz;
2c7b101b 196
197 if (to->expires > from->expires) to->expires= from->expires;
bc01473e 198}
199
98db6da3 200void *adns__alloc_final(adns_query qu, size_t sz) {
201 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
202 * each allocation, and use final_allocspace to point to the next free
203 * bit.
204 */
205 void *rp;
206
207 sz= MEM_ROUND(sz);
208 rp= qu->final_allocspace;
209 assert(rp);
210 qu->interim_allocd -= sz;
211 assert(qu->interim_allocd>=0);
212 qu->final_allocspace= (byte*)rp + sz;
213 return rp;
214}
215
61093792 216static void cancel_children(adns_query qu) {
217 adns_query cqu, ncqu;
218
219 for (cqu= qu->children.head; cqu; cqu= ncqu) {
220 ncqu= cqu->siblings.next;
221 adns_cancel(cqu);
222 }
223 LIST_INIT(qu->children);
224}
225
11c8bf9b 226void adns__reset_cnameonly(adns_query qu) {
9a2b67d4 227 assert(!qu->final_allocspace);
61093792 228 cancel_children(qu);
98db6da3 229 qu->answer->nrrs= 0;
230 qu->answer->rrs= 0;
231 qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
d05cc330 232}
233
f318f883 234static void free_query_allocs(adns_query qu) {
235 allocnode *an, *ann;
f318f883 236
61093792 237 cancel_children(qu);
bc01473e 238 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
f318f883 239 adns__vbuf_free(&qu->vb);
240}
241
26eb6bdc 242void adns_cancel(adns_query qu) {
f318f883 243 switch (qu->state) {
244 case query_udp: case query_tcpwait: case query_tcpsent:
26eb6bdc 245 LIST_UNLINK(qu->ads->timew,qu);
f318f883 246 break;
247 case query_child:
26eb6bdc 248 LIST_UNLINK(qu->ads->childw,qu);
f318f883 249 break;
250 case query_done:
26eb6bdc 251 LIST_UNLINK(qu->ads->output,qu);
f318f883 252 break;
253 default:
254 abort();
255 }
256 free_query_allocs(qu);
257 free(qu->answer);
258 free(qu);
259}
bc01473e 260
2c7b101b 261void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
262 time_t max;
263
264 assert(ttl <= MAXTTLBELIEVE);
265 max= now.tv_sec + ttl;
266 if (qu->expires < max) return;
267 qu->expires= max;
268}
269
bc01473e 270static void makefinal_query(adns_query qu) {
5c596e4d 271 adns_answer *ans;
f318f883 272 int rrn;
965c9782 273
f318f883 274 ans= qu->answer;
bc01473e 275
f318f883 276 if (qu->interim_allocd) {
f318f883 277 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
bc01473e 278 if (!ans) goto x_nomem;
f2ad23ee 279 qu->answer= ans;
280 }
965c9782 281
f318f883 282 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
965c9782 283 adns__makefinal_str(qu,&ans->cname);
f318f883 284
965c9782 285 if (ans->nrrs) {
f318f883 286 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
965c9782 287
f318f883 288 for (rrn=0; rrn<ans->nrrs; rrn++)
289 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
f318f883 290 }
2c7b101b 291
f318f883 292 free_query_allocs(qu);
bc01473e 293 return;
965c9782 294
bc01473e 295 x_nomem:
9b86645c 296 qu->answer->status= adns_s_nomemory;
bc01473e 297 qu->answer->cname= 0;
298 adns__reset_cnameonly(qu);
299 free_query_allocs(qu);
300}
301
302void adns__query_done(adns_query qu) {
303 adns_answer *ans;
304 adns_query parent;
305
d05cc330 306 qu->id= -1;
bc01473e 307 ans= qu->answer;
308
309 if (ans->nrrs && qu->typei->diff_needswap) {
310 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
9b86645c 311 adns__query_fail(qu,adns_s_nomemory);
bc01473e 312 return;
313 }
314 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
8c3aa944 315 qu->vb.buf,
316 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
317 qu->ads);
bc01473e 318 }
a095483e 319
2c7b101b 320 ans->expires= qu->expires;
bc01473e 321 parent= qu->parent;
322 if (parent) {
323 LIST_UNLINK_PART(parent->children,qu,siblings.);
cd1bde2f 324 qu->ctx.callback(parent,qu);
bc01473e 325 free_query_allocs(qu);
326 free(qu);
bc01473e 327 } else {
328 makefinal_query(qu);
329 LIST_LINK_TAIL(qu->ads->output,qu);
330 }
6f17710a 331}
332
11c8bf9b 333void adns__query_fail(adns_query qu, adns_status stat) {
334 adns__reset_cnameonly(qu);
965c9782 335 qu->answer->status= stat;
11c8bf9b 336 adns__query_done(qu);
6f17710a 337}
98db6da3 338
339void adns__makefinal_str(adns_query qu, char **strp) {
340 int l;
341 char *before, *after;
342
343 before= *strp;
9a2b67d4 344 if (!before) return;
98db6da3 345 l= strlen(before)+1;
346 after= adns__alloc_final(qu,l);
347 memcpy(after,before,l);
348 *strp= after;
349}
350
11c8bf9b 351void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
9a2b67d4 352 void *before, *after;
98db6da3 353
9a2b67d4 354 before= *blpp;
355 if (!before) return;
98db6da3 356 after= adns__alloc_final(qu,sz);
9a2b67d4 357 memcpy(after,before,sz);
98db6da3 358 *blpp= after;
359}