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