-
Notifications
You must be signed in to change notification settings - Fork 4
/
dns_server.c
155 lines (123 loc) · 4.94 KB
/
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
/*
MIT License
Copyright (c) 2017 Olof Astrand (Ebiroll)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <lwip/sockets.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <esp_system.h>
#include <esp_wifi.h>
#include <esp_event_loop.h>
#include <esp_log.h>
#include <esp_err.h>
#include <nvs_flash.h>
#include <lwip/err.h>
#include <lwip/sockets.h>
#include <lwip/sys.h>
#include <lwip/netdb.h>
#include <lwip/dns.h>
static const char TAG[] = "DNSSRV";
void receive_thread(void *pvParameters) {
int socket_fd;
struct sockaddr_in sa, ra;
socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_fd < 0){
ESP_LOGE(TAG, "Failed to create socket");
exit(0);
}
memset(&sa, 0, sizeof(struct sockaddr_in));
tcpip_adapter_ip_info_t ip;
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
ra.sin_family = AF_INET;
ra.sin_addr.s_addr = ip.ip.addr;
ra.sin_port = htons(53);
if (bind(socket_fd, (struct sockaddr *)&ra, sizeof(struct sockaddr_in)) == -1) {
ESP_LOGE(TAG, "Failed to bind to 53/udp");
close(socket_fd);
exit(1);
}
struct sockaddr_in client;
socklen_t client_len;
client_len = sizeof(client);
int length;
char data[80];
char response[100];
char ipAddress[INET_ADDRSTRLEN];
int idx;
int err;
ESP_LOGI(TAG, "DNS Server listening on 53/udp");
while (1) {
length = recvfrom(socket_fd, data, sizeof(data), 0, (struct sockaddr *)&client, &client_len);
if (length > 0) {
data[length] = '\0';
inet_ntop(AF_INET, &(client.sin_addr), ipAddress, INET_ADDRSTRLEN);
ESP_LOGI(TAG, "Replying to DNS request (len=%d) from %s", length, ipAddress);
// Prepare our response
response[0] = data[0];
response[1] = data[1];
response[2] = 0b10000100 | (0b00000001 & data[2]); //response, authorative answer, not truncated, copy the recursion bit
response[3] = 0b00000000; //no recursion available, no errors
response[4] = data[4];
response[5] = data[5]; //Question count
response[6] = data[4];
response[7] = data[5]; //answer count
response[8] = 0x00;
response[9] = 0x00; //NS record count
response[10]= 0x00;
response[11]= 0x00; //Resource record count
memcpy(response+12, data+12, length-12); //Copy the rest of the query section
idx = length;
// Prune off the OPT
// FIXME: We should parse the packet better than this!
if ((response[idx-11] == 0x00) && (response[idx-10] == 0x00) && (response[idx-9] == 0x29))
idx -= 11;
//Set a pointer to the domain name in the question section
response[idx] = 0xC0;
response[idx+1] = 0x0C;
//Set the type to "Host Address"
response[idx+2] = 0x00;
response[idx+3] = 0x01;
//Set the response class to IN
response[idx+4] = 0x00;
response[idx+5] = 0x01;
//A 32 bit integer specifying TTL in seconds, 0 means no caching
response[idx+6] = 0x00;
response[idx+7] = 0x00;
response[idx+8] = 0x00;
response[idx+9] = 0x00;
//RDATA length
response[idx+10] = 0x00;
response[idx+11] = 0x04; //4 byte IP address
//The IP address
response[idx + 12] = 192;
response[idx + 13] = 168;
response[idx + 14] = 1;
response[idx + 15] = 1;
err = sendto(socket_fd, response, idx+16, 0, (struct sockaddr *)&client, client_len);
if (err < 0) {
ESP_LOGE(TAG, "sendto failed: %s", strerror(errno));
}
}
}
close(socket_fd);
}
void init_dns_server() {
xTaskCreate(&receive_thread, "receive_thread", 3048, NULL, 5, NULL);
}