@@ -14,6 +14,8 @@ NetworkManager::NetworkManager(){
14
14
15
15
}
16
16
17
+ NetworkInterface * getNetifByID (Network_Interface_ID id);
18
+
17
19
bool NetworkManager::begin (){
18
20
static bool initialized = false ;
19
21
if (!initialized){
@@ -44,18 +46,11 @@ bool NetworkManager::begin(){
44
46
*/
45
47
int NetworkManager::hostByName (const char * aHostname, IPAddress& aResult)
46
48
{
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
52
49
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;
59
54
60
55
aResult = static_cast <uint32_t >(0 );
61
56
@@ -64,8 +59,33 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)
64
59
return 1 ;
65
60
}
66
61
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
+
69
89
struct addrinfo hints;
70
90
memset (&hints, 0 , sizeof (hints));
71
91
hints.ai_family = AF_UNSPEC;
@@ -130,8 +150,6 @@ bool NetworkManager::setHostname(const char * name)
130
150
return true ;
131
151
}
132
152
133
- NetworkInterface * getNetifByID (Network_Interface_ID id);
134
-
135
153
bool NetworkManager::setDefaultInterface (NetworkInterface & ifc)
136
154
{
137
155
return ifc.setDefault ();
0 commit comments