]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsdemog.cc
rec: ensure correct service user on debian
[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 <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
37 #include "namespaces.hh"
38
39 StatBag S;
40
41 struct Entry
42 {
43 ComboAddress ip;
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
54 typedef map<Entry, uint32_t> emap_t;
55 emap_t ecount;
56
57 int main(int argc, char** argv)
58 try
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) {
67 try {
68 dnsheader* dh= (dnsheader*) pr.d_payload;
69
70 if(dh->rd || dh->qr)
71 continue;
72
73 MOADNSParser mdp(false, (const char*)pr.d_payload, pr.d_len);
74
75 entry.ip = pr.getSource();
76 entry.port = pr.d_udp->uh_sport;
77 entry.id=dh->id;
78
79 cout << "insert into dnsstats (source, port, id, query, qtype, tstampSec, tstampUsec, arcount) values ('" << entry.ip.toString() <<"', "<< ntohs(entry.port) <<", "<< ntohs(dh->id);
80 cout <<", '"<<mdp.d_qname<<"', "<<mdp.d_qtype<<", " << pr.d_pheader.ts.tv_sec <<", " << pr.d_pheader.ts.tv_usec;
81 cout <<", "<< ntohs(dh->arcount) <<");\n";
82
83 }
84 catch(const MOADNSException &mde) {
85 // cerr<<"error parsing packet: "<<mde.what()<<endl;
86 continue;
87 }
88 catch(std::exception& e) {
89 cerr << e.what() << endl;
90 continue;
91 }
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 }
104 catch(std::exception& e)
105 {
106 cerr<<"Fatal: "<<e.what()<<endl;
107 }