| 1 | /**/ |
| 2 | |
| 3 | #include "internal.h" |
| 4 | |
| 5 | adns_status adns__mkquery(adns_state ads, const char *owner, int ol, int id, |
| 6 | adns_rrtype type, adns_queryflags flags, int *qml_r) { |
| 7 | int ll, c, nlabs, qbufreq; |
| 8 | unsigned char label[255], *nqbuf; |
| 9 | const char *p, *pe; |
| 10 | |
| 11 | #define MKQUERY_ADDB(b) *nqbuf++= (b) |
| 12 | #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff)) |
| 13 | |
| 14 | qbufreq= 12+strlen(owner)+3; |
| 15 | if (ads->qbufavail < qbufreq) { |
| 16 | nqbuf= realloc(ads->qbuf,qbufreq); |
| 17 | if (!nqbuf) return adns_s_nolocalmem; |
| 18 | ads->qbuf= nqbuf; ads->qbufavail= qbufreq; |
| 19 | } |
| 20 | nqbuf= ads->qbuf; |
| 21 | |
| 22 | MKQUERY_ADDW(id); |
| 23 | MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */ |
| 24 | MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */ |
| 25 | MKQUERY_ADDW(1); /* QDCOUNT=1 */ |
| 26 | MKQUERY_ADDW(0); /* ANCOUNT=0 */ |
| 27 | MKQUERY_ADDW(0); /* NSCOUNT=0 */ |
| 28 | MKQUERY_ADDW(0); /* ARCOUNT=0 */ |
| 29 | p= owner; pe= owner+ol; |
| 30 | nlabs= 0; |
| 31 | if (!*p) return adns_s_invaliddomain; |
| 32 | do { |
| 33 | ll= 0; |
| 34 | while (p!=pe && (c= *p++)!='.') { |
| 35 | if (c=='\\') { |
| 36 | if (!(flags & adns_f_anyquote)) return adns_s_invaliddomain; |
| 37 | if (ctype_digit(p[0])) { |
| 38 | if (ctype_digit(p[1]) && ctype_digit(p[2])) { |
| 39 | c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0'); |
| 40 | if (c >= 256) return adns_s_invaliddomain; |
| 41 | } else { |
| 42 | return adns_s_invaliddomain; |
| 43 | } |
| 44 | } else if (!(c= *p++)) { |
| 45 | return adns_s_invaliddomain; |
| 46 | } |
| 47 | } |
| 48 | if (!(flags & adns_f_anyquote)) { |
| 49 | if (ctype_digit(c) || c == '-') { |
| 50 | if (!ll) return adns_s_invaliddomain; |
| 51 | } else if ((c < 'a' || c > 'z') && (c < 'A' && c > 'Z')) { |
| 52 | return adns_s_invaliddomain; |
| 53 | } |
| 54 | } |
| 55 | if (ll == sizeof(label)) return adns_s_invaliddomain; |
| 56 | label[ll++]= c; |
| 57 | } |
| 58 | if (!ll) return adns_s_invaliddomain; |
| 59 | if (nlabs++ > 63) return adns_s_invaliddomain; |
| 60 | MKQUERY_ADDB(ll); |
| 61 | memcpy(nqbuf,label,ll); nqbuf+= ll; |
| 62 | } while (p!=pe); |
| 63 | |
| 64 | MKQUERY_ADDB(0); |
| 65 | MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */ |
| 66 | MKQUERY_ADDW(1); /* QCLASS=IN */ |
| 67 | |
| 68 | *qml_r= nqbuf - ads->qbuf; |
| 69 | |
| 70 | return adns_s_ok; |
| 71 | } |
| 72 | |
| 73 | static void quproc_tosend_tcp(adns_state ads, adns_query qu, struct timeval now) { |
| 74 | unsigned char length[2]; |
| 75 | struct iovec iov[2]; |
| 76 | |
| 77 | length[0]= (qu->querylen&0x0ff00U) >>8; |
| 78 | length[1]= (qu->querylen&0x0ff); |
| 79 | |
| 80 | adns__tcp_tryconnect(ads); |
| 81 | /* fixme: try sending queries as soon as server comes up */ |
| 82 | /* fixme: use vbuf_ensure, and preallocate buffer space */ |
| 83 | if (ads->tcpstate != server_ok) return; |
| 84 | |
| 85 | DLIST_UNLINK(ads->tosend,qu); |
| 86 | timevaladd(&now,TCPMS); |
| 87 | qu->timeout= now; |
| 88 | qu->senttcpserver= ads->tcpserver; |
| 89 | DLIST_LINKTAIL(ads->timew,qu); |
| 90 | |
| 91 | if (ads->opbufused) { |
| 92 | r= 0; |
| 93 | } else { |
| 94 | iov[0].iovbase= length; |
| 95 | iov[0].iov_len= 2; |
| 96 | iov[1].iovbase= qu->querymsg; |
| 97 | iov[1].iov_len= qu->querylen; |
| 98 | r= writev(ads->tcpsocket,iov,2); |
| 99 | if (r < 0) { |
| 100 | if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC || |
| 101 | errno == ENOBUFS || errno == ENOMEM)) { |
| 102 | adns__tcp_broken(ads,"write",strerror(errno)); |
| 103 | return; |
| 104 | } |
| 105 | r= 0; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (r < qu->querylen+2) { |
| 110 | newopbufused= qu->opbufused + qu->querylen + 2 - r; |
| 111 | if (newopbufused > ads->opbufavail) { |
| 112 | newopbufavail= ads->newopbufused<<1; |
| 113 | newopbuf= realloc(newopbufavail); |
| 114 | if (!newopbuf) { |
| 115 | DLIST_UNLINK(ads->timew,qu); |
| 116 | query_fail(ads,qu,adns_s_nolocalmem); |
| 117 | return; |
| 118 | } |
| 119 | ads->opbuf= newopbuf; |
| 120 | ads->opbufavail= newopbufavail; |
| 121 | } |
| 122 | if (r<2) { |
| 123 | memcpy(ads->opbuf+ads->opbufused,length+r,2-r); |
| 124 | ads->opbufused += (2-r); |
| 125 | r= 0; |
| 126 | } else { |
| 127 | r -= 2; |
| 128 | } |
| 129 | memcpy(ads->opbuf+ads->opbufused,qu->querymsg+r,qu->querylen-r); |
| 130 | ads->opbufused= newopbufused; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void adns__quproc_tosend(adns_state ads, adns_query qu, struct timeval now) { |
| 135 | /* Query must be on the `tosend' queue, and we guarantee to remove it. |
| 136 | */ |
| 137 | struct sockaddr_in servaddr; |
| 138 | int serv; |
| 139 | |
| 140 | if (qu->nextudpserver == -1) { quproc_tosend_tcp(ads,qu,now); return; } |
| 141 | if (qu->querylen > UDPMAXDGRAM) { |
| 142 | qu->nextudpserver= -1; |
| 143 | quproc_tosend_tcp(ads,qu,now); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | if (qu->udpretries >= UDPMAXRETRIES) { |
| 148 | DLIST_UNLINK(ads->tosend,qu); |
| 149 | query_fail(ads,qu,adns_s_notresponding); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | serv= qu->nextudpserver; |
| 154 | memset(&servaddr,0,sizeof(servaddr)); |
| 155 | servaddr.sin_family= AF_INET; |
| 156 | servaddr.sin_addr= ads->servers[serv].addr; |
| 157 | servaddr.sin_port= htons(NSPORT); |
| 158 | |
| 159 | r= sendto(ads->udpsocket,qu->querymsg,qu->querylen,0,&servaddr,sizeof(servaddr)); |
| 160 | if (r<0 && errno == EMSGSIZE) { |
| 161 | qu->nextudpserver= -1; |
| 162 | quproc_tosend_tcp(ads,qu,now); return; |
| 163 | } |
| 164 | if (r<0) warn("sendto %s failed: %s",inet_ntoa(servaddr.sin_addr),strerror(errno)); |
| 165 | |
| 166 | DLIST_UNLINK(ads->tosend,qu); |
| 167 | timevaladd(&now,UDPRETRYMS); |
| 168 | qu->timeout= now; |
| 169 | qu->sentudp |= (1<<serv); |
| 170 | qu->nextudpserver= (serv+1)%ads->nservers; |
| 171 | qu->udpretries++; |
| 172 | DLIST_LINKTAIL(ads->timew,qu); |
| 173 | } |
| 174 | |
| 175 | void adns__query_fail(adns_state ads, adns_query qu, adns_status stat) { |
| 176 | adns_answer *ans; |
| 177 | |
| 178 | ans= qu->answer; |
| 179 | if (!ans) ans= malloc(sizeof(*qu->answer)); |
| 180 | if (ans) { |
| 181 | ans->status= stat; |
| 182 | ans->cname= 0; |
| 183 | ans->type= qu->type; |
| 184 | ans->nrrs= 0; |
| 185 | } |
| 186 | qu->answer= ans; |
| 187 | qu->id= -1; |
| 188 | LIST_LINK_TAIL(ads->output,qu); |
| 189 | } |