chiark / gitweb /
Update copyright dates everywhere
[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,2014-2016,2020  Ian Jackson
9  *    Copyright (C) 2014  Mark Wooding
10  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
11  *    Copyright (C) 1991 Massachusetts Institute of Technology
12  *  (See the file INSTALL for full details.)
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 3, 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  */
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 static adns_status qdparselabel(adns_state ads,
78                                 const char **p_io, const char *pe,
79                                 char label_r[], int *ll_io,
80                                 adns_queryflags flags) {
81   int ll, c;
82   const char *p;
83
84   ll= 0;
85   p= *p_io;
86   
87   while (p!=pe && (c= *p++)!='.') {
88     if (c=='\\') {
89       if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
90       if (p==pe) return adns_s_querydomaininvalid;
91       if (ctype_digit(p[0])) {
92         if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid;
93         if (ctype_digit(p[1]) && ctype_digit(p[2])) {
94           c= (*p++ - '0')*100;
95           c += (*p++ - '0')*10;
96           c += (*p++ - '0');
97           if (c >= 256) return adns_s_querydomaininvalid;
98         } else {
99           return adns_s_querydomaininvalid;
100         }
101       } else if (!(c= *p++)) {
102         return adns_s_querydomaininvalid;
103       }
104     }
105     if (ll == *ll_io) return adns_s_querydomaininvalid;
106     label_r[ll++]= c;
107   }
108   
109   *p_io= p;
110   *ll_io= ll;
111   return adns_s_ok;
112 }
113
114 adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
115                           const char *owner, int ol,
116                           const typeinfo *typei, adns_rrtype type,
117                           adns_queryflags flags) {
118   int ll, nbytes;
119   byte label[255];
120   byte *rqp;
121   const char *p, *pe;
122   adns_status st;
123
124   st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
125   
126   MKQUERY_START(vb);
127
128   p= owner; pe= owner+ol;
129   nbytes= 0;
130   while (p!=pe) {
131     ll= sizeof(label);
132     st= qdparselabel(ads, &p,pe, label, &ll, flags);
133     if (st) return st;
134     if (!ll) return adns_s_querydomaininvalid;
135     if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong;
136     nbytes+= ll+1;
137     if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
138     MKQUERY_ADDB(ll);
139     memcpy(rqp,label,ll); rqp+= ll;
140   }
141   MKQUERY_ADDB(0);
142
143   MKQUERY_STOP(vb);
144   
145   st= mkquery_footer(vb,type);
146   
147   return adns_s_ok;
148 }
149
150 adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
151                                   const byte *qd_dgram, int qd_dglen,
152                                   int qd_begin,
153                                   adns_rrtype type, adns_queryflags flags) {
154   byte *rqp;
155   findlabel_state fls;
156   int lablen, labstart;
157   adns_status st;
158
159   st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
160
161   MKQUERY_START(vb);
162
163   adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
164   for (;;) {
165     st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
166     if (!lablen) break;
167     assert(lablen<255);
168     MKQUERY_ADDB(lablen);
169     memcpy(rqp,qd_dgram+labstart,lablen);
170     rqp+= lablen;
171   }
172   MKQUERY_ADDB(0);
173
174   MKQUERY_STOP(vb);
175   
176   st= mkquery_footer(vb,type);
177   
178   return adns_s_ok;
179 }
180
181 void adns__querysend_tcp(adns_query qu, struct timeval now) {
182   byte length[2];
183   struct iovec iov[2];
184   int wr, r;
185   adns_state ads;
186
187   if (qu->ads->tcpstate != server_ok) return;
188
189   assert(qu->state == query_tcpw);
190
191   length[0]= (qu->query_dglen&0x0ff00U) >>8;
192   length[1]= (qu->query_dglen&0x0ff);
193
194   ads= qu->ads;
195   if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
196     return;
197
198   qu->retries++;
199
200   /* Reset idle timeout. */
201   ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
202
203   if (ads->tcpsend.used) {
204     wr= 0;
205   } else {
206     iov[0].iov_base= length;
207     iov[0].iov_len= 2;
208     iov[1].iov_base= qu->query_dgram;
209     iov[1].iov_len= qu->query_dglen;
210     adns__sigpipe_protect(qu->ads);
211     wr= writev(qu->ads->tcpsocket,iov,2);
212     adns__sigpipe_unprotect(qu->ads);
213     if (wr < 0) {
214       if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
215             errno == ENOBUFS || errno == ENOMEM)) {
216         adns__tcp_broken(ads,"write",strerror(errno));
217         return;
218       }
219       wr= 0;
220     }
221   }
222
223   if (wr<2) {
224     r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
225     wr= 0;
226   } else {
227     wr-= 2;
228   }
229   if (wr<qu->query_dglen) {
230     r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr);
231     assert(r);
232   }
233 }
234
235 static void query_usetcp(adns_query qu, struct timeval now) {
236   qu->state= query_tcpw;
237   qu->timeout= now;
238   timevaladd(&qu->timeout,TCPWAITMS);
239   LIST_LINK_TAIL(qu->ads->tcpw,qu);
240   adns__querysend_tcp(qu,now);
241   adns__tcp_tryconnect(qu->ads,now);
242 }
243
244 struct udpsocket *adns__udpsocket_by_af(adns_state ads, int af) {
245   int i;
246   for (i=0; i<ads->nudpsockets; i++)
247     if (ads->udpsockets[i].af == af) return &ads->udpsockets[i];
248   return 0;
249 }
250
251 void adns__query_send(adns_query qu, struct timeval now) {
252   int serv, r;
253   adns_state ads;
254   struct udpsocket *udp;
255   adns_rr_addr *addr;
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   ads= qu->ads;
269   serv= qu->udpnextserver;
270   addr= &ads->servers[serv];
271   udp= adns__udpsocket_by_af(ads, addr->addr.sa.sa_family);
272   assert(udp);
273   
274   r= sendto(udp->fd,qu->query_dgram,qu->query_dglen,0,
275             &addr->addr.sa,addr->len);
276   if (r<0 && errno == EMSGSIZE) {
277     qu->retries= 0;
278     query_usetcp(qu,now);
279     return;
280   }
281   if (r<0 && errno != EAGAIN)
282     adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
283   
284   qu->timeout= now;
285   timevaladd(&qu->timeout,UDPRETRYMS);
286   qu->udpsent |= (1<<serv);
287   qu->udpnextserver= (serv+1)%ads->nservers;
288   qu->retries++;
289   LIST_LINK_TAIL(ads->udpw,qu);
290 }