Skip to content

Add DNS forwarder to DNSServer #7237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
407e481
Added DNS forwarder support.
mhightower83 Apr 21, 2020
0e33d6e
Added example to use DNS forwarder
mhightower83 Apr 22, 2020
63f8958
Cleanup for CI
mhightower83 Apr 22, 2020
aa430d0
Fixed insert error
mhightower83 Apr 22, 2020
a101809
Add/updated comments. Remove commented code.
mhightower83 Apr 22, 2020
3fc1be2
Merge branch 'master' into pr-dns-forwarder
mhightower83 Apr 23, 2020
ad62a7f
Added missing include for unsupported build environment
mhightower83 Apr 23, 2020
ee97777
Updated HTML to limit/remove screen flash when using show password.
mhightower83 Apr 25, 2020
16d02f6
Merge branch 'master' into pr-dns-forwarder
mhightower83 Apr 27, 2020
6755a5b
Reserved space in String page. Re-adjusted size of chuncks.
mhightower83 Apr 27, 2020
7453544
Merge branch 'master' into pr-dns-forwarder
mhightower83 May 3, 2020
10f6311
Added logic to handle dividing large PROGMEM strings into smaller chunks
mhightower83 May 5, 2020
96eacc3
Merge branch 'master' into pr-dns-forwarder
mhightower83 May 31, 2020
1b67f96
Merge branch 'pr-dns-forwarder' of github.com:mhightower83/Arduino in…
mhightower83 May 31, 2020
eb1f508
Merge branch 'master' into pr-dns-forwarder
mhightower83 Jun 19, 2020
88d1abf
Merge branch 'master' into pr-dns-forwarder
mhightower83 Jul 7, 2020
5370ba3
Merge branch 'pr-dns-forwarder' of github.com:mhightower83/Arduino in…
mhightower83 Jul 10, 2020
f9371b3
Corrected case statement in getPhyModeChar().
mhightower83 Jul 10, 2020
33aed64
Merge branch 'master' into pr-dns-forwarder
mhightower83 Jul 10, 2020
0613216
Fixed errors.
mhightower83 Jul 10, 2020
1ef128c
typo
mhightower83 Jul 11, 2020
696c865
Merge branch 'master' into pr-dns-forwarder
mhightower83 Jul 17, 2020
22769c5
Merge branch 'master' into pr-dns-forwarder
mhightower83 Jul 21, 2020
ff5d79a
Merge branch 'master' into pr-dns-forwarder
mhightower83 Sep 2, 2020
e09e6e8
Merge branch 'master' into pr-dns-forwarder
mhightower83 Sep 17, 2020
ab0472e
Merge branch 'master' into pr-dns-forwarder
mhightower83 Oct 2, 2020
64a8191
Merge branch 'master' into pr-dns-forwarder
mhightower83 Nov 16, 2020
7178c2e
Merge branch 'master' into pr-dns-forwarder
mhightower83 Dec 6, 2020
f605f12
Merge branch 'master' into pr-dns-forwarder
mhightower83 Jul 28, 2021
aa138b5
Merge branch 'master' into pr-dns-forwarder
mhightower83 Jan 12, 2022
682d08d
Merge branch 'master' into pr-dns-forwarder
mhightower83 Feb 10, 2022
3681092
Merge branch 'master' into pr-dns-forwarder
mhightower83 Apr 19, 2022
5a69137
Merge branch 'master' into pr-dns-forwarder
mhightower83 Apr 22, 2022
6d95e41
Added inline to constexpr
mhightower83 Apr 27, 2022
32fe7e1
Updates
mhightower83 May 1, 2022
dda4e04
Update libraries/DNSServer/examples/NAPTCaptivePortal/NAPTCaptivePort…
mhightower83 May 2, 2022
8afa9ca
Update libraries/DNSServer/examples/NAPTCaptivePortal/NAPTCaptivePort…
mhightower83 May 2, 2022
1da9785
Merge branch 'master' into pr-dns-forwarder
mhightower83 May 2, 2022
ea3e971
style
mhightower83 May 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added inline to constexpr
removed typedef

Changed run_CI_locally.sh to run ci/style_check.sh.
Copied resulting style changes from /tmp to PR
  • Loading branch information
mhightower83 committed Apr 28, 2022
commit 6d95e41e6978c3ce882798d54073518804a89561
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,42 @@
#endif

