Halfway through getting it to compile; about to move various bits of
[adns] / src / submit.c
1 /**/
2
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <errno.h>
6
7 #include <sys/time.h>
8
9 #include "internal.h"
10
11 int adns__internal_submit(adns_state ads, adns_query *query_r,
12 adns_rrtype type, vbuf *qumsg_vb, int id,
13 adns_queryflags flags, struct timeval now,
14 adns_status failstat, const qcontext *ctx) {
15 adns_query qu;
16 adns_status stat;
17 int ol, id, r;
18 struct timeval now;
19 const typeinfo *typei;
20 adns_query qu;
21
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
25 qu->state= query_udp;
26 qu->back= qu->next= qu->parent= 0;
27 LIST_INIT(qu->children);
28 qu->siblings.next= qu->siblings.back= 0;
29 qu->allocations= 0;
30 qu->interim_allocd= 0;
31 qu->perm_used= 0;
32
33 qu->typei= adns__findtype(type);
34 adns__vbuf_init(&qu->vb);
35
36 qu->cname_dgram= 0;
37 qu->cname_dglen= qu->cname_begin= 0;
38
39 qu->id= id;
40 qu->flags= flags;
41 qu->udpretries= 0;
42 qu->udpnextserver= 0;
43 qu->udpsent= qu->tcpfailed= 0;
44 timerclear(&qu->timeout);
45 memcpy(&qu->context,ctx,sizeof(qu->context));
46 memcpy(qu->owner,owner,ol); qu->owner[ol]= 0;
47
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;
53
54 if (qu->typei) {
55 qu->answer->rrsz= qu->rrsz;
56 } else {
57 qu->answer->rrsz= -1;
58 failstat= adns_s_notimplemented;
59 }
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
72 if (failstat) {
73 adns__query_fail(ads,qu,failstat);
74 return;
75 }
76 adns__query_udp(ads,qu,now);
77 adns__autosys(ads,now);
78
79 return 0;
80
81 x_freequ_nomemory:
82 free(qu);
83 x_nomemory:
84 free(query_dgram);
85 return adns_s_nomemory;
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) {
94 qcontext ctx;
95 int id;
96 vbuf vb;
97
98 ctx.ext= context;
99 r= gettimeofday(&now,0); if (r) return errno;
100 id= 0;
101
102 adns__vbuf_init(&vb);
103
104 ol= strlen(owner);
105 if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_invaliddomain; goto xit; }
106
107 if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
108
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);
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) {
134 abort(); /* fixme */
135 }
136
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
156 void *adns__alloc_interim(adns_state ads, adns_query qu, size_t sz) {
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) {
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 }
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 }