From 46473a060e068dcae96c1c1ea36a5a6cdde9cce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Wed, 22 Jul 2020 19:38:14 +0200 Subject: [PATCH] C rewrite: phase out std::string --- TunDevice.cpp | 13 +++++++------ TunDevice.hpp | 7 +++---- UdpVpn.cpp | 1 + UdpVpnServer.cpp | 1 + VpnPacket.cpp | 2 ++ VpnPeer.cpp | 1 + congestion_control.cpp | 3 ++- util.cpp | 21 +-------------------- util.hpp | 18 ------------------ 9 files changed, 18 insertions(+), 49 deletions(-) diff --git a/TunDevice.cpp b/TunDevice.cpp index 0ac330c..ac7f4b1 100644 --- a/TunDevice.cpp +++ b/TunDevice.cpp @@ -7,12 +7,13 @@ #include #include #include +#include #include "TunDevice.hpp" static const size_t TUN_MTU = 1500; // TODO determine this cleanly -TunDevice::TunDevice(const std::string& dev) +TunDevice::TunDevice(const char* dev) { struct ifreq ifr; int fd; @@ -30,12 +31,12 @@ TunDevice::TunDevice(const std::string& dev) * IFF_NO_PI - Do not provide packet information */ ifr.ifr_flags = IFF_TUN | IFF_NO_PI; - if(!dev.empty()) { - if(dev.size() >= IFNAMSIZ - 2) { + if(dev != nullptr) { + if(strlen(dev) >= IFNAMSIZ - 2) { fprintf(stderr, "Tun device: device name is too long.\n"); exit(1); } - strncpy(ifr.ifr_name, dev.c_str(), IFNAMSIZ-1); + strncpy(ifr.ifr_name, dev, IFNAMSIZ-1); } if(ioctl(fd, TUNSETIFF, (void *) &ifr) < 0){ @@ -79,7 +80,7 @@ uint16_t TunDevice::get_mtu() const { struct ifreq ifr; ifr.ifr_addr.sa_family = AF_INET6; - strncpy(ifr.ifr_name, _dev_name.c_str(), sizeof(ifr.ifr_name)-1); + strncpy(ifr.ifr_name, _dev_name, sizeof(ifr.ifr_name)-1); if (ioctl(sockfd, SIOCGIFMTU, (caddr_t)&ifr) < 0) return 0; close(sockfd); @@ -91,7 +92,7 @@ bool TunDevice::set_mtu(uint16_t mtu) { struct ifreq ifr; ifr.ifr_addr.sa_family = AF_INET6; - strncpy(ifr.ifr_name, _dev_name.c_str(), sizeof(ifr.ifr_name)-1); + strncpy(ifr.ifr_name, _dev_name, sizeof(ifr.ifr_name)-1); ifr.ifr_mtu = mtu; if (ioctl(sockfd, SIOCSIFMTU, (caddr_t)&ifr) < 0) return false; diff --git a/TunDevice.hpp b/TunDevice.hpp index 7a54491..1d82024 100644 --- a/TunDevice.hpp +++ b/TunDevice.hpp @@ -1,15 +1,14 @@ #pragma once -#include #include #include "util.hpp" class TunDevice { public: - TunDevice(const std::string& dev); + TunDevice(const char* dev); ~TunDevice(); - const std::string& get_dev_name() const { return _dev_name; } + const char* get_dev_name() const { return _dev_name; } int get_fd() const { return _fd; } /** Get the interface's MTU */ @@ -31,7 +30,7 @@ class TunDevice { private: int _fd; - std::string _dev_name; + char*_dev_name; struct pollfd _poll_fd; size_t _last_read_size; }; diff --git a/UdpVpn.cpp b/UdpVpn.cpp index b7cb9eb..fb8262b 100644 --- a/UdpVpn.cpp +++ b/UdpVpn.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "ip_header.hpp" diff --git a/UdpVpnServer.cpp b/UdpVpnServer.cpp index 40cba72..27721db 100644 --- a/UdpVpnServer.cpp +++ b/UdpVpnServer.cpp @@ -1,4 +1,5 @@ #include +#include #include "UdpVpnServer.hpp" #include "ip_header.hpp" diff --git a/VpnPacket.cpp b/VpnPacket.cpp index ebec760..ed1fd6b 100644 --- a/VpnPacket.cpp +++ b/VpnPacket.cpp @@ -2,6 +2,8 @@ #include "VpnPeer.hpp" #include +#include +#include const size_t VpnPacket::VPN_HEADER_BYTES = 8; const size_t VpnControlPacket::TLV_HEADER_BYTES = 3; diff --git a/VpnPeer.cpp b/VpnPeer.cpp index a424853..3b48373 100644 --- a/VpnPeer.cpp +++ b/VpnPeer.cpp @@ -1,6 +1,7 @@ #include "UdpVpn.hpp" #include "congestion_control.hpp" +#include #include #include diff --git a/congestion_control.cpp b/congestion_control.cpp index 2e2341b..7f1d120 100644 --- a/congestion_control.cpp +++ b/congestion_control.cpp @@ -47,5 +47,6 @@ void CongestionController::update_lossbased() { void CongestionController::update_params() { _bandwidth = _loss_based.bandwidth; // meant to integrate other controllers _bucket_max_level = _bandwidth * _peer.get_rtt().avg_rtt(); - _bucket_level = std::min(_bucket_level, _bucket_max_level); + if(_bucket_level > _bucket_max_level) + _bucket_level = _bucket_max_level; } diff --git a/util.cpp b/util.cpp index b6c782b..c65f4f3 100644 --- a/util.cpp +++ b/util.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "util.hpp" @@ -89,23 +90,3 @@ uint32_t current_us_timestamp() { clock_gettime(CLOCK_MONOTONIC, &now); return (now.tv_sec * 1000*1000) + (now.tv_nsec / 1000); } - -namespace std { -size_t hash::operator() (const in6_addr& addr) const { - size_t out_hash = 0; - for(int i=0; i < 4; ++i) { - uint32_t value; - memcpy((unsigned char*)(&value), - addr.s6_addr + 4*i, - 4); - out_hash ^= (std::hash{}(value) << 1); - } - return out_hash; -} - -bool equal_to::operator()( - const in6_addr& lhs, const in6_addr& rhs) const -{ - return memcmp(lhs.s6_addr, rhs.s6_addr, sizeof(lhs.s6_addr)) == 0; -} -} diff --git a/util.hpp b/util.hpp index 85b5c65..7db205e 100644 --- a/util.hpp +++ b/util.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include /* Debugging -- taken from babeld */ @@ -78,20 +77,3 @@ uint32_t timespec_us_ellapsed(const struct timespec ref); /** Get the current timestamp, in microseconds */ uint32_t current_us_timestamp(); - - - -/** in6_addr hash & equality */ -namespace std { - template<> - class hash { - public: - size_t operator()(const in6_addr& addr) const; - }; - - template<> - class equal_to { - public: - bool operator()(const in6_addr& lhs, const in6_addr& rhs) const; - }; -}