Listen to tun and write back
This commit is contained in:
parent
53444e725c
commit
8ec3f20513
1 changed files with 21 additions and 2 deletions
23
main.cpp
23
main.cpp
|
@ -1,14 +1,33 @@
|
|||
#include <cstdio>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "TunDevice.hpp"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char read_buffer[BUFFER_SIZE];
|
||||
|
||||
signal(SIGINT, stop_sig_handler);
|
||||
try {
|
||||
TunDevice tun_dev("cvpn%d");
|
||||
printf("Tunnel device opened: <%s>, fd <%d>.\nSleeping 10 seconds.\n",
|
||||
printf("Tunnel device opened: <%s>, fd <%d>.\n",
|
||||
tun_dev.get_dev_name().c_str(), tun_dev.get_fd());
|
||||
sleep(10);
|
||||
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);
|
||||
}
|
||||
}
|
||||
printf("Shutting down.\n");
|
||||
} catch(const TunDevice::InitializationError& exn) {
|
||||
fprintf(stderr, "ERROR: %s\n", exn.what());
|
||||
|
|
Loading…
Reference in a new issue