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