2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
22 #ifndef PDNS_DNSPCAP_HH
23 #define PDNS_DNSPCAP_HH
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__)
38 #include <net/if_ether.h>
39 #elif defined (__OpenBSD__)
41 #include <netinet/if_ether.h>
42 #elif defined (__SVR4) && defined (__sun)
43 #include <sys/ethernet.h>
45 #include <net/ethernet.h>
48 #include <boost/format.hpp>
49 #include "namespaces.hh"
51 struct pdns_pcap_file_header {
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_*) */
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) */
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 */
82 class PcapPacketReader
85 class EofException : public runtime_error
88 EofException(const string& str="") : runtime_error(str)
93 PcapPacketReader(const string& fname);
98 void checkedFread(T* ptr)
100 checkedFreadSize(ptr, sizeof(*ptr));
103 void checkedFreadSize(void* ptr, size_t size) ;
107 ComboAddress getSource() const;
108 ComboAddress getDest() const;
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;
120 pdns_pcap_file_header d_pfh;
121 unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp;
122 char d_buffer[32768];
126 int d_skipMediaHeader;
129 class PcapPacketWriter
132 PcapPacketWriter(const string& fname, const PcapPacketReader& ppr);
133 PcapPacketWriter(const string& fname);
136 void setPPR(const PcapPacketReader& ppr) { d_ppr = &ppr; }
141 const PcapPacketReader* d_ppr{nullptr};