11 static adns_query
allocquery(adns_state ads
, const char *owner
, int ol
,
12 int id
, const typeinfo
*typei
,
13 adns_queryflags flags
, const qcontext
*ctx
) {
14 /* Query message used is the one assembled in ads->rqbuf */
18 qu
= malloc(sizeof(*qu
)+ol
+1+ads
->rqbuf
.used
); if (!qu
) return 0;
20 adns__vbuf_init(&qu
->ans
);
21 if (!adns__vbuf_ensure(&qu
->ans
,sizeof(adns_answer
))) { free(qu
); return 0; }
22 ans
= (adns_answer
*)qu
->ans
.buf
;
23 ans
->status
= adns_s_ok
;
24 ans
->type
= qu
->typei
->type
;
29 qu
->next
= qu
->back
= qu
->parent
= 0;
30 LIST_INIT(qu
->children
);
31 qu
->siblings
.next
= qu
->siblings
.back
= 0;
37 qu
->udpsent
= qu
->tcpfailed
= 0;
38 timerclear(&qu
->timeout
);
39 memcpy(&qu
->context
,ctx
,sizeof(qu
->context
));
40 memcpy(qu
->owner
,owner
,ol
); qu
->owner
[ol
]= 0;
41 qu
->querymsg
= qu
->owner
+ol
+1;
42 memcpy(qu
->owner
+ol
+1,ads
->rqbuf
.buf
,ads
->rqbuf
.used
);
43 qu
->querylen
= ads
->rqbuf
.used
;
48 static int failsubmit(adns_state ads
, const qcontext
*ctx
, adns_query
*query_r
,
49 adns_queryflags flags
, int id
, adns_status stat
) {
53 qu
= allocquery(ads
,0,0,id
,0,flags
,ctx
); if (!qu
) return errno
;
54 adns__query_fail(ads
,qu
,stat
);
59 int adns_submit(adns_state ads
,
62 adns_queryflags flags
,
64 adns_query
*query_r
) {
70 const typeinfo
*typei
;
75 r
= gettimeofday(&now
,0); if (r
) return errno
;
77 typei
= adns__findtype(type
);
78 if (!typei
) return failsubmit(ads
,context
,query_r
,flags
,id
,adns_s_notimplemented
);
81 if (ol
<=1 || ol
>DNS_MAXDOMAIN
+1)
82 return failsubmit(ads
,context
,query_r
,flags
,id
,adns_s_invaliddomain
);
83 if (owner
[ol
-1]=='.' && owner
[ol
-2]!='\\') { flags
&= ~adns_qf_search
; ol
--; }
85 stat
= adns__mkquery(ads
,owner
,ol
,id
,typei
,flags
);
86 if (stat
) return failsubmit(ads
,context
,query_r
,flags
,id
,stat
);
88 qu
= allocquery(ads
,owner
,ol
,id
,typei
,flags
,&ctx
); if (!qu
) return errno
;
89 adns__query_udp(ads
,qu
,now
);
90 adns__autosys(ads
,now
);
96 int adns_synchronous(adns_state ads
,
99 adns_queryflags flags
,
100 adns_answer
**answer_r
) {
104 r
= adns_submit(ads
,owner
,type
,flags
,0,&qu
);
108 r
= adns_wait(ads
,&qu
,answer_r
,0);
110 if (r
) adns_cancel(ads
,qu
);
114 void adns_cancel(adns_state ads
, adns_query query
) {