| 1 | /**/ |
| 2 | |
| 3 | #include <errno.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #include <sys/uio.h> |
| 8 | |
| 9 | #include "internal.h" |
| 10 | |
| 11 | adns_status adns__mkquery(adns_state ads, vbuf *vb, |
| 12 | const char *owner, int ol, int *id_r, |
| 13 | const typeinfo *typei, adns_queryflags flags) { |
| 14 | int ll, c, nlabs, id; |
| 15 | byte label[255], *rqp; |
| 16 | const char *p, *pe; |
| 17 | |
| 18 | #define MKQUERY_ADDB(b) *rqp++= (b) |
| 19 | #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff)) |
| 20 | |
| 21 | vb->used= 0; |
| 22 | if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+strlen(owner)+1+5)) |
| 23 | return adns_s_nolocalmem; |
| 24 | rqp= vb->buf; |
| 25 | |
| 26 | *id_r= id= (ads->nextid++) & 0x0ffff; |
| 27 | |
| 28 | MKQUERY_ADDW(id); |
| 29 | MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */ |
| 30 | MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */ |
| 31 | MKQUERY_ADDW(1); /* QDCOUNT=1 */ |
| 32 | MKQUERY_ADDW(0); /* ANCOUNT=0 */ |
| 33 | MKQUERY_ADDW(0); /* NSCOUNT=0 */ |
| 34 | MKQUERY_ADDW(0); /* ARCOUNT=0 */ |
| 35 | p= owner; pe= owner+ol; |
| 36 | nlabs= 0; |
| 37 | if (!*p) return adns_s_invaliddomain; |
| 38 | do { |
| 39 | ll= 0; |
| 40 | while (p!=pe && (c= *p++)!='.') { |
| 41 | if (c=='\\') { |
| 42 | if (!(flags & adns_qf_anyquote)) return adns_s_invaliddomain; |
| 43 | if (ctype_digit(p[0])) { |
| 44 | if (ctype_digit(p[1]) && ctype_digit(p[2])) { |
| 45 | c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0'); |
| 46 | if (c >= 256) return adns_s_invaliddomain; |
| 47 | } else { |
| 48 | return adns_s_invaliddomain; |
| 49 | } |
| 50 | } else if (!(c= *p++)) { |
| 51 | return adns_s_invaliddomain; |
| 52 | } |
| 53 | } |
| 54 | if (!(flags & adns_qf_anyquote)) { |
| 55 | if (ctype_digit(c) || c == '-') { |
| 56 | if (!ll) return adns_s_invaliddomain; |
| 57 | } else if (!ctype_alpha(c)) { |
| 58 | return adns_s_invaliddomain; |
| 59 | } |
| 60 | } |
| 61 | if (ll == sizeof(label)) return adns_s_invaliddomain; |
| 62 | label[ll++]= c; |
| 63 | } |
| 64 | if (!ll) return adns_s_invaliddomain; |
| 65 | if (nlabs++ > 63) return adns_s_invaliddomain; |
| 66 | MKQUERY_ADDB(ll); |
| 67 | memcpy(rqp,label,ll); rqp+= ll; |
| 68 | } while (p!=pe); |
| 69 | |
| 70 | MKQUERY_ADDB(0); |
| 71 | MKQUERY_ADDW(typei->type & adns__rrt_typemask); /* QTYPE */ |
| 72 | MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */ |
| 73 | |
| 74 | vb->used= rqp - vb->buf; |
| 75 | assert(vb->used <= vb->avail); |
| 76 | |
| 77 | return adns_s_ok; |
| 78 | } |
| 79 | |
| 80 | void adns__query_tcp(adns_state ads, adns_query qu, struct timeval now) { |
| 81 | /* Query must be in state tcpwait/timew; it will be moved to a new state |
| 82 | * if possible and no further processing can be done on it for now. |
| 83 | * (Resulting state is one of tcpwait/timew (if server not connected), |
| 84 | * tcpsent/timew, child/childw or done/output.) |
| 85 | * |
| 86 | * adns__tcp_tryconnect should already have been called - _tcp |
| 87 | * will only use an existing connection (if there is one), which it |
| 88 | * may break. If the conn list lost then the caller is responsible for any |
| 89 | * reestablishment and retry. |
| 90 | */ |
| 91 | byte length[2]; |
| 92 | struct iovec iov[2]; |
| 93 | int wr, r; |
| 94 | |
| 95 | if (ads->tcpstate != server_ok) return; |
| 96 | |
| 97 | length[0]= (qu->query_dglen&0x0ff00U) >>8; |
| 98 | length[1]= (qu->query_dglen&0x0ff); |
| 99 | |
| 100 | if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return; |
| 101 | |
| 102 | timevaladd(&now,TCPMS); |
| 103 | qu->timeout= now; |
| 104 | qu->state= query_tcpsent; |
| 105 | LIST_LINK_TAIL(ads->timew,qu); |
| 106 | |
| 107 | if (ads->tcpsend.used) { |
| 108 | wr= 0; |
| 109 | } else { |
| 110 | iov[0].iov_base= length; |
| 111 | iov[0].iov_len= 2; |
| 112 | iov[1].iov_base= qu->query_dgram; |
| 113 | iov[1].iov_len= qu->query_dglen; |
| 114 | wr= writev(ads->tcpsocket,iov,2); |
| 115 | if (wr < 0) { |
| 116 | if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC || |
| 117 | errno == ENOBUFS || errno == ENOMEM)) { |
| 118 | adns__tcp_broken(ads,"write",strerror(errno)); |
| 119 | return; |
| 120 | } |
| 121 | wr= 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (wr<2) { |
| 126 | r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r); |
| 127 | wr= 0; |
| 128 | } else { |
| 129 | wr-= 2; |
| 130 | } |
| 131 | if (wr<qu->query_dglen) { |
| 132 | r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static void query_usetcp(adns_state ads, adns_query qu, struct timeval now) { |
| 137 | timevaladd(&now,TCPMS); |
| 138 | qu->timeout= now; |
| 139 | qu->state= query_tcpwait; |
| 140 | LIST_LINK_TAIL(ads->timew,qu); |
| 141 | adns__query_tcp(ads,qu,now); |
| 142 | adns__tcp_tryconnect(ads,now); |
| 143 | } |
| 144 | |
| 145 | void adns__query_udp(adns_state ads, adns_query qu, struct timeval now) { |
| 146 | /* Query must be in state udp/NONE; it will be moved to a new state, |
| 147 | * and no further processing can be done on it for now. |
| 148 | * (Resulting state is one of udp/timew, tcpwait/timew (if server not connected), |
| 149 | * tcpsent/timew, child/childw or done/output.) |
| 150 | */ |
| 151 | struct sockaddr_in servaddr; |
| 152 | int serv, r; |
| 153 | |
| 154 | assert(qu->state == query_udp); |
| 155 | if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) { |
| 156 | query_usetcp(ads,qu,now); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (qu->udpretries >= UDPMAXRETRIES) { |
| 161 | adns__query_fail(ads,qu,adns_s_timeout); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | serv= qu->udpnextserver; |
| 166 | memset(&servaddr,0,sizeof(servaddr)); |
| 167 | servaddr.sin_family= AF_INET; |
| 168 | servaddr.sin_addr= ads->servers[serv].addr; |
| 169 | servaddr.sin_port= htons(DNS_PORT); |
| 170 | |
| 171 | r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,&servaddr,sizeof(servaddr)); |
| 172 | if (r<0 && errno == EMSGSIZE) { query_usetcp(ads,qu,now); return; } |
| 173 | if (r<0) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno)); |
| 174 | |
| 175 | timevaladd(&now,UDPRETRYMS); |
| 176 | qu->timeout= now; |
| 177 | qu->udpsent |= (1<<serv); |
| 178 | qu->udpnextserver= (serv+1)%ads->nservers; |
| 179 | qu->udpretries++; |
| 180 | LIST_LINK_TAIL(ads->timew,qu); |
| 181 | } |
| 182 | |
| 183 | static void adns__query_done(adns_state ads, adns_query qu) { |
| 184 | adns_answer *ans; |
| 185 | allocnode *an, *ann; |
| 186 | int i; |
| 187 | |
| 188 | qu->answer= ans= realloc(qu->answer, |
| 189 | MEM_ROUND(MEM_ROUND(sizeof(*ans)) + |
| 190 | qu->interim_allocd)); |
| 191 | qu->final_allocspace= (byte*)qu->answer + MEM_ROUND(sizeof(*ans)); |
| 192 | |
| 193 | adns__makefinal_str(qu,&ans->cname); |
| 194 | if (ans->nrrs) { |
| 195 | adns__makefinal_block(qu,&ans->rrs.untyped,ans->rrsz*ans->nrrs); |
| 196 | for (i=0; i<ans->nrrs; i++) |
| 197 | qu->typei->makefinal(ads,qu,ans->rrs.bytes+ans->rrsz*i); |
| 198 | } |
| 199 | |
| 200 | for (an= qu->allocations; an; an= ann) { ann= an->next; free(an); } |
| 201 | |
| 202 | adns__vbuf_free(&qu->vb); |
| 203 | |
| 204 | qu->id= -1; |
| 205 | LIST_LINK_TAIL(ads->output,qu); |
| 206 | } |
| 207 | |
| 208 | void adns__reset_cnameonly(adns_state ads, adns_query qu) { |
| 209 | assert(qu->final_allocspace); |
| 210 | qu->answer->nrrs= 0; |
| 211 | qu->answer->rrs= 0; |
| 212 | qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0; |
| 213 | } |
| 214 | |
| 215 | void adns__query_fail(adns_state ads, adns_query qu, adns_status stat) { |
| 216 | adns__reset_cnameonly(ads,qu); |
| 217 | qu->answer->status= stat; |
| 218 | adns__query_done(ads,qu); |
| 219 | } |