Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
DNSServer.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/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 #ifndef _SMING_CORE_NETWORK_DNS_SERVER_H_
24 #define _SMING_CORE_NETWORK_DNS_SERVER_H_
25 
26 #include "UdpConnection.h"
27 #include "WString.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,
37  NonExistentDomain = 3,
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; // authoritive 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 
61 class DNSServer : public UdpConnection
62 {
63 public:
64  DNSServer()
65  {
66  }
67 
68  void setErrorReplyCode(DNSReplyCode replyCode)
69  {
70  errorReplyCode = replyCode;
71  }
72 
73  void setTTL(uint32_t ttl)
74  {
75  this->ttl = ttl;
76  }
77 
78  // Returns true if successful, false if there are no sockets available
79  bool start(uint16_t port, const String& domainName, const IPAddress& resolvedIP);
80 
81  // stops the DNS server
82  void stop();
83 
84 protected:
85  void onReceive(pbuf* buf, IPAddress remoteIP, uint16_t remotePort) override;
86 
87 private:
88  uint16_t port = 0;
89  String domainName;
90  ip_addr resolvedIP;
91  char* buffer = nullptr;
92  DNSHeader* dnsHeader = nullptr;
93  uint32_t ttl = 60;
94  DNSReplyCode errorReplyCode = DNSReplyCode::NonExistentDomain;
95 
96  static void downcaseAndRemoveWwwPrefix(String& domainName);
97  String getDomainNameWithoutWwwPrefix();
98  bool requestIncludesOnlyOneQuestion();
99 };
100 
102 #endif /* _SMING_CORE_NETWORK_DNS_SERVER_H_ */
Definition: UdpConnection.h:29
The string class.
Definition: WString.h:104
Definition: DNSServer.h:61
Definition: DNSServer.h:45
Definition: IPAddress.h:37