]>
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 | */ | |
a4a74820 PL |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
f9cad4da | 25 | #include <boost/uuid/uuid.hpp> |
f9cad4da | 26 | |
f483cc6d RG |
27 | #include "iputils.hh" |
28 | #include "misc.hh" | |
4898a348 | 29 | #include "protobuf.hh" |
f483cc6d RG |
30 | #include "dns.hh" |
31 | #include "dnspcap.hh" | |
32 | #include "dnsparser.hh" | |
d9d3f9c1 | 33 | #include "protobuf.hh" |
d61aa945 | 34 | #include "uuid-utils.hh" |
f483cc6d RG |
35 | |
36 | #include "statbag.hh" | |
37 | StatBag S; | |
38 | ||
d9d3f9c1 | 39 | static void usage() |
a4a74820 PL |
40 | { |
41 | cerr<<"This program reads DNS queries and responses from a PCAP file and stores them into our protobuf format."<<endl; | |
42 | cerr<<"Usage: dnspcap2protobuf PCAPFILE OUTFILE"<<endl; | |
43 | } | |
44 | ||
f483cc6d | 45 | int main(int argc, char **argv) |
d7729968 | 46 | try { |
a4a74820 PL |
47 | for(int n=1 ; n < argc; ++n) { |
48 | if ((string) argv[n] == "--help") { | |
49 | usage(); | |
50 | return EXIT_SUCCESS; | |
51 | } | |
52 | ||
53 | if ((string) argv[n] == "--version") { | |
54 | cerr<<"dnspcap2protobuf "<<VERSION<<endl; | |
55 | return EXIT_SUCCESS; | |
56 | } | |
57 | } | |
58 | ||
2718cbf4 | 59 | if(argc < 3) { |
a4a74820 | 60 | usage(); |
f483cc6d RG |
61 | exit(EXIT_FAILURE); |
62 | } | |
63 | ||
a4a74820 | 64 | |
f483cc6d RG |
65 | PcapPacketReader pr(argv[1]); |
66 | ||
67 | FILE* fp = fopen(argv[2], "w"); | |
68 | if (!fp) { | |
69 | cerr<<"Error opening output file "<<argv[2]<<": "<<strerror(errno)<<endl; | |
70 | exit(EXIT_FAILURE); | |
71 | } | |
2718cbf4 | 72 | |
73 | int ind=0; | |
74 | if(argc==4) | |
75 | ind=atoi(argv[3]); | |
76 | ||
58307a85 | 77 | std::map<uint16_t,std::pair<boost::uuids::uuid,struct timeval> > ids; |
d7729968 RG |
78 | try { |
79 | while (pr.getUDPPacket()) { | |
80 | const dnsheader* dh=(dnsheader*)pr.d_payload; | |
81 | if (!dh->qdcount) | |
82 | continue; | |
83 | ||
84 | if (pr.d_len < sizeof(dnsheader)) | |
85 | continue; | |
86 | ||
87 | if(!dh->rd) | |
88 | continue; | |
89 | ||
90 | uint16_t qtype, qclass; | |
91 | DNSName qname; | |
92 | try { | |
93 | qname=DNSName((const char*)pr.d_payload, pr.d_len, sizeof(dnsheader), false, &qtype, &qclass); | |
94 | } | |
95 | catch(const std::exception& e) { | |
96 | cerr<<"Error while parsing qname: "<<e.what()<<endl; | |
97 | continue; | |
98 | } | |
f483cc6d | 99 | |
d9d3f9c1 | 100 | boost::uuids::uuid uniqueId; |
58307a85 RG |
101 | struct timeval queryTime = { 0, 0 }; |
102 | bool hasQueryTime = false; | |
d7729968 | 103 | if (!dh->qr) { |
58307a85 RG |
104 | queryTime.tv_sec = pr.d_pheader.ts.tv_sec; |
105 | queryTime.tv_usec = pr.d_pheader.ts.tv_usec; | |
d61aa945 | 106 | uniqueId = getUniqueID(); |
58307a85 | 107 | ids[dh->id] = std::make_pair(uniqueId, queryTime); |
d7729968 RG |
108 | } |
109 | else { | |
110 | const auto& it = ids.find(dh->id); | |
111 | if (it != ids.end()) { | |
58307a85 RG |
112 | uniqueId = it->second.first; |
113 | queryTime = it->second.second; | |
114 | hasQueryTime = true; | |
d9d3f9c1 RG |
115 | } |
116 | else { | |
d61aa945 | 117 | uniqueId = getUniqueID(); |
d7729968 | 118 | } |
f9cad4da | 119 | } |
ec469dd7 | 120 | |
d9d3f9c1 RG |
121 | const ComboAddress requestor = dh->qr ? pr.getDest() : pr.getSource(); |
122 | const ComboAddress responder = dh->qr ? pr.getSource() : pr.getDest(); | |
123 | *((char*)&requestor.sin4.sin_addr.s_addr)|=ind; | |
124 | *((char*)&responder.sin4.sin_addr.s_addr)|=ind; | |
125 | ||
126 | DNSProtoBufMessage message(dh->qr ? DNSProtoBufMessage::DNSProtoBufMessageType::Response : DNSProtoBufMessage::DNSProtoBufMessageType::Query, uniqueId, &requestor, &responder, qname, qtype, qclass, dh->id, false, pr.d_len); | |
127 | message.setTime(pr.d_pheader.ts.tv_sec, pr.d_pheader.ts.tv_usec); | |
128 | ||
129 | if (dh->qr) { | |
130 | message.setResponseCode(dh->rcode); | |
58307a85 RG |
131 | if (hasQueryTime) { |
132 | message.setQueryTime(queryTime.tv_sec, queryTime.tv_usec); | |
133 | } | |
134 | ||
d9d3f9c1 | 135 | try { |
165c9030 | 136 | message.addRRsFromPacket((const char*) dh, pr.d_len, true); |
d9d3f9c1 RG |
137 | } |
138 | catch(std::exception& e) | |
139 | { | |
140 | cerr<<"Error parsing response records: "<<e.what()<<endl; | |
141 | } | |
142 | catch(const PDNSException& e) | |
143 | { | |
144 | cerr<<"Error parsing response records: "<<e.reason<<endl; | |
145 | } | |
146 | } | |
3bde188c | 147 | |
d7729968 | 148 | std::string str; |
d9d3f9c1 RG |
149 | message.serialize(str); |
150 | ||
d7729968 RG |
151 | uint16_t mlen = htons(str.length()); |
152 | fwrite(&mlen, 1, sizeof(mlen), fp); | |
153 | fwrite(str.c_str(), 1, str.length(), fp); | |
154 | } | |
155 | } | |
156 | catch (const std::exception& e) { | |
157 | cerr<<"Error while parsing the PCAP file: "<<e.what()<<endl; | |
158 | fclose(fp); | |
159 | exit(EXIT_FAILURE); | |
f483cc6d | 160 | } |
d7729968 | 161 | |
f483cc6d RG |
162 | fclose(fp); |
163 | } | |
d7729968 RG |
164 | catch(const std::exception& e) { |
165 | cerr<<"Error opening PCAP file: "<<e.what()<<endl; | |
166 | exit(EXIT_FAILURE); | |
167 | } |