64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
|
|
#include "util.hpp"
|
|
#include "TunDevice.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();
|
|
~UdpVpn();
|
|
|
|
void bind(in_port_t port);
|
|
void bind(const struct in6_addr& bind_addr6, in_port_t port);
|
|
|
|
int get_socket_fd() const { return _socket; }
|
|
const TunDevice& get_tun_dev() const { return _tun_dev; }
|
|
bool is_bound() const { return _bound; }
|
|
|
|
// Sets the peer address
|
|
void set_peer(const sockaddr_in6& peer_addr);
|
|
void unset_peer() { _has_peer = false; }
|
|
|
|
// Run the server.
|
|
void run();
|
|
|
|
// Stop the server. Can be called from an interrupt.
|
|
void stop() { _stopped = true; }
|
|
|
|
protected:
|
|
void receive_from_tun();
|
|
void receive_from_udp();
|
|
|
|
size_t send_over_udp(const char* data, size_t len);
|
|
|
|
private:
|
|
int _socket;
|
|
bool _bound;
|
|
bool _stopped;
|
|
bool _has_peer;
|
|
struct sockaddr_in6 _serv_addr, _peer_addr;
|
|
|
|
TunDevice _tun_dev;
|
|
};
|