Compiles but does not link.
[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/*
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 */
4353a5c4 24
4bec51a4 25#include "internal.h"
656b2da9 26
e576be50 27#include <stdlib.h>
28#include <unistd.h>
29#include <errno.h>
656b2da9 30
e576be50 31#include <sys/time.h>
656b2da9 32
e576be50 33#include "internal.h"
ddfda861 34
e576be50 35int adns__internal_submit(adns_state ads, adns_query *query_r,
3955725c 36 const typeinfo *typei, vbuf *qumsg_vb, int id,
e576be50 37 adns_queryflags flags, struct timeval now,
38 adns_status failstat, const qcontext *ctx) {
39 adns_query qu;
e576be50 40
41 qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory;
42 qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory;
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);
48 qu->siblings.next= qu->siblings.back= 0;
49 qu->allocations= 0;
50 qu->interim_allocd= 0;
3955725c 51 qu->final_allocspace= 0;
e576be50 52
3955725c 53 qu->typei= typei;
e576be50 54 adns__vbuf_init(&qu->vb);
55
56 qu->cname_dgram= 0;
57 qu->cname_dglen= qu->cname_begin= 0;
4bec51a4 58
e576be50 59 qu->id= id;
60 qu->flags= flags;
61 qu->udpretries= 0;
62 qu->udpnextserver= 0;
63 qu->udpsent= qu->tcpfailed= 0;
64 timerclear(&qu->timeout);
65 memcpy(&qu->context,ctx,sizeof(qu->context));
e576be50 66
67 qu->answer->status= adns_s_ok;
68 qu->answer->cname= 0;
3955725c 69 qu->answer->type= typei->type;
e576be50 70 qu->answer->nrrs= 0;
71 qu->answer->rrs= 0;
3955725c 72 qu->answer->rrsz= typei->rrsz;
e576be50 73
74 *query_r= qu;
ddfda861 75
e576be50 76 qu->query_dgram= malloc(qumsg_vb->used);
77 if (!qu->query_dgram) {
3955725c 78 adns__query_fail(qu,adns_s_nolocalmem);
79 return adns_s_ok;
ddfda861 80 }
e576be50 81 memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
82 qu->vb= *qumsg_vb;
83 adns__vbuf_init(qumsg_vb);
84
85 if (failstat) {
3955725c 86 adns__query_fail(qu,failstat);
87 return adns_s_ok;
656b2da9 88 }
3955725c 89 adns__query_udp(qu,now);
e576be50 90 adns__autosys(ads,now);
91
3955725c 92 return adns_s_ok;
e576be50 93
94 x_freequ_nomemory:
95 free(qu);
96 x_nomemory:
3955725c 97 adns__vbuf_free(qumsg_vb);
98 return adns_s_nolocalmem;
4bec51a4 99}
100
e576be50 101int adns_submit(adns_state ads,
102 const char *owner,
103 adns_rrtype type,
104 adns_queryflags flags,
105 void *context,
106 adns_query *query_r) {
107 qcontext ctx;
3955725c 108 int id, r, ol;
e576be50 109 vbuf vb;
3955725c 110 adns_status stat;
111 const typeinfo *typei;
112 struct timeval now;
e576be50 113
3955725c 114 typei= adns__findtype(type);
115 if (!typei) return adns_s_notimplemented;
116
e576be50 117 ctx.ext= context;
118 r= gettimeofday(&now,0); if (r) return errno;
119 id= 0;
120
121 adns__vbuf_init(&vb);
122
123 ol= strlen(owner);
124 if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_invaliddomain; goto xit; }
125
126 if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
127
3955725c 128 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
e576be50 129
130 xit:
3955725c 131 return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx);
ddfda861 132}
133
e576be50 134int adns_synchronous(adns_state ads,
135 const char *owner,
136 adns_rrtype type,
137 adns_queryflags flags,
138 adns_answer **answer_r) {
139 adns_query qu;
140 int r;
141
142 r= adns_submit(ads,owner,type,flags,0,&qu);
143 if (r) return r;
4bec51a4 144
e576be50 145 do {
146 r= adns_wait(ads,&qu,answer_r,0);
147 } while (r==EINTR);
3955725c 148 if (r) adns_cancel(qu);
e576be50 149 return r;
150}
ddfda861 151
3955725c 152void adns_cancel(adns_query query) {
e576be50 153 abort(); /* fixme */
154}
155
3955725c 156void *adns__alloc_interim(adns_query qu, size_t sz) {
e576be50 157 allocnode *an;
158
159 assert(!qu->final_allocspace);
160 sz= MEM_ROUND(sz);
161 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
162 if (!an) {
3955725c 163 adns__query_fail(qu,adns_s_nolocalmem);
e576be50 164 return 0;
4bec51a4 165 }
3955725c 166 qu->interim_allocd += sz;
e576be50 167 an->next= qu->allocations;
168 qu->allocations= an;
169 return (byte*)an + MEM_ROUND(sizeof(*an));
170}
4bec51a4 171
e576be50 172void *adns__alloc_final(adns_query qu, size_t sz) {
173 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
174 * each allocation, and use final_allocspace to point to the next free
175 * bit.
176 */
177 void *rp;
178
179 sz= MEM_ROUND(sz);
180 rp= qu->final_allocspace;
181 assert(rp);
182 qu->interim_allocd -= sz;
183 assert(qu->interim_allocd>=0);
184 qu->final_allocspace= (byte*)rp + sz;
185 return rp;
186}
187
3955725c 188void adns__reset_cnameonly(adns_query qu) {
e576be50 189 assert(qu->final_allocspace);
190 qu->answer->nrrs= 0;
191 qu->answer->rrs= 0;
192 qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
4353a5c4 193}
194
3955725c 195void adns__query_done(adns_query qu) {
98a3f706 196 adns_answer *ans;
8e5b0abb 197 allocnode *an, *ann;
68442019 198 int i;
8e5b0abb 199
200 qu->answer= ans= realloc(qu->answer,
201 MEM_ROUND(MEM_ROUND(sizeof(*ans)) +
202 qu->interim_allocd));
68442019 203 qu->final_allocspace= (byte*)qu->answer + MEM_ROUND(sizeof(*ans));
8e5b0abb 204
205 adns__makefinal_str(qu,&ans->cname);
206 if (ans->nrrs) {
207 adns__makefinal_block(qu,&ans->rrs.untyped,ans->rrsz*ans->nrrs);
208 for (i=0; i<ans->nrrs; i++)
3955725c 209 qu->typei->makefinal(qu,ans->rrs.bytes+ans->rrsz*i);
8e5b0abb 210 }
211
212 for (an= qu->allocations; an; an= ann) { ann= an->next; free(an); }
213
214 adns__vbuf_free(&qu->vb);
215
4353a5c4 216 qu->id= -1;
3955725c 217 LIST_LINK_TAIL(qu->ads->output,qu);
656b2da9 218}
219
3955725c 220void adns__query_fail(adns_query qu, adns_status stat) {
221 adns__reset_cnameonly(qu);
8e5b0abb 222 qu->answer->status= stat;
3955725c 223 adns__query_done(qu);
656b2da9 224}
e576be50 225
226void adns__makefinal_str(adns_query qu, char **strp) {
227 int l;
228 char *before, *after;
229
230 before= *strp;
231 l= strlen(before)+1;
232 after= adns__alloc_final(qu,l);
233 memcpy(after,before,l);
234 *strp= after;
235}
236
3955725c 237void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
e576be50 238 void *after;
239
240 after= adns__alloc_final(qu,sz);
241 memcpy(after,*blpp,sz);
242 *blpp= after;
243}
244