Skip to content

Commit 858b107

Browse files
authored
feat(dns): Check type of IP addresses and clear DNS cache if they changed (espressif#9476)
1 parent 1788867 commit 858b107

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

libraries/Network/src/NetworkManager.cpp

+33-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ NetworkManager::NetworkManager(){
1414

1515
}
1616

17+
NetworkInterface * getNetifByID(Network_Interface_ID id);
18+
1719
bool NetworkManager::begin(){
1820
static bool initialized = false;
1921
if(!initialized){
@@ -44,18 +46,11 @@ bool NetworkManager::begin(){
4446
*/
4547
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)
4648
{
47-
err_t err = ERR_OK;
48-
49-
// This should generally check if we have a global address assigned to one of the interfaces.
50-
// 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.
51-
// That is of course, if 'preferV6' is not set to true
5249
static bool hasGlobalV6 = false;
53-
bool hasGlobalV6Now = false;//ToDo: implement this!
54-
if(hasGlobalV6 != hasGlobalV6Now){
55-
hasGlobalV6 = hasGlobalV6Now;
56-
dns_clear_cache();
57-
log_d("Clearing DNS cache");
58-
}
50+
static bool hasGlobalV4 = false;
51+
err_t err = ERR_OK;
52+
const char *servname = "0";
53+
struct addrinfo *res;
5954

6055
aResult = static_cast<uint32_t>(0);
6156

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

67-
const char *servname = "0";
68-
struct addrinfo *res;
62+
// This should generally check if we have a global address assigned to one of the interfaces.
63+
// 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.
64+
bool hasGlobalV6Now = false;
65+
bool hasGlobalV4Now = false;
66+
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i){
67+
NetworkInterface * iface = getNetifByID((Network_Interface_ID)i);
68+
if(iface != NULL) {
69+
if(iface->hasGlobalIPv6()) {
70+
hasGlobalV6Now = true;
71+
}
72+
if(iface->hasIP()) {
73+
hasGlobalV4Now = true;
74+
}
75+
}
76+
if (hasGlobalV6Now && hasGlobalV4Now){
77+
break;
78+
}
79+
}
80+
81+
// If the state of IP addresses has changed, clear the DNS cache
82+
if(hasGlobalV6 != hasGlobalV6Now || hasGlobalV4 != hasGlobalV4Now){
83+
hasGlobalV6 = hasGlobalV6Now;
84+
hasGlobalV4 = hasGlobalV4Now;
85+
dns_clear_cache();
86+
log_d("Clearing DNS cache");
87+
}
88+
6989
struct addrinfo hints;
7090
memset(&hints, 0, sizeof(hints));
7191
hints.ai_family = AF_UNSPEC;
@@ -130,8 +150,6 @@ bool NetworkManager::setHostname(const char * name)
130150
return true;
131151
}
132152

133-
NetworkInterface * getNetifByID(Network_Interface_ID id);
134-
135153
bool NetworkManager::setDefaultInterface(NetworkInterface & ifc)
136154
{
137155
return ifc.setDefault();

0 commit comments

Comments
 (0)