1 #ifndef PDNS_DNSPCAP_HH
2 #define PDNS_DNSPCAP_HH
11 #include <netinet/in_systm.h>
12 #include <netinet/ip.h>
13 #include <netinet/ip6.h>
14 #include <netinet/udp.h>
15 #if defined(__NetBSD__)
17 #include <net/if_ether.h>
18 #elif defined (__OpenBSD__)
20 #include <netinet/if_ether.h>
21 #elif defined (__SVR4) && defined (__sun)
22 #include <sys/ethernet.h>
24 #include <net/ethernet.h>
27 #include <boost/format.hpp>
28 #include "namespaces.hh"
30 struct pdns_pcap_file_header {
32 uint16_t version_major;
33 uint16_t version_minor;
34 uint32_t thiszone; /* gmt to local correction */
35 uint32_t sigfigs; /* accuracy of timestamps */
36 uint32_t snaplen; /* max length saved portion of each pkt */
37 uint32_t linktype; /* data link type (LINKTYPE_*) */
47 struct pdns_pcap_pkthdr {
48 struct pdns_timeval ts; /* time stamp */
49 uint32_t caplen{0}; /* length of portion present */
50 uint32_t len{0}; /* length this packet (off wire) */
53 struct pdns_lcc_header {
54 uint16_t lcc_pkttype;/* packet type */
55 uint16_t lcc_hatype;/* link-layer address type */
56 uint16_t lcc_halen;/* link-layer address length */
57 uint8_t lcc_addr[8];/* link-layer address */
58 uint16_t lcc_protocol;/* protocol */
61 class PcapPacketReader
64 class EofException : public runtime_error
67 EofException(const string& str="") : runtime_error(str)
72 PcapPacketReader(const string& fname);
77 void checkedFread(T* ptr)
79 checkedFreadSize(ptr, sizeof(*ptr));
82 void checkedFreadSize(void* ptr, size_t size) ;
86 ComboAddress getSource() const;
87 ComboAddress getDest() const;
89 struct pdns_lcc_header* d_lcc{nullptr};
90 struct ether_header* d_ether{nullptr};
91 struct ip *d_ip{nullptr};
92 struct ip6_hdr *d_ip6{nullptr};
93 const struct tcphdr *d_tcp{nullptr};
94 const struct udphdr *d_udp{nullptr};
95 const uint8_t* d_payload{nullptr};
96 unsigned int d_len{0};
97 struct pdns_pcap_pkthdr d_pheader;
99 pdns_pcap_file_header d_pfh;
100 unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp;
101 char d_buffer[32768];
105 int d_skipMediaHeader;
108 class PcapPacketWriter
111 PcapPacketWriter(const string& fname, const PcapPacketReader& ppr);
112 PcapPacketWriter(const string& fname);
115 void setPPR(const PcapPacketReader& ppr) { d_ppr = &ppr; }
120 const PcapPacketReader* d_ppr{nullptr};