]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lwres.cc
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2005 PowerDNS.COM BV
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <semaphore.h>
30 #include "dnspacket.hh"
33 #include "tcpreceiver.hh"
34 #include "ahuexception.hh"
36 #include "arguments.hh"
45 d_buf
=new unsigned char[d_bufsize
];
51 Utility::closesocket(d_sock
);
56 //! returns -1 for permanent error, 0 for timeout, 1 for success
58 int LWRes::asyncresolve(const string
&ip
, const char *domain
, int type
, bool doTCP
)
61 p
.setQuestion(Opcode::Query
,domain
,type
);
70 struct sockaddr_in toaddr
;
72 Utility::socklen_t addrlen
=sizeof(toaddr
);
73 Utility::inet_aton(ip
.c_str(),&inp
);
74 toaddr
.sin_addr
.s_addr
=inp
.s_addr
;
76 toaddr
.sin_port
=htons(53);
77 toaddr
.sin_family
=AF_INET
;
85 if(asendto(p
.getData(), p
.len
, 0, (struct sockaddr
*)(&toaddr
), sizeof(toaddr
),p
.d
.id
)<0) {
89 // sleep until we see an answer to this, interface to mtasker
91 ret
=arecvfrom(reinterpret_cast<char *>(d_buf
), d_bufsize
-1,0,(struct sockaddr
*)(&toaddr
), &addrlen
, &d_len
, p
.d
.id
);
94 // cerr<<"do tcp"<<endl;
95 Socket
s(InterNetwork
, Stream
);
96 IPEndpoint
ie(ip
, 53);
100 unsigned int len
=htons(p
.len
);
101 char *lenP
=(char*)&len
;
102 const char *msgP
=p
.getData();
103 string packet
=string(lenP
, lenP
+2)+string(msgP
, msgP
+p
.len
);
105 if(asendtcp(packet
, &s
) == 0) {
106 // cerr<<"asendtcp: timeout"<<endl;
111 if(arecvtcp(packet
,2, &s
)==0) {
112 // cerr<<"arecvtcp: timeout"<<endl;
116 memcpy(&len
, packet
.c_str(), 2);
119 // cerr<<"Now reading "<<len<<" bytes"<<endl;
121 if(arecvtcp(packet
, len
, &s
)==0) {
122 // cerr<<"arecvtcp: timeout"<<endl;
125 if(len
> d_bufsize
) {
128 d_buf
= new unsigned char[d_bufsize
];
130 memcpy(d_buf
, packet
.c_str(), len
);
140 LWRes::res_t
LWRes::result()
145 if(p
.parse((char *)d_buf
, d_len
)<0)
146 throw LWResException("resolver: unable to parse packet of "+itoa(d_len
)+" bytes");
150 return p
.getAnswers();
153 d_rcode
=RCode::ServFail
;