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 
28 #define DNS_QR_QUERY 0
29 #define DNS_QR_RESPONSE 1
30 #define DNS_OPCODE_QUERY 0
31 
32 enum class DnsReplyCode {
33  NoError = 0,
34  FormError = 1,
35  ServerFailure = 2,
37  NotImplemented = 4,
38  Refused = 5,
39  YXDomain = 6,
40  YXRRSet = 7,
41  NXRRSet = 8
42 };
43 
44 struct DnsHeader {
45  uint16_t ID; // identification number
46  char RD : 1; // recursion desired
47  char TC : 1; // truncated message
48  char AA : 1; // authoritive answer
49  char OPCode : 4; // message_type
50  char QR : 1; // query/response flag
51  char RCode : 4; // response code
52  char Z : 3; // its z! reserved
53  char RA : 1; // recursion available
54  uint16_t QDCount; // number of question entries
55  uint16_t ANCount; // number of answer entries
56  uint16_t NSCount; // number of authority entries
57  uint16_t ARCount; // number of resource entries
58 };
59 
60 class DnsServer : public UdpConnection
61 {
62 public:
64  {
65  }
66 
68  {
69  errorReplyCode = replyCode;
70  }
71 
72  void setTTL(uint32_t ttl)
73  {
74  this->ttl = ttl;
75  }
76 
77  // Returns true if successful, false if there are no sockets available
78  bool start(uint16_t port, const String& domainName, const IpAddress& resolvedIP);
79 
80  // stops the DNS server
81  void stop();
82 
83 protected:
84  void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;
85 
86 private:
87  uint16_t port = 0;
88  String domainName;
89  ip_addr resolvedIP;
90  char* buffer = nullptr;
91  DnsHeader* dnsHeader = nullptr;
92  uint32_t ttl = 60;
94 
95  static void downcaseAndRemoveWwwPrefix(String& domainName);
96  String getDomainNameWithoutWwwPrefix();
97  bool requestIncludesOnlyOneQuestion();
98 };
99 
102 
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:37
Definition: UdpConnection.h:26
char RD
Definition: DnsServer.h:46
char TC
Definition: DnsServer.h:47
char RA
Definition: DnsServer.h:53
char AA
Definition: DnsServer.h:48
The String class.
Definition: WString.h:136
char QR
Definition: DnsServer.h:50
char RCode
Definition: DnsServer.h:51
uint16_t ID
Definition: DnsServer.h:45
DnsReplyCode
Definition: DnsServer.h:32
char Z
Definition: DnsServer.h:52
#define SMING_DEPRECATED
Definition: sming_attr.h:30
void setErrorReplyCode(DnsReplyCode replyCode)
Definition: DnsServer.h:67
uint16_t NSCount
Definition: DnsServer.h:56
Definition: DnsServer.h:60
uint16_t ARCount
Definition: DnsServer.h:57
uint16_t ANCount
Definition: DnsServer.h:55
Definition: DnsServer.h:44
uint16_t QDCount
Definition: DnsServer.h:54
void setTTL(uint32_t ttl)
Definition: DnsServer.h:72
DnsServer()
Definition: DnsServer.h:63
char OPCode
Definition: DnsServer.h:49