chiark / gitweb /
Turn query domain parsing upside-down.
[adns] / src / transmit.c
1 /*
2  * transmit.c
3  * - construct queries
4  * - send queries
5  */
6 /*
7  *  This file is part of adns, which is
8  *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
9  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
10  *    Copyright (C) 1991 Massachusetts Institute of Technology
11  *  (See the file INSTALL for full details.)
12  *  
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 2, or (at your option)
16  *  any later version.
17  *  
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *  
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software Foundation,
25  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
26  */
27
28 #include <errno.h>
29
30 #include <sys/types.h>
31 #include <sys/uio.h>
32
33 #include "internal.h"
34 #include "tvarith.h"
35
36 #define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used)
37 #define MKQUERY_ADDB(b) *rqp++= (b)
38 #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
39 #define MKQUERY_STOP(vb) ((vb)->used= rqp-(vb)->buf)
40
41 static adns_status mkquery_header(adns_state ads, vbuf *vb,
42                                   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
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
77 adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
78                           const char *owner, int ol,
79                           const typeinfo *typei, adns_rrtype type,
80                           adns_queryflags flags) {
81   int labelnum, ll, c, nbytes;
82   byte label[255];
83   byte *rqp;
84   const char *p, *pe;
85   adns_status st;
86
87   st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
88   
89   MKQUERY_START(vb);
90
91   p= owner; pe= owner+ol;
92   nbytes= 0;
93   labelnum= 0;
94   while (p!=pe) {
95
96     ll= 0;
97     while (p!=pe && (c= *p++)!='.') {
98       if (c=='\\') {
99         if (ctype_digit(p[0])) {
100           if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid;
101           if (ctype_digit(p[1]) && ctype_digit(p[2])) {
102             c= (*p++ - '0')*100;
103             c += (*p++ - '0')*10;
104             c += (*p++ - '0');
105             if (c >= 256) return adns_s_querydomaininvalid;
106           } else {
107             return adns_s_querydomaininvalid;
108           }
109         } else if (!(c= *p++)) {
110           return adns_s_querydomaininvalid;
111         }
112       }
113       if (ll >= DNS_MAXLABEL) return adns_s_querydomaintoolong;
114       label[ll++]= c;
115     }
116     if (!ll) return adns_s_querydomaininvalid;
117     nbytes+= ll+1;
118     if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
119     MKQUERY_ADDB(ll);
120     memcpy(rqp,label,ll); rqp+= ll;
121   }
122   MKQUERY_ADDB(0);
123
124   MKQUERY_STOP(vb);
125   
126   st= mkquery_footer(vb,type);
127   
128   return adns_s_ok;
129 }
130
131 adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
132                                   const byte *qd_dgram, int qd_dglen,
133                                   int qd_begin,
134                                   adns_rrtype type, adns_queryflags flags) {
135   byte *rqp;
136   findlabel_state fls;
137   int lablen, labstart;
138   adns_status st;
139
140   st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
141
142   MKQUERY_START(vb);
143
144   adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
145   for (;;) {
146     st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
147     if (!lablen) break;
148     assert(lablen<255);
149     MKQUERY_ADDB(lablen);
150     memcpy(rqp,qd_dgram+labstart,lablen);
151     rqp+= lablen;
152   }
153   MKQUERY_ADDB(0);
154
155   MKQUERY_STOP(vb);
156   
157   st= mkquery_footer(vb,type);
158   
159   return adns_s_ok;
160 }
161
162 void adns__querysend_tcp(adns_query qu, struct timeval now) {
163   byte length[2];
164   struct iovec iov[2];
165   int wr, r;
166   adns_state ads;
167
168   if (qu->ads->tcpstate != server_ok) return;
169
170   assert(qu->state == query_tcpw);
171
172   length[0]= (qu->query_dglen&0x0ff00U) >>8;
173   length[1]= (qu->query_dglen&0x0ff);
174
175   ads= qu->ads;
176   if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
177     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);
212     assert(r);
213   }
214 }
215
216 static void query_usetcp(adns_query qu, struct timeval now) {
217   qu->state= query_tcpw;
218   qu->timeout= now;
219   timevaladd(&qu->timeout,TCPWAITMS);
220   LIST_LINK_TAIL(qu->ads->tcpw,qu);
221   adns__querysend_tcp(qu,now);
222   adns__tcp_tryconnect(qu->ads,now);
223 }
224
225 void adns__query_send(adns_query qu, struct timeval now) {
226   int serv, r, i;
227   adns_state ads;
228   int fd = -1;
229   struct udpsocket *udp;
230   adns_rr_addr *addr;
231
232   assert(qu->state == query_tosend);
233   if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
234     query_usetcp(qu,now);
235     return;
236   }
237
238   if (qu->retries >= UDPMAXRETRIES) {
239     adns__query_fail(qu,adns_s_timeout);
240     return;
241   }
242
243   ads= qu->ads;
244   serv= qu->udpnextserver;
245   addr= &ads->servers[serv];
246   for (i = 0; i < ads->nudp; i++) {
247     udp = &ads->udpsocket[i];
248     if (udp->ai->af == addr->addr.sa.sa_family) { fd = udp->fd; break; }
249   }
250   assert(fd >= 0);
251   
252   r= sendto(fd,qu->query_dgram,qu->query_dglen,0,
253             &addr->addr.sa,addr->len);
254   if (r<0 && errno == EMSGSIZE) {
255     qu->retries= 0;
256     query_usetcp(qu,now);
257     return;
258   }
259   if (r<0 && errno != EAGAIN)
260     adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
261   
262   qu->timeout= now;
263   timevaladd(&qu->timeout,UDPRETRYMS);
264   qu->udpsent |= (1<<serv);
265   qu->udpnextserver= (serv+1)%ads->nservers;
266   qu->retries++;
267   LIST_LINK_TAIL(ads->udpw,qu);
268 }