-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDNSResolver.cpp
341 lines (260 loc) · 9.99 KB
/
DNSResolver.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
This file is part of CPPDNSResolver.
Copyright (C) 2020 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "DNSResolver.hpp"
using namespace YukiWorkshop;
void DNSResolver::__init() {
dns_init(nullptr, 0);
ctx_udns = dns_new(nullptr);
if (!ctx_udns)
throw std::system_error(ENOMEM, std::system_category(), strerror(ENOMEM));
if (dns_init(ctx_udns, 0) < 0)
THROW_ERRNO;
}
void DNSResolver::__fini() {
if (ctx_udns) {
fprintf(stderr,"__fini\n");
dns_free(ctx_udns);
ctx_udns = nullptr;
}
}
void DNSResolver::__open() {
if ((fd_udns = dns_open(ctx_udns)) < 0) {
THROW_ERRNO;
}
struct sockaddr sa;
socklen_t len = sizeof(sa);
if (getsockname(fd_udns, &sa, &len))
throw std::system_error(std::error_code(errno, std::system_category()), "getsockname");
asio_socket = std::make_unique<boost::asio::ip::udp::socket>(asio_iosvc,
sa.sa_family == AF_INET ?
boost::asio::ip::udp::v4()
: boost::asio::ip::udp::v6(),
dns_sock(ctx_udns));
}
void DNSResolver::io_wait_read() {
fprintf(stderr,"io_wait_read\n");
asio_socket->async_receive(boost::asio::null_buffers(),
boost::bind(&DNSResolver::iocb_read_avail, this));
}
void DNSResolver::iocb_read_avail() {
fprintf(stderr,"iocb_read_avail\n");
dns_ioevent(ctx_udns, time(nullptr));
if (requests_pending)
io_wait_read();
}
void DNSResolver::set_servers(const std::initializer_list<std::string> &__nameservers) {
dns_add_serv(ctx_udns, nullptr);
for (auto &it : __nameservers) {
if (dns_add_serv(ctx_udns, it.c_str()) < 0) {
THROW_ERRNO;
}
}
}
void DNSResolver::post_resolve() {
requests_pending++;
dns_timeouts(ctx_udns, -1, time(nullptr));
io_wait_read();
}
void DNSResolver::dnscb_a4(struct dns_ctx *ctx, struct dns_rr_a4 *result, void *data) {
auto *pd = (std::pair<DNSResolver *, A4Callback> *) data;
pd->first->requests_pending--;
std::vector<boost::asio::ip::address_v4> addrs;
if (result) {
for (uint32_t i = 0; i < result->dnsa4_nrr; i++) {
std::array<unsigned char, 4> buf;
memcpy(buf.data(), &result->dnsa4_addr[i].s_addr, 4);
addrs.emplace_back(buf);
}
std::string_view cname(result->dnsa4_cname);
std::string_view qname(result->dnsa4_qname);
pd->second(DNS_E_NOERROR, addrs, qname, cname, result->dnsa4_ttl);
free(result);
} else {
pd->second(dns_status(pd->first->ctx_udns), addrs, {}, {}, 0);
}
delete pd;
}
void DNSResolver::dnscb_a6(struct dns_ctx *ctx, struct dns_rr_a6 *result, void *data) {
auto *pd = (std::pair<DNSResolver *, A6Callback> *) data;
pd->first->requests_pending--;
std::vector<boost::asio::ip::address_v6> addrs;
if (result) {
for (uint32_t i = 0; i < result->dnsa6_nrr; i++) {
std::array<unsigned char, 16> buf;
memcpy(buf.data(), &result->dnsa6_addr[i], 16);
addrs.emplace_back(buf);
}
std::string_view cname(result->dnsa6_cname);
std::string_view qname(result->dnsa6_qname);
pd->second(DNS_E_NOERROR, addrs, qname, cname, result->dnsa6_ttl);
free(result);
} else {
pd->second(dns_status(pd->first->ctx_udns), addrs, {}, {}, 0);
}
delete pd;
}
void DNSResolver::dnscb_txt(struct dns_ctx *ctx, struct dns_rr_txt *result, void *data) {
auto *pd = (std::pair<DNSResolver *, TXTCallback> *) data;
pd->first->requests_pending--;
if (result) {
std::vector<std::string_view> addrs;
for (uint32_t i = 0; i < result->dnstxt_nrr; i++) {
addrs.emplace_back((const char *) result->dnstxt_txt[i].txt, result->dnstxt_txt[i].len);
}
std::string_view cname(result->dnstxt_cname);
std::string_view qname(result->dnstxt_qname);
pd->second(DNS_E_NOERROR, addrs, qname, cname, result->dnstxt_ttl);
free(result);
} else {
pd->second(dns_status(pd->first->ctx_udns), {}, {}, {}, 0);
}
delete pd;
}
void DNSResolver::dnscb_mx(struct dns_ctx *ctx, struct dns_rr_mx *result, void *data) {
auto *pd = (std::pair<DNSResolver *, MXCallback> *) data;
pd->first->requests_pending--;
if (result) {
std::vector<MXRecord> addrs;
for (uint32_t i = 0; i < result->dnsmx_nrr; i++) {
addrs.emplace_back(result->dnsmx_mx[i].priority, result->dnsmx_mx[i].name);
}
std::string_view cname(result->dnsmx_cname);
std::string_view qname(result->dnsmx_qname);
pd->second(DNS_E_NOERROR, addrs, qname, cname, result->dnsmx_ttl);
free(result);
} else {
pd->second(dns_status(pd->first->ctx_udns), {}, {}, {}, 0);
}
delete pd;
}
void DNSResolver::dnscb_srv(struct dns_ctx *ctx, struct dns_rr_srv *result, void *data) {
auto *pd = (std::pair<DNSResolver *, SRVCallback> *) data;
pd->first->requests_pending--;
if (result) {
std::vector<SRVRecord> addrs;
for (uint32_t i = 0; i < result->dnssrv_nrr; i++) {
auto &r = result->dnssrv_srv[i];
addrs.emplace_back(r.priority, r.weight, r.port, r.name);
}
std::string_view cname(result->dnssrv_cname);
std::string_view qname(result->dnssrv_qname);
pd->second(DNS_E_NOERROR, addrs, qname, cname, result->dnssrv_ttl);
free(result);
} else {
pd->second(dns_status(pd->first->ctx_udns), {}, {}, {}, 0);
}
delete pd;
}
void DNSResolver::dnscb_ptr(struct dns_ctx *ctx, struct dns_rr_ptr *result, void *data) {
auto *pd = (std::pair<DNSResolver *, PtrCallback> *) data;
pd->first->requests_pending--;
if (result) {
std::vector<std::string_view> addrs;
for (uint32_t i = 0; i < result->dnsptr_nrr; i++) {
addrs.emplace_back(result->dnsptr_ptr[i]);
}
std::string_view cname(result->dnsptr_cname);
std::string_view qname(result->dnsptr_qname);
pd->second(DNS_E_NOERROR, addrs, qname, cname, result->dnsptr_ttl);
free(result);
} else {
pd->second(dns_status(pd->first->ctx_udns), {}, {}, {}, 0);
}
delete pd;
}
DNSResolver::DNSResolver(boost::asio::io_service &__io_svc) : asio_iosvc(__io_svc) {
__init();
__open();
}
DNSResolver::DNSResolver(boost::asio::io_service &__io_svc, const std::initializer_list<std::string> &__nameservers)
: asio_iosvc(__io_svc) {
__init();
set_servers(__nameservers);
__open();
}
DNSResolver::~DNSResolver() {
__fini();
}
void DNSResolver::resolve_a4(const std::string &__hostname, const DNSResolver::A4Callback &__callback) {
auto *pd = new std::pair<DNSResolver *, A4Callback>(this, __callback);
fprintf(stderr,"resolve_a4: %s\n", __hostname.c_str());
dns_submit_a4(ctx_udns, __hostname.c_str(), 0, &DNSResolver::dnscb_a4, pd);
post_resolve();
}
void DNSResolver::resolve_a6(const std::string &__hostname, const DNSResolver::A6Callback &__callback) {
auto *pd = new std::pair<DNSResolver *, A6Callback>(this, __callback);
fprintf(stderr,"resolve_a6: %s\n", __hostname.c_str());
dns_submit_a6(ctx_udns, __hostname.c_str(), 0, &DNSResolver::dnscb_a6, pd);
post_resolve();
}
void DNSResolver::resolve_a4ptr(const boost::asio::ip::address_v4 &__addr, const DNSResolver::PtrCallback &__callback) {
auto *pd = new std::pair<DNSResolver *, PtrCallback>(this, __callback);
fprintf(stderr,"resolve_a4ptr: %s\n", __addr.to_string().c_str());
dns_submit_a4ptr(ctx_udns, (const struct in_addr *)__addr.to_bytes().data(), &DNSResolver::dnscb_ptr, pd);
post_resolve();
}
void DNSResolver::resolve_a6ptr(const boost::asio::ip::address_v6 &__addr, const DNSResolver::PtrCallback &__callback) {
auto *pd = new std::pair<DNSResolver *, PtrCallback>(this, __callback);
fprintf(stderr,"resolve_a6ptr: %s\n", __addr.to_string().c_str());
dns_submit_a6ptr(ctx_udns, (const struct in6_addr *)__addr.to_bytes().data(), &DNSResolver::dnscb_ptr, pd);
post_resolve();
}
void DNSResolver::resolve_txt(const std::string &__hostname, const DNSResolver::TXTCallback &__callback) {
auto *pd = new std::pair<DNSResolver *, TXTCallback>(this, __callback);
fprintf(stderr,"resolve_txt: %s\n", __hostname.c_str());
dns_submit_txt(ctx_udns, __hostname.c_str(), 0, 0, &DNSResolver::dnscb_txt, pd);
post_resolve();
}
void DNSResolver::resolve_mx(const std::string &__hostname, const DNSResolver::MXCallback &__callback) {
auto *pd = new std::pair<DNSResolver *, MXCallback>(this, __callback);
fprintf(stderr,"resolve_mx: %s\n", __hostname.c_str());
dns_submit_mx(ctx_udns, __hostname.c_str(), 0, &DNSResolver::dnscb_mx, pd);
post_resolve();
}
void DNSResolver::resolve_srv(const std::string &__hostname, const std::string &__srv, const std::string &__proto,
const DNSResolver::SRVCallback &__callback) {
auto *pd = new std::pair<DNSResolver *, SRVCallback>(this, __callback);
fprintf(stderr, "resolve_srv: %s\n", __hostname.c_str());
dns_submit_srv(ctx_udns, __hostname.c_str(), __srv.c_str(), __proto.c_str(), 0, &DNSResolver::dnscb_srv, pd);
post_resolve();
}
static const std::string errstr_none = "No error";
static const std::string errstr_tempfail = "Server timeout or down";
static const std::string errstr_protocol = "Malformed reply";
static const std::string errstr_nodata = "Domain exists but no data of requested type";
static const std::string errstr_nomem = "Out of memory";
static const std::string errstr_badquery = "Malformed query";
static const std::string errstr_nxdomain = "Domain does not exist";
static const std::string errstr_invalid = "Invalid error";
const std::string &DNSResolver::error_string(int __err) {
switch (__err) {
case DNS_E_NOERROR:
return errstr_none;
case DNS_E_TEMPFAIL:
return errstr_tempfail;
case DNS_E_PROTOCOL:
return errstr_protocol;
case DNS_E_NXDOMAIN:
return errstr_nxdomain;
case DNS_E_NODATA:
return errstr_nodata;
case DNS_E_NOMEM:
return errstr_nomem;
case DNS_E_BADQUERY:
return errstr_badquery;
default:
return errstr_invalid;
}
}