forked from aarond10/https_dns_proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns_server.c
More file actions
227 lines (200 loc) · 8.21 KB
/
dns_server.c
File metadata and controls
227 lines (200 loc) · 8.21 KB
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
#include <ares.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include "dns_server.h"
#include "logging.h"
// Creates and bind a listening UDP socket for incoming requests.
static int get_listen_sock(struct addrinfo *listen_addrinfo) {
int sock = socket(listen_addrinfo->ai_family, SOCK_DGRAM, 0);
if (sock < 0) {
FLOG("Error creating socket: %s (%d)", strerror(errno), errno);
}
uint16_t port = 0;
char ipstr[INET6_ADDRSTRLEN];
if (listen_addrinfo->ai_family == AF_INET) {
port = ntohs(((struct sockaddr_in*) listen_addrinfo->ai_addr)->sin_port);
inet_ntop(AF_INET, &((struct sockaddr_in *)listen_addrinfo->ai_addr)->sin_addr, ipstr, sizeof(ipstr));
} else if (listen_addrinfo->ai_family == AF_INET6) {
port = ntohs(((struct sockaddr_in6*) listen_addrinfo->ai_addr)->sin6_port);
inet_ntop(AF_INET6, &((struct sockaddr_in6 *)listen_addrinfo->ai_addr)->sin6_addr, ipstr, sizeof(ipstr));
} else {
FLOG("Unknown address family: %d", listen_addrinfo->ai_family);
}
int res = bind(sock, listen_addrinfo->ai_addr, listen_addrinfo->ai_addrlen);
if (res < 0) {
close(sock);
FLOG("Error binding on %s:%d UDP: %s (%d)", ipstr, port,
strerror(errno), errno);
}
ILOG("Listening on %s:%d UDP", ipstr, port);
return sock;
}
static void watcher_cb(struct ev_loop __attribute__((unused)) *loop,
ev_io *w, int __attribute__((unused)) revents) {
dns_server_t *d = (dns_server_t *)w->data;
char tmp_buf[DNS_REQUEST_BUFFER_SIZE];
struct sockaddr_storage tmp_raddr;
socklen_t tmp_addrlen = d->addrlen; // recvfrom can write to addrlen
ssize_t len = recvfrom(w->fd, tmp_buf, DNS_REQUEST_BUFFER_SIZE, MSG_TRUNC,
(struct sockaddr*)&tmp_raddr, &tmp_addrlen);
if (len < 0) {
ELOG("recvfrom failed: %s", strerror(errno));
return;
}
if (len > DNS_REQUEST_BUFFER_SIZE) {
WLOG("Unsupported request received, too large: %d. Limit is: %d",
len, DNS_REQUEST_BUFFER_SIZE);
return;
}
if (len < DNS_HEADER_LENGTH) {
WLOG("Malformed request received, too short: %d", len);
return;
}
char *dns_req = (char *)malloc((size_t)len); // To free buffer after https request is complete.
if (dns_req == NULL) {
FLOG("Out of mem");
}
memcpy(dns_req, tmp_buf, (size_t)len);
d->cb(d, 0, d->cb_data, (struct sockaddr*)&tmp_raddr, dns_req, (size_t)len);
}
void dns_server_init(dns_server_t *d, struct ev_loop *loop,
struct addrinfo *listen_addrinfo,
dns_req_received_cb cb, void *data) {
d->loop = loop;
d->sock = get_listen_sock(listen_addrinfo);
d->addrlen = listen_addrinfo->ai_addrlen;
d->cb = cb;
d->cb_data = data;
ev_io_init(&d->watcher, watcher_cb, d->sock, EV_READ);
d->watcher.data = d;
ev_io_start(d->loop, &d->watcher);
}
static uint16_t get_edns_udp_size(const char *dns_req, const size_t dns_req_len) {
ares_dns_record_t *dnsrec = NULL;
ares_status_t parse_status = ares_dns_parse((const unsigned char *)dns_req, dns_req_len, 0, &dnsrec);
if (parse_status != ARES_SUCCESS) {
WLOG("Failed to parse DNS request: %s", ares_strerror((int)parse_status));
return DNS_SIZE_LIMIT;
}
const uint16_t tx_id = ares_dns_record_get_id(dnsrec);
uint16_t udp_size = 0;
const size_t record_count = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ADDITIONAL);
for (size_t i = 0; i < record_count; ++i) {
const ares_dns_rr_t *rr = ares_dns_record_rr_get(dnsrec, ARES_SECTION_ADDITIONAL, i);
if (ares_dns_rr_get_type(rr) == ARES_REC_TYPE_OPT) {
udp_size = ares_dns_rr_get_u16(rr, ARES_RR_OPT_UDP_SIZE);
if (udp_size > 0) {
DLOG("%04hX: Found EDNS0 UDP buffer size: %u", tx_id, udp_size);
}
break;
}
}
ares_dns_record_destroy(dnsrec);
if (udp_size < DNS_SIZE_LIMIT) {
DLOG("%04hX: EDNS0 UDP buffer size %u overruled to %d", tx_id, udp_size, DNS_SIZE_LIMIT);
return DNS_SIZE_LIMIT; // RFC6891 4.3 "Values lower than 512 MUST be treated as equal to 512."
}
return udp_size;
}
static void truncate_dns_response(char *buf, size_t *buflen, const uint16_t size_limit) {
const size_t old_size = *buflen;
buf[2] |= 0x02; // anyway: set truncation flag
ares_dns_record_t *dnsrec = NULL;
ares_status_t status = ares_dns_parse((const unsigned char *)buf, *buflen, 0, &dnsrec);
if (status != ARES_SUCCESS) {
WLOG("Failed to parse DNS response: %s", ares_strerror((int)status));
return;
}
const uint16_t tx_id = ares_dns_record_get_id(dnsrec);
// NOTE: according to current c-ares implementation, removing first or last elements are the fastest!
// remove every additional and authority record
while (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ADDITIONAL) > 0) {
status = ares_dns_record_rr_del(dnsrec, ARES_SECTION_ADDITIONAL, 0);
if (status != ARES_SUCCESS) {
WLOG("%04hX: Could not remove additional record: %s", tx_id, ares_strerror((int)status));
}
}
while (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_AUTHORITY) > 0) {
status = ares_dns_record_rr_del(dnsrec, ARES_SECTION_AUTHORITY, 0);
if (status != ARES_SUCCESS) {
WLOG("%04hX: Could not remove authority record: %s", tx_id, ares_strerror((int)status));
}
}
// rough estimate to reach size limit
size_t answers = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
size_t answers_to_keep = ((size_limit - DNS_HEADER_LENGTH) * answers) / old_size;
answers_to_keep = answers_to_keep > 0 ? answers_to_keep : 1; // try to keep 1 answer
// remove answer records until fit size limit or running out of answers
unsigned char *new_resp = NULL;
size_t new_resp_len = 0;
for (uint8_t g = 0; g < UINT8_MAX; ++g) { // endless loop guard
status = ares_dns_write(dnsrec, &new_resp, &new_resp_len);
if (status != ARES_SUCCESS) {
WLOG("%04hX: Failed to create truncated DNS response: %s", tx_id, ares_strerror((int)status));
new_resp = NULL; // just to be sure
break;
}
if (new_resp_len < size_limit || answers == 0) {
break;
}
if (new_resp_len >= old_size) {
WLOG("%04hX: Truncated DNS response size larger or equal to original: %u >= %u",
tx_id, new_resp_len, old_size); // impossible?
}
ares_free_string(new_resp);
new_resp = NULL;
DLOG("%04hX: DNS response size truncated from %u to %u but to keep %u limit reducing answers from %u to %u",
tx_id, old_size, new_resp_len, size_limit, answers, answers_to_keep);
while (answers > answers_to_keep) {
status = ares_dns_record_rr_del(dnsrec, ARES_SECTION_ANSWER, answers - 1);
if (status != ARES_SUCCESS) {
WLOG("%04hX: Could not remove answer record: %s", tx_id, ares_strerror((int)status));
break;
}
--answers;
}
answers = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); // update to be sure!
answers_to_keep /= 2;
}
ares_dns_record_destroy(dnsrec);
if (new_resp != NULL) {
if (new_resp_len < old_size) {
memcpy(buf, new_resp, new_resp_len);
*buflen = new_resp_len;
buf[2] |= 0x02; // set truncation flag
ILOG("%04hX: DNS response size truncated from %u to %u to keep %u limit",
tx_id, old_size, new_resp_len, size_limit);
}
ares_free_string(new_resp);
new_resp = NULL;
}
}
void dns_server_respond(dns_server_t *d, struct sockaddr *raddr,
const char *dns_req, const size_t dns_req_len, char *dns_resp, size_t dns_resp_len) {
if (dns_resp_len < DNS_HEADER_LENGTH) {
WLOG("Malformed response received, invalid length: %u", dns_resp_len);
return;
}
if (dns_resp_len > DNS_SIZE_LIMIT) {
const uint16_t udp_size = get_edns_udp_size(dns_req, dns_req_len);
if (dns_resp_len > udp_size) {
truncate_dns_response(dns_resp, &dns_resp_len, udp_size);
} else {
uint16_t tx_id = ntohs(*((uint16_t*)dns_req));
DLOG("%04hX: DNS response size %u larger than %d but EDNS0 UDP buffer size %u allows it",
tx_id, dns_resp_len, DNS_SIZE_LIMIT, udp_size);
}
}
ssize_t len = sendto(d->sock, dns_resp, dns_resp_len, 0, raddr, d->addrlen);
if(len == -1) {
DLOG("sendto failed: %s", strerror(errno));
}
}
void dns_server_stop(dns_server_t *d) {
ev_io_stop(d->loop, &d->watcher);
}
void dns_server_cleanup(dns_server_t *d) {
close(d->sock);
}