/
fluffymax2005
/
libwgcpp
Обзор
Документация
Войти
/
fluffymax2005
/
libwgcpp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
2
CI/CD
Аналитика
Безопасность
master
include/wgendpoint.h
128 строк
4 KB
fluffymax2005
.hpp implementations moved to .h files.
07 авг 2026, 21:46
07 авг 2026, 21:46
4e59e8f
Код
Авторство
О чём код?
/* * libwgcpp — C++ wrapper for embeddable-wg-library * Copyright (C) 2026 Ledovskiy Maksim aka fluffymax2005 <santech_montage@mail.ru> * * This file is part of libwgcpp. * * libwgcpp is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation, * either version 3 of the License, or (at your option) any later version. * * libwgcpp is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with libwgcpp. If not, see <https://www.gnu.org/licenses/>. */ /** * @file * @brief Provides wg_endpoint struct class wrapper */ #ifndef WGENDPOINT_H #define WGENDPOINT_H extern "C" { #include "wireguard.h" } #include "threadsafety.h" #include <stdexcept> #include <arpa/inet.h> #include <string> /** * @class WgEndpoint * @brief Wrapper over wg_endpoint struct. * @tparam ThreadPolicy Thread safety policy for using. MultiThreaded is used by default. */ template<typename ThreadPolicy = MultiThreaded> class WgEndpoint { public: /** * @brief Default constructor. Makes WgEndpoint::endpoint as if it contains "0.0.0.0:51820". */ WgEndpoint() noexcept; /** * @brief Copy constructor. Copies this->endpoint from other.endpoint. */ WgEndpoint(const WgEndpoint&) noexcept = default; /** * @brief Copy assignment. Copies this->endpoint from other.endpoint. */ WgEndpoint& operator=(const WgEndpoint&) noexcept = default; /** * @brief Move constructor. Copies this->endpoint from other.endpoint. */ WgEndpoint(WgEndpoint&&) noexcept = default; /** * @brief Move assignment. Copies this->endpoint from other.endpoint. */ WgEndpoint& operator=(WgEndpoint&&) noexcept = default; /** * @brief Creates WgEndpoint instance with given ip address and port. * @param ip string representation of IP address. IPv4 or IPv6. * @param port port number from range [1; 65535] * @return created instance * @throws * - std::invalid_argument if invalid ip address <b>OR</b> port number provided */ static WgEndpoint create(const std::string& ip, uint16_t port); /** * @brief Get endpoint pure wg_endpoint struct to directly read data * @return WgEndpoint::endpoint */ const wg_endpoint& getStruct() const noexcept; private: /** * @brief Pure struct */ wg_endpoint endpoint{}; }; template<typename TP> WgEndpoint<TP>::WgEndpoint() noexcept { // Default Wireguard port + phony ip addr endpoint.addr4.sin_family = AF_INET; endpoint.addr4.sin_port = htons(51820); } template<typename TP> WgEndpoint<TP> WgEndpoint<TP>::create(const std::string& ip, uint16_t port) { WgEndpoint ep; if (port == 0) throw std::invalid_argument("Port number must be nonzero"); if (inet_pton(AF_INET, ip.c_str(), &ep.endpoint.addr4.sin_addr) == 1) { ep.endpoint.addr4.sin_family = AF_INET; ep.endpoint.addr4.sin_port = htons(port); } else if (inet_pton(AF_INET6, ip.c_str(), &ep.endpoint.addr6.sin6_addr) == 1) { ep.endpoint.addr6.sin6_family = AF_INET6; ep.endpoint.addr6.sin6_port = htons(port); } else { throw std::invalid_argument("Invalid IP: " + ip); } return ep; } template<typename TP> const wg_endpoint& WgEndpoint<TP>::getStruct() const noexcept { return endpoint; } #endif // WGENDPOINT_H