congestvpn/UdpVpn.hpp

82 lines
2.3 KiB
C++
Raw Normal View History

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"
#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:
class InitializationError : public MsgException {
public:
InitializationError(
const std::string& msg,
int code=0,
bool is_perror=false)
: MsgException(msg, code, is_perror) {}
};
class NetError : public MsgException {
public:
NetError(
const std::string& msg,
int code=0,
bool is_perror=false)
: MsgException(msg, code, is_perror) {}
};
UdpVpn();
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:
virtual void acquire_peer(
2020-06-26 19:41:55 +02:00
VpnDataPacket& packet,
const sockaddr_in6& peer_ext_addr) = 0;
2020-06-04 11:47:00 +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);
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
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-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;
std::unique_ptr<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
};