|
| 1 | +// Port of CC3000 MDNS Responder to WINC1500. |
| 2 | +// Author: Tony DiCola |
| 3 | +// |
| 4 | +// This MDNSResponder class implements just enough MDNS functionality to respond |
| 5 | +// to name requests, for example 'foo.local'. This does not implement any other |
| 6 | +// MDNS or Bonjour functionality like services, etc. |
| 7 | +// |
| 8 | +// Copyright (c) 2016 Adafruit Industries. All right reserved. |
| 9 | +// |
| 10 | +// This library is free software; you can redistribute it and/or |
| 11 | +// modify it under the terms of the GNU Lesser General Public |
| 12 | +// License as published by the Free Software Foundation; either |
| 13 | +// version 2.1 of the License, or (at your option) any later version. |
| 14 | +// |
| 15 | +// This library is distributed in the hope that it will be useful, |
| 16 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | +// Lesser General Public License for more details. |
| 19 | +// |
| 20 | +// You should have received a copy of the GNU Lesser General Public |
| 21 | +// License along with this library; if not, write to the Free Software |
| 22 | +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | + |
| 24 | +#include <avr/pgmspace.h> |
| 25 | + |
| 26 | +#include "Arduino.h" |
| 27 | +#include "WiFiMdns.h" |
| 28 | + |
| 29 | +// Important RFC's for reference: |
| 30 | +// - DNS request and response: http://www.ietf.org/rfc/rfc1035.txt |
| 31 | +// - Multicast DNS: http://www.ietf.org/rfc/rfc6762.txt |
| 32 | + |
| 33 | +#define HEADER_SIZE 12 |
| 34 | +#define TTL_OFFSET 4 |
| 35 | +#define IP_OFFSET 10 |
| 36 | + |
| 37 | +const uint8_t expectedRequestHeader[HEADER_SIZE] PROGMEM = { |
| 38 | + 0x00, 0x00, |
| 39 | + 0x00, 0x00, |
| 40 | + 0x00, 0x01, |
| 41 | + 0x00, 0x00, |
| 42 | + 0x00, 0x00, |
| 43 | + 0x00, 0x00 |
| 44 | +}; |
| 45 | + |
| 46 | +const uint8_t responseHeader[] PROGMEM = { |
| 47 | + 0x00, 0x00, // ID = 0 |
| 48 | + 0x84, 0x00, // Flags = response + authoritative answer |
| 49 | + 0x00, 0x00, // Question count = 0 |
| 50 | + 0x00, 0x01, // Answer count = 1 |
| 51 | + 0x00, 0x00, // Name server records = 0 |
| 52 | + 0x00, 0x01 // Additional records = 1 |
| 53 | +}; |
| 54 | + |
| 55 | +// Generate positive response for IPV4 address |
| 56 | +const uint8_t aRecord[] PROGMEM = { |
| 57 | + 0x00, 0x01, // Type = 1, A record/IPV4 address |
| 58 | + 0x80, 0x01, // Class = Internet, with cache flush bit |
| 59 | + 0x00, 0x00, 0x00, 0x00, // TTL in seconds, to be filled in later |
| 60 | + 0x00, 0x04, // Length of record |
| 61 | + 0x00, 0x00, 0x00, 0x00 // IP address, to be filled in later |
| 62 | +}; |
| 63 | + |
| 64 | +// Generate negative response for IPV6 address (CC3000 doesn't support IPV6) |
| 65 | +const uint8_t nsecRecord[] PROGMEM = { |
| 66 | + 0xC0, 0x0C, // Name offset |
| 67 | + 0x00, 0x2F, // Type = 47, NSEC (overloaded by MDNS) |
| 68 | + 0x80, 0x01, // Class = Internet, with cache flush bit |
| 69 | + 0x00, 0x00, 0x00, 0x00, // TTL in seconds, to be filled in later |
| 70 | + 0x00, 0x08, // Length of record |
| 71 | + 0xC0, 0x0C, // Next domain = offset to FQDN |
| 72 | + 0x00, // Block number = 0 |
| 73 | + 0x04, // Length of bitmap = 4 bytes |
| 74 | + 0x40, 0x00, 0x00, 0x00 // Bitmap value = Only first bit (A record/IPV4) is set |
| 75 | +}; |
| 76 | + |
| 77 | +const uint8_t domain[] PROGMEM = { 'l', 'o', 'c', 'a', 'l' }; |
| 78 | + |
| 79 | +MDNSResponder::MDNSResponder() : |
| 80 | + expectedRequestLength(0) |
| 81 | +{ |
| 82 | +} |
| 83 | + |
| 84 | +MDNSResponder::~MDNSResponder() |
| 85 | +{ |
| 86 | +} |
| 87 | + |
| 88 | +bool MDNSResponder::begin(const char* _name, uint32_t _ttlSeconds) |
| 89 | +{ |
| 90 | + int nameLength = strlen(_name); |
| 91 | + |
| 92 | + if (nameLength > 255) { |
| 93 | + // Can only handle domains that are upto 255 chars in length. |
| 94 | + expectedRequestLength = 0; |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + name = _name; |
| 99 | + ttlSeconds = _ttlSeconds; |
| 100 | + |
| 101 | + name.toLowerCase(); |
| 102 | + expectedRequestLength = HEADER_SIZE + 1 + nameLength + 1 + sizeof(domain) + 5; |
| 103 | + |
| 104 | + // Open the MDNS UDP listening socket on port 5353 with multicast address |
| 105 | + // 224.0.0.251 (0xE00000FB) |
| 106 | + if (!udpSocket.beginMulti(IPAddress(224, 0, 0, 251), 5353)) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + return true; |
| 111 | +} |
| 112 | + |
| 113 | +void MDNSResponder::poll() |
| 114 | +{ |
| 115 | + if (parseRequest()) { |
| 116 | + replyToRequest(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +bool MDNSResponder::parseRequest() |
| 121 | +{ |
| 122 | + if (udpSocket.parsePacket()) { |
| 123 | + // check if parsed packet matches expected request length |
| 124 | + if (udpSocket.available() != expectedRequestLength) { |
| 125 | + // it does not, read the full packet in and drop data |
| 126 | + while(udpSocket.available()) { |
| 127 | + udpSocket.read(); |
| 128 | + } |
| 129 | + |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + // read packet |
| 134 | + uint8_t request[expectedRequestLength]; |
| 135 | + udpSocket.read(request, expectedRequestLength); |
| 136 | + |
| 137 | + // parse request |
| 138 | + uint8_t requestNameLength = request[HEADER_SIZE]; |
| 139 | + uint8_t* requestName = &request[HEADER_SIZE + 1]; |
| 140 | + uint8_t requestDomainLength = request[HEADER_SIZE + 1 + requestNameLength]; |
| 141 | + uint8_t* requestDomain = &request[HEADER_SIZE + 1 + requestNameLength + 1]; |
| 142 | + uint16_t requestQtype; |
| 143 | + uint16_t requestQclass; |
| 144 | + |
| 145 | + memcpy(&requestQtype, &request[expectedRequestLength - 4], sizeof(requestQtype)); |
| 146 | + memcpy(&requestQclass, &request[expectedRequestLength - 2], sizeof(requestQclass)); |
| 147 | + |
| 148 | + requestQtype = _ntohs(requestQtype); |
| 149 | + requestQclass = _ntohs(requestQclass); |
| 150 | + |
| 151 | + // compare request |
| 152 | + if (memcmp_P(request, expectedRequestHeader, HEADER_SIZE) == 0 && // request header match |
| 153 | + requestNameLength == name.length() && // name length match |
| 154 | + strncasecmp(name.c_str(), (char*)requestName, requestNameLength) == 0 && // name match |
| 155 | + requestDomainLength == sizeof(domain) && // domain length match |
| 156 | + memcmp_P(requestDomain, domain, requestDomainLength) == 0 && // suffix match |
| 157 | + requestQtype == 0x0001 && // request QType match |
| 158 | + requestQclass == 0x0001) { // request QClass match |
| 159 | + |
| 160 | + return true; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return false; |
| 165 | +} |
| 166 | + |
| 167 | +void MDNSResponder::replyToRequest() |
| 168 | +{ |
| 169 | + int nameLength = name.length(); |
| 170 | + int domainLength = sizeof(domain); |
| 171 | + uint32_t ipAddress = WiFi.localIP(); |
| 172 | + uint32_t ttl = _htonl(ttlSeconds); |
| 173 | + |
| 174 | + int responseSize = sizeof(responseHeader) + 1 + nameLength + 1 + domainLength + 1 + sizeof(aRecord) + sizeof(nsecRecord); |
| 175 | + uint8_t response[responseSize]; |
| 176 | + uint8_t* r = response; |
| 177 | + |
| 178 | + // copy header |
| 179 | + memcpy_P(r, responseHeader, sizeof(responseHeader)); |
| 180 | + r += sizeof(responseHeader); |
| 181 | + |
| 182 | + // copy name |
| 183 | + *r = nameLength; |
| 184 | + memcpy(r + 1, name.c_str(), nameLength); |
| 185 | + r += (1 + nameLength); |
| 186 | + |
| 187 | + // copy domain |
| 188 | + *r = domainLength; |
| 189 | + memcpy_P(r + 1, domain, domainLength); |
| 190 | + r += (1 + domainLength); |
| 191 | + |
| 192 | + // terminator |
| 193 | + *r = 0x00; |
| 194 | + r++; |
| 195 | + |
| 196 | + // copy A record |
| 197 | + memcpy_P(r, aRecord, sizeof(aRecord)); |
| 198 | + memcpy(r + TTL_OFFSET, &ttl, sizeof(ttl)); // replace TTL value |
| 199 | + memcpy(r + IP_OFFSET, &ipAddress, sizeof(ipAddress)); // replace IP address value |
| 200 | + r += sizeof(aRecord); |
| 201 | + |
| 202 | + // copy NSEC record |
| 203 | + memcpy_P(r, nsecRecord, sizeof(nsecRecord)); |
| 204 | + r += sizeof(nsecRecord); |
| 205 | + |
| 206 | + udpSocket.beginPacket(IPAddress(224, 0, 0, 251), 5353); |
| 207 | + udpSocket.write(response, responseSize); |
| 208 | + udpSocket.endPacket(); |
| 209 | +} |
0 commit comments