From 4710cdc0031513368233ef2bcebff83d3fed4b72 Mon Sep 17 00:00:00 2001 From: ConorShore Date: Sat, 22 Jan 2022 17:59:37 +0000 Subject: [PATCH] Added wg subnet and gateway options --- src/WireGuard-ESP32.h | 1 + src/WireGuard.cpp | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/WireGuard-ESP32.h b/src/WireGuard-ESP32.h index 29820a4..b30c884 100644 --- a/src/WireGuard-ESP32.h +++ b/src/WireGuard-ESP32.h @@ -10,6 +10,7 @@ class WireGuard private: bool _is_initialized = false; public: + bool begin(const IPAddress& localIP, const IPAddress& Subnet, const IPAddress& Gateway, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort); bool begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort); void end(); bool is_initialized() const { return this->_is_initialized; } diff --git a/src/WireGuard.cpp b/src/WireGuard.cpp index 15d5c4e..ce69650 100644 --- a/src/WireGuard.cpp +++ b/src/WireGuard.cpp @@ -29,12 +29,12 @@ static uint8_t wireguard_peer_index = WIREGUARDIF_INVALID_INDEX; #define TAG "[WireGuard] " -bool WireGuard::begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort) { +bool WireGuard::begin(const IPAddress& localIP, const IPAddress& Subnet, const IPAddress& Gateway, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort) { struct wireguardif_init_data wg; struct wireguardif_peer peer; ip_addr_t ipaddr = IPADDR4_INIT(static_cast(localIP)); - ip_addr_t netmask = IPADDR4_INIT_BYTES(255, 255, 255, 255); - ip_addr_t gateway = IPADDR4_INIT_BYTES(0, 0, 0, 0); + ip_addr_t netmask = IPADDR4_INIT(static_cast(Subnet)); + ip_addr_t gateway = IPADDR4_INIT(static_cast(Gateway)); assert(privateKey != NULL); assert(remotePeerAddress != NULL); @@ -119,6 +119,13 @@ bool WireGuard::begin(const IPAddress& localIP, const char* privateKey, const ch return true; } +bool WireGuard::begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort) { + // Maintain compatiblity with old begin + auto subnet = IPAddress(255,255,255,255); + auto gateway = IPAddress(0,0,0,0); + return WireGuard::begin(localIP, subnet, gateway, privateKey, remotePeerAddress, remotePeerPublicKey, remotePeerPort); +} + void WireGuard::end() { if( !this->_is_initialized ) return;