#define _PRINTF(a, ...) printf_P(PSTR(a), ##__VA_ARGS__)
#define _PRINT(a) print(String(F(a)))
#define _PRINTLN(a) println(String(F(a)))
#define _PRINTLN2(a, b) println(String(F(a)) + b )
#define _PRINT(a) print(String(F(a)))
#define _PRINTLN(a) println(String(F(a)))
#define _PRINTLN2(a, b) println(String(F(a)) + b)

#define CONSOLE_PRINTF CONSOLE._PRINTF
#define CONSOLE_PRINT CONSOLE._PRINT
#define CONSOLE_PRINTLN CONSOLE._PRINTLN
#define CONSOLE_PRINTF CONSOLE._PRINTF
#define CONSOLE_PRINT CONSOLE._PRINT
#define CONSOLE_PRINTLN CONSOLE._PRINTLN
#define CONSOLE_PRINTLN2 CONSOLE._PRINTLN2

#ifdef DEBUG_SKETCH
#define DEBUG_PRINTF CONSOLE_PRINTF
#define DEBUG_PRINT CONSOLE_PRINT
#define DEBUG_PRINTLN CONSOLE_PRINTLN
#define DEBUG_PRINTLN2 CONSOLE_PRINTLN2
#define DEBUG_PRINTF CONSOLE_PRINTF
#define DEBUG_PRINT CONSOLE_PRINT
#define DEBUG_PRINTLN CONSOLE_PRINTLN
#define DEBUG_PRINTLN2 CONSOLE_PRINTLN2

#else
#define DEBUG_PRINTF(...) do { } while(false)
#define DEBUG_PRINT(...) do { } while(false)
#define DEBUG_PRINTLN(...) do { } while(false)
#define DEBUG_PRINTLN2(...) do { } while(false)
#define DEBUG_PRINTF(...) \
do { \
} while (false)
#define DEBUG_PRINT(...) \
do { \
} while (false)
#define DEBUG_PRINTLN(...) \
do { \
} while (false)
#define DEBUG_PRINTLN2(...) \
do { \
} while (false)
#endif



/* Set these to your desired softAP credentials. They are not configurable at runtime */
#ifndef APSSID
#define APSSID "MagicPortal"
#define APPSK "ShowTime"
#define APPSK "ShowTime"
#endif

const char *softAP_ssid = APSSID;
Expand Down Expand Up @@ -154,7 +162,7 @@ bool connect = false;

/** Set to true to start WiFi STA at setup time when credentials loaded successfuly from EEPROM */
/** Set to false to defer WiFi STA until configured through web interface. */
bool staReady = false; // Don't connect right away
bool staReady = false; // Don't connect right away

/** Last time I tried to connect to WLAN */
unsigned long lastConnectTry = 0;
Expand All @@ -163,28 +171,28 @@ unsigned long lastConnectTry = 0;
unsigned int status = WL_IDLE_STATUS;

void setup() {
WiFi.persistent(false); // w/o this a flash write occurs at every boot
WiFi.mode(WIFI_OFF); // Prevent use of SDK stored credentials
WiFi.persistent(false); // w/o this a flash write occurs at every boot
WiFi.mode(WIFI_OFF); // Prevent use of SDK stored credentials
CONSOLE.begin(115200);
CONSOLE_PRINTLN("\r\n\r\nNAPT with Configuration Portal ...");

staModeConnectedHandler = WiFi.onStationModeConnected(
[](WiFiEventStationModeConnected data) {
// Keep a copy of the BSSID for the AP that WLAN connects to.
// This is used in the WLAN report on WiFi Details page.
memcpy(bssid, data.bssid, sizeof(bssid));
});
[](WiFiEventStationModeConnected data) {
// Keep a copy of the BSSID for the AP that WLAN connects to.
// This is used in the WLAN report on WiFi Details page.
memcpy(bssid, data.bssid, sizeof(bssid));
});

staModeDisconnectedHandler = WiFi.onStationModeDisconnected(
[](WiFiEventStationModeDisconnected data) {
(void)data;
if (dnsServer.isForwarding()) {
dnsServer.disableForwarder("*");
dnsServer.setTTL(0);
// Reminder, Serial.println() will not work from these callbacks.
// For debug printf use ets_uart_printf().
}
});
[](WiFiEventStationModeDisconnected data) {
(void)data;
if (dnsServer.isForwarding()) {
dnsServer.disableForwarder("*");
dnsServer.setTTL(0);
// Reminder, Serial.println() will not work from these callbacks.
// For debug printf use ets_uart_printf().
}
});

