-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathNetworkManager.cpp
201 lines (175 loc) · 5.5 KB
/
NetworkManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "NetworkManager.h"
#include "IPAddress.h"
#include "esp_netif.h"
#include "lwip/dns.h"
#include "esp_mac.h"
#include "netdb.h"
#if CONFIG_ESP_WIFI_REMOTE_ENABLED
extern "C" esp_err_t esp_hosted_init(void *);
#endif
NetworkManager::NetworkManager() {}
NetworkInterface *getNetifByID(Network_Interface_ID id);
bool NetworkManager::begin() {
static bool initialized = false;
if (!initialized) {
initialized = true;
#if CONFIG_ESP_WIFI_REMOTE_ENABLED
esp_hosted_init(NULL);
#endif
#if CONFIG_IDF_TARGET_ESP32
uint8_t mac[8];
if (esp_efuse_mac_get_default(mac) == ESP_OK) {
esp_base_mac_addr_set(mac);
}
#endif
initialized = esp_netif_init() == ESP_OK;
if (!initialized) {
log_e("esp_netif_init failed!");
}
}
if (initialized) {
initNetworkEvents();
}
return initialized;
}
/**
* Resolve the given hostname to an IP address.
* @param aHostname Name to be resolved
* @param aResult IPAddress structure to store the returned IP address
* @return 1 if aIPAddrString was successfully converted to an IP address,
* else error code
*/
int NetworkManager::hostByName(const char *aHostname, IPAddress &aResult) {
static bool hasGlobalV6 = false;
static bool hasGlobalV4 = false;
err_t err = ERR_OK;
const char *servname = "0";
struct addrinfo *res;
aResult = static_cast<uint32_t>(0);
// First check if the host parses as a literal address
if (aResult.fromString(aHostname)) {
return 1;
}
// 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_socktype = SOCK_STREAM;
// **Workaround**
// LWIP AF_UNSPEC always prefers IPv4 and doesn't check what network is
// available. See https://github.com/espressif/esp-idf/issues/13255
// Until that is fixed, as a work around if we have a global scope IPv6,
// then we check IPv6 only first.
if (hasGlobalV6) {
hints.ai_family = AF_INET6;
err = lwip_getaddrinfo(aHostname, servname, &hints, &res);
if (err == ERR_OK) {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr;
// As an array of u8_t
aResult = IPAddress(IPv6, ipv6->sin6_addr.s6_addr);
log_d("DNS found IPv6 first %s", aResult.toString().c_str());
lwip_freeaddrinfo(res);
return 1;
}
}
// **End Workaround**
hints.ai_family = AF_UNSPEC;
err = lwip_getaddrinfo(aHostname, servname, &hints, &res);
if (err == ERR_OK) {
if (res->ai_family == AF_INET6) {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr;
// As an array of u8_t
aResult = IPAddress(IPv6, ipv6->sin6_addr.s6_addr);
log_d("DNS found IPv6 %s", aResult.toString().c_str());
} else {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
// As a single u32_t
aResult = IPAddress(ipv4->sin_addr.s_addr);
log_d("DNS found IPv4 %s", aResult.toString().c_str());
}
lwip_freeaddrinfo(res);
return 1;
}
log_e("DNS Failed for '%s' with error '%d'", aHostname, err);
return err;
}
uint8_t *NetworkManager::macAddress(uint8_t *mac) {
esp_base_mac_addr_get(mac);
return mac;
}
String NetworkManager::macAddress(void) {
uint8_t mac[6];
char macStr[18] = {0};
macAddress(mac);
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(macStr);
}
static char default_hostname[32] = {
0,
};
const char *NetworkManager::getHostname() {
if (default_hostname[0] == 0) {
uint8_t eth_mac[6];
esp_base_mac_addr_get(eth_mac);
snprintf(default_hostname, 32, "%s%02X%02X%02X", CONFIG_IDF_TARGET "-", eth_mac[3], eth_mac[4], eth_mac[5]);
}
return (const char *)default_hostname;
}
bool NetworkManager::setHostname(const char *name) {
if (name) {
snprintf(default_hostname, 32, "%s", name);
}
return true;
}
bool NetworkManager::setDefaultInterface(NetworkInterface &ifc) {
return ifc.setDefault();
}
NetworkInterface *NetworkManager::getDefaultInterface() {
esp_netif_t *netif = esp_netif_get_default_netif();
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i) {
NetworkInterface *iface = getNetifByID((Network_Interface_ID)i);
if (iface != NULL && iface->netif() == netif) {
return iface;
}
}
return NULL;
}
size_t NetworkManager::printTo(Print &out) const {
size_t bytes = 0;
for (int i = 0; i < ESP_NETIF_ID_MAX; ++i) {
NetworkInterface *iface = getNetifByID((Network_Interface_ID)i);
if (iface != NULL && iface->netif() != NULL) {
bytes += out.println(*iface);
}
}
return bytes;
}
NetworkManager Network;