]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnspcap.hh
Make the constructors taking a pthread_rwlock_t * private.
[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 */
e8c59f2d 22#pragma once
ab06937b
BH
23#include <cstdio>
24#include <stdexcept>
a5d9353e 25#include "iputils.hh"
ab06937b 26#include <string>
11212b81
BH
27#include "misc.hh"
28#include <iostream>
0b86fb59 29#define __FAVOR_BSD
3042f1ba 30#include <netinet/in_systm.h>
11212b81 31#include <netinet/ip.h>
bf32bff8 32#include <netinet/ip6.h>
11212b81 33#include <netinet/udp.h>
b35da1b6 34#if defined(__NetBSD__)
525d1f90
PD
35#include <net/if.h>
36#include <net/if_ether.h>
b35da1b6
FO
37#elif defined (__OpenBSD__)
38#include <net/if.h>
39#include <netinet/if_ether.h>
c833728c 40#elif defined (__SVR4) && defined (__sun)
8f3456a7 41#include <sys/ethernet.h>
525d1f90 42#else
0b86fb59 43#include <net/ethernet.h>
525d1f90 44#endif
11212b81 45#include <vector>
11212b81 46#include <boost/format.hpp>
10f4eea8 47#include "namespaces.hh"
ab06937b 48
7be3bc79
BH
49struct pdns_pcap_file_header {
50 uint32_t magic;
51 uint16_t version_major;
52 uint16_t version_minor;
53 uint32_t thiszone; /* gmt to local correction */
54 uint32_t sigfigs; /* accuracy of timestamps */
55 uint32_t snaplen; /* max length saved portion of each pkt */
56 uint32_t linktype; /* data link type (LINKTYPE_*) */
57};
58
59
a1e7d16d
BH
60struct pdns_timeval
61{
d7729968
RG
62 uint32_t tv_sec{0};
63 uint32_t tv_usec{0};
a1e7d16d
BH
64};
65
7be3bc79 66struct pdns_pcap_pkthdr {
a1e7d16d 67 struct pdns_timeval ts; /* time stamp */
d7729968
RG
68 uint32_t caplen{0}; /* length of portion present */
69 uint32_t len{0}; /* length this packet (off wire) */
7be3bc79
BH
70};
71
2acf3098
BH
72struct pdns_lcc_header {
73 uint16_t lcc_pkttype;/* packet type */
74 uint16_t lcc_hatype;/* link-layer address type */
75 uint16_t lcc_halen;/* link-layer address length */
76 uint8_t lcc_addr[8];/* link-layer address */
77 uint16_t lcc_protocol;/* protocol */
78};
79
ab06937b
BH
80class PcapPacketReader
81{
82public:
83 class EofException : public runtime_error
84 {
85 public:
e08c410b 86 EofException(const string& str="PcapPacketReader::EofException") : runtime_error(str)
ab06937b
BH
87 {
88 }
89 };
90
91 PcapPacketReader(const string& fname);
92
93 ~PcapPacketReader();
94
95 template<typename T>
96 void checkedFread(T* ptr)
97 {
98 checkedFreadSize(ptr, sizeof(*ptr));
99 }
100
101 void checkedFreadSize(void* ptr, size_t size) ;
102
103 bool getUDPPacket();
104
a5d9353e 105 ComboAddress getSource() const;
106 ComboAddress getDest() const;
107
d7729968
RG
108 struct pdns_lcc_header* d_lcc{nullptr};
109 struct ether_header* d_ether{nullptr};
110 struct ip *d_ip{nullptr};
111 struct ip6_hdr *d_ip6{nullptr};
112 const struct tcphdr *d_tcp{nullptr};
113 const struct udphdr *d_udp{nullptr};
114 const uint8_t* d_payload{nullptr};
115 unsigned int d_len{0};
7be3bc79 116 struct pdns_pcap_pkthdr d_pheader;
11212b81 117
7be3bc79 118 pdns_pcap_file_header d_pfh;
11212b81 119 unsigned int d_runts, d_oversized, d_correctpackets, d_nonetheripudp;
3042f1ba 120 char d_buffer[32768];
ab06937b
BH
121private:
122 FILE* d_fp;
123 string d_fname;
fe92c902 124 unsigned int d_skipMediaHeader;
ab06937b
BH
125};
126
127class PcapPacketWriter
128{
129public:
e795f590 130 PcapPacketWriter(const string& fname, const PcapPacketReader& ppr);
131 PcapPacketWriter(const string& fname);
ab06937b
BH
132
133 void write();
e795f590 134 void setPPR(const PcapPacketReader& ppr) { d_ppr = &ppr; }
ab06937b
BH
135 ~PcapPacketWriter();
136
137private:
138 string d_fname;
d7729968 139 const PcapPacketReader* d_ppr{nullptr};
ab06937b
BH
140
141 FILE *d_fp;
e795f590 142 bool d_first{true};
ab06937b 143};