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:
68  {
69  }
70 
75  {
76  errorReplyCode = replyCode;
77  }
78 
82  void setTTL(uint32_t ttl)
83  {
84  this->ttl = ttl;
85  }
86 
94  bool start(uint16_t port, const String& domainName, const IpAddress& resolvedIP);
95 
99  void stop();
100 
101 protected:
102  void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;
103 
104 private:
105  uint16_t port = 0;
106  String domainName;
107  IpAddress resolvedIP;
108  char* buffer = nullptr;
109  DnsHeader* dnsHeader = nullptr;
110  uint32_t ttl = 60;
112 
113  static void downcaseAndRemoveWwwPrefix(String& domainName);
114  String getDomainNameWithoutWwwPrefix();
115  bool requestIncludesOnlyOneQuestion();
116 };
117 
char RCode
Definition: DnsServer.h:52
uint16_t ARCount
Definition: DnsServer.h:58
char TC
Definition: DnsServer.h:48
void stop()
Stop the DNS server.
Definition: UdpConnection.h:27
char RA
Definition: DnsServer.h:54
void setTTL(uint32_t ttl)
Set message Time-To-Live in seconds.
Definition: DnsServer.h:82
char OPCode
Definition: DnsServer.h:50
The String class.
Definition: WString.h:136
DnsReplyCode
Definition: DnsServer.h:33
void onReceive(pbuf *buf, IpAddress remoteIP, uint16_t remotePort) override
DnsServer()
Definition: DnsServer.h:67
uint16_t ID
Definition: DnsServer.h:46
uint16_t QDCount
Definition: DnsServer.h:55
void setErrorReplyCode(DnsReplyCode replyCode)
Set error reply code.
Definition: DnsServer.h:74
bool start(uint16_t port, const String &domainName, const IpAddress &resolvedIP)
Start the DNS server.
DNS server class.
Definition: DnsServer.h:64
char QR
Definition: DnsServer.h:51
Definition: DnsServer.h:45
uint16_t ANCount
Definition: DnsServer.h:56
char RD
Definition: DnsServer.h:47
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:44
char Z
Definition: DnsServer.h:53
char AA
Definition: DnsServer.h:49
uint16_t NSCount
Definition: DnsServer.h:57