]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/lwres.cc
fix TCP related crashes due to HUUUUGE daum.net records (3.5kbyte!)
[thirdparty/pdns.git] / pdns / lwres.cc
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2005 PowerDNS.COM BV
4
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
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
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include "utility.hh"
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>
30 #include "dnspacket.hh"
31 #include "dns.hh"
32 #include "qtype.hh"
33 #include "tcpreceiver.hh"
34 #include "ahuexception.hh"
35 #include "statbag.hh"
36 #include "arguments.hh"
37 #include "sstuff.hh"
38 #include "syncres.hh"
39
40 LWRes::LWRes()
41 {
42 d_sock=-1;
43 d_timeout=500000;
44 d_bufsize=1500;
45 d_buf=new unsigned char[d_bufsize];
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! */
58 int LWRes::asyncresolve(const string &ip, const char *domain, int type, bool doTCP)
59 {
60 DNSPacket p;
61 p.setQuestion(Opcode::Query,domain,type);
62 p.setRD(false);
63 p.wrapup();
64
65 d_domain=domain;
66 d_type=type;
67 d_inaxfr=false;
68 d_rcode=0;
69
70 struct sockaddr_in toaddr;
71 struct in_addr inp;
72 Utility::socklen_t addrlen=sizeof(toaddr);
73 Utility::inet_aton(ip.c_str(),&inp);
74 toaddr.sin_addr.s_addr=inp.s_addr;
75
76 toaddr.sin_port=htons(53);
77 toaddr.sin_family=AF_INET;
78
79 int ret;
80
81 DTime dt;
82 dt.set();
83
84 if(!doTCP) {
85 if(asendto(p.getData(), p.len, 0, (struct sockaddr*)(&toaddr), sizeof(toaddr),p.d.id)<0) {
86 return -1;
87 }
88
89 // sleep until we see an answer to this, interface to mtasker
90
91 ret=arecvfrom(reinterpret_cast<char *>(d_buf), d_bufsize-1,0,(struct sockaddr*)(&toaddr), &addrlen, &d_len, p.d.id);
92 }
93 else {
94 // cerr<<"do tcp"<<endl;
95 Socket s(InterNetwork, Stream);
96 IPEndpoint ie(ip, 53);
97 s.setNonBlocking();
98 s.connect(ie);
99
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);
104
105 if(asendtcp(packet, &s) == 0) {
106 // cerr<<"asendtcp: timeout"<<endl;
107 return 0;
108 }
109
110 packet.clear();
111 if(arecvtcp(packet,2, &s)==0) {
112 // cerr<<"arecvtcp: timeout"<<endl;
113 return 0;
114 }
115
116 memcpy(&len, packet.c_str(), 2);
117 len=ntohs(len);
118
119 // cerr<<"Now reading "<<len<<" bytes"<<endl;
120
121 if(arecvtcp(packet, len, &s)==0) {
122 // cerr<<"arecvtcp: timeout"<<endl;
123 return 0;
124 }
125 if(len > d_bufsize) {
126 d_bufsize=len;
127 delete[] d_buf;
128 d_buf = new unsigned char[d_bufsize];
129 }
130 memcpy(d_buf, packet.c_str(), len);
131 d_len=len;
132 ret=1;
133 }
134 d_usec=dt.udiff();
135
136 return ret;
137 }
138
139
140 LWRes::res_t LWRes::result()
141 {
142 DNSPacket p;
143
144 try {
145 if(p.parse((char *)d_buf, d_len)<0)
146 throw LWResException("resolver: unable to parse packet of "+itoa(d_len)+" bytes");
147 d_aabit=p.d.aa;
148 d_tcbit=p.d.tc;
149 d_rcode=p.d.rcode;
150 return p.getAnswers();
151 }
152 catch(...) {
153 d_rcode=RCode::ServFail;
154 LWRes::res_t empty;
155 return empty;
156 }
157 }
158