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