diff --git a/user/cli.c b/user/cli.c index 32f4821..d0a0241 100644 --- a/user/cli.c +++ b/user/cli.c @@ -176,6 +176,10 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) { os_sprintf_flash(response, "time\r\nset [ntp_server|ntp_interval|ntp_timezone|ntp_time|ntp_weekday] \r\n"); to_console(response); #endif +#ifdef DNS_RESP + os_sprintf_flash(response, "set dns_name \r\n"); + to_console(response); +#endif #ifdef MQTT_CLIENT os_sprintf_flash(response, "set [mqtt_host|mqtt_port|mqtt_ssl|mqtt_user|mqtt_password|mqtt_id] \r\n"); to_console(response); @@ -213,6 +217,12 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) { // if static DNS, add it os_sprintf(response, config.dns_addr.addr ? "DNS: %d.%d.%d.%d\r\n" : "", IP2STR(&config.dns_addr)); to_console(response); +#ifdef DNS_RESP + if (strcmp(config.broker_dns_name, "none")!=0 && config.ap_on) { + os_sprintf(response, "DNS name: %s\r\n", config.broker_dns_name); + to_console(response); + } +#endif #ifdef MDNS if (config.mdns_mode) { os_sprintf(response, "mDNS: %s interface\r\n", config.mdns_mode==1 ? "STA": "SoftAP"); @@ -1026,6 +1036,14 @@ void ICACHE_FLASH_ATTR console_handle_command(struct espconn *pespconn) { goto command_handled; } #endif +#ifdef DNS_RESP + if (strcmp(tokens[1], "dns_name") == 0) { + os_strncpy(config.broker_dns_name, tokens[2], 32); + config.mqtt_host[31] = 0; + os_sprintf_flash(response, "DNS name set\r\n"); + goto command_handled; + } +#endif #ifdef MQTT_CLIENT if (strcmp(tokens[1], "mqtt_host") == 0) { os_strncpy(config.mqtt_host, tokens[2], 32); diff --git a/user/config_flash.c b/user/config_flash.c index 7c05a7f..191f22e 100644 --- a/user/config_flash.c +++ b/user/config_flash.c @@ -63,6 +63,9 @@ void ICACHE_FLASH_ATTR config_load_default(sysconfig_p config) { config->ntp_interval = 300000000; config->ntp_timezone = 0; #endif +#ifdef DNS_RESP + os_sprintf(config->broker_dns_name, "%s", "none"); +#endif #ifdef GPIO #ifdef GPIO_PWM config->pwm_period = 5000; diff --git a/user/config_flash.h b/user/config_flash.h index 202f439..a39e0c4 100644 --- a/user/config_flash.h +++ b/user/config_flash.h @@ -81,6 +81,9 @@ typedef struct uint32_t ntp_interval; // Sync interval in usec int16_t ntp_timezone; // Timezone (hour offset to GMT) #endif +#ifdef DNS_RESP + uint8_t broker_dns_name[32]; // DNS name of the MQTT broker in the SoftAP network, "none" if empty +#endif #ifdef GPIO #ifdef GPIO_PWM uint32_t pwm_period; // PWM period diff --git a/user/user_config.h b/user/user_config.h index 5ee896d..f302b01 100644 --- a/user/user_config.h +++ b/user/user_config.h @@ -88,6 +88,12 @@ // #define MDNS 1 +// +// Define this if you want to have access the DNS responder. +// Experimental feature - not yet tested +// +//#define DNS_RESP 1 + // // Define this to support the "scan" command for AP search // diff --git a/user/user_main.c b/user/user_main.c index a1753a6..4b172b0 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -26,6 +26,10 @@ uint64_t t_ntp_resync = 0; static struct mdns_info mdnsinfo; #endif +#ifdef DNS_RESP +#include "dns_responder.h" +#endif + #ifdef SCRIPTED #include "lang.h" #include "pub_list.h" @@ -752,6 +756,24 @@ void ICACHE_FLASH_ATTR mqtt_got_retained(retained_entry *topic) { } +#ifdef DNS_RESP +int ICACHE_FLASH_ATTR get_A_Record(uint8_t addr[4], const char domain_name[]) +{ + if (strcmp(config.broker_dns_name, domain_name) == 0) { + *(uint32_t*)addr = config.network_addr.addr; + return 0; + } else { + return -1; + } +} + +int ICACHE_FLASH_ATTR get_AAAA_Record(uint8_t addr[16], const char domain_name[]) +{ + return -1; +} +#endif + + void user_init() { struct ip_info info; @@ -843,6 +865,11 @@ void user_init() { wifi_set_opmode(STATIONAP_MODE); user_set_softap_wifi_config(); do_ip_config = true; +#ifdef DNS_RESP + if (strcmp(config.broker_dns_name, "none")!=0) { + dns_resp_init(DNS_MODE_AP); + } +#endif } else { wifi_set_opmode(STATION_MODE); }