Select one of the symbols to view example projects that use it.
 
Outline
#include <sys/param.h>
#include <inttypes.h>
#include "esp_log.h"
#include "esp_system.h"
#include "esp_check.h"
#include "esp_netif.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "dns_server.h"
#define DNS_PORT
#define DNS_MAX_LEN
#define OPCODE_MASK
#define QR_FLAG
#define QD_TYPE_A
#define ANS_TTL_SEC
TAG
dns_header_t
dns_question_t
dns_answer_t
dns_server_handle
parse_dns_name(char *, char *, size_t)
parse_dns_request(char *, size_t, char *, size_t, dns_server_handle_t)
dns_server_task(void *)
start_dns_server(dns_server_config_t *)
stop_dns_server(dns_server_handle_t)
Files
loading...
SourceVuESP-IDF Framework and Examplescaptive_portal samplecomponents/dns_server/dns_server.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <sys/param.h> #include <inttypes.h> #include "esp_log.h" #include "esp_system.h" #include "esp_check.h" #include "esp_netif.h" #include "lwip/err.h" #include "lwip/sockets.h" #include "lwip/sys.h" #include "lwip/netdb.h" #include "dns_server.h"11 includes #define DNS_PORT (53) #define DNS_MAX_LEN (256) #define OPCODE_MASK (0x7800) #define QR_FLAG (1 << 7) #define QD_TYPE_A (0x0001) #define ANS_TTL_SEC (300)6 defines static const char *TAG = "example_dns_redirect_server"; // DNS Header Packet typedef struct __attribute__((__packed__)) { uint16_t id; uint16_t flags; uint16_t qd_count; uint16_t an_count; uint16_t ns_count; uint16_t ar_count; }{...} dns_header_t; // DNS Question Packet typedef struct { uint16_t type; uint16_t class; }{ ... } dns_question_t; // DNS Answer Packet typedef struct __attribute__((__packed__)) { uint16_t ptr_offset; uint16_t type; uint16_t class; uint32_t ttl; uint16_t addr_len; uint32_t ip_addr; }{...} dns_answer_t; // DNS server handle struct dns_server_handle { bool started; TaskHandle_t task; int num_of_entries; dns_entry_pair_t entry[]; }{ ... }; /* Parse the name from the packet from the DNS name format to a regular .-seperated name returns the pointer to the next part of the packet *//* ... */ static char *parse_dns_name(char *raw_name, char *parsed_name, size_t parsed_name_max_len) { char *label = raw_name; char *name_itr = parsed_name; int name_len = 0; do { int sub_name_len = *label; // (len + 1) since we are adding a '.' name_len += (sub_name_len + 1); if (name_len > parsed_name_max_len) { return NULL; }{...} // Copy the sub name that follows the the label memcpy(name_itr, label + 1, sub_name_len); name_itr[sub_name_len] = '.'; name_itr += (sub_name_len + 1); label += sub_name_len + 1; }{...} while (*label != 0); // Terminate the final string, replacing the last '.' parsed_name[name_len - 1] = '\0'; // Return pointer to first char after the name return label + 1; }{ ... } // Parses the DNS request and prepares a DNS response with the IP of the softAP static int parse_dns_request(char *req, size_t req_len, char *dns_reply, size_t dns_reply_max_len, dns_server_handle_t h) { if (req_len > dns_reply_max_len) { return -1; }{...} // Prepare the reply memset(dns_reply, 0, dns_reply_max_len); memcpy(dns_reply, req, req_len); // Endianess of NW packet different from chip dns_header_t *header = (dns_header_t *)dns_reply; ESP_LOGD(TAG, "DNS query with header id: 0x%X, flags: 0x%X, qd_count: %d", ntohs(header->id), ntohs(header->flags), ntohs(header->qd_count)); // Not a standard query if ((header->flags & OPCODE_MASK) != 0) { return 0; }{...} // Set question response flag header->flags |= QR_FLAG; uint16_t qd_count = ntohs(header->qd_count); header->an_count = htons(qd_count); int reply_len = qd_count * sizeof(dns_answer_t) + req_len; if (reply_len > dns_reply_max_len) { return -1; }{...} // Pointer to current answer and question char *cur_ans_ptr = dns_reply + req_len; char *cur_qd_ptr = dns_reply + sizeof(dns_header_t); char name[128]; // Respond to all questions based on configured rules for (int qd_i = 0; qd_i < qd_count; qd_i++) { char *name_end_ptr = parse_dns_name(cur_qd_ptr, name, sizeof(name)); if (name_end_ptr == NULL) { ESP_LOGE(TAG, "Failed to parse DNS question: %s", cur_qd_ptr); return -1; }{...} dns_question_t *question = (dns_question_t *)(name_end_ptr); uint16_t qd_type = ntohs(question->type); uint16_t qd_class = ntohs(question->class); ESP_LOGD(TAG, "Received type: %d | Class: %d | Question for: %s", qd_type, qd_class, name); if (qd_type == QD_TYPE_A) { esp_ip4_addr_t ip = { .addr = IPADDR_ANY }; // Check the configured rules to decide whether to answer this question or not for (int i = 0; i < h->num_of_entries; ++i) { // check if the name either corresponds to the entry, or if we should answer to all queries ("*") if (strcmp(h->entry[i].name, "*") == 0 || strcmp(h->entry[i].name, name) == 0) { if (h->entry[i].if_key) { esp_netif_ip_info_t ip_info; esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey(h->entry[i].if_key), &ip_info); ip.addr = ip_info.ip.addr; break; }{...} else if (h->entry->ip.addr != IPADDR_ANY) { ip.addr = h->entry[i].ip.addr; break; }{...} }{...} }{...} if (ip.addr == IPADDR_ANY) { // no rule applies, continue with another question continue; }{...} dns_answer_t *answer = (dns_answer_t *)cur_ans_ptr; answer->ptr_offset = htons(0xC000 | (cur_qd_ptr - dns_reply)); answer->type = htons(qd_type); answer->class = htons(qd_class); answer->ttl = htonl(ANS_TTL_SEC); ESP_LOGD(TAG, "Answer with PTR offset: 0x%" PRIX16 " and IP 0x%" PRIX32, ntohs(answer->ptr_offset), ip.addr); answer->addr_len = htons(sizeof(ip.addr)); answer->ip_addr = ip.addr; }{...} }{...} return reply_len; }{ ... } /* Sets up a socket and listen for DNS queries, replies to all type A queries with the IP of the softAP *//* ... */ void dns_server_task(void *pvParameters) { char rx_buffer[128]; char addr_str[128]; int addr_family; int ip_protocol; dns_server_handle_t handle = pvParameters; while (handle->started) { struct sockaddr_in dest_addr; dest_addr.sin_addr.s_addr = htonl(INADDR_ANY); dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(DNS_PORT); addr_family = AF_INET; ip_protocol = IPPROTO_IP; inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1); int sock = socket(addr_family, SOCK_DGRAM, ip_protocol); if (sock < 0) { ESP_LOGE(TAG, "Unable to create socket: errno %d", errno); break; }{...} ESP_LOGI(TAG, "Socket created"); int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); if (err < 0) { ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno); }{...} ESP_LOGI(TAG, "Socket bound, port %d", DNS_PORT); while (handle->started) { ESP_LOGI(TAG, "Waiting for data"); struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6 socklen_t socklen = sizeof(source_addr); int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen); // Error occurred during receiving if (len < 0) { ESP_LOGE(TAG, "recvfrom failed: errno %d", errno); close(sock); break; }{...} // Data received else { // Get the sender's ip address as string if (source_addr.sin6_family == PF_INET) { inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1); }{...} else if (source_addr.sin6_family == PF_INET6) { inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1); }{...} // Null-terminate whatever we received and treat like a string... rx_buffer[len] = 0; char reply[DNS_MAX_LEN]; int reply_len = parse_dns_request(rx_buffer, len, reply, DNS_MAX_LEN, handle); ESP_LOGI(TAG, "Received %d bytes from %s | DNS reply with len: %d", len, addr_str, reply_len); if (reply_len <= 0) { ESP_LOGE(TAG, "Failed to prepare a DNS reply"); }{...} else { int err = sendto(sock, reply, reply_len, 0, (struct sockaddr *)&source_addr, sizeof(source_addr)); if (err < 0) { ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno); break; }{...} }{...} }{...} }{...} if (sock != -1) { ESP_LOGE(TAG, "Shutting down socket"); shutdown(sock, 0); close(sock); }{...} }{...} vTaskDelete(NULL); }{ ... } dns_server_handle_t start_dns_server(dns_server_config_t *config) { dns_server_handle_t handle = calloc(1, sizeof(struct dns_server_handle) + config->num_of_entries * sizeof(dns_entry_pair_t)); ESP_RETURN_ON_FALSE(handle, NULL, TAG, "Failed to allocate dns server handle"); handle->started = true; handle->num_of_entries = config->num_of_entries; memcpy(handle->entry, config->item, config->num_of_entries * sizeof(dns_entry_pair_t)); xTaskCreate(dns_server_task, "dns_server", 4096, handle, 5, &handle->task); return handle; }{ ... } void stop_dns_server(dns_server_handle_t handle) { if (handle) { handle->started = false; vTaskDelete(handle->task); free(handle); }{...} }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.