Use struct sockaddr in several places; distinguish various places where
[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
a49a6d7b 76 qu->query_dglen= qumsg_vb->used;
77 if (qumsg_vb->used) {
78 qu->query_dgram= malloc(qumsg_vb->used);
79 if (!qu->query_dgram) {
80 adns__query_fail(qu,adns_s_nolocalmem);
81 return adns_s_ok;
82 }
83 memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
84 } else {
85 qu->query_dgram= 0;
ddfda861 86 }
e576be50 87 qu->vb= *qumsg_vb;
88 adns__vbuf_init(qumsg_vb);
89
90 if (failstat) {
3955725c 91 adns__query_fail(qu,failstat);
92 return adns_s_ok;
656b2da9 93 }
3955725c 94 adns__query_udp(qu,now);
e576be50 95 adns__autosys(ads,now);
96
3955725c 97 return adns_s_ok;
e576be50 98
99 x_freequ_nomemory:
100 free(qu);
101 x_nomemory:
3955725c 102 adns__vbuf_free(qumsg_vb);
103 return adns_s_nolocalmem;
4bec51a4 104}
105
e576be50 106int adns_submit(adns_state ads,
107 const char *owner,
108 adns_rrtype type,
109 adns_queryflags flags,
110 void *context,
111 adns_query *query_r) {
112 qcontext ctx;
3955725c 113 int id, r, ol;
e576be50 114 vbuf vb;
3955725c 115 adns_status stat;
116 const typeinfo *typei;
117 struct timeval now;
e576be50 118
3955725c 119 typei= adns__findtype(type);
120 if (!typei) return adns_s_notimplemented;
121
e576be50 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);
ffbda80c 129 if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_domaintoolong; goto xit; }
e576be50 130
131 if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
132
3955725c 133 stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
e576be50 134
135 xit:
3955725c 136 return adns__internal_submit(ads,query_r, typei,&vb,id, flags,now, stat,&ctx);
ddfda861 137}
138
e576be50 139int 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);
3955725c 153 if (r) adns_cancel(qu);
e576be50 154 return r;
155}
ddfda861 156
3955725c 157void adns_cancel(adns_query query) {
e576be50 158 abort(); /* fixme */
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;
e576be50 168 an->next= qu->allocations;
169 qu->allocations= an;
170 return (byte*)an + MEM_ROUND(sizeof(*an));
171}
4bec51a4 172
f759e52e 173void *adns__alloc_interim(adns_query qu, size_t sz) {
174 sz= MEM_ROUND(sz);
175 qu->interim_allocd += sz;
176 return alloc_common(qu,sz);
177}
178
179void *adns__alloc_mine(adns_query qu, size_t sz) {
180 return alloc_common(qu,MEM_ROUND(sz));
181}
182
e576be50 183void *adns__alloc_final(adns_query qu, size_t sz) {
184 /* When we're in the _final stage, we _subtract_ from interim_alloc'd
185 * each allocation, and use final_allocspace to point to the next free
186 * bit.
187 */
188 void *rp;
189
190 sz= MEM_ROUND(sz);
191 rp= qu->final_allocspace;
192 assert(rp);
193 qu->interim_allocd -= sz;
194 assert(qu->interim_allocd>=0);
195 qu->final_allocspace= (byte*)rp + sz;
196 return rp;
197}
198
3955725c 199void adns__reset_cnameonly(adns_query qu) {
9557e604 200 assert(!qu->final_allocspace);
e576be50 201 qu->answer->nrrs= 0;
202 qu->answer->rrs= 0;
203 qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
4353a5c4 204}
205
3955725c 206void adns__query_done(adns_query qu) {
98a3f706 207 adns_answer *ans;
8e5b0abb 208 allocnode *an, *ann;
68442019 209 int i;
8e5b0abb 210
e062dcae 211 if (qu->answer->status == adns_s_nolocalmem && !qu->interim_allocd) {
212 ans= qu->answer;
213 } else {
214 ans= realloc(qu->answer,
215 MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
216 if (!ans) {
217 qu->answer->cname= 0;
218 adns__query_fail(qu, adns_s_nolocalmem);
219 return;
220 }
221 qu->answer= ans;
222 }
223 qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
8e5b0abb 224
225 adns__makefinal_str(qu,&ans->cname);
226 if (ans->nrrs) {
227 adns__makefinal_block(qu,&ans->rrs.untyped,ans->rrsz*ans->nrrs);
228 for (i=0; i<ans->nrrs; i++)
3955725c 229 qu->typei->makefinal(qu,ans->rrs.bytes+ans->rrsz*i);
8e5b0abb 230 }
231
232 for (an= qu->allocations; an; an= ann) { ann= an->next; free(an); }
233
234 adns__vbuf_free(&qu->vb);
235
4353a5c4 236 qu->id= -1;
3955725c 237 LIST_LINK_TAIL(qu->ads->output,qu);
656b2da9 238}
239
3955725c 240void adns__query_fail(adns_query qu, adns_status stat) {
241 adns__reset_cnameonly(qu);
8e5b0abb 242 qu->answer->status= stat;
3955725c 243 adns__query_done(qu);
656b2da9 244}
e576be50 245
246void adns__makefinal_str(adns_query qu, char **strp) {
247 int l;
248 char *before, *after;
249
250 before= *strp;
9557e604 251 if (!before) return;
e576be50 252 l= strlen(before)+1;
253 after= adns__alloc_final(qu,l);
254 memcpy(after,before,l);
255 *strp= after;
256}
257
3955725c 258void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
9557e604 259 void *before, *after;
e576be50 260
9557e604 261 before= *blpp;
262 if (!before) return;
e576be50 263 after= adns__alloc_final(qu,sz);
9557e604 264 memcpy(after,before,sz);
e576be50 265 *blpp= after;
266}
267