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