]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnspcap.hh
Merge pull request #4305 from rgacogne/dnsdist-lua-anon
[thirdparty/pdns.git] / pdns / dnspcap.hh
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 #ifndef PDNS_DNSPCAP_HH
23 #define PDNS_DNSPCAP_HH
24
25 #include <cstdio>
26 #include <stdexcept>
27 #include "iputils.hh"
28 #include <string>
29 #include "misc.hh"
30 #include <iostream>
31 #define __FAVOR_BSD
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip6.h>
35 #include <netinet/udp.h>
36 #if defined(__NetBSD__)
37 #include <net/if.h>
38 #include <net/if_ether.h>
39 #elif defined (__OpenBSD__)
40 #include <net/if.h>
41 #include <netinet/if_ether.h>
42 #elif defined (__SVR4) && defined (__sun)
43 #include <sys/ethernet.h>
44 #else
45 #include <net/ethernet.h>
46 #endif
47 #include <vector>
48 #include <boost/format.hpp>
49 #include "namespaces.hh"
50
51 struct pdns_pcap_file_header {
52 uint32_t magic;
53 uint16_t version_major;
54 uint16_t version_minor;
55 uint32_t thiszone; /* gmt to local correction */
56 uint32_t sigfigs; /* accuracy of timestamps */
57 uint32_t snaplen; /* max length saved portion of each pkt */
58 uint32_t linktype; /* data link type (LINKTYPE_*) */
59 };
60
61
62 struct pdns_timeval
63 {
64 uint32_t tv_sec{0};
65 uint32_t tv_usec{0};
66 };
67
68 struct pdns_pcap_pkthdr {
69 struct pdns_timeval ts; /* time stamp */
70 uint32_t caplen{0}; /* length of portion present */
71 uint32_t len{0}; /* length this packet (off wire) */
72 };
73
74 struct pdns_lcc_header {
75 uint16_t lcc_pkttype;/* packet type */
76 uint16_t lcc_hatype;/* link-layer address type */
77 uint16_t lcc_halen;/* link-layer address length */
78 uint8_t lcc_addr[8];/* link-layer address */
79 uint16_t lcc_protocol;/* protocol */
80 };
81
82 class PcapPacketReader
83 {
84 public:
85 class EofException : public runtime_error
86 {
87 public:
88 EofException(const string& str="") : runtime_error(str)
89 {
90 }
91 };
92
93 PcapPacketReader(const string& fname);
94
95 ~PcapPacketReader();
96
97 template<typename T>
98 void checkedFread(T* ptr)
99 {
100 checkedFreadSize(ptr, sizeof(*ptr));
101 }
102
103 void checkedFreadSize(void* ptr, size_t size) ;
104
105 bool getUDPPacket();
106
107 ComboAddress getSource() const;
108 ComboAddress getDest() const;
109
110 struct pdns_lcc_header* d_lcc{nullptr};
111 struct ether_header* d_ether{nullptr};
112 struct ip *d_ip{nullptr};
113 struct ip6_hdr *d_ip6{nullptr};
114 const struct tcphdr *d_tcp{nullptr};
115 const struct udphdr *d_udp{nullptr};
116 const uint8_t* d_payload{nullptr};
117 unsigned int d_len{0};
118 struct pdns_pcap_pkthdr d_pheader;
119
120 pdns_pcap_file_header d_pfh;
121 unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp;
122 char d_buffer[32768];
123 private:
124 FILE* d_fp;
125 string d_fname;
126 int d_skipMediaHeader;
127 };
128
129 class PcapPacketWriter
130 {
131 public:
132 PcapPacketWriter(const string& fname, const PcapPacketReader& ppr);
133 PcapPacketWriter(const string& fname);
134
135 void write();
136 void setPPR(const PcapPacketReader& ppr) { d_ppr = &ppr; }
137 ~PcapPacketWriter();
138
139 private:
140 string d_fname;
141 const PcapPacketReader* d_ppr{nullptr};
142
143 FILE *d_fp;
144 bool d_first{true};
145 };
146
147 #endif // DNSPCAP_HH