New TCP tests.
[adns] / src / transmit.c
CommitLineData
e576be50 1/*
2 * transmit.c
3 * - construct queries
4 * - send queries
5 */
6/*
d942707d 7 * This file is
8 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
9 *
10 * It is part of adns, which is
11 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
e576be50 13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
3955725c 29#include <errno.h>
3955725c 30
71a6ff46 31#include <sys/types.h>
3955725c 32#include <sys/uio.h>
33
e576be50 34#include "internal.h"
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
e062dcae 41static adns_status mkquery_header(adns_state ads, vbuf *vb, int *id_r, int qdlen) {
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);
68 MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */
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
76adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
77 const char *owner, int ol,
78 const typeinfo *typei, adns_queryflags flags) {
79 int ll, c, nlabs;
80 byte label[255], *rqp;
81 const char *p, *pe;
82 adns_status st;
83
84 st= mkquery_header(ads,vb,id_r,strlen(owner)+2); if (st) return st;
85
86 MKQUERY_START(vb);
87
e576be50 88 p= owner; pe= owner+ol;
89 nlabs= 0;
e9e53c73 90 while (p!=pe) {
e576be50 91 ll= 0;
92 while (p!=pe && (c= *p++)!='.') {
93 if (c=='\\') {
ea1e31e3 94 if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
e576be50 95 if (ctype_digit(p[0])) {
96 if (ctype_digit(p[1]) && ctype_digit(p[2])) {
97 c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0');
ea1e31e3 98 if (c >= 256) return adns_s_querydomaininvalid;
e576be50 99 } else {
ea1e31e3 100 return adns_s_querydomaininvalid;
e576be50 101 }
102 } else if (!(c= *p++)) {
ea1e31e3 103 return adns_s_querydomaininvalid;
e576be50 104 }
105 }
828d89bd 106 if (!(flags & adns_qf_quoteok_query)) {
ffbda80c 107 if (c == '-') {
ea1e31e3 108 if (!ll) return adns_s_querydomaininvalid;
ffbda80c 109 } else if (!ctype_alpha(c) && !ctype_digit(c)) {
ea1e31e3 110 return adns_s_querydomaininvalid;
e576be50 111 }
112 }
ea1e31e3 113 if (ll == sizeof(label)) return adns_s_querydomaininvalid;
e576be50 114 label[ll++]= c;
115 }
ea1e31e3 116 if (!ll) return adns_s_querydomaininvalid;
117 if (nlabs++ > 63) return adns_s_querydomaintoolong;
e576be50 118 MKQUERY_ADDB(ll);
119 memcpy(rqp,label,ll); rqp+= ll;
e9e53c73 120 }
e062dcae 121 MKQUERY_ADDB(0);
122
123 MKQUERY_STOP(vb);
124
125 st= mkquery_footer(vb,typei->type);
126
127 return adns_s_ok;
128}
e576be50 129
e062dcae 130adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
131 const byte *qd_dgram, int qd_dglen, int qd_begin,
132 adns_rrtype type, adns_queryflags flags) {
133 byte *rqp;
134 findlabel_state fls;
135 int lablen, labstart;
136 adns_status st;
137
138 st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
139
140 MKQUERY_START(vb);
141
142 adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
143 for (;;) {
144 st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
145 if (!lablen) break;
146 assert(lablen<255);
147 MKQUERY_ADDB(lablen);
148 memcpy(rqp,qd_dgram+labstart,lablen);
149 rqp+= lablen;
150 }
e576be50 151 MKQUERY_ADDB(0);
e576be50 152
e062dcae 153 MKQUERY_STOP(vb);
154
155 st= mkquery_footer(vb,type);
e576be50 156
157 return adns_s_ok;
158}
159
3955725c 160void adns__query_tcp(adns_query qu, struct timeval now) {
e576be50 161 byte length[2];
162 struct iovec iov[2];
163 int wr, r;
3955725c 164 adns_state ads;
e576be50 165
3955725c 166 if (qu->ads->tcpstate != server_ok) return;
e576be50 167
168 length[0]= (qu->query_dglen&0x0ff00U) >>8;
169 length[1]= (qu->query_dglen&0x0ff);
3955725c 170
171 ads= qu->ads;
e576be50 172 if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return;
173
174 timevaladd(&now,TCPMS);
175 qu->timeout= now;
176 qu->state= query_tcpsent;
e576be50 177
178 if (ads->tcpsend.used) {
179 wr= 0;
180 } else {
181 iov[0].iov_base= length;
182 iov[0].iov_len= 2;
183 iov[1].iov_base= qu->query_dgram;
184 iov[1].iov_len= qu->query_dglen;
ac868fa8 185 adns__sigpipe_protect(qu->ads);
3955725c 186 wr= writev(qu->ads->tcpsocket,iov,2);
ac868fa8 187 adns__sigpipe_unprotect(qu->ads);
e576be50 188 if (wr < 0) {
189 if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
190 errno == ENOBUFS || errno == ENOMEM)) {
191 adns__tcp_broken(ads,"write",strerror(errno));
192 return;
193 }
194 wr= 0;
195 }
196 }
197
198 if (wr<2) {
199 r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
200 wr= 0;
201 } else {
202 wr-= 2;
203 }
204 if (wr<qu->query_dglen) {
205 r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r);
206 }
207}
208
3955725c 209static void query_usetcp(adns_query qu, struct timeval now) {
e576be50 210 timevaladd(&now,TCPMS);
211 qu->timeout= now;
212 qu->state= query_tcpwait;
3955725c 213 LIST_LINK_TAIL(qu->ads->timew,qu);
214 adns__query_tcp(qu,now);
215 adns__tcp_tryconnect(qu->ads,now);
e576be50 216}
217
d8c062fa 218void adns__query_send(adns_query qu, struct timeval now) {
e576be50 219 struct sockaddr_in servaddr;
220 int serv, r;
3955725c 221 adns_state ads;
e576be50 222
d8c062fa 223 assert(qu->state == query_tosend);
e576be50 224 if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
3955725c 225 query_usetcp(qu,now);
e576be50 226 return;
227 }
228
229 if (qu->udpretries >= UDPMAXRETRIES) {
3955725c 230 adns__query_fail(qu,adns_s_timeout);
e576be50 231 return;
232 }
233
234 serv= qu->udpnextserver;
235 memset(&servaddr,0,sizeof(servaddr));
3955725c 236
237 ads= qu->ads;
e576be50 238 servaddr.sin_family= AF_INET;
239 servaddr.sin_addr= ads->servers[serv].addr;
240 servaddr.sin_port= htons(DNS_PORT);
241
71a6ff46 242 r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
243 (const struct sockaddr*)&servaddr,sizeof(servaddr));
3955725c 244 if (r<0 && errno == EMSGSIZE) { query_usetcp(qu,now); return; }
e576be50 245 if (r<0) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
246
247 timevaladd(&now,UDPRETRYMS);
248 qu->timeout= now;
249 qu->udpsent |= (1<<serv);
250 qu->udpnextserver= (serv+1)%ads->nservers;
251 qu->udpretries++;
252 LIST_LINK_TAIL(ads->timew,qu);
253}