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