656b2da9 |
1 | /**/ |
2 | |
4353a5c4 |
3 | #include <stdlib.h> |
4 | #include <unistd.h> |
5 | #include <errno.h> |
6 | |
7 | #include <sys/time.h> |
8 | |
9 | #include "internal.h" |
656b2da9 |
10 | |
11 | static adns_query allocquery(adns_state ads, const char *owner, int ol, |
4353a5c4 |
12 | int id, adns_rrtype type, |
13 | adns_queryflags flags, const qcontext *ctx) { |
14 | /* Query message used is the one assembled in ads->rqbuf */ |
656b2da9 |
15 | adns_query qu; |
656b2da9 |
16 | |
4353a5c4 |
17 | qu= malloc(sizeof(*qu)+ol+1+ads->rqbuf.used); if (!qu) return 0; |
18 | qu->state= query_udp; |
656b2da9 |
19 | qu->next= qu->back= qu->parent= 0; |
4353a5c4 |
20 | LIST_INIT(qu->children); |
656b2da9 |
21 | qu->siblings.next= qu->siblings.back= 0; |
656b2da9 |
22 | qu->type= type; |
4353a5c4 |
23 | adns__vbuf_init(&qu->answer); |
24 | qu->id= id; |
656b2da9 |
25 | qu->flags= flags; |
656b2da9 |
26 | qu->udpretries= 0; |
4353a5c4 |
27 | qu->udpnextserver= 0; |
28 | qu->udpsent= qu->tcpfailed= 0; |
29 | timerclear(&qu->timeout); |
30 | memcpy(&qu->context,ctx,sizeof(qu->context)); |
656b2da9 |
31 | memcpy(qu->owner,owner,ol); qu->owner[ol]= 0; |
4353a5c4 |
32 | qu->querymsg= qu->owner+ol+1; |
33 | memcpy(qu->owner+ol+1,ads->rqbuf.buf,ads->rqbuf.used); |
34 | qu->querylen= ads->rqbuf.used; |
656b2da9 |
35 | return qu; |
36 | } |
37 | |
4353a5c4 |
38 | static int failsubmit(adns_state ads, const qcontext *ctx, adns_query *query_r, |
656b2da9 |
39 | adns_rrtype type, adns_queryflags flags, |
40 | int id, adns_status stat) { |
41 | adns_query qu; |
42 | |
4353a5c4 |
43 | ads->rqbuf.used= 0; |
44 | qu= allocquery(ads,0,0,id,type,flags,ctx); if (!qu) return errno; |
45 | adns__query_fail(ads,qu,stat); |
656b2da9 |
46 | *query_r= qu; |
47 | return 0; |
48 | } |
49 | |
50 | int adns_submit(adns_state ads, |
51 | const char *owner, |
52 | adns_rrtype type, |
53 | adns_queryflags flags, |
54 | void *context, |
55 | adns_query *query_r) { |
56 | adns_query qu; |
57 | adns_status stat; |
4353a5c4 |
58 | int ol, id, r; |
59 | qcontext ctx; |
60 | struct timeval now; |
656b2da9 |
61 | |
4353a5c4 |
62 | ctx.ext= context; |
656b2da9 |
63 | id= ads->nextid++; |
64 | |
ddfda861 |
65 | r= gettimeofday(&now,0); if (r) return errno; |
66 | |
656b2da9 |
67 | ol= strlen(owner); |
68 | if (ol<=1 || ol>MAXDNAME+1) |
69 | return failsubmit(ads,context,query_r,type,flags,id,adns_s_invaliddomain); |
4353a5c4 |
70 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; } |
656b2da9 |
71 | |
4353a5c4 |
72 | stat= adns__mkquery(ads,owner,ol,id,type,flags); |
656b2da9 |
73 | if (stat) return failsubmit(ads,context,query_r,type,flags,id,stat); |
74 | |
94436798 |
75 | qu= allocquery(ads,owner,ol,id,type,flags,&ctx); if (!qu) return errno; |
ddfda861 |
76 | adns__query_udp(ads,qu,now); |
4353a5c4 |
77 | adns__autosys(ads,now); |
656b2da9 |
78 | |
79 | *query_r= qu; |
80 | return 0; |
81 | } |
82 | |
83 | int adns_synchronous(adns_state ads, |
84 | const char *owner, |
85 | adns_rrtype type, |
86 | adns_queryflags flags, |
87 | adns_answer **answer_r) { |
88 | adns_query qu; |
89 | int r; |
90 | |
91 | r= adns_submit(ads,owner,type,flags,0,&qu); |
92 | if (r) return r; |
93 | |
94 | do { |
95 | r= adns_wait(ads,&qu,answer_r,0); |
96 | } while (r==EINTR); |
97 | if (r) adns_cancel(ads,qu); |
98 | return r; |
99 | } |
100 | |
101 | void adns_cancel(adns_state ads, adns_query query) { |
94436798 |
102 | abort(); /* fixme */ |
656b2da9 |
103 | } |