656b2da9 |
1 | /**/ |
2 | |
4353a5c4 |
3 | #include <stdlib.h> |
4 | #include <unistd.h> |
5 | #include <errno.h> |
6 | |
7 | #include <sys/time.h> |
8 | |
9 | #include "internal.h" |
656b2da9 |
10 | |
8e5b0abb |
11 | int adns__internal_submit(adns_state ads, adns_query *query_r, |
68442019 |
12 | adns_rrtype type, vbuf *qumsg_vb, int id, |
8e5b0abb |
13 | adns_queryflags flags, struct timeval now, |
14 | adns_status failstat, const qcontext *ctx) { |
8e5b0abb |
15 | adns_query qu; |
16 | adns_status stat; |
17 | int ol, id, r; |
18 | struct timeval now; |
19 | const typeinfo *typei; |
656b2da9 |
20 | adns_query qu; |
0ba0614a |
21 | |
8e5b0abb |
22 | qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory; |
23 | qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory; |
24 | |
4353a5c4 |
25 | qu->state= query_udp; |
8e5b0abb |
26 | qu->back= qu->next= qu->parent= 0; |
4353a5c4 |
27 | LIST_INIT(qu->children); |
656b2da9 |
28 | qu->siblings.next= qu->siblings.back= 0; |
8e5b0abb |
29 | qu->allocations= 0; |
30 | qu->interim_allocd= 0; |
31 | qu->perm_used= 0; |
32 | |
33 | qu->typei= adns__findtype(type); |
8e5b0abb |
34 | adns__vbuf_init(&qu->vb); |
35 | |
36 | qu->cname_dgram= 0; |
37 | qu->cname_dglen= qu->cname_begin= 0; |
38 | |
4353a5c4 |
39 | qu->id= id; |
656b2da9 |
40 | qu->flags= flags; |
656b2da9 |
41 | qu->udpretries= 0; |
4353a5c4 |
42 | qu->udpnextserver= 0; |
43 | qu->udpsent= qu->tcpfailed= 0; |
44 | timerclear(&qu->timeout); |
45 | memcpy(&qu->context,ctx,sizeof(qu->context)); |
656b2da9 |
46 | memcpy(qu->owner,owner,ol); qu->owner[ol]= 0; |
656b2da9 |
47 | |
8e5b0abb |
48 | qu->answer->status= adns_s_ok; |
49 | qu->answer->cname= 0; |
50 | qu->answer->type= type; |
51 | qu->answer->nrrs= 0; |
52 | qu->answer->rrs= 0; |
656b2da9 |
53 | |
8e5b0abb |
54 | if (qu->typei) { |
55 | qu->answer->rrsz= qu->rrsz; |
56 | } else { |
57 | qu->answer->rrsz= -1; |
58 | failstat= adns_s_notimplemented; |
59 | } |
68442019 |
60 | |
61 | *query_r= qu; |
62 | |
63 | qu->query_dgram= malloc(qumsg_vb->used); |
64 | if (!qu->query_dgram) { |
65 | adns__query_fail(ads,qu,adns_s_nomemory); |
66 | return; |
67 | } |
68 | memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used); |
69 | qu->vb= *qumsg_vb; |
70 | adns__vbuf_init(qumsg_vb); |
71 | |
8e5b0abb |
72 | if (failstat) { |
73 | adns__query_fail(ads,qu,failstat); |
74 | return; |
75 | } |
8e5b0abb |
76 | adns__query_udp(ads,qu,now); |
77 | adns__autosys(ads,now); |
78 | |
656b2da9 |
79 | return 0; |
8e5b0abb |
80 | |
81 | x_freequ_nomemory: |
82 | free(qu); |
83 | x_nomemory: |
84 | free(query_dgram); |
85 | return adns_s_nomemory; |
656b2da9 |
86 | } |
87 | |
88 | int adns_submit(adns_state ads, |
89 | const char *owner, |
90 | adns_rrtype type, |
91 | adns_queryflags flags, |
92 | void *context, |
93 | adns_query *query_r) { |
4353a5c4 |
94 | qcontext ctx; |
68442019 |
95 | int id; |
96 | vbuf vb; |
656b2da9 |
97 | |
4353a5c4 |
98 | ctx.ext= context; |
ddfda861 |
99 | r= gettimeofday(&now,0); if (r) return errno; |
68442019 |
100 | id= 0; |
101 | |
102 | adns__vbuf_init(&vb); |
ddfda861 |
103 | |
656b2da9 |
104 | ol= strlen(owner); |
68442019 |
105 | if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_invaliddomain; goto xit; } |
106 | |
4353a5c4 |
107 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } |
656b2da9 |
108 | |
68442019 |
109 | stat= adns__mkquery(ads,&vb, &id, owner,ol, typei,flags); |
110 | |
111 | xit: |
112 | return adns__internal_submit(ads,query_r, type,&vb,id, flags,now, stat,&ctx); |
656b2da9 |
113 | } |
114 | |
115 | int adns_synchronous(adns_state ads, |
116 | const char *owner, |
117 | adns_rrtype type, |
118 | adns_queryflags flags, |
119 | adns_answer **answer_r) { |
120 | adns_query qu; |
121 | int r; |
122 | |
123 | r= adns_submit(ads,owner,type,flags,0,&qu); |
124 | if (r) return r; |
125 | |
126 | do { |
127 | r= adns_wait(ads,&qu,answer_r,0); |
128 | } while (r==EINTR); |
129 | if (r) adns_cancel(ads,qu); |
130 | return r; |
131 | } |
132 | |
133 | void adns_cancel(adns_state ads, adns_query query) { |
94436798 |
134 | abort(); /* fixme */ |
656b2da9 |
135 | } |
8e5b0abb |
136 | |
68442019 |
137 | void adns__makefinal_str(adns_query qu, char **strp) { |
138 | int l; |
139 | char *before, *after; |
140 | |
141 | before= *strp; |
142 | l= strlen(before)+1; |
143 | after= adns__alloc_final(qu,l); |
144 | memcpy(after,before,l); |
145 | *strp= after; |
146 | } |
147 | |
148 | void adns__makefinal_block(adns__query qu, void **blpp, size_t sz) { |
149 | void *after; |
150 | |
151 | after= adns__alloc_final(qu,sz); |
152 | memcpy(after,*blpp,sz); |
153 | *blpp= after; |
154 | } |
155 | |
8e5b0abb |
156 | void *adns__alloc_interim(adns_state ads, adns_query qu, size_t sz) { |
157 | allocnode *an; |
158 | |
68442019 |
159 | assert(!qu->final_allocspace); |
8e5b0abb |
160 | sz= MEM_ROUND(sz); |
161 | an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz)); |
162 | if (!an) { |
163 | adns__query_fail(ads,qu,adns_s_nolocalmem); |
164 | return 0; |
165 | } |
166 | qu->permalloclen += sz; |
167 | an->next= qu->allocations; |
168 | qu->allocations= an; |
169 | return (byte*)an + MEM_ROUND(sizeof(*an)); |
170 | } |
68442019 |
171 | |
172 | void *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 | } |