5
5
*/
6
6
#include " NetworkManager.h"
7
7
#include " NetworkInterface.h"
8
+ #include " IPAddress.h"
8
9
#include " esp_netif.h"
9
- #include " lwip/ip_addr.h"
10
10
#include " lwip/dns.h"
11
- #include " esp32-hal-log.h"
12
11
#include " esp_mac.h"
12
+ #include " netdb.h"
13
13
14
14
NetworkManager::NetworkManager (){
15
15
@@ -36,54 +36,16 @@ bool NetworkManager::begin(){
36
36
return initialized;
37
37
}
38
38
39
- typedef struct gethostbynameParameters {
40
- const char *hostname;
41
- ip_addr_t addr;
42
- uint8_t addr_type;
43
- int result;
44
- } gethostbynameParameters_t;
45
-
46
- /* *
47
- * DNS callback
48
- * @param name
49
- * @param ipaddr
50
- * @param callback_arg
51
- */
52
- static void wifi_dns_found_callback (const char *name, const ip_addr_t *ipaddr, void *callback_arg)
53
- {
54
- gethostbynameParameters_t *parameters = static_cast <gethostbynameParameters_t *>(callback_arg);
55
- if (ipaddr) {
56
- if (parameters->result == 0 ){
57
- memcpy (&(parameters->addr ), ipaddr, sizeof (ip_addr_t ));
58
- parameters->result = 1 ;
59
- }
60
- } else {
61
- parameters->result = -1 ;
62
- }
63
- Network.setStatusBits (NET_DNS_DONE_BIT);
64
- }
65
-
66
- /* *
67
- * Callback to execute dns_gethostbyname in lwIP's TCP/IP context
68
- * @param param Parameters for dns_gethostbyname call
69
- */
70
- static esp_err_t wifi_gethostbyname_tcpip_ctx (void *param)
71
- {
72
- gethostbynameParameters_t *parameters = static_cast <gethostbynameParameters_t *>(param);
73
- return dns_gethostbyname_addrtype (parameters->hostname , ¶meters->addr , &wifi_dns_found_callback, parameters, parameters->addr_type );
74
- }
75
-
76
39
/* *
77
40
* Resolve the given hostname to an IP address.
78
41
* @param aHostname Name to be resolved
79
42
* @param aResult IPAddress structure to store the returned IP address
80
43
* @return 1 if aIPAddrString was successfully converted to an IP address,
81
44
* else error code
82
45
*/
83
- int NetworkManager::hostByName (const char * aHostname, IPAddress& aResult, bool preferV6 )
46
+ int NetworkManager::hostByName (const char * aHostname, IPAddress& aResult)
84
47
{
85
48
err_t err = ERR_OK;
86
- gethostbynameParameters_t params;
87
49
88
50
// This should generally check if we have a global address assigned to one of the interfaces.
89
51
// 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.
@@ -97,32 +59,41 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool p
97
59
}
98
60
99
61
aResult = static_cast <uint32_t >(0 );
100
- params.hostname = aHostname;
101
- params.addr_type = (preferV6 || hasGlobalV6)?LWIP_DNS_ADDRTYPE_IPV6_IPV4:LWIP_DNS_ADDRTYPE_IPV4;
102
- params.result = 0 ;
103
- aResult.to_ip_addr_t (&(params.addr ));
104
-
105
- if (!aResult.fromString (aHostname)) {
106
- Network.waitStatusBits (NET_DNS_IDLE_BIT, 16000 );
107
- Network.clearStatusBits (NET_DNS_IDLE_BIT | NET_DNS_DONE_BIT);
108
-
109
- err = esp_netif_tcpip_exec (wifi_gethostbyname_tcpip_ctx, ¶ms);
110
- if (err == ERR_OK) {
111
- aResult.from_ip_addr_t (&(params.addr ));
112
- } else if (err == ERR_INPROGRESS) {
113
- Network.waitStatusBits (NET_DNS_DONE_BIT, 15000 ); // real internal timeout in lwip library is 14[s]
114
- Network.clearStatusBits (NET_DNS_DONE_BIT);
115
- if (params.result == 1 ) {
116
- aResult.from_ip_addr_t (&(params.addr ));
117
- err = ERR_OK;
118
- }
119
- }
120
- Network.setStatusBits (NET_DNS_IDLE_BIT);
62
+
63
+ // First check if the host parses as a literal address
64
+ if (aResult.fromString (aHostname)) {
65
+ return 1 ;
121
66
}
122
- if (err == ERR_OK) {
67
+
68
+ const char *servname = " 0" ;
69
+ struct addrinfo *res;
70
+ const struct addrinfo hints = {
71
+ .ai_family = AF_UNSPEC,
72
+ .ai_socktype = SOCK_STREAM,
73
+ };
74
+ err = lwip_getaddrinfo (aHostname, servname, &hints, &res);
75
+ if (err == ERR_OK)
76
+ {
77
+ if (res->ai_family == AF_INET6)
78
+ {
79
+ struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr ;
80
+ // As an array of u8_t
81
+ aResult = IPAddress (IPv6, ipv6->sin6_addr .s6_addr );
82
+ log_d (" DNS found IPv6 %s" , aResult.toString ().c_str ());
83
+ }
84
+ else
85
+ {
86
+ struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr ;
87
+ // As a single u32_t
88
+ aResult = IPAddress (ipv4->sin_addr .s_addr );
89
+ log_d (" DNS found IPv4 %s" , aResult.toString ().c_str ());
90
+ }
91
+
92
+ lwip_freeaddrinfo (res);
123
93
return 1 ;
124
94
}
125
- log_e (" DNS Failed for '%s' with error '%d' and result '%d'" , aHostname, err, params.result );
95
+
96
+ log_e (" DNS Failed for '%s' with error '%d'" , aHostname, err);
126
97
return err;
127
98
}
128
99
0 commit comments