New memory management arrangements pretty much finished, but probably still will...
[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,
12 adns_rrtype type, char *query_dgram, int query_len,
13 adns_queryflags flags, struct timeval now,
14 adns_status failstat, const qcontext *ctx) {
15 /* Submits a query (for internal use, called during external submits).
16 *
17 * The new query is returned in *query_r, or we return adns_s_nomemory.
18 *
19 * The query datagram should already have been assembled; memory for it
20 * is taken over by this routine whether it succeeds or fails.
21 *
22 * If failstat is nonzero then if we are successful in creating the query
23 * it is immediately failed with code failstat (but _submit still succeds).
24 *
25 * ctx is copied byte-for-byte into the query.
26 */
27 adns_query qu;
28 adns_status stat;
29 int ol, id, r;
30 struct timeval now;
31 const typeinfo *typei;
6f17710a 32 adns_query qu;
8312a1c2 33
965c9782 34 id= ads->nextid++;
35
36 qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory;
37 qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory;
38
d05cc330 39 qu->state= query_udp;
965c9782 40 qu->back= qu->next= qu->parent= 0;
d05cc330 41 LIST_INIT(qu->children);
6f17710a 42 qu->siblings.next= qu->siblings.back= 0;
965c9782 43 qu->allocations= 0;
44 qu->interim_allocd= 0;
45 qu->perm_used= 0;
46
47 qu->typei= adns__findtype(type);
48 qu->query_dgram= query_dgram;
49 qu->query_dglen= query_dglen;
50 adns__vbuf_init(&qu->vb);
51
52 qu->cname_dgram= 0;
53 qu->cname_dglen= qu->cname_begin= 0;
54
d05cc330 55 qu->id= id;
6f17710a 56 qu->flags= flags;
6f17710a 57 qu->udpretries= 0;
d05cc330 58 qu->udpnextserver= 0;
59 qu->udpsent= qu->tcpfailed= 0;
60 timerclear(&qu->timeout);
61 memcpy(&qu->context,ctx,sizeof(qu->context));
6f17710a 62 memcpy(qu->owner,owner,ol); qu->owner[ol]= 0;
6f17710a 63
965c9782 64 qu->answer->status= adns_s_ok;
65 qu->answer->cname= 0;
66 qu->answer->type= type;
67 qu->answer->nrrs= 0;
68 qu->answer->rrs= 0;
6f17710a 69
6f17710a 70 *query_r= qu;
965c9782 71
72 if (qu->typei) {
73 qu->answer->rrsz= qu->rrsz;
74 } else {
75 qu->answer->rrsz= -1;
76 failstat= adns_s_notimplemented;
77 }
78 if (failstat) {
79 adns__query_fail(ads,qu,failstat);
80 return;
81 }
82
83 adns__query_udp(ads,qu,now);
84 adns__autosys(ads,now);
85
6f17710a 86 return 0;
965c9782 87
88 x_freequ_nomemory:
89 free(qu);
90 x_nomemory:
91 free(query_dgram);
92 return adns_s_nomemory;
6f17710a 93}
94
95int adns_submit(adns_state ads,
96 const char *owner,
97 adns_rrtype type,
98 adns_queryflags flags,
99 void *context,
100 adns_query *query_r) {
d05cc330 101 qcontext ctx;
6f17710a 102
d05cc330 103 ctx.ext= context;
96e79df5 104 r= gettimeofday(&now,0); if (r) return errno;
105
6f17710a 106 ol= strlen(owner);
5c596e4d 107 if (ol<=1 || ol>DNS_MAXDOMAIN+1)
108 return failsubmit(ads,context,query_r,flags,id,adns_s_invaliddomain);
d05cc330 109 if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
6f17710a 110
5c596e4d 111 stat= adns__mkquery(ads,owner,ol,id,typei,flags);
112 if (stat) return failsubmit(ads,context,query_r,flags,id,stat);
8312a1c2 113
965c9782 114 adns__internal_submit(ads,type,flags,now,query_r
6f17710a 115}
116
117int adns_synchronous(adns_state ads,
118 const char *owner,
119 adns_rrtype type,
120 adns_queryflags flags,
121 adns_answer **answer_r) {
122 adns_query qu;
123 int r;
124
125 r= adns_submit(ads,owner,type,flags,0,&qu);
126 if (r) return r;
127
128 do {
129 r= adns_wait(ads,&qu,answer_r,0);
130 } while (r==EINTR);
131 if (r) adns_cancel(ads,qu);
132 return r;
133}
134
135void adns_cancel(adns_state ads, adns_query query) {
de8b18da 136 abort(); /* fixme */
6f17710a 137}
965c9782 138
139void *adns__alloc_interim(adns_state ads, adns_query qu, size_t sz) {
140 allocnode *an;
141
142 sz= MEM_ROUND(sz);
143 an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
144 if (!an) {
145 adns__query_fail(ads,qu,adns_s_nolocalmem);
146 return 0;
147 }
148 qu->permalloclen += sz;
149 an->next= qu->allocations;
150 qu->allocations= an;
151 return (byte*)an + MEM_ROUND(sizeof(*an));
152}