chiark / gitweb /
Licensing: Copy GPL-3 into COPYING
[adns.git] / 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 3, 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  */
26
27 #include <errno.h>
28
29 #include <sys/types.h>
30 #include <sys/uio.h>
31
32 #include "internal.h"
33 #include "tvarith.h"
34
35 #define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used)
36 #define MKQUERY_ADDB(b) *rqp++= (b)
37 #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
38 #define MKQUERY_STOP(vb) ((vb)->used= rqp-(vb)->buf)
39
40 static adns_status mkquery_header(adns_state ads, vbuf *vb,
41                                   int *id_r, int qdlen) {
42   int id;
43   byte *rqp;
44   
45   if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+qdlen+4)) return adns_s_nomemory;
46
47   vb->used= 0;
48   MKQUERY_START(vb);
49   
50   *id_r= id= (ads->nextid++) & 0x0ffff;
51   MKQUERY_ADDW(id);
52   MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
53   MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
54   MKQUERY_ADDW(1); /* QDCOUNT=1 */
55   MKQUERY_ADDW(0); /* ANCOUNT=0 */
56   MKQUERY_ADDW(0); /* NSCOUNT=0 */
57   MKQUERY_ADDW(0); /* ARCOUNT=0 */
58
59   MKQUERY_STOP(vb);
60   
61   return adns_s_ok;
62 }
63
64 static adns_status mkquery_footer(vbuf *vb, adns_rrtype type) {
65   byte *rqp;
66
67   MKQUERY_START(vb);
68   MKQUERY_ADDW(type & adns_rrt_typemask); /* QTYPE */
69   MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */
70   MKQUERY_STOP(vb);
71   assert(vb->used <= vb->avail);
72   
73   return adns_s_ok;
74 }
75
76 static adns_status qdparselabel(adns_state ads,
77                                 const char **p_io, const char *pe,
78                                 char label_r[], int *ll_io,
79                                 adns_queryflags flags) {
80   int ll, c;
81   const char *p;
82
83   ll= 0;
84   p= *p_io;
85   
86   while (p!=pe && (c= *p++)!='.') {
87     if (c=='\\') {
88       if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
89       if (ctype_digit(p[0])) {
90         if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid;
91         if (ctype_digit(p[1]) && ctype_digit(p[2])) {
92           c= (*p++ - '0')*100;
93           c += (*p++ - '0')*10;
94           c += (*p++ - '0');
95           if (c >= 256) return adns_s_querydomaininvalid;
96         } else {
97           return adns_s_querydomaininvalid;
98         }
99       } else if (!(c= *p++)) {
100         return adns_s_querydomaininvalid;
101       }
102     }
103     if (ll == *ll_io) return adns_s_querydomaininvalid;
104     label_r[ll++]= c;
105   }
106   
107   *p_io= p;
108   *ll_io= ll;
109   return adns_s_ok;
110 }
111
112 adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
113                           const char *owner, int ol,
114                           const typeinfo *typei, adns_rrtype type,
115                           adns_queryflags flags) {
116   int ll, nbytes;
117   byte label[255];
118   byte *rqp;
119   const char *p, *pe;
120   adns_status st;
121
122   st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
123   
124   MKQUERY_START(vb);
125
126   p= owner; pe= owner+ol;
127   nbytes= 0;
128   while (p!=pe) {
129     ll= sizeof(label);
130     st= qdparselabel(ads, &p,pe, label, &ll, flags);
131     if (st) return st;
132     if (!ll) return adns_s_querydomaininvalid;
133     if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong;
134     nbytes+= ll+1;
135     if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
136     MKQUERY_ADDB(ll);
137     memcpy(rqp,label,ll); rqp+= ll;
138   }
139   MKQUERY_ADDB(0);
140
141   MKQUERY_STOP(vb);
142   
143   st= mkquery_footer(vb,type);
144   
145   return adns_s_ok;
146 }
147
148 adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
149                                   const byte *qd_dgram, int qd_dglen,
150                                   int qd_begin,
151                                   adns_rrtype type, adns_queryflags flags) {
152   byte *rqp;
153   findlabel_state fls;
154   int lablen, labstart;
155   adns_status st;
156
157   st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
158
159   MKQUERY_START(vb);
160
161   adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
162   for (;;) {
163     st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
164     if (!lablen) break;
165     assert(lablen<255);
166     MKQUERY_ADDB(lablen);
167     memcpy(rqp,qd_dgram+labstart,lablen);
168     rqp+= lablen;
169   }
170   MKQUERY_ADDB(0);
171
172   MKQUERY_STOP(vb);
173   
174   st= mkquery_footer(vb,type);
175   
176   return adns_s_ok;
177 }
178
179 void adns__querysend_tcp(adns_query qu, struct timeval now) {
180   byte length[2];
181   struct iovec iov[2];
182   int wr, r;
183   adns_state ads;
184
185   if (qu->ads->tcpstate != server_ok) return;
186
187   assert(qu->state == query_tcpw);
188
189   length[0]= (qu->query_dglen&0x0ff00U) >>8;
190   length[1]= (qu->query_dglen&0x0ff);
191
192   ads= qu->ads;
193   if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
194     return;
195
196   qu->retries++;
197
198   /* Reset idle timeout. */
199   ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
200
201   if (ads->tcpsend.used) {
202     wr= 0;
203   } else {
204     iov[0].iov_base= length;
205     iov[0].iov_len= 2;
206     iov[1].iov_base= qu->query_dgram;
207     iov[1].iov_len= qu->query_dglen;
208     adns__sigpipe_protect(qu->ads);
209     wr= writev(qu->ads->tcpsocket,iov,2);
210     adns__sigpipe_unprotect(qu->ads);
211     if (wr < 0) {
212       if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
213             errno == ENOBUFS || errno == ENOMEM)) {
214         adns__tcp_broken(ads,"write",strerror(errno));
215         return;
216       }
217       wr= 0;
218     }
219   }
220
221   if (wr<2) {
222     r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
223     wr= 0;
224   } else {
225     wr-= 2;
226   }
227   if (wr<qu->query_dglen) {
228     r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr);
229     assert(r);
230   }
231 }
232
233 static void query_usetcp(adns_query qu, struct timeval now) {
234   qu->state= query_tcpw;
235   qu->timeout= now;
236   timevaladd(&qu->timeout,TCPWAITMS);
237   LIST_LINK_TAIL(qu->ads->tcpw,qu);
238   adns__querysend_tcp(qu,now);
239   adns__tcp_tryconnect(qu->ads,now);
240 }
241
242 struct udpsocket *adns__udpsocket_by_af(adns_state ads, int af) {
243   int i;
244   for (i=0; i<ads->nudpsockets; i++)
245     if (ads->udpsockets[i].af == af) return &ads->udpsockets[i];
246   return 0;
247 }
248
249 void adns__query_send(adns_query qu, struct timeval now) {
250   int serv, r;
251   adns_state ads;
252   struct udpsocket *udp;
253   adns_rr_addr *addr;
254
255   assert(qu->state == query_tosend);
256   if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
257     query_usetcp(qu,now);
258     return;
259   }
260
261   if (qu->retries >= UDPMAXRETRIES) {
262     adns__query_fail(qu,adns_s_timeout);
263     return;
264   }
265
266   ads= qu->ads;
267   serv= qu->udpnextserver;
268   addr= &ads->servers[serv];
269   udp= adns__udpsocket_by_af(ads, addr->addr.sa.sa_family);
270   assert(udp);
271   
272   r= sendto(udp->fd,qu->query_dgram,qu->query_dglen,0,
273             &addr->addr.sa,addr->len);
274   if (r<0 && errno == EMSGSIZE) {
275     qu->retries= 0;
276     query_usetcp(qu,now);
277     return;
278   }
279   if (r<0 && errno != EAGAIN)
280     adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
281   
282   qu->timeout= now;
283   timevaladd(&qu->timeout,UDPRETRYMS);
284   qu->udpsent |= (1<<serv);
285   qu->udpnextserver= (serv+1)%ads->nservers;
286   qu->retries++;
287   LIST_LINK_TAIL(ads->udpw,qu);
288 }