Add class VpnPeer
This commit is contained in:
parent
c728aacb70
commit
55ada8abc1
3 changed files with 57 additions and 1 deletions
2
Makefile
2
Makefile
|
@ -2,7 +2,7 @@ CXX=g++
|
|||
CXXFLAGS=-O2 -g -Wall -Wextra -std=c++17
|
||||
CXXLIBS=
|
||||
|
||||
OBJS=UdpVpn.o TunDevice.o util.o main.o
|
||||
OBJS=UdpVpn.o VpnPeer.o TunDevice.o util.o main.o
|
||||
TARGET=congestvpn
|
||||
|
||||
all: $(TARGET)
|
||||
|
|
23
VpnPeer.cpp
Normal file
23
VpnPeer.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "VpnPeer.hpp"
|
||||
#include "UdpVpn.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
|
||||
VpnPeer::VpnPeer(UdpVpn* vpn, const sockaddr_in6& ext_addr,
|
||||
const in6_addr int_addr)
|
||||
: _vpn(vpn), _ext_addr(ext_addr), _int_addr(int_addr)
|
||||
{}
|
||||
|
||||
|
||||
size_t VpnPeer::write(const char* data, size_t len) {
|
||||
ssize_t nsent;
|
||||
|
||||
nsent = sendto(_vpn->get_socket_fd(), data, len, MSG_CONFIRM,
|
||||
(const struct sockaddr*) &_ext_addr, sizeof(_ext_addr));
|
||||
if(nsent < 0)
|
||||
throw NetError("Could not send UDP packet", errno, true);
|
||||
|
||||
return (size_t) nsent;
|
||||
}
|
33
VpnPeer.hpp
Normal file
33
VpnPeer.hpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
/** A peer of a bound (server) instance of UdpVpn */
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include "util.hpp"
|
||||
|
||||
class UdpVpn;
|
||||
|
||||
class VpnPeer {
|
||||
public:
|
||||
class NetError : public MsgException {
|
||||
public:
|
||||
NetError(
|
||||
const std::string& msg,
|
||||
int code=0,
|
||||
bool is_perror=false)
|
||||
: MsgException(msg, code, is_perror) {}
|
||||
};
|
||||
|
||||
VpnPeer(UdpVpn* vpn, const sockaddr_in6& ext_addr,
|
||||
const in6_addr int_addr);
|
||||
|
||||
const sockaddr_in6& get_ext_addr() const { return _ext_addr; }
|
||||
const in6_addr& get_int_addr() const { return _int_addr; }
|
||||
|
||||
size_t write(const char* data, size_t len);
|
||||
|
||||
private:
|
||||
UdpVpn* _vpn;
|
||||
sockaddr_in6 _ext_addr;
|
||||
in6_addr _int_addr;
|
||||
};
|
Loading…
Reference in a new issue