]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdemog.cc
Merge pull request #14195 from rgacogne/ddist-no-assertions
[thirdparty/pdns.git] / pdns / dnsdemog.cc
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 */
22 #define __FAVOR_BSD
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "statbag.hh"
27 #include "dnspcap.hh"
28 #include "dnsparser.hh"
29 #include <map>
30 #include <set>
31 #include <fstream>
32 #include <algorithm>
33 #include "anadns.hh"
34
35 #include "namespaces.hh"
36
37 StatBag S;
38
39 struct Entry
40 {
41 ComboAddress ip;
42 uint16_t port;
43 uint16_t id;
44
45 bool operator<(const struct Entry& rhs) const
46 {
47 return std::tie(ip, port, id) < std::tie(rhs.ip, rhs.port, rhs.id);
48 }
49 };
50
51
52 typedef map<Entry, uint32_t> emap_t;
53 emap_t ecount;
54
55 int main(int argc, char** argv)
56 try
57 {
58 cout << "begin;";
59 for(int n=1 ; n < argc; ++n) {
60 PcapPacketReader pr(argv[n]);
61
62 Entry entry;
63 while(pr.getUDPPacket()) {
64 if(ntohs(pr.d_udp->uh_dport)==53 && pr.d_len > 12) {
65 try {
66 dnsheader* dh= (dnsheader*) pr.d_payload;
67
68 if(dh->rd || dh->qr)
69 continue;
70
71 MOADNSParser mdp(false, (const char*)pr.d_payload, pr.d_len);
72
73 entry.ip = pr.getSource();
74 entry.port = pr.d_udp->uh_sport;
75 entry.id=dh->id;
76
77 cout << "insert into dnsstats (source, port, id, query, qtype, tstampSec, tstampUsec, arcount) values ('" << entry.ip.toString() <<"', "<< ntohs(entry.port) <<", "<< ntohs(dh->id);
78 cout <<", '"<<mdp.d_qname<<"', "<<mdp.d_qtype<<", " << pr.d_pheader.ts.tv_sec <<", " << pr.d_pheader.ts.tv_usec;
79 cout <<", "<< ntohs(dh->arcount) <<");\n";
80
81 }
82 catch(const MOADNSException &mde) {
83 // cerr<<"error parsing packet: "<<mde.what()<<endl;
84 continue;
85 }
86 catch(std::exception& e) {
87 cerr << e.what() << endl;
88 continue;
89 }
90 }
91 }
92 }
93 cout <<"commit;";
94 /*
95 for(emap_t::const_iterator i = ecount.begin(); i != ecount.end(); ++i) {
96 if(i->second > 1)
97 cout << U32ToIP(ntohl(i->first.ip)) <<":"<<ntohs(i->first.port)<<" -> "<<i->second <<endl;
98 }
99 */
100
101 }
102 catch(std::exception& e)
103 {
104 cerr<<"Fatal: "<<e.what()<<endl;
105 }