2020-06-04 11:47:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2020-07-22 15:45:16 +02:00
|
|
|
#include <time.h>
|
2020-06-04 11:47:00 +02:00
|
|
|
|
|
|
|
#include "util.hpp"
|
|
|
|
#include "TunDevice.hpp"
|
2020-06-05 16:14:49 +02:00
|
|
|
#include "VpnPeer.hpp"
|
2020-06-05 17:31:02 +02:00
|
|
|
#include "VpnPacket.hpp"
|
2020-06-04 11:47:00 +02:00
|
|
|
|
|
|
|
/** Handles UDP communication */
|
|
|
|
|
|
|
|
class UdpVpn {
|
|
|
|
public:
|
|
|
|
UdpVpn();
|
2020-06-05 16:14:49 +02:00
|
|
|
virtual ~UdpVpn();
|
2020-06-04 11:47:00 +02:00
|
|
|
|
|
|
|
int get_socket_fd() const { return _socket; }
|
|
|
|
const TunDevice& get_tun_dev() const { return _tun_dev; }
|
|
|
|
|
|
|
|
// Run the server.
|
|
|
|
void run();
|
|
|
|
|
|
|
|
// Stop the server. Can be called from an interrupt.
|
|
|
|
void stop() { _stopped = true; }
|
|
|
|
|
2020-06-26 19:41:55 +02:00
|
|
|
// A state dump has been requested
|
|
|
|
void dump_requested() { _dump_requested = true; }
|
|
|
|
|
2020-06-28 23:29:39 +02:00
|
|
|
size_t get_mtu() const { return _vpn_mtu; }
|
|
|
|
|
|
|
|
size_t transmit_to_peer(VpnPacket& packet);
|
|
|
|
|
2020-06-04 11:47:00 +02:00
|
|
|
protected:
|
2020-06-15 20:21:07 +02:00
|
|
|
virtual void acquire_peer(
|
2020-06-26 19:41:55 +02:00
|
|
|
VpnDataPacket& packet,
|
2020-06-15 20:21:07 +02:00
|
|
|
const sockaddr_in6& peer_ext_addr) = 0;
|
2020-06-04 11:47:00 +02:00
|
|
|
|
2020-06-05 16:14:49 +02:00
|
|
|
size_t read_from_tun(char* buffer, size_t len);
|
2020-06-26 19:41:55 +02:00
|
|
|
size_t read_from_tun(VpnDataPacket& packet);
|
2020-06-05 16:14:49 +02:00
|
|
|
size_t read_from_udp(char* buffer, size_t len, sockaddr_in6& peer_addr);
|
2020-06-05 17:31:02 +02:00
|
|
|
size_t read_from_udp(VpnPacket& packet, sockaddr_in6& peer_addr);
|
2020-06-04 11:47:00 +02:00
|
|
|
|
2020-06-15 20:21:07 +02:00
|
|
|
void receive_from_tun();
|
|
|
|
void receive_from_udp();
|
|
|
|
|
2020-06-26 19:41:55 +02:00
|
|
|
void receive_tunnelled_tlv(VpnDataPacket& packet);
|
|
|
|
|
|
|
|
void dump_state() const;
|
2020-06-15 20:21:07 +02:00
|
|
|
|
2020-06-04 11:47:00 +02:00
|
|
|
int _socket;
|
2020-06-26 19:41:55 +02:00
|
|
|
bool _stopped, _dump_requested;
|
2020-06-04 11:47:00 +02:00
|
|
|
|
2020-06-05 17:31:02 +02:00
|
|
|
size_t _vpn_mtu;
|
|
|
|
|
2020-06-04 11:47:00 +02:00
|
|
|
TunDevice _tun_dev;
|
2020-07-22 17:04:22 +02:00
|
|
|
VpnPeer* _peer;
|
2020-06-28 23:29:39 +02:00
|
|
|
|
2020-07-22 15:45:16 +02:00
|
|
|
struct timespec
|
2020-07-03 18:03:26 +02:00
|
|
|
_last_control_sent, /**< A control is offered to be sent approx.
|
|
|
|
every 100ms */
|
|
|
|
_last_tick /**< A tick occurs approx. each 50ms */;
|
2020-06-04 11:47:00 +02:00
|
|
|
};
|