-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathnfq_wrapper.hpp
74 lines (56 loc) · 1.47 KB
/
nfq_wrapper.hpp
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
#pragma once
#include <cstdint>
#include <vector>
#include <functional>
#include <memory>
#include <mutex>
#include <span>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <libnfnetlink/libnfnetlink.h>
#include <linux/netfilter.h>
#include <libmnl/libmnl.h>
#include "misc.hpp"
// https://elixir.bootlin.com/linux/v4.4/source/include/uapi/linux/netfilter.h
enum class nfq_verdict_t : int {
DROP = 0,
ACCEPT = 1,
STOLEN = 2,
QUEUE = 3,
REPEAT = 4,
STOP = 5
};
// https://www.nftables.org/projects/libmnl/doxygen/html/libmnl_8h_source.html
enum class nfq_cb_result_t : int {
ERROR = -1,
STOP = 0,
OK = 1
};
class nfq_wrapper {
public:
typedef std::function<nfq_cb_result_t (
nfq_wrapper *,
uint32_t,
const std::span<const std::byte> &
)> cb_t;
nfq_wrapper(
const unsigned int p_queue_index,
cb_t p_cb,
const address_family_t p_family
);
~nfq_wrapper();
int get_fd();
void step();
void send_verdict(const uint32_t p_id, const nfq_verdict_t p_verdict);
private:
std::vector<char> m_buffer;
const std::unique_ptr<struct mnl_socket, int(*)(struct mnl_socket *)>
m_socket;
const unsigned int m_queue_index;
unsigned int m_port_id;
static int queue_cb_proxy(
const struct nlmsghdr *const p_header,
void *const p_context
);
const cb_t m_cb;
std::mutex m_send_lock;
};