7 * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
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)
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.
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.
31 adns_status
adns__mkquery(adns_state ads
, vbuf
*vb
, int *id_r
,
32 const char *owner
, int ol
,
33 const typeinfo
*typei
, adns_queryflags flags
) {
35 byte label
[255], *rqp
;
38 #define MKQUERY_ADDB(b) *rqp++= (b)
39 #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
42 if (!adns__vbuf_ensure(vb
,DNS_HDRSIZE
+strlen(owner
)+1+5))
43 return adns_s_nolocalmem
;
46 *id_r
= id
= (ads
->nextid
++) & 0x0ffff;
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
;
57 if (!*p
) return adns_s_invaliddomain
;
60 while (p
!=pe
&& (c
= *p
++)!='.') {
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
;
68 return adns_s_invaliddomain
;
70 } else if (!(c
= *p
++)) {
71 return adns_s_invaliddomain
;
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
;
81 if (ll
== sizeof(label
)) return adns_s_invaliddomain
;
84 if (!ll
) return adns_s_invaliddomain
;
85 if (nlabs
++ > 63) return adns_s_invaliddomain
;
87 memcpy(rqp
,label
,ll
); rqp
+= ll
;
91 MKQUERY_ADDW(typei
->type
& adns__rrt_typemask
); /* QTYPE */
92 MKQUERY_ADDW(DNS_CLASS_IN
); /* QCLASS=IN */
94 vb
->used
= rqp
- vb
->buf
;
95 assert(vb
->used
<= vb
->avail
);
100 void adns__query_tcp(adns_query qu
, struct timeval now
) {
106 if (qu
->ads
->tcpstate
!= server_ok
) return;
108 length
[0]= (qu
->query_dglen
&0x0ff00U
) >>8;
109 length
[1]= (qu
->query_dglen
&0x0ff);
112 if (!adns__vbuf_ensure(&ads
->tcpsend
,ads
->tcpsend
.used
+qu
->query_dglen
+2)) return;
114 timevaladd(&now
,TCPMS
);
116 qu
->state
= query_tcpsent
;
117 LIST_LINK_TAIL(ads
->timew
,qu
);
119 if (ads
->tcpsend
.used
) {
122 iov
[0].iov_base
= length
;
124 iov
[1].iov_base
= qu
->query_dgram
;
125 iov
[1].iov_len
= qu
->query_dglen
;
126 wr
= writev(qu
->ads
->tcpsocket
,iov
,2);
128 if (!(errno
== EAGAIN
|| errno
== EINTR
|| errno
== ENOSPC
||
129 errno
== ENOBUFS
|| errno
== ENOMEM
)) {
130 adns__tcp_broken(ads
,"write",strerror(errno
));
138 r
= adns__vbuf_append(&ads
->tcpsend
,length
,2-wr
); assert(r
);
143 if (wr
<qu
->query_dglen
) {
144 r
= adns__vbuf_append(&ads
->tcpsend
,qu
->query_dgram
+wr
,qu
->query_dglen
-wr
); assert(r
);
148 static void query_usetcp(adns_query qu
, struct timeval now
) {
149 timevaladd(&now
,TCPMS
);
151 qu
->state
= query_tcpwait
;
152 LIST_LINK_TAIL(qu
->ads
->timew
,qu
);
153 adns__query_tcp(qu
,now
);
154 adns__tcp_tryconnect(qu
->ads
,now
);
157 void adns__query_udp(adns_query qu
, struct timeval now
) {
158 struct sockaddr_in servaddr
;
162 assert(qu
->state
== query_udp
);
163 if ((qu
->flags
& adns_qf_usevc
) || (qu
->query_dglen
> DNS_MAXUDP
)) {
164 query_usetcp(qu
,now
);
168 if (qu
->udpretries
>= UDPMAXRETRIES
) {
169 adns__query_fail(qu
,adns_s_timeout
);
173 serv
= qu
->udpnextserver
;
174 memset(&servaddr
,0,sizeof(servaddr
));
177 servaddr
.sin_family
= AF_INET
;
178 servaddr
.sin_addr
= ads
->servers
[serv
].addr
;
179 servaddr
.sin_port
= htons(DNS_PORT
);
181 r
= sendto(ads
->udpsocket
,qu
->query_dgram
,qu
->query_dglen
,0,&servaddr
,sizeof(servaddr
));
182 if (r
<0 && errno
== EMSGSIZE
) { query_usetcp(qu
,now
); return; }
183 if (r
<0) adns__warn(ads
,serv
,0,"sendto failed: %s",strerror(errno
));
185 timevaladd(&now
,UDPRETRYMS
);
187 qu
->udpsent
|= (1<<serv
);
188 qu
->udpnextserver
= (serv
+1)%ads
->nservers
;
190 LIST_LINK_TAIL(ads
->timew
,qu
);