]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnspcap2protobuf.cc
dnsdist: Add HTTPStatusAction to return a specific HTTP response
[thirdparty/pdns.git] / pdns / dnspcap2protobuf.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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <boost/uuid/uuid.hpp>
26
27 #include "iputils.hh"
28 #include "misc.hh"
29 #include "protobuf.hh"
30 #include "dns.hh"
31 #include "dnspcap.hh"
32 #include "dnsparser.hh"
33 #include "protobuf.hh"
34 #include "uuid-utils.hh"
35
36 #include "statbag.hh"
37 StatBag S;
38
39 static void usage()
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
45 int main(int argc, char **argv)
46 try {
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
59 if(argc < 3) {
60 usage();
61 exit(EXIT_FAILURE);
62 }
63
64
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 }
72
73 int ind=0;
74 if(argc==4)
75 ind=atoi(argv[3]);
76
77 std::map<uint16_t,std::pair<boost::uuids::uuid,struct timeval> > ids;
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 }
99
100 boost::uuids::uuid uniqueId;
101 struct timeval queryTime = { 0, 0 };
102 bool hasQueryTime = false;
103 if (!dh->qr) {
104 queryTime.tv_sec = pr.d_pheader.ts.tv_sec;
105 queryTime.tv_usec = pr.d_pheader.ts.tv_usec;
106 uniqueId = getUniqueID();
107 ids[dh->id] = std::make_pair(uniqueId, queryTime);
108 }
109 else {
110 const auto& it = ids.find(dh->id);
111 if (it != ids.end()) {
112 uniqueId = it->second.first;
113 queryTime = it->second.second;
114 hasQueryTime = true;
115 }
116 else {
117 uniqueId = getUniqueID();
118 }
119 }
120
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);
131 if (hasQueryTime) {
132 message.setQueryTime(queryTime.tv_sec, queryTime.tv_usec);
133 }
134
135 try {
136 message.addRRsFromPacket((const char*) dh, pr.d_len, true);
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 }
147
148 std::string str;
149 message.serialize(str);
150
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);
160 }
161
162 fclose(fp);
163 }
164 catch(const std::exception& e) {
165 cerr<<"Error opening PCAP file: "<<e.what()<<endl;
166 exit(EXIT_FAILURE);
167 }