1*c2c66affSColin Finck /*
2*c2c66affSColin Finck * transmit.c
3*c2c66affSColin Finck * - construct queries
4*c2c66affSColin Finck * - send queries
5*c2c66affSColin Finck */
6*c2c66affSColin Finck /*
7*c2c66affSColin Finck * This file is
8*c2c66affSColin Finck * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
9*c2c66affSColin Finck *
10*c2c66affSColin Finck * It is part of adns, which is
11*c2c66affSColin Finck * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
12*c2c66affSColin Finck * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
13*c2c66affSColin Finck *
14*c2c66affSColin Finck * This program is free software; you can redistribute it and/or modify
15*c2c66affSColin Finck * it under the terms of the GNU General Public License as published by
16*c2c66affSColin Finck * the Free Software Foundation; either version 2, or (at your option)
17*c2c66affSColin Finck * any later version.
18*c2c66affSColin Finck *
19*c2c66affSColin Finck * This program is distributed in the hope that it will be useful,
20*c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
21*c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22*c2c66affSColin Finck * GNU General Public License for more details.
23*c2c66affSColin Finck *
24*c2c66affSColin Finck * You should have received a copy of the GNU General Public License
25*c2c66affSColin Finck * along with this program; if not, write to the Free Software Foundation,
26*c2c66affSColin Finck * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27*c2c66affSColin Finck */
28*c2c66affSColin Finck
29*c2c66affSColin Finck #include <errno.h>
30*c2c66affSColin Finck
31*c2c66affSColin Finck #ifdef ADNS_JGAA_WIN32
32*c2c66affSColin Finck # include "adns_win32.h"
33*c2c66affSColin Finck #else
34*c2c66affSColin Finck # include <sys/types.h>
35*c2c66affSColin Finck # include <sys/uio.h>
36*c2c66affSColin Finck #endif
37*c2c66affSColin Finck
38*c2c66affSColin Finck #include "internal.h"
39*c2c66affSColin Finck #include "tvarith.h"
40*c2c66affSColin Finck
41*c2c66affSColin Finck #define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used)
42*c2c66affSColin Finck #define MKQUERY_ADDB(b) *rqp++= (b)
43*c2c66affSColin Finck #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
44*c2c66affSColin Finck #define MKQUERY_STOP(vb) ((vb)->used= rqp-(vb)->buf)
45*c2c66affSColin Finck
mkquery_header(adns_state ads,vbuf * vb,int * id_r,int qdlen)46*c2c66affSColin Finck static adns_status mkquery_header(adns_state ads, vbuf *vb, int *id_r, int qdlen) {
47*c2c66affSColin Finck int id;
48*c2c66affSColin Finck byte *rqp;
49*c2c66affSColin Finck
50*c2c66affSColin Finck if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+qdlen+4)) return adns_s_nomemory;
51*c2c66affSColin Finck
52*c2c66affSColin Finck vb->used= 0;
53*c2c66affSColin Finck MKQUERY_START(vb);
54*c2c66affSColin Finck
55*c2c66affSColin Finck *id_r= id= (ads->nextid++) & 0x0ffff;
56*c2c66affSColin Finck MKQUERY_ADDW(id);
57*c2c66affSColin Finck MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
58*c2c66affSColin Finck MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
59*c2c66affSColin Finck MKQUERY_ADDW(1); /* QDCOUNT=1 */
60*c2c66affSColin Finck MKQUERY_ADDW(0); /* ANCOUNT=0 */
61*c2c66affSColin Finck MKQUERY_ADDW(0); /* NSCOUNT=0 */
62*c2c66affSColin Finck MKQUERY_ADDW(0); /* ARCOUNT=0 */
63*c2c66affSColin Finck
64*c2c66affSColin Finck MKQUERY_STOP(vb);
65*c2c66affSColin Finck
66*c2c66affSColin Finck return adns_s_ok;
67*c2c66affSColin Finck }
68*c2c66affSColin Finck
mkquery_footer(vbuf * vb,adns_rrtype type)69*c2c66affSColin Finck static adns_status mkquery_footer(vbuf *vb, adns_rrtype type) {
70*c2c66affSColin Finck byte *rqp;
71*c2c66affSColin Finck
72*c2c66affSColin Finck MKQUERY_START(vb);
73*c2c66affSColin Finck MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */
74*c2c66affSColin Finck MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */
75*c2c66affSColin Finck MKQUERY_STOP(vb);
76*c2c66affSColin Finck assert(vb->used <= vb->avail);
77*c2c66affSColin Finck
78*c2c66affSColin Finck return adns_s_ok;
79*c2c66affSColin Finck }
80*c2c66affSColin Finck
adns__mkquery(adns_state ads,vbuf * vb,int * id_r,const char * owner,int ol,const typeinfo * typei,adns_queryflags flags)81*c2c66affSColin Finck adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
82*c2c66affSColin Finck const char *owner, int ol,
83*c2c66affSColin Finck const typeinfo *typei, adns_queryflags flags) {
84*c2c66affSColin Finck int ll, c, nbytes;
85*c2c66affSColin Finck byte label[255], *rqp;
86*c2c66affSColin Finck const char *p, *pe;
87*c2c66affSColin Finck adns_status st;
88*c2c66affSColin Finck
89*c2c66affSColin Finck st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
90*c2c66affSColin Finck
91*c2c66affSColin Finck MKQUERY_START(vb);
92*c2c66affSColin Finck
93*c2c66affSColin Finck p= owner; pe= owner+ol;
94*c2c66affSColin Finck nbytes= 0;
95*c2c66affSColin Finck while (p!=pe) {
96*c2c66affSColin Finck ll= 0;
97*c2c66affSColin Finck while (p!=pe && (c= *p++)!='.') {
98*c2c66affSColin Finck if (c=='\\') {
99*c2c66affSColin Finck if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
100*c2c66affSColin Finck if (ctype_digit(p[0])) {
101*c2c66affSColin Finck if (ctype_digit(p[1]) && ctype_digit(p[2])) {
102*c2c66affSColin Finck c= (p[0] - '0')*100 + (p[1] - '0')*10 + (p[2] - '0');
103*c2c66affSColin Finck p += 3;
104*c2c66affSColin Finck if (c >= 256) return adns_s_querydomaininvalid;
105*c2c66affSColin Finck } else {
106*c2c66affSColin Finck return adns_s_querydomaininvalid;
107*c2c66affSColin Finck }
108*c2c66affSColin Finck } else if (!(c= *p++)) {
109*c2c66affSColin Finck return adns_s_querydomaininvalid;
110*c2c66affSColin Finck }
111*c2c66affSColin Finck }
112*c2c66affSColin Finck if (!(flags & adns_qf_quoteok_query)) {
113*c2c66affSColin Finck if (c == '-') {
114*c2c66affSColin Finck if (!ll) return adns_s_querydomaininvalid;
115*c2c66affSColin Finck } else if (!ctype_alpha(c) && !ctype_digit(c)) {
116*c2c66affSColin Finck return adns_s_querydomaininvalid;
117*c2c66affSColin Finck }
118*c2c66affSColin Finck }
119*c2c66affSColin Finck if (ll == sizeof(label)) return adns_s_querydomaininvalid;
120*c2c66affSColin Finck label[ll++]= c;
121*c2c66affSColin Finck }
122*c2c66affSColin Finck if (!ll) return adns_s_querydomaininvalid;
123*c2c66affSColin Finck if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong;
124*c2c66affSColin Finck nbytes+= ll+1;
125*c2c66affSColin Finck if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
126*c2c66affSColin Finck MKQUERY_ADDB(ll);
127*c2c66affSColin Finck memcpy(rqp,label,(size_t) ll); rqp+= ll;
128*c2c66affSColin Finck }
129*c2c66affSColin Finck MKQUERY_ADDB(0);
130*c2c66affSColin Finck
131*c2c66affSColin Finck MKQUERY_STOP(vb);
132*c2c66affSColin Finck
133*c2c66affSColin Finck st= mkquery_footer(vb,typei->type);
134*c2c66affSColin Finck
135*c2c66affSColin Finck return adns_s_ok;
136*c2c66affSColin Finck }
137*c2c66affSColin Finck
adns__mkquery_frdgram(adns_state ads,vbuf * vb,int * id_r,const byte * qd_dgram,int qd_dglen,int qd_begin,adns_rrtype type,adns_queryflags flags)138*c2c66affSColin Finck adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
139*c2c66affSColin Finck const byte *qd_dgram, int qd_dglen, int qd_begin,
140*c2c66affSColin Finck adns_rrtype type, adns_queryflags flags) {
141*c2c66affSColin Finck byte *rqp;
142*c2c66affSColin Finck findlabel_state fls;
143*c2c66affSColin Finck int lablen, labstart;
144*c2c66affSColin Finck adns_status st;
145*c2c66affSColin Finck
146*c2c66affSColin Finck st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
147*c2c66affSColin Finck
148*c2c66affSColin Finck MKQUERY_START(vb);
149*c2c66affSColin Finck
150*c2c66affSColin Finck adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
151*c2c66affSColin Finck for (;;) {
152*c2c66affSColin Finck st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
153*c2c66affSColin Finck if (!lablen) break;
154*c2c66affSColin Finck assert(lablen<255);
155*c2c66affSColin Finck MKQUERY_ADDB(lablen);
156*c2c66affSColin Finck memcpy(rqp,qd_dgram+labstart, (size_t) lablen);
157*c2c66affSColin Finck rqp+= lablen;
158*c2c66affSColin Finck }
159*c2c66affSColin Finck MKQUERY_ADDB(0);
160*c2c66affSColin Finck
161*c2c66affSColin Finck MKQUERY_STOP(vb);
162*c2c66affSColin Finck
163*c2c66affSColin Finck st= mkquery_footer(vb,type);
164*c2c66affSColin Finck
165*c2c66affSColin Finck return adns_s_ok;
166*c2c66affSColin Finck }
167*c2c66affSColin Finck
adns__querysend_tcp(adns_query qu,struct timeval now)168*c2c66affSColin Finck void adns__querysend_tcp(adns_query qu, struct timeval now) {
169*c2c66affSColin Finck byte length[2];
170*c2c66affSColin Finck struct iovec iov[2];
171*c2c66affSColin Finck int wr, r;
172*c2c66affSColin Finck adns_state ads;
173*c2c66affSColin Finck
174*c2c66affSColin Finck if (qu->ads->tcpstate != server_ok) return;
175*c2c66affSColin Finck
176*c2c66affSColin Finck assert(qu->state == query_tcpw);
177*c2c66affSColin Finck
178*c2c66affSColin Finck length[0]= (qu->query_dglen&0x0ff00U) >>8;
179*c2c66affSColin Finck length[1]= (qu->query_dglen&0x0ff);
180*c2c66affSColin Finck
181*c2c66affSColin Finck ads= qu->ads;
182*c2c66affSColin Finck if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return;
183*c2c66affSColin Finck
184*c2c66affSColin Finck qu->retries++;
185*c2c66affSColin Finck
186*c2c66affSColin Finck /* Reset idle timeout. */
187*c2c66affSColin Finck ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
188*c2c66affSColin Finck
189*c2c66affSColin Finck if (ads->tcpsend.used) {
190*c2c66affSColin Finck wr= 0;
191*c2c66affSColin Finck } else {
192*c2c66affSColin Finck iov[0].iov_base= (char*)length;
193*c2c66affSColin Finck iov[0].iov_len= 2;
194*c2c66affSColin Finck iov[1].iov_base= (char*)qu->query_dgram;
195*c2c66affSColin Finck iov[1].iov_len= qu->query_dglen;
196*c2c66affSColin Finck adns__sigpipe_protect(qu->ads);
197*c2c66affSColin Finck
198*c2c66affSColin Finck ADNS_CLEAR_ERRNO;
199*c2c66affSColin Finck wr= writev(qu->ads->tcpsocket,iov,2);
200*c2c66affSColin Finck ADNS_CAPTURE_ERRNO;
201*c2c66affSColin Finck adns__sigpipe_unprotect(qu->ads);
202*c2c66affSColin Finck if (wr < 0) {
203*c2c66affSColin Finck if (!(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR || errno == ENOSPC ||
204*c2c66affSColin Finck errno == ENOBUFS || errno == ENOMEM)) {
205*c2c66affSColin Finck adns__tcp_broken(ads,"write",strerror(errno));
206*c2c66affSColin Finck return;
207*c2c66affSColin Finck }
208*c2c66affSColin Finck wr= 0;
209*c2c66affSColin Finck }
210*c2c66affSColin Finck }
211*c2c66affSColin Finck
212*c2c66affSColin Finck if (wr<2) {
213*c2c66affSColin Finck r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
214*c2c66affSColin Finck wr= 0;
215*c2c66affSColin Finck } else {
216*c2c66affSColin Finck wr-= 2;
217*c2c66affSColin Finck }
218*c2c66affSColin Finck if (wr<qu->query_dglen) {
219*c2c66affSColin Finck r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r);
220*c2c66affSColin Finck }
221*c2c66affSColin Finck }
222*c2c66affSColin Finck
query_usetcp(adns_query qu,struct timeval now)223*c2c66affSColin Finck static void query_usetcp(adns_query qu, struct timeval now) {
224*c2c66affSColin Finck qu->state= query_tcpw;
225*c2c66affSColin Finck qu->timeout= now;
226*c2c66affSColin Finck timevaladd(&qu->timeout,TCPWAITMS);
227*c2c66affSColin Finck LIST_LINK_TAIL(qu->ads->tcpw,qu);
228*c2c66affSColin Finck adns__querysend_tcp(qu,now);
229*c2c66affSColin Finck adns__tcp_tryconnect(qu->ads,now);
230*c2c66affSColin Finck }
231*c2c66affSColin Finck
adns__query_send(adns_query qu,struct timeval now)232*c2c66affSColin Finck void adns__query_send(adns_query qu, struct timeval now) {
233*c2c66affSColin Finck struct sockaddr_in servaddr;
234*c2c66affSColin Finck int serv, r;
235*c2c66affSColin Finck adns_state ads;
236*c2c66affSColin Finck
237*c2c66affSColin Finck assert(qu->state == query_tosend);
238*c2c66affSColin Finck if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
239*c2c66affSColin Finck query_usetcp(qu,now);
240*c2c66affSColin Finck return;
241*c2c66affSColin Finck }
242*c2c66affSColin Finck
243*c2c66affSColin Finck if (qu->retries >= UDPMAXRETRIES) {
244*c2c66affSColin Finck adns__query_fail(qu,adns_s_timeout);
245*c2c66affSColin Finck return;
246*c2c66affSColin Finck }
247*c2c66affSColin Finck
248*c2c66affSColin Finck serv= qu->udpnextserver;
249*c2c66affSColin Finck memset(&servaddr,0,sizeof(servaddr));
250*c2c66affSColin Finck
251*c2c66affSColin Finck ads= qu->ads;
252*c2c66affSColin Finck servaddr.sin_family= AF_INET;
253*c2c66affSColin Finck servaddr.sin_addr= ads->servers[serv].addr;
254*c2c66affSColin Finck servaddr.sin_port= htons(DNS_PORT);
255*c2c66affSColin Finck
256*c2c66affSColin Finck ADNS_CLEAR_ERRNO;
257*c2c66affSColin Finck r= sendto(ads->udpsocket,(char*)qu->query_dgram,qu->query_dglen,0,
258*c2c66affSColin Finck (const struct sockaddr*)&servaddr,sizeof(servaddr));
259*c2c66affSColin Finck ADNS_CAPTURE_ERRNO;
260*c2c66affSColin Finck if (r<0 && errno == EMSGSIZE) { qu->retries= 0; query_usetcp(qu,now); return; }
261*c2c66affSColin Finck if (r<0 && ((errno != EAGAIN) && (errno != EWOULDBLOCK))) adns__warn(ads,serv,0,"sendto failed: %s (%d)",strerror(errno), errno);
262*c2c66affSColin Finck
263*c2c66affSColin Finck qu->timeout= now;
264*c2c66affSColin Finck timevaladd(&qu->timeout,UDPRETRYMS);
265*c2c66affSColin Finck qu->udpsent |= (1<<serv);
266*c2c66affSColin Finck qu->udpnextserver= (serv+1)%ads->nservers;
267*c2c66affSColin Finck qu->retries++;
268*c2c66affSColin Finck LIST_LINK_TAIL(ads->udpw,qu);
269*c2c66affSColin Finck }
270