+ * New adns_submit_reverse_any for eg RBL lookups, and corresponding
[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) {
4b2c4f8a 80 int ll, c, nbytes;
e062dcae 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;
4b2c4f8a 90 nbytes= 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;
4b2c4f8a 118 if (ll > 63) return adns_s_querydomaintoolong;
119 nbytes+= ll+1;
120 if (nbytes > 254) return adns_s_querydomaintoolong;
e576be50 121 MKQUERY_ADDB(ll);
122 memcpy(rqp,label,ll); rqp+= ll;
e9e53c73 123 }
e062dcae 124 MKQUERY_ADDB(0);
125
126 MKQUERY_STOP(vb);
127
128 st= mkquery_footer(vb,typei->type);
129
130 return adns_s_ok;
131}
e576be50 132
e062dcae 133adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
134 const byte *qd_dgram, int qd_dglen, int qd_begin,
135 adns_rrtype type, adns_queryflags flags) {
136 byte *rqp;
137 findlabel_state fls;
138 int lablen, labstart;
139 adns_status st;
140
141 st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
142
143 MKQUERY_START(vb);
144
145 adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
146 for (;;) {
147 st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
148 if (!lablen) break;
149 assert(lablen<255);
150 MKQUERY_ADDB(lablen);
151 memcpy(rqp,qd_dgram+labstart,lablen);
152 rqp+= lablen;
153 }
e576be50 154 MKQUERY_ADDB(0);
e576be50 155
e062dcae 156 MKQUERY_STOP(vb);
157
158 st= mkquery_footer(vb,type);
e576be50 159
160 return adns_s_ok;
161}
162
f7f83b4a 163void adns__querysend_tcp(adns_query qu, struct timeval now) {
e576be50 164 byte length[2];
165 struct iovec iov[2];
166 int wr, r;
3955725c 167 adns_state ads;
e576be50 168
3955725c 169 if (qu->ads->tcpstate != server_ok) return;
e576be50 170
f7f83b4a 171 assert(qu->state == query_tcpw);
172
e576be50 173 length[0]= (qu->query_dglen&0x0ff00U) >>8;
174 length[1]= (qu->query_dglen&0x0ff);
3955725c 175
176 ads= qu->ads;
e576be50 177 if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return;
178
f7f83b4a 179 qu->retries++;
180
181 /* Reset idle timeout. */
182 ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
e576be50 183
184 if (ads->tcpsend.used) {
185 wr= 0;
186 } else {
187 iov[0].iov_base= length;
188 iov[0].iov_len= 2;
189 iov[1].iov_base= qu->query_dgram;
190 iov[1].iov_len= qu->query_dglen;
ac868fa8 191 adns__sigpipe_protect(qu->ads);
3955725c 192 wr= writev(qu->ads->tcpsocket,iov,2);
ac868fa8 193 adns__sigpipe_unprotect(qu->ads);
e576be50 194 if (wr < 0) {
195 if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
196 errno == ENOBUFS || errno == ENOMEM)) {
197 adns__tcp_broken(ads,"write",strerror(errno));
198 return;
199 }
200 wr= 0;
201 }
202 }
203
204 if (wr<2) {
205 r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
206 wr= 0;
207 } else {
208 wr-= 2;
209 }
210 if (wr<qu->query_dglen) {
211 r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r);
212 }
213}
214
3955725c 215static void query_usetcp(adns_query qu, struct timeval now) {
f7f83b4a 216 qu->state= query_tcpw;
e576be50 217 qu->timeout= now;
f7f83b4a 218 timevaladd(&qu->timeout,TCPWAITMS);
219 LIST_LINK_TAIL(qu->ads->tcpw,qu);
220 adns__querysend_tcp(qu,now);
3955725c 221 adns__tcp_tryconnect(qu->ads,now);
e576be50 222}
223
d8c062fa 224void adns__query_send(adns_query qu, struct timeval now) {
e576be50 225 struct sockaddr_in servaddr;
226 int serv, r;
3955725c 227 adns_state ads;
e576be50 228
d8c062fa 229 assert(qu->state == query_tosend);
e576be50 230 if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
3955725c 231 query_usetcp(qu,now);
e576be50 232 return;
233 }
234
f7f83b4a 235 if (qu->retries >= UDPMAXRETRIES) {
3955725c 236 adns__query_fail(qu,adns_s_timeout);
e576be50 237 return;
238 }
239
240 serv= qu->udpnextserver;
241 memset(&servaddr,0,sizeof(servaddr));
3955725c 242
243 ads= qu->ads;
e576be50 244 servaddr.sin_family= AF_INET;
245 servaddr.sin_addr= ads->servers[serv].addr;
246 servaddr.sin_port= htons(DNS_PORT);
247
71a6ff46 248 r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
249 (const struct sockaddr*)&servaddr,sizeof(servaddr));
f7f83b4a 250 if (r<0 && errno == EMSGSIZE) { qu->retries= 0; query_usetcp(qu,now); return; }
e576be50 251 if (r<0) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
252
e576be50 253 qu->timeout= now;
f7f83b4a 254 timevaladd(&qu->timeout,UDPRETRYMS);
e576be50 255 qu->udpsent |= (1<<serv);
256 qu->udpnextserver= (serv+1)%ads->nservers;
f7f83b4a 257 qu->retries++;
258 LIST_LINK_TAIL(ads->udpw,qu);
e576be50 259}