| 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 | struct pdns_lcc_header { |
|---|
| 36 | uint16_t lcc_pkttype;/* packet type */ |
|---|
| 37 | uint16_t lcc_hatype;/* link-layer address type */ |
|---|
| 38 | uint16_t lcc_halen;/* link-layer address length */ |
|---|
| 39 | uint8_t lcc_addr[8];/* link-layer address */ |
|---|
| 40 | uint16_t lcc_protocol;/* protocol */ |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | class PcapPacketReader |
|---|
| 44 | { |
|---|
| 45 | public: |
|---|
| 46 | class EofException : public runtime_error |
|---|
| 47 | { |
|---|
| 48 | public: |
|---|
| 49 | EofException(const string& str="") : runtime_error(str) |
|---|
| 50 | { |
|---|
| 51 | } |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | PcapPacketReader(const string& fname); |
|---|
| 55 | |
|---|
| 56 | ~PcapPacketReader(); |
|---|
| 57 | |
|---|
| 58 | template<typename T> |
|---|
| 59 | void checkedFread(T* ptr) |
|---|
| 60 | { |
|---|
| 61 | checkedFreadSize(ptr, sizeof(*ptr)); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | void checkedFreadSize(void* ptr, size_t size) ; |
|---|
| 65 | |
|---|
| 66 | bool getUDPPacket(); |
|---|
| 67 | |
|---|
| 68 | struct pdns_lcc_header* d_lcc; |
|---|
| 69 | struct ether_header* d_ether; |
|---|
| 70 | struct ip *d_ip; |
|---|
| 71 | const struct tcphdr *d_tcp; |
|---|
| 72 | const struct udphdr *d_udp; |
|---|
| 73 | const uint8_t* d_payload; |
|---|
| 74 | int d_len; |
|---|
| 75 | struct pdns_pcap_pkthdr d_pheader; |
|---|
| 76 | |
|---|
| 77 | pdns_pcap_file_header d_pfh; |
|---|
| 78 | unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp; |
|---|
| 79 | char d_buffer[32768]; |
|---|
| 80 | private: |
|---|
| 81 | FILE* d_fp; |
|---|
| 82 | string d_fname; |
|---|
| 83 | int d_skipMediaHeader; |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | class PcapPacketWriter |
|---|
| 87 | { |
|---|
| 88 | public: |
|---|
| 89 | PcapPacketWriter(const string& fname, PcapPacketReader& ppr); |
|---|
| 90 | |
|---|
| 91 | void write(); |
|---|
| 92 | |
|---|
| 93 | ~PcapPacketWriter(); |
|---|
| 94 | |
|---|
| 95 | private: |
|---|
| 96 | string d_fname; |
|---|
| 97 | const PcapPacketReader& d_ppr; |
|---|
| 98 | |
|---|
| 99 | FILE *d_fp; |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | #endif // DNSPCAP_HH |
|---|