congestvpn/main.cpp

37 lines
1,007 B
C++
Raw Normal View History

2020-06-02 13:08:23 +02:00
#include <cstdio>
#include <unistd.h>
2020-06-03 14:54:26 +02:00
#include <signal.h>
2020-06-02 13:08:23 +02:00
#include "TunDevice.hpp"
2020-06-03 14:54:26 +02:00
static const size_t BUFFER_SIZE = 1500;
static volatile bool stopped = false;
void stop_sig_handler(int signal) {
printf("Received signal %d. Stopping.\n", signal);
stopped = true;
}
2020-06-02 13:08:23 +02:00
int main(void) {
2020-06-03 14:54:26 +02:00
char read_buffer[BUFFER_SIZE];
signal(SIGINT, stop_sig_handler);
2020-06-02 13:08:23 +02:00
try {
TunDevice tun_dev("cvpn%d");
2020-06-03 14:54:26 +02:00
printf("Tunnel device opened: <%s>, fd <%d>.\n",
2020-06-02 13:08:23 +02:00
tun_dev.get_dev_name().c_str(), tun_dev.get_fd());
2020-06-03 14:54:26 +02:00
while(!stopped) {
size_t packet_size = tun_dev.read_packet(read_buffer, BUFFER_SIZE);
if(packet_size > 0) {
printf("Received packet of size %lu.\n", packet_size);
tun_dev.write_packet(read_buffer, packet_size);
}
}
2020-06-02 13:08:23 +02:00
printf("Shutting down.\n");
} catch(const TunDevice::InitializationError& exn) {
fprintf(stderr, "ERROR: %s\n", exn.what());
}
return 0;
}