Commit | Line | Data |
---|---|---|
e576be50 | 1 | /* |
2 | * transmit.c | |
3 | * - construct queries | |
4 | * - send queries | |
5 | */ | |
6 | /* | |
ae8cc977 | 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.) | |
e576be50 | 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 | |
7f8bbe29 | 15 | * the Free Software Foundation; either version 3, or (at your option) |
e576be50 | 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 | ||
3955725c | 28 | #include <errno.h> |
3955725c | 29 | |
71a6ff46 | 30 | #include <sys/types.h> |
3955725c | 31 | #include <sys/uio.h> |
32 | ||
e576be50 | 33 | #include "internal.h" |
2953d358 | 34 | #include "tvarith.h" |
e576be50 | 35 | |
e062dcae | 36 | #define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used) |
e576be50 | 37 | #define MKQUERY_ADDB(b) *rqp++= (b) |
38 | #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff)) | |
e062dcae | 39 | #define MKQUERY_STOP(vb) ((vb)->used= rqp-(vb)->buf) |
e576be50 | 40 | |
609133ee | 41 | static adns_status mkquery_header(adns_state ads, vbuf *vb, |
42 | int *id_r, int qdlen) { | |
e062dcae | 43 | int id; |
44 | byte *rqp; | |
45 | ||
ea1e31e3 | 46 | if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+qdlen+4)) return adns_s_nomemory; |
e576be50 | 47 | |
e062dcae | 48 | vb->used= 0; |
49 | MKQUERY_START(vb); | |
50 | ||
660d7d3b | 51 | *id_r= id= (ads->nextid++) & 0x0ffff; |
e576be50 | 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 */ | |
e062dcae | 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); | |
2c6eb096 | 69 | MKQUERY_ADDW(type & adns_rrt_typemask); /* QTYPE */ |
e062dcae | 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 | ||
e8e5aeac MW |
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) { | |
d19b03d0 | 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 (ctype_digit(p[0])) { | |
91 | if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid; | |
92 | if (ctype_digit(p[1]) && ctype_digit(p[2])) { | |
93 | c= (*p++ - '0')*100; | |
94 | c += (*p++ - '0')*10; | |
95 | c += (*p++ - '0'); | |
96 | if (c >= 256) return adns_s_querydomaininvalid; | |
97 | } else { | |
98 | return adns_s_querydomaininvalid; | |
99 | } | |
100 | } else if (!(c= *p++)) { | |
101 | return adns_s_querydomaininvalid; | |
102 | } | |
103 | } | |
d19b03d0 | 104 | if (ll == *ll_io) return adns_s_querydomaininvalid; |
105 | label_r[ll++]= c; | |
106 | } | |
107 | ||
108 | *p_io= p; | |
109 | *ll_io= ll; | |
110 | return adns_s_ok; | |
111 | } | |
112 | ||
e062dcae | 113 | adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r, |
114 | const char *owner, int ol, | |
2c6eb096 | 115 | const typeinfo *typei, adns_rrtype type, |
116 | adns_queryflags flags) { | |
e8e5aeac | 117 | int ll, nbytes; |
d19b03d0 | 118 | byte label[255]; |
119 | byte *rqp; | |
e062dcae | 120 | const char *p, *pe; |
121 | adns_status st; | |
122 | ||
914a5ff5 | 123 | st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st; |
e062dcae | 124 | |
125 | MKQUERY_START(vb); | |
126 | ||
e576be50 | 127 | p= owner; pe= owner+ol; |
4b2c4f8a | 128 | nbytes= 0; |
e9e53c73 | 129 | while (p!=pe) { |
d19b03d0 | 130 | ll= sizeof(label); |
e8e5aeac | 131 | st= qdparselabel(ads, &p,pe, label, &ll, flags); |
d19b03d0 | 132 | if (st) return st; |
ea1e31e3 | 133 | if (!ll) return adns_s_querydomaininvalid; |
5b9dd636 | 134 | if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong; |
4b2c4f8a | 135 | nbytes+= ll+1; |
5b9dd636 | 136 | if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong; |
e576be50 | 137 | MKQUERY_ADDB(ll); |
138 | memcpy(rqp,label,ll); rqp+= ll; | |
e9e53c73 | 139 | } |
e062dcae | 140 | MKQUERY_ADDB(0); |
141 | ||
142 | MKQUERY_STOP(vb); | |
143 | ||
2c6eb096 | 144 | st= mkquery_footer(vb,type); |
e062dcae | 145 | |
146 | return adns_s_ok; | |
147 | } | |
e576be50 | 148 | |
e062dcae | 149 | adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r, |
609133ee | 150 | const byte *qd_dgram, int qd_dglen, |
151 | int qd_begin, | |
e062dcae | 152 | adns_rrtype type, adns_queryflags flags) { |
153 | byte *rqp; | |
154 | findlabel_state fls; | |
155 | int lablen, labstart; | |
156 | adns_status st; | |
157 | ||
158 | st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st; | |
159 | ||
160 | MKQUERY_START(vb); | |
161 | ||
162 | adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0); | |
163 | for (;;) { | |
164 | st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st); | |
165 | if (!lablen) break; | |
166 | assert(lablen<255); | |
167 | MKQUERY_ADDB(lablen); | |
168 | memcpy(rqp,qd_dgram+labstart,lablen); | |
169 | rqp+= lablen; | |
170 | } | |
e576be50 | 171 | MKQUERY_ADDB(0); |
e576be50 | 172 | |
e062dcae | 173 | MKQUERY_STOP(vb); |
174 | ||
175 | st= mkquery_footer(vb,type); | |
e576be50 | 176 | |
177 | return adns_s_ok; | |
178 | } | |
179 | ||
f7f83b4a | 180 | void adns__querysend_tcp(adns_query qu, struct timeval now) { |
e576be50 | 181 | byte length[2]; |
182 | struct iovec iov[2]; | |
183 | int wr, r; | |
3955725c | 184 | adns_state ads; |
e576be50 | 185 | |
3955725c | 186 | if (qu->ads->tcpstate != server_ok) return; |
e576be50 | 187 | |
f7f83b4a | 188 | assert(qu->state == query_tcpw); |
189 | ||
e576be50 | 190 | length[0]= (qu->query_dglen&0x0ff00U) >>8; |
191 | length[1]= (qu->query_dglen&0x0ff); | |
3955725c | 192 | |
193 | ads= qu->ads; | |
609133ee | 194 | if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) |
195 | return; | |
e576be50 | 196 | |
f7f83b4a | 197 | qu->retries++; |
198 | ||
199 | /* Reset idle timeout. */ | |
200 | ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0; | |
e576be50 | 201 | |
202 | if (ads->tcpsend.used) { | |
203 | wr= 0; | |
204 | } else { | |
205 | iov[0].iov_base= length; | |
206 | iov[0].iov_len= 2; | |
207 | iov[1].iov_base= qu->query_dgram; | |
208 | iov[1].iov_len= qu->query_dglen; | |
ac868fa8 | 209 | adns__sigpipe_protect(qu->ads); |
3955725c | 210 | wr= writev(qu->ads->tcpsocket,iov,2); |
ac868fa8 | 211 | adns__sigpipe_unprotect(qu->ads); |
e576be50 | 212 | if (wr < 0) { |
213 | if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC || | |
214 | errno == ENOBUFS || errno == ENOMEM)) { | |
215 | adns__tcp_broken(ads,"write",strerror(errno)); | |
216 | return; | |
217 | } | |
218 | wr= 0; | |
219 | } | |
220 | } | |
221 | ||
222 | if (wr<2) { | |
223 | r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r); | |
224 | wr= 0; | |
225 | } else { | |
226 | wr-= 2; | |
227 | } | |
228 | if (wr<qu->query_dglen) { | |
609133ee | 229 | r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); |
230 | assert(r); | |
e576be50 | 231 | } |
232 | } | |
233 | ||
3955725c | 234 | static void query_usetcp(adns_query qu, struct timeval now) { |
f7f83b4a | 235 | qu->state= query_tcpw; |
e576be50 | 236 | qu->timeout= now; |
f7f83b4a | 237 | timevaladd(&qu->timeout,TCPWAITMS); |
238 | LIST_LINK_TAIL(qu->ads->tcpw,qu); | |
239 | adns__querysend_tcp(qu,now); | |
3955725c | 240 | adns__tcp_tryconnect(qu->ads,now); |
e576be50 | 241 | } |
242 | ||
3eb20edd MW |
243 | struct udpsocket *adns__udpsocket_by_af(adns_state ads, int af) { |
244 | int i; | |
97a937c8 IJ |
245 | for (i=0; i<ads->nudpsockets; i++) |
246 | if (ads->udpsockets[i].af == af) return &ads->udpsockets[i]; | |
3eb20edd MW |
247 | return 0; |
248 | } | |
249 | ||
d8c062fa | 250 | void adns__query_send(adns_query qu, struct timeval now) { |
e576be50 | 251 | int serv, r; |
3955725c | 252 | adns_state ads; |
3eb20edd | 253 | struct udpsocket *udp; |
f930c455 | 254 | adns_rr_addr *addr; |
e576be50 | 255 | |
d8c062fa | 256 | assert(qu->state == query_tosend); |
e576be50 | 257 | if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) { |
3955725c | 258 | query_usetcp(qu,now); |
e576be50 | 259 | return; |
260 | } | |
261 | ||
f7f83b4a | 262 | if (qu->retries >= UDPMAXRETRIES) { |
3955725c | 263 | adns__query_fail(qu,adns_s_timeout); |
e576be50 | 264 | return; |
265 | } | |
266 | ||
3955725c | 267 | ads= qu->ads; |
f930c455 MW |
268 | serv= qu->udpnextserver; |
269 | addr= &ads->servers[serv]; | |
3eb20edd MW |
270 | udp= adns__udpsocket_by_af(ads, addr->addr.sa.sa_family); |
271 | assert(udp); | |
e576be50 | 272 | |
3eb20edd | 273 | r= sendto(udp->fd,qu->query_dgram,qu->query_dglen,0, |
f930c455 | 274 | &addr->addr.sa,addr->len); |
609133ee | 275 | if (r<0 && errno == EMSGSIZE) { |
276 | qu->retries= 0; | |
277 | query_usetcp(qu,now); | |
278 | return; | |
279 | } | |
280 | if (r<0 && errno != EAGAIN) | |
281 | adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno)); | |
e576be50 | 282 | |
e576be50 | 283 | qu->timeout= now; |
f7f83b4a | 284 | timevaladd(&qu->timeout,UDPRETRYMS); |
e576be50 | 285 | qu->udpsent |= (1<<serv); |
286 | qu->udpnextserver= (serv+1)%ads->nservers; | |
f7f83b4a | 287 | qu->retries++; |
288 | LIST_LINK_TAIL(ads->udpw,qu); | |
e576be50 | 289 | } |