src/setup.c, src/transmit.c: New function finds udpsocket by AF.
[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 struct udpsocket *adns__udpsocket_by_af(adns_state ads, int af) {
226 int i;
227 for (i=0; i<ads->nudp; i++)
228 if (ads->udpsocket[i].af == af) return &ads->udpsocket[i];
229 return 0;
230 }
231
232 void adns__query_send(adns_query qu, struct timeval now) {
233 int serv, r, i;
234 adns_state ads;
235 struct udpsocket *udp;
236 adns_rr_addr *addr;
237
238 assert(qu->state == query_tosend);
239 if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
240 query_usetcp(qu,now);
241 return;
242 }
243
244 if (qu->retries >= UDPMAXRETRIES) {
245 adns__query_fail(qu,adns_s_timeout);
246 return;
247 }
248
249 ads= qu->ads;
250 serv= qu->udpnextserver;
251 addr= &ads->servers[serv];
252 udp= adns__udpsocket_by_af(ads, addr->addr.sa.sa_family);
253 assert(udp);
254
255 r= sendto(udp->fd,qu->query_dgram,qu->query_dglen,0,
256 &addr->addr.sa,addr->len);
257 if (r<0 && errno == EMSGSIZE) {
258 qu->retries= 0;
259 query_usetcp(qu,now);
260 return;
261 }
262 if (r<0 && errno != EAGAIN)
263 adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
264
265 qu->timeout= now;
266 timevaladd(&qu->timeout,UDPRETRYMS);
267 qu->udpsent |= (1<<serv);
268 qu->udpnextserver= (serv+1)%ads->nservers;
269 qu->retries++;
270 LIST_LINK_TAIL(ads->udpw,qu);
271 }