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