-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathNetworkManager.cpp
165 lines (142 loc) · 4.46 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
/*
* 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"
NetworkManager::NetworkManager(){
}
bool NetworkManager::begin(){
static bool initialized = false;
if(!initialized){
initialized = true;
#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)
{
err_t err = ERR_OK;
// 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.
// That is of course, if 'preferV6' is not set to true
static bool hasGlobalV6 = false;
bool hasGlobalV6Now = false;//ToDo: implement this!
if(hasGlobalV6 != hasGlobalV6Now){
hasGlobalV6 = hasGlobalV6Now;
dns_clear_cache();
log_d("Clearing DNS cache");
}
aResult = static_cast<uint32_t>(0);
// First check if the host parses as a literal address
if (aResult.fromString(aHostname)) {
return 1;
}
const char *servname = "0";
struct addrinfo *res;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
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;
}
NetworkInterface * getNetifByID(Network_Interface_ID id);
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;