diff --git a/libraries/DNSServer/examples/CaptivePortal2/CaptivePortal2.ino b/libraries/DNSServer/examples/CaptivePortal2/CaptivePortal2.ino new file mode 100644 index 000000000..80bf77b1b --- /dev/null +++ b/libraries/DNSServer/examples/CaptivePortal2/CaptivePortal2.ino @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +const char* ssid = "esp8266"; +boolean LEDstate[] = {LOW, false, LOW}; + +const char* html = "Success" + "" + "" + ""; + +const byte DNS_PORT = 53; +IPAddress apIP(192, 168, 1, 1); +IPAddress netMsk(255, 255, 255, 0); +DNSServer dnsServer; +ESP8266WebServer server(80); + +void setup() { + pinMode(0, OUTPUT); + pinMode(2, OUTPUT); + digitalWrite(2, LEDstate[0]); + digitalWrite(2, LEDstate[2]); + Serial.begin(115200); + Serial.setDebugOutput(true); + WiFi.mode(WIFI_AP); + WiFi.softAPConfig(apIP, apIP, netMsk); + WiFi.softAP(ssid); + Serial.print("SSID: "); + Serial.println(ssid); + Serial.print("IP address: "); + Serial.println(WiFi.softAPIP()); + dnsServer.setErrorReplyCode(DNSReplyCode::NoError); + dnsServer.start(DNS_PORT, "*", apIP); + Serial.println("USP Server started"); + server.on("/", handle_root); + server.on("/generate_204", handle_root); //Android captive portal + server.on("/L0", handle_L0); + server.on("/L2", handle_L2); + server.on("/ALL", handle_ALL); + server.onNotFound(handleNotFound); + server.begin(); + Serial.println("HTTP server started"); + +} + +void loop() { + dnsServer.processNextRequest(); + server.handleClient(); +} + +void handleNotFound() { + Serial.print("\t\t\t\t URI Not Found: "); + Serial.println(server.uri()); + server.send ( 200, "text/plain", "URI Not Found" ); +} + +void handle_root() { + Serial.println("Page served"); + String toSend = html; + toSend.replace("TGT0", LEDstate[0] ? "y" : "b"); + toSend.replace("TGT2", LEDstate[2] ? "y" : "b"); + server.send(200, "text/html", toSend); + delay(100); +} + +void handle_L0() { + change_states(0); + handle_root(); +} + +void handle_L2() { + change_states(2); + handle_root(); +} + +void handle_ALL() { + change_states(0); + change_states(2); + handle_root(); +} + +void change_states(int tgt) { + if (server.hasArg("v")) { + int state = server.arg("v").toInt() == 1; + Serial.print("LED"); + Serial.print(tgt); + Serial.print("="); + Serial.println(state); + LEDstate[tgt] = state ? HIGH : LOW; + digitalWrite(tgt, LEDstate[tgt]); + } +} diff --git a/libraries/DNSServer/src/DNSServer.cpp b/libraries/DNSServer/src/DNSServer.cpp index 10fc81d4a..65d2b31d6 100644 --- a/libraries/DNSServer/src/DNSServer.cpp +++ b/libraries/DNSServer/src/DNSServer.cpp @@ -2,6 +2,9 @@ #include #include +#define DEBUG +#define DEBUG_OUTPUT Serial + DNSServer::DNSServer() { _ttl = htonl(60); @@ -110,16 +113,43 @@ void DNSServer::replyWithIP() { _dnsHeader->QR = DNS_QR_RESPONSE; _dnsHeader->ANCount = _dnsHeader->QDCount; - _dnsHeader->QDCount = 0; + _dnsHeader->QDCount = _dnsHeader->QDCount; + //_dnsHeader->RA = 1; _udp.beginPacket(_udp.remoteIP(), _udp.remotePort()); _udp.write(_buffer, _currentPacketSize); + + _udp.write((uint8_t)192); // answer name is a pointer + _udp.write((uint8_t)12); // pointer to offset at 0x00c + + _udp.write((uint8_t)0); // 0x0001 answer is type A query (host address) + _udp.write((uint8_t)1); + + _udp.write((uint8_t)0); //0x0001 answer is class IN (internet address) + _udp.write((uint8_t)1); + _udp.write((unsigned char*)&_ttl, 4); + // Length of RData is 4 bytes (because, in this case, RData is IPv4) _udp.write((uint8_t)0); _udp.write((uint8_t)4); _udp.write(_resolvedIP, sizeof(_resolvedIP)); _udp.endPacket(); + + + + #ifdef DEBUG + DEBUG_OUTPUT.print("DNS responds: "); + DEBUG_OUTPUT.print(_resolvedIP[0]); + DEBUG_OUTPUT.print("."); + DEBUG_OUTPUT.print(_resolvedIP[1]); + DEBUG_OUTPUT.print("."); + DEBUG_OUTPUT.print(_resolvedIP[2]); + DEBUG_OUTPUT.print("."); + DEBUG_OUTPUT.print(_resolvedIP[3]); + DEBUG_OUTPUT.print(" for "); + DEBUG_OUTPUT.println(getDomainNameWithoutWwwPrefix()); + #endif } void DNSServer::replyWithCustomCode() @@ -131,4 +161,4 @@ void DNSServer::replyWithCustomCode() _udp.beginPacket(_udp.remoteIP(), _udp.remotePort()); _udp.write(_buffer, sizeof(DNSHeader)); _udp.endPacket(); -} +} \ No newline at end of file diff --git a/libraries/DNSServer/src/DNSServer.h b/libraries/DNSServer/src/DNSServer.h index d58efbbdd..ca96afea3 100644 --- a/libraries/DNSServer/src/DNSServer.h +++ b/libraries/DNSServer/src/DNSServer.h @@ -68,4 +68,4 @@ class DNSServer void replyWithIP(); void replyWithCustomCode(); }; -#endif +#endif \ No newline at end of file
ONOFF
" + "ONOFF
 
ALL ON
" + "
ALL OFF