Skip to content

Commit 615217d

Browse files
authored
Merge branch 'master' into feature/ppp_modem_support
2 parents b35430a + 8c75c35 commit 615217d

File tree

6 files changed

+79
-25
lines changed

6 files changed

+79
-25
lines changed

libraries/Network/src/NetworkInterface.cpp

+21-5
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s
357357

358358
esp_netif_flags_t flags = esp_netif_get_flags(_esp_netif);
359359
if(flags & ESP_NETIF_DHCP_SERVER){
360+
361+
// Set DNS Server
362+
if(d2.ip.u_addr.ip4.addr != 0){
363+
err = esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d2);
364+
if(err){
365+
log_e("Netif Set DNS Info Failed! 0x%04x: %s", err, esp_err_to_name(err));
366+
return false;
367+
}
368+
}
369+
360370
// Stop DHCPS
361371
err = esp_netif_dhcps_stop(_esp_netif);
362372
if(err && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED){
@@ -371,11 +381,6 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s
371381
return false;
372382
}
373383

374-
// Set DNS Server
375-
if(d2.ip.u_addr.ip4.addr != 0){
376-
esp_netif_set_dns_info(_esp_netif, ESP_NETIF_DNS_MAIN, &d2);
377-
}
378-
379384
dhcps_lease_t lease;
380385
lease.enable = true;
381386
uint8_t CIDR = calculateSubnetCIDR(subnet);
@@ -438,6 +443,17 @@ bool NetworkInterface::config(IPAddress local_ip, IPAddress gateway, IPAddress s
438443
log_e("DHCPS Set Lease Failed! 0x%04x: %s", err, esp_err_to_name(err));
439444
return false;
440445
}
446+
447+
// Offer DNS to DHCP clients
448+
if(d2.ip.u_addr.ip4.addr != 0){
449+
dhcps_offer_t dhcps_dns_value = OFFER_DNS;
450+
err = esp_netif_dhcps_option(_esp_netif, ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, &dhcps_dns_value, sizeof(dhcps_dns_value));
451+
if(err){
452+
log_e("Netif Set DHCP Option Failed! 0x%04x: %s", err, esp_err_to_name(err));
453+
return false;
454+
}
455+
}
456+
441457
// Start DHCPS
442458
err = esp_netif_dhcps_start(_esp_netif);
443459
if(err){

libraries/Network/src/NetworkManager.cpp

+33-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ NetworkManager::NetworkManager(){
1414

1515
}
1616

17+
NetworkInterface * getNetifByID(Network_Interface_ID id);
18+
1719
bool NetworkManager::begin(){
1820
static bool initialized = false;
1921
if(!initialized){
@@ -44,18 +46,11 @@ bool NetworkManager::begin(){
4446
*/
4547
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)
4648
{
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
5249
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;
5954

6055
aResult = static_cast<uint32_t>(0);
6156

@@ -64,8 +59,33 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)
6459
return 1;
6560
}
6661

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+
6989
struct addrinfo hints;
7090
memset(&hints, 0, sizeof(hints));
7191
hints.ai_family = AF_UNSPEC;
@@ -130,8 +150,6 @@ bool NetworkManager::setHostname(const char * name)
130150
return true;
131151
}
132152

133-
NetworkInterface * getNetifByID(Network_Interface_ID id);
134-
135153
bool NetworkManager::setDefaultInterface(NetworkInterface & ifc)
136154
{
137155
return ifc.setDefault();

libraries/USB/src/USBHIDKeyboard.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ size_t USBHIDKeyboard::pressRaw(uint8_t k)
222222
uint8_t i;
223223
if (k >= 0xE0 && k < 0xE8) {
224224
// it's a modifier key
225-
_keyReport.modifiers |= (1<<(k-0x80));
225+
_keyReport.modifiers |= (1<<(k-0xE0));
226226
} else if (k && k < 0xA5) {
227227
// Add k to the key report only if it's not already present
228228
// and if there is an empty slot.
@@ -253,7 +253,7 @@ size_t USBHIDKeyboard::releaseRaw(uint8_t k)
253253
uint8_t i;
254254
if (k >= 0xE0 && k < 0xE8) {
255255
// it's a modifier key
256-
_keyReport.modifiers &= ~(1<<(k-0x80));
256+
_keyReport.modifiers &= ~(1<<(k-0xE0));
257257
} else if (k && k < 0xA5) {
258258
// Test the key report to see if k is present. Clear it if it exists.
259259
// Check all positions in case the key is present more than once (which it shouldn't be)

libraries/WiFi/src/AP.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <esp_event.h>
2121
#include <lwip/ip_addr.h>
2222
#include "dhcpserver/dhcpserver_options.h"
23+
#include "esp_netif.h"
2324

2425

2526
esp_netif_t* get_esp_interface_netif(esp_interface_t interface);
@@ -279,6 +280,24 @@ bool APClass::bandwidth(wifi_bandwidth_t bandwidth){
279280
return true;
280281
}
281282

283+
bool APClass::enableNAPT(bool enable){
284+
if(!started()) {
285+
log_e("AP must be first started to enable/disable NAPT");
286+
return false;
287+
}
288+
esp_err_t err = ESP_OK;
289+
if(enable){
290+
err = esp_netif_napt_enable(_esp_netif);
291+
} else {
292+
err = esp_netif_napt_disable(_esp_netif);
293+
}
294+
if(err){
295+
log_e("Could not set enable/disable NAPT! 0x%x: %s", err, esp_err_to_name(err));
296+
return false;
297+
}
298+
return true;
299+
}
300+
282301
String APClass::SSID(void) const{
283302
if(!started()){
284303
return String();

libraries/WiFi/src/WiFiAP.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ String WiFiAPClass::softAPSSID() const
6767
* @param gateway gateway IP
6868
* @param subnet subnet mask
6969
*/
70-
bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start)
70+
bool WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start, IPAddress dns)
7171
{
72-
return AP.config(local_ip, gateway, subnet, dhcp_lease_start);
72+
return AP.begin() && AP.config(local_ip, gateway, subnet, dhcp_lease_start, dns);
7373
}
7474

7575
/**

libraries/WiFi/src/WiFiAP.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class APClass: public NetworkInterface {
4848
bool clear();
4949

5050
bool bandwidth(wifi_bandwidth_t bandwidth);
51+
bool enableNAPT(bool enable=true);
5152

5253
String SSID(void) const;
5354
uint8_t stationCount();
@@ -77,7 +78,7 @@ class WiFiAPClass
7778
return softAP(ssid.c_str(), passphrase.c_str(), channel, ssid_hidden, max_connection, ftm_responder, auth_mode, cipher);
7879
}
7980

80-
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start = (uint32_t) 0);
81+
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dhcp_lease_start = (uint32_t) 0, IPAddress dns = (uint32_t) 0);
8182
bool softAPdisconnect(bool wifioff = false);
8283

8384
bool softAPbandwidth(wifi_bandwidth_t bandwidth);

0 commit comments

Comments
 (0)