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