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; // 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:
65  {
66  }
67 
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  IpAddress resolvedIP;
91  char* buffer = nullptr;
92  DnsHeader* dnsHeader = nullptr;
93  uint32_t ttl = 60;
95 
96  static void downcaseAndRemoveWwwPrefix(String& domainName);
97  String getDomainNameWithoutWwwPrefix();
98  bool requestIncludesOnlyOneQuestion();
99 };
100 
103 
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:43
Definition: UdpConnection.h:26
char RD
Definition: DnsServer.h:47
char TC
Definition: DnsServer.h:48
char RA
Definition: DnsServer.h:54
char AA
Definition: DnsServer.h:49
The String class.
Definition: WString.h:136
char QR
Definition: DnsServer.h:51
char RCode
Definition: DnsServer.h:52
uint16_t ID
Definition: DnsServer.h:46
DnsReplyCode
Definition: DnsServer.h:33
char Z
Definition: DnsServer.h:53
#define SMING_DEPRECATED
Definition: sming_attr.h:30
void setErrorReplyCode(DnsReplyCode replyCode)
Definition: DnsServer.h:68
uint16_t NSCount
Definition: DnsServer.h:57
Definition: DnsServer.h:61
uint16_t ARCount
Definition: DnsServer.h:58
uint16_t ANCount
Definition: DnsServer.h:56
Definition: DnsServer.h:45
uint16_t QDCount
Definition: DnsServer.h:55
void setTTL(uint32_t ttl)
Definition: DnsServer.h:73
DnsServer()
Definition: DnsServer.h:64
char OPCode
Definition: DnsServer.h:50