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