+ * Better reporting of unexpected or weird replies from nameserver.
[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"
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
e062dcae 42static adns_status mkquery_header(adns_state ads, vbuf *vb, int *id_r, int qdlen) {
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);
69 MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */
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
77adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
78 const char *owner, int ol,
79 const typeinfo *typei, adns_queryflags flags) {
80 int ll, c, nlabs;
81 byte label[255], *rqp;
82 const char *p, *pe;
83 adns_status st;
84
914a5ff5 85 st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
e062dcae 86
87 MKQUERY_START(vb);
88
e576be50 89 p= owner; pe= owner+ol;
90 nlabs= 0;
e9e53c73 91 while (p!=pe) {
e576be50 92 ll= 0;
93 while (p!=pe && (c= *p++)!='.') {
94 if (c=='\\') {
ea1e31e3 95 if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
e576be50 96 if (ctype_digit(p[0])) {
97 if (ctype_digit(p[1]) && ctype_digit(p[2])) {
98 c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0');
ea1e31e3 99 if (c >= 256) return adns_s_querydomaininvalid;
e576be50 100 } else {
ea1e31e3 101 return adns_s_querydomaininvalid;
e576be50 102 }
103 } else if (!(c= *p++)) {
ea1e31e3 104 return adns_s_querydomaininvalid;
e576be50 105 }
106 }
828d89bd 107 if (!(flags & adns_qf_quoteok_query)) {
ffbda80c 108 if (c == '-') {
ea1e31e3 109 if (!ll) return adns_s_querydomaininvalid;
ffbda80c 110 } else if (!ctype_alpha(c) && !ctype_digit(c)) {
ea1e31e3 111 return adns_s_querydomaininvalid;
e576be50 112 }
113 }
ea1e31e3 114 if (ll == sizeof(label)) return adns_s_querydomaininvalid;
e576be50 115 label[ll++]= c;
116 }
ea1e31e3 117 if (!ll) return adns_s_querydomaininvalid;
118 if (nlabs++ > 63) return adns_s_querydomaintoolong;
e576be50 119 MKQUERY_ADDB(ll);
120 memcpy(rqp,label,ll); rqp+= ll;
e9e53c73 121 }
e062dcae 122 MKQUERY_ADDB(0);
123
124 MKQUERY_STOP(vb);
125
126 st= mkquery_footer(vb,typei->type);
127
128 return adns_s_ok;
129}
e576be50 130
e062dcae 131adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
132 const byte *qd_dgram, int qd_dglen, int qd_begin,
133 adns_rrtype type, adns_queryflags flags) {
134 byte *rqp;
135 findlabel_state fls;
136 int lablen, labstart;
137 adns_status st;
138
139 st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
140
141 MKQUERY_START(vb);
142
143 adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
144 for (;;) {
145 st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
146 if (!lablen) break;
147 assert(lablen<255);
148 MKQUERY_ADDB(lablen);
149 memcpy(rqp,qd_dgram+labstart,lablen);
150 rqp+= lablen;
151 }
e576be50 152 MKQUERY_ADDB(0);
e576be50 153
e062dcae 154 MKQUERY_STOP(vb);
155
156 st= mkquery_footer(vb,type);
e576be50 157
158 return adns_s_ok;
159}
160
f7f83b4a 161void adns__querysend_tcp(adns_query qu, struct timeval now) {
e576be50 162 byte length[2];
163 struct iovec iov[2];
164 int wr, r;
3955725c 165 adns_state ads;
e576be50 166
3955725c 167 if (qu->ads->tcpstate != server_ok) return;
e576be50 168
f7f83b4a 169 assert(qu->state == query_tcpw);
170
e576be50 171 length[0]= (qu->query_dglen&0x0ff00U) >>8;
172 length[1]= (qu->query_dglen&0x0ff);
3955725c 173
174 ads= qu->ads;
e576be50 175 if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return;
176
f7f83b4a 177 qu->retries++;
178
179 /* Reset idle timeout. */
180 ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
e576be50 181
182 if (ads->tcpsend.used) {
183 wr= 0;
184 } else {
185 iov[0].iov_base= length;
186 iov[0].iov_len= 2;
187 iov[1].iov_base= qu->query_dgram;
188 iov[1].iov_len= qu->query_dglen;
ac868fa8 189 adns__sigpipe_protect(qu->ads);
3955725c 190 wr= writev(qu->ads->tcpsocket,iov,2);
ac868fa8 191 adns__sigpipe_unprotect(qu->ads);
e576be50 192 if (wr < 0) {
193 if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
194 errno == ENOBUFS || errno == ENOMEM)) {
195 adns__tcp_broken(ads,"write",strerror(errno));
196 return;
197 }
198 wr= 0;
199 }
200 }
201
202 if (wr<2) {
203 r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
204 wr= 0;
205 } else {
206 wr-= 2;
207 }
208 if (wr<qu->query_dglen) {
209 r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r);
210 }
211}
212
3955725c 213static void query_usetcp(adns_query qu, struct timeval now) {
f7f83b4a 214 qu->state= query_tcpw;
e576be50 215 qu->timeout= now;
f7f83b4a 216 timevaladd(&qu->timeout,TCPWAITMS);
217 LIST_LINK_TAIL(qu->ads->tcpw,qu);
218 adns__querysend_tcp(qu,now);
3955725c 219 adns__tcp_tryconnect(qu->ads,now);
e576be50 220}
221
d8c062fa 222void adns__query_send(adns_query qu, struct timeval now) {
e576be50 223 struct sockaddr_in servaddr;
224 int serv, r;
3955725c 225 adns_state ads;
e576be50 226
d8c062fa 227 assert(qu->state == query_tosend);
e576be50 228 if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
3955725c 229 query_usetcp(qu,now);
e576be50 230 return;
231 }
232
f7f83b4a 233 if (qu->retries >= UDPMAXRETRIES) {
3955725c 234 adns__query_fail(qu,adns_s_timeout);
e576be50 235 return;
236 }
237
238 serv= qu->udpnextserver;
239 memset(&servaddr,0,sizeof(servaddr));
3955725c 240
241 ads= qu->ads;
e576be50 242 servaddr.sin_family= AF_INET;
243 servaddr.sin_addr= ads->servers[serv].addr;
244 servaddr.sin_port= htons(DNS_PORT);
245
71a6ff46 246 r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
247 (const struct sockaddr*)&servaddr,sizeof(servaddr));
f7f83b4a 248 if (r<0 && errno == EMSGSIZE) { qu->retries= 0; query_usetcp(qu,now); return; }
e576be50 249 if (r<0) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
250
e576be50 251 qu->timeout= now;
f7f83b4a 252 timevaladd(&qu->timeout,UDPRETRYMS);
e576be50 253 qu->udpsent |= (1<<serv);
254 qu->udpnextserver= (serv+1)%ads->nservers;
f7f83b4a 255 qu->retries++;
256 LIST_LINK_TAIL(ads->udpw,qu);
e576be50 257}