-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipe_daemon.cpp
107 lines (82 loc) · 2.59 KB
/
pipe_daemon.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (C) 2020 Leonard Seibold
#include <cstdio>
#include <fcntl.h>
#include <string.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "dpi.h"
#define OUT_PIPE "/tmp/packetout"
#define IN_PIPE "/tmp/packetin"
#define LOG_PREFIX "[Pfeifendämon 😈] "
#define log(format, ...) \
printf(string(LOG_PREFIX) \
.append(format) \
.append("\n") \
.c_str() __VA_OPT__(, ) __VA_ARGS__)
using namespace std;
const unsigned int default_verdict = DPI_ACCEPT;
void write_packets() {
int fd = open(OUT_PIPE, O_WRONLY);
if (fd < 0) {
log("Couldn't open output FIFO.");
exit(EXIT_FAILURE);
}
while (1) {
p_buff *packet = pull_packet();
if (!packet) {
log("Failed to pull packet. Is the LKM still active?");
exit(EXIT_FAILURE);
}
log("Pulled packet: ID=%d, Length=%d.", packet->packet_id,
(int)packet->len);
write_packet(fd, packet, default_verdict);
delete packet;
}
}
void read_packets() {
int fd = open(IN_PIPE, O_RDONLY);
if (fd < 0) {
log("Couldn't open input FIFO.");
exit(EXIT_FAILURE);
}
while (1) {
p_buff packet(false);
unsigned int verdict;
unsigned char data[MAX_BUF_SIZE];
read_packet(fd, &packet, data, &verdict);
push_packet(&packet, verdict);
if (dpi_state != DPI_Connected) {
log("Failed to push packet back to LKM (State=%d, ClientState=%d).",
dpi_state, client->state);
}
log("Pushed packet back to LKM: ID=%d, Length=%d, Verdict=%d.",
packet.packet_id, (int)packet.len, verdict);
}
}
int main(int argc, char **argv) {
size_t pid;
char out_fifo[sizeof(OUT_PIPE)];
char in_fifo[sizeof(IN_PIPE)];
strcpy(out_fifo, OUT_PIPE);
strcpy(in_fifo, IN_PIPE);
mkfifo(out_fifo, 0666);
mkfifo(in_fifo, 0666);
log("Created FIFOs.");
log("Trying to connect to DPI LKM...");
if (!dpi_connect()) {
log("Couldn't connect to LKM 😢. Client state is %d.", client->state);
return EXIT_FAILURE;
}
log("Connected to DPI LKM.");
pid = fork();
if (pid == 0) {
write_packets();
} else if (pid > 0) {
read_packets();
} else {
perror("Fork failed.");
return EXIT_FAILURE;
}
}