66 */
77#include "captive_dns.h"
88
9+ #include <string.h>
10+
911#include "esp_err.h"
1012#include "esp_log.h"
11-
12- #include "lwip/udp.h"
13- #include "lwip/ip_addr.h"
1413#include "lwip/inet.h"
14+ #include "lwip/ip_addr.h"
1515#include "lwip/pbuf.h"
16- #include <string.h>
16+ #include "lwip/udp.h"
1717
18- static const char * TAG = "captive_dns" ;
18+ static const char * TAG = "captive_dns" ;
1919
20- static struct udp_pcb * s_pcb ; // UDP handle
21- static ip_addr_t s_target_ip ; // IP we point every host to
20+ static struct udp_pcb * s_pcb ; // UDP handle
21+ static ip_addr_t s_target_ip ; // IP we point every host to
2222
2323/* ---------- very small DNS helper ---------- */
2424
25- static void dns_recv_cb (void * arg , struct udp_pcb * pcb ,
26- struct pbuf * p , const ip_addr_t * addr , u16_t port )
27- {
28- if (! p ) return ;
25+ static void dns_recv_cb (void * arg , struct udp_pcb * pcb , struct pbuf * p , const ip_addr_t * addr ,
26+ u16_t port ) {
27+ if (! p )
28+ return ;
2929
3030 /* copy the whole packet into RAM (max 512 B for DNS over UDP) */
3131 uint8_t buf [512 ];
32- if (p -> tot_len > sizeof (buf )) { // ignore oversize queries
32+ if (p -> tot_len > sizeof (buf )) { // ignore oversize queries
3333 pbuf_free (p );
3434 return ;
3535 }
3636 pbuf_copy_partial (p , buf , p -> tot_len , 0 );
3737 uint16_t qdcount = (buf [4 ] << 8 ) | buf [5 ];
38- if (qdcount == 0 ) { // malformed – drop
38+ if (qdcount == 0 ) { // malformed – drop
3939 pbuf_free (p );
4040 return ;
4141 }
4242
4343 /* build DNS response in-place --------------------------------- */
44- buf [2 ] = 0x81 ; // QR=1, Opcode=0, AA=1
45- buf [3 ] = 0x80 ; // RA=0, RCODE=0
46- buf [6 ] = buf [4 ]; // ANCOUNT = QDCOUNT
44+ buf [2 ] = 0x81 ; // QR=1, Opcode=0, AA=1
45+ buf [3 ] = 0x80 ; // RA=0, RCODE=0
46+ buf [6 ] = buf [4 ]; // ANCOUNT = QDCOUNT
4747 buf [7 ] = buf [5 ];
48- buf [8 ] = buf [9 ] = buf [10 ] = buf [11 ] = 0 ; // NSCOUNT, ARCOUNT = 0
48+ buf [8 ] = buf [9 ] = buf [10 ] = buf [11 ] = 0 ; // NSCOUNT, ARCOUNT = 0
4949
50- size_t idx = 12 ; // skip header
50+ size_t idx = 12 ; // skip header
5151 /* skip over all questions */
5252 for (uint16_t q = 0 ; q < qdcount ; ++ q ) {
53- while (idx < p -> tot_len && buf [idx ] != 0 ) // labels
53+ while (idx < p -> tot_len && buf [idx ] != 0 ) // labels
5454 idx += buf [idx ] + 1 ;
55- idx += 5 ; // zero + type + class
55+ idx += 5 ; // zero + type + class
5656 }
5757 /* append one answer per question */
5858 for (uint16_t q = 0 ; q < qdcount ; ++ q ) {
59- buf [idx ++ ] = 0xC0 ; buf [idx ++ ] = 0x0C ; // name pointer to 0x0c
60- buf [idx ++ ] = 0x00 ; buf [idx ++ ] = 0x01 ; // TYPE = A
61- buf [idx ++ ] = 0x00 ; buf [idx ++ ] = 0x01 ; // CLASS = IN
62- buf [idx ++ ] = 0x00 ; buf [idx ++ ] = 0x00 ; buf [idx ++ ] = 0x00 ; buf [idx ++ ] = 0x1E ; // TTL 30 s
63- buf [idx ++ ] = 0x00 ; buf [idx ++ ] = 0x04 ; // RDLEN = 4
59+ buf [idx ++ ] = 0xC0 ;
60+ buf [idx ++ ] = 0x0C ; // name pointer to 0x0c
61+ buf [idx ++ ] = 0x00 ;
62+ buf [idx ++ ] = 0x01 ; // TYPE = A
63+ buf [idx ++ ] = 0x00 ;
64+ buf [idx ++ ] = 0x01 ; // CLASS = IN
65+ buf [idx ++ ] = 0x00 ;
66+ buf [idx ++ ] = 0x00 ;
67+ buf [idx ++ ] = 0x00 ;
68+ buf [idx ++ ] = 0x1E ; // TTL 30 s
69+ buf [idx ++ ] = 0x00 ;
70+ buf [idx ++ ] = 0x04 ; // RDLEN = 4
6471 uint32_t ip_be = lwip_htonl ( /* RDATA must be big-endian */
65- ip4_addr_get_u32 ( /* get uint32_t */
66- ip_2_ip4 (& s_target_ip )));
67- memcpy (& buf [idx ], & ip_be , sizeof (ip_be )); // RDATA = target IPv4
72+ ip4_addr_get_u32 ( /* get uint32_t */
73+ ip_2_ip4 (& s_target_ip )));
74+ memcpy (& buf [idx ], & ip_be , sizeof (ip_be )); // RDATA = target IPv4
6875 idx += 4 ;
6976 }
7077
71- struct pbuf * resp = pbuf_alloc (PBUF_TRANSPORT , idx , PBUF_RAM );
78+ struct pbuf * resp = pbuf_alloc (PBUF_TRANSPORT , idx , PBUF_RAM );
7279 if (resp ) {
7380 pbuf_take (resp , buf , idx );
7481 udp_sendto (pcb , resp , addr , port );
@@ -79,8 +86,7 @@ static void dns_recv_cb(void *arg, struct udp_pcb *pcb,
7986
8087/* ---------- public API ------------------------------------------ */
8188
82- esp_err_t captive_dns_start (const char * ip_str )
83- {
89+ esp_err_t captive_dns_start (const char * ip_str ) {
8490 if (s_pcb ) {
8591 ESP_LOGW (TAG , "already running" );
8692 return ESP_OK ;
@@ -90,7 +96,8 @@ esp_err_t captive_dns_start(const char *ip_str)
9096 }
9197
9298 s_pcb = udp_new_ip_type (IPADDR_TYPE_V4 );
93- if (!s_pcb ) return ESP_ERR_NO_MEM ;
99+ if (!s_pcb )
100+ return ESP_ERR_NO_MEM ;
94101
95102 err_t err = udp_bind (s_pcb , IP_ADDR_ANY , 53 );
96103 if (err != ERR_OK ) {
@@ -105,8 +112,7 @@ esp_err_t captive_dns_start(const char *ip_str)
105112 return ESP_OK ;
106113}
107114
108- void captive_dns_stop (void )
109- {
115+ void captive_dns_stop (void ) {
110116 if (s_pcb ) {
111117 udp_remove (s_pcb );
112118 s_pcb = NULL ;
0 commit comments