C rewrite: phase out smart pointers

This commit is contained in:
Théophile Bastian 2020-07-22 17:04:22 +02:00
parent b012375427
commit 7c1e5562f2
10 changed files with 47 additions and 26 deletions

View file

@ -170,7 +170,7 @@ void UdpVpn::receive_from_tun() {
debugf("Dropping packet: no peer yet.\n");
return;
}
packet.set_peer(_peer.get());
packet.set_peer(_peer);
kdebugf("Transmitting %s -> %s, size %d\n",
format_address(packet.get_ipv6_header().source.s6_addr),
@ -189,7 +189,7 @@ void UdpVpn::receive_from_udp() {
return;
// If we don't have a peer yet -- we're just setting the peer to nullptr.
packet.set_peer(_peer.get());
packet.set_peer(_peer);
if(packet.is_control()) {
VpnControlPacket ctrl_packet(std::move(packet));

View file

@ -72,7 +72,7 @@ class UdpVpn {
size_t _vpn_mtu;
TunDevice _tun_dev;
std::unique_ptr<VpnPeer> _peer;
VpnPeer* _peer;
struct timespec
_last_control_sent, /**< A control is offered to be sent approx.

View file

@ -4,7 +4,11 @@
#include "ip_header.hpp"
UdpVpnClient::UdpVpnClient(const struct sockaddr_in6& server) : UdpVpn() {
_peer = std::make_unique<VpnPeer>(this, server, in6addr_any);
_peer = new VpnPeer(this, server, in6addr_any);
}
UdpVpnClient::~UdpVpnClient() {
delete _peer;
}
void UdpVpnClient::acquire_peer(

View file

@ -5,6 +5,7 @@
class UdpVpnClient: public UdpVpn {
public:
UdpVpnClient(const struct sockaddr_in6& server);
~UdpVpnClient();
protected:
virtual void acquire_peer(

View file

@ -15,6 +15,11 @@ UdpVpnServer::UdpVpnServer(const struct in6_addr& bind_addr6, in_port_t port)
bind(bind_addr6, port);
}
UdpVpnServer::~UdpVpnServer() {
if(_peer != nullptr)
delete _peer;
}
void UdpVpnServer::acquire_peer(
VpnDataPacket& packet,
const sockaddr_in6& peer_ext_addr)
@ -26,9 +31,9 @@ void UdpVpnServer::acquire_peer(
if(!packet.parse_as_ipv6())
return;
const in6_addr& peer_inner_addr = packet.get_ipv6_header().source;
_peer = std::make_unique<VpnPeer>(this, peer_ext_addr, peer_inner_addr);
_peer = new VpnPeer(this, peer_ext_addr, peer_inner_addr);
packet.set_peer(_peer.get());
packet.set_peer(_peer);
debugf("Got new peer %s:%d -- %s\n",
format_address(peer_ext_addr.sin6_addr.s6_addr),

View file

@ -1,13 +1,13 @@
#pragma once
#include <memory>
#include "UdpVpn.hpp"
class UdpVpnServer: public UdpVpn {
public:
UdpVpnServer(in_port_t port);
UdpVpnServer(const struct in6_addr& bind_addr6, in_port_t port);
~UdpVpnServer();
protected:
virtual void acquire_peer(
VpnDataPacket& packet,

View file

@ -19,10 +19,12 @@ VpnPacket::VpnPacket(size_t mtu, bool inbound)
: _peer(nullptr), _inbound(inbound), _data_space(mtu-OUTER_HEADERS_BYTES),
_data_size(VPN_HEADER_BYTES), _reception_timestamp(0)
{
_data = std::unique_ptr<char[]>(new char[mtu - OUTER_HEADERS_BYTES]);
_data = (char*) malloc(sizeof(char) * (mtu - OUTER_HEADERS_BYTES));
}
VpnPacket::~VpnPacket() {}
VpnPacket::~VpnPacket() {
free(_data);
}
VpnPacket::VpnPacket(VpnPacket&& move_from) :
_peer(move_from._peer),
@ -44,31 +46,31 @@ void VpnPacket::set_peer(VpnPeer* peer) {
}
uint32_t VpnPacket::get_seqno() const {
return ntohl(*(uint32_t*)(_data.get() + DATA_SEQNO_POS));
return ntohl(*(uint32_t*)(_data + DATA_SEQNO_POS));
}
uint32_t VpnPacket::get_sending_timestamp() const {
return ntohl(
*(uint32_t*)(_data.get() + DATA_TIMESTAMP_POS) & 0x7fffffffUL
*(uint32_t*)(_data + DATA_TIMESTAMP_POS) & 0x7fffffffUL
);
}
bool VpnPacket::is_control() const {
return *(unsigned char*)(_data.get() + DATA_CTRLBIT_POS) & 0x80;
return *(unsigned char*)(_data + DATA_CTRLBIT_POS) & 0x80;
}
void VpnPacket::set_control(bool is_control) {
unsigned char* ctrl_field =
(unsigned char*) (_data.get() + DATA_CTRLBIT_POS);
(unsigned char*) (_data + DATA_CTRLBIT_POS);
*ctrl_field &= 0x7f;
if(is_control)
*ctrl_field |= 0x80;
}
void VpnPacket::prepare_for_sending() {
uint32_t* ts_field = (uint32_t*) (_data.get() + DATA_TIMESTAMP_POS);
uint32_t* ts_field = (uint32_t*) (_data + DATA_TIMESTAMP_POS);
*ts_field &= htonl(0x80000000UL);
*(uint32_t*)(_data.get() + DATA_SEQNO_POS) = htonl(next_seqno());
*(uint32_t*)(_data + DATA_SEQNO_POS) = htonl(next_seqno());
*ts_field |= htonl(to_us_timestamp(current_us_timestamp()));
}

View file

@ -3,7 +3,7 @@
/** A packet to be transmitted or received over the VPN socket */
#include <stdlib.h>
#include <memory>
#include <exception>
#include "ip_header.hpp"
@ -74,9 +74,9 @@ class VpnPacket {
/// Get a pointer to the packet payload (const version)
const char* get_payload() const {
return _data.get() + VPN_HEADER_BYTES; }
return _data + VPN_HEADER_BYTES; }
/// Get a pointer to the packet payload
char* get_payload() { return _data.get() + VPN_HEADER_BYTES; }
char* get_payload() { return _data + VPN_HEADER_BYTES; }
/// Get a pointer to the first free byte of the packet payload
char* get_next_payload() { return get_payload() + get_payload_size(); }
/// Get the space allocated for the packet payload
@ -94,9 +94,9 @@ class VpnPacket {
_data_size += payload_size_increment; }
/// Get a pointer to the full packet data (const version)
const char* get_data() const { return _data.get(); }
const char* get_data() const { return _data; }
/// Get a pointer to the full packet data
char* get_data() { return _data.get(); }
char* get_data() { return _data; }
/// Get the space allocated for the packet
size_t get_data_space() const { return _data_space; }
/// Get the total current size of the packet
@ -134,7 +134,7 @@ class VpnPacket {
VpnPeer* _peer; // raw pointer: we do not own the peer in any way
bool _inbound; ///< is the packet received or sent?
std::unique_ptr<char[]> _data;
char* _data;
size_t _data_space, _data_size;
uint32_t _reception_timestamp;

View file

@ -11,12 +11,18 @@ VpnPeer::VpnPeer(UdpVpn* vpn, const sockaddr_in6& ext_addr,
const in6_addr& int_addr)
: _vpn(vpn), _ext_addr(ext_addr), _int_addr(int_addr), _next_send_seqno(0),
_tot_bytes_sent(0), _prev_tot_bytes_sent(0),
_congestion_controller(*this)
_congestion_controller(*this),
_next_control_packet(nullptr)
{
clock_gettime(CLOCK_MONOTONIC, &_prev_tick_time);
cycle_next_control();
}
VpnPeer::~VpnPeer() {
if(_next_control_packet != nullptr)
delete _next_control_packet;
}
void VpnPeer::make_loss_report() {
VpnTlvLossReport report = VpnTlvLossReport::create(*_next_control_packet);
report.set_report_seqno(_packet_loss.get_cur_seqno());
@ -24,8 +30,9 @@ void VpnPeer::make_loss_report() {
}
void VpnPeer::cycle_next_control() {
_next_control_packet =
std::make_unique<VpnControlPacket>(_vpn->get_mtu(), false);
if(_next_control_packet != nullptr)
delete _next_control_packet;
_next_control_packet = new VpnControlPacket(_vpn->get_mtu(), false);
_next_control_packet->set_peer(this);
}

View file

@ -91,6 +91,8 @@ class VpnPeer {
VpnPeer(UdpVpn* vpn, const sockaddr_in6& ext_addr,
const in6_addr& int_addr);
~VpnPeer();
const sockaddr_in6& get_ext_addr() const { return _ext_addr; }
const in6_addr& get_int_addr() const { return _int_addr; }
@ -145,5 +147,5 @@ class VpnPeer {
RTTLogger _rtt;
CongestionController _congestion_controller;
std::unique_ptr<VpnControlPacket> _next_control_packet;
VpnControlPacket* _next_control_packet;
};