mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-27 18:02:17 +03:00
DHCP custom option (#8582)
* works * fixup! works * back to callbacks * names * daisy chain * seconds * less inline * fix dns setter * might as well keep using initlist /to d-a-v it has automatic storage, here it's the same stack based one (just one less line for us) * shift blame * naming * fix impl * revert to ip4 dns * merge fix * restyle * masking done wrong
This commit is contained in:
20
libraries/ESP8266WiFi/examples/CustomOffer/CustomOffer.ino
Normal file
20
libraries/ESP8266WiFi/examples/CustomOffer/CustomOffer.ino
Normal file
@ -0,0 +1,20 @@
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
void setup() {
|
||||
auto& server = WiFi.softAPDhcpServer();
|
||||
server.onSendOptions([](const DhcpServer& server, auto& options) {
|
||||
// VENDOR is... vendor specific
|
||||
options.add(43, { 0xca, 0xfe, 0xca, 0xfe, 0xfe });
|
||||
|
||||
// Captive Portal URI
|
||||
const IPAddress gateway = netif_ip4_addr(server.getNetif());
|
||||
const String captive = F("http://") + gateway.toString();
|
||||
options.add(114, captive.c_str(), captive.length());
|
||||
});
|
||||
WiFi.softAP("TEST", "testtesttest");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(100);
|
||||
}
|
@ -52,10 +52,13 @@ void setup() {
|
||||
}
|
||||
Serial.printf("\nSTA: %s (dns: %s / %s)\n", WiFi.localIP().toString().c_str(), WiFi.dnsIP(0).toString().c_str(), WiFi.dnsIP(1).toString().c_str());
|
||||
|
||||
// give DNS servers to AP side
|
||||
// By default, DNS option will point to the interface IP
|
||||
// Instead, point it to the real DNS server.
|
||||
// Notice that:
|
||||
// - DhcpServer class only supports IPv4
|
||||
// - Only a single IP can be set
|
||||
auto& server = WiFi.softAPDhcpServer();
|
||||
server.dhcps_set_dns(0, WiFi.dnsIP(0));
|
||||
server.dhcps_set_dns(1, WiFi.dnsIP(1));
|
||||
server.setDns(WiFi.dnsIP(0));
|
||||
|
||||
WiFi.softAPConfig( // enable AP, with android-compatible google domain
|
||||
IPAddress(172, 217, 28, 254), IPAddress(172, 217, 28, 254), IPAddress(255, 255, 255, 0));
|
||||
|
@ -176,9 +176,9 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel,
|
||||
if(ip.ip.addr == 0x00000000) {
|
||||
DEBUG_WIFI("[AP] IP config Invalid resetting...\n");
|
||||
ret = softAPConfig(
|
||||
0x0104A8C0 /* 192.168.4.1 */,
|
||||
0x0104A8C0 /* 192.168.4.1 */,
|
||||
0x00FFFFFF /* 255.255.255.0 */);
|
||||
IPAddress(192, 168, 4, 1),
|
||||
IPAddress(192, 168, 4, 1),
|
||||
IPAddress(255, 255, 255, 0));
|
||||
if(!ret) {
|
||||
DEBUG_WIFI("[AP] softAPConfig failed!\n");
|
||||
ret = false;
|
||||
@ -227,9 +227,8 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
|
||||
info.gw.addr = gateway.v4();
|
||||
info.netmask.addr = subnet.v4();
|
||||
|
||||
if(!wifi_softap_dhcps_stop()) {
|
||||
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n");
|
||||
}
|
||||
auto& server = softAPDhcpServer();
|
||||
server.end();
|
||||
|
||||
if(!wifi_set_ip_info(SOFTAP_IF, &info)) {
|
||||
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
|
||||
@ -247,24 +246,14 @@ bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPA
|
||||
dhcp_lease.end_ip.addr = ip.v4();
|
||||
DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());
|
||||
|
||||
auto& server = softAPDhcpServer();
|
||||
if(!server.set_dhcps_lease(&dhcp_lease))
|
||||
{
|
||||
DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
|
||||
ret = false;
|
||||
}
|
||||
|
||||
uint8 mode = info.gw.addr ? 1 : 0;
|
||||
if(!server.set_dhcps_offer_option(OFFER_ROUTER, &mode))
|
||||
{
|
||||
DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n");
|
||||
ret = false;
|
||||
}
|
||||
|
||||
if(!wifi_softap_dhcps_start()) {
|
||||
DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n");
|
||||
ret = false;
|
||||
}
|
||||
server.setRouter(true); // send ROUTER option with netif's gateway IP
|
||||
server.begin();
|
||||
|
||||
// check config
|
||||
if(wifi_get_ip_info(SOFTAP_IF, &info)) {
|
||||
|
Reference in New Issue
Block a user