adns-1.4-ipv6
[platform/upstream/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 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 /* FIXME: Return value is always adns_s_ok, and never used. But I
66  * don't understand why we can assert that we have space in the vbuf. */
67 static adns_status mkquery_footer(vbuf *vb, adns_rrtype type) {
68   byte *rqp;
69
70   MKQUERY_START(vb);
71   MKQUERY_ADDW(type & adns_rrt_typemask); /* QTYPE */
72   MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */
73   MKQUERY_STOP(vb);
74   assert(vb->used <= vb->avail);
75   
76   return adns_s_ok;
77 }
78
79 adns_status adns__qdpl_normal(adns_state ads,
80                               const char **p_io, const char *pe, int labelnum,
81                               char label_r[], int *ll_io,
82                               adns_queryflags flags,
83                               const typeinfo *typei) {
84   int ll, c;
85   const char *p;
86
87   ll= 0;
88   p= *p_io;
89   
90   while (p!=pe && (c= *p++)!='.') {
91     if (c=='\\') {
92       if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
93       if (ctype_digit(p[0])) {
94         if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid;
95         if (ctype_digit(p[1]) && ctype_digit(p[2])) {
96           c= (*p++ - '0')*100;
97           c += (*p++ - '0')*10;
98           c += (*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 == *ll_io) return adns_s_querydomaininvalid;
115     label_r[ll++]= c;
116   }
117   
118   *p_io= p;
119   *ll_io= ll;
120   return adns_s_ok;
121 }
122
123 adns_status adns__mkquery_labels(adns_state ads, vbuf *vb,
124                           const char *owner, int ol,
125                                  const typeinfo *typei, adns_queryflags flags) {
126   int labelnum, ll, nbytes;
127   byte label[255], *rqp;
128   const char *p, *pe;
129   adns_status st;
130
131   if (!adns__vbuf_ensure(vb,ol+2)) return adns_s_nomemory;
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   return adns_s_ok;  
153 }
154
155 adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
156                           const char *owner, int ol,
157                           const typeinfo *typei, adns_rrtype type,
158                           adns_queryflags flags) {
159   adns_status st;
160   
161   st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
162   st= adns__mkquery_labels(ads, vb, owner, ol, typei, flags); if (st) return st;
163   st= mkquery_footer(vb,type);
164   
165   return adns_s_ok;
166 }
167
168 adns_status adns__mkquery_labels_frdgram(adns_state ads, vbuf *vb,
169                                   const byte *qd_dgram, int qd_dglen,
170                                          int qd_begin) {
171   adns_status st;
172   byte *rqp;
173   findlabel_state fls;
174   int lablen, labstart;
175
176   if (!adns__vbuf_ensure(vb,qd_dglen)) return adns_s_nomemory;
177
178   MKQUERY_START(vb);
179
180   adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
181   for (;;) {
182     st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
183     if (!lablen) break;
184     assert(lablen<255);
185     MKQUERY_ADDB(lablen);
186     memcpy(rqp,qd_dgram+labstart,lablen);
187     rqp+= lablen;
188   }
189   MKQUERY_ADDB(0);
190
191   MKQUERY_STOP(vb);
192   
193   return adns_s_ok;  
194 }
195
196 adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
197                                   const byte *qd_dgram, int qd_dglen,
198                                   int qd_begin,
199                                   adns_rrtype type, adns_queryflags flags) {
200   adns_status st;
201
202   st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
203   st= adns__mkquery_labels_frdgram(ads, vb, qd_dgram, qd_dglen, qd_begin);
204   if (st) return st;
205   st= mkquery_footer(vb,type);
206   
207   return adns_s_ok;
208 }
209
210 adns_status adns__mkquery_frlabels(adns_state ads, vbuf *vb, int *id_r,
211                                    char *l, int llen,
212                                    adns_rrtype type, adns_queryflags flags) {
213   adns_status st;
214   
215   st= mkquery_header(ads,vb,id_r,llen); if (st) return st;
216   if (!adns__vbuf_append(vb, l, llen)) return adns_s_nomemory;
217   st= mkquery_footer(vb,type);
218   
219   return adns_s_ok;
220 }
221
222 void adns__querysend_tcp(adns_query qu, struct timeval now) {
223   byte length[2];
224   struct iovec iov[2];
225   int wr, r;
226   adns_state ads;
227
228   if (qu->ads->tcpstate != server_ok) return;
229
230   assert(qu->state == query_tcpw);
231
232   length[0]= (qu->query_dglen&0x0ff00U) >>8;
233   length[1]= (qu->query_dglen&0x0ff);
234
235   ads= qu->ads;
236   if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
237     return;
238
239   qu->retries++;
240
241   /* Reset idle timeout. */
242   ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
243
244   if (ads->tcpsend.used) {
245     wr= 0;
246   } else {
247     iov[0].iov_base= length;
248     iov[0].iov_len= 2;
249     iov[1].iov_base= qu->query_dgram;
250     iov[1].iov_len= qu->query_dglen;
251     adns__sigpipe_protect(qu->ads);
252     wr= writev(qu->ads->tcpsocket,iov,2);
253     adns__sigpipe_unprotect(qu->ads);
254     if (wr < 0) {
255       if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
256             errno == ENOBUFS || errno == ENOMEM)) {
257         adns__tcp_broken(ads,"write",strerror(errno));
258         return;
259       }
260       wr= 0;
261     }
262   }
263
264   if (wr<2) {
265     r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
266     wr= 0;
267   } else {
268     wr-= 2;
269   }
270   if (wr<qu->query_dglen) {
271     r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr);
272     assert(r);
273   }
274 }
275
276 static void query_usetcp(adns_query qu, struct timeval now) {
277   qu->state= query_tcpw;
278   qu->timeout= now;
279   timevaladd(&qu->timeout,TCPWAITMS);
280   LIST_LINK_TAIL(qu->ads->tcpw,qu);
281   adns__querysend_tcp(qu,now);
282   adns__tcp_tryconnect(qu->ads,now);
283 }
284
285 void adns__query_send(adns_query qu, struct timeval now) {
286   struct sockaddr_in servaddr;
287   int serv, r;
288   adns_state ads;
289
290   assert(qu->state == query_tosend);
291   if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
292     query_usetcp(qu,now);
293     return;
294   }
295
296   if (qu->retries >= UDPMAXRETRIES) {
297     adns__query_fail(qu,adns_s_timeout);
298     return;
299   }
300
301   serv= qu->udpnextserver;
302   memset(&servaddr,0,sizeof(servaddr));
303
304   ads= qu->ads;
305   servaddr.sin_family= AF_INET;
306   servaddr.sin_addr= ads->servers[serv].addr;
307   servaddr.sin_port= htons(DNS_PORT);
308   
309   r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
310             (const struct sockaddr*)&servaddr,sizeof(servaddr));
311   if (r<0 && errno == EMSGSIZE) {
312     qu->retries= 0;
313     query_usetcp(qu,now);
314     return;
315   }
316   if (r<0 && errno != EAGAIN)
317     adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
318   
319   qu->timeout= now;
320   timevaladd(&qu->timeout,UDPRETRYMS);
321   qu->udpsent |= (1<<serv);
322   qu->udpnextserver= (serv+1)%ads->nservers;
323   qu->retries++;
324   LIST_LINK_TAIL(ads->udpw,qu);
325 }