Théophile Bastian
94f42d08c0
Clean up dramatically the codebase by assuming the server has only a single connection at any time.
68 lines
1.8 KiB
C++
68 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
|
|
#include "util.hpp"
|
|
#include "TunDevice.hpp"
|
|
#include "VpnPeer.hpp"
|
|
#include "VpnPacket.hpp"
|
|
|
|
/** 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();
|
|
|
|
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; }
|
|
|
|
protected:
|
|
virtual void acquire_peer(
|
|
TunnelledPacket& packet,
|
|
const sockaddr_in6& peer_ext_addr) = 0;
|
|
|
|
size_t read_from_tun(char* buffer, size_t len);
|
|
size_t read_from_tun(TunnelledPacket& packet);
|
|
size_t read_from_udp(char* buffer, size_t len, sockaddr_in6& peer_addr);
|
|
size_t read_from_udp(VpnPacket& packet, sockaddr_in6& peer_addr);
|
|
|
|
size_t transmit_to_peer(VpnPacket& packet);
|
|
|
|
void receive_from_tun();
|
|
void receive_from_udp();
|
|
|
|
void receive_tunnelled_tlv(TunnelledPacket& packet);
|
|
|
|
int _socket;
|
|
bool _stopped;
|
|
|
|
size_t _vpn_mtu;
|
|
|
|
TunDevice _tun_dev;
|
|
std::unique_ptr<VpnPeer> _peer;
|
|
};
|