2018-12-12 02:52:51 +00:00
|
|
|
#include <dnsd.hpp>
|
2019-02-02 23:12:42 +00:00
|
|
|
|
|
|
|
#include <util/buffer.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.hpp>
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
extern dns_tracker dns_udp_tracker;
|
2018-07-23 23:56:26 +00:00
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define wmin(x, y) (((x) < (y)) ? (x) : (y))
|
|
|
|
#define MIN wmin
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 12:12:08 +00:00
|
|
|
constexpr size_t BUFFER_SIZE = 1500;
|
|
|
|
|
2018-07-16 12:48:04 +00:00
|
|
|
ssize_t
|
2018-07-21 13:24:47 +00:00
|
|
|
raw_sendto_dns_hook_func(void *sock, const struct sockaddr *from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer buf)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
int *fd = (int *)sock;
|
|
|
|
// how do we get to these??
|
2018-07-21 13:24:47 +00:00
|
|
|
socklen_t addrLen = sizeof(struct sockaddr_in);
|
2019-02-02 23:12:42 +00:00
|
|
|
return sendto(*fd, (const char *)buf.underlying.base, buf.underlying.sz, 0,
|
|
|
|
from, addrLen);
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2018-07-21 13:24:47 +00:00
|
|
|
llarp_sendto_dns_hook_func(void *sock, const struct sockaddr *from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer buf)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
struct llarp_udp_io *udp = (struct llarp_udp_io *)sock;
|
2018-07-21 13:24:47 +00:00
|
|
|
if(!udp)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-18 21:06:18 +00:00
|
|
|
llarp::LogWarn("couldnt cast to udp");
|
2018-07-16 12:48:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp::LogInfo("hook sending ", udp, " bytes: ", length);
|
2018-07-18 21:06:18 +00:00
|
|
|
// udp seems ok
|
|
|
|
// this call isn't calling the function...
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp::ev_io * evio = static_cast< llarp::ev_io * >(udp->impl);
|
|
|
|
// printf("ev_io[%x]\n", evio);
|
2019-02-02 23:12:42 +00:00
|
|
|
return llarp_ev_udp_sendto(udp, from, buf.underlying);
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
void
|
2018-12-01 14:35:11 +00:00
|
|
|
write404_dnss_response(const dnsd_question_request *request)
|
2018-08-01 12:51:28 +00:00
|
|
|
{
|
2018-11-09 12:12:08 +00:00
|
|
|
char buf[BUFFER_SIZE] = {0};
|
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
char *write_buffer = buf;
|
|
|
|
char *bufferBegin = buf;
|
|
|
|
// build header
|
2018-12-01 14:35:11 +00:00
|
|
|
put16bits(write_buffer, request->hdr.id);
|
|
|
|
// not found flag set
|
2018-12-02 18:07:07 +00:00
|
|
|
put16bits(write_buffer, (1 << 15) | request->hdr.fields() | 3);
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
put16bits(write_buffer, 1); // QD (number of questions)
|
2018-12-03 13:09:35 +00:00
|
|
|
put16bits(write_buffer, 1); // AN (number of answers)
|
2018-08-01 12:51:28 +00:00
|
|
|
put16bits(write_buffer, 0); // NS (number of auth RRs)
|
|
|
|
put16bits(write_buffer, 0); // AR (number of Additional RRs)
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
// code question
|
|
|
|
code_domain(write_buffer, request->question.name);
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-12-03 13:09:35 +00:00
|
|
|
// code answer
|
|
|
|
code_domain(write_buffer, request->question.name); // com, type=6, ttl=0
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
put32bits(write_buffer, 1); // ttl
|
|
|
|
put16bits(write_buffer, 1); // rdLength
|
|
|
|
*write_buffer++ = 0; // write a null byte
|
|
|
|
|
2018-10-13 01:55:06 +00:00
|
|
|
uint32_t out_bytes = write_buffer - bufferBegin;
|
2018-08-02 12:51:49 +00:00
|
|
|
llarp::LogDebug("Sending 404, ", out_bytes, " bytes");
|
2018-08-01 12:51:28 +00:00
|
|
|
// struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
2018-12-02 18:07:07 +00:00
|
|
|
request->sendto_hook(request->user, request->from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer(llarp_buffer_t(buf, out_bytes)));
|
2018-08-01 12:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-02 18:07:07 +00:00
|
|
|
writecname_dnss_response(std::string cname,
|
|
|
|
const dnsd_question_request *request)
|
2018-08-01 12:51:28 +00:00
|
|
|
{
|
2018-11-09 12:12:08 +00:00
|
|
|
char buf[BUFFER_SIZE] = {0};
|
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
char *write_buffer = buf;
|
|
|
|
char *bufferBegin = buf;
|
|
|
|
// build header
|
2018-12-01 14:35:11 +00:00
|
|
|
put16bits(write_buffer, request->hdr.id);
|
2018-08-08 12:40:54 +00:00
|
|
|
// fields |= 1UL << 7; // RA recursion available
|
|
|
|
// fields |= 1UL << 8; // RD recursion desired
|
|
|
|
// fields |= 1UL << 9; // 9 is truncate, forces TCP
|
2018-12-01 14:35:11 +00:00
|
|
|
put16bits(write_buffer, request->hdr.fields() | (1 << 15));
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
put16bits(write_buffer, 1); // QD (number of questions)
|
|
|
|
put16bits(write_buffer, 1); // AN (number of answers)
|
|
|
|
put16bits(write_buffer, 1); // NS (number of auth RRs)
|
|
|
|
put16bits(write_buffer, 1); // AR (number of Additional RRs)
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
// code question
|
|
|
|
code_domain(write_buffer, request->question.name);
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
// code answer
|
|
|
|
code_domain(write_buffer, request->question.name); // com, type=6, ttl=0
|
2018-08-08 12:40:54 +00:00
|
|
|
put16bits(write_buffer, 5); // cname
|
2018-08-01 12:51:28 +00:00
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
put32bits(write_buffer, 1); // ttl
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
put16bits(write_buffer, cname.length() + 2); // rdLength
|
2018-11-18 23:08:02 +00:00
|
|
|
code_domain(write_buffer, cname); //
|
2018-08-01 12:51:28 +00:00
|
|
|
// location of cname
|
|
|
|
//*write_buffer++ = ip[0];
|
|
|
|
//*write_buffer++ = ip[1];
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
// write auth RR
|
|
|
|
code_domain(write_buffer, cname); // com, type=6, ttl=0
|
2018-08-08 12:40:54 +00:00
|
|
|
put16bits(write_buffer, 2); // NS
|
2018-08-01 12:51:28 +00:00
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
put32bits(write_buffer, 1); // ttl
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
std::string local("ns1.loki");
|
|
|
|
put16bits(write_buffer, local.length() + 2); // rdLength
|
2018-08-08 12:40:54 +00:00
|
|
|
code_domain(write_buffer, local); // com, type=6, ttl=0
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 12:51:28 +00:00
|
|
|
// write addl RR
|
|
|
|
code_domain(write_buffer, local); // com, type=6, ttl=0
|
2018-08-08 12:40:54 +00:00
|
|
|
put16bits(write_buffer, 1); // A
|
2018-08-01 12:51:28 +00:00
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
put32bits(write_buffer, 1); // ttl
|
|
|
|
|
|
|
|
put16bits(write_buffer, 4); // rdLength
|
2018-08-02 12:51:49 +00:00
|
|
|
*write_buffer++ = 127;
|
|
|
|
*write_buffer++ = 0;
|
2018-08-01 12:51:28 +00:00
|
|
|
*write_buffer++ = 0;
|
2018-08-02 12:51:49 +00:00
|
|
|
*write_buffer++ = 1;
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-10-13 01:55:06 +00:00
|
|
|
uint32_t out_bytes = write_buffer - bufferBegin;
|
2018-08-02 12:51:49 +00:00
|
|
|
llarp::LogDebug("Sending cname, ", out_bytes, " bytes");
|
2018-08-01 12:51:28 +00:00
|
|
|
// struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
2018-12-02 18:07:07 +00:00
|
|
|
request->sendto_hook(request->user, request->from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer(llarp_buffer_t(buf, out_bytes)));
|
2018-08-01 12:51:28 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 16:55:43 +00:00
|
|
|
void
|
2018-12-02 18:07:07 +00:00
|
|
|
writesend_dnss_revresponse(std::string reverse,
|
|
|
|
const dnsd_question_request *request)
|
2018-09-23 16:55:43 +00:00
|
|
|
{
|
2018-11-09 12:12:08 +00:00
|
|
|
char buf[BUFFER_SIZE] = {0};
|
|
|
|
|
|
|
|
char *write_buffer = buf;
|
|
|
|
char *bufferBegin = buf;
|
2018-09-23 16:55:43 +00:00
|
|
|
// build header
|
2018-12-01 14:35:11 +00:00
|
|
|
put16bits(write_buffer, request->hdr.id);
|
|
|
|
// response
|
2018-12-02 18:07:07 +00:00
|
|
|
put16bits(write_buffer, request->hdr.fields() | (1 << 15) | (1 << 10));
|
2018-09-23 16:55:43 +00:00
|
|
|
|
|
|
|
put16bits(write_buffer, 1); // QD (number of questions)
|
|
|
|
put16bits(write_buffer, 1); // AN (number of answers)
|
|
|
|
put16bits(write_buffer, 0); // NS (number of auth RRs)
|
|
|
|
put16bits(write_buffer, 0); // AR (number of Additional RRs)
|
|
|
|
|
|
|
|
// code question
|
|
|
|
code_domain(write_buffer, request->question.name);
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
|
|
|
|
// code answer
|
|
|
|
code_domain(write_buffer, request->question.name); // com, type=6, ttl=0
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
2018-12-02 18:07:07 +00:00
|
|
|
put32bits(write_buffer, 1); // ttl
|
2018-12-01 14:35:11 +00:00
|
|
|
put16bits(write_buffer, reverse.size() + 2); // rdLength
|
2018-09-23 16:55:43 +00:00
|
|
|
code_domain(write_buffer, reverse);
|
|
|
|
|
2018-10-13 01:55:06 +00:00
|
|
|
uint32_t out_bytes = write_buffer - bufferBegin;
|
2018-09-23 16:55:43 +00:00
|
|
|
llarp::LogDebug("Sending reverse: ", reverse, " ", out_bytes, " bytes");
|
|
|
|
// struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
2018-12-02 18:07:07 +00:00
|
|
|
request->sendto_hook(request->user, request->from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer(llarp_buffer_t(buf, out_bytes)));
|
2018-09-23 16:55:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 03:34:28 +00:00
|
|
|
// FIXME: we need an DNS answer not a sockaddr
|
|
|
|
// otherwise ttl, type and class can't be relayed correctly
|
2018-07-16 12:48:04 +00:00
|
|
|
void
|
2018-11-18 23:48:50 +00:00
|
|
|
writesend_dnss_response(llarp::huint32_t *hostRes,
|
2018-12-01 14:35:11 +00:00
|
|
|
const dnsd_question_request *request)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-08-08 12:40:54 +00:00
|
|
|
// llarp::Addr test(*from);
|
|
|
|
// llarp::LogInfo("from ", test);
|
2018-07-21 13:24:47 +00:00
|
|
|
if(!hostRes)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-09-20 10:06:24 +00:00
|
|
|
llarp::LogWarn("Failed to resolve ", request->question.name);
|
2018-11-18 23:48:50 +00:00
|
|
|
write404_dnss_response(request);
|
2018-07-16 12:48:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-09 12:12:08 +00:00
|
|
|
char buf[BUFFER_SIZE] = {0};
|
|
|
|
|
2018-07-21 13:24:47 +00:00
|
|
|
char *write_buffer = buf;
|
|
|
|
char *bufferBegin = buf;
|
2018-07-16 12:48:04 +00:00
|
|
|
// build header
|
2018-12-01 14:35:11 +00:00
|
|
|
put16bits(write_buffer, request->hdr.id);
|
|
|
|
put16bits(write_buffer, request->hdr.fields() | (1 << 15));
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-21 13:24:47 +00:00
|
|
|
put16bits(write_buffer, 1); // QD (number of questions)
|
|
|
|
put16bits(write_buffer, 1); // AN (number of answers)
|
|
|
|
put16bits(write_buffer, 0); // NS (number of auth RRs)
|
|
|
|
put16bits(write_buffer, 0); // AR (number of Additional RRs)
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
// code question
|
2018-07-22 03:34:28 +00:00
|
|
|
code_domain(write_buffer, request->question.name);
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
// code answer
|
2018-09-23 16:55:43 +00:00
|
|
|
llarp::LogDebug("Sending question name: ", request->question.name);
|
2018-07-22 03:34:28 +00:00
|
|
|
code_domain(write_buffer, request->question.name); // com, type=6, ttl=0
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
put32bits(write_buffer, 1); // ttl
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
put16bits(write_buffer, 4); // rdLength
|
|
|
|
// put32bits(write_buffer, xhtonl(*hostRes).n);
|
|
|
|
put32bits(write_buffer, hostRes->h);
|
|
|
|
|
2018-07-16 12:48:04 +00:00
|
|
|
// has to be a string of 4 bytes
|
2018-11-03 13:19:18 +00:00
|
|
|
/*
|
2018-07-16 12:48:04 +00:00
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)hostRes;
|
2018-07-21 13:24:47 +00:00
|
|
|
unsigned char *ip = (unsigned char *)&sin->sin_addr.s_addr;
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-21 13:24:47 +00:00
|
|
|
put16bits(write_buffer, 4); // rdLength
|
2018-09-23 16:55:43 +00:00
|
|
|
llarp::LogDebug("Sending ip: ", std::to_string(ip[0]), '.',
|
2018-09-24 13:09:01 +00:00
|
|
|
std::to_string(ip[1]), '.', std::to_string(ip[2]), '.',
|
|
|
|
std::to_string(ip[3]));
|
2018-07-16 12:48:04 +00:00
|
|
|
*write_buffer++ = ip[0];
|
|
|
|
*write_buffer++ = ip[1];
|
|
|
|
*write_buffer++ = ip[2];
|
|
|
|
*write_buffer++ = ip[3];
|
2018-11-03 13:19:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
uint32_t out_bytes = write_buffer - bufferBegin;
|
2018-11-22 01:24:59 +00:00
|
|
|
llarp::LogDebug("Sending found, ", out_bytes, " bytes");
|
2018-11-03 13:19:18 +00:00
|
|
|
// struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
2018-12-02 18:07:07 +00:00
|
|
|
request->sendto_hook(request->user, request->from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer(llarp_buffer_t(buf, out_bytes)));
|
2018-11-03 13:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
writesend_dnss_mxresponse(uint16_t priority, std::string mx,
|
|
|
|
const struct sockaddr *from,
|
|
|
|
dnsd_question_request *request)
|
|
|
|
{
|
2018-11-09 12:12:08 +00:00
|
|
|
char buf[BUFFER_SIZE] = {0};
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
char *write_buffer = buf;
|
|
|
|
char *bufferBegin = buf;
|
|
|
|
// build header
|
2018-12-01 14:35:11 +00:00
|
|
|
put16bits(write_buffer, request->hdr.id);
|
|
|
|
put16bits(write_buffer, request->hdr.fields() | (1 << 15));
|
2018-11-03 13:19:18 +00:00
|
|
|
|
|
|
|
put16bits(write_buffer, 1); // QD (number of questions)
|
|
|
|
put16bits(write_buffer, 1); // AN (number of answers)
|
|
|
|
put16bits(write_buffer, 0); // NS (number of auth RRs)
|
|
|
|
put16bits(write_buffer, 0); // AR (number of Additional RRs)
|
|
|
|
|
|
|
|
// code question
|
|
|
|
code_domain(write_buffer, request->question.name);
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
|
|
|
|
// code answer
|
|
|
|
llarp::LogDebug("Sending question name: ", request->question.name);
|
|
|
|
code_domain(write_buffer, request->question.name); // com, type=6, ttl=0
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
put32bits(write_buffer, 1); // ttl
|
|
|
|
|
|
|
|
put16bits(write_buffer, 2 + (mx.size() + 2)); // rdLength
|
|
|
|
put16bits(write_buffer, priority); // priority
|
|
|
|
code_domain(write_buffer, mx); //
|
|
|
|
|
|
|
|
uint32_t out_bytes = write_buffer - bufferBegin;
|
|
|
|
llarp::LogDebug("Sending found, ", out_bytes, " bytes");
|
|
|
|
// struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
2019-02-02 23:12:42 +00:00
|
|
|
request->sendto_hook(request->user, from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer(llarp_buffer_t(buf, out_bytes)));
|
2018-11-03 13:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
writesend_dnss_txtresponse(std::string txt, const struct sockaddr *from,
|
|
|
|
dnsd_question_request *request)
|
|
|
|
{
|
2018-11-09 12:12:08 +00:00
|
|
|
char buf[BUFFER_SIZE] = {0};
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
char *write_buffer = buf;
|
|
|
|
char *bufferBegin = buf;
|
|
|
|
// build header
|
2018-12-01 14:35:11 +00:00
|
|
|
put16bits(write_buffer, request->hdr.id);
|
|
|
|
put16bits(write_buffer, request->hdr.fields() | (1 << 15));
|
2018-11-03 13:19:18 +00:00
|
|
|
|
|
|
|
put16bits(write_buffer, 1); // QD (number of questions)
|
|
|
|
put16bits(write_buffer, 1); // AN (number of answers)
|
|
|
|
put16bits(write_buffer, 0); // NS (number of auth RRs)
|
|
|
|
put16bits(write_buffer, 0); // AR (number of Additional RRs)
|
|
|
|
|
|
|
|
// code question
|
|
|
|
code_domain(write_buffer, request->question.name);
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
|
|
|
|
// code answer
|
|
|
|
llarp::LogDebug("Sending question name: ", request->question.name);
|
|
|
|
code_domain(write_buffer, request->question.name); // com, type=6, ttl=0
|
|
|
|
put16bits(write_buffer, request->question.type);
|
|
|
|
put16bits(write_buffer, request->question.qClass);
|
|
|
|
put32bits(write_buffer, 1); // ttl
|
|
|
|
|
|
|
|
put16bits(write_buffer, txt.size() + 2); // rdLength
|
|
|
|
*write_buffer = txt.size(); // write size
|
|
|
|
write_buffer++;
|
|
|
|
code_domain(write_buffer, txt); //
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-10-13 01:55:06 +00:00
|
|
|
uint32_t out_bytes = write_buffer - bufferBegin;
|
2018-08-02 12:51:49 +00:00
|
|
|
llarp::LogDebug("Sending found, ", out_bytes, " bytes");
|
2018-07-21 13:24:47 +00:00
|
|
|
// struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
2019-02-02 23:12:42 +00:00
|
|
|
request->sendto_hook(request->user, from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer(llarp_buffer_t(buf, out_bytes)));
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 13:24:47 +00:00
|
|
|
void
|
2018-07-22 03:34:28 +00:00
|
|
|
handle_dnsc_result(dnsc_answer_request *client_request)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-25 00:35:11 +00:00
|
|
|
dnsd_question_request *server_request =
|
|
|
|
(dnsd_question_request *)client_request->user;
|
2018-08-08 12:40:54 +00:00
|
|
|
if(!server_request)
|
2018-08-01 12:51:28 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Couldn't map client requser user to a server request");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-18 23:08:02 +00:00
|
|
|
|
2018-12-01 14:35:11 +00:00
|
|
|
client_request->packet.header.id = server_request->hdr.id; // stomp ID
|
2018-12-02 18:07:07 +00:00
|
|
|
std::vector< byte_t > test = packet2bytes(client_request->packet);
|
2018-11-18 23:08:02 +00:00
|
|
|
// llarp::LogInfo("packet2bytes figures we should send ", test.size(), "
|
|
|
|
// bytes");
|
|
|
|
|
|
|
|
server_request->sendto_hook(server_request->user, server_request->from,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer(llarp_buffer_t(test)));
|
2018-11-18 23:08:02 +00:00
|
|
|
|
|
|
|
llarp_host_resolved(client_request);
|
|
|
|
return;
|
|
|
|
|
2018-09-23 16:55:43 +00:00
|
|
|
// llarp::LogDebug("handle_dnsc_result - client request question type",
|
|
|
|
// std::to_string(client_request->question.type));
|
2018-11-18 23:48:50 +00:00
|
|
|
|
|
|
|
/*
|
2018-09-23 16:55:43 +00:00
|
|
|
if(client_request->question.type == 12)
|
|
|
|
{
|
|
|
|
writesend_dnss_revresponse(client_request->revDNS, server_request->from,
|
|
|
|
server_request);
|
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
else if(client_request->question.type == 15)
|
|
|
|
{
|
|
|
|
writesend_dnss_mxresponse(client_request->result.h, client_request->revDNS,
|
|
|
|
server_request->from, server_request);
|
|
|
|
}
|
|
|
|
else if(client_request->question.type == 16)
|
|
|
|
{
|
|
|
|
llarp::LogInfo("Writing TXT ", client_request->revDNS);
|
|
|
|
writesend_dnss_txtresponse(client_request->revDNS, server_request->from,
|
|
|
|
server_request);
|
|
|
|
}
|
2018-09-23 16:55:43 +00:00
|
|
|
else
|
|
|
|
{
|
2018-11-03 13:19:18 +00:00
|
|
|
if(client_request->question.type != 1)
|
|
|
|
{
|
|
|
|
llarp::LogInfo("Returning type ", client_request->question.type,
|
|
|
|
" as standard");
|
|
|
|
}
|
|
|
|
llarp::huint32_t *useHostRes = nullptr;
|
2018-10-13 15:42:47 +00:00
|
|
|
if(client_request->found)
|
2018-11-03 13:19:18 +00:00
|
|
|
useHostRes = &client_request->result;
|
2018-10-10 13:33:25 +00:00
|
|
|
writesend_dnss_response(useHostRes, server_request->from, server_request);
|
2018-09-23 16:55:43 +00:00
|
|
|
}
|
2018-07-20 10:28:21 +00:00
|
|
|
llarp_host_resolved(client_request);
|
2018-11-18 23:48:50 +00:00
|
|
|
*/
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 13:19:06 +00:00
|
|
|
// our generic version
|
2018-07-16 12:48:04 +00:00
|
|
|
void
|
2018-12-02 18:07:07 +00:00
|
|
|
handle_recvfrom(llarp_buffer_t *buffer, dnsd_question_request *request)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
const size_t HDR_OFFSET = 12;
|
2018-12-01 14:35:11 +00:00
|
|
|
const char *p_buffer = (const char *)buffer->base;
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-12-01 14:35:11 +00:00
|
|
|
int rcode = (buffer->base[3] & 0x0F);
|
2018-07-23 23:56:26 +00:00
|
|
|
llarp::LogDebug("dnsd rcode ", rcode);
|
2018-07-21 13:24:47 +00:00
|
|
|
|
2018-12-01 14:35:11 +00:00
|
|
|
if(!decode_hdr(buffer, &request->hdr))
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to decode dns header");
|
|
|
|
return;
|
|
|
|
}
|
2018-07-16 12:48:04 +00:00
|
|
|
p_buffer += HDR_OFFSET;
|
|
|
|
std::string m_qName = "";
|
2018-07-21 13:24:47 +00:00
|
|
|
int length = *p_buffer++;
|
|
|
|
while(length != 0)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
char c = *p_buffer++;
|
|
|
|
m_qName.append(1, c);
|
|
|
|
}
|
|
|
|
length = *p_buffer++;
|
2018-07-21 13:24:47 +00:00
|
|
|
if(length != 0)
|
|
|
|
m_qName.append(1, '.');
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
2018-07-22 03:34:28 +00:00
|
|
|
request->question.name = m_qName;
|
|
|
|
request->question.type = get16bits(p_buffer);
|
|
|
|
request->question.qClass = get16bits(p_buffer);
|
2018-07-24 01:43:32 +00:00
|
|
|
|
2018-07-23 23:56:26 +00:00
|
|
|
llarp::LogDebug("qName ", request->question.name);
|
|
|
|
llarp::LogDebug("qType ", request->question.type);
|
|
|
|
llarp::LogDebug("qClass ", request->question.qClass);
|
2018-07-21 13:24:47 +00:00
|
|
|
|
2018-07-18 21:06:18 +00:00
|
|
|
/*
|
|
|
|
llarp::Addr test(*request->from);
|
|
|
|
llarp::LogInfo("DNS request from ", test);
|
|
|
|
llarp::Addr test2(from);
|
|
|
|
llarp::LogInfo("DNS request from ", test2);
|
|
|
|
*/
|
2018-07-24 01:43:32 +00:00
|
|
|
|
2018-11-18 23:48:50 +00:00
|
|
|
// sockaddr *fromCopy = new sockaddr(*from); // make our own sockaddr that
|
|
|
|
// won't get cleaned up
|
2018-10-03 10:59:49 +00:00
|
|
|
if(!request)
|
|
|
|
{
|
|
|
|
llarp::LogError("request is not configured");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!request->context)
|
|
|
|
{
|
|
|
|
llarp::LogError("request context is not configured");
|
|
|
|
return;
|
|
|
|
}
|
2018-07-25 00:35:11 +00:00
|
|
|
if(request->context->intercept)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-08-08 12:40:54 +00:00
|
|
|
// llarp::Addr test(*from);
|
|
|
|
// llarp::LogInfo("from ", test);
|
|
|
|
dnsd_query_hook_response *intercept =
|
2018-11-18 23:48:50 +00:00
|
|
|
request->context->intercept(request->question.name, request);
|
2018-07-25 00:35:11 +00:00
|
|
|
// if(!forward_dns_request(m_qName))
|
|
|
|
if(intercept != nullptr)
|
2018-07-23 23:56:26 +00:00
|
|
|
{
|
2018-08-02 12:51:49 +00:00
|
|
|
llarp::LogDebug("hook returned a response");
|
2018-08-08 12:40:54 +00:00
|
|
|
if(intercept->dontSendResponse)
|
2018-08-02 12:51:49 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("HOOKED: Not sending a response");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 14:42:05 +00:00
|
|
|
if(intercept->dontLookUp && intercept->returnThis.h)
|
2018-08-02 12:51:49 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("HOOKED: sending an immediate override");
|
|
|
|
// told that hook will handle overrides
|
2018-11-29 14:01:13 +00:00
|
|
|
writesend_dnss_response(&intercept->returnThis, request);
|
2018-08-02 12:51:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-23 23:56:26 +00:00
|
|
|
}
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 12:51:49 +00:00
|
|
|
// FIXME: check request and context's client
|
2018-08-08 12:40:54 +00:00
|
|
|
if(!request->context)
|
2018-08-01 09:04:40 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("dnsd request context was not a dnsd context");
|
2018-11-18 23:48:50 +00:00
|
|
|
writesend_dnss_response(nullptr, request);
|
2018-08-01 09:04:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-02 12:51:49 +00:00
|
|
|
/*
|
2018-08-01 09:04:40 +00:00
|
|
|
struct dns_tracker *tracker = (struct dns_tracker *)request->context->tracker;
|
|
|
|
if (!tracker)
|
|
|
|
{
|
|
|
|
llarp::LogError("dnsd request context tracker was not a dns_tracker");
|
|
|
|
sockaddr *fromCopy = new sockaddr(*from);
|
|
|
|
writesend_dnss_response(nullptr, fromCopy, request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dnsd_context *dnsd = tracker->dnsd;
|
|
|
|
if (!dnsd)
|
|
|
|
{
|
|
|
|
llarp::LogError("tracker dnsd was not a dnsd context");
|
|
|
|
sockaddr *fromCopy = new sockaddr(*from);
|
|
|
|
writesend_dnss_response(nullptr, fromCopy, request);
|
|
|
|
return;
|
|
|
|
}
|
2018-08-02 12:51:49 +00:00
|
|
|
*/
|
2018-11-18 23:48:50 +00:00
|
|
|
// delete fromCopy;
|
2018-11-18 23:08:02 +00:00
|
|
|
// call DNSc
|
2018-07-22 03:34:28 +00:00
|
|
|
if(request->llarp)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-08-01 09:04:40 +00:00
|
|
|
// make async request
|
2018-08-08 12:40:54 +00:00
|
|
|
llarp_resolve_host(&request->context->client, m_qName.c_str(),
|
2018-11-03 13:19:18 +00:00
|
|
|
&handle_dnsc_result, (void *)request,
|
|
|
|
request->question.type);
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
2018-07-22 03:34:28 +00:00
|
|
|
else
|
|
|
|
{
|
2018-08-01 09:04:40 +00:00
|
|
|
// make raw/sync request
|
2018-08-08 12:40:54 +00:00
|
|
|
raw_resolve_host(&request->context->client, m_qName.c_str(),
|
2018-11-03 13:19:18 +00:00
|
|
|
&handle_dnsc_result, (void *)request,
|
|
|
|
request->question.type);
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-25 00:35:11 +00:00
|
|
|
llarp_handle_dnsd_recvfrom(struct llarp_udp_io *udp,
|
2019-02-03 00:48:10 +00:00
|
|
|
const struct sockaddr *saddr, ManagedBuffer buf)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-08-08 12:40:54 +00:00
|
|
|
if(!dns_udp_tracker.dnsd)
|
2018-08-01 09:04:40 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("No tracker set in dnsd context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// create new request
|
2018-07-22 03:34:28 +00:00
|
|
|
dnsd_question_request *llarp_dns_request = new dnsd_question_request;
|
2018-08-08 12:40:54 +00:00
|
|
|
llarp_dns_request->context = dns_udp_tracker.dnsd; // set context
|
|
|
|
llarp_dns_request->from =
|
|
|
|
new sockaddr(*saddr); // make a copy of the sockaddr
|
|
|
|
llarp_dns_request->user = (void *)udp;
|
|
|
|
llarp_dns_request->llarp = true;
|
|
|
|
llarp_dns_request->sendto_hook =
|
|
|
|
&llarp_sendto_dns_hook_func; // set sock hook
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp::LogInfo("Server request's UDP ", llarp_dns_request->user);
|
2019-02-02 23:12:42 +00:00
|
|
|
handle_recvfrom(&buf.underlying, llarp_dns_request);
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-22 00:39:09 +00:00
|
|
|
raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr,
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer buffer)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-08-08 12:40:54 +00:00
|
|
|
if(!dns_udp_tracker.dnsd)
|
2018-08-01 09:04:40 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("No tracker set in dnsd context");
|
|
|
|
return;
|
|
|
|
}
|
2018-07-22 03:34:28 +00:00
|
|
|
dnsd_question_request *llarp_dns_request = new dnsd_question_request;
|
2018-08-08 12:40:54 +00:00
|
|
|
llarp_dns_request->context = dns_udp_tracker.dnsd; // set context
|
|
|
|
llarp_dns_request->from =
|
|
|
|
new sockaddr(*saddr); // make a copy of the sockaddr
|
|
|
|
llarp_dns_request->user = (void *)sockfd;
|
|
|
|
llarp_dns_request->llarp = false;
|
|
|
|
llarp_dns_request->sendto_hook = &raw_sendto_dns_hook_func;
|
2018-11-21 23:37:47 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
handle_recvfrom(&buffer.underlying, llarp_dns_request);
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
2018-07-20 10:28:21 +00:00
|
|
|
|
|
|
|
bool
|
2018-12-10 14:14:55 +00:00
|
|
|
llarp_dnsd_init(struct dnsd_context *const dnsd, llarp::Logic *const logic,
|
2018-10-03 10:59:49 +00:00
|
|
|
struct llarp_ev_loop *const netloop,
|
|
|
|
const llarp::Addr &dnsd_sockaddr,
|
2018-09-29 10:22:48 +00:00
|
|
|
const llarp::Addr &dnsc_sockaddr)
|
2018-07-20 10:28:21 +00:00
|
|
|
{
|
2018-09-29 10:22:48 +00:00
|
|
|
// struct sockaddr_in bindaddr;
|
|
|
|
/*
|
|
|
|
llarp::Addr dnsd_addr;
|
|
|
|
bool ifRes = GetIFAddr(std::string(dnsd_ifname), dnsd_addr, AF_INET);
|
|
|
|
if (!ifRes)
|
|
|
|
{
|
|
|
|
llarp::LogError("Couldn't init dns server, can't resolve interface: ",
|
|
|
|
dnsd_ifname); return false;
|
|
|
|
}
|
|
|
|
llarp::LogInfo("DNSd interface ip ", dnsd_addr);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
bindaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // network byte
|
2018-07-21 13:24:47 +00:00
|
|
|
bindaddr.sin_family = AF_INET;
|
|
|
|
bindaddr.sin_port = htons(dnsd_port);
|
2018-09-29 10:22:48 +00:00
|
|
|
*/
|
2018-07-21 13:24:47 +00:00
|
|
|
|
|
|
|
dnsd->udp.user = &dns_udp_tracker;
|
|
|
|
dnsd->udp.recvfrom = &llarp_handle_dns_recvfrom;
|
|
|
|
dnsd->udp.tick = nullptr;
|
|
|
|
|
2018-07-21 13:19:06 +00:00
|
|
|
dns_udp_tracker.dnsd = dnsd;
|
2018-07-21 13:24:47 +00:00
|
|
|
|
2018-09-29 10:22:48 +00:00
|
|
|
dnsd->tracker = &dns_udp_tracker; // register global tracker with context
|
|
|
|
dnsd->intercept = nullptr; // set default intercepter
|
|
|
|
|
|
|
|
// set up fresh socket
|
|
|
|
dnsd->client.udp = new llarp_udp_io;
|
|
|
|
dnsd->client.udp->user = &dns_udp_tracker;
|
|
|
|
dnsd->client.udp->recvfrom = &llarp_handle_dns_recvfrom;
|
|
|
|
dnsd->client.udp->tick = nullptr;
|
2018-07-24 01:43:32 +00:00
|
|
|
|
2018-07-20 10:28:21 +00:00
|
|
|
// configure dns client
|
2018-09-29 10:22:48 +00:00
|
|
|
// llarp::LogInfo("DNSd setting relay to ", dnsc_sockaddr);
|
|
|
|
if(!llarp_dnsc_init(&dnsd->client, logic, netloop, dnsc_sockaddr))
|
2018-07-20 10:28:21 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Couldnt init dns client");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:40:54 +00:00
|
|
|
if(netloop)
|
2018-08-01 09:04:40 +00:00
|
|
|
{
|
2018-09-29 10:22:48 +00:00
|
|
|
llarp::LogInfo("DNSd binding to ", dnsd_sockaddr);
|
|
|
|
return llarp_ev_add_udp(netloop, &dnsd->udp,
|
|
|
|
(const sockaddr *)dnsd_sockaddr)
|
2018-08-08 12:40:54 +00:00
|
|
|
!= -1;
|
2018-08-01 09:04:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-20 10:28:21 +00:00
|
|
|
}
|