lokinet/llarp/dnsd.cpp

277 lines
7.7 KiB
C++
Raw Normal View History

2018-07-16 12:48:04 +00:00
#include "dnsd.hpp"
#include <string>
2018-07-16 12:48:04 +00:00
#include "logger.hpp"
#include "net.hpp"
int
get16bits(const char *&buffer) throw()
2018-07-16 12:48:04 +00:00
{
int value = static_cast< unsigned char >(buffer[0]);
value = value << 8;
value += static_cast< unsigned char >(buffer[1]);
2018-07-16 12:48:04 +00:00
buffer += 2;
return value;
}
void
put16bits(char *&buffer, uint value) throw()
2018-07-16 12:48:04 +00:00
{
buffer[0] = (value & 0xFF00) >> 8;
buffer[1] = value & 0xFF;
buffer += 2;
}
void
put32bits(char *&buffer, unsigned long value) throw()
2018-07-16 12:48:04 +00:00
{
buffer[0] = (value & 0xFF000000) >> 24;
buffer[1] = (value & 0x00FF0000) >> 16;
buffer[2] = (value & 0x0000FF00) >> 8;
buffer[3] = (value & 0x000000FF) >> 0;
2018-07-16 12:48:04 +00:00
buffer += 4;
}
dns_msg *
decode_hdr(const char *buffer)
2018-07-16 12:48:04 +00:00
{
dns_msg *hdr = new dns_msg;
hdr->id = get16bits(buffer);
uint fields = get16bits(buffer);
hdr->qr = fields & 0x8000;
hdr->opcode = fields & 0x7800;
hdr->aa = fields & 0x0400;
hdr->tc = fields & 0x0200;
hdr->rd = fields & 0x0100;
hdr->ra = fields & 0x8000;
2018-07-16 12:48:04 +00:00
hdr->qdCount = get16bits(buffer);
hdr->anCount = get16bits(buffer);
hdr->nsCount = get16bits(buffer);
hdr->arCount = get16bits(buffer);
return hdr;
}
void
code_domain(char *&buffer, const std::string &domain) throw()
2018-07-16 12:48:04 +00:00
{
2018-07-16 23:26:58 +00:00
std::string::size_type start(0), end; // indexes
// llarp::LogInfo("domain [", domain, "]");
while((end = domain.find('.', start)) != std::string::npos)
2018-07-16 12:48:04 +00:00
{
*buffer++ = end - start; // label length octet
2018-07-16 23:26:58 +00:00
for(auto i = start; i < end; i++)
2018-07-16 12:48:04 +00:00
{
*buffer++ = domain[i]; // label octets
// llarp::LogInfo("Writing ", domain[i], " at ", i);
2018-07-16 12:48:04 +00:00
}
start = end + 1; // Skip '.'
2018-07-16 12:48:04 +00:00
}
// llarp::LogInfo("start ", start, " domain size ", domain.size());
2018-07-16 12:48:04 +00:00
*buffer++ = domain.size() - start; // last label length octet
2018-07-16 23:26:58 +00:00
for(size_t i = start; i < domain.size(); i++)
2018-07-16 12:48:04 +00:00
{
*buffer++ = domain[i]; // last label octets
// llarp::LogInfo("Writing ", domain[i], " at ", i);
2018-07-16 12:48:04 +00:00
}
*buffer++ = 0;
}
ssize_t
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??
socklen_t addrLen = sizeof(struct sockaddr_in);
2018-07-16 12:48:04 +00:00
return sendto(*fd, buffer, length, 0, from, addrLen);
}
ssize_t
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;
if(!udp)
2018-07-16 12:48:04 +00:00
{
return 0;
}
return llarp_ev_udp_sendto(udp, from, buffer, length);
}
bool
forward_dns_request(std::string request)
2018-07-16 12:48:04 +00:00
{
return true;
}
void
writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr *from,
dns_request *request)
2018-07-16 12:48:04 +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);
char *write_buffer = buf;
char *bufferBegin = buf;
2018-07-16 12:48:04 +00:00
// build header
put16bits(write_buffer, request->id);
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);
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
code_domain(write_buffer, request->m_qName);
put16bits(write_buffer, request->m_qType);
put16bits(write_buffer, request->m_qClass);
// code answer
code_domain(write_buffer, request->m_qName); // com, type=6, ttl=0
2018-07-16 12:48:04 +00:00
put16bits(write_buffer, request->m_qType);
put16bits(write_buffer, request->m_qClass);
put32bits(write_buffer, 1453); // 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;
unsigned char *ip = (unsigned char *)&sin->sin_addr.s_addr;
2018-07-16 12:48:04 +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;
llarp::LogInfo("Sending ", out_bytes, " bytes");
// 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);
}
void
phase2(dns_client_request *client_request, struct sockaddr *result)
2018-07-16 12:48:04 +00:00
{
llarp::LogInfo("phase2");
// writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr
// *from, dns_request *request)
2018-07-16 12:48:04 +00:00
dns_request *server_request = (dns_request *)client_request->user;
writesend_dnss_response(result, server_request->from, server_request);
}
void
handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from,
dns_request *request)
2018-07-16 12:48:04 +00:00
{
const size_t HDR_OFFSET = 12;
const char *p_buffer = buffer;
2018-07-16 12:48:04 +00:00
dns_msg *msg = decode_hdr(p_buffer);
// llarp::LogInfo("DNS_MSG size", sizeof(dns_msg));
2018-07-16 12:48:04 +00:00
p_buffer += HDR_OFFSET;
request->id = msg->id;
2018-07-16 12:48:04 +00:00
std::string m_qName = "";
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++;
if(length != 0)
m_qName.append(1, '.');
2018-07-16 12:48:04 +00:00
}
request->m_qName = m_qName;
request->m_qType = get16bits(p_buffer);
2018-07-16 12:48:04 +00:00
request->m_qClass = get16bits(p_buffer);
llarp::LogInfo("qName ", m_qName);
llarp::LogInfo("qType ", request->m_qType);
llarp::LogInfo("qClass ", request->m_qClass);
if(!forward_dns_request(m_qName))
2018-07-16 12:48:04 +00:00
{
// told that hook will handle overrides
return;
}
sockaddr *hostRes = nullptr;
2018-07-17 04:37:50 +00:00
if(1)
2018-07-16 12:48:04 +00:00
{
hostRes = resolveHost(m_qName.c_str());
llarp::Addr anIp(*hostRes);
llarp::LogInfo("DNS got ", anIp);
// writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr
// *from, dns_request *request)
2018-07-16 12:48:04 +00:00
writesend_dnss_response(hostRes, from, request);
}
else
{
2018-07-16 23:26:58 +00:00
// llarp::Addr anIp;
// struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
// hostRes = llarp_resolveHost(udp->parent, m_qName.c_str());
2018-07-16 23:26:58 +00:00
// llarp_resolve_host(udp->parent, m_qName.c_str(), &phase2, (void
// *)request);
2018-07-16 12:48:04 +00:00
}
}
void
2018-07-16 23:26:58 +00:00
handle_dns_recvfrom(struct llarp_udp_io *udp, const struct sockaddr *saddr,
const void *buf, ssize_t sz)
2018-07-16 12:48:04 +00:00
{
2018-07-16 23:26:58 +00:00
llarp::LogDebug("Received Bytes ", sz);
2018-07-16 12:48:04 +00:00
dns_request llarp_dns_request;
llarp_dns_request.from = (struct sockaddr *)saddr;
llarp_dns_request.user = (void *)udp;
llarp_dns_request.hook = &llarp_sendto_dns_hook_func;
handle_recvfrom((char *)buf, sz, saddr, &llarp_dns_request);
}
2018-07-16 23:26:58 +00:00
extern "C"
2018-07-16 12:48:04 +00:00
{
2018-07-16 23:26:58 +00:00
bool
llarp_dns_init(struct dns_context *dns, struct llarp_ev_loop *mainloop,
const char *bindaddr, uint16_t bindport)
{
struct sockaddr_in srcaddr;
if(inet_pton(AF_INET, bindaddr, &srcaddr.sin_addr.s_addr) == -1)
return false;
srcaddr.sin_family = AF_INET;
srcaddr.sin_port = htons(bindport);
dns->udp.user = dns;
dns->udp.recvfrom = &handle_dns_recvfrom;
dns->udp.tick = nullptr;
return llarp_ev_add_udp(mainloop, &dns->udp, (const sockaddr *)&srcaddr)
!= -1;
}
// this is called in net threadpool
void
raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr,
const void *buf, ssize_t sz)
{
llarp::LogInfo("Received Bytes ", sz);
dns_request llarp_dns_request;
llarp_dns_request.from = (struct sockaddr *)saddr;
llarp_dns_request.user = (void *)sockfd;
llarp_dns_request.hook = &raw_sendto_dns_hook_func;
handle_recvfrom((char *)buf, sz, saddr, &llarp_dns_request);
}
2018-07-16 12:48:04 +00:00
}