|
| 1 | +#include "DNSServer.h" |
| 2 | +#include <lwip/def.h> |
| 3 | +#include <Arduino.h> |
| 4 | + |
| 5 | +DNSServer::DNSServer() |
| 6 | +{ |
| 7 | + _ttl = htonl(DNS_DEFAULT_TTL); |
| 8 | + _ErrReplyCode = DNSReplyCode::NonExistentDomain; |
| 9 | + _DnsHeader = (DNSHeader*) malloc( sizeof(DNSHeader) ) ; |
| 10 | + _DnsQuestion = (DNSQuestion*) malloc( sizeof(DNSQuestion) ) ; |
| 11 | + _buf = NULL; |
| 12 | + _CurPacketSize = 0; |
| 13 | + _port = 0; |
| 14 | +} |
| 15 | + |
| 16 | +DNSServer::~DNSServer() |
| 17 | +{ |
| 18 | + if (_DnsHeader) { |
| 19 | + free(_DnsHeader); |
| 20 | + _DnsHeader = NULL; |
| 21 | + } |
| 22 | + if (_DnsQuestion) { |
| 23 | + free(_DnsQuestion); |
| 24 | + _DnsQuestion = NULL; |
| 25 | + } |
| 26 | + if (_buf) { |
| 27 | + free(_buf); |
| 28 | + _buf = NULL; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +bool DNSServer::start(const uint16_t &port, const String &domainName, |
| 33 | + const IPAddress &resolvedIP) |
| 34 | +{ |
| 35 | + _port = port; |
| 36 | + _buf = NULL; |
| 37 | + _domain = domainName; |
| 38 | + _ResolveIP[0] = resolvedIP[0]; |
| 39 | + _ResolveIP[1] = resolvedIP[1]; |
| 40 | + _ResolveIP[2] = resolvedIP[2]; |
| 41 | + _ResolveIP[3] = resolvedIP[3]; |
| 42 | + downcaseAndRemoveWwwPrefix(_domain); |
| 43 | + return _Udp.begin(_port) == 1; |
| 44 | +} |
| 45 | + |
| 46 | +void DNSServer::setErrorReplyCode(const DNSReplyCode &replyCode) |
| 47 | +{ |
| 48 | + _ErrReplyCode = replyCode; |
| 49 | +} |
| 50 | + |
| 51 | +void DNSServer::setTTL(const uint32_t &ttl) |
| 52 | +{ |
| 53 | + _ttl = htonl(ttl); |
| 54 | +} |
| 55 | + |
| 56 | +void DNSServer::stop() |
| 57 | +{ |
| 58 | + _Udp.stop(); |
| 59 | + free(_buf); |
| 60 | + _buf = NULL; |
| 61 | +} |
| 62 | + |
| 63 | +void DNSServer::downcaseAndRemoveWwwPrefix(String &domainName) |
| 64 | +{ |
| 65 | + domainName.toLowerCase(); |
| 66 | + domainName.replace("www.", ""); |
| 67 | +} |
| 68 | + |
| 69 | +void DNSServer::processNextRequest() |
| 70 | +{ |
| 71 | + _CurPacketSize = _Udp.parsePacket(); |
| 72 | + if (_CurPacketSize) |
| 73 | + { |
| 74 | + // Allocate buffer for the DNS query |
| 75 | + if (_buf != NULL) |
| 76 | + free(_buf); |
| 77 | + _buf = (unsigned char*)malloc(_CurPacketSize * sizeof(char)); |
| 78 | + if (_buf == NULL) |
| 79 | + return; |
| 80 | + |
| 81 | + // Put the packet received in the buffer and get DNS header (beginning of message) |
| 82 | + // and the question |
| 83 | + _Udp.read(_buf, _CurPacketSize); |
| 84 | + memcpy( _DnsHeader, _buf, DNS_HEADER_SIZE ) ; |
| 85 | + if ( requestIncludesOnlyOneQuestion() ) |
| 86 | + { |
| 87 | + // The QName has a variable length, maximum 255 bytes and is comprised of multiple labels. |
| 88 | + // Each label contains a byte to describe its length and the label itself. The list of |
| 89 | + // labels terminates with a zero-valued byte. In "github.com", we have two labels "github" & "com" |
| 90 | + // Iterate through the labels and copy them as they come into a single buffer (for simplicity's sake) |
| 91 | + _DnsQuestion->QNameLength = 0 ; |
| 92 | + while ( _buf[ DNS_HEADER_SIZE + _DnsQuestion->QNameLength ] != 0 ) |
| 93 | + { |
| 94 | + memcpy( (void*) &_DnsQuestion->QName[_DnsQuestion->QNameLength], (void*) &_buf[DNS_HEADER_SIZE + _DnsQuestion->QNameLength], _buf[DNS_HEADER_SIZE + _DnsQuestion->QNameLength] + 1 ) ; |
| 95 | + _DnsQuestion->QNameLength += _buf[DNS_HEADER_SIZE + _DnsQuestion->QNameLength] + 1 ; |
| 96 | + } |
| 97 | + _DnsQuestion->QName[_DnsQuestion->QNameLength] = 0 ; |
| 98 | + _DnsQuestion->QNameLength++ ; |
| 99 | + |
| 100 | + // Copy the QType and QClass |
| 101 | + memcpy( &_DnsQuestion->QType, (void*) &_buf[DNS_HEADER_SIZE + _DnsQuestion->QNameLength], sizeof(_DnsQuestion->QType) ) ; |
| 102 | + memcpy( &_DnsQuestion->QClass, (void*) &_buf[DNS_HEADER_SIZE + _DnsQuestion->QNameLength + sizeof(_DnsQuestion->QType)], sizeof(_DnsQuestion->QClass) ) ; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + if (_DnsHeader->QRFlag == DNS_QR_QUERY && |
| 107 | + _DnsHeader->OPCode == DNS_OPCODE_QUERY && |
| 108 | + requestIncludesOnlyOneQuestion() && |
| 109 | + (_domain == "*" || getDomainNameWithoutWwwPrefix() == _domain) |
| 110 | + ) |
| 111 | + { |
| 112 | + replyWithIP(); |
| 113 | + } |
| 114 | + else if (_DnsHeader->QRFlag == DNS_QR_QUERY) |
| 115 | + { |
| 116 | + replyWithCustomCode(); |
| 117 | + } |
| 118 | + |
| 119 | + free(_buf); |
| 120 | + _buf = NULL; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +bool DNSServer::requestIncludesOnlyOneQuestion() |
| 125 | +{ |
| 126 | + return ntohs(_DnsHeader->QuesCnt) == 1 && |
| 127 | + _DnsHeader->AnsCnt == 0 && |
| 128 | + _DnsHeader->AuthCnt == 0 && |
| 129 | + _DnsHeader->ResCnt == 0; |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +String DNSServer::getDomainNameWithoutWwwPrefix() |
| 134 | +{ |
| 135 | + // Error checking : if the buffer containing the DNS request is a null pointer, return an empty domain |
| 136 | + String parsedDomainName = ""; |
| 137 | + if (_buf == NULL) |
| 138 | + return parsedDomainName; |
| 139 | + |
| 140 | + // Set the start of the domain just after the header (12 bytes). If equal to null character, return an empty domain |
| 141 | + unsigned char *start = _buf + DNS_OFFSET_DOMAIN_NAME; |
| 142 | + if (*start == 0) |
| 143 | + { |
| 144 | + return parsedDomainName; |
| 145 | + } |
| 146 | + |
| 147 | + int pos = 0; |
| 148 | + while(true) |
| 149 | + { |
| 150 | + unsigned char labelLength = *(start + pos); |
| 151 | + for(int i = 0; i < labelLength; i++) |
| 152 | + { |
| 153 | + pos++; |
| 154 | + parsedDomainName += (char)*(start + pos); |
| 155 | + } |
| 156 | + pos++; |
| 157 | + if (*(start + pos) == 0) |
| 158 | + { |
| 159 | + downcaseAndRemoveWwwPrefix(parsedDomainName); |
| 160 | + return parsedDomainName; |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + parsedDomainName += "."; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +void DNSServer::replyWithIP() |
| 170 | +{ |
| 171 | + _Udp.beginPacket(_Udp.remoteIP(), _Udp.remotePort()); |
| 172 | + |
| 173 | + // Change the type of message to a response and set the number of answers equal to |
| 174 | + // the number of questions in the header |
| 175 | + _DnsHeader->QRFlag = DNS_QR_RESPONSE; |
| 176 | + _DnsHeader->AnsCnt = _DnsHeader->QuesCnt; |
| 177 | + _Udp.write( (unsigned char*) _DnsHeader, DNS_HEADER_SIZE ) ; |
| 178 | + |
| 179 | + // Write the question |
| 180 | + _Udp.write(_DnsQuestion->QName, _DnsQuestion->QNameLength) ; |
| 181 | + _Udp.write( (unsigned char*) &_DnsQuestion->QType, 2 ) ; |
| 182 | + _Udp.write( (unsigned char*) &_DnsQuestion->QClass, 2 ) ; |
| 183 | + |
| 184 | + // Write the answer |
| 185 | + // Use DNS name compression : instead of repeating the name in this RNAME occurence, |
| 186 | + // set the two MSB of the byte corresponding normally to the length to 1. The following |
| 187 | + // 14 bits must be used to specify the offset of the domain name in the message |
| 188 | + // (<255 here so the first byte has the 6 LSB at 0) |
| 189 | + _Udp.write((uint8_t) 0xC0); |
| 190 | + _Udp.write((uint8_t) DNS_OFFSET_DOMAIN_NAME); |
| 191 | + |
| 192 | + // DNS type A : host address, DNS class IN for INternet, returning an IPv4 address |
| 193 | + uint16_t answerType = htons(DNS_TYPE_HOST_ADDRESS), answerClass = htons(DNS_CLASS_IN), answerIPv4 = htons(DNS_RDLENGTH_IPV4) ; |
| 194 | + _Udp.write((unsigned char*) &answerType, 2 ); |
| 195 | + _Udp.write((unsigned char*) &answerClass, 2 ); |
| 196 | + _Udp.write((unsigned char*) &_ttl, 4); // DNS Time To Live |
| 197 | + _Udp.write((unsigned char*) &answerIPv4, 2 ); |
| 198 | + _Udp.write(_ResolveIP, sizeof(_ResolveIP)); // The IP address to return |
| 199 | + _Udp.endPacket(); |
| 200 | + |
| 201 | +} |
| 202 | + |
| 203 | +void DNSServer::replyWithCustomCode() |
| 204 | +{ |
| 205 | + _DnsHeader->QRFlag = DNS_QR_RESPONSE; |
| 206 | + _DnsHeader->RspCode = (unsigned char)_ErrReplyCode; |
| 207 | + _DnsHeader->QuesCnt = 0; |
| 208 | + |
| 209 | + _Udp.beginPacket(_Udp.remoteIP(), _Udp.remotePort()); |
| 210 | + _Udp.write((unsigned char*)_DnsHeader, sizeof(DNSHeader)); |
| 211 | + _Udp.endPacket(); |
| 212 | +} |
0 commit comments