Halfway through IPv6 stuff. Most of the _rr_addr handling is left
[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
e576be50 36int adns__internal_submit(adns_state ads, adns_query *query_r,
3955725c 37 const typeinfo *typei, vbuf *qumsg_vb, int id,
e576be50 38 adns_queryflags flags, struct timeval now,
39 adns_status failstat, const qcontext *ctx) {
40 adns_query qu;
e576be50 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
3955725c 45 qu->ads= ads;
e576be50 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;
551ff40f 50 LIST_INIT(qu->allocations);
e576be50 51 qu->interim_allocd= 0;
3955725c 52 qu->final_allocspace= 0;
e576be50 53
3955725c 54 qu->typei= typei;
e576be50 55 adns__vbuf_init(&qu->vb);
56
57 qu->cname_dgram= 0;
58 qu->cname_dglen= qu->cname_begin= 0;
4bec51a4 59
e576be50 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);
a6536d8b 66 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
e576be50 67
68 qu->answer->status= adns_s_ok;
69 qu->answer->cname= 0;
3955725c 70 qu->answer->type= typei->type;
e576be50 71 qu->answer->nrrs= 0;
72 qu->answer->rrs= 0;
3955725c 73 qu->answer->rrsz= typei->rrsz;
e576be50 74
75 *query_r= qu;
ddfda861 76
a49a6d7b 77 qu->query_dglen= qumsg_vb->used;
78 if (qumsg_vb->used) {
79 qu->query_dgram= malloc(qumsg_vb->used);
80 if (!qu->query_dgram) {
ea1e31e3 81 adns__query_fail(qu,adns_s_nomemory);
a49a6d7b 82 return adns_s_ok;
83 }
84 memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
85 } else {
86 qu->query_dgram= 0;
ddfda861 87 }
e576be50 88 qu->vb= *qumsg_vb;
89 adns__vbuf_init(qumsg_vb);
5ed1b7e7 90
91 if (failstat) goto x_failure;
e576be50 92
5ed1b7e7 93 if (qu->typei->begin) {
94 failstat= qu->typei->begin(ads,qu,now);
95 if (failstat) goto x_failure;
96 } else {
97 adns__query_udp(qu,now);
656b2da9 98 }
5ed1b7e7 99
e576be50 100 adns__autosys(ads,now);
101
3955725c 102 return adns_s_ok;
e576be50 103
104 x_freequ_nomemory:
105 free(qu);
106 x_nomemory:
3955725c 107 adns__vbuf_free(qumsg_vb);
ea1e31e3 108 return adns_s_nomemory;
5ed1b7e7 109
110 x_failure:
111 adns__query_fail(qu,failstat);
112 return adns_s_ok;
113}
114
115adns_status adns__subquery(adns_state ads, adns_query parent, vbuf *vb,
116 const byte *qd_dgram, int qd_dglen, int qd_begin,
117 adns_rrtype type, adns_queryflags flags,
118 struct timeval now, const qcontext *ctx) {
119 adns_status st;
120 int id;
121 adns_query nqu;
122
123 st= adns__mkquery_frdgram(ads, vb, &id,
124 qd_dgram, qd_dglen, qd_begin,
125 type, flags);
126 if (st) return st;
127
128 st= adns__internal_submit(ads, &nqu, adns__findtype(type),
129 vb, id, flags, now, 0, ctx);
130 if (st) return st;
131
132 nqu->parent= parent;
133 LIST_LINK_TAIL_PART(parent->children,nqu,siblings.);
134
135 return adns_s_ok;
4bec51a4 136}
137
e576be50 138int adns_submit(adns_state ads,
139 const char *owner,
140 adns_rrtype type,
141 adns_queryflags flags,
142 void *context,
143 adns_query *query_r) {
144 qcontext ctx;
3955725c 145 int id, r, ol;
e576be50 146 vbuf vb;
3955725c 147 adns_status stat;
148 const typeinfo *typei;
149 struct timeval now;
e576be50 150
3955725c 151 typei= adns__findtype(type);
ea1e31e3 152 if (!typei) return adns_s_unknownrrtype;
3955725c 153
e576be50 154 ctx.ext= context;
a6536d8b 155 ctx.callback= 0;
156 memset(&ctx.info,0,sizeof(ctx.info));
157
e576be50 158 r= gettimeofday(&now,0); if (r) return errno;
159 id= 0;
160
5ed1b7e7 161 if (!(flags & adns__iqaf_override)) {
162 flags &= ~adns__iqaf_mask;
163 flags |= ads->iflags & adns__iqaf_mask;
164 }
165
e576be50 166 adns__vbuf_init(&vb);
167
168 ol= strlen(owner);
ea1e31e3 169 if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto xit; }
e576be50 170
171 if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
172
5ed1b7e7 173
174 typei->initialtype ? typei->initialtype(ads,flags) : typei->type
175 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
e576be50 176
177 xit:
3955725c 178 return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx);
ddfda861 179}
180
e576be50 181int adns_synchronous(adns_state ads,
182 const char *owner,
183 adns_rrtype type,
184 adns_queryflags flags,
185 adns_answer **answer_r) {
186 adns_query qu;
187 int r;
188
189 r= adns_submit(ads,owner,type,flags,0,&qu);
190 if (r) return r;
4bec51a4 191
88372443 192 r= adns_wait(ads,&qu,answer_r,0);
3955725c 193 if (r) adns_cancel(qu);
ddfda861 194
88372443 195 return r;
e576be50 196}
197
f759e52e 198static void *alloc_common(adns_query qu, size_t sz) {
e576be50 199 allocnode *an;
200
a49a6d7b 201 if (!sz) return qu; /* Any old pointer will do */
e576be50 202 assert(!qu->final_allocspace);
e576be50 203 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
e062dcae 204 if (!an) return 0;
551ff40f 205 LIST_LINK_TAIL(qu->allocations,an);
e576be50 206 return (byte*)an + MEM_ROUND(sizeof(*an));
207}
4bec51a4 208
f759e52e 209void *adns__alloc_interim(adns_query qu, size_t sz) {
210 sz= MEM_ROUND(sz);
211 qu->interim_allocd += sz;
212 return alloc_common(qu,sz);
213}
214
215void *adns__alloc_mine(adns_query qu, size_t sz) {
216 return alloc_common(qu,MEM_ROUND(sz));
217}
218
551ff40f 219void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
220 allocnode *an;
221
222 if (!block) return;
223 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
224
225 assert(!to->final_allocspace);
226 assert(!from->final_allocspace);
227
228 LIST_UNLINK(from->allocations,an);
229 LIST_LINK_TAIL(to->allocations,an);
230
231 from->interim_allocd -= sz;
232 to->interim_allocd += sz;
233}
234
e576be50 235void *adns__alloc_final(adns_query qu, size_t sz) {
236 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
237 * each allocation, and use final_allocspace to point to the next free
238 * bit.
239 */
240 void *rp;
241
242 sz= MEM_ROUND(sz);
243 rp= qu->final_allocspace;
244 assert(rp);
245 qu->interim_allocd -= sz;
246 assert(qu->interim_allocd>=0);
247 qu->final_allocspace= (byte*)rp + sz;
248 return rp;
249}
250
9ec44266 251static void cancel_children(adns_query qu) {
252 adns_query cqu, ncqu;
253
254 for (cqu= qu->children.head; cqu; cqu= ncqu) {
255 ncqu= cqu->siblings.next;
256 adns_cancel(cqu);
257 }
258 LIST_INIT(qu->children);
259}
260
3955725c 261void adns__reset_cnameonly(adns_query qu) {
9557e604 262 assert(!qu->final_allocspace);
9ec44266 263 cancel_children(qu);
e576be50 264 qu->answer->nrrs= 0;
265 qu->answer->rrs= 0;
266 qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
4353a5c4 267}
268
88372443 269static void free_query_allocs(adns_query qu) {
270 allocnode *an, *ann;
88372443 271
9ec44266 272 cancel_children(qu);
551ff40f 273 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
88372443 274 adns__vbuf_free(&qu->vb);
275}
276
1dfe95d8 277void adns_cancel(adns_query qu) {
88372443 278 switch (qu->state) {
279 case query_udp: case query_tcpwait: case query_tcpsent:
1dfe95d8 280 LIST_UNLINK(qu->ads->timew,qu);
88372443 281 break;
282 case query_child:
1dfe95d8 283 LIST_UNLINK(qu->ads->childw,qu);
88372443 284 break;
285 case query_done:
1dfe95d8 286 LIST_UNLINK(qu->ads->output,qu);
88372443 287 break;
288 default:
289 abort();
290 }
291 free_query_allocs(qu);
292 free(qu->answer);
293 free(qu);
294}
551ff40f 295
296static void makefinal_query(adns_query qu) {
98a3f706 297 adns_answer *ans;
88372443 298 int rrn;
8e5b0abb 299
88372443 300 ans= qu->answer;
551ff40f 301
88372443 302 if (qu->interim_allocd) {
88372443 303 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
551ff40f 304 if (!ans) goto x_nomem;
e062dcae 305 qu->answer= ans;
306 }
8e5b0abb 307
88372443 308 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
8e5b0abb 309 adns__makefinal_str(qu,&ans->cname);
88372443 310
8e5b0abb 311 if (ans->nrrs) {
88372443 312 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
8e5b0abb 313
88372443 314 for (rrn=0; rrn<ans->nrrs; rrn++)
315 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
88372443 316 }
551ff40f 317
88372443 318 free_query_allocs(qu);
551ff40f 319 return;
8e5b0abb 320
551ff40f 321 x_nomem:
ea1e31e3 322 qu->answer->status= adns_s_nomemory;
551ff40f 323 qu->answer->cname= 0;
324 adns__reset_cnameonly(qu);
325 free_query_allocs(qu);
326}
327
328void adns__query_done(adns_query qu) {
329 adns_answer *ans;
330 adns_query parent;
331
4353a5c4 332 qu->id= -1;
551ff40f 333 ans= qu->answer;
334
335 if (ans->nrrs && qu->typei->diff_needswap) {
336 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
ea1e31e3 337 adns__query_fail(qu,adns_s_nomemory);
551ff40f 338 return;
339 }
340 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
09957b1c 341 qu->vb.buf,
342 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
343 qu->ads);
551ff40f 344 }
e553d072 345
551ff40f 346 parent= qu->parent;
347 if (parent) {
348 LIST_UNLINK_PART(parent->children,qu,siblings.);
a6536d8b 349 qu->ctx.callback(parent,qu);
551ff40f 350 free_query_allocs(qu);
351 free(qu);
551ff40f 352 } else {
353 makefinal_query(qu);
354 LIST_LINK_TAIL(qu->ads->output,qu);
355 }
656b2da9 356}
357
3955725c 358void adns__query_fail(adns_query qu, adns_status stat) {
359 adns__reset_cnameonly(qu);
8e5b0abb 360 qu->answer->status= stat;
3955725c 361 adns__query_done(qu);
656b2da9 362}
e576be50 363
364void adns__makefinal_str(adns_query qu, char **strp) {
365 int l;
366 char *before, *after;
367
368 before= *strp;
9557e604 369 if (!before) return;
e576be50 370 l= strlen(before)+1;
371 after= adns__alloc_final(qu,l);
372 memcpy(after,before,l);
373 *strp= after;
374}
375
3955725c 376void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
9557e604 377 void *before, *after;
e576be50 378
9557e604 379 before= *blpp;
380 if (!before) return;
e576be50 381 after= adns__alloc_final(qu,sz);
9557e604 382 memcpy(after,before,sz);
e576be50 383 *blpp= after;
384}