]>
Commit | Line | Data |
---|---|---|
e14f094b BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
d0166f4a | 3 | Copyright (C) 2002 - 2005 PowerDNS.COM BV |
e14f094b BH |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
d0166f4a BH |
6 | it under the terms of the GNU General Public License version 2 as |
7 | published by the Free Software Foundation | |
e14f094b BH |
8 | |
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. | |
13 | ||
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 | |
06bd9ccf | 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
e14f094b | 17 | */ |
36c5ee42 | 18 | #include "utility.hh" |
e14f094b BH |
19 | #include "lwres.hh" |
20 | #include <pthread.h> | |
21 | #include <semaphore.h> | |
22 | #include <iostream> | |
23 | #include <errno.h> | |
24 | #include "misc.hh" | |
25 | #include <algorithm> | |
26 | #include <sstream> | |
27 | #include <cstring> | |
28 | #include <string> | |
29 | #include <vector> | |
e14f094b BH |
30 | #include "dns.hh" |
31 | #include "qtype.hh" | |
e14f094b | 32 | #include "ahuexception.hh" |
e14f094b | 33 | #include "arguments.hh" |
5c633640 BH |
34 | #include "sstuff.hh" |
35 | #include "syncres.hh" | |
ea634573 BH |
36 | #include "dnswriter.hh" |
37 | #include "dnsparser.hh" | |
aab4adb0 | 38 | #include "logger.hh" |
e14f094b BH |
39 | |
40 | LWRes::LWRes() | |
41 | { | |
42 | d_sock=-1; | |
43 | d_timeout=500000; | |
b636533b BH |
44 | d_bufsize=1500; |
45 | d_buf=new unsigned char[d_bufsize]; | |
e14f094b BH |
46 | } |
47 | ||
48 | LWRes::~LWRes() | |
49 | { | |
50 | if(d_sock>=0) | |
51 | Utility::closesocket(d_sock); | |
52 | delete[] d_buf; | |
53 | } | |
54 | ||
55 | ||
56 | //! returns -1 for permanent error, 0 for timeout, 1 for success | |
57 | /** Never throws! */ | |
42724edf | 58 | int LWRes::asyncresolve(uint32_t ip, const char *domain, int type, bool doTCP, struct timeval* now) |
e14f094b | 59 | { |
2353fffa | 60 | d_ip=ip; |
ea634573 BH |
61 | vector<uint8_t> vpacket; |
62 | DNSPacketWriter pw(vpacket, domain, type); | |
63 | ||
64 | pw.getHeader()->rd=0; | |
8a63d3ce | 65 | pw.getHeader()->id=random(); |
e14f094b BH |
66 | d_domain=domain; |
67 | d_type=type; | |
68 | d_inaxfr=false; | |
36f5e3db | 69 | d_rcode=0; |
e14f094b | 70 | |
2353fffa | 71 | |
e14f094b | 72 | struct sockaddr_in toaddr; |
5c633640 | 73 | Utility::socklen_t addrlen=sizeof(toaddr); |
42724edf | 74 | toaddr.sin_addr.s_addr=htonl(ip); |
e14f094b BH |
75 | |
76 | toaddr.sin_port=htons(53); | |
77 | toaddr.sin_family=AF_INET; | |
78 | ||
eefd15f9 BH |
79 | int ret; |
80 | ||
81 | DTime dt; | |
5e3da48d | 82 | dt.setTimeval(*now); |
5c633640 BH |
83 | |
84 | if(!doTCP) { | |
ea634573 | 85 | if(asendto((const char*)&*vpacket.begin(), vpacket.size(), 0, (struct sockaddr*)(&toaddr), sizeof(toaddr), pw.getHeader()->id)<0) { |
5c633640 BH |
86 | return -1; |
87 | } | |
88 | ||
89 | // sleep until we see an answer to this, interface to mtasker | |
90 | ||
ea634573 | 91 | ret=arecvfrom(reinterpret_cast<char *>(d_buf), d_bufsize-1,0,(struct sockaddr*)(&toaddr), &addrlen, &d_len, pw.getHeader()->id); |
e14f094b | 92 | } |
5c633640 BH |
93 | else { |
94 | Socket s(InterNetwork, Stream); | |
42724edf | 95 | IPEndpoint ie(U32ToIP(ip), 53); |
5c633640 BH |
96 | s.setNonBlocking(); |
97 | s.connect(ie); | |
98 | ||
ea634573 | 99 | unsigned int len=htons(vpacket.size()); |
5c633640 | 100 | char *lenP=(char*)&len; |
ea634573 BH |
101 | const char *msgP=(const char*)&*vpacket.begin(); |
102 | string packet=string(lenP, lenP+2)+string(msgP, msgP+vpacket.size()); | |
5c633640 BH |
103 | |
104 | if(asendtcp(packet, &s) == 0) { | |
d0166f4a | 105 | return 0; |
5c633640 | 106 | } |
eefd15f9 | 107 | |
5c633640 BH |
108 | packet.clear(); |
109 | if(arecvtcp(packet,2, &s)==0) { | |
d0166f4a | 110 | return 0; |
5c633640 BH |
111 | } |
112 | ||
113 | memcpy(&len, packet.c_str(), 2); | |
114 | len=ntohs(len); | |
115 | ||
5c633640 | 116 | if(arecvtcp(packet, len, &s)==0) { |
d0166f4a | 117 | return 0; |
5c633640 | 118 | } |
ea634573 | 119 | if(len > (unsigned int)d_bufsize) { |
66ab6a63 BH |
120 | d_bufsize=len; |
121 | delete[] d_buf; | |
122 | d_buf = new unsigned char[d_bufsize]; | |
123 | } | |
5c633640 BH |
124 | memcpy(d_buf, packet.c_str(), len); |
125 | d_len=len; | |
126 | ret=1; | |
127 | } | |
128 | d_usec=dt.udiff(); | |
5e3da48d | 129 | *now=dt.getTimeval(); |
eefd15f9 | 130 | return ret; |
e14f094b BH |
131 | } |
132 | ||
133 | ||
eefd15f9 | 134 | LWRes::res_t LWRes::result() |
e14f094b | 135 | { |
c836dc19 | 136 | try { |
ea634573 BH |
137 | MOADNSParser mdp((const char*)d_buf, d_len); |
138 | // if(p.parse((char *)d_buf, d_len)<0) | |
139 | // throw LWResException("resolver: unable to parse packet of "+itoa(d_len)+" bytes"); | |
140 | d_aabit=mdp.d_header.aa; | |
141 | d_tcbit=mdp.d_header.tc; | |
142 | d_rcode=mdp.d_header.rcode; | |
143 | ||
50c79a76 | 144 | if(strcasecmp(d_domain.c_str(), mdp.d_qname.c_str())) { |
2353fffa BH |
145 | L<<Logger::Error<<"Packet purporting to come from remote server "<<U32ToIP(d_ip)<<" contained wrong answer: '" << d_domain << "' != '" << mdp.d_qname << "'" << endl; |
146 | g_stats.spoofedCount++; | |
147 | goto out; | |
148 | } | |
149 | ||
ea634573 BH |
150 | LWRes::res_t ret; |
151 | for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i!=mdp.d_answers.end(); ++i) { | |
152 | // cout<<i->first.d_place<<"\t"<<i->first.d_label<<"\tIN\t"<<DNSRecordContent::NumberToType(i->first.d_type); | |
153 | // cout<<"\t"<<i->first.d_ttl<<"\t"<< i->first.d_content->getZoneRepresentation()<<endl; | |
154 | DNSResourceRecord rr; | |
155 | rr.qtype=i->first.d_type; | |
156 | rr.qname=i->first.d_label; | |
157 | rr.ttl=i->first.d_ttl; | |
158 | rr.content=i->first.d_content->getZoneRepresentation(); // this should be the serialised form | |
ea634573 BH |
159 | rr.d_place=(DNSResourceRecord::Place) i->first.d_place; |
160 | ret.push_back(rr); | |
161 | } | |
162 | ||
163 | return ret; | |
164 | // return p.getAnswers(); | |
c836dc19 | 165 | } |
aab4adb0 BH |
166 | catch(exception &mde) { |
167 | if(::arg().mustDo("log-common-errors")) | |
7b1469bb | 168 | L<<Logger::Error<<"Unable to parse packet from remote server: "<<mde.what()<<endl; |
aab4adb0 | 169 | } |
c836dc19 | 170 | catch(...) { |
aab4adb0 | 171 | L<<Logger::Error<<"Unknown error parsing packet from remote server"<<endl; |
c836dc19 | 172 | } |
2353fffa | 173 | |
aab4adb0 | 174 | g_stats.serverParseError++; |
2353fffa BH |
175 | |
176 | out: | |
aab4adb0 BH |
177 | d_rcode=RCode::ServFail; |
178 | LWRes::res_t empty; | |
179 | return empty; | |
e14f094b BH |
180 | } |
181 |