C rewrite: phase out std::string

This commit is contained in:
Théophile Bastian 2020-07-22 19:38:14 +02:00
parent ecc996f3f0
commit 46473a060e
9 changed files with 18 additions and 49 deletions

View file

@ -7,12 +7,13 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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;

View file

@ -1,15 +1,14 @@
#pragma once
#include <string>
#include <poll.h>
#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;
};

View file

@ -9,6 +9,7 @@
#include <poll.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include "ip_header.hpp"

View file

@ -1,4 +1,5 @@
#include <string.h>
#include <stdio.h>
#include "UdpVpnServer.hpp"
#include "ip_header.hpp"

View file

@ -2,6 +2,8 @@
#include "VpnPeer.hpp"
#include <string.h>
#include <stdio.h>
#include <utility>
const size_t VpnPacket::VPN_HEADER_BYTES = 8;
const size_t VpnControlPacket::TLV_HEADER_BYTES = 3;

View file

@ -1,6 +1,7 @@
#include "UdpVpn.hpp"
#include "congestion_control.hpp"
#include <stdio.h>
#include <stdint.h>
#include <string.h>

View file

@ -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;
}

View file

@ -2,6 +2,7 @@
#include <string.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <time.h>
#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<in6_addr>::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<uint32_t>{}(value) << 1);
}
return out_hash;
}
bool equal_to<in6_addr>::operator()(
const in6_addr& lhs, const in6_addr& rhs) const
{
return memcmp(lhs.s6_addr, rhs.s6_addr, sizeof(lhs.s6_addr)) == 0;
}
}

View file

@ -1,6 +1,5 @@
#pragma once
#include <string>
#include <netinet/in.h>
/* 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<in6_addr> {
public:
size_t operator()(const in6_addr& addr) const;
};
template<>
class equal_to<in6_addr> {
public:
bool operator()(const in6_addr& lhs, const in6_addr& rhs) const;
};
}