Skip to content

Commit

Permalink
Merge pull request letscontrolit#5031 from TD-er/feature/IPv6_checkbox
Browse files Browse the repository at this point in the history
[IPv6] Add option to enable/disable IPv6
  • Loading branch information
TD-er authored Apr 17, 2024
2 parents 00a68a7 + 7af2865 commit 1eb98a4
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 45 deletions.
16 changes: 3 additions & 13 deletions src/src/DataStructs/EthernetEventData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if FEATURE_ETHERNET

#include "../ESPEasyCore/ESPEasy_Log.h"
#include "../Globals/Settings.h"
#include "../Helpers/Networking.h"

#include <ETH.h>
Expand Down Expand Up @@ -189,20 +190,9 @@ void EthernetEventData_t::markConnected() {
#endif

#if FEATURE_USE_IPV6
ETH.enableIPv6(true);
/*
// workaround for the race condition in LWIP, see https://github.com/espressif/arduino-esp32/pull/9016#discussion_r1451774885
{
uint32_t i = 5; // try 5 times only
while (esp_netif_create_ip6_linklocal(ETH.netif()) != ESP_OK) {
delay(1);
if (i-- == 0) {
// addLog(LOG_LEVEL_ERROR, ">>>> HELP");
break;
}
}
if (Settings.EnableIPv6()) {
ETH.enableIPv6(true);
}
*/
#endif
}

Expand Down
2 changes: 2 additions & 0 deletions src/src/DataStructs/FactoryDefault_WiFi_NVS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void FactoryDefault_WiFi_NVS::fromSettings() {
bits.WaitWiFiConnect = Settings.WaitWiFiConnect();
bits.SDK_WiFi_autoreconnect = Settings.SDK_WiFi_autoreconnect();
bits.HiddenSSID_SlowConnectPerBSSID = Settings.HiddenSSID_SlowConnectPerBSSID();
bits.EnableIPv6 = Settings.EnableIPv6();
}

