update copyright dates
[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
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
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
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 41static 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
65static 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
d19b03d0 77adns_status adns__qdpl_normal(adns_state ads,
78 const char **p_io, const char *pe, int labelnum,
79 char label_r[], int *ll_io,
80 adns_queryflags flags,
81 const typeinfo *typei) {
82 int ll, c;
83 const char *p;
84
85 ll= 0;
86 p= *p_io;
87
88 while (p!=pe && (c= *p++)!='.') {
89 if (c=='\\') {
90 if (!(flags & adns_qf_quoteok_query)) 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 (!(flags & adns_qf_quoteok_query)) {
106 if (c == '-') {
107 if (!ll) return adns_s_querydomaininvalid;
108 } else if (!ctype_alpha(c) && !ctype_digit(c)) {
109 return adns_s_querydomaininvalid;
110 }
111 }
112 if (ll == *ll_io) return adns_s_querydomaininvalid;
113 label_r[ll++]= c;
114 }
115
116 *p_io= p;
117 *ll_io= ll;
118 return adns_s_ok;
119}
120
e062dcae 121adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
122 const char *owner, int ol,
2c6eb096 123 const typeinfo *typei, adns_rrtype type,
124 adns_queryflags flags) {
d19b03d0 125 int labelnum, ll, nbytes;
126 byte label[255];
127 byte *rqp;
e062dcae 128 const char *p, *pe;
129 adns_status st;
130
914a5ff5 131 st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
e062dcae 132
133 MKQUERY_START(vb);
134
e576be50 135 p= owner; pe= owner+ol;
4b2c4f8a 136 nbytes= 0;
d19b03d0 137 labelnum= 0;
e9e53c73 138 while (p!=pe) {
d19b03d0 139 ll= sizeof(label);
140 st= typei->qdparselabel(ads, &p,pe, labelnum++, label, &ll, flags, typei);
141 if (st) return st;
ea1e31e3 142 if (!ll) return adns_s_querydomaininvalid;
5b9dd636 143 if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong;
4b2c4f8a 144 nbytes+= ll+1;
5b9dd636 145 if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
e576be50 146 MKQUERY_ADDB(ll);
147 memcpy(rqp,label,ll); rqp+= ll;
e9e53c73 148 }
e062dcae 149 MKQUERY_ADDB(0);
150
151 MKQUERY_STOP(vb);
152
2c6eb096 153 st= mkquery_footer(vb,type);
e062dcae 154
155 return adns_s_ok;
156}
e576be50 157
e062dcae 158adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
609133ee 159 const byte *qd_dgram, int qd_dglen,
160 int qd_begin,
e062dcae 161 adns_rrtype type, adns_queryflags flags) {
162 byte *rqp;
163 findlabel_state fls;
164 int lablen, labstart;
165 adns_status st;
166
167 st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
168
169 MKQUERY_START(vb);
170
171 adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
172 for (;;) {
173 st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
174 if (!lablen) break;
175 assert(lablen<255);
176 MKQUERY_ADDB(lablen);
177 memcpy(rqp,qd_dgram+labstart,lablen);
178 rqp+= lablen;
179 }
e576be50 180 MKQUERY_ADDB(0);
e576be50 181
e062dcae 182 MKQUERY_STOP(vb);
183
184 st= mkquery_footer(vb,type);
e576be50 185
186 return adns_s_ok;
187}
188
f7f83b4a 189void adns__querysend_tcp(adns_query qu, struct timeval now) {
e576be50 190 byte length[2];
191 struct iovec iov[2];
192 int wr, r;
3955725c 193 adns_state ads;
e576be50 194
3955725c 195 if (qu->ads->tcpstate != server_ok) return;
e576be50 196
f7f83b4a 197 assert(qu->state == query_tcpw);
198
e576be50 199 length[0]= (qu->query_dglen&0x0ff00U) >>8;
200 length[1]= (qu->query_dglen&0x0ff);
3955725c 201
202 ads= qu->ads;
609133ee 203 if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
204 return;
e576be50 205
f7f83b4a 206 qu->retries++;
207
208 /* Reset idle timeout. */
209 ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
e576be50 210
211 if (ads->tcpsend.used) {
212 wr= 0;
213 } else {
214 iov[0].iov_base= length;
215 iov[0].iov_len= 2;
216 iov[1].iov_base= qu->query_dgram;
217 iov[1].iov_len= qu->query_dglen;
ac868fa8 218 adns__sigpipe_protect(qu->ads);
3955725c 219 wr= writev(qu->ads->tcpsocket,iov,2);
ac868fa8 220 adns__sigpipe_unprotect(qu->ads);
e576be50 221 if (wr < 0) {
222 if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
223 errno == ENOBUFS || errno == ENOMEM)) {
224 adns__tcp_broken(ads,"write",strerror(errno));
225 return;
226 }
227 wr= 0;
228 }
229 }
230
231 if (wr<2) {
232 r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
233 wr= 0;
234 } else {
235 wr-= 2;
236 }
237 if (wr<qu->query_dglen) {
609133ee 238 r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr);
239 assert(r);
e576be50 240 }
241}
242
3955725c 243static void query_usetcp(adns_query qu, struct timeval now) {
f7f83b4a 244 qu->state= query_tcpw;
e576be50 245 qu->timeout= now;
f7f83b4a 246 timevaladd(&qu->timeout,TCPWAITMS);
247 LIST_LINK_TAIL(qu->ads->tcpw,qu);
248 adns__querysend_tcp(qu,now);
3955725c 249 adns__tcp_tryconnect(qu->ads,now);
e576be50 250}
251
d8c062fa 252void adns__query_send(adns_query qu, struct timeval now) {
e576be50 253 struct sockaddr_in servaddr;
254 int serv, r;
3955725c 255 adns_state ads;
e576be50 256
d8c062fa 257 assert(qu->state == query_tosend);
e576be50 258 if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
3955725c 259 query_usetcp(qu,now);
e576be50 260 return;
261 }
262
f7f83b4a 263 if (qu->retries >= UDPMAXRETRIES) {
3955725c 264 adns__query_fail(qu,adns_s_timeout);
e576be50 265 return;
266 }
267
268 serv= qu->udpnextserver;
269 memset(&servaddr,0,sizeof(servaddr));
3955725c 270
271 ads= qu->ads;
e576be50 272 servaddr.sin_family= AF_INET;
273 servaddr.sin_addr= ads->servers[serv].addr;
274 servaddr.sin_port= htons(DNS_PORT);
275
71a6ff46 276 r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
277 (const struct sockaddr*)&servaddr,sizeof(servaddr));
609133ee 278 if (r<0 && errno == EMSGSIZE) {
279 qu->retries= 0;
280 query_usetcp(qu,now);
281 return;
282 }
283 if (r<0 && errno != EAGAIN)
284 adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
e576be50 285
e576be50 286 qu->timeout= now;
f7f83b4a 287 timevaladd(&qu->timeout,UDPRETRYMS);
e576be50 288 qu->udpsent |= (1<<serv);
289 qu->udpnextserver= (serv+1)%ads->nservers;
f7f83b4a 290 qu->retries++;
291 LIST_LINK_TAIL(ads->udpw,qu);
e576be50 292}