Skip to content

Commit 15f64eb

Browse files
aalkuigrr
authored andcommitted
Fixes by SwiCago on DNSServer
Fixes by SwiCago on this forum comment http://www.esp8266.com/viewtopic.php?p=23900#p23900
1 parent 5a1ec13 commit 15f64eb

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <ESP8266WiFi.h>
2+
#include <WiFiClient.h>
3+
#include <ESP8266WebServer.h>
4+
#include <DNSServer.h>
5+
6+
const char* ssid = "esp8266";
7+
boolean LEDstate[] = {LOW, false, LOW};
8+
9+
const char* html = "<html><head><title>Success</title><style>.bt{display:block;width:250px;height:100px;padding:10px;margin:10px;"
10+
"text-align:center;border-radius:5px;color:white;font-weight:bold;font-size:70px;text-decoration:none;} "
11+
"body{background:#000;} .r{background:#933;} .g{background:#363;} .y{background:#EE0;height:100px;"
12+
"width:100px;border-radius:50px;} .b{background:#000;height:100px;width:100px;border-radius:50px;} "
13+
".a{font-size:35px;} td{vertical-align:middle;}</style>"
14+
"</head><body><table><tr><td><div class='TGT0'></div></td><td><a class='bt g' href='/L0?v=1'>ON</a></td>"
15+
"<td><a class='bt r' href='/L0?v=0'>OFF</a></td></tr><tr><td><div class='TGT2'></div></td><td>"
16+
"<a class='bt g' href='/L2?v=1'>ON</a></td><td><a class='bt r' href='/L2?v=0'>OFF</a></td></tr>"
17+
"<tr><td>&nbsp;</td><td><a class='bt g a' href='/ALL?v=1'><br/>ALL ON</a></td><td>"
18+
"<a class='bt r a' href='/ALL?v=0'><br/>ALL OFF</a></td></tr></body></html>";
19+
20+
const byte DNS_PORT = 53;
21+
IPAddress apIP(192, 168, 1, 1);
22+
IPAddress netMsk(255, 255, 255, 0);
23+
DNSServer dnsServer;
24+
ESP8266WebServer server(80);
25+
26+
void setup() {
27+
pinMode(0, OUTPUT);
28+
pinMode(2, OUTPUT);
29+
digitalWrite(2, LEDstate[0]);
30+
digitalWrite(2, LEDstate[2]);
31+
Serial.begin(115200);
32+
Serial.setDebugOutput(true);
33+
WiFi.mode(WIFI_AP);
34+
WiFi.softAPConfig(apIP, apIP, netMsk);
35+
WiFi.softAP(ssid);
36+
Serial.print("SSID: ");
37+
Serial.println(ssid);
38+
Serial.print("IP address: ");
39+
Serial.println(WiFi.softAPIP());
40+
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
41+
dnsServer.start(DNS_PORT, "*", apIP);
42+
Serial.println("USP Server started");
43+
server.on("/", handle_root);
44+
server.on("/generate_204", handle_root); //Android captive portal
45+
server.on("/L0", handle_L0);
46+
server.on("/L2", handle_L2);
47+
server.on("/ALL", handle_ALL);
48+
server.onNotFound(handleNotFound);
49+
server.begin();
50+
Serial.println("HTTP server started");
51+
52+
}
53+
54+
void loop() {
55+
dnsServer.processNextRequest();
56+
server.handleClient();
57+
}
58+
59+
void handleNotFound() {
60+
Serial.print("\t\t\t\t URI Not Found: ");
61+
Serial.println(server.uri());
62+
server.send ( 200, "text/plain", "URI Not Found" );
63+
}
64+
65+
void handle_root() {
66+
Serial.println("Page served");
67+
String toSend = html;
68+
toSend.replace("TGT0", LEDstate[0] ? "y" : "b");
69+
toSend.replace("TGT2", LEDstate[2] ? "y" : "b");
70+
server.send(200, "text/html", toSend);
71+
delay(100);
72+
}
73+
74+
void handle_L0() {
75+
change_states(0);
76+
handle_root();
77+
}
78+
79+
void handle_L2() {
80+
change_states(2);
81+
handle_root();
82+
}
83+
84+
void handle_ALL() {
85+
change_states(0);
86+
change_states(2);
87+
handle_root();
88+
}
89+
90+
void change_states(int tgt) {
91+
if (server.hasArg("v")) {
92+
int state = server.arg("v").toInt() == 1;
93+
Serial.print("LED");
94+
Serial.print(tgt);
95+
Serial.print("=");
96+
Serial.println(state);
97+
LEDstate[tgt] = state ? HIGH : LOW;
98+
digitalWrite(tgt, LEDstate[tgt]);
99+
}
100+
}

hardware/esp8266com/esp8266/libraries/DNSServer/src/DNSServer.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include <lwip/def.h>
33
#include <Arduino.h>
44

5+
#define DEBUG
6+
#define DEBUG_OUTPUT Serial
7+
58
DNSServer::DNSServer()
69
{
710
_ttl = htonl(60);
@@ -110,16 +113,43 @@ void DNSServer::replyWithIP()
110113
{
111114
_dnsHeader->QR = DNS_QR_RESPONSE;
112115
_dnsHeader->ANCount = _dnsHeader->QDCount;
113-
_dnsHeader->QDCount = 0;
116+
_dnsHeader->QDCount = _dnsHeader->QDCount;
117+
//_dnsHeader->RA = 1;
114118

115119
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
116120
_udp.write(_buffer, _currentPacketSize);
121+
122+
_udp.write((uint8_t)192); // answer name is a pointer
123+
_udp.write((uint8_t)12); // pointer to offset at 0x00c
124+
125+
_udp.write((uint8_t)0); // 0x0001 answer is type A query (host address)
126+
_udp.write((uint8_t)1);
127+
128+
_udp.write((uint8_t)0); //0x0001 answer is class IN (internet address)
129+
_udp.write((uint8_t)1);
130+
117131
_udp.write((unsigned char*)&_ttl, 4);
132+
118133
// Length of RData is 4 bytes (because, in this case, RData is IPv4)
119134
_udp.write((uint8_t)0);
120135
_udp.write((uint8_t)4);
121136
_udp.write(_resolvedIP, sizeof(_resolvedIP));
122137
_udp.endPacket();
138+
139+
140+
141+
#ifdef DEBUG
142+
DEBUG_OUTPUT.print("DNS responds: ");
143+
DEBUG_OUTPUT.print(_resolvedIP[0]);
144+
DEBUG_OUTPUT.print(".");
145+
DEBUG_OUTPUT.print(_resolvedIP[1]);
146+
DEBUG_OUTPUT.print(".");
147+
DEBUG_OUTPUT.print(_resolvedIP[2]);
148+
DEBUG_OUTPUT.print(".");
149+
DEBUG_OUTPUT.print(_resolvedIP[3]);
150+
DEBUG_OUTPUT.print(" for ");
151+
DEBUG_OUTPUT.println(getDomainNameWithoutWwwPrefix());
152+
#endif
123153
}
124154

125155
void DNSServer::replyWithCustomCode()
@@ -131,4 +161,4 @@ void DNSServer::replyWithCustomCode()
131161
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
132162
_udp.write(_buffer, sizeof(DNSHeader));
133163
_udp.endPacket();
134-
}
164+
}

hardware/esp8266com/esp8266/libraries/DNSServer/src/DNSServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ class DNSServer
6868
void replyWithIP();
6969
void replyWithCustomCode();
7070
};
71-
#endif
71+
#endif

0 commit comments

Comments
 (0)