91 socklen_t client_addr_len =
sizeof(client_addr);
103 if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
104 ESP_LOGE(TAG,
"recvfrom failed: %d", errno);
109 ESP_LOGVV(TAG,
"Received %d bytes from %s:%d",
len, inet_ntoa(client_addr.
sin_addr), ntohs(client_addr.
sin_port));
111 if (
len <
static_cast<ssize_t>(
sizeof(DNSHeader) + 1)) {
112 ESP_LOGV(TAG,
"Request too short: %d",
len);
117 DNSHeader *header = (DNSHeader *) this->
buffer_;
118 uint16_t
flags = ntohs(header->flags);
119 uint16_t
qd_count = ntohs(header->qd_count);
123 ESP_LOGV(TAG,
"Not a standard query: flags=0x%04X, qd_count=%d",
flags,
qd_count);
128 uint8_t *ptr = this->
buffer_ +
sizeof(DNSHeader);
131 while (ptr <
end && *ptr != 0) {
132 uint8_t label_len = *ptr;
133 if (label_len > 63) {
137 if (ptr + label_len + 1 >
end) {
140 ptr += label_len + 1;
144 if (ptr >=
end || *ptr != 0) {
150 if (ptr +
sizeof(DNSQuestion) >
end) {
155 DNSQuestion *question = (DNSQuestion *) ptr;
156 uint16_t qtype = ntohs(question->type);
157 uint16_t qclass = ntohs(question->dns_class);
160 if (qtype != DNS_QTYPE_A || qclass != DNS_QCLASS_IN) {
161 ESP_LOGV(TAG,
"Not an A query: type=0x%04X, class=0x%04X", qtype, qclass);
166 header->flags = htons(DNS_QR_FLAG | DNS_AA_FLAG);
167 header->an_count = htons(1);
170 size_t question_len = (ptr +
sizeof(DNSQuestion)) - this->
buffer_ -
sizeof(DNSHeader);
171 size_t answer_offset =
sizeof(DNSHeader) + question_len;
174 if (answer_offset +
sizeof(DNSAnswer) >
sizeof(this->
buffer_)) {
175 ESP_LOGW(TAG,
"Response too large");
179 DNSAnswer *answer = (DNSAnswer *) (this->
buffer_ + answer_offset);
182 answer->ptr_offset = htons(0xC000 |
sizeof(DNSHeader));
183 answer->type = htons(DNS_QTYPE_A);
184 answer->dns_class = htons(DNS_QCLASS_IN);
185 answer->ttl = htonl(DNS_ANSWER_TTL);
186 answer->addr_len = htons(4);
190 answer->ip_addr = addr.addr;
192 size_t response_len = answer_offset +
sizeof(DNSAnswer);
198 ESP_LOGV(TAG,
"Send failed: %d", errno);
200 ESP_LOGV(TAG,
"Sent %d bytes", sent);
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().