ee73b37516d92401695f5c0a6328dba36cfc03e9
[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, 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;
32 adns_query qu;
33
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
39 qu->state= query_udp;
40 qu->back= qu->next= qu->parent= 0;
41 LIST_INIT(qu->children);
42 qu->siblings.next= qu->siblings.back= 0;
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
55 qu->id= id;
56 qu->flags= flags;
57 qu->udpretries= 0;
58 qu->udpnextserver= 0;
59 qu->udpsent= qu->tcpfailed= 0;
60 timerclear(&qu->timeout);
61 memcpy(&qu->context,ctx,sizeof(qu->context));
62 memcpy(qu->owner,owner,ol); qu->owner[ol]= 0;
63
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;
69
70 *query_r= qu;
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
86 return 0;
87
88 x_freequ_nomemory:
89 free(qu);
90 x_nomemory:
91 free(query_dgram);
92 return adns_s_nomemory;
93 }
94
95 int 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) {
101 qcontext ctx;
102
103 ctx.ext= context;
104 r= gettimeofday(&now,0); if (r) return errno;
105
106 ol= strlen(owner);
107 if (ol<=1 || ol>DNS_MAXDOMAIN+1)
108 return failsubmit(ads,context,query_r,flags,id,adns_s_invaliddomain);
109 if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
110
111 stat= adns__mkquery(ads,owner,ol,id,typei,flags);
112 if (stat) return failsubmit(ads,context,query_r,flags,id,stat);
113
114 adns__internal_submit(ads,type,flags,now,query_r
115 }
116
117 int 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
135 void adns_cancel(adns_state ads, adns_query query) {
136 abort(); /* fixme */
137 }
138
139 void *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 }