Skip to content

feat(dns): Check type of IP addresses and clear DNS cache if they changed #9476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions libraries/Network/src/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ NetworkManager::NetworkManager(){

}

NetworkInterface * getNetifByID(Network_Interface_ID id);

bool NetworkManager::begin(){
static bool initialized = false;
if(!initialized){
Expand Down Expand Up @@ -44,18 +46,11 @@ bool NetworkManager::begin(){
*/
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)
{
err_t err = ERR_OK;

// This should generally check if we have a global address assigned to one of the interfaces.
// If such address is not assigned, there is no point in trying to get V6 from DNS as we will not be able to reach it.
// That is of course, if 'preferV6' is not set to true
static bool hasGlobalV6 = false;
bool hasGlobalV6Now = false;//ToDo: implement this!
if(hasGlobalV6 != hasGlobalV6Now){
hasGlobalV6 = hasGlobalV6Now;
dns_clear_cache();
log_d("Clearing DNS cache");
}
static bool hasGlobalV4 = false;
err_t err = ERR_OK;
const char *servname = "0";
struct addrinfo *res;

aResult = static_cast<uint32_t>(0);

Expand All @@ -64,8 +59,33 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)
return 1;
}

const char *servname = "0";
struct addrinfo *res;
// This should generally check if we have a global address assigned to one of the interfaces.
// If such address is not assigned, there is no point in trying to get V6 from DNS as we will not be able to reach it.
bool hasGlobalV6Now = false;
bool hasGlobalV4Now = false;
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i){
NetworkInterface * iface = getNetifByID((Network_Interface_ID)i);
if(iface != NULL) {
if(iface->hasGlobalIPv6()) {
hasGlobalV6Now = true;
}
if(iface->hasIP()) {
hasGlobalV4Now = true;
}
}
if (hasGlobalV6Now && hasGlobalV4Now){
break;
}
}

// If the state of IP addresses has changed, clear the DNS cache
if(hasGlobalV6 != hasGlobalV6Now || hasGlobalV4 != hasGlobalV4Now){
hasGlobalV6 = hasGlobalV6Now;
hasGlobalV4 = hasGlobalV4Now;
dns_clear_cache();
log_d("Clearing DNS cache");
}

struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
Expand Down Expand Up @@ -130,8 +150,6 @@ bool NetworkManager::setHostname(const char * name)
return true;
}

NetworkInterface * getNetifByID(Network_Interface_ID id);

bool NetworkManager::setDefaultInterface(NetworkInterface & ifc)
{
return ifc.setDefault();
Expand Down