98db6da3 |
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 | |
11c8bf9b |
24 | #include <errno.h> |
25 | #include <string.h> |
26 | |
27 | #include <sys/uio.h> |
28 | |
98db6da3 |
29 | #include "internal.h" |
30 | |
11c8bf9b |
31 | adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r, |
32 | const char *owner, int ol, |
98db6da3 |
33 | const typeinfo *typei, adns_queryflags flags) { |
34 | int ll, c, nlabs, id; |
35 | byte label[255], *rqp; |
36 | const char *p, *pe; |
37 | |
38 | #define MKQUERY_ADDB(b) *rqp++= (b) |
39 | #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff)) |
40 | |
41 | vb->used= 0; |
42 | if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+strlen(owner)+1+5)) |
43 | return adns_s_nolocalmem; |
44 | rqp= vb->buf; |
45 | |
46 | *id_r= id= (ads->nextid++) & 0x0ffff; |
47 | |
48 | MKQUERY_ADDW(id); |
49 | MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */ |
50 | MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */ |
51 | MKQUERY_ADDW(1); /* QDCOUNT=1 */ |
52 | MKQUERY_ADDW(0); /* ANCOUNT=0 */ |
53 | MKQUERY_ADDW(0); /* NSCOUNT=0 */ |
54 | MKQUERY_ADDW(0); /* ARCOUNT=0 */ |
55 | p= owner; pe= owner+ol; |
56 | nlabs= 0; |
57 | if (!*p) return adns_s_invaliddomain; |
58 | do { |
59 | ll= 0; |
60 | while (p!=pe && (c= *p++)!='.') { |
61 | if (c=='\\') { |
62 | if (!(flags & adns_qf_anyquote)) return adns_s_invaliddomain; |
63 | if (ctype_digit(p[0])) { |
64 | if (ctype_digit(p[1]) && ctype_digit(p[2])) { |
65 | c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0'); |
66 | if (c >= 256) return adns_s_invaliddomain; |
67 | } else { |
68 | return adns_s_invaliddomain; |
69 | } |
70 | } else if (!(c= *p++)) { |
71 | return adns_s_invaliddomain; |
72 | } |
73 | } |
74 | if (!(flags & adns_qf_anyquote)) { |
75 | if (ctype_digit(c) || c == '-') { |
76 | if (!ll) return adns_s_invaliddomain; |
77 | } else if (!ctype_alpha(c)) { |
78 | return adns_s_invaliddomain; |
79 | } |
80 | } |
81 | if (ll == sizeof(label)) return adns_s_invaliddomain; |
82 | label[ll++]= c; |
83 | } |
84 | if (!ll) return adns_s_invaliddomain; |
85 | if (nlabs++ > 63) return adns_s_invaliddomain; |
86 | MKQUERY_ADDB(ll); |
87 | memcpy(rqp,label,ll); rqp+= ll; |
88 | } while (p!=pe); |
89 | |
90 | MKQUERY_ADDB(0); |
91 | MKQUERY_ADDW(typei->type & adns__rrt_typemask); /* QTYPE */ |
92 | MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */ |
93 | |
94 | vb->used= rqp - vb->buf; |
95 | assert(vb->used <= vb->avail); |
96 | |
97 | return adns_s_ok; |
98 | } |
99 | |
11c8bf9b |
100 | void adns__query_tcp(adns_query qu, struct timeval now) { |
98db6da3 |
101 | byte length[2]; |
102 | struct iovec iov[2]; |
103 | int wr, r; |
11c8bf9b |
104 | adns_state ads; |
98db6da3 |
105 | |
11c8bf9b |
106 | if (qu->ads->tcpstate != server_ok) return; |
98db6da3 |
107 | |
108 | length[0]= (qu->query_dglen&0x0ff00U) >>8; |
109 | length[1]= (qu->query_dglen&0x0ff); |
11c8bf9b |
110 | |
111 | ads= qu->ads; |
98db6da3 |
112 | if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return; |
113 | |
114 | timevaladd(&now,TCPMS); |
115 | qu->timeout= now; |
116 | qu->state= query_tcpsent; |
117 | LIST_LINK_TAIL(ads->timew,qu); |
118 | |
119 | if (ads->tcpsend.used) { |
120 | wr= 0; |
121 | } else { |
122 | iov[0].iov_base= length; |
123 | iov[0].iov_len= 2; |
124 | iov[1].iov_base= qu->query_dgram; |
125 | iov[1].iov_len= qu->query_dglen; |
11c8bf9b |
126 | wr= writev(qu->ads->tcpsocket,iov,2); |
98db6da3 |
127 | if (wr < 0) { |
128 | if (!(errno == EAGAIN || errno == EINTR || errno == ENOSPC || |
129 | errno == ENOBUFS || errno == ENOMEM)) { |
130 | adns__tcp_broken(ads,"write",strerror(errno)); |
131 | return; |
132 | } |
133 | wr= 0; |
134 | } |
135 | } |
136 | |
137 | if (wr<2) { |
138 | r= adns__vbuf_append(&ads->tcpsend,length,2-wr); assert(r); |
139 | wr= 0; |
140 | } else { |
141 | wr-= 2; |
142 | } |
143 | if (wr<qu->query_dglen) { |
144 | r= adns__vbuf_append(&ads->tcpsend,qu->query_dgram+wr,qu->query_dglen-wr); assert(r); |
145 | } |
146 | } |
147 | |
11c8bf9b |
148 | static void query_usetcp(adns_query qu, struct timeval now) { |
98db6da3 |
149 | timevaladd(&now,TCPMS); |
150 | qu->timeout= now; |
151 | qu->state= query_tcpwait; |
11c8bf9b |
152 | LIST_LINK_TAIL(qu->ads->timew,qu); |
153 | adns__query_tcp(qu,now); |
154 | adns__tcp_tryconnect(qu->ads,now); |
98db6da3 |
155 | } |
156 | |
11c8bf9b |
157 | void adns__query_udp(adns_query qu, struct timeval now) { |
98db6da3 |
158 | struct sockaddr_in servaddr; |
159 | int serv, r; |
11c8bf9b |
160 | adns_state ads; |
98db6da3 |
161 | |
162 | assert(qu->state == query_udp); |
163 | if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) { |
11c8bf9b |
164 | query_usetcp(qu,now); |
98db6da3 |
165 | return; |
166 | } |
167 | |
168 | if (qu->udpretries >= UDPMAXRETRIES) { |
11c8bf9b |
169 | adns__query_fail(qu,adns_s_timeout); |
98db6da3 |
170 | return; |
171 | } |
172 | |
173 | serv= qu->udpnextserver; |
174 | memset(&servaddr,0,sizeof(servaddr)); |
11c8bf9b |
175 | |
176 | ads= qu->ads; |
98db6da3 |
177 | servaddr.sin_family= AF_INET; |
178 | servaddr.sin_addr= ads->servers[serv].addr; |
179 | servaddr.sin_port= htons(DNS_PORT); |
180 | |
181 | r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,&servaddr,sizeof(servaddr)); |
11c8bf9b |
182 | if (r<0 && errno == EMSGSIZE) { query_usetcp(qu,now); return; } |
98db6da3 |
183 | if (r<0) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno)); |
184 | |
185 | timevaladd(&now,UDPRETRYMS); |
186 | qu->timeout= now; |
187 | qu->udpsent |= (1<<serv); |
188 | qu->udpnextserver= (serv+1)%ads->nservers; |
189 | qu->udpretries++; |
190 | LIST_LINK_TAIL(ads->timew,qu); |
191 | } |