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,43 +36,6 @@ 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
@@ -82,8 +45,9 @@ static esp_err_t wifi_gethostbyname_tcpip_ctx(void *param)
82
45
*/
83
46
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool preferV6)
84
47
{
48
+ // IDEA: Rename to getAddressInfo() ?
49
+
85
50
err_t err = ERR_OK;
86
- gethostbynameParameters_t params;
87
51
88
52
// This should generally check if we have a global address assigned to one of the interfaces.
89
53
// 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 +61,44 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool p
97
61
}
98
62
99
63
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
64
65
+ // First check if the host parses as a literal address
105
66
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;
67
+ const char *servname = "0";
68
+ struct addrinfo *res;
69
+ const struct addrinfo hints = {
70
+ .ai_family = AF_UNSPEC,
71
+ .ai_socktype = SOCK_STREAM,
72
+ };
73
+ err = lwip_getaddrinfo(aHostname, servname, &hints, &res);
74
+ if (err == 0)
75
+ {
76
+ if (res->ai_family == AF_INET6)
77
+ {
78
+ struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr;
79
+ // As an array of u8_t
80
+ aResult = IPAddress(IPv6, ipv6->sin6_addr.s6_addr);
81
+ log_d("DNS found IPv6 %s", aResult.toString().c_str());
82
+ }
83
+ else if (res->ai_family == AF_INET)
84
+ {
85
+ struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
86
+ // As a single u32_t
87
+ aResult = IPAddress(ipv4->sin_addr.s_addr);
88
+ log_d("DNS found IPv4 %s", aResult.toString().c_str());
118
89
}
90
+ else
91
+ {
92
+ err = -1;
93
+ }
94
+
95
+ lwip_freeaddrinfo(res);
119
96
}
120
- Network.setStatusBits(NET_DNS_IDLE_BIT);
121
97
}
122
98
if (err == ERR_OK) {
123
99
return 1;
124
100
}
125
- log_e("DNS Failed for '%s' with error '%d' and result '%d' ", aHostname, err, params.result );
101
+ log_e("DNS Failed for '%s' with error '%d'", aHostname, err);
126
102
return err;
127
103
}
128
104
0 commit comments