2#if defined(USE_ESP32) && (defined(USE_CAPTIVE_PORTAL) || defined(USE_WEBSERVER_CAPTIVE))
7#include <lwip/sockets.h>
15static constexpr uint16_t DNS_PORT = 53;
16static constexpr uint16_t DNS_QR_FLAG = 1 << 15;
17static constexpr uint16_t DNS_AA_FLAG = 1 << 10;
18static constexpr uint16_t DNS_OPCODE_MASK = 0x7800;
19static constexpr uint16_t DNS_QTYPE_A = 0x0001;
20static constexpr uint16_t DNS_QCLASS_IN = 0x0001;
22static constexpr uint16_t DNS_ANSWER_TTL = 60;
52#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
53 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
54 ESP_LOGV(TAG,
"Starting DNS server on %s", ip.
str_to(ip_buf));
60 ESP_LOGE(TAG,
"Socket create failed");
74 ESP_LOGE(TAG,
"Bind failed: %d", errno);
78 ESP_LOGV(TAG,
"Bound to port %d", DNS_PORT);
84 ESP_LOGV(TAG,
"Stopped");
93 socklen_t client_addr_len =
sizeof(client_addr);
105 const int err = errno;
106 if (err != EAGAIN && err != EWOULDBLOCK && err != EINTR) {
107 ESP_LOGE(TAG,
"recvfrom failed: %d", err);
112 ESP_LOGVV(TAG,
"Received %d bytes from %s:%d",
len, inet_ntoa(client_addr.
sin_addr), ntohs(client_addr.
sin_port));
114 if (
len <
static_cast<ssize_t>(
sizeof(DNSHeader) + 1)) {
115 ESP_LOGV(TAG,
"Request too short: %d",
len);
120 DNSHeader *header = (DNSHeader *) this->
buffer_;
121 uint16_t
flags = ntohs(header->flags);
122 uint16_t
qd_count = ntohs(header->qd_count);
126 ESP_LOGV(TAG,
"Not a standard query: flags=0x%04X, qd_count=%d",
flags,
qd_count);
131 uint8_t *ptr = this->
buffer_ +
sizeof(DNSHeader);
134 while (ptr <
end && *ptr != 0) {
135 uint8_t label_len = *ptr;
136 if (label_len > 63) {
140 if (ptr + label_len + 1 >
end) {
143 ptr += label_len + 1;
147 if (ptr >=
end || *ptr != 0) {
153 if (ptr +
sizeof(DNSQuestion) >
end) {
158 DNSQuestion *question = (DNSQuestion *) ptr;
159 uint16_t qtype = ntohs(question->type);
160 uint16_t qclass = ntohs(question->dns_class);
163 if (qtype != DNS_QTYPE_A || qclass != DNS_QCLASS_IN) {
164 ESP_LOGV(TAG,
"Not an A query: type=0x%04X, class=0x%04X", qtype, qclass);
169 header->flags = htons(DNS_QR_FLAG | DNS_AA_FLAG);
170 header->an_count = htons(1);
173 size_t question_len = (ptr +
sizeof(DNSQuestion)) - this->
buffer_ -
sizeof(DNSHeader);
174 size_t answer_offset =
sizeof(DNSHeader) + question_len;
177 if (answer_offset +
sizeof(DNSAnswer) >
sizeof(this->
buffer_)) {
178 ESP_LOGW(TAG,
"Response too large");
182 DNSAnswer *answer = (DNSAnswer *) (this->
buffer_ + answer_offset);
185 answer->ptr_offset = htons(0xC000 |
sizeof(DNSHeader));
186 answer->type = htons(DNS_QTYPE_A);
187 answer->dns_class = htons(DNS_QCLASS_IN);
188 answer->ttl = htonl(DNS_ANSWER_TTL);
189 answer->addr_len = htons(4);
193 answer->ip_addr = addr.addr;
195 size_t response_len = answer_offset +
sizeof(DNSAnswer);
201 ESP_LOGV(TAG,
"Send failed: %d", errno);
203 ESP_LOGV(TAG,
"Sent %d bytes", sent);
bool ready() const
Check if the socket has buffered data ready to read.
int bind(const struct sockaddr *addr, socklen_t addrlen)
ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
int setsockopt(int level, int optname, const void *optval, socklen_t optlen)
network::IPAddress server_ip_
uint8_t buffer_[DNS_BUFFER_SIZE]
socket::ListenSocket * socket_
void process_next_request()
bool start(const network::IPAddress &ip)
Returns false when the socket could not be created or bound.
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
struct in_addr ip4_addr_t
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
ESPHOME_LOG_TAG(TAG, "web_server_base.dns")
char * str_to(char *buf) const