DnsServer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * DnsServer.h
8  *
9  * File Author: https://github.com/patrickjahns
10  *
11  * The code is a port of the following projects
12  * https://github.com/israellot/esp-ginx/tree/master/app/dns
13  * https://github.com/esp8266/Arduino/tree/master/libraries/DNSServer
14  * Created on March 4, 2016
15  *
16  ****/
17 
23 #pragma once
24 
25 #include "UdpConnection.h"
26 #include <WString.h>
27 #include <IpAddress.h>
28 
29 #define DNS_QR_QUERY 0
30 #define DNS_QR_RESPONSE 1
31 #define DNS_OPCODE_QUERY 0
32 
33 enum class DnsReplyCode {
34  NoError = 0,
35  FormError = 1,
36  ServerFailure = 2,
38  NotImplemented = 4,
39  Refused = 5,
40  YXDomain = 6,
41  YXRRSet = 7,
42  NXRRSet = 8
43 };
44 
45 struct DnsHeader {
46  uint16_t ID; // identification number
47  char RD : 1; // recursion desired
48  char TC : 1; // truncated message
49  char AA : 1; // authoritative answer
50  char OPCode : 4; // message_type
51  char QR : 1; // query/response flag
52  char RCode : 4; // response code
53  char Z : 3; // its z! reserved
54  char RA : 1; // recursion available
55  uint16_t QDCount; // number of question entries
56  uint16_t ANCount; // number of answer entries
57  uint16_t NSCount; // number of authority entries
58  uint16_t ARCount; // number of resource entries
59 };
60 
64 class DnsServer : public UdpConnection
65 {
66 public:
71  {
72  errorReplyCode = replyCode;
73  }
74 
78  void setTTL(uint32_t ttl)
79  {
80  this->ttl = ttl;
81  }
82 
90  bool start(uint16_t port, const String& domainName, const IpAddress& resolvedIP);
91 
95  void stop()
96  {
97  close();
98  }
99 
100 protected:
101  void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;
102  size_t processQuestion(char* buffer, size_t requestLen);
103 
104 private:
105  uint16_t port = 0;
106  String domainName;
107  IpAddress resolvedIP;
108  uint32_t ttl = 60;
110 };
111 
DNS server class.
Definition: DnsServer.h:65
bool start(uint16_t port, const String &domainName, const IpAddress &resolvedIP)
Start the DNS server.
size_t processQuestion(char *buffer, size_t requestLen)
void onReceive(pbuf *buf, IpAddress remoteIP, uint16_t remotePort) override
void stop()
Stop the DNS server.
Definition: DnsServer.h:95
void setTTL(uint32_t ttl)
Set message Time-To-Live in seconds.
Definition: DnsServer.h:78
void setErrorReplyCode(DnsReplyCode replyCode)
Set error reply code.
Definition: DnsServer.h:70
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
The String class.
Definition: WString.h:133
Definition: UdpConnection.h:28
virtual void close()
DnsReplyCode
Definition: DnsServer.h:33
Definition: DnsServer.h:45
char TC
Definition: DnsServer.h:48
char RA
Definition: DnsServer.h:54
uint16_t ID
Definition: DnsServer.h:46
uint16_t QDCount
Definition: DnsServer.h:55
char RD
Definition: DnsServer.h:47
uint16_t ANCount
Definition: DnsServer.h:56
char AA
Definition: DnsServer.h:49
char RCode
Definition: DnsServer.h:52
char Z
Definition: DnsServer.h:53
char QR
Definition: DnsServer.h:51
uint16_t NSCount
Definition: DnsServer.h:57
char OPCode
Definition: DnsServer.h:50
uint16_t ARCount
Definition: DnsServer.h:58