void FactoryDefault_WiFi_NVS::applyToSettings() const {
Expand All @@ -43,6 +44,7 @@ void FactoryDefault_WiFi_NVS::applyToSettings() const {
Settings.WaitWiFiConnect(bits.WaitWiFiConnect);
Settings.SDK_WiFi_autoreconnect(bits.SDK_WiFi_autoreconnect);
Settings.HiddenSSID_SlowConnectPerBSSID(bits.HiddenSSID_SlowConnectPerBSSID);
Settings.EnableIPv6(bits.EnableIPv6);
}

struct FactoryDefault_WiFi_NVS_securityPrefs {
Expand Down
3 changes: 2 additions & 1 deletion src/src/DataStructs/FactoryDefault_WiFi_NVS.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class FactoryDefault_WiFi_NVS {
uint64_t WaitWiFiConnect : 1;
uint64_t SDK_WiFi_autoreconnect : 1;
uint64_t HiddenSSID_SlowConnectPerBSSID : 1;
uint64_t EnableIPv6 : 1;

uint64_t unused : 52;
uint64_t unused : 51;
} bits;

uint64_t data{};
Expand Down
9 changes: 6 additions & 3 deletions src/src/DataStructs/NodeStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ bool NodeStruct::validate(const IPAddress& remoteIP) {

#if FEATURE_USE_IPV6
// Check if we're in the same global subnet
if (hasIPv6_mac_based_link_global && remoteIP.type() == IPv6) {
if (Settings.EnableIPv6() &&
hasIPv6_mac_based_link_global &&
remoteIP.type() == IPv6) {
const IPAddress this_global = NetworkGlobalIP6();
// Check first 64 bit to see if we're in the same global scope
for (int i = 0; i < 8 && hasIPv6_mac_based_link_global; ++i) {
Expand Down Expand Up @@ -151,7 +153,7 @@ IPAddress NodeStruct::IP() const {
#if FEATURE_USE_IPV6
IPAddress NodeStruct::IPv6_link_local(bool stripZone) const
{
if (hasIPv6_mac_based_link_local) {
if (Settings.EnableIPv6() && hasIPv6_mac_based_link_local) {
// Base IPv6 on MAC address
IPAddress ipv6;
if (IPv6_link_local_from_MAC(sta_mac, ipv6)) {
Expand All @@ -166,7 +168,7 @@ IPAddress NodeStruct::IPv6_link_local(bool stripZone) const

IPAddress NodeStruct::IPv6_global() const
{
if (hasIPv6_mac_based_link_global) {
if (Settings.EnableIPv6() && hasIPv6_mac_based_link_global) {
// Base IPv6 on MAC address
IPAddress ipv6;
if (IPv6_global_from_MAC(sta_mac, ipv6)) {
Expand All @@ -177,6 +179,7 @@ IPAddress NodeStruct::IPv6_global() const
}

bool NodeStruct::hasIPv6() const {
if (!Settings.EnableIPv6()) return false;
return hasIPv6_mac_based_link_local ||
hasIPv6_mac_based_link_global;
}
Expand Down
5 changes: 4 additions & 1 deletion src/src/DataStructs/SettingsStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class SettingsStruct_tmpl
bool HiddenSSID_SlowConnectPerBSSID() const { return !VariousBits_2.HiddenSSID_SlowConnectPerBSSID; }
void HiddenSSID_SlowConnectPerBSSID(bool value) { VariousBits_2.HiddenSSID_SlowConnectPerBSSID = !value; }

bool EnableIPv6() const { return !VariousBits_2.EnableIPv6; }
void EnableIPv6(bool value) { VariousBits_2.EnableIPv6 = !value; }

// Use Espressif's auto reconnect.
bool SDK_WiFi_autoreconnect() const { return VariousBits_2.SDK_WiFi_autoreconnect; }
void SDK_WiFi_autoreconnect(bool value) { VariousBits_2.SDK_WiFi_autoreconnect = value; }
Expand Down Expand Up @@ -519,7 +522,7 @@ class SettingsStruct_tmpl
uint32_t SDK_WiFi_autoreconnect : 1; // Bit 01
uint32_t DisableRulesCodeCompletion : 1; // Bit 02
uint32_t HiddenSSID_SlowConnectPerBSSID : 1; // Bit 03 // inverted
uint32_t unused_04 : 1; // Bit 04
uint32_t EnableIPv6 : 1; // Bit 04 // inverted
uint32_t unused_05 : 1; // Bit 05
uint32_t unused_06 : 1; // Bit 06
uint32_t unused_07 : 1; // Bit 07
Expand Down
13 changes: 3 additions & 10 deletions src/src/DataStructs/WiFiEventData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../ESPEasyCore/ESPEasy_Log.h"

#include "../Globals/RTC.h"
#include "../Globals/Settings.h"
#include "../Globals/WiFi_AP_Candidates.h"

#include "../Helpers/ESPEasy_Storage.h"
Expand Down Expand Up @@ -236,16 +237,8 @@ void WiFiEventData_t::markConnected(const String& ssid, const uint8_t bssid[6],
}
}
#if FEATURE_USE_IPV6
WiFi.enableIPv6(true);
// workaround for the race condition in LWIP, see https://github.com/espressif/arduino-esp32/pull/9016#discussion_r1451774885
{
uint32_t i = 5; // try 5 times only
while (esp_netif_create_ip6_linklocal(get_esp_interface_netif(ESP_IF_WIFI_STA)) != ESP_OK) {
delay(1);
if (i-- == 0) {
break;
}
}
if (Settings.EnableIPv6()) {
WiFi.enableIPv6(true);
}
#endif
}
Expand Down
4 changes: 3 additions & 1 deletion src/src/ESPEasyCore/ESPEasyEth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ bool ETHConnectRelaxed() {
(eth_clock_mode_t)Settings.ETH_Clock_Mode);
#else
#if FEATURE_USE_IPV6
ETH.enableIPv6(true);
if (Settings.EnableIPv6()) {
ETH.enableIPv6(true);
}
#endif

if (isSPI_EthernetType(Settings.ETH_Phy_Type)) {
Expand Down
4 changes: 3 additions & 1 deletion src/src/ESPEasyCore/ESPEasyWifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ void AttemptWiFiConnect() {
const String key = WiFi_AP_CandidatesList::get_key(candidate.index);

#if FEATURE_USE_IPV6
WiFi.enableIPv6(true);
if (Settings.EnableIPv6()) {
WiFi.enableIPv6(true);
}
#endif

if ((Settings.HiddenSSID_SlowConnectPerBSSID() || !candidate.bits.isHidden)
Expand Down
8 changes: 8 additions & 0 deletions src/src/Helpers/StringProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ const __FlashStringHelper * getLabel(LabelType::Enum label) {
case LabelType::CONNECT_HIDDEN_SSID: return F("Include Hidden SSID");
case LabelType::HIDDEN_SSID_SLOW_CONNECT: return F("Hidden SSID Slow Connect");
case LabelType::SDK_WIFI_AUTORECONNECT: return F("Enable SDK WiFi Auto Reconnect");
#if FEATURE_USE_IPV6
case LabelType::ENABLE_IPV6: return F("Enable IPv6");
#endif


case LabelType::BUILD_DESC: return F("Build");
case LabelType::GIT_BUILD: return F("Git Build");
Expand Down Expand Up @@ -509,6 +513,10 @@ String getValue(LabelType::Enum label) {
case LabelType::CONNECT_HIDDEN_SSID: return jsonBool(Settings.IncludeHiddenSSID());
case LabelType::HIDDEN_SSID_SLOW_CONNECT: return jsonBool(Settings.HiddenSSID_SlowConnectPerBSSID());
case LabelType::SDK_WIFI_AUTORECONNECT: return jsonBool(Settings.SDK_WiFi_autoreconnect());
#if FEATURE_USE_IPV6
case LabelType::ENABLE_IPV6: return jsonBool(Settings.EnableIPv6());
#endif


case LabelType::BUILD_DESC: return getSystemBuildString();
case LabelType::GIT_BUILD:
Expand Down
3 changes: 3 additions & 0 deletions src/src/Helpers/StringProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ struct LabelType {
HIDDEN_SSID_SLOW_CONNECT,
CONNECT_HIDDEN_SSID,
SDK_WIFI_AUTORECONNECT,
#if FEATURE_USE_IPV6
ENABLE_IPV6,
#endif

BUILD_DESC,
GIT_BUILD,
Expand Down
8 changes: 8 additions & 0 deletions src/src/WebServer/AdvancedConfigPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ void handle_advanced() {
Settings.WaitWiFiConnect(isFormItemChecked(LabelType::WAIT_WIFI_CONNECT));
Settings.HiddenSSID_SlowConnectPerBSSID(isFormItemChecked(LabelType::HIDDEN_SSID_SLOW_CONNECT));
Settings.SDK_WiFi_autoreconnect(isFormItemChecked(LabelType::SDK_WIFI_AUTORECONNECT));
#if FEATURE_USE_IPV6
Settings.EnableIPv6(isFormItemChecked(LabelType::ENABLE_IPV6));
addFormNote(F("Toggling IPv6 requires reboot"));
#endif



#ifndef BUILD_NO_RAM_TRACKER
Expand Down Expand Up @@ -387,6 +392,9 @@ void handle_advanced() {
addFormCheckBox(LabelType::WAIT_WIFI_CONNECT, Settings.WaitWiFiConnect());
addFormCheckBox(LabelType::SDK_WIFI_AUTORECONNECT, Settings.SDK_WiFi_autoreconnect());
addFormCheckBox(LabelType::HIDDEN_SSID_SLOW_CONNECT, Settings.HiddenSSID_SlowConnectPerBSSID());
#if FEATURE_USE_IPV6
addFormCheckBox(LabelType::ENABLE_IPV6, Settings.EnableIPv6());
#endif



Expand Down
5 changes: 5 additions & 0 deletions src/src/WebServer/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ void handle_json()
#if FEATURE_USE_IPV6
LabelType::IP6_LOCAL,
LabelType::IP6_GLOBAL,
LabelType::ENABLE_IPV6,
#endif
LabelType::IP_SUBNET,
LabelType::GATEWAY,
Expand Down Expand Up @@ -288,6 +289,10 @@ void handle_json()
LabelType::WIFI_USE_LAST_CONN_FROM_RTC,
LabelType::WIFI_RSSI,

LabelType::WAIT_WIFI_CONNECT,
LabelType::HIDDEN_SSID_SLOW_CONNECT,
LabelType::CONNECT_HIDDEN_SSID,
LabelType::SDK_WIFI_AUTORECONNECT,

LabelType::MAX_LABEL
};
Expand Down
32 changes: 20 additions & 12 deletions src/src/WebServer/RootPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ void handle_root() {
{
addRowLabelValue(LabelType::IP_ADDRESS);
#if FEATURE_USE_IPV6
addRowLabelValue(LabelType::IP6_LOCAL);
// Do not show global IPv6 on the root page
if (Settings.EnableIPv6()) {
addRowLabelValue(LabelType::IP6_LOCAL);
// Do not show global IPv6 on the root page
}
#endif
addRowLabel(LabelType::WIFI_RSSI);
addHtml(strformat(
Expand All @@ -213,8 +215,10 @@ void handle_root() {
addRowLabelValue(LabelType::ETH_SPEED_STATE);
addRowLabelValue(LabelType::ETH_IP_ADDRESS);
#if FEATURE_USE_IPV6
addRowLabelValue(LabelType::ETH_IP6_LOCAL);
// Do not show global IPv6 on the root page
if (Settings.EnableIPv6()) {
addRowLabelValue(LabelType::ETH_IP6_LOCAL);
// Do not show global IPv6 on the root page
}
#endif
}
# endif // if FEATURE_ETHERNET
Expand Down Expand Up @@ -337,8 +341,10 @@ void handle_root() {

if (it->second.ip[0] != 0
#if FEATURE_USE_IPV6
|| it->second.hasIPv6_mac_based_link_local
|| it->second.hasIPv6_mac_based_link_global
|| (Settings.EnableIPv6() &&
(it->second.hasIPv6_mac_based_link_local ||
it->second.hasIPv6_mac_based_link_global)
)
#endif
)
{
Expand All @@ -347,12 +353,14 @@ void handle_root() {

#if FEATURE_USE_IPV6
bool isIPv6 = false;
if (it->second.hasIPv6_mac_based_link_local) {
ip = it->second.IPv6_link_local(true);
isIPv6 = true;
} else if (it->second.hasIPv6_mac_based_link_global) {
ip = it->second.IPv6_global();
isIPv6 = true;
if (Settings.EnableIPv6()) {
if (it->second.hasIPv6_mac_based_link_local) {
ip = it->second.IPv6_link_local(true);
isIPv6 = true;
} else if (it->second.hasIPv6_mac_based_link_global) {
ip = it->second.IPv6_global();
isIPv6 = true;
}
}
if (it->second.hasIPv4 && it->second.hasIPv6()) {
// Add 2 buttons for IPv4 and IPv6 address
Expand Down
12 changes: 9 additions & 3 deletions src/src/WebServer/SysInfoPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ void handle_sysinfo_json() {
json_prop(F("dhcp"), useStaticIP() ? getLabel(LabelType::IP_CONFIG_STATIC) : getLabel(LabelType::IP_CONFIG_DYNAMIC));
json_prop(F("ip"), getValue(LabelType::IP_ADDRESS));
#if FEATURE_USE_IPV6
json_prop(F("ip6_local"), getValue(LabelType::IP6_LOCAL));
json_prop(F("ip6_global"), getValue(LabelType::IP6_GLOBAL));
if (Settings.EnableIPv6()) {
json_prop(F("ip6_local"), getValue(LabelType::IP6_LOCAL));
json_prop(F("ip6_global"), getValue(LabelType::IP6_GLOBAL));
}
#endif

json_prop(F("subnet"), getValue(LabelType::IP_SUBNET));
Expand Down Expand Up @@ -153,7 +155,8 @@ void handle_sysinfo_json() {
json_prop(F("ethstate"), getValue(LabelType::ETH_STATE));
json_prop(F("ethspeedstate"), getValue(LabelType::ETH_SPEED_STATE));
#if FEATURE_USE_IPV6
json_prop(F("ethipv6local"), getValue(LabelType::ETH_IP6_LOCAL));
if (Settings.EnableIPv6())
json_prop(F("ethipv6local"), getValue(LabelType::ETH_IP6_LOCAL));
#endif
json_close();
# endif // if FEATURE_ETHERNET
Expand Down Expand Up @@ -542,6 +545,9 @@ void handle_sysinfo_WiFiSettings() {
LabelType::HIDDEN_SSID_SLOW_CONNECT,
LabelType::CONNECT_HIDDEN_SSID,
LabelType::SDK_WIFI_AUTORECONNECT,
#if FEATURE_USE_IPV6
LabelType::ENABLE_IPV6,
#endif

LabelType::MAX_LABEL
};
Expand Down

0 comments on commit 1eb98a4

Please sign in to comment.