| 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 | 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 */ |
| 15 | adns_query qu; |
| 16 | adns_answer *ans; |
| 17 | |
| 18 | qu= malloc(sizeof(*qu)+ol+1+ads->rqbuf.used); if (!qu) return 0; |
| 19 | |
| 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; |
| 25 | ans->nrrs= 0; |
| 26 | ans->rrs.str= 0; |
| 27 | |
| 28 | qu->state= query_udp; |
| 29 | qu->next= qu->back= qu->parent= 0; |
| 30 | LIST_INIT(qu->children); |
| 31 | qu->siblings.next= qu->siblings.back= 0; |
| 32 | qu->typei= typei; |
| 33 | qu->id= id; |
| 34 | qu->flags= flags; |
| 35 | qu->udpretries= 0; |
| 36 | qu->udpnextserver= 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; |
| 44 | |
| 45 | return qu; |
| 46 | } |
| 47 | |
| 48 | static int failsubmit(adns_state ads, const qcontext *ctx, adns_query *query_r, |
| 49 | adns_queryflags flags, int id, adns_status stat) { |
| 50 | adns_query qu; |
| 51 | |
| 52 | ads->rqbuf.used= 0; |
| 53 | qu= allocquery(ads,0,0,id,0,flags,ctx); if (!qu) return errno; |
| 54 | adns__query_fail(ads,qu,stat); |
| 55 | *query_r= qu; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int adns_submit(adns_state ads, |
| 60 | const char *owner, |
| 61 | adns_rrtype type, |
| 62 | adns_queryflags flags, |
| 63 | void *context, |
| 64 | adns_query *query_r) { |
| 65 | adns_query qu; |
| 66 | adns_status stat; |
| 67 | int ol, id, r; |
| 68 | qcontext ctx; |
| 69 | struct timeval now; |
| 70 | const typeinfo *typei; |
| 71 | |
| 72 | ctx.ext= context; |
| 73 | id= ads->nextid++; |
| 74 | |
| 75 | r= gettimeofday(&now,0); if (r) return errno; |
| 76 | |
| 77 | typei= adns__findtype(type); |
| 78 | if (!typei) return failsubmit(ads,context,query_r,flags,id,adns_s_notimplemented); |
| 79 | |
| 80 | ol= strlen(owner); |
| 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--; } |
| 84 | |
| 85 | stat= adns__mkquery(ads,owner,ol,id,typei,flags); |
| 86 | if (stat) return failsubmit(ads,context,query_r,flags,id,stat); |
| 87 | |
| 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); |
| 91 | |
| 92 | *query_r= qu; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int adns_synchronous(adns_state ads, |
| 97 | const char *owner, |
| 98 | adns_rrtype type, |
| 99 | adns_queryflags flags, |
| 100 | adns_answer **answer_r) { |
| 101 | adns_query qu; |
| 102 | int r; |
| 103 | |
| 104 | r= adns_submit(ads,owner,type,flags,0,&qu); |
| 105 | if (r) return r; |
| 106 | |
| 107 | do { |
| 108 | r= adns_wait(ads,&qu,answer_r,0); |
| 109 | } while (r==EINTR); |
| 110 | if (r) adns_cancel(ads,qu); |
| 111 | return r; |
| 112 | } |
| 113 | |
| 114 | void adns_cancel(adns_state ads, adns_query query) { |
| 115 | abort(); /* fixme */ |
| 116 | } |