/*
While you can remove the password parameter to make the AP open.
Expand Down Expand Up @@ -240,13 +248,13 @@ void setup() {
server.on("/", handleRoot);
server.on("/wifi", handleWifi);
server.on("/wifisave", handleWifiSave);
server.on("/generate_204", handleRoot); //Android captive portal. Maybe not needed. Might be handled by notFound handler.
server.on("/fwlink", handleRoot); //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
server.on("/generate_204", handleRoot); // Android captive portal. Maybe not needed. Might be handled by notFound handler.
server.on("/fwlink", handleRoot); // Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
server.onNotFound(handleNotFound);
server.begin(); // Web server start
server.begin(); // Web server start
CONSOLE_PRINTLN("HTTP server started");
loadCredentials(); // Load WLAN credentials from network
connect = (strlen(ssid) > 0 && staReady); // Request WLAN connect if there is a SSID and we want to connect at startup
loadCredentials(); // Load WLAN credentials from network
connect = (strlen(ssid) > 0 && staReady); // Request WLAN connect if there is a SSID and we want to connect at startup
}

void connectWifi() {
Expand Down Expand Up @@ -286,7 +294,7 @@ void loop() {
/* Don't set retry time too low as retry interfere the softAP operation */
connect = true;
}
if (status != s) { // WLAN status change
if (status != s) { // WLAN status change
CONSOLE_PRINTF("WLAN Status changed:\r\n");
CONSOLE_PRINTF(" new status: %s, %d\r\n", getWiFiStatusString(s).c_str(), s);
CONSOLE_PRINTF(" previous status: %s, %d\r\n", getWiFiStatusString(status).c_str(), status);
Expand Down Expand Up @@ -318,7 +326,7 @@ void loop() {
Setup the DNSServer to respond only to request for our hostname and
forward other name request to the DNS configured to the WLAN.
*/
dnsServer.setTTL(600); // 10 minutes
dnsServer.setTTL(600); // 10 minutes
dnsServer.enableForwarder(myHostname, WiFi.dnsIP(0));
CONSOLE_PRINTF("DNSServer changes/status:\r\n");
CONSOLE_PRINTF(" DNS Forwarding is %s\r\n", dnsServer.isForwarding() ? "on" : "off");
Expand All @@ -331,7 +339,7 @@ void loop() {
}
CONSOLE_PRINTF(" TTL set to %u\r\n", dnsServer.getTTL());

} else {
} else {
/* Captive portals will usually use a TTL of 0 to avoid DNS cache poisoning. */
dnsServer.setTTL(0);
/* Setup the DNSServer to redirect all the domain lookups to the apIP */
Expand Down Expand Up @@ -363,9 +371,9 @@ void loop() {
}
}
// Do work:
//DNS
// DNS
dnsServer.processNextRequest();
//HTTP
// HTTP
server.handleClient();
}

Expand All @@ -382,4 +390,4 @@ void setup() {
void loop() {
}

#endif // LWIP_FEATURES && !LWIP_IPV6
#endif // LWIP_FEATURES && !LWIP_IPV6
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ void sendPortalRedirect(String path, String targetName) {
server.send(302, "text/html", reply);
}

#endif // LWIP_FEATURES && !LWIP_IPV6
#endif // LWIP_FEATURES && !LWIP_IPV6
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#ifndef WIFIHTTP_H_
#define WIFIHTTP_H_


// #define DEBUG_VIEW
// The idea here is to debug HTML with DEBUG_VIEW defined then, when finished,
Expand Down Expand Up @@ -326,5 +323,3 @@ static const char configEnd2[] PROGMEM = R"EOF(
#else
static const char configEnd[] PROGMEM = R"EOF(</table> <br /><form method='POST' action='wifisave'><h4>Connect to Network:</h4> <input id='s' class='lg' type='text' size=32 maxlength=32 placeholder='Network Name/SSID' name='n' spellcheck='false' data-gramm_editor='false'/> <br /><br /> <input id='p' class='lg' type='password' size=32 maxlength=64 placeholder='password' name='p' spellcheck='false' data-gramm_editor='false'/> &nbsp;&nbsp;<a class='lg' onclick='pv();'>&#x1f441;</a> <br /><br /><input type='submit' value='Connect/Disconnect'/></form> <br /><p>You may want to <a href='/'>return to the home page</a>.</p><p></p></div></div></body></html>)EOF";
#endif

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ void saveCredentials() {
EEPROM.end();
}

#endif // LWIP_FEATURES && !LWIP_IPV6
#endif // LWIP_FEATURES && !LWIP_IPV6
37 changes: 17 additions & 20 deletions libraries/DNSServer/examples/NAPTCaptivePortal/handleHttp.ino
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#if LWIP_FEATURES && !LWIP_IPV6

#include "WifiHttp.h"

#ifndef TCP_MSS
#define TCP_MSS 1460
#endif
/*
Use kMaxChunkSize to limit size of chuncks
*/
constexpr size_t kMaxChunkSize = TCP_MSS;
String& sendIfOver(String & str, size_t threshold = kMaxChunkSize / 2);
constexpr inline size_t kMaxChunkSize = TCP_MSS;
String& sendIfOver(String& str, size_t threshold = kMaxChunkSize / 2);
size_t sendAsChunks_P(PGM_P content, size_t chunkSize = kMaxChunkSize);

size_t maxPage = 0;
Expand All @@ -21,7 +19,7 @@ void addNoCacheHeader() {
}


String& sendIfOver(String & str, size_t threshold) {
String& sendIfOver(String& str, size_t threshold) {
size_t len = str.length();
if (len > threshold) {
// Use later to determine if we reserved enough room in page to avoid realloc
Expand Down Expand Up @@ -54,19 +52,19 @@ void handleRoot() {

String Page;
Page += F(
"<!DOCTYPE html>"
"<html lang='en'><head><meta name='viewport' content='width=device-width'>"
"<title>ADV CAP Portal Example</title>"
"</head><body>"
"<h1>HELLO WORLD!!</h1>");
"<!DOCTYPE html>"
"<html lang='en'><head><meta name='viewport' content='width=device-width'>"
"<title>ADV CAP Portal Example</title>"
"</head><body>"
"<h1>HELLO WORLD!!</h1>");
if (server.client().localIP() == apIP) {
Page += String(F("<p>You are connected through the soft AP: ")) + softAP_ssid + F("</p>");
} else {
Page += String(F("<p>You are connected through the wifi network: ")) + ssid + F("</p>");
}
Page += F(
"<p>You may want to <a href='/wifi'>config the wifi connection</a>.</p>"
"</body></html>");
"<p>You may want to <a href='/wifi'>config the wifi connection</a>.</p>"
"</body></html>");

server.send(200, F("text/html"), Page);
}
Expand All @@ -90,9 +88,8 @@ boolean captivePortal() {
return false;
}

if (hAddr.isSet() ||
(server.hostHeader() != (String(myHostname) + ".local") && // arrived here by mDNS
server.hostHeader() != String(myHostname))) { // arrived here by local router DNS
if (hAddr.isSet() || (server.hostHeader() != (String(myHostname) + ".local") && // arrived here by mDNS
server.hostHeader() != String(myHostname))) { // arrived here by local router DNS
String whereTo = String("http://") + server.client().localIP().toString();
sendPortalRedirect(whereTo, F("Captive Portal Example"));
return true;
Expand Down Expand Up @@ -159,7 +156,7 @@ void handleWifi() {
sendIfOver(page);
if (sta_cnt) {
page += String(F("\r\n<PRE>\r\n"));
struct station_info *info = wifi_softap_get_station_info();
struct station_info* info = wifi_softap_get_station_info();
IPAddress addr;
while (info != NULL) {
addr = info->ip;
Expand Down Expand Up @@ -213,7 +210,7 @@ void handleWifi() {
} else {
page += FPSTR(configNoAPs);
}
sendIfOver(page, 0); // send what we have buffered before next direct send.
sendIfOver(page, 0); // send what we have buffered before next direct send.
sendAsChunks_P(configEnd);

CONSOLE_PRINTLN2("MAX String memory used: ", (maxPage));
Expand All @@ -227,11 +224,11 @@ void handleWifiSave() {
server.arg("p").toCharArray(password, sizeof(password) - 1);
sendPortalRedirect(F("wifi"), F("Wifi Config"));
saveCredentials();
connect = strlen(ssid) > 0; // Request WLAN connect with new credentials if there is a SSID
connect = strlen(ssid) > 0; // Request WLAN connect with new credentials if there is a SSID
}

void handleNotFound() {
if (captivePortal()) { // If captive portal redirect instead of displaying the error page.
if (captivePortal()) { // If captive portal redirect instead of displaying the error page.
return;
}
String message = F("File Not Found\r\n\r\n");
Expand All @@ -250,4 +247,4 @@ void handleNotFound() {
server.send(404, F("text/plain"), message);
}

#endif // LWIP_FEATURES && !LWIP_IPV6
#endif // LWIP_FEATURES && !LWIP_IPV6
10 changes: 5 additions & 5 deletions libraries/DNSServer/examples/NAPTCaptivePortal/tools.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Returns a descriptive string for WiFi.status() value
*/
String getWiFiStatusString(uint32_t status) {
const __FlashStringHelper *r;
const __FlashStringHelper* r;
switch (status) {
case WL_IDLE_STATUS:
r = F("WL_IDLE_STATUS");
Expand Down Expand Up @@ -55,11 +55,11 @@ String getWiFiStatusString(uint32_t status) {
char getPhyModeChar(WiFiPhyMode_t i) {
switch (i) {
case WIFI_PHY_MODE_11B:
return 'b'; // = 1
return 'b'; // = 1
case WIFI_PHY_MODE_11G:
return 'g'; // = 2,
return 'g'; // = 2,
case WIFI_PHY_MODE_11N:
return 'n'; // = 3,
return 'n'; // = 3,
default:
break;
}
Expand All @@ -81,4 +81,4 @@ String macToString(const unsigned char* mac) {
return String(buf);
}

#endif // LWIP_FEATURES && !LWIP_IPV6
#endif // LWIP_FEATURES && !LWIP_IPV6
2 changes: 1 addition & 1 deletion libraries/DNSServer/src/DNSServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ bool DNSServer::enableForwarder(const String &domainName, const IPAddress &dns)

if (_dns.isSet()) {
if (!_que) {
_que = std::unique_ptr<dnss_requester_t[]> (new (std::nothrow) dnss_requester_t[kDNSSQueSize]);
_que = std::unique_ptr<struct DNSS_REQUESTER[]> (new (std::nothrow) struct DNSS_REQUESTER[kDNSSQueSize]);
DEBUG_PRINTF("Created new _que\r\n");
if (_que) {
for (size_t i = 0; i < kDNSSQueSize; i++) {
Expand Down
12 changes: 6 additions & 6 deletions libraries/DNSServer/src/DNSServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt
#ifndef IANA_DNS_PORT
#define IANA_DNS_PORT 53 // AKA domain
constexpr uint16_t kIanaDnsPort = 53;
constexpr inline uint16_t kIanaDnsPort = 53;
#endif

#define DNS_QR_QUERY 0
Expand Down Expand Up @@ -53,14 +53,14 @@ struct DNSHeader
uint16_t ARCount; // number of resource entries
};

constexpr size_t kDNSSQueSizeAddrBits = 3; // The number of bits used to address que entries
constexpr size_t kDNSSQueSize = BIT(kDNSSQueSizeAddrBits);
constexpr inline size_t kDNSSQueSizeAddrBits = 3; // The number of bits used to address que entries
constexpr inline size_t kDNSSQueSize = BIT(kDNSSQueSizeAddrBits);

typedef struct DNSS_REQUESTER {
struct DNSS_REQUESTER {
uint32_t ip;
uint16_t port;
uint16_t id;
} dnss_requester_t;
};

class DNSServer
{
Expand Down Expand Up @@ -110,7 +110,7 @@ class DNSServer
WiFiUDP _udp;
String _domainName;
IPAddress _dns;
std::unique_ptr<dnss_requester_t[]> _que;
std::unique_ptr<struct DNSS_REQUESTER[]> _que;
uint32_t _ttl;
#ifdef DEBUG_DNSSERVER
// There are 2 possiblities for OverFlow:
Expand Down
2 changes: 1 addition & 1 deletion tests/run_CI_locally.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ elif [ "$BUILD_TYPE" = host ]; then
tests/ci/host_test.sh

elif [ "$BUILD_TYPE" = style ]; then
tests/ci/check_restyle.sh
tests/ci/style_check.sh
tests/restyle.sh

else
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy