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