Skip to content

Commit 15ceed0

Browse files
author
Laurent Louf
committed
Retrieve some code from what has been done on the ESP8266. Clarify a bit the signification of several bytes in the response.
1 parent 52d4705 commit 15ceed0

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

libraries/DNSServer/src/DNSServer.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,29 @@ void DNSServer::replyWithIP()
123123
if (_buffer == NULL) return;
124124
_dnsHeader->QR = DNS_QR_RESPONSE;
125125
_dnsHeader->ANCount = _dnsHeader->QDCount;
126-
_dnsHeader->QDCount = 0;
126+
//_dnsHeader->QDCount = 0;
127127

128128
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
129129
_udp.write(_buffer, _currentPacketSize);
130+
131+
// Use DNS name compression : instead of repeating the name in this RNAME occurence,
132+
// set the two MSB of the byte corresponding normally to the length to 1. The following
133+
// 14 bits must be used to specify the offset of the domain name in the message
134+
// (<255 here so the first byte has the 6 LSB at 0)
135+
_udp.write((uint8_t) 0xC0);
136+
_udp.write((uint8_t) DNS_OFFSET_DOMAIN_NAME);
137+
// DNS type A : host address
138+
_udp.write((uint8_t) (DNS_TYPE_A >> 8) );
139+
_udp.write((uint8_t) (DNS_TYPE_A & 0xFF) );
140+
// DNS class IN for INternet
141+
_udp.write((uint8_t) (DNS_CLASS_IN >> 8) );
142+
_udp.write((uint8_t) (DNS_CLASS_IN & 0xFF) );
143+
// DNS Time To Live
130144
_udp.write((unsigned char*)&_ttl, 4);
131-
_udp.write((uint8_t)0);
132-
_udp.write((uint8_t)4);
133-
_udp.write(_resolvedIP, 4);
145+
// Returning an IPv4 address
146+
_udp.write((uint8_t) (DNS_RDLENGTH_IPV4 >> 8) );
147+
_udp.write((uint8_t) (DNS_RDLENGTH_IPV4 & 0xFF) );
148+
_udp.write(_resolvedIP, sizeof(_resolvedIP));
134149
_udp.endPacket();
135150
}
136151

libraries/DNSServer/src/DNSServer.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define DNS_QR_QUERY 0
66
#define DNS_QR_RESPONSE 1
77
#define DNS_OPCODE_QUERY 0
8+
#define DNS_OFFSET_DOMAIN_NAME 12 // Offset in bytes to reach the domain name in the DNS message
89

910
enum class DNSReplyCode
1011
{
@@ -19,6 +20,26 @@ enum class DNSReplyCode
1920
NXRRSet = 8
2021
};
2122

23+
enum DNSType
24+
{
25+
DNS_TYPE_A = 1, // Host Address
26+
DNS_TYPE_AAAA = 28, // IPv6 Address
27+
DNS_TYPE_SOA = 6, // Start Of a zone of Authority
28+
DNS_TYPE_PTR = 12, // Domain name PoinTeR
29+
DNS_TYPE_DNAME = 39 // Delegation Name
30+
} ;
31+
32+
enum DNSClass
33+
{
34+
DNS_CLASS_IN = 1, // INternet
35+
DNS_CLASS_CH = 3 // CHaos
36+
} ;
37+
38+
enum DNSRDLength
39+
{
40+
DNS_RDLENGTH_IPV4 = 4 // 4 bytes for an IPv4 address
41+
} ;
42+
2243
struct DNSHeader
2344
{
2445
uint16_t ID; // identification number

0 commit comments

Comments
 (0)