2018-07-16 12:48:04 +00:00
|
|
|
#include "dnsd.hpp"
|
2018-07-22 03:34:28 +00:00
|
|
|
#include <llarp/dns.h>
|
2018-07-21 13:24:47 +00:00
|
|
|
#include <string>
|
|
|
|
#include "ev.hpp"
|
2018-07-27 00:21:57 +00:00
|
|
|
#include "llarp/net.hpp"
|
2018-07-16 12:48:04 +00:00
|
|
|
#include "logger.hpp"
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define wmin(x, y) (((x) < (y)) ? (x) : (y))
|
|
|
|
#define MIN wmin
|
|
|
|
#endif
|
|
|
|
|
2018-07-23 23:56:26 +00:00
|
|
|
dns_tracker dns_udp_tracker;
|
|
|
|
|
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,
|
|
|
|
const void *buffer, size_t length)
|
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);
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return sendto(*fd, (const char *)buffer, length, 0, from, addrLen);
|
|
|
|
#else
|
2018-07-16 12:48:04 +00:00
|
|
|
return sendto(*fd, buffer, length, 0, from, addrLen);
|
2018-07-30 04:38:14 +00:00
|
|
|
#endif
|
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,
|
|
|
|
const void *buffer, size_t length)
|
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);
|
2018-07-16 12:48:04 +00:00
|
|
|
return llarp_ev_udp_sendto(udp, from, buffer, length);
|
|
|
|
}
|
|
|
|
|
2018-07-21 13:24:47 +00:00
|
|
|
bool
|
|
|
|
forward_dns_request(std::string request)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-07-21 13:24:47 +00:00
|
|
|
writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr *from,
|
2018-07-22 03:34:28 +00:00
|
|
|
dnsd_question_request *request)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-25 00:35:11 +00:00
|
|
|
// lock_t lock(m_dnsd2_Mutex);
|
2018-07-21 13:24:47 +00:00
|
|
|
if(!hostRes)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("Failed to resolve");
|
|
|
|
// FIXME: actually return correct packet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t BUFFER_SIZE = 1024;
|
|
|
|
char buf[BUFFER_SIZE];
|
|
|
|
memset(buf, 0, BUFFER_SIZE);
|
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
|
|
|
|
put16bits(write_buffer, request->id);
|
2018-07-21 13:24:47 +00:00
|
|
|
int fields = (1 << 15); // QR => message type, 1 = response
|
|
|
|
fields += (0 << 14); // I think opcode is always 0
|
|
|
|
fields += 0; // response code (3 => not found, 0 = Ok)
|
2018-07-16 12:48:04 +00:00
|
|
|
put16bits(write_buffer, fields);
|
|
|
|
|
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-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
|
|
|
|
|
|
|
// has to be a string of 4 bytes
|
|
|
|
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-07-16 12:48:04 +00:00
|
|
|
*write_buffer++ = ip[0];
|
|
|
|
*write_buffer++ = ip[1];
|
|
|
|
*write_buffer++ = ip[2];
|
|
|
|
*write_buffer++ = ip[3];
|
|
|
|
|
|
|
|
uint out_bytes = write_buffer - bufferBegin;
|
2018-07-23 23:56:26 +00:00
|
|
|
llarp::LogDebug("Sending ", out_bytes, " bytes");
|
2018-07-21 13:24:47 +00:00
|
|
|
// struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
2018-07-16 12:48:04 +00:00
|
|
|
request->hook(request->user, from, buf, out_bytes);
|
|
|
|
}
|
|
|
|
|
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-21 13:24:47 +00:00
|
|
|
// llarp::LogInfo("phase2 client ", client_request);
|
|
|
|
// writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr
|
2018-07-22 03:34:28 +00:00
|
|
|
// *from, dnsd_question_request *request)
|
2018-07-25 00:35:11 +00:00
|
|
|
dnsd_question_request *server_request =
|
|
|
|
(dnsd_question_request *)client_request->user;
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp::Addr test(*server_request->from);
|
|
|
|
// llarp::LogInfo("server request sock ", server_request->from, " is ", test);
|
|
|
|
// llarp::LogInfo("phase2 server ", server_request);
|
|
|
|
writesend_dnss_response(
|
|
|
|
client_request->found ? &client_request->result : nullptr,
|
|
|
|
server_request->from, server_request);
|
2018-07-20 10:28:21 +00:00
|
|
|
llarp_host_resolved(client_request);
|
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-07-21 13:24:47 +00:00
|
|
|
handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from,
|
2018-07-22 03:34:28 +00:00
|
|
|
dnsd_question_request *request)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-25 00:35:11 +00:00
|
|
|
// lock_t lock(m_dnsd_Mutex);
|
2018-07-16 12:48:04 +00:00
|
|
|
const size_t HDR_OFFSET = 12;
|
2018-07-21 13:24:47 +00:00
|
|
|
const char *p_buffer = buffer;
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-21 13:19:06 +00:00
|
|
|
int rcode = (buffer[3] & 0x0F);
|
2018-07-23 23:56:26 +00:00
|
|
|
llarp::LogDebug("dnsd rcode ", rcode);
|
2018-07-21 13:24:47 +00:00
|
|
|
|
2018-07-21 13:19:06 +00:00
|
|
|
dns_msg_header *msg = decode_hdr(p_buffer);
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp::LogInfo("DNS_MSG size", sizeof(dns_msg));
|
2018-07-16 12:48:04 +00:00
|
|
|
p_buffer += HDR_OFFSET;
|
2018-07-21 13:24:47 +00:00
|
|
|
request->id = msg->id;
|
2018-07-16 12:48:04 +00:00
|
|
|
std::string m_qName = "";
|
2018-07-21 13:24:47 +00:00
|
|
|
int length = *p_buffer++;
|
|
|
|
// llarp::LogInfo("qNamLen", length);
|
|
|
|
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-25 00:35:11 +00:00
|
|
|
// request->m_qName = m_qName;
|
|
|
|
// request->m_qType = request->question.type;
|
|
|
|
// request->m_qClass = request->question.qClass;
|
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-07-25 00:35:11 +00:00
|
|
|
if(request->context->intercept)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
sockaddr *intercept =
|
|
|
|
request->context->intercept(request->question.name, request->context);
|
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
|
|
|
{
|
|
|
|
// told that hook will handle overrides
|
|
|
|
sockaddr *fromCopy = new sockaddr(*from);
|
|
|
|
writesend_dnss_response(intercept, fromCopy, request);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr *hostRes = nullptr;
|
2018-07-22 03:34:28 +00:00
|
|
|
if(request->llarp)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp::Addr anIp;
|
|
|
|
// llarp::LogInfo("Checking server request ", request);
|
|
|
|
struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
2018-07-21 13:19:06 +00:00
|
|
|
struct dns_tracker *tracker = (struct dns_tracker *)udp->user;
|
2018-07-21 13:24:47 +00:00
|
|
|
dnsd_context *dnsd = tracker->dnsd;
|
|
|
|
// dnsd_context *dnsd = (dnsd_context *)udp->user;
|
|
|
|
// llarp::LogInfo("Server request UDP ", request->user);
|
|
|
|
// llarp::LogInfo("server request hook ", request->hook);
|
|
|
|
// llarp::LogInfo("UDP ", udp);
|
|
|
|
// hostRes = llarp_resolveHost(udp->parent, m_qName.c_str());
|
|
|
|
llarp_resolve_host(&dnsd->client, m_qName.c_str(), &handle_dnsc_result,
|
|
|
|
(void *)request);
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
2018-07-22 03:34:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hostRes = raw_resolve_host(m_qName.c_str());
|
|
|
|
llarp::Addr anIp(*hostRes);
|
2018-07-23 23:56:26 +00:00
|
|
|
llarp::LogDebug("DNSc got ", anIp);
|
2018-07-22 03:34:28 +00:00
|
|
|
// writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr
|
|
|
|
// *from, dnsd_question_request *request)
|
|
|
|
sockaddr *fromCopy = new sockaddr(*from);
|
|
|
|
writesend_dnss_response(hostRes, fromCopy, request);
|
|
|
|
}
|
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,
|
|
|
|
const struct sockaddr *paddr, const void *buf,
|
|
|
|
ssize_t sz)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-25 00:35:11 +00:00
|
|
|
// lock_t lock(m_dnsd3_Mutex);
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp_link *link = static_cast< llarp_link * >(udp->user);
|
2018-07-23 23:56:26 +00:00
|
|
|
llarp::LogDebug("llarp Received Bytes ", sz);
|
2018-07-22 03:34:28 +00:00
|
|
|
dnsd_question_request *llarp_dns_request = new dnsd_question_request;
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp::LogInfo("Creating server request ", &llarp_dns_request);
|
|
|
|
// llarp::LogInfo("Server UDP address ", udp);
|
2018-07-23 23:56:26 +00:00
|
|
|
llarp_dns_request->context = dns_udp_tracker.dnsd;
|
2018-07-21 13:19:06 +00:00
|
|
|
// make a copy of the sockaddr
|
2018-07-22 03:34:28 +00:00
|
|
|
llarp_dns_request->from = new sockaddr(*paddr);
|
|
|
|
llarp_dns_request->user = (void *)udp;
|
|
|
|
llarp_dns_request->llarp = true;
|
2018-07-21 13:19:06 +00:00
|
|
|
// set sock hook
|
2018-07-18 21:06:18 +00:00
|
|
|
llarp_dns_request->hook = &llarp_sendto_dns_hook_func;
|
2018-07-21 13:19:06 +00:00
|
|
|
|
2018-07-21 13:24:47 +00:00
|
|
|
// llarp::LogInfo("Server request's UDP ", llarp_dns_request->user);
|
2018-07-18 21:06:18 +00:00
|
|
|
handle_recvfrom((char *)buf, sz, llarp_dns_request->from, llarp_dns_request);
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-21 13:24:47 +00:00
|
|
|
raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr, const void *buf,
|
|
|
|
ssize_t sz)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-18 21:06:18 +00:00
|
|
|
llarp::LogInfo("raw Received Bytes ", sz);
|
2018-07-22 03:34:28 +00:00
|
|
|
dnsd_question_request *llarp_dns_request = new dnsd_question_request;
|
2018-07-25 00:35:11 +00:00
|
|
|
llarp_dns_request->from = (struct sockaddr *)saddr;
|
|
|
|
llarp_dns_request->user = (void *)sockfd;
|
|
|
|
llarp_dns_request->llarp = false;
|
|
|
|
llarp_dns_request->hook = &raw_sendto_dns_hook_func;
|
2018-07-18 21:06:18 +00:00
|
|
|
handle_recvfrom((char *)buf, sz, saddr, llarp_dns_request);
|
2018-07-16 12:48:04 +00:00
|
|
|
}
|
2018-07-20 10:28:21 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_dnsd_init(struct dnsd_context *dnsd, struct llarp_ev_loop *netloop,
|
|
|
|
const char *dnsd_ifname, uint16_t dnsd_port,
|
|
|
|
const char *dnsc_hostname, uint16_t dnsc_port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in bindaddr;
|
|
|
|
bindaddr.sin_addr.s_addr = inet_addr("0.0.0.0");
|
2018-07-21 13:24:47 +00:00
|
|
|
bindaddr.sin_family = AF_INET;
|
|
|
|
bindaddr.sin_port = htons(dnsd_port);
|
|
|
|
|
|
|
|
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-07-25 00:35:11 +00:00
|
|
|
dnsd->intercept = nullptr;
|
2018-07-24 01:43:32 +00:00
|
|
|
|
2018-07-20 10:28:21 +00:00
|
|
|
// configure dns client
|
2018-07-21 13:24:47 +00:00
|
|
|
if(!llarp_dnsc_init(&dnsd->client, &dnsd->udp, dnsc_hostname, dnsc_port))
|
2018-07-20 10:28:21 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Couldnt init dns client");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-21 13:24:47 +00:00
|
|
|
return llarp_ev_add_udp(netloop, &dnsd->udp, (const sockaddr *)&bindaddr)
|
|
|
|
!= -1;
|
2018-07-20 10:28:21 +00:00
|
|
|
}
|