656b2da9 |
1 | /**/ |
2 | |
3 | #include "adns-internal.h" |
4 | |
5 | static adns_query allocquery(adns_state ads, const char *owner, int ol, |
6 | int qml, int id, adns_rrtype type, |
7 | adns_queryflags flags, void *context) { |
8 | adns_query qu; |
9 | unsigned char *qm; |
10 | |
11 | qu= malloc(sizeof(*qu)+ol+1+qml); if (!qu) return 0; |
12 | qu->next= qu->back= qu->parent= 0; |
13 | qu->children.head= qu->children.tail= 0; |
14 | qu->siblings.next= qu->siblings.back= 0; |
15 | qu->id= id; |
16 | qu->type= type; |
17 | qu->answer= 0; |
18 | qu->flags= flags; |
19 | qu->context= context; |
20 | qu->udpretries= 0; |
21 | qu->sentudp= qu->senttcp= 0; |
22 | qu->nextserver= 0; |
23 | memcpy(qu->owner,owner,ol); qu->owner[ol]= 0; |
24 | qu->querymsg= qm= qu->owner+ol+1; |
25 | memcpy(qm,ads->qbuf,qml); |
26 | qu->querylen= qml; |
27 | return qu; |
28 | } |
29 | |
30 | static int failsubmit(adns_state ads, void *context, adns_query *query_r, |
31 | adns_rrtype type, adns_queryflags flags, |
32 | int id, adns_status stat) { |
33 | adns_query qu; |
34 | |
35 | qu= allocquery(ads,0,0,0,id,type,flags,context); if (!qu) return errno; |
36 | query_fail(ads,qu,stat); |
37 | *query_r= qu; |
38 | return 0; |
39 | } |
40 | |
41 | int adns_submit(adns_state ads, |
42 | const char *owner, |
43 | adns_rrtype type, |
44 | adns_queryflags flags, |
45 | void *context, |
46 | adns_query *query_r) { |
47 | adns_query qu; |
48 | adns_status stat; |
49 | int ol, id, qml; |
50 | |
51 | id= ads->nextid++; |
52 | |
53 | ol= strlen(owner); |
54 | if (ol<=1 || ol>MAXDNAME+1) |
55 | return failsubmit(ads,context,query_r,type,flags,id,adns_s_invaliddomain); |
56 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_f_search; ol--; } |
57 | |
4bec51a4 |
58 | stat= adns__mkquery(ads,owner,ol,id,type,flags,&qml); |
656b2da9 |
59 | if (stat) return failsubmit(ads,context,query_r,type,flags,id,stat); |
60 | |
61 | qu= allocquery(ads,owner,ol,qml,id,type,flags,context); if (!qu) return errno; |
62 | if (qu->flags & adns_f_usevc) qu->udpretries= -1; |
63 | LIST_LINK_TAIL(ads->tosend,qu); |
64 | |
65 | r= gettimeofday(&now,0); if (r) return; |
66 | quproc_tosend(ads,qu,now); |
67 | autosys(ads,now); |
68 | |
69 | *query_r= qu; |
70 | return 0; |
71 | } |
72 | |
73 | int adns_synchronous(adns_state ads, |
74 | const char *owner, |
75 | adns_rrtype type, |
76 | adns_queryflags flags, |
77 | adns_answer **answer_r) { |
78 | adns_query qu; |
79 | int r; |
80 | |
81 | r= adns_submit(ads,owner,type,flags,0,&qu); |
82 | if (r) return r; |
83 | |
84 | do { |
85 | r= adns_wait(ads,&qu,answer_r,0); |
86 | } while (r==EINTR); |
87 | if (r) adns_cancel(ads,qu); |
88 | return r; |
89 | } |
90 | |
91 | void adns_cancel(adns_state ads, adns_query query) { |
92 | abort(); /* FIXME */ |
93 | } |