]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnspcap.hh
b2b-migrate did not open a transaction, breaking it for lmdb
[thirdparty/pdns.git] / pdns / dnspcap.hh
CommitLineData
12471842
PL
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
ab06937b
BH
22#ifndef PDNS_DNSPCAP_HH
23#define PDNS_DNSPCAP_HH
0b86fb59 24
ab06937b
BH
25#include <cstdio>
26#include <stdexcept>
a5d9353e 27#include "iputils.hh"
ab06937b 28#include <string>
11212b81
BH
29#include "misc.hh"
30#include <iostream>
0b86fb59 31#define __FAVOR_BSD
3042f1ba 32#include <netinet/in_systm.h>
11212b81 33#include <netinet/ip.h>
bf32bff8 34#include <netinet/ip6.h>
11212b81 35#include <netinet/udp.h>
b35da1b6 36#if defined(__NetBSD__)
525d1f90
PD
37#include <net/if.h>
38#include <net/if_ether.h>
b35da1b6
FO
39#elif defined (__OpenBSD__)
40#include <net/if.h>
41#include <netinet/if_ether.h>
c833728c 42#elif defined (__SVR4) && defined (__sun)
8f3456a7 43#include <sys/ethernet.h>
525d1f90 44#else
0b86fb59 45#include <net/ethernet.h>
525d1f90 46#endif
11212b81 47#include <vector>
11212b81 48#include <boost/format.hpp>
10f4eea8 49#include "namespaces.hh"
ab06937b 50
7be3bc79
BH
51struct pdns_pcap_file_header {
52 uint32_t magic;
53 uint16_t version_major;
54 uint16_t version_minor;
55 uint32_t thiszone; /* gmt to local correction */
56 uint32_t sigfigs; /* accuracy of timestamps */
57 uint32_t snaplen; /* max length saved portion of each pkt */
58 uint32_t linktype; /* data link type (LINKTYPE_*) */
59};
60
61
a1e7d16d
BH
62struct pdns_timeval
63{
d7729968
RG
64 uint32_t tv_sec{0};
65 uint32_t tv_usec{0};
a1e7d16d
BH
66};
67
7be3bc79 68struct pdns_pcap_pkthdr {
a1e7d16d 69 struct pdns_timeval ts; /* time stamp */
d7729968
RG
70 uint32_t caplen{0}; /* length of portion present */
71 uint32_t len{0}; /* length this packet (off wire) */
7be3bc79
BH
72};
73
2acf3098
BH
74struct pdns_lcc_header {
75 uint16_t lcc_pkttype;/* packet type */
76 uint16_t lcc_hatype;/* link-layer address type */
77 uint16_t lcc_halen;/* link-layer address length */
78 uint8_t lcc_addr[8];/* link-layer address */
79 uint16_t lcc_protocol;/* protocol */
80};
81
ab06937b
BH
82class PcapPacketReader
83{
84public:
85 class EofException : public runtime_error
86 {
87 public:
88 EofException(const string& str="") : runtime_error(str)
89 {
90 }
91 };
92
93 PcapPacketReader(const string& fname);
94
95 ~PcapPacketReader();
96
97 template<typename T>
98 void checkedFread(T* ptr)
99 {
100 checkedFreadSize(ptr, sizeof(*ptr));
101 }
102
103 void checkedFreadSize(void* ptr, size_t size) ;
104
105 bool getUDPPacket();
106
a5d9353e 107 ComboAddress getSource() const;
108 ComboAddress getDest() const;
109
d7729968
RG
110 struct pdns_lcc_header* d_lcc{nullptr};
111 struct ether_header* d_ether{nullptr};
112 struct ip *d_ip{nullptr};
113 struct ip6_hdr *d_ip6{nullptr};
114 const struct tcphdr *d_tcp{nullptr};
115 const struct udphdr *d_udp{nullptr};
116 const uint8_t* d_payload{nullptr};
117 unsigned int d_len{0};
7be3bc79 118 struct pdns_pcap_pkthdr d_pheader;
11212b81 119
7be3bc79 120 pdns_pcap_file_header d_pfh;
11212b81 121 unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp;
3042f1ba 122 char d_buffer[32768];
ab06937b
BH
123private:
124 FILE* d_fp;
125 string d_fname;
fe92c902 126 unsigned int d_skipMediaHeader;
ab06937b
BH
127};
128
129class PcapPacketWriter
130{
131public:
e795f590 132 PcapPacketWriter(const string& fname, const PcapPacketReader& ppr);
133 PcapPacketWriter(const string& fname);
ab06937b
BH
134
135 void write();
e795f590 136 void setPPR(const PcapPacketReader& ppr) { d_ppr = &ppr; }
ab06937b
BH
137 ~PcapPacketWriter();
138
139private:
140 string d_fname;
d7729968 141 const PcapPacketReader* d_ppr{nullptr};
ab06937b
BH
142
143 FILE *d_fp;
e795f590 144 bool d_first{true};
ab06937b
BH
145};
146
147#endif // DNSPCAP_HH