2017-03-11 07:15:44 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
ESP8266 Multicast DNS (port of CC3000 Multicast DNS library)
|
|
|
|
Version 1.1
|
|
|
|
Copyright (c) 2013 Tony DiCola (tony@tonydicola.com)
|
|
|
|
ESP8266 port (c) 2015 Ivan Grokhotkov (ivan@esp8266.com)
|
|
|
|
MDNS-SD Suport 2015 Hristo Gochkov (hristo@espressif.com)
|
|
|
|
Extended MDNS-SD support 2016 Lars Englund (lars.englund@gmail.com)
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Important RFC's for reference:
|
|
|
|
// - DNS request and response: http://www.ietf.org/rfc/rfc1035.txt
|
|
|
|
// - Multicast DNS: http://www.ietf.org/rfc/rfc6762.txt
|
|
|
|
// - MDNS-SD: https://tools.ietf.org/html/rfc6763
|
|
|
|
|
|
|
|
#ifndef LWIP_OPEN_SRC
|
|
|
|
#define LWIP_OPEN_SRC
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ESPmDNS.h"
|
2018-01-17 23:56:58 +01:00
|
|
|
#include "WiFi.h"
|
2017-03-11 07:15:44 +01:00
|
|
|
#include <functional>
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
|
2018-12-16 17:10:56 +01:00
|
|
|
// Add quotes around defined value
|
|
|
|
#ifdef __IN_ECLIPSE__
|
|
|
|
#define STR_EXPAND(tok) #tok
|
|
|
|
#define STR(tok) STR_EXPAND(tok)
|
|
|
|
#else
|
|
|
|
#define STR(tok) tok
|
|
|
|
#endif
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
static void _on_sys_event(system_event_t *event){
|
|
|
|
mdns_handle_system_event(NULL, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
MDNSResponder::MDNSResponder() :results(NULL) {}
|
2017-03-11 07:15:44 +01:00
|
|
|
MDNSResponder::~MDNSResponder() {
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
bool MDNSResponder::begin(const char* hostName){
|
|
|
|
if(mdns_init()){
|
2017-03-11 07:15:44 +01:00
|
|
|
log_e("Failed starting MDNS");
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-17 23:56:58 +01:00
|
|
|
WiFi.onEvent(_on_sys_event);
|
2017-03-11 07:15:44 +01:00
|
|
|
_hostname = hostName;
|
2018-03-04 20:50:38 +01:00
|
|
|
_hostname.toLowerCase();
|
2018-01-17 23:56:58 +01:00
|
|
|
if(mdns_hostname_set(hostName)) {
|
2017-03-11 07:15:44 +01:00
|
|
|
log_e("Failed setting MDNS hostname");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MDNSResponder::end() {
|
2018-01-17 23:56:58 +01:00
|
|
|
mdns_free();
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MDNSResponder::setInstanceName(String name) {
|
|
|
|
if (name.length() > 63) return;
|
2018-01-17 23:56:58 +01:00
|
|
|
if(mdns_instance_name_set(name.c_str())){
|
2017-03-11 07:15:44 +01:00
|
|
|
log_e("Failed setting MDNS instance");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
|
2017-03-11 07:15:44 +01:00
|
|
|
void MDNSResponder::enableArduino(uint16_t port, bool auth){
|
2018-01-17 23:56:58 +01:00
|
|
|
mdns_txt_item_t arduTxtData[4] = {
|
2018-12-16 17:10:56 +01:00
|
|
|
{(char*)"board" ,(char*)STR(ARDUINO_VARIANT)},
|
2018-01-17 23:56:58 +01:00
|
|
|
{(char*)"tcp_check" ,(char*)"no"},
|
|
|
|
{(char*)"ssh_upload" ,(char*)"no"},
|
|
|
|
{(char*)"auth_upload" ,(char*)"no"}
|
2017-03-11 07:15:44 +01:00
|
|
|
};
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
if(mdns_service_add(NULL, "_arduino", "_tcp", port, arduTxtData, 4)) {
|
2017-03-11 07:15:44 +01:00
|
|
|
log_e("Failed adding Arduino service");
|
2018-01-17 23:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(auth && mdns_service_txt_item_set("_arduino", "_tcp", "auth_upload", "yes")){
|
|
|
|
log_e("Failed setting Arduino txt item");
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MDNSResponder::disableArduino(){
|
2018-01-17 23:56:58 +01:00
|
|
|
if(mdns_service_remove("_arduino", "_tcp")) {
|
2017-03-11 07:15:44 +01:00
|
|
|
log_w("Failed removing Arduino service");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
void MDNSResponder::enableWorkstation(wifi_interface_t interface){
|
2017-03-11 07:15:44 +01:00
|
|
|
char winstance[21+_hostname.length()];
|
|
|
|
uint8_t mac[6];
|
2018-01-17 23:56:58 +01:00
|
|
|
esp_wifi_get_mac(interface, mac);
|
2017-03-11 07:15:44 +01:00
|
|
|
sprintf(winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]", _hostname.c_str(), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
if(mdns_service_add(NULL, "_workstation", "_tcp", 9, NULL, 0)) {
|
2017-03-11 07:15:44 +01:00
|
|
|
log_e("Failed adding Workstation service");
|
2018-01-17 23:56:58 +01:00
|
|
|
} else if(mdns_service_instance_name_set("_workstation", "_tcp", winstance)) {
|
2017-03-11 07:15:44 +01:00
|
|
|
log_e("Failed setting Workstation service instance name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MDNSResponder::disableWorkstation(){
|
2018-01-17 23:56:58 +01:00
|
|
|
if(mdns_service_remove("_workstation", "_tcp")) {
|
2017-03-11 07:15:44 +01:00
|
|
|
log_w("Failed removing Workstation service");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:44:24 +02:00
|
|
|
bool MDNSResponder::addService(char *name, char *proto, uint16_t port){
|
2018-03-04 20:50:38 +01:00
|
|
|
char _name[strlen(name)+2];
|
|
|
|
char _proto[strlen(proto)+2];
|
|
|
|
if (name[0] == '_') {
|
|
|
|
sprintf(_name, "%s", name);
|
|
|
|
} else {
|
|
|
|
sprintf(_name, "_%s", name);
|
|
|
|
}
|
|
|
|
if (proto[0] == '_') {
|
|
|
|
sprintf(_proto, "%s", proto);
|
|
|
|
} else {
|
|
|
|
sprintf(_proto, "_%s", proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mdns_service_add(NULL, _name, _proto, port, NULL, 0)) {
|
2017-03-11 07:15:44 +01:00
|
|
|
log_e("Failed adding service %s.%s.\n", name, proto);
|
2020-10-01 14:44:24 +02:00
|
|
|
return false;
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
2020-10-01 14:44:24 +02:00
|
|
|
return true;
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){
|
2018-03-04 20:50:38 +01:00
|
|
|
char _name[strlen(name)+2];
|
|
|
|
char _proto[strlen(proto)+2];
|
|
|
|
if (name[0] == '_') {
|
|
|
|
sprintf(_name, "%s", name);
|
|
|
|
} else {
|
|
|
|
sprintf(_name, "_%s", name);
|
|
|
|
}
|
|
|
|
if (proto[0] == '_') {
|
|
|
|
sprintf(_proto, "%s", proto);
|
|
|
|
} else {
|
|
|
|
sprintf(_proto, "_%s", proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mdns_service_txt_item_set(_name, _proto, key, value)) {
|
2017-03-11 07:15:44 +01:00
|
|
|
log_e("Failed setting service TXT");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
IPAddress MDNSResponder::queryHost(char *host, uint32_t timeout){
|
|
|
|
struct ip4_addr addr;
|
|
|
|
addr.addr = 0;
|
|
|
|
|
|
|
|
esp_err_t err = mdns_query_a(host, timeout, &addr);
|
|
|
|
if(err){
|
|
|
|
if(err == ESP_ERR_NOT_FOUND){
|
|
|
|
log_w("Host was not found!");
|
|
|
|
return IPAddress();
|
|
|
|
}
|
|
|
|
log_e("Query Failed");
|
|
|
|
return IPAddress();
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
2018-01-17 23:56:58 +01:00
|
|
|
return IPAddress(addr.addr);
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 23:56:58 +01:00
|
|
|
|
|
|
|
int MDNSResponder::queryService(char *service, char *proto) {
|
|
|
|
if(!service || !service[0] || !proto || !proto[0]){
|
|
|
|
log_e("Bad Parameters");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(results){
|
|
|
|
mdns_query_results_free(results);
|
|
|
|
results = NULL;
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
2018-01-17 23:56:58 +01:00
|
|
|
|
|
|
|
char srv[strlen(service)+2];
|
|
|
|
char prt[strlen(proto)+2];
|
2018-03-04 20:50:38 +01:00
|
|
|
if (service[0] == '_') {
|
|
|
|
sprintf(srv, "%s", service);
|
|
|
|
} else {
|
|
|
|
sprintf(srv, "_%s", service);
|
|
|
|
}
|
|
|
|
if (proto[0] == '_') {
|
|
|
|
sprintf(prt, "%s", proto);
|
|
|
|
} else {
|
|
|
|
sprintf(prt, "_%s", proto);
|
|
|
|
}
|
2018-01-17 23:56:58 +01:00
|
|
|
|
|
|
|
esp_err_t err = mdns_query_ptr(srv, prt, 3000, 20, &results);
|
|
|
|
if(err){
|
|
|
|
log_e("Query Failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!results){
|
|
|
|
log_w("No results found!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdns_result_t * r = results;
|
|
|
|
int i = 0;
|
|
|
|
while(r){
|
|
|
|
i++;
|
|
|
|
r = r->next;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdns_result_t * MDNSResponder::_getResult(int idx){
|
|
|
|
mdns_result_t * result = results;
|
|
|
|
int i = 0;
|
|
|
|
while(result){
|
|
|
|
if(i == idx){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
result = result->next;
|
|
|
|
}
|
|
|
|
return result;
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String MDNSResponder::hostname(int idx) {
|
2018-01-17 23:56:58 +01:00
|
|
|
mdns_result_t * result = _getResult(idx);
|
2017-03-11 07:15:44 +01:00
|
|
|
if(!result){
|
|
|
|
log_e("Result %d not found", idx);
|
|
|
|
return String();
|
|
|
|
}
|
2018-01-17 23:56:58 +01:00
|
|
|
return String(result->hostname);
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress MDNSResponder::IP(int idx) {
|
2018-01-17 23:56:58 +01:00
|
|
|
mdns_result_t * result = _getResult(idx);
|
2017-03-11 07:15:44 +01:00
|
|
|
if(!result){
|
|
|
|
log_e("Result %d not found", idx);
|
|
|
|
return IPAddress();
|
|
|
|
}
|
2018-01-17 23:56:58 +01:00
|
|
|
mdns_ip_addr_t * addr = result->addr;
|
|
|
|
while(addr){
|
|
|
|
if(addr->addr.type == MDNS_IP_PROTOCOL_V4){
|
|
|
|
return IPAddress(addr->addr.u_addr.ip4.addr);
|
|
|
|
}
|
|
|
|
addr = addr->next;
|
|
|
|
}
|
|
|
|
return IPAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
IPv6Address MDNSResponder::IPv6(int idx) {
|
|
|
|
mdns_result_t * result = _getResult(idx);
|
|
|
|
if(!result){
|
|
|
|
log_e("Result %d not found", idx);
|
|
|
|
return IPv6Address();
|
|
|
|
}
|
|
|
|
mdns_ip_addr_t * addr = result->addr;
|
|
|
|
while(addr){
|
|
|
|
if(addr->addr.type == MDNS_IP_PROTOCOL_V6){
|
|
|
|
return IPv6Address(addr->addr.u_addr.ip6.addr);
|
|
|
|
}
|
|
|
|
addr = addr->next;
|
|
|
|
}
|
|
|
|
return IPv6Address();
|
2017-03-11 07:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t MDNSResponder::port(int idx) {
|
2018-01-17 23:56:58 +01:00
|
|
|
mdns_result_t * result = _getResult(idx);
|
2017-03-11 07:15:44 +01:00
|
|
|
if(!result){
|
|
|
|
log_e("Result %d not found", idx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return result->port;
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:18:50 +02:00
|
|
|
int MDNSResponder::numTxt(int idx) {
|
|
|
|
mdns_result_t * result = _getResult(idx);
|
|
|
|
if(!result){
|
|
|
|
log_e("Result %d not found", idx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return result->txt_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MDNSResponder::hasTxt(int idx, const char * key) {
|
|
|
|
mdns_result_t * result = _getResult(idx);
|
|
|
|
if(!result){
|
|
|
|
log_e("Result %d not found", idx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
while(i < result->txt_count) {
|
|
|
|
if (strcmp(result->txt[i].key, key) == 0) return true;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String MDNSResponder::txt(int idx, const char * key) {
|
|
|
|
mdns_result_t * result = _getResult(idx);
|
|
|
|
if(!result){
|
|
|
|
log_e("Result %d not found", idx);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
while(i < result->txt_count) {
|
|
|
|
if (strcmp(result->txt[i].key, key) == 0) return result->txt[i].value;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
String MDNSResponder::txt(int idx, int txtIdx) {
|
|
|
|
mdns_result_t * result = _getResult(idx);
|
|
|
|
if(!result){
|
|
|
|
log_e("Result %d not found", idx);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if (txtIdx >= result->txt_count) return "";
|
|
|
|
return result->txt[txtIdx].value;
|
|
|
|
}
|
|
|
|
|
2017-03-11 07:15:44 +01:00
|
|
|
MDNSResponder MDNS;
|