Skip to content

Commit ed919eb

Browse files
DNSServer: Clarify and tidy writing the answer record
Modify the code used to write the answer record back to the server so that it is clearer that we are writing network byte order 16-bit quantities, and to clarify what's happening with the pointer used at the start of the answer.
1 parent 96e08e1 commit ed919eb

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

Diff for: libraries/DNSServer/src/DNSServer.cpp

+18-11
Original file line numberDiff line numberDiff line change
@@ -186,36 +186,43 @@ void DNSServer::processNextRequest()
186186
respondToRequest(buffer.get(), currentPacketSize);
187187
}
188188

189+
void DNSServer::writeNBOShort(uint16_t value)
190+
{
191+
_udp.write((unsigned char *)&value, 2);
192+
}
193+
189194
void DNSServer::replyWithIP(DNSHeader *dnsHeader,
190195
unsigned char * query,
191196
size_t queryLength)
192197
{
198+
uint16_t value;
199+
193200
dnsHeader->QR = DNS_QR_RESPONSE;
194201
dnsHeader->QDCount = lwip_htons(1);
195202
dnsHeader->ANCount = lwip_htons(1);
196203
dnsHeader->NSCount = 0;
197204
dnsHeader->ARCount = 0;
198205

199-
//_dnsHeader->RA = 1;
200-
201206
_udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
202207
_udp.write((unsigned char *) dnsHeader, sizeof(DNSHeader));
203208
_udp.write(query, queryLength);
204209

205-
_udp.write((uint8_t)192); // answer name is a pointer
206-
_udp.write((uint8_t)12); // pointer to offset at 0x00c
210+
// Rather than restate the name here, we use a pointer to the name contained
211+
// in the query section. Pointers have the top two bits set.
212+
value = 0xC000 | DNS_HEADER_SIZE;
213+
writeNBOShort(lwip_htons(value));
214+
215+
// Answer is type A (an IPv4 address)
216+
writeNBOShort(lwip_htons(DNS_QTYPE_A));
207217

208-
_udp.write((uint8_t)0); // 0x0001 answer is type A query (host address)
209-
_udp.write((uint8_t)1);
218+
// Answer is in the Internet Class
219+
writeNBOShort(lwip_htons(DNS_QCLASS_IN));
210220

211-
_udp.write((uint8_t)0); //0x0001 answer is class IN (internet address)
212-
_udp.write((uint8_t)1);
213-
221+
// Output TTL (already NBO)
214222
_udp.write((unsigned char*)&_ttl, 4);
215223

216224
// Length of RData is 4 bytes (because, in this case, RData is IPv4)
217-
_udp.write((uint8_t)0);
218-
_udp.write((uint8_t)4);
225+
writeNBOShort(lwip_htons(sizeof(_resolvedIP)));
219226
_udp.write(_resolvedIP, sizeof(_resolvedIP));
220227
_udp.endPacket();
221228
}

Diff for: libraries/DNSServer/src/DNSServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,6 @@ class DNSServer
8282
void replyWithError(DNSHeader *dnsHeader,
8383
DNSReplyCode rcode);
8484
void respondToRequest(uint8_t *buffer, size_t length);
85+
void writeNBOShort(uint16_t value);
8586
};
8687
#endif

0 commit comments

Comments
 (0)