1 /*
2  * transmit.c
3  * - construct queries
4  * - send queries
5  */
6 /*
7  *  This file is
8  *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
9  *
10  *  It is part of adns, which is
11  *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
12  *    Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
13  *
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2, or (at your option)
17  *  any later version.
18  *
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software Foundation,
26  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28 
29 #include <errno.h>
30 
31 #include <sys/types.h>
32 #include <sys/uio.h>
33 
34 #include "internal.h"
35 #include "tvarith.h"
36 
37 #define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used)
38 #define MKQUERY_ADDB(b) *rqp++= (b)
39 #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
40 #define MKQUERY_STOP(vb) ((vb)->used= rqp-(vb)->buf)
41 
mkquery_header(adns_state ads,vbuf * vb,int * id_r,int qdlen)42 static adns_status mkquery_header(adns_state ads, vbuf *vb, int *id_r, int qdlen) {
43   int id;
44   byte *rqp;
45 
46   if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+qdlen+4)) return adns_s_nomemory;
47 
48   vb->used= 0;
49   MKQUERY_START(vb);
50 
51   *id_r= id= (ads->nextid++) & 0x0ffff;
52   MKQUERY_ADDW(id);
53   MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
54   MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
55   MKQUERY_ADDW(1); /* QDCOUNT=1 */
56   MKQUERY_ADDW(0); /* ANCOUNT=0 */
57   MKQUERY_ADDW(0); /* NSCOUNT=0 */
58   MKQUERY_ADDW(0); /* ARCOUNT=0 */
59 
60   MKQUERY_STOP(vb);
61 
62   return adns_s_ok;
63 }
64 
mkquery_footer(vbuf * vb,adns_rrtype type)65 static adns_status mkquery_footer(vbuf *vb, adns_rrtype type) {
66   byte *rqp;
67 
68   MKQUERY_START(vb);
69   MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */
70   MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */
71   MKQUERY_STOP(vb);
72   assert(vb->used <= vb->avail);
73 
74   return adns_s_ok;
75 }
76 
adns__mkquery(adns_state ads,vbuf * vb,int * id_r,const char * owner,int ol,const typeinfo * typei,adns_queryflags flags)77 adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
78 			  const char *owner, int ol,
79 			  const typeinfo *typei, adns_queryflags flags) {
80   int ll, c, nbytes;
81   byte label[255], *rqp;
82   const char *p, *pe;
83   adns_status st;
84 
85   st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
86 
87   MKQUERY_START(vb);
88 
89   p= owner; pe= owner+ol;
90   nbytes= 0;
91   while (p!=pe) {
92     ll= 0;
93     while (p!=pe && (c= *p++)!='.') {
94       if (c=='\\') {
95 	if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
96 	if (ctype_digit(p[0])) {
97 	  if (ctype_digit(p[1]) && ctype_digit(p[2])) {
98 	    c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0');
99 	    if (c >= 256) return adns_s_querydomaininvalid;
100 	  } else {
101 	    return adns_s_querydomaininvalid;
102 	  }
103 	} else if (!(c= *p++)) {
104 	  return adns_s_querydomaininvalid;
105 	}
106       }
107       if (!(flags & adns_qf_quoteok_query)) {
108 	if (c == '-') {
109 	  if (!ll) return adns_s_querydomaininvalid;
110 	} else if (!ctype_alpha(c) && !ctype_digit(c)) {
111 	  return adns_s_querydomaininvalid;
112 	}
113       }
114       if (ll == sizeof(label)) return adns_s_querydomaininvalid;
115       label[ll++]= c;
116     }
117     if (!ll) return adns_s_querydomaininvalid;
118     if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong;
119     nbytes+= ll+1;
120     if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
121     MKQUERY_ADDB(ll);
122     memcpy(rqp,label,ll); rqp+= ll;
123   }
124   MKQUERY_ADDB(0);
125 
126   MKQUERY_STOP(vb);
127 
128   st= mkquery_footer(vb,typei->type);
129 
130   return adns_s_ok;
131 }
132 
adns__mkquery_frdgram(adns_state ads,vbuf * vb,int * id_r,const byte * qd_dgram,int qd_dglen,int qd_begin,adns_rrtype type,adns_queryflags flags)133 adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
134 				  const byte *qd_dgram, int qd_dglen, int qd_begin,
135 				  adns_rrtype type, adns_queryflags flags) {
136   byte *rqp;
137   findlabel_state fls;
138   int lablen, labstart;
139   adns_status st;
140 
141   st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
142 
143   MKQUERY_START(vb);
144 
145   adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
146   for (;;) {
147     st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
148     if (!lablen) break;
149     assert(lablen<255);
150     MKQUERY_ADDB(lablen);
151     memcpy(rqp,qd_dgram+labstart,lablen);
152     rqp+= lablen;
153   }
154   MKQUERY_ADDB(0);
155 
156   MKQUERY_STOP(vb);
157 
158   st= mkquery_footer(vb,type);
159 
160   return adns_s_ok;
161 }
162 
adns__querysend_tcp(adns_query qu,struct timeval now)163 void adns__querysend_tcp(adns_query qu, struct timeval now) {
164   byte length[2];
165   struct iovec iov[2];
166   int wr, r;
167   adns_state ads;
168 
169   if (qu->ads->tcpstate != server_ok) return;
170 
171   assert(qu->state == query_tcpw);
172 
173   length[0]= (qu->query_dglen&0x0ff00U) >>8;
174   length[1]= (qu->query_dglen&0x0ff);
175 
176   ads= qu->ads;
177   if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return;
178 
179   qu->retries++;
180 
181   /* Reset idle timeout. */
182   ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
183 
184   if (ads->tcpsend.used) {
185     wr= 0;
186   } else {
187     iov[0].iov_base= length;
188     iov[0].iov_len= 2;
189     iov[1].iov_base= qu->query_dgram;
190     iov[1].iov_len= qu->query_dglen;
191     adns__sigpipe_protect(qu->ads);
192     wr= writev(qu->ads->tcpsocket,iov,2);
193     adns__sigpipe_unprotect(qu->ads);
194     if (wr < 0) {
195       if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
196 	    errno == ENOBUFS || errno == ENOMEM)) {
197 	adns__tcp_broken(ads,"write",strerror(errno));
198 	return;
199       }
200       wr= 0;
201     }
202   }
203 
204   if (wr<2) {
205     r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
206     wr= 0;
207   } else {
208     wr-= 2;
209   }
210   if (wr<qu->query_dglen) {
211     r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r);
212   }
213 }
214 
query_usetcp(adns_query qu,struct timeval now)215 static void query_usetcp(adns_query qu, struct timeval now) {
216   qu->state= query_tcpw;
217   qu->timeout= now;
218   timevaladd(&qu->timeout,TCPWAITMS);
219   LIST_LINK_TAIL(qu->ads->tcpw,qu);
220   adns__querysend_tcp(qu,now);
221   adns__tcp_tryconnect(qu->ads,now);
222 }
223 
adns__query_send(adns_query qu,struct timeval now)224 void adns__query_send(adns_query qu, struct timeval now) {
225   struct sockaddr_in servaddr;
226   int serv, r;
227   adns_state ads;
228 
229   assert(qu->state == query_tosend);
230   if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
231     query_usetcp(qu,now);
232     return;
233   }
234 
235   if (qu->retries >= UDPMAXRETRIES) {
236     adns__query_fail(qu,adns_s_timeout);
237     return;
238   }
239 
240   serv= qu->udpnextserver;
241   memset(&servaddr,0,sizeof(servaddr));
242 
243   ads= qu->ads;
244   servaddr.sin_family= AF_INET;
245   servaddr.sin_addr= ads->servers[serv].addr;
246   servaddr.sin_port= htons(DNS_PORT);
247 
248   r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
249 	    (const struct sockaddr*)&servaddr,sizeof(servaddr));
250   if (r<0 && errno == EMSGSIZE) { qu->retries= 0; query_usetcp(qu,now); return; }
251   if (r<0 && errno != EAGAIN) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
252 
253   qu->timeout= now;
254   timevaladd(&qu->timeout,UDPRETRYMS);
255   qu->udpsent |= (1<<serv);
256   qu->udpnextserver= (serv+1)%ads->nservers;
257   qu->retries++;
258   LIST_LINK_TAIL(ads->udpw,qu);
259 }
260