7 * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 adns_status adns__mkquery(adns_state ads, vbuf *vb,
27 const char *owner, int ol, int *id_r,
28 const typeinfo *typei, adns_queryflags flags) {
30 byte label[255], *rqp;
33 #define MKQUERY_ADDB(b) *rqp++= (b)
34 #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
37 if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+strlen(owner)+1+5))
38 return adns_s_nolocalmem;
41 *id_r= id= (ads->nextid++) & 0x0ffff;
44 MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
45 MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
46 MKQUERY_ADDW(1); /* QDCOUNT=1 */
47 MKQUERY_ADDW(0); /* ANCOUNT=0 */
48 MKQUERY_ADDW(0); /* NSCOUNT=0 */
49 MKQUERY_ADDW(0); /* ARCOUNT=0 */
50 p= owner; pe= owner+ol;
52 if (!*p) return adns_s_invaliddomain;
55 while (p!=pe && (c= *p++)!='.') {
57 if (!(flags & adns_qf_anyquote)) return adns_s_invaliddomain;
58 if (ctype_digit(p[0])) {
59 if (ctype_digit(p[1]) && ctype_digit(p[2])) {
60 c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0');
61 if (c >= 256) return adns_s_invaliddomain;
63 return adns_s_invaliddomain;
65 } else if (!(c= *p++)) {
66 return adns_s_invaliddomain;
69 if (!(flags & adns_qf_anyquote)) {
70 if (ctype_digit(c) || c == '-') {
71 if (!ll) return adns_s_invaliddomain;
72 } else if (!ctype_alpha(c)) {
73 return adns_s_invaliddomain;
76 if (ll == sizeof(label)) return adns_s_invaliddomain;
79 if (!ll) return adns_s_invaliddomain;
80 if (nlabs++ > 63) return adns_s_invaliddomain;
82 memcpy(rqp,label,ll); rqp+= ll;
86 MKQUERY_ADDW(typei->type & adns__rrt_typemask); /* QTYPE */
87 MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */
89 vb->used= rqp - vb->buf;
90 assert(vb->used <= vb->avail);
95 void adns__query_tcp(adns_state ads, adns_query qu, struct timeval now) {
100 if (ads->tcpstate != server_ok) return;
102 length[0]= (qu->query_dglen&0x0ff00U) >>8;
103 length[1]= (qu->query_dglen&0x0ff);
105 if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return;
107 timevaladd(&now,TCPMS);
109 qu->state= query_tcpsent;
110 LIST_LINK_TAIL(ads->timew,qu);
112 if (ads->tcpsend.used) {
115 iov[0].iov_base= length;
117 iov[1].iov_base= qu->query_dgram;
118 iov[1].iov_len= qu->query_dglen;
119 wr= writev(ads->tcpsocket,iov,2);
121 if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
122 errno == ENOBUFS || errno == ENOMEM)) {
123 adns__tcp_broken(ads,"write",strerror(errno));
131 r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
136 if (wr<qu->query_dglen) {
137 r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r);
141 static void query_usetcp(adns_state ads, adns_query qu, struct timeval now) {
142 timevaladd(&now,TCPMS);
144 qu->state= query_tcpwait;
145 LIST_LINK_TAIL(ads->timew,qu);
146 adns__query_tcp(ads,qu,now);
147 adns__tcp_tryconnect(ads,now);
150 void adns__query_udp(adns_state ads, adns_query qu, struct timeval now) {
151 struct sockaddr_in servaddr;
154 assert(qu->state == query_udp);
155 if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
156 query_usetcp(ads,qu,now);
160 if (qu->udpretries >= UDPMAXRETRIES) {
161 adns__query_fail(ads,qu,adns_s_timeout);
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);
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));
175 timevaladd(&now,UDPRETRYMS);
177 qu->udpsent |= (1<<serv);
178 qu->udpnextserver= (serv+1)%ads->nservers;
180 LIST_LINK_TAIL(ads->timew,qu);