aboutsummaryrefslogtreecommitdiff
path: root/experiments/pcap/main.c
blob: 25fb0fccc61c8b5a05b58009f0c10038aca411d6 (plain)
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
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <pcap/pcap.h>

pcap_dumper_t* dumper;

ssize_t test_write(uint8_t* buf, size_t count) {
	struct pcap_pkthdr packet;
	packet.len = count;
	packet.caplen = count;
	pcap_dump((u_char*) dumper, &packet, buf);

	return count;
}

ssize_t test_read(uint8_t* buf, size_t count) {
	strncpy((char*) buf, "i read hello world!", count);

	struct pcap_pkthdr packet;
	packet.len = count;
	packet.caplen = count;
	pcap_dump((u_char*) dumper, &packet, buf);

	return count;
}

void test() {
	ssize_t len;
	const char* msg = "i write hello world!";

	len = test_write((uint8_t*) msg, strlen(msg));
	printf("wrote %lu bytes: \"%s\"\n", len, msg);

	char buf[80] = { 0 };
	len = test_read((uint8_t*) buf, 16);
	buf[len] = '\0';
	printf("read %lu bytes: \"%s\"\n", len, buf);
}

int main() {
	// see also: <https://www.tcpdump.org/linktypes.html>
	pcap_t* handle = pcap_open_dead(DLT_NULL, 1 << 16);
  dumper = pcap_dump_open(handle, "dump.pcap");

	test();

	pcap_dump_close(dumper);

	return 0;
}