]>
Commit | Line | Data |
---|---|---|
12471842 PL |
1 | /* |
2 | * This file is part of PowerDNS or dnsdist. | |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
830515d8 BH |
25 | #include <boost/accumulators/accumulators.hpp> |
26 | #include <boost/array.hpp> | |
27 | #include <boost/accumulators/statistics.hpp> | |
b3bd2e75 | 28 | #include <boost/program_options.hpp> |
4dd35bb1 | 29 | #include "inflighter.cc" |
a4382ef3 | 30 | #include <deque> |
4dd35bb1 BH |
31 | #include "namespaces.hh" |
32 | #include "dnsparser.hh" | |
33 | #include "sstuff.hh" | |
34 | #include "misc.hh" | |
35 | #include "dnswriter.hh" | |
36 | #include "dnsrecords.hh" | |
37 | ||
830515d8 | 38 | using namespace boost::accumulators; |
b3bd2e75 | 39 | namespace po = boost::program_options; |
de43ec0f | 40 | |
b3bd2e75 | 41 | po::variables_map g_vm; |
830515d8 | 42 | |
4dd35bb1 BH |
43 | StatBag S; |
44 | ||
b3bd2e75 PD |
45 | bool g_quiet=false; |
46 | bool g_envoutput=false; | |
47 | ||
4dd35bb1 BH |
48 | struct DNSResult |
49 | { | |
50 | vector<ComboAddress> ips; | |
51 | int rcode; | |
d712a557 | 52 | bool seenauthsoa; |
4dd35bb1 BH |
53 | }; |
54 | ||
62921444 BH |
55 | struct TypedQuery |
56 | { | |
57 | TypedQuery(const string& name_, uint16_t type_) : name(name_), type(type_){} | |
a9456e80 | 58 | DNSName name; |
62921444 BH |
59 | uint16_t type; |
60 | }; | |
830515d8 | 61 | |
4dd35bb1 BH |
62 | struct SendReceive |
63 | { | |
64 | typedef int Identifier; | |
65 | typedef DNSResult Answer; // ip | |
66 | int d_socket; | |
a4382ef3 | 67 | deque<uint16_t> d_idqueue; |
62921444 | 68 | |
830515d8 BH |
69 | typedef accumulator_set< |
70 | double | |
71 | , stats<boost::accumulators::tag::extended_p_square, | |
232f0877 | 72 | boost::accumulators::tag::median(with_p_square_quantile), |
830515d8 | 73 | boost::accumulators::tag::mean(immediate) |
232f0877 | 74 | > |
830515d8 BH |
75 | > acc_t; |
76 | acc_t* d_acc; | |
77 | ||
78 | boost::array<double, 11> d_probs; | |
79 | ||
80 | SendReceive(const std::string& remoteAddr, uint16_t port) | |
4dd35bb1 | 81 | { |
830515d8 BH |
82 | boost::array<double, 11> tmp ={{0.001,0.01, 0.025, 0.1, 0.25,0.5,0.75,0.9,0.975, 0.99,0.9999}}; |
83 | d_probs = tmp; | |
84 | d_acc = new acc_t(boost::accumulators::tag::extended_p_square::probabilities=d_probs); | |
85 | // | |
86 | //d_acc = acc_t | |
4dd35bb1 BH |
87 | d_socket = socket(AF_INET, SOCK_DGRAM, 0); |
88 | int val=1; | |
89 | setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); | |
90 | ||
910d27bb | 91 | ComboAddress remote(remoteAddr, port); |
4dd35bb1 | 92 | connect(d_socket, (struct sockaddr*)&remote, remote.getSocklen()); |
9cb47734 | 93 | d_oks = d_errors = d_nodatas = d_nxdomains = d_unknowns = 0; |
bf2aaa52 | 94 | d_receiveds = d_receiveerrors = d_senderrors = 0; |
04bbeaa0 | 95 | for(unsigned int id =0 ; id < std::numeric_limits<uint16_t>::max(); ++id) |
a4382ef3 | 96 | d_idqueue.push_back(id); |
4dd35bb1 BH |
97 | } |
98 | ||
99 | ~SendReceive() | |
100 | { | |
101 | close(d_socket); | |
102 | } | |
103 | ||
62921444 | 104 | Identifier send(TypedQuery& domain) |
4dd35bb1 BH |
105 | { |
106 | //cerr<<"Sending query for '"<<domain<<"'"<<endl; | |
107 | ||
108 | // send it, copy code from 'sdig' | |
109 | vector<uint8_t> packet; | |
110 | ||
62921444 | 111 | DNSPacketWriter pw(packet, domain.name, domain.type); |
bf2aaa52 BH |
112 | |
113 | if(d_idqueue.empty()) { | |
114 | cerr<<"Exhausted ids!"<<endl; | |
115 | exit(1); | |
116 | } | |
a4382ef3 BH |
117 | pw.getHeader()->id = d_idqueue.front(); |
118 | d_idqueue.pop_front(); | |
4dd35bb1 BH |
119 | pw.getHeader()->rd = 1; |
120 | pw.getHeader()->qr = 0; | |
121 | ||
bf2aaa52 BH |
122 | if(::send(d_socket, &*packet.begin(), packet.size(), 0) < 0) |
123 | d_senderrors++; | |
4dd35bb1 | 124 | |
a9456e80 | 125 | if(!g_quiet) |
126 | cout<<"Sent out query for '"<<domain.name<<"' with id "<<pw.getHeader()->id<<endl; | |
4dd35bb1 BH |
127 | return pw.getHeader()->id; |
128 | } | |
129 | ||
130 | bool receive(Identifier& id, DNSResult& dr) | |
131 | { | |
132 | if(waitForData(d_socket, 0, 500000) > 0) { | |
133 | char buf[512]; | |
9cb47734 | 134 | |
4dd35bb1 | 135 | int len = recv(d_socket, buf, sizeof(buf), 0); |
9cb47734 BH |
136 | if(len < 0) { |
137 | d_receiveerrors++; | |
4dd35bb1 | 138 | return 0; |
9cb47734 BH |
139 | } |
140 | else { | |
141 | d_receiveds++; | |
142 | } | |
4dd35bb1 BH |
143 | // parse packet, set 'id', fill out 'ip' |
144 | ||
145 | MOADNSParser mdp(string(buf, len)); | |
b3bd2e75 | 146 | if(!g_quiet) { |
c48816ca | 147 | cout<<"Reply to question for qname='"<<mdp.d_qname<<"', qtype="<<DNSRecordContent::NumberToType(mdp.d_qtype)<<endl; |
b3bd2e75 PD |
148 | cout<<"Rcode: "<<mdp.d_header.rcode<<", RD: "<<mdp.d_header.rd<<", QR: "<<mdp.d_header.qr; |
149 | cout<<", TC: "<<mdp.d_header.tc<<", AA: "<<mdp.d_header.aa<<", opcode: "<<mdp.d_header.opcode<<endl; | |
150 | } | |
4dd35bb1 BH |
151 | dr.rcode = mdp.d_header.rcode; |
152 | for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i!=mdp.d_answers.end(); ++i) { | |
62921444 | 153 | if(i->first.d_place == 1 && i->first.d_type == mdp.d_qtype) |
4dd35bb1 | 154 | dr.ips.push_back(ComboAddress(i->first.d_content->getZoneRepresentation())); |
9cb47734 | 155 | if(i->first.d_place == 2 && i->first.d_type == QType::SOA) { |
d712a557 BH |
156 | dr.seenauthsoa = 1; |
157 | } | |
b3bd2e75 PD |
158 | if(!g_quiet) |
159 | { | |
c48816ca | 160 | cout<<i->first.d_place-1<<"\t"<<i->first.d_name<<"\tIN\t"<<DNSRecordContent::NumberToType(i->first.d_type); |
b3bd2e75 PD |
161 | cout<<"\t"<<i->first.d_ttl<<"\t"<< i->first.d_content->getZoneRepresentation()<<"\n"; |
162 | } | |
4dd35bb1 | 163 | } |
d712a557 | 164 | |
4dd35bb1 | 165 | id = mdp.d_header.id; |
bf2aaa52 | 166 | d_idqueue.push_back(id); |
d712a557 | 167 | |
4dd35bb1 BH |
168 | return 1; |
169 | } | |
170 | return 0; | |
171 | } | |
172 | ||
a4382ef3 BH |
173 | void deliverTimeout(const Identifier& id) |
174 | { | |
a9456e80 | 175 | if(!g_quiet) { |
176 | cout<<"Timeout for id "<<id<<endl; | |
177 | } | |
a4382ef3 BH |
178 | d_idqueue.push_back(id); |
179 | } | |
180 | ||
62921444 | 181 | void deliverAnswer(TypedQuery& domain, const DNSResult& dr, unsigned int usec) |
4dd35bb1 | 182 | { |
830515d8 BH |
183 | (*d_acc)(usec/1000.0); |
184 | // if(usec > 1000000) | |
185 | // cerr<<"Slow: "<<domain<<" ("<<usec/1000.0<<" msec)\n"; | |
b3bd2e75 | 186 | if(!g_quiet) { |
62921444 | 187 | cout<<domain.name<<"|"<<DNSRecordContent::NumberToType(domain.type)<<": ("<<usec/1000.0<<"msec) rcode: "<<dr.rcode; |
ef7cd021 | 188 | for(const ComboAddress& ca : dr.ips) { |
b3bd2e75 PD |
189 | cout<<", "<<ca.toString(); |
190 | } | |
191 | cout<<endl; | |
4dd35bb1 | 192 | } |
d712a557 BH |
193 | if(dr.rcode == RCode::NXDomain) { |
194 | d_nxdomains++; | |
195 | } | |
196 | else if(dr.rcode) { | |
910d27bb | 197 | d_errors++; |
d712a557 BH |
198 | } |
199 | else if(dr.ips.empty() && dr.seenauthsoa) | |
200 | d_nodatas++; | |
910d27bb BH |
201 | else if(!dr.ips.empty()) |
202 | d_oks++; | |
9cb47734 | 203 | else { |
b3bd2e75 | 204 | if(!g_quiet) cout<<"UNKNOWN!! ^^"<<endl; |
9cb47734 BH |
205 | d_unknowns++; |
206 | } | |
4dd35bb1 | 207 | } |
9cb47734 | 208 | unsigned int d_errors, d_nxdomains, d_nodatas, d_oks, d_unknowns; |
bf2aaa52 | 209 | unsigned int d_receiveds, d_receiveerrors, d_senderrors; |
4dd35bb1 BH |
210 | }; |
211 | ||
d2c701a0 PL |
212 | void usage(po::options_description &desc) { |
213 | cerr << "Usage: dnsbulktest [OPTION].. IPADDRESS PORTNUMBER [LIMIT]"<<endl; | |
214 | cerr << desc << "\n"; | |
215 | } | |
216 | ||
910d27bb | 217 | int main(int argc, char** argv) |
fc9152ee | 218 | try |
4dd35bb1 | 219 | { |
b3bd2e75 PD |
220 | po::options_description desc("Allowed options"); |
221 | desc.add_options() | |
222 | ("help,h", "produce help message") | |
223 | ("quiet,q", "be quiet about individual queries") | |
62921444 | 224 | ("type,t", po::value<string>()->default_value("A"), "What type to query for") |
b3bd2e75 | 225 | ("envoutput,e", "write report in shell environment format") |
d2c701a0 | 226 | ("version", "show the version number") |
b3bd2e75 PD |
227 | ; |
228 | ||
229 | po::options_description alloptions; | |
230 | po::options_description hidden("hidden options"); | |
231 | hidden.add_options() | |
232 | ("ip-address", po::value<string>(), "ip-address") | |
233 | ("portnumber", po::value<uint16_t>(), "portnumber") | |
234 | ("limit", po::value<uint32_t>()->default_value(0), "limit"); | |
235 | ||
236 | alloptions.add(desc).add(hidden); | |
237 | po::positional_options_description p; | |
238 | p.add("ip-address", 1); | |
239 | p.add("portnumber", 1); | |
240 | p.add("limit", 1); | |
241 | ||
242 | po::store(po::command_line_parser(argc, argv).options(alloptions).positional(p).run(), g_vm); | |
243 | po::notify(g_vm); | |
244 | ||
245 | if (g_vm.count("help")) { | |
d2c701a0 | 246 | usage(desc); |
b3bd2e75 | 247 | return EXIT_SUCCESS; |
ac379ae2 | 248 | } |
d2c701a0 PL |
249 | |
250 | if (g_vm.count("version")) { | |
251 | cerr<<"dnsbulktest "<<VERSION<<endl; | |
252 | return EXIT_SUCCESS; | |
253 | } | |
254 | ||
b3bd2e75 PD |
255 | if(!g_vm.count("portnumber")) { |
256 | cerr<<"Fatal, need to specify ip-address and portnumber"<<endl; | |
d2c701a0 | 257 | usage(desc); |
b3bd2e75 PD |
258 | return EXIT_FAILURE; |
259 | } | |
260 | ||
62921444 BH |
261 | g_quiet = g_vm.count("quiet") > 0; |
262 | g_envoutput = g_vm.count("envoutput") > 0; | |
263 | uint16_t qtype; | |
264 | reportAllTypes(); | |
265 | try { | |
266 | qtype = DNSRecordContent::TypeToNumber(g_vm["type"].as<string>()); | |
267 | } | |
268 | catch(std::exception& e) { | |
269 | cerr << e.what() << endl; | |
270 | return EXIT_FAILURE; | |
271 | } | |
b3bd2e75 PD |
272 | |
273 | SendReceive sr(g_vm["ip-address"].as<string>(), g_vm["portnumber"].as<uint16_t>()); | |
274 | unsigned int limit = g_vm["limit"].as<unsigned int>(); | |
910d27bb | 275 | |
62921444 | 276 | vector<TypedQuery> domains; |
4dd35bb1 | 277 | |
62921444 | 278 | Inflighter<vector<TypedQuery>, SendReceive> inflighter(domains, sr); |
0eafd0fe | 279 | inflighter.d_maxInFlight = 1000; |
bf2aaa52 | 280 | inflighter.d_timeoutSeconds = 3; |
0eafd0fe | 281 | inflighter.d_burst = 100; |
4dd35bb1 BH |
282 | string line; |
283 | ||
284 | pair<string, string> split; | |
013f96e9 | 285 | string::size_type pos; |
4dd35bb1 | 286 | while(stringfgets(stdin, line)) { |
910d27bb BH |
287 | if(limit && domains.size() >= limit) |
288 | break; | |
246ccf7b | 289 | |
4dd35bb1 | 290 | trim_right(line); |
246ccf7b KM |
291 | if(line.empty() || line[0] == '#') |
292 | continue; | |
4dd35bb1 | 293 | split=splitField(line,','); |
246ccf7b KM |
294 | if (split.second.empty()) |
295 | split=splitField(line,'\t'); | |
296 | if(!split.second.find('.')) // skip 'Hidden profile' in quantcast list. | |
297 | continue; | |
013f96e9 | 298 | pos=split.second.find('/'); |
246ccf7b | 299 | if(pos != string::npos) // alexa has whole urls in the list now. |
013f96e9 | 300 | split.second.resize(pos); |
6fc9c5ea BH |
301 | if(find_if(split.second.begin(), split.second.end(), isalpha) == split.second.end()) |
302 | { | |
303 | continue; // this was an IP address | |
304 | } | |
62921444 BH |
305 | domains.push_back(TypedQuery(split.second, qtype)); |
306 | domains.push_back(TypedQuery("www."+split.second, qtype)); | |
4dd35bb1 BH |
307 | } |
308 | cerr<<"Read "<<domains.size()<<" domains!"<<endl; | |
d712a557 | 309 | random_shuffle(domains.begin(), domains.end()); |
4dd35bb1 | 310 | |
bf2aaa52 BH |
311 | boost::format datafmt("%s %|20t|%+15s %|40t|%s %|60t|%+15s\n"); |
312 | ||
4dd35bb1 BH |
313 | for(;;) { |
314 | try { | |
315 | inflighter.run(); | |
316 | break; | |
317 | } | |
830515d8 | 318 | catch(std::exception& e) { |
4dd35bb1 BH |
319 | cerr<<"Caught exception: "<<e.what()<<endl; |
320 | } | |
321 | } | |
9cb47734 | 322 | |
bf2aaa52 BH |
323 | cerr<< datafmt % "Sending" % "" % "Receiving" % ""; |
324 | cerr<< datafmt % " Queued " % domains.size() % " Received" % sr.d_receiveds; | |
325 | cerr<< datafmt % " Error -/-" % sr.d_senderrors % " Timeouts" % inflighter.getTimeouts(); | |
326 | cerr<< datafmt % " " % "" % " Unexpected" % inflighter.getUnexpecteds(); | |
327 | ||
328 | cerr<< datafmt % " Sent" % (domains.size() - sr.d_senderrors) % " Total" % (sr.d_receiveds + inflighter.getTimeouts() + inflighter.getUnexpecteds()); | |
329 | ||
330 | cerr<<endl; | |
331 | cerr<< datafmt % "DNS Status" % "" % "" % ""; | |
332 | cerr<< datafmt % " OK" % sr.d_oks % "" % ""; | |
333 | cerr<< datafmt % " Error" % sr.d_errors % "" % ""; | |
334 | cerr<< datafmt % " No Data" % sr.d_nodatas % "" % ""; | |
335 | cerr<< datafmt % " NXDOMAIN" % sr.d_nxdomains % "" % ""; | |
336 | cerr<< datafmt % " Unknowns" % sr.d_unknowns % "" % ""; | |
337 | cerr<< datafmt % "Answers" % (sr.d_oks + sr.d_errors + sr.d_nodatas + sr.d_nxdomains + sr.d_unknowns) % "" % ""; | |
338 | cerr<< datafmt % " Timeouts " % (inflighter.getTimeouts()) % "" % ""; | |
339 | cerr<< datafmt % "Total " % (sr.d_oks + sr.d_errors + sr.d_nodatas + sr.d_nxdomains + sr.d_unknowns + inflighter.getTimeouts()) % "" % ""; | |
830515d8 BH |
340 | |
341 | cerr<<"\n"; | |
342 | cerr<< "Mean response time: "<<mean(*sr.d_acc) << " msec"<<", median: "<<median(*sr.d_acc)<< " msec\n"; | |
830515d8 | 343 | |
d497340c | 344 | boost::format statfmt("Time < %6.03f msec %|30t|%6.03f%% cumulative\n"); |
830515d8 BH |
345 | |
346 | for (unsigned int i = 0; i < sr.d_probs.size(); ++i) { | |
347 | cerr << statfmt % extended_p_square(*sr.d_acc)[i] % (100*sr.d_probs[i]); | |
348 | } | |
349 | ||
b3bd2e75 PD |
350 | if(g_envoutput) { |
351 | cout<<"DBT_QUEUED="<<domains.size()<<endl; | |
352 | cout<<"DBT_SENDERRORS="<<sr.d_senderrors<<endl; | |
353 | cout<<"DBT_RECEIVED="<<sr.d_receiveds<<endl; | |
354 | cout<<"DBT_TIMEOUTS="<<inflighter.getTimeouts()<<endl; | |
355 | cout<<"DBT_UNEXPECTEDS="<<inflighter.getUnexpecteds()<<endl; | |
bbb6ce1f | 356 | cout<<"DBT_OKPERCENTAGE="<<((float)sr.d_oks/domains.size()*100)<<endl; |
dfddcdaf | 357 | cout<<"DBT_OKPERCENTAGEINT="<<(int)((float)sr.d_oks/domains.size()*100)<<endl; |
b3bd2e75 | 358 | } |
4dd35bb1 | 359 | } |
fc9152ee | 360 | catch(PDNSException& pe) |
361 | { | |
362 | cerr<<"Fatal error: "<<pe.reason<<endl; | |
363 | exit(EXIT_FAILURE); | |
364 | } |