root/trunk/pdns/pdns/dnspcap.hh @ 1976

Revision 1976, 2.2 KB (checked in by ahu, 2 years ago)

big batch of 'using namespace std;' removal

Line 
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#include "namespaces.hh"
17
18struct 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
29struct pdns_timeval
30{
31  uint32_t tv_sec;
32  uint32_t tv_usec;
33};
34
35struct pdns_pcap_pkthdr {
36  struct pdns_timeval ts;      /* time stamp */
37  uint32_t caplen;     /* length of portion present */
38  uint32_t len;        /* length this packet (off wire) */
39};
40
41struct pdns_lcc_header {
42  uint16_t lcc_pkttype;/* packet type */
43  uint16_t lcc_hatype;/* link-layer address type */
44  uint16_t lcc_halen;/* link-layer address length */
45  uint8_t lcc_addr[8];/* link-layer address */
46  uint16_t lcc_protocol;/* protocol */
47};
48
49class PcapPacketReader
50{
51public:
52  class EofException : public runtime_error
53  {
54  public:
55    EofException(const string& str="") : runtime_error(str)
56    {
57    }
58  };
59
60  PcapPacketReader(const string& fname); 
61
62  ~PcapPacketReader();
63
64  template<typename T>
65  void checkedFread(T* ptr)
66  {
67    checkedFreadSize(ptr, sizeof(*ptr));
68  }
69
70  void checkedFreadSize(void* ptr, size_t size) ;
71
72  bool getUDPPacket();
73
74  struct pdns_lcc_header* d_lcc;
75  struct ether_header* d_ether;
76  struct ip *d_ip;
77  const struct tcphdr *d_tcp;
78  const struct udphdr *d_udp;
79  const uint8_t* d_payload;
80  int d_len;
81  struct pdns_pcap_pkthdr d_pheader;
82
83  pdns_pcap_file_header d_pfh;
84  unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp;
85  char d_buffer[32768];
86private:
87  FILE* d_fp;
88  string d_fname;
89  int d_skipMediaHeader;
90};
91
92class PcapPacketWriter
93{
94public: 
95  PcapPacketWriter(const string& fname, PcapPacketReader& ppr);
96 
97  void write();
98
99  ~PcapPacketWriter();
100
101private:
102  string d_fname;
103  const PcapPacketReader& d_ppr;
104
105  FILE *d_fp;
106}; 
107
108#endif // DNSPCAP_HH
Note: See TracBrowser for help on using the browser.