unknown rr types seem to work
[adns] / src / transmit.c
1 /*
2 * transmit.c
3 * - construct queries
4 * - send queries
5 */
6 /*
7 * This file is
8 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
9 *
10 * It is part of adns, which is
11 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
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
29 #include <errno.h>
30
31 #include <sys/types.h>
32 #include <sys/uio.h>
33
34 #include "internal.h"
35 #include "tvarith.h"
36
37 #define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used)
38 #define MKQUERY_ADDB(b) *rqp++= (b)
39 #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
40 #define MKQUERY_STOP(vb) ((vb)->used= rqp-(vb)->buf)
41
42 static adns_status mkquery_header(adns_state ads, vbuf *vb,
43 int *id_r, int qdlen) {
44 int id;
45 byte *rqp;
46
47 if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+qdlen+4)) return adns_s_nomemory;
48
49 vb->used= 0;
50 MKQUERY_START(vb);
51
52 *id_r= id= (ads->nextid++) & 0x0ffff;
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 */
60
61 MKQUERY_STOP(vb);
62
63 return adns_s_ok;
64 }
65
66 static 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
78 adns_status adns__qdpl_normal(adns_state ads,
79 const char **p_io, const char *pe, int labelnum,
80 char label_r[], int *ll_io,
81 adns_queryflags flags,
82 const typeinfo *typei) {
83 int ll, c;
84 const char *p;
85
86 ll= 0;
87 p= *p_io;
88
89 while (p!=pe && (c= *p++)!='.') {
90 if (c=='\\') {
91 if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid;
92 if (ctype_digit(p[0])) {
93 if (p+1==pe || p+2==pe) return adns_s_querydomaininvalid;
94 if (ctype_digit(p[1]) && ctype_digit(p[2])) {
95 c= (*p++ - '0')*100;
96 c += (*p++ - '0')*10;
97 c += (*p++ - '0');
98 if (c >= 256) return adns_s_querydomaininvalid;
99 } else {
100 return adns_s_querydomaininvalid;
101 }
102 } else if (!(c= *p++)) {
103 return adns_s_querydomaininvalid;
104 }
105 }
106 if (!(flags & adns_qf_quoteok_query)) {
107 if (c == '-') {
108 if (!ll) return adns_s_querydomaininvalid;
109 } else if (!ctype_alpha(c) && !ctype_digit(c)) {
110 return adns_s_querydomaininvalid;
111 }
112 }
113 if (ll == *ll_io) return adns_s_querydomaininvalid;
114 label_r[ll++]= c;
115 }
116
117 *p_io= p;
118 *ll_io= ll;
119 return adns_s_ok;
120 }
121
122 adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r,
123 const char *owner, int ol,
124 const typeinfo *typei, adns_rrtype type,
125 adns_queryflags flags) {
126 int labelnum, ll, nbytes;
127 byte label[255];
128 byte *rqp;
129 const char *p, *pe;
130 adns_status st;
131
132 st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
133
134 MKQUERY_START(vb);
135
136 p= owner; pe= owner+ol;
137 nbytes= 0;
138 labelnum= 0;
139 while (p!=pe) {
140 ll= sizeof(label);
141 st= typei->qdparselabel(ads, &p,pe, labelnum++, label, &ll, flags, typei);
142 if (st) return st;
143 if (!ll) return adns_s_querydomaininvalid;
144 if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong;
145 nbytes+= ll+1;
146 if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
147 MKQUERY_ADDB(ll);
148 memcpy(rqp,label,ll); rqp+= ll;
149 }
150 MKQUERY_ADDB(0);
151
152 MKQUERY_STOP(vb);
153
154 st= mkquery_footer(vb,type);
155
156 return adns_s_ok;
157 }
158
159 adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r,
160 const byte *qd_dgram, int qd_dglen,
161 int qd_begin,
162 adns_rrtype type, adns_queryflags flags) {
163 byte *rqp;
164 findlabel_state fls;
165 int lablen, labstart;
166 adns_status st;
167
168 st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
169
170 MKQUERY_START(vb);
171
172 adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
173 for (;;) {
174 st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
175 if (!lablen) break;
176 assert(lablen<255);
177 MKQUERY_ADDB(lablen);
178 memcpy(rqp,qd_dgram+labstart,lablen);
179 rqp+= lablen;
180 }
181 MKQUERY_ADDB(0);
182
183 MKQUERY_STOP(vb);
184
185 st= mkquery_footer(vb,type);
186
187 return adns_s_ok;
188 }
189
190 void adns__querysend_tcp(adns_query qu, struct timeval now) {
191 byte length[2];
192 struct iovec iov[2];
193 int wr, r;
194 adns_state ads;
195
196 if (qu->ads->tcpstate != server_ok) return;
197
198 assert(qu->state == query_tcpw);
199
200 length[0]= (qu->query_dglen&0x0ff00U) >>8;
201 length[1]= (qu->query_dglen&0x0ff);
202
203 ads= qu->ads;
204 if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2))
205 return;
206
207 qu->retries++;
208
209 /* Reset idle timeout. */
210 ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
211
212 if (ads->tcpsend.used) {
213 wr= 0;
214 } else {
215 iov[0].iov_base= length;
216 iov[0].iov_len= 2;
217 iov[1].iov_base= qu->query_dgram;
218 iov[1].iov_len= qu->query_dglen;
219 adns__sigpipe_protect(qu->ads);
220 wr= writev(qu->ads->tcpsocket,iov,2);
221 adns__sigpipe_unprotect(qu->ads);
222 if (wr < 0) {
223 if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
224 errno == ENOBUFS || errno == ENOMEM)) {
225 adns__tcp_broken(ads,"write",strerror(errno));
226 return;
227 }
228 wr= 0;
229 }
230 }
231
232 if (wr<2) {
233 r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r);
234 wr= 0;
235 } else {
236 wr-= 2;
237 }
238 if (wr<qu->query_dglen) {
239 r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr);
240 assert(r);
241 }
242 }
243
244 static void query_usetcp(adns_query qu, struct timeval now) {
245 qu->state= query_tcpw;
246 qu->timeout= now;
247 timevaladd(&qu->timeout,TCPWAITMS);
248 LIST_LINK_TAIL(qu->ads->tcpw,qu);
249 adns__querysend_tcp(qu,now);
250 adns__tcp_tryconnect(qu->ads,now);
251 }
252
253 void adns__query_send(adns_query qu, struct timeval now) {
254 struct sockaddr_in servaddr;
255 int serv, r;
256 adns_state ads;
257
258 assert(qu->state == query_tosend);
259 if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
260 query_usetcp(qu,now);
261 return;
262 }
263
264 if (qu->retries >= UDPMAXRETRIES) {
265 adns__query_fail(qu,adns_s_timeout);
266 return;
267 }
268
269 serv= qu->udpnextserver;
270 memset(&servaddr,0,sizeof(servaddr));
271
272 ads= qu->ads;
273 servaddr.sin_family= AF_INET;
274 servaddr.sin_addr= ads->servers[serv].addr;
275 servaddr.sin_port= htons(DNS_PORT);
276
277 r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,
278 (const struct sockaddr*)&servaddr,sizeof(servaddr));
279 if (r<0 && errno == EMSGSIZE) {
280 qu->retries= 0;
281 query_usetcp(qu,now);
282 return;
283 }
284 if (r<0 && errno != EAGAIN)
285 adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno));
286
287 qu->timeout= now;
288 timevaladd(&qu->timeout,UDPRETRYMS);
289 qu->udpsent |= (1<<serv);
290 qu->udpnextserver= (serv+1)%ads->nservers;
291 qu->retries++;
292 LIST_LINK_TAIL(ads->udpw,qu);
293 }