]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnspcap.hh
drop pcap.h dependency
[thirdparty/pdns.git] / pdns / dnspcap.hh
1 #ifndef PDNS_DNSPCAP_HH
2 #define PDNS_DNSPCAP_HH
3
4 #include <cstdio>
5 #include <stdexcept>
6 #include <string>
7 #include "misc.hh"
8 #include <iostream>
9 #define __FAVOR_BSD
10 #include <netinet/in_systm.h>
11 #include <netinet/ip.h>
12 #include <netinet/udp.h>
13 #include <net/ethernet.h>
14 #include <vector>
15 #include <boost/format.hpp>
16 using namespace std;
17
18 struct pdns_pcap_file_header {
19 uint32_t magic;
20 uint16_t version_major;
21 uint16_t version_minor;
22 uint32_t thiszone; /* gmt to local correction */
23 uint32_t sigfigs; /* accuracy of timestamps */
24 uint32_t snaplen; /* max length saved portion of each pkt */
25 uint32_t linktype; /* data link type (LINKTYPE_*) */
26 };
27
28
29 struct pdns_pcap_pkthdr {
30 struct timeval ts; /* time stamp */
31 uint32_t caplen; /* length of portion present */
32 uint32_t len; /* length this packet (off wire) */
33 };
34
35 class PcapPacketReader
36 {
37 public:
38 class EofException : public runtime_error
39 {
40 public:
41 EofException(const string& str="") : runtime_error(str)
42 {
43 }
44 };
45
46 PcapPacketReader(const string& fname);
47
48 ~PcapPacketReader();
49
50 template<typename T>
51 void checkedFread(T* ptr)
52 {
53 checkedFreadSize(ptr, sizeof(*ptr));
54 }
55
56 void checkedFreadSize(void* ptr, size_t size) ;
57
58 bool getUDPPacket();
59
60 struct ether_header* d_ether;
61 struct ip *d_ip;
62 const struct tcphdr *d_tcp;
63 const struct udphdr *d_udp;
64 const uint8_t* d_payload;
65 int d_len;
66 struct pdns_pcap_pkthdr d_pheader;
67
68 pdns_pcap_file_header d_pfh;
69 unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp;
70 char d_buffer[32768];
71 private:
72 FILE* d_fp;
73 string d_fname;
74 };
75
76 class PcapPacketWriter
77 {
78 public:
79 PcapPacketWriter(const string& fname, PcapPacketReader& ppr);
80
81 void write();
82
83 ~PcapPacketWriter();
84
85 private:
86 string d_fname;
87 const PcapPacketReader& d_ppr;
88
89 FILE *d_fp;
90 };
91
92 #endif // DNSPCAP_HH