congestvpn/UdpVpn.hpp

55 lines
1.4 KiB
C++

#pragma once
#include <sys/socket.h>
#include <netinet/in.h>
#include "util.hpp"
#include "TunDevice.hpp"
#include "VpnPeer.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 receive_from_tun() = 0;
virtual void receive_from_udp() = 0;
size_t read_from_tun(char* buffer, size_t len);
size_t read_from_udp(char* buffer, size_t len, sockaddr_in6& peer_addr);
int _socket;
bool _stopped;
TunDevice _tun_dev;
};