Implement SOA, RP, HINFO (but no mailbox quoting). Implement _finish.
[adns] / src / query.c
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 /*
8 * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
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 */
24
25 #include "internal.h"
26
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31
32 #include <sys/time.h>
33
34 #include "internal.h"
35
36 int adns__internal_submit(adns_state ads, adns_query *query_r,
37 const typeinfo *typei, vbuf *qumsg_vb, int id,
38 adns_queryflags flags, struct timeval now,
39 adns_status failstat, const qcontext *ctx) {
40 adns_query qu;
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
45 qu->ads= ads;
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;
50 LIST_INIT(qu->allocations);
51 qu->interim_allocd= 0;
52 qu->final_allocspace= 0;
53
54 qu->typei= typei;
55 adns__vbuf_init(&qu->vb);
56
57 qu->cname_dgram= 0;
58 qu->cname_dglen= qu->cname_begin= 0;
59
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);
66 memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
67
68 qu->answer->status= adns_s_ok;
69 qu->answer->cname= 0;
70 qu->answer->type= typei->type;
71 qu->answer->nrrs= 0;
72 qu->answer->rrs= 0;
73 qu->answer->rrsz= typei->rrsz;
74
75 *query_r= qu;
76
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) {
81 adns__query_fail(qu,adns_s_nolocalmem);
82 return adns_s_ok;
83 }
84 memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
85 } else {
86 qu->query_dgram= 0;
87 }
88 qu->vb= *qumsg_vb;
89 adns__vbuf_init(qumsg_vb);
90
91 if (failstat) {
92 adns__query_fail(qu,failstat);
93 return adns_s_ok;
94 }
95 adns__query_udp(qu,now);
96 adns__autosys(ads,now);
97
98 return adns_s_ok;
99
100 x_freequ_nomemory:
101 free(qu);
102 x_nomemory:
103 adns__vbuf_free(qumsg_vb);
104 return adns_s_nolocalmem;
105 }
106
107 int adns_submit(adns_state ads,
108 const char *owner,
109 adns_rrtype type,
110 adns_queryflags flags,
111 void *context,
112 adns_query *query_r) {
113 qcontext ctx;
114 int id, r, ol;
115 vbuf vb;
116 adns_status stat;
117 const typeinfo *typei;
118 struct timeval now;
119
120 typei= adns__findtype(type);
121 if (!typei) return adns_s_notimplemented;
122
123 ctx.ext= context;
124 ctx.callback= 0;
125 memset(&ctx.info,0,sizeof(ctx.info));
126
127 r= gettimeofday(&now,0); if (r) return errno;
128 id= 0;
129
130 adns__vbuf_init(&vb);
131
132 ol= strlen(owner);
133 if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_domaintoolong; goto xit; }
134
135 if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
136
137 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
138
139 xit:
140 return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx);
141 }
142
143 int adns_synchronous(adns_state ads,
144 const char *owner,
145 adns_rrtype type,
146 adns_queryflags flags,
147 adns_answer **answer_r) {
148 adns_query qu;
149 int r;
150
151 r= adns_submit(ads,owner,type,flags,0,&qu);
152 if (r) return r;
153
154 r= adns_wait(ads,&qu,answer_r,0);
155 if (r) adns_cancel(qu);
156
157 return r;
158 }
159
160 static void *alloc_common(adns_query qu, size_t sz) {
161 allocnode *an;
162
163 if (!sz) return qu; /* Any old pointer will do */
164 assert(!qu->final_allocspace);
165 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
166 if (!an) return 0;
167 LIST_LINK_TAIL(qu->allocations,an);
168 return (byte*)an + MEM_ROUND(sizeof(*an));
169 }
170
171 void *adns__alloc_interim(adns_query qu, size_t sz) {
172 sz= MEM_ROUND(sz);
173 qu->interim_allocd += sz;
174 return alloc_common(qu,sz);
175 }
176
177 void *adns__alloc_mine(adns_query qu, size_t sz) {
178 return alloc_common(qu,MEM_ROUND(sz));
179 }
180
181 void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
182 allocnode *an;
183
184 if (!block) return;
185 an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
186
187 assert(!to->final_allocspace);
188 assert(!from->final_allocspace);
189
190 LIST_UNLINK(from->allocations,an);
191 LIST_LINK_TAIL(to->allocations,an);
192
193 from->interim_allocd -= sz;
194 to->interim_allocd += sz;
195 }
196
197 void *adns__alloc_final(adns_query qu, size_t sz) {
198 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
199 * each allocation, and use final_allocspace to point to the next free
200 * bit.
201 */
202 void *rp;
203
204 sz= MEM_ROUND(sz);
205 rp= qu->final_allocspace;
206 assert(rp);
207 qu->interim_allocd -= sz;
208 assert(qu->interim_allocd>=0);
209 qu->final_allocspace= (byte*)rp + sz;
210 return rp;
211 }
212
213 static void cancel_children(adns_query qu) {
214 adns_query cqu, ncqu;
215
216 for (cqu= qu->children.head; cqu; cqu= ncqu) {
217 ncqu= cqu->siblings.next;
218 adns_cancel(cqu);
219 }
220 LIST_INIT(qu->children);
221 }
222
223 void adns__reset_cnameonly(adns_query qu) {
224 assert(!qu->final_allocspace);
225 cancel_children(qu);
226 qu->answer->nrrs= 0;
227 qu->answer->rrs= 0;
228 qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
229 }
230
231 static void free_query_allocs(adns_query qu) {
232 allocnode *an, *ann;
233
234 cancel_children(qu);
235 for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
236 adns__vbuf_free(&qu->vb);
237 }
238
239 void adns_cancel(adns_query qu) {
240 switch (qu->state) {
241 case query_udp: case query_tcpwait: case query_tcpsent:
242 LIST_UNLINK(qu->ads->timew,qu);
243 break;
244 case query_child:
245 LIST_UNLINK(qu->ads->childw,qu);
246 break;
247 case query_done:
248 LIST_UNLINK(qu->ads->output,qu);
249 break;
250 default:
251 abort();
252 }
253 free_query_allocs(qu);
254 free(qu->answer);
255 free(qu);
256 }
257
258 static void makefinal_query(adns_query qu) {
259 adns_answer *ans;
260 int rrn;
261
262 ans= qu->answer;
263
264 if (qu->interim_allocd) {
265 ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
266 if (!ans) goto x_nomem;
267 qu->answer= ans;
268 }
269
270 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
271 adns__makefinal_str(qu,&ans->cname);
272
273 if (ans->nrrs) {
274 adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
275
276 for (rrn=0; rrn<ans->nrrs; rrn++)
277 qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
278 }
279
280 free_query_allocs(qu);
281 return;
282
283 x_nomem:
284 qu->answer->status= adns_s_nolocalmem;
285 qu->answer->cname= 0;
286 adns__reset_cnameonly(qu);
287 free_query_allocs(qu);
288 }
289
290 void adns__query_done(adns_query qu) {
291 adns_answer *ans;
292 adns_query parent;
293
294 qu->id= -1;
295 ans= qu->answer;
296
297 if (ans->nrrs && qu->typei->diff_needswap) {
298 if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
299 adns__query_fail(qu,adns_s_nolocalmem);
300 return;
301 }
302 adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
303 qu->vb.buf, qu->typei->diff_needswap);
304 }
305
306 parent= qu->parent;
307 if (parent) {
308 LIST_UNLINK_PART(parent->children,qu,siblings.);
309 qu->ctx.callback(parent,qu);
310 free_query_allocs(qu);
311 free(qu);
312 } else {
313 makefinal_query(qu);
314 LIST_LINK_TAIL(qu->ads->output,qu);
315 }
316 }
317
318 void adns__query_fail(adns_query qu, adns_status stat) {
319 adns__reset_cnameonly(qu);
320 qu->answer->status= stat;
321 adns__query_done(qu);
322 }
323
324 void adns__makefinal_str(adns_query qu, char **strp) {
325 int l;
326 char *before, *after;
327
328 before= *strp;
329 if (!before) return;
330 l= strlen(before)+1;
331 after= adns__alloc_final(qu,l);
332 memcpy(after,before,l);
333 *strp= after;
334 }
335
336 void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
337 void *before, *after;
338
339 before= *blpp;
340 if (!before) return;
341 after= adns__alloc_final(qu,sz);
342 memcpy(after,before,sz);
343 *blpp= after;
344 }