-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathafpacket.h
91 lines (70 loc) · 2 KB
/
afpacket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#pragma once
#include "PcapException.h"
#include <pcap/pcap.h>
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
#include <pcapplusplus/PcapLiveDevice.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#include <atomic>
#include <cstdint>
#include <functional>
#include <linux/filter.h>
#include <linux/if_packet.h>
#include <string>
#include <sys/uio.h>
#include <thread>
namespace visor::input::pcap {
const int physical_offset = TPACKET_ALIGN(sizeof(struct tpacket3_hdr));
static const int VERSION = TPACKET_V3;
struct block_desc {
uint32_t version;
uint32_t offset_to_priv;
struct tpacket_hdr_v1 h1;
};
class PcapInputStream;
class AFPacket final
{
int fd;
unsigned int block_size;
unsigned int frame_size;
unsigned int num_blocks;
int interface;
int interface_type;
std::string interface_name;
struct sock_fprog bpf;
std::string filter;
int fanout_group_id;
std::vector<struct iovec> rd;
uint8_t *map;
pcpp::OnPacketArrivesCallback cb;
PcapInputStream *inputStream;
void flush_block(struct block_desc *pbd);
void walk_block(struct block_desc *pbd);
void set_interface();
void set_socket_opts();
void setup();
std::atomic<bool> running;
std::unique_ptr<std::thread> cap_thread;
public:
AFPacket(PcapInputStream *stream, pcpp::OnPacketArrivesCallback cb, std::string filter,
std::string interface_name,
int fanout_group_id = -1,
unsigned int block_size = 1 << 22,
unsigned int frame_size = 1 << 11,
unsigned int num_blocks = 64);
~AFPacket();
void start_capture();
void stop_capture()
{
running = false;
}
};
void filter_try_compile(const std::string &, struct sock_fprog *, int);
} // namespace visor