]>
Commit | Line | Data |
---|---|---|
52ebf44e RG |
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 | #ifdef HAVE_CONFIG_H | |
23 | #include "config.h" | |
24 | #endif | |
25 | ||
26 | #include <fstream> | |
27 | ||
28 | #include "iputils.hh" | |
29 | #include "misc.hh" | |
30 | #include "dns.hh" | |
31 | #include "dnspcap.hh" | |
32 | #include "dnsparser.hh" | |
33 | ||
34 | #include "statbag.hh" | |
35 | StatBag S; | |
36 | ||
37 | static void usage() | |
38 | { | |
39 | cerr<<"This program reads DNS queries from a PCAP file and outputs them in the calidns format."<<endl; | |
40 | cerr<<"Usage: dnspcap2calidns PCAPFILE OUTFILE"<<endl; | |
41 | } | |
42 | ||
43 | int main(int argc, char **argv) | |
44 | { | |
45 | try { | |
46 | for(int n=1 ; n < argc; ++n) { | |
47 | if ((string) argv[n] == "--help") { | |
48 | usage(); | |
49 | return EXIT_SUCCESS; | |
50 | } | |
51 | ||
52 | if ((string) argv[n] == "--version") { | |
53 | cerr<<"dnspcap2calidns "<<VERSION<<endl; | |
54 | return EXIT_SUCCESS; | |
55 | } | |
56 | } | |
57 | ||
58 | if(argc < 3) { | |
59 | usage(); | |
60 | exit(EXIT_FAILURE); | |
61 | } | |
62 | ||
63 | ||
64 | PcapPacketReader pr(argv[1]); | |
65 | ofstream fp(argv[2]); | |
66 | ||
67 | if (!fp) { | |
68 | cerr<<"Error opening output file "<<argv[2]<<": "<<strerror(errno)<<endl; | |
69 | exit(EXIT_FAILURE); | |
70 | } | |
71 | ||
72 | try { | |
73 | while (pr.getUDPPacket()) { | |
74 | if (pr.d_len < sizeof(dnsheader)) { | |
75 | continue; | |
76 | } | |
77 | ||
78 | const dnsheader* dh=reinterpret_cast<const dnsheader*>(pr.d_payload); | |
79 | if (!dh->qdcount) { | |
80 | continue; | |
81 | } | |
82 | ||
83 | if (!dh->rd) { | |
84 | continue; | |
85 | } | |
86 | ||
87 | if (dh->qr) { | |
88 | continue; | |
89 | } | |
90 | ||
91 | uint16_t qtype, qclass; | |
92 | DNSName qname; | |
93 | try { | |
94 | qname=DNSName(reinterpret_cast<const char*>(pr.d_payload), pr.d_len, sizeof(dnsheader), false, &qtype, &qclass); | |
95 | } | |
96 | catch(const std::exception& e) { | |
97 | cerr<<"Error while parsing qname: "<<e.what()<<endl; | |
98 | continue; | |
99 | } | |
100 | ||
101 | const ComboAddress requestor = pr.getSource(); | |
102 | ||
103 | fp << qname << " " << QType(qtype).getName() << " " << requestor.toString() << endl; | |
104 | } | |
105 | } | |
106 | catch (const std::exception& e) { | |
107 | cerr<<"Error while parsing the PCAP file: "<<e.what()<<endl; | |
108 | fp.close(); | |
109 | exit(EXIT_FAILURE); | |
110 | } | |
111 | ||
112 | fp.flush(); | |
113 | fp.close(); | |
114 | } | |
115 | catch(const std::exception& e) { | |
116 | cerr<<"Error opening PCAP file: "<<e.what()<<endl; | |
117 | exit(EXIT_FAILURE); | |
118 | } | |
119 | ||
120 | return EXIT_SUCCESS; | |
121 | } |