SIGPIPE protection available.
[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));
73dba56e 67 qu->expires= now.tv_sec + MAXTTLBELIEVE;
e576be50 68
69 qu->answer->status= adns_s_ok;
70 qu->answer->cname= 0;
3955725c 71 qu->answer->type= typei->type;
e576be50 72 qu->answer->nrrs= 0;
73 qu->answer->rrs= 0;
3955725c 74 qu->answer->rrsz= typei->rrsz;
e576be50 75
76 *query_r= qu;
ddfda861 77
a49a6d7b 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) {
ea1e31e3 82 adns__query_fail(qu,adns_s_nomemory);
a49a6d7b 83 return adns_s_ok;
84 }
85 memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
86 } else {
87 qu->query_dgram= 0;
ddfda861 88 }
e576be50 89 qu->vb= *qumsg_vb;
90 adns__vbuf_init(qumsg_vb);
91
92 if (failstat) {
3955725c 93 adns__query_fail(qu,failstat);
94 return adns_s_ok;
656b2da9 95 }
3955725c 96 adns__query_udp(qu,now);
e576be50 97 adns__autosys(ads,now);
98
3955725c 99 return adns_s_ok;
e576be50 100
101 x_freequ_nomemory:
102 free(qu);
103 x_nomemory:
3955725c 104 adns__vbuf_free(qumsg_vb);
ea1e31e3 105 return adns_s_nomemory;
4bec51a4 106}
107
e576be50 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;
3955725c 115 int id, r, ol;
e576be50 116 vbuf vb;
3955725c 117 adns_status stat;
118 const typeinfo *typei;
119 struct timeval now;
e576be50 120
3955725c 121 typei= adns__findtype(type);
ea1e31e3 122 if (!typei) return adns_s_unknownrrtype;
3955725c 123
e576be50 124 ctx.ext= context;
a6536d8b 125 ctx.callback= 0;
126 memset(&ctx.info,0,sizeof(ctx.info));
127
e576be50 128 r= gettimeofday(&now,0); if (r) return errno;
129 id= 0;
130
131 adns__vbuf_init(&vb);
132
133 ol= strlen(owner);
ea1e31e3 134 if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto xit; }
e576be50 135
136 if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
137
3955725c 138 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
e576be50 139
140 xit:
3955725c 141 return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx);
ddfda861 142}
143
e576be50 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;
4bec51a4 154
88372443 155 r= adns_wait(ads,&qu,answer_r,0);
3955725c 156 if (r) adns_cancel(qu);
ddfda861 157
88372443 158 return r;
e576be50 159}
160
f759e52e 161static void *alloc_common(adns_query qu, size_t sz) {
e576be50 162 allocnode *an;
163
a49a6d7b 164 if (!sz) return qu; /* Any old pointer will do */
e576be50 165 assert(!qu->final_allocspace);
e576be50 166 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
e062dcae 167 if (!an) return 0;
551ff40f 168 LIST_LINK_TAIL(qu->allocations,an);
e576be50 169 return (byte*)an + MEM_ROUND(sizeof(*an));
170}
4bec51a4 171
f759e52e 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
551ff40f 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;
73dba56e 196
197 if (to->expires > from->expires) to->expires= from->expires;
551ff40f 198}
199
e576be50 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
9ec44266 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
3955725c 226void adns__reset_cnameonly(adns_query qu) {
9557e604 227 assert(!qu->final_allocspace);
9ec44266 228 cancel_children(qu);
e576be50 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;
4353a5c4 232}
233
88372443 234static void free_query_allocs(adns_query qu) {
235 allocnode *an, *ann;
88372443 236
9ec44266 237 cancel_children(qu);
551ff40f 238 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
88372443 239 adns__vbuf_free(&qu->vb);
240}
241
1dfe95d8 242void adns_cancel(adns_query qu) {
88372443 243 switch (qu->state) {
244 case query_udp: case query_tcpwait: case query_tcpsent:
1dfe95d8 245 LIST_UNLINK(qu->ads->timew,qu);
88372443 246 break;
247 case query_child:
1dfe95d8 248 LIST_UNLINK(qu->ads->childw,qu);
88372443 249 break;
250 case query_done:
1dfe95d8 251 LIST_UNLINK(qu->ads->output,qu);
88372443 252 break;
253 default:
254 abort();
255 }
256 free_query_allocs(qu);
257 free(qu->answer);
258 free(qu);
259}
551ff40f 260
73dba56e 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
551ff40f 270static void makefinal_query(adns_query qu) {
98a3f706 271 adns_answer *ans;
88372443 272 int rrn;
8e5b0abb 273
88372443 274 ans= qu->answer;
551ff40f 275
88372443 276 if (qu->interim_allocd) {
88372443 277 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
551ff40f 278 if (!ans) goto x_nomem;
e062dcae 279 qu->answer= ans;
280 }
8e5b0abb 281
88372443 282 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
8e5b0abb 283 adns__makefinal_str(qu,&ans->cname);
88372443 284
8e5b0abb 285 if (ans->nrrs) {
88372443 286 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
8e5b0abb 287
88372443 288 for (rrn=0; rrn<ans->nrrs; rrn++)
289 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
88372443 290 }
73dba56e 291
88372443 292 free_query_allocs(qu);
551ff40f 293 return;
8e5b0abb 294
551ff40f 295 x_nomem:
ea1e31e3 296 qu->answer->status= adns_s_nomemory;
551ff40f 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
4353a5c4 306 qu->id= -1;
551ff40f 307 ans= qu->answer;
308
309 if (ans->nrrs && qu->typei->diff_needswap) {
310 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
ea1e31e3 311 adns__query_fail(qu,adns_s_nomemory);
551ff40f 312 return;
313 }
314 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
09957b1c 315 qu->vb.buf,
316 (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
317 qu->ads);
551ff40f 318 }
e553d072 319
73dba56e 320 ans->expires= qu->expires;
551ff40f 321 parent= qu->parent;
322 if (parent) {
323 LIST_UNLINK_PART(parent->children,qu,siblings.);
a6536d8b 324 qu->ctx.callback(parent,qu);
551ff40f 325 free_query_allocs(qu);
326 free(qu);
551ff40f 327 } else {
328 makefinal_query(qu);
329 LIST_LINK_TAIL(qu->ads->output,qu);
330 }
656b2da9 331}
332
3955725c 333void adns__query_fail(adns_query qu, adns_status stat) {
334 adns__reset_cnameonly(qu);
8e5b0abb 335 qu->answer->status= stat;
3955725c 336 adns__query_done(qu);
656b2da9 337}
e576be50 338
339void adns__makefinal_str(adns_query qu, char **strp) {
340 int l;
341 char *before, *after;
342
343 before= *strp;
9557e604 344 if (!before) return;
e576be50 345 l= strlen(before)+1;
346 after= adns__alloc_final(qu,l);
347 memcpy(after,before,l);
348 *strp= after;
349}
350
3955725c 351void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
9557e604 352 void *before, *after;
e576be50 353
9557e604 354 before= *blpp;
355 if (!before) return;
e576be50 356 after= adns__alloc_final(qu,sz);
9557e604 357 memcpy(after,before,sz);
e576be50 358 *blpp= after;
359}