e576be50 |
1 | /* |
2 | * transmit.c |
3 | * - construct queries |
4 | * - send queries |
5 | */ |
6 | /* |
a719a4be |
7 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
e576be50 |
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 | |
71a6ff46 |
27 | #include <sys/types.h> |
3955725c |
28 | #include <sys/uio.h> |
29 | |
e576be50 |
30 | #include "internal.h" |
31 | |
e062dcae |
32 | #define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used) |
e576be50 |
33 | #define MKQUERY_ADDB(b) *rqp++= (b) |
34 | #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff)) |
e062dcae |
35 | #define MKQUERY_STOP(vb) ((vb)->used= rqp-(vb)->buf) |
e576be50 |
36 | |
e062dcae |
37 | static adns_status mkquery_header(adns_state ads, vbuf *vb, int *id_r, int qdlen) { |
38 | int id; |
39 | byte *rqp; |
40 | |
ea1e31e3 |
41 | if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+qdlen+4)) return adns_s_nomemory; |
e576be50 |
42 | |
e062dcae |
43 | vb->used= 0; |
44 | MKQUERY_START(vb); |
45 | |
660d7d3b |
46 | *id_r= id= (ads->nextid++) & 0x0ffff; |
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; |
e9e53c73 |
86 | while (p!=pe) { |
e576be50 |
87 | ll= 0; |
88 | while (p!=pe && (c= *p++)!='.') { |
89 | if (c=='\\') { |
ea1e31e3 |
90 | if (!(flags & adns_qf_quoteok_query)) return adns_s_querydomaininvalid; |
e576be50 |
91 | if (ctype_digit(p[0])) { |
92 | if (ctype_digit(p[1]) && ctype_digit(p[2])) { |
93 | c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0'); |
ea1e31e3 |
94 | if (c >= 256) return adns_s_querydomaininvalid; |
e576be50 |
95 | } else { |
ea1e31e3 |
96 | return adns_s_querydomaininvalid; |
e576be50 |
97 | } |
98 | } else if (!(c= *p++)) { |
ea1e31e3 |
99 | return adns_s_querydomaininvalid; |
e576be50 |
100 | } |
101 | } |
828d89bd |
102 | if (!(flags & adns_qf_quoteok_query)) { |
ffbda80c |
103 | if (c == '-') { |
ea1e31e3 |
104 | if (!ll) return adns_s_querydomaininvalid; |
ffbda80c |
105 | } else if (!ctype_alpha(c) && !ctype_digit(c)) { |
ea1e31e3 |
106 | return adns_s_querydomaininvalid; |
e576be50 |
107 | } |
108 | } |
ea1e31e3 |
109 | if (ll == sizeof(label)) return adns_s_querydomaininvalid; |
e576be50 |
110 | label[ll++]= c; |
111 | } |
ea1e31e3 |
112 | if (!ll) return adns_s_querydomaininvalid; |
113 | if (nlabs++ > 63) return adns_s_querydomaintoolong; |
e576be50 |
114 | MKQUERY_ADDB(ll); |
115 | memcpy(rqp,label,ll); rqp+= ll; |
e9e53c73 |
116 | } |
e062dcae |
117 | MKQUERY_ADDB(0); |
118 | |
119 | MKQUERY_STOP(vb); |
120 | |
121 | st= mkquery_footer(vb,typei->type); |
122 | |
123 | return adns_s_ok; |
124 | } |
e576be50 |
125 | |
e062dcae |
126 | adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r, |
127 | const byte *qd_dgram, int qd_dglen, int qd_begin, |
128 | adns_rrtype type, adns_queryflags flags) { |
129 | byte *rqp; |
130 | findlabel_state fls; |
131 | int lablen, labstart; |
132 | adns_status st; |
133 | |
134 | st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st; |
135 | |
136 | MKQUERY_START(vb); |
137 | |
138 | adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0); |
139 | for (;;) { |
140 | st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st); |
141 | if (!lablen) break; |
142 | assert(lablen<255); |
143 | MKQUERY_ADDB(lablen); |
144 | memcpy(rqp,qd_dgram+labstart,lablen); |
145 | rqp+= lablen; |
146 | } |
e576be50 |
147 | MKQUERY_ADDB(0); |
e576be50 |
148 | |
e062dcae |
149 | MKQUERY_STOP(vb); |
150 | |
151 | st= mkquery_footer(vb,type); |
e576be50 |
152 | |
153 | return adns_s_ok; |
154 | } |
155 | |
3955725c |
156 | void adns__query_tcp(adns_query qu, struct timeval now) { |
e576be50 |
157 | byte length[2]; |
158 | struct iovec iov[2]; |
159 | int wr, r; |
3955725c |
160 | adns_state ads; |
e576be50 |
161 | |
3955725c |
162 | if (qu->ads->tcpstate != server_ok) return; |
e576be50 |
163 | |
164 | length[0]= (qu->query_dglen&0x0ff00U) >>8; |
165 | length[1]= (qu->query_dglen&0x0ff); |
3955725c |
166 | |
167 | ads= qu->ads; |
e576be50 |
168 | if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return; |
169 | |
170 | timevaladd(&now,TCPMS); |
171 | qu->timeout= now; |
172 | qu->state= query_tcpsent; |
e576be50 |
173 | |
174 | if (ads->tcpsend.used) { |
175 | wr= 0; |
176 | } else { |
177 | iov[0].iov_base= length; |
178 | iov[0].iov_len= 2; |
179 | iov[1].iov_base= qu->query_dgram; |
180 | iov[1].iov_len= qu->query_dglen; |
ac868fa8 |
181 | adns__sigpipe_protect(qu->ads); |
3955725c |
182 | wr= writev(qu->ads->tcpsocket,iov,2); |
ac868fa8 |
183 | adns__sigpipe_unprotect(qu->ads); |
e576be50 |
184 | if (wr < 0) { |
185 | if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC || |
186 | errno == ENOBUFS || errno == ENOMEM)) { |
187 | adns__tcp_broken(ads,"write",strerror(errno)); |
188 | return; |
189 | } |
190 | wr= 0; |
191 | } |
192 | } |
193 | |
194 | if (wr<2) { |
195 | r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r); |
196 | wr= 0; |
197 | } else { |
198 | wr-= 2; |
199 | } |
200 | if (wr<qu->query_dglen) { |
201 | r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r); |
202 | } |
203 | } |
204 | |
3955725c |
205 | static void query_usetcp(adns_query qu, struct timeval now) { |
e576be50 |
206 | timevaladd(&now,TCPMS); |
207 | qu->timeout= now; |
208 | qu->state= query_tcpwait; |
3955725c |
209 | LIST_LINK_TAIL(qu->ads->timew,qu); |
210 | adns__query_tcp(qu,now); |
211 | adns__tcp_tryconnect(qu->ads,now); |
e576be50 |
212 | } |
213 | |
d8c062fa |
214 | void adns__query_send(adns_query qu, struct timeval now) { |
e576be50 |
215 | struct sockaddr_in servaddr; |
216 | int serv, r; |
3955725c |
217 | adns_state ads; |
e576be50 |
218 | |
d8c062fa |
219 | assert(qu->state == query_tosend); |
e576be50 |
220 | if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) { |
3955725c |
221 | query_usetcp(qu,now); |
e576be50 |
222 | return; |
223 | } |
224 | |
225 | if (qu->udpretries >= UDPMAXRETRIES) { |
3955725c |
226 | adns__query_fail(qu,adns_s_timeout); |
e576be50 |
227 | return; |
228 | } |
229 | |
230 | serv= qu->udpnextserver; |
231 | memset(&servaddr,0,sizeof(servaddr)); |
3955725c |
232 | |
233 | ads= qu->ads; |
e576be50 |
234 | servaddr.sin_family= AF_INET; |
235 | servaddr.sin_addr= ads->servers[serv].addr; |
236 | servaddr.sin_port= htons(DNS_PORT); |
237 | |
71a6ff46 |
238 | r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0, |
239 | (const struct sockaddr*)&servaddr,sizeof(servaddr)); |
3955725c |
240 | if (r<0 && errno == EMSGSIZE) { query_usetcp(qu,now); return; } |
e576be50 |
241 | if (r<0) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno)); |
242 | |
243 | timevaladd(&now,UDPRETRYMS); |
244 | qu->timeout= now; |
245 | qu->udpsent |= (1<<serv); |
246 | qu->udpnextserver= (serv+1)%ads->nservers; |
247 | qu->udpretries++; |
248 | LIST_LINK_TAIL(ads->timew,qu); |
249 | } |