congestvpn/VpnPeer.hpp

77 lines
2.1 KiB
C++
Raw Normal View History

2020-06-05 16:14:05 +02:00
#pragma once
/** A peer of a bound (server) instance of UdpVpn */
#include <netinet/in.h>
2020-06-23 15:36:49 +02:00
#include <bitset>
2020-06-05 16:14:05 +02:00
#include "util.hpp"
2020-06-05 17:31:02 +02:00
#include "VpnPacket.hpp"
2020-06-05 16:14:05 +02:00
class UdpVpn;
2020-06-23 15:36:49 +02:00
const int PACKET_LOSS_HISTSIZE = 128, PACKET_LOST_AFTER = 8;
class PacketLossLogger {
public:
PacketLossLogger();
void log_packet(uint32_t seqno);
double get_loss_rate() const {
return (double)_packet_loss_hist.count() / PACKET_LOSS_HISTSIZE;
}
const std::bitset<PACKET_LOSS_HISTSIZE> get_loss_hist() const {
return _packet_loss_hist;
}
const std::bitset<PACKET_LOST_AFTER> get_received_ahead() const {
return _received_ahead;
}
uint32_t get_cur_seqno() const { return _cur_seqno; }
private:
void reboot(); ///< completely reset the internal state
std::bitset<PACKET_LOSS_HISTSIZE> _packet_loss_hist;
std::bitset<PACKET_LOST_AFTER> _received_ahead;
uint32_t _cur_seqno;
};
2020-06-05 16:14:05 +02:00
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);
2020-06-05 16:14:05 +02:00
const sockaddr_in6& get_ext_addr() const { return _ext_addr; }
const in6_addr& get_int_addr() const { return _int_addr; }
void set_int_addr(const in6_addr& int_addr);
2020-06-26 19:41:55 +02:00
const PacketLossLogger& get_loss_logger() { return _packet_loss; }
2020-06-05 16:14:05 +02:00
size_t write(const char* data, size_t len);
2020-06-05 17:31:02 +02:00
size_t write(const VpnPacket& packet);
2020-06-05 16:14:05 +02:00
2020-06-23 15:36:49 +02:00
uint32_t peek_next_seqno() const { return _next_send_seqno; }
uint32_t next_seqno() { return _next_send_seqno++; }
2020-06-10 18:49:36 +02:00
2020-06-26 19:41:55 +02:00
void got_inbound_packet(const VpnPacket& packet);
2020-06-05 16:14:05 +02:00
private:
UdpVpn* _vpn;
sockaddr_in6 _ext_addr;
in6_addr _int_addr;
2020-06-23 15:36:49 +02:00
uint32_t _next_send_seqno;
2020-06-26 19:41:55 +02:00
PacketLossLogger _packet_loss;
2020-06-05 16:14:05 +02:00
};