working commit
This commit is contained in:
+20
-5
@@ -7,11 +7,26 @@ extern "C" {
|
||||
#include <format>
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
#include <networkaux.hpp>
|
||||
#include <stringaux.hpp>
|
||||
|
||||
std::expected<uint32_t, std::string> netprefix(const std::string network) {
|
||||
uint netcapa6(const uint prefix) {
|
||||
int hostBits = 128 - prefix;
|
||||
int totalIPs = std::pow(2, hostBits);
|
||||
return totalIPs;
|
||||
}
|
||||
|
||||
|
||||
uint netcapa4(const uint prefix) {
|
||||
int hostBits = 32 - prefix;
|
||||
int totalIPs = std::pow(2, hostBits);
|
||||
return totalIPs;
|
||||
}
|
||||
|
||||
|
||||
std::expected<uint, std::string> netprefix(const std::string network) {
|
||||
auto hostprefix = split(network, "/");
|
||||
if (hostprefix.size() < 2) {
|
||||
return std::unexpected("Incorrect network definition");
|
||||
@@ -32,13 +47,13 @@ std::expected<std::string, std::string> network(const std::string network) {
|
||||
}
|
||||
|
||||
|
||||
std::expected<std::string, std::string> nethost6(std::string network, int prefix, uint32_t num) {
|
||||
std::expected<std::string, std::string> nethost6(std::string network, uint prefix, uint32_t num) {
|
||||
struct in6_addr addr;
|
||||
unsigned char mask[16] = {0};
|
||||
if (inet_pton(AF_INET6, network.data(), &addr) != 1) {
|
||||
return std::unexpected(std::format("Invalid network address {}", network));
|
||||
}
|
||||
for (int i = 0; i < prefix; i++) {
|
||||
for (uint i = 0; i < prefix; i++) {
|
||||
mask[i / 8] |= (1 << (7 - (i % 8)));
|
||||
}
|
||||
for (int i = 0; i < 16; i++) {
|
||||
@@ -57,7 +72,7 @@ std::expected<std::string, std::string> nethost6(std::string network, int prefix
|
||||
}
|
||||
|
||||
|
||||
std::expected<std::string, std::string> nethost4(std::string network, int prefix, uint32_t num) {
|
||||
std::expected<std::string, std::string> nethost4(std::string network, uint prefix, uint num) {
|
||||
struct in_addr inaddr;
|
||||
if (inet_pton(AF_INET, network.data(), &inaddr) != 1) {
|
||||
return std::unexpected(std::format("Invalid network address {}", network));
|
||||
@@ -70,7 +85,7 @@ std::expected<std::string, std::string> nethost4(std::string network, int prefix
|
||||
return std::string(inet_ntoa(ip_addr));
|
||||
}
|
||||
|
||||
std::expected<std::string, std::string> nethost(std::string network, int prefix, uint32_t num) {
|
||||
std::expected<std::string, std::string> nethost(std::string network, uint prefix, uint num) {
|
||||
struct sockaddr_in sa;
|
||||
if (inet_pton(AF_INET, network.data(), &(sa.sin_addr)) == 1) {
|
||||
return nethost4(network, prefix, num);
|
||||
|
||||
+2
-2
@@ -4,8 +4,8 @@
|
||||
#include <expected>
|
||||
#include <string>
|
||||
|
||||
std::expected<std::string, std::string> nethost(std::string network, int prefix, uint32_t num);
|
||||
std::expected<uint32_t, std::string> netprefix(const std::string network);
|
||||
std::expected<std::string, std::string> nethost(std::string network, uint prefix, uint num);
|
||||
std::expected<uint, std::string> netprefix(const std::string network);
|
||||
std::expected<std::string, std::string> network(const std::string network);
|
||||
|
||||
#endif
|
||||
|
||||
+14
-1
@@ -13,7 +13,6 @@ extern "C" {
|
||||
#include <uxlogger.hpp>
|
||||
#include <networkaux.hpp>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
TunService::TunService(int svcport, std::string itunnelnet, std::vector<std::string> ilocalnets) {
|
||||
listenport = svcport;
|
||||
@@ -21,6 +20,20 @@ TunService::TunService(int svcport, std::string itunnelnet, std::vector<std::str
|
||||
localnets = ilocalnets;
|
||||
}
|
||||
|
||||
std::expected<void, std::string> TunService::Init(void) {
|
||||
auto netprefixRes = netprefix(tunnelnet);
|
||||
if (!netprefixRes) {
|
||||
return std::unexpected(netprefixRes.error());
|
||||
}
|
||||
auto networkRes = network(tunnelnet);
|
||||
if (!netprefixRes) {
|
||||
return std::unexpected(networkRes.error());
|
||||
};
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
TunService::~TunService() {
|
||||
close(listenport);
|
||||
}
|
||||
|
||||
+3
-5
@@ -8,10 +8,7 @@
|
||||
|
||||
#include <sockhand.hpp>
|
||||
|
||||
class ClientSlot {
|
||||
private:
|
||||
bool free;
|
||||
int num;
|
||||
class NetworkSlot {
|
||||
};
|
||||
|
||||
|
||||
@@ -19,11 +16,12 @@ class TunService {
|
||||
private:
|
||||
std::string tunnelnet;
|
||||
std::vector<std::string> localnets;
|
||||
std::vector<NetworkSlot> netslots;
|
||||
int listenport;
|
||||
int sock;
|
||||
std::vector<ClientSlot> clientSlots;
|
||||
public:
|
||||
explicit TunService(int port, std::string tunnelnet, std::vector<std::string> localnets);
|
||||
std::expected<void, std::string> Init(void);
|
||||
std::expected<void, std::string> Bind(void);
|
||||
std::expected<void, std::string> Listen(void);
|
||||
void Handle(int sock);
|
||||
|
||||
Reference in New Issue
Block a user