-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add an LLMNR responder implementation #2880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+568
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,281 @@ | ||
/* | ||
* ESP8266 LLMNR responder | ||
* Copyright (C) 2017 Stephen Warren <[email protected]> | ||
* | ||
* Based on: | ||
* ESP8266 Multicast DNS (port of CC3000 Multicast DNS library) | ||
* Version 1.1 | ||
* Copyright (c) 2013 Tony DiCola ([email protected]) | ||
* ESP8266 port (c) 2015 Ivan Grokhotkov ([email protected]) | ||
* MDNS-SD Suport 2015 Hristo Gochkov | ||
* Extended MDNS-SD support 2016 Lars Englund ([email protected]) | ||
* | ||
* License (MIT license): | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* Reference: | ||
* https://tools.ietf.org/html/rfc4795 (LLMNR) | ||
* https://tools.ietf.org/html/rfc1035 (DNS) | ||
*/ | ||
|
||
#include <debug.h> | ||
#include <functional> | ||
#include <ESP8266LLMNR.h> | ||
#include <WiFiUdp.h> | ||
|
||
extern "C" { | ||
#include <user_interface.h> | ||
} | ||
|
||
#include <lwip/udp.h> | ||
#include <lwip/igmp.h> | ||
#include <include/UdpContext.h> | ||
|
||
//#define LLMNR_DEBUG | ||
|
||
#define BIT(x) (1 << (x)) | ||
|
||
#define FLAGS_QR BIT(15) | ||
#define FLAGS_OP_SHIFT 11 | ||
#define FLAGS_OP_MASK 0xf | ||
#define FLAGS_C BIT(10) | ||
#define FLAGS_TC BIT(9) | ||
#define FLAGS_T BIT(8) | ||
#define FLAGS_RCODE_SHIFT 0 | ||
#define FLAGS_RCODE_MASK 0xf | ||
|
||
#define _conn_read16() (((uint16_t)_conn->read() << 8) | _conn->read()) | ||
#define _conn_read8() _conn->read() | ||
#define _conn_readS(b, l) _conn->read((b), (l)); | ||
|
||
static const IPAddress LLMNR_MULTICAST_ADDR(224, 0, 0, 252); | ||
static const int LLMNR_MULTICAST_TTL = 1; | ||
static const int LLMNR_PORT = 5355; | ||
|
||
LLMNRResponder::LLMNRResponder() : | ||
_conn(0) { | ||
} | ||
|
||
LLMNRResponder::~LLMNRResponder() { | ||
if (_conn) | ||
_conn->unref(); | ||
} | ||
|
||
bool LLMNRResponder::begin(const char* hostname) { | ||
// Max length for a single label in DNS | ||
if (strlen(hostname) > 63) | ||
return false; | ||
|
||
_hostname = hostname; | ||
_hostname.toLowerCase(); | ||
|
||
_sta_got_ip_handler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& event){ | ||
_restart(); | ||
}); | ||
|
||
_sta_disconnected_handler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& event) { | ||
_restart(); | ||
}); | ||
|
||
return _restart(); | ||
} | ||
|
||
void LLMNRResponder::notify_ap_change() { | ||
_restart(); | ||
} | ||
|
||
bool LLMNRResponder::_restart() { | ||
if (_conn) { | ||
_conn->unref(); | ||
_conn = 0; | ||
} | ||
|
||
ip_addr_t multicast_addr; | ||
multicast_addr.addr = (uint32_t)LLMNR_MULTICAST_ADDR; | ||
|
||
if (igmp_joingroup(IP_ADDR_ANY, &multicast_addr) != ERR_OK) | ||
return false; | ||
|
||
_conn = new UdpContext; | ||
_conn->ref(); | ||
|
||
if (!_conn->listen(*IP_ADDR_ANY, LLMNR_PORT)) | ||
return false; | ||
|
||
_conn->setMulticastTTL(LLMNR_MULTICAST_TTL); | ||
_conn->onRx(std::bind(&LLMNRResponder::_process_packet, this)); | ||
_conn->connect(multicast_addr, LLMNR_PORT); | ||
} | ||
|
||
void LLMNRResponder::_process_packet() { | ||
if (!_conn || !_conn->next()) | ||
return; | ||
|
||
#ifdef LLMNR_DEBUG | ||
Serial.println("LLMNR: RX'd packet"); | ||
#endif | ||
|
||
uint16_t id = _conn_read16(); | ||
uint16_t flags = _conn_read16(); | ||
uint16_t qdcount = _conn_read16(); | ||
uint16_t ancount = _conn_read16(); | ||
uint16_t nscount = _conn_read16(); | ||
uint16_t arcount = _conn_read16(); | ||
|
||
#ifdef LLMNR_DEBUG | ||
Serial.print("LLMNR: ID="); | ||
Serial.println(id, HEX); | ||
Serial.print("LLMNR: FLAGS="); | ||
Serial.println(flags, HEX); | ||
Serial.print("LLMNR: QDCOUNT="); | ||
Serial.println(qdcount); | ||
Serial.print("LLMNR: ANCOUNT="); | ||
Serial.println(ancount); | ||
Serial.print("LLMNR: NSCOUNT="); | ||
Serial.println(nscount); | ||
Serial.print("LLMNR: ARCOUNT="); | ||
Serial.println(arcount); | ||
#endif | ||
|
||
#define BAD_FLAGS (FLAGS_QR | (FLAGS_OP_MASK << FLAGS_OP_SHIFT) | FLAGS_C) | ||
if (flags & BAD_FLAGS) { | ||
#ifdef LLMNR_DEBUG | ||
Serial.println("Bad flags"); | ||
#endif | ||
return; | ||
} | ||
|
||
if (qdcount != 1) { | ||
#ifdef LLMNR_DEBUG | ||
Serial.println("QDCOUNT != 1"); | ||
#endif | ||
return; | ||
} | ||
|
||
if (ancount || nscount || arcount) { | ||
#ifdef LLMNR_DEBUG | ||
Serial.println("AN/NS/AR-COUNT != 0"); | ||
#endif | ||
return; | ||
} | ||
|
||
uint8_t namelen = _conn_read8(); | ||
#ifdef LLMNR_DEBUG | ||
Serial.print("QNAME len "); | ||
Serial.println(namelen); | ||
#endif | ||
if (namelen != _hostname.length()) { | ||
#ifdef LLMNR_DEBUG | ||
Serial.println("QNAME len mismatch"); | ||
#endif | ||
return; | ||
} | ||
|
||
char qname[64]; | ||
_conn_readS(qname, namelen); | ||
_conn_read8(); | ||
qname[namelen] = '\0'; | ||
#ifdef LLMNR_DEBUG | ||
Serial.print("QNAME "); | ||
Serial.println(qname); | ||
#endif | ||
|
||
if (strcmp(_hostname.c_str(), qname)) { | ||
#ifdef LLMNR_DEBUG | ||
Serial.println("QNAME mismatch"); | ||
#endif | ||
return; | ||
} | ||
|
||
uint16_t qtype = _conn_read16(); | ||
uint16_t qclass = _conn_read16(); | ||
|
||
#ifdef LLMNR_DEBUG | ||
Serial.print("QTYPE "); | ||
Serial.print(qtype); | ||
Serial.print(" QCLASS "); | ||
Serial.println(qclass); | ||
#endif | ||
|
||
bool have_rr = | ||
(qtype == 1) && /* A */ | ||
(qclass == 1); /* IN */ | ||
|
||
_conn->flush(); | ||
|
||
#ifdef LLMNR_DEBUG | ||
Serial.println("Match; responding"); | ||
if (!have_rr) | ||
Serial.println("(no matching RRs)"); | ||
#endif | ||
|
||
struct ip_info remote_ip_info; | ||
remote_ip_info.ip.addr = _conn->getRemoteAddress(); | ||
struct ip_info ip_info; | ||
bool match_ap = false; | ||
if (wifi_get_opmode() & SOFTAP_MODE) { | ||
wifi_get_ip_info(SOFTAP_IF, &ip_info); | ||
if (ip_info.ip.addr && ip_addr_netcmp(&remote_ip_info.ip, &ip_info.ip, &ip_info.netmask)) | ||
match_ap = true; | ||
} | ||
if (!match_ap) | ||
wifi_get_ip_info(STATION_IF, &ip_info); | ||
uint32_t ip = ip_info.ip.addr; | ||
|
||
// Header | ||
uint8_t header[] = { | ||
id >> 8, id & 0xff, // ID | ||
FLAGS_QR >> 8, 0, // FLAGS | ||
0, 1, // QDCOUNT | ||
0, !!have_rr, // ANCOUNT | ||
0, 0, // NSCOUNT | ||
0, 0, // ARCOUNT | ||
}; | ||
_conn->append(reinterpret_cast<const char*>(header), sizeof(header)); | ||
// Question | ||
_conn->append(reinterpret_cast<const char*>(&namelen), 1); | ||
_conn->append(qname, namelen); | ||
uint8_t q[] = { | ||
0, // Name terminator | ||
0, 1, // TYPE (A) | ||
0, 1, // CLASS (IN) | ||
}; | ||
_conn->append(reinterpret_cast<const char*>(q), sizeof(q)); | ||
// Answer, if we have one | ||
if (have_rr) { | ||
_conn->append(reinterpret_cast<const char*>(&namelen), 1); | ||
_conn->append(qname, namelen); | ||
uint8_t rr[] = { | ||
0, // Name terminator | ||
0, 1, // TYPE (A) | ||
0, 1, // CLASS (IN) | ||
0, 0, 0, 30, // TTL (30 seconds) | ||
0, 4, // RDLENGTH | ||
ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff, // RDATA | ||
}; | ||
_conn->append(reinterpret_cast<const char*>(rr), sizeof(rr)); | ||
} | ||
_conn->setMulticastInterface(remote_ip_info.ip); | ||
_conn->send(&remote_ip_info.ip, _conn->getRemotePort()); | ||
} | ||
|
||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LLMNR) | ||
LLMNRResponder LLMNR; | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* ESP8266 LLMNR responder | ||
* Copyright (C) 2017 Stephen Warren <[email protected]> | ||
* | ||
* Based on: | ||
* ESP8266 Multicast DNS (port of CC3000 Multicast DNS library) | ||
* Version 1.1 | ||
* Copyright (c) 2013 Tony DiCola ([email protected]) | ||
* ESP8266 port (c) 2015 Ivan Grokhotkov ([email protected]) | ||
* MDNS-SD Suport 2015 Hristo Gochkov | ||
* Extended MDNS-SD support 2016 Lars Englund ([email protected]) | ||
* | ||
* License (MIT license): | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#ifndef ESP8266LLMNR_H | ||
#define ESP8266LLMNR_H | ||
|
||
#include <ESP8266WiFi.h> | ||
|
||
class UdpContext; | ||
|
||
class LLMNRResponder { | ||
public: | ||
LLMNRResponder(); | ||
~LLMNRResponder(); | ||
|
||
/* Initialize and start responding to LLMNR requests on all interfaces */ | ||
bool begin(const char* hostname); | ||
|
||
/* Application should call this whenever AP is configured/disabled */ | ||
void notify_ap_change(); | ||
|
||
private: | ||
String _hostname; | ||
UdpContext *_conn; | ||
WiFiEventHandler _sta_got_ip_handler; | ||
WiFiEventHandler _sta_disconnected_handler; | ||
|
||
bool _restart(); | ||
void _process_packet(); | ||
}; | ||
|
||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LLMNR) | ||
extern LLMNRResponder LLMNR; | ||
#endif | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a minor thing, but in some other places you put braces around one-line if bodies. Suggest doing the same thing everywhere.