-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipe_printer.cpp
40 lines (32 loc) · 1.04 KB
/
pipe_printer.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
// Copyright (C) 2020 Leonard Seibold
#include "dpi.h"
#include <arpa/inet.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <stdio.h>
#include <unistd.h>
int main() {
p_buff packet;
unsigned char data[MAX_BUF_SIZE];
unsigned int verdict;
while (1) {
read_packet(STDIN_FILENO, &packet, data, &verdict);
iphdr *ip = (iphdr *)packet.data;
// TCP Header starts 4 Bytes after ip->daddr (as long as IHL is not > 5)
tcphdr *tcp = (tcphdr *)(((unsigned char *)&(ip->daddr)) + 4);
struct in_addr src {
.s_addr = (unsigned int)ip->saddr
};
struct in_addr dest {
.s_addr = (unsigned int)ip->daddr
};
fprintf(stderr,
"Read packet. Length: %d Source IP: %s Dest IP: %s Source "
"port: %d "
"Dest port: %d\n",
packet.len, inet_ntoa(src), inet_ntoa(dest), ntohs(tcp->source),
ntohs(tcp->dest));
write_packet(STDOUT_FILENO, &packet, verdict);
}
return 0;
}