| 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 | #if defined(__NetBSD__) |
|---|
| 14 | #include <net/if.h> |
|---|
| 15 | #include <net/if_ether.h> |
|---|
| 16 | #else |
|---|
| 17 | #include <net/ethernet.h> |
|---|
| 18 | #endif |
|---|
| 19 | #include <vector> |
|---|
| 20 | #include <boost/format.hpp> |
|---|
| 21 | #include "namespaces.hh" |
|---|
| 22 | |
|---|
| 23 | struct pdns_pcap_file_header { |
|---|
| 24 | uint32_t magic; |
|---|
| 25 | uint16_t version_major; |
|---|
| 26 | uint16_t version_minor; |
|---|
| 27 | uint32_t thiszone; /* gmt to local correction */ |
|---|
| 28 | uint32_t sigfigs; /* accuracy of timestamps */ |
|---|
| 29 | uint32_t snaplen; /* max length saved portion of each pkt */ |
|---|
| 30 | uint32_t linktype; /* data link type (LINKTYPE_*) */ |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | struct pdns_timeval |
|---|
| 35 | { |
|---|
| 36 | uint32_t tv_sec; |
|---|
| 37 | uint32_t tv_usec; |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | struct pdns_pcap_pkthdr { |
|---|
| 41 | struct pdns_timeval ts; /* time stamp */ |
|---|
| 42 | uint32_t caplen; /* length of portion present */ |
|---|
| 43 | uint32_t len; /* length this packet (off wire) */ |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | struct pdns_lcc_header { |
|---|
| 47 | uint16_t lcc_pkttype;/* packet type */ |
|---|
| 48 | uint16_t lcc_hatype;/* link-layer address type */ |
|---|
| 49 | uint16_t lcc_halen;/* link-layer address length */ |
|---|
| 50 | uint8_t lcc_addr[8];/* link-layer address */ |
|---|
| 51 | uint16_t lcc_protocol;/* protocol */ |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | class PcapPacketReader |
|---|
| 55 | { |
|---|
| 56 | public: |
|---|
| 57 | class EofException : public runtime_error |
|---|
| 58 | { |
|---|
| 59 | public: |
|---|
| 60 | EofException(const string& str="") : runtime_error(str) |
|---|
| 61 | { |
|---|
| 62 | } |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | PcapPacketReader(const string& fname); |
|---|
| 66 | |
|---|
| 67 | ~PcapPacketReader(); |
|---|
| 68 | |
|---|
| 69 | template<typename T> |
|---|
| 70 | void checkedFread(T* ptr) |
|---|
| 71 | { |
|---|
| 72 | checkedFreadSize(ptr, sizeof(*ptr)); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | void checkedFreadSize(void* ptr, size_t size) ; |
|---|
| 76 | |
|---|
| 77 | bool getUDPPacket(); |
|---|
| 78 | |
|---|
| 79 | struct pdns_lcc_header* d_lcc; |
|---|
| 80 | struct ether_header* d_ether; |
|---|
| 81 | struct ip *d_ip; |
|---|
| 82 | const struct tcphdr *d_tcp; |
|---|
| 83 | const struct udphdr *d_udp; |
|---|
| 84 | const uint8_t* d_payload; |
|---|
| 85 | int d_len; |
|---|
| 86 | struct pdns_pcap_pkthdr d_pheader; |
|---|
| 87 | |
|---|
| 88 | pdns_pcap_file_header d_pfh; |
|---|
| 89 | unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp; |
|---|
| 90 | char d_buffer[32768]; |
|---|
| 91 | private: |
|---|
| 92 | FILE* d_fp; |
|---|
| 93 | string d_fname; |
|---|
| 94 | int d_skipMediaHeader; |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | class PcapPacketWriter |
|---|
| 98 | { |
|---|
| 99 | public: |
|---|
| 100 | PcapPacketWriter(const string& fname, PcapPacketReader& ppr); |
|---|
| 101 | |
|---|
| 102 | void write(); |
|---|
| 103 | |
|---|
| 104 | ~PcapPacketWriter(); |
|---|
| 105 | |
|---|
| 106 | private: |
|---|
| 107 | string d_fname; |
|---|
| 108 | const PcapPacketReader& d_ppr; |
|---|
| 109 | |
|---|
| 110 | FILE *d_fp; |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | #endif // DNSPCAP_HH |
|---|