another DNS clean up pass

This commit is contained in:
Ryan Tharp 2018-08-01 02:04:40 -07:00
parent 7469e35399
commit de56a32069
8 changed files with 345 additions and 394 deletions

View File

@ -126,6 +126,20 @@ main(int argc, char *argv[])
}
else
{
// configure main netloop
struct dnsd_context dnsd;
if(!llarp_dnsd_init(&dnsd, nullptr, "*", 1053,
(const char *)dnsr_config.upstream_host.c_str(),
dnsr_config.upstream_port))
{
// llarp::LogError("failed to initialize dns subsystem");
llarp::LogError("Couldnt init dns daemon");
return 0;
}
// Configure intercept
dnsd.intercept = &hookChecker;
struct sockaddr_in m_address;
int m_sockfd;

View File

@ -3,7 +3,6 @@
#include <llarp/ev.h> // for sockaadr
#include <sys/types.h> // for uint & ssize_t
#include <map> // for udp DNS tracker
#ifdef __cplusplus
extern "C"
@ -26,40 +25,12 @@ extern "C"
// struct dnsd_question_request;
struct dnsc_answer_request;
// dnsc can work over any UDP socket
// however we can't ignore udp->user
// we need to be able to reference the request (being a request or response)
// bottom line is we can't use udp->user
// so we'll need to track all incoming and outgoing requests
struct dns_tracker;
//struct dns_tracker;
// should we pass by llarp::Addr
// not as long as we're supporting raw
typedef void (*dnsc_answer_hook_func)(dnsc_answer_request *request);
struct sockaddr *
raw_resolve_host(const char *url);
/// async resolve hostname
bool
llarp_resolve_host(struct dnsc_context *dns, const char *url,
dnsc_answer_hook_func resolved, void *user);
void
llarp_host_resolved(dnsc_answer_request *request);
/*
// XXX: these should be internal and not exposed
void
llarp_handle_recvfrom(struct llarp_udp_io *udp, const struct sockaddr *saddr,
const void *buf, ssize_t sz);
void
raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr, const void
*buf, ssize_t sz);
*/
#ifdef __cplusplus
}
#endif

View File

@ -123,12 +123,12 @@ extern "C"
void
code_domain(char *&buffer, const std::string &domain) throw()
{
int start(0), end; // indexes
size_t start(0), end; // indexes
// llarp::LogInfo("domain [", domain, "]");
while((end = domain.find('.', start)) != std::string::npos)
{
*buffer++ = end - start; // label length octet
for(int i = start; i < end; i++)
for(size_t i = start; i < end; i++)
{
*buffer++ = domain[i]; // label octets
// llarp::LogInfo("Writing ", domain[i], " at ", i);
@ -188,4 +188,4 @@ extern "C"
}
*/
}
}
}

View File

@ -6,10 +6,16 @@
#include <map>
#include <string>
// dnsc can work over any UDP socket
// however we can't ignore udp->user
// we need to be able to reference the request (being a request or response)
// bottom line is we can't use udp->user
// so we'll need to track all incoming and outgoing requests
struct dns_tracker
{
// uint c_responses;
uint c_requests;
// request has to be a pointer
std::map< uint, dnsc_answer_request * > client_request;
// FIXME: support multiple dns server contexts
dnsd_context *dnsd;

View File

@ -18,12 +18,12 @@
#include "llarp/net.hpp" // for llarp::Addr
#include "logger.hpp"
// FIXME: make configurable
#define SERVER "8.8.8.8"
#define PORT 53
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
dns_tracker dns_udp_tracker;
#define DNC_BUF_SIZE 512
// a question to be asked remotely
/// a question to be asked remotely (the actual bytes to send on the wire)
// header, question
struct dns_query
{
@ -33,6 +33,7 @@ struct dns_query
// uint16_t reqType;
};
/// build a DNS question packet
struct dns_query *
build_dns_packet(char *url, uint16_t id, uint16_t reqType)
{
@ -88,274 +89,100 @@ build_dns_packet(char *url, uint16_t id, uint16_t reqType)
return dnsQuery;
}
struct sockaddr *
raw_resolve_host(const char *url)
dns_query *
answer_request_alloc(struct dnsc_context *dnsc, void *sock, const char *url,
dnsc_answer_hook_func resolved, void *user)
{
// char *sUrl = strdup(url);
// struct dns_query dnsQuery;
dns_query *dns_packet = build_dns_packet((char *)url, 0xDB42, 1);
/*
dnsQuery.length = 12;
dnsQuery.url = sUrl;
dnsQuery.reqType = 0x01;
// dnsQuery.request = { 0xDB, 0x42, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00 };
dnsQuery.request[0] = 0xDB;
dnsQuery.request[1] = 0x42;
dnsQuery.request[2] = 0x01;
dnsQuery.request[3] = 0x00;
dnsQuery.request[4] = 0x00;
dnsQuery.request[5] = 0x01;
dnsQuery.request[6] = 0x00;
dnsQuery.request[7] = 0x00;
dnsQuery.request[8] = 0x00;
dnsQuery.request[9] = 0x00;
dnsQuery.request[10] = 0x00;
dnsQuery.request[11] = 0x00;
*/
// char *word;
unsigned int i;
llarp::LogDebug("Asking DNS server ", SERVER, " about ", url);
// dnsQuery.reqType = 0x01;
/*
word = strtok(sUrl, ".");
while(word)
dnsc_answer_request *request = new dnsc_answer_request;
if (!request)
{
llarp::LogDebug("parsing hostname: \"%s\" is %zu characters\n", word,
strlen(word));
dnsQuery.request[dnsQuery.length++] = strlen(word);
for(i = 0; i < strlen(word); i++)
{
dnsQuery.request[dnsQuery.length++] = word[i];
}
word = strtok(NULL, ".");
}
dnsQuery.request[dnsQuery.length++] = 0x00; // End of the host name
dnsQuery.request[dnsQuery.length++] =
0x00; // 0x0001 - Query is a Type A query (host address)
dnsQuery.request[dnsQuery.length++] = dnsQuery.reqType;
dnsQuery.request[dnsQuery.length++] =
0x00; // 0x0001 - Query is class IN (Internet address)
dnsQuery.request[dnsQuery.length++] = 0x01;
*/
struct sockaddr_in addr;
// int socket;
ssize_t ret;
int rcode;
socklen_t size;
int ip = 0;
// int length;
unsigned char buffer[DNC_BUF_SIZE];
// unsigned char tempBuf[3];
uint16_t QDCOUNT; // No. of items in Question Section
uint16_t ANCOUNT; // No. of items in Answer Section
uint16_t NSCOUNT; // No. of items in Authority Section
uint16_t ARCOUNT; // No. of items in Additional Section
// uint16_t QCLASS; // Specifies the class of the query
uint16_t ATYPE; // Specifies the meaning of the data in the RDATA field
// uint16_t ACLASS; // Specifies the class of the data in the RDATA field
// uint32_t TTL; // The number of seconds the results can be cached
// uint16_t RDLENGTH; // The length of the RDATA field
// uint16_t MSGID;
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
llarp::LogWarn("Error creating socket!\n");
llarp::LogError("Couldn't make dnsc request");
return nullptr;
}
// socket = sockfd;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(SERVER);
addr.sin_port = htons(PORT);
size = sizeof(addr);
// hexdump("sending packet", &dnsQuery.request, dnsQuery.length);
ret = sendto(sockfd, dns_packet->request, dns_packet->length, 0,
(struct sockaddr *)&addr, size);
delete dns_packet;
if(ret < 0)
request->sock = sock;
request->user = user;
request->resolved = resolved;
request->found = false;
request->context = dnsc;
char *sUrl = strdup(url);
request->question.name = (char *)sUrl;
// leave 256 bytes available
if (request->question.name.size() > 255)
{
llarp::LogWarn("Error Sending Request");
//size_t diff = request->question.name.size() - 255;
//request->question.name = request->question.name.substr(diff); // get the rightmost 255 bytes
llarp::LogWarn("dnsc request question too long");
return nullptr;
}
// llarp::LogInfo("Sent\n");
memset(&buffer, 0, DNC_BUF_SIZE);
ret = recvfrom(sockfd, buffer, DNC_BUF_SIZE, 0, (struct sockaddr *)&addr,
&size);
if(ret < 0)
{
llarp::LogWarn("Error Receiving Response");
return nullptr;
}
// hexdump("received packet", &buffer, ret);
close(sockfd);
rcode = (buffer[3] & 0x0F);
// tempBuf[0] = buffer[4];
// tempBuf[1] = buffer[5];
// tempBuf[2] = '\0';
// printf("%0x %0x %0x %0x\n", buffer[4], buffer[5], tempBuf[0], tempBuf[1]);
// QDCOUNT = (uint16_t) strtol(tempBuf, NULL, 16);
QDCOUNT = (uint16_t)buffer[4] * 0x100 + buffer[5];
llarp::LogDebug("entries in question section: %u\n", QDCOUNT);
ANCOUNT = (uint16_t)buffer[6] * 0x100 + buffer[7];
llarp::LogDebug("records in answer section: %u\n", ANCOUNT);
NSCOUNT = (uint16_t)buffer[8] * 0x100 + buffer[9];
llarp::LogDebug("name server resource record count: %u\n", NSCOUNT);
ARCOUNT = (uint16_t)buffer[10] * 0x100 + buffer[11];
llarp::LogDebug("additional records count: %u\n", ARCOUNT);
/*
llarp::LogDebug("query type: %u\n", dnsQuery.reqType);
QCLASS = (uint16_t)dnsQuery.request[dnsQuery.length - 2] * 0x100
+ dnsQuery.request[dnsQuery.length - 1];
llarp::LogDebug("query class: %u\n", QCLASS);
length = dnsQuery.length + 1; // to skip 0xc00c
ATYPE = (uint16_t)buffer[length + 1] * 0x100 + buffer[length + 2];
llarp::LogDebug("answer type: %u\n", ATYPE);
ACLASS = (uint16_t)buffer[length + 3] * 0x100 + buffer[length + 4];
llarp::LogDebug("answer class: %u\n", ACLASS);
TTL = (uint32_t)buffer[length + 5] * 0x1000000 + buffer[length + 6] * 0x10000
+ buffer[length + 7] * 0x100 + buffer[length + 8];
llarp::LogDebug("seconds to cache: %u\n", TTL);
RDLENGTH = (uint16_t)buffer[length + 9] * 0x100 + buffer[length + 10];
llarp::LogDebug("bytes in answer: %u\n", RDLENGTH);
MSGID = (uint16_t)buffer[0] * 0x100 + buffer[1];
llarp::LogDebug("answer msg id: %u\n", MSGID);
*/
if(rcode == 2)
{
llarp::LogWarn("nameserver %s returned SERVFAIL:\n", SERVER);
llarp::LogWarn(
" the name server was unable to process this query due to a\n "
"problem with the name server.\n");
return nullptr;
}
else if(rcode == 3)
{
llarp::LogWarn("nameserver %s returned NXDOMAIN for ", SERVER);
llarp::LogWarn(
" the domain name referenced in the query does not exist\n");
return nullptr;
}
/* search for and print IPv4 addresses */
// if(dnsQuery.reqType == 0x01)
if(1)
{
llarp::LogDebug("DNS server's answer is: (type#=%u):", ATYPE);
// printf("IPv4 address(es) for %s:\n", dnsQuery.url);
for(i = 0; i < ret; i++)
{
if(buffer[i] == 0xC0 && buffer[i + 3] == 0x01)
{
ip++;
i += 12; /* ! += buf[i+1]; */
llarp::LogDebug(" %u.%u.%u.%u\n", buffer[i], buffer[i + 1],
buffer[i + 2], buffer[i + 3]);
struct sockaddr *g_addr = new sockaddr;
g_addr->sa_family = AF_INET;
#if((__APPLE__ && __MACH__) || __FreeBSD__)
g_addr->sa_len = sizeof(in_addr);
#endif
struct in_addr *addr = &((struct sockaddr_in *)g_addr)->sin_addr;
unsigned char *ip;
// have ip point to s_addr
ip = (unsigned char *)&(addr->s_addr);
ip[0] = buffer[i + 0];
ip[1] = buffer[i + 1];
ip[2] = buffer[i + 2];
ip[3] = buffer[i + 3];
return g_addr;
}
}
if(!ip)
{
llarp::LogWarn(" No IPv4 address found in the DNS response!\n");
return nullptr;
}
}
return nullptr;
request->question.type = 1;
request->question.qClass = 1;
// register our self with the tracker
dns_tracker *tracker = request->context->tracker;
uint16_t id = ++tracker->c_requests;
tracker->client_request[id] = request;
dns_query *dns_packet = build_dns_packet((char *)request->question.name.c_str(), id, 1);
return dns_packet;
}
/// generic dnsc handler
void
llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *saddr, const void *buf,
ssize_t sz)
generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
const struct sockaddr *saddr, const void *buf,
ssize_t sz)
{
// lock_t lock(m_dnsc_Mutex);
// llarp::LogInfo("got a response, udp user is ", udp->user);
unsigned char *castBuf = (unsigned char *)buf;
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
dns_msg_header *hdr = decode_hdr((const char *)castBuf);
llarp::LogDebug("Header got client responses for id: ", hdr->id);
// if we sent this out, then there's an id
struct dns_tracker *tracker = (struct dns_tracker *)udp->user;
struct dnsc_answer_request *request = tracker->client_request[hdr->id];
if(!request)
{
llarp::LogError(
"User data to DNS Client response not a dnsc_answer_request");
"User data to DNS Client response not a dnsc_answer_request");
// we can't call back the hook
return;
}
// llarp_dnsc_unbind(request);
if(sz < 0)
{
llarp::LogWarn("Error Receiving DNS Client Response");
request->resolved(request);
return;
}
// unsigned char *castBuf = (unsigned char *)buf;
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
// hexdump("received packet", &buffer, ret);
/*
uint16_t QDCOUNT; // No. of items in Question Section
uint16_t ANCOUNT; // No. of items in Answer Section
uint16_t NSCOUNT; // No. of items in Authority Section
uint16_t ARCOUNT; // No. of items in Additional Section
uint16_t QCLASS; // Specifies the class of the query
uint16_t ATYPE; // Specifies the meaning of the data in the RDATA field
uint16_t ACLASS; // Specifies the class of the data in the RDATA field
uint32_t TTL; // The number of seconds the results can be cached
uint16_t RDLENGTH; // The length of the RDATA field
uint16_t MSGID;
*/
uint16_t QDCOUNT; // No. of items in Question Section
uint16_t ANCOUNT; // No. of items in Answer Section
uint16_t NSCOUNT; // No. of items in Authority Section
uint16_t ARCOUNT; // No. of items in Additional Section
uint16_t QCLASS; // Specifies the class of the query
uint16_t ATYPE; // Specifies the meaning of the data in the RDATA field
uint16_t ACLASS; // Specifies the class of the data in the RDATA field
uint32_t TTL; // The number of seconds the results can be cached
uint16_t RDLENGTH; // The length of the RDATA field
uint16_t MSGID;
*/
uint8_t rcode;
// int length;
// struct dns_query *dnsQuery = &request->query;
// rcode = (buffer[3] & 0x0F);
// llarp::LogInfo("dnsc rcode ", rcode);
dns_msg_header *msg = decode_hdr((const char *)castBuf);
castBuf += 12;
llarp::LogDebug("msg id ", msg->id);
@ -365,23 +192,23 @@ llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
llarp::LogDebug("msg op ", opcode);
rcode = msg->rcode;
llarp::LogDebug("msg rc ", rcode);
llarp::LogDebug("msg qdc ", msg->qdCount);
llarp::LogDebug("msg anc ", msg->anCount);
llarp::LogDebug("msg nsc ", msg->nsCount);
llarp::LogDebug("msg arc ", msg->arCount);
// we may need to parse question first
/*
dns_msg_question *question = decode_question((const char *)castBuf);
llarp::LogInfo("que name ", question->name);
castBuf += question->name.length() + 8;
dns_msg_answer *answer = decode_answer((const char *)castBuf);
castBuf += answer->name.length() + 4 + 4 + 4 + answer->rdLen;
*/
dns_msg_question *question = decode_question((const char *)castBuf);
llarp::LogInfo("que name ", question->name);
castBuf += question->name.length() + 8;
dns_msg_answer *answer = decode_answer((const char *)castBuf);
castBuf += answer->name.length() + 4 + 4 + 4 + answer->rdLen;
*/
// FIXME: only handling one atm
dns_msg_question *question = nullptr;
for(uint i = 0; i < hdr->qdCount; i++)
@ -390,7 +217,7 @@ llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
llarp::LogDebug("Read a question");
castBuf += question->name.length() + 8;
}
// FIXME: only handling one atm
dns_msg_answer *answer = nullptr;
for(uint i = 0; i < hdr->anCount; i++)
@ -407,78 +234,79 @@ llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
llarp::LogDebug("Read an authority");
castBuf += answer->name.length() + 4 + 4 + 4 + answer->rdLen;
}
// dns_msg_answer *answer2 = decode_answer((const char*)castBuf);
// castBuf += answer->name.length() + 4 + 4 + 4 + answer->rdLen;
// llarp::LogDebug("query type: %u\n", dnsQuery->reqType);
/*
QCLASS = (uint16_t)dnsQuery->request[dnsQuery->length - 2] * 0x100
+ dnsQuery->request[dnsQuery->length - 1];
llarp::LogInfo("query class: ", QCLASS);
length = dnsQuery->length + 1; // to skip 0xc00c
// printf("length [%d] from [%d]\n", length, buffer.base);
ATYPE = (uint16_t)buffer[length + 1] * 0x100 + buffer[length + 2];
llarp::LogInfo("answer type: ", ATYPE);
ACLASS = (uint16_t)buffer[length + 3] * 0x100 + buffer[length + 4];
llarp::LogInfo("answer class: ", ACLASS);
TTL = (uint32_t)buffer[length + 5] * 0x1000000 + buffer[length + 6] * 0x10000
+ buffer[length + 7] * 0x100 + buffer[length + 8];
llarp::LogInfo("seconds to cache: ", TTL);
RDLENGTH = (uint16_t)buffer[length + 9] * 0x100 + buffer[length + 10];
llarp::LogInfo("bytes in answer: ", RDLENGTH);
MSGID = (uint16_t)buffer[0] * 0x100 + buffer[1];
// llarp::LogDebug("answer msg id: %u\n", MSGID);
*/
QCLASS = (uint16_t)dnsQuery->request[dnsQuery->length - 2] * 0x100
+ dnsQuery->request[dnsQuery->length - 1];
llarp::LogInfo("query class: ", QCLASS);
length = dnsQuery->length + 1; // to skip 0xc00c
// printf("length [%d] from [%d]\n", length, buffer.base);
ATYPE = (uint16_t)buffer[length + 1] * 0x100 + buffer[length + 2];
llarp::LogInfo("answer type: ", ATYPE);
ACLASS = (uint16_t)buffer[length + 3] * 0x100 + buffer[length + 4];
llarp::LogInfo("answer class: ", ACLASS);
TTL = (uint32_t)buffer[length + 5] * 0x1000000 + buffer[length + 6] * 0x10000
+ buffer[length + 7] * 0x100 + buffer[length + 8];
llarp::LogInfo("seconds to cache: ", TTL);
RDLENGTH = (uint16_t)buffer[length + 9] * 0x100 + buffer[length + 10];
llarp::LogInfo("bytes in answer: ", RDLENGTH);
MSGID = (uint16_t)buffer[0] * 0x100 + buffer[1];
// llarp::LogDebug("answer msg id: %u\n", MSGID);
*/
llarp::Addr upstreamAddr(*request->context->server);
if(answer == nullptr)
{
llarp::LogWarn("nameserver ", SERVER, " didnt return any answers:");
llarp::LogWarn("nameserver ", upstreamAddr, " didnt return any answers:");
request->resolved(request);
return;
}
llarp::LogDebug("ans class ", answer->aClass);
llarp::LogDebug("ans type ", answer->type);
llarp::LogDebug("ans ttl ", answer->ttl);
llarp::LogDebug("ans rdlen ", answer->rdLen);
/*
llarp::LogInfo("ans2 class ", answer2->aClass);
llarp::LogInfo("ans2 type ", answer2->type);
llarp::LogInfo("ans2 ttl ", answer2->ttl);
llarp::LogInfo("ans2 rdlen ", answer2->rdLen);
*/
llarp::LogInfo("ans2 class ", answer2->aClass);
llarp::LogInfo("ans2 type ", answer2->type);
llarp::LogInfo("ans2 ttl ", answer2->ttl);
llarp::LogInfo("ans2 rdlen ", answer2->rdLen);
*/
if(rcode == 2)
{
llarp::LogWarn("nameserver ", SERVER, " returned SERVFAIL:");
llarp::LogWarn("nameserver ", upstreamAddr, " returned SERVFAIL:");
llarp::LogWarn(
" the name server was unable to process this query due to a problem "
"with the name server.");
" the name server was unable to process this query due to a problem "
"with the name server.");
request->resolved(request);
return;
}
else if(rcode == 3)
{
llarp::LogWarn("nameserver ", SERVER,
llarp::LogWarn("nameserver ", upstreamAddr,
" returned NXDOMAIN for: ", request->question.name);
llarp::LogWarn(" the domain name referenced in the query does not exist");
request->resolved(request);
return;
}
int ip = 0;
/* search for and print IPv4 addresses */
// if(dnsQuery->reqType == 0x01)
if(request->question.type == 1)
{
// llarp::LogInfo("DNS server's answer is: (type#=", ATYPE, "):");
llarp::LogDebug("IPv4 address(es) for ", request->question.name, ":");
if(answer->rdLen == 4)
{
request->result.sa_family = AF_INET;
@ -486,21 +314,21 @@ llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
request->result.sa_len = sizeof(in_addr);
#endif
struct in_addr *addr =
&((struct sockaddr_in *)&request->result)->sin_addr;
&((struct sockaddr_in *)&request->result)->sin_addr;
unsigned char *ip = (unsigned char *)&(addr->s_addr);
ip[0] = answer->rData[0];
ip[1] = answer->rData[1];
ip[2] = answer->rData[2];
ip[3] = answer->rData[3];
llarp::Addr test(request->result);
llarp::LogDebug(test);
request->found = true;
request->resolved(request);
return;
}
if(!ip)
{
llarp::LogWarn(" No IPv4 address found in the DNS answer!");
@ -510,24 +338,114 @@ llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
}
}
void
raw_resolve_host(struct dnsc_context *dnsc, const char *url,
dnsc_answer_hook_func resolved, void *user)
{
dns_query *dns_packet = answer_request_alloc(dnsc, nullptr, url, resolved, user);
if (!dns_packet)
{
llarp::LogError("Couldn't make dnsc packet");
return;
}
// char *word;
llarp::Addr upstreamAddr(*dnsc->server);
llarp::LogDebug("Asking DNS server ", upstreamAddr, " about ", url);
struct sockaddr_in addr;
ssize_t ret;
socklen_t size;
// int length;
unsigned char buffer[DNC_BUF_SIZE];
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
llarp::LogWarn("Error creating socket!\n");
return;
}
// socket = sockfd;
sockaddr_in *dnscSock = ((sockaddr_in *)dnsc->server);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = dnscSock->sin_addr.s_addr;
addr.sin_port = dnscSock->sin_port;
size = sizeof(addr);
//hexdump("sending packet", &dnsQuery.request, dnsQuery.length);
ret = sendto(sockfd, dns_packet->request, dns_packet->length, 0,
(struct sockaddr *)&addr, size);
delete dns_packet;
if(ret < 0)
{
llarp::LogWarn("Error Sending Request");
return;
}
llarp::LogInfo("Sent");
memset(&buffer, 0, DNC_BUF_SIZE);
llarp::LogInfo("Waiting for recv");
ret = recvfrom(sockfd, buffer, DNC_BUF_SIZE, 0, (struct sockaddr *)&addr,
&size);
llarp::LogInfo("recv done ", size);
if(ret < 0)
{
llarp::LogWarn("Error Receiving Response");
return;
}
llarp::LogInfo("closing new socket\n");
// hexdump("received packet", &buffer, ret);
close(sockfd);
unsigned char *castBuf = (unsigned char *)buffer;
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
dns_msg_header *hdr = decode_hdr((const char *)castBuf);
llarp::LogInfo("response header says it belongs to id #", hdr->id);
// if we sent this out, then there's an id
struct dns_tracker *tracker = (struct dns_tracker *)dnsc->tracker;
struct dnsc_answer_request *request = tracker->client_request[hdr->id];
generic_handle_dnsc_recvfrom(request, nullptr, castBuf, size);
}
/// intermediate udp_io handler
void
llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *saddr, const void *buf,
ssize_t sz)
{
unsigned char *castBuf = (unsigned char *)buf;
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
dns_msg_header *hdr = decode_hdr((const char *)castBuf);
llarp::LogDebug("Header got client responses for id: ", hdr->id);
// if we sent this out, then there's an id
struct dns_tracker *tracker = (struct dns_tracker *)udp->user;
struct dnsc_answer_request *request = tracker->client_request[hdr->id];
generic_handle_dnsc_recvfrom(request, saddr, buf, sz);
}
bool
llarp_resolve_host(struct dnsc_context *dnsc, const char *url,
dnsc_answer_hook_func resolved, void *user)
{
dnsc_answer_request *request = new dnsc_answer_request;
request->sock = (void *)&dnsc->udp;
request->user = user;
request->resolved = resolved;
request->found = false;
request->context = dnsc;
char *sUrl = strdup(url);
request->question.name = sUrl;
request->question.type = 1;
request->question.qClass = 1;
dns_query *dns_packet = answer_request_alloc(dnsc, &dnsc->udp, url, resolved, user);
if (!dns_packet)
{
llarp::LogError("Couldn't make dnsc packet");
return false;
}
// register request with udp response tracker
dns_tracker *tracker = (dns_tracker *)dnsc->udp->user;
//dns_tracker *tracker = (dns_tracker *)dnsc->udp->user;
/*
uint16_t length = 0;
@ -561,12 +479,11 @@ llarp_resolve_host(struct dnsc_context *dnsc, const char *url,
memcpy(bytes + 12, &request->question, qLen);
*/
uint16_t id = ++tracker->c_requests;
tracker->client_request[id] = request;
//uint16_t id = ++tracker->c_requests;
//tracker->client_request[id] = request;
// llarp::LogInfo("Sending request #", tracker->c_requests, " ", length, "
// bytes");
dns_query *dns_packet = build_dns_packet((char *)url, id, 1);
// ssize_t ret = llarp_ev_udp_sendto(dnsc->udp, dnsc->server, bytes, length);
ssize_t ret = llarp_ev_udp_sendto(dnsc->udp, dnsc->server,
dns_packet->request, dns_packet->length);
@ -596,6 +513,7 @@ llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_udp_io *udp,
trgaddr->sin_family = AF_INET;
dnsc->server = (sockaddr *)trgaddr;
dnsc->udp = udp;
dnsc->tracker = &dns_udp_tracker;
return true;
}

View File

@ -9,11 +9,12 @@
struct dnsc_answer_request;
/// hook function to handle an dns client request
// should we pass by llarp::Addr
// not as long as we're supporting raw
typedef void (*dnsc_answer_hook_func)(dnsc_answer_request *request);
// FIXME: separate generic from llarp
/// struct for dns client requests
struct dnsc_answer_request
{
/// sock type
@ -31,30 +32,52 @@ struct dnsc_answer_request
struct dnsc_context *context;
};
/// event handler for processing DNS responses
void
llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *saddr, const void *buf,
ssize_t sz);
/// generic handler for processing DNS responses
void
raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr, const void *buf,
ssize_t sz);
/// DNS client context (one needed per upstream DNS server)
struct dnsc_context
{
/// Target: DNS server hostname/port to use
// FIXME: ipv6 it
sockaddr *server;
/// tracker
struct dns_tracker *tracker;
/// sock type
void *sock;
// where to create the new sockets
struct llarp_udp_io *udp;
};
/// async resolve a hostname using generic socks
void
raw_resolve_host(struct dnsc_context *dnsc, const char *url,
dnsc_answer_hook_func resolved, void *user);
/// async resolve a hostname using llarp platform framework
bool
llarp_resolve_host(struct dnsc_context *dns, const char *url,
dnsc_answer_hook_func resolved, void *user);
/// cleans up request structure allocations
void
llarp_host_resolved(dnsc_answer_request *request);
/// initialize dns subsystem and bind socket
/// returns true on bind success otherwise returns false
bool
llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_udp_io *udp,
const char *dnsc_hostname, uint16_t dnsc_port);
/// shutdowns any events, and deallocates for this context
bool
llarp_dnsc_stop(struct dnsc_context *dnsc);

View File

@ -5,7 +5,7 @@
#include "llarp/net.hpp"
#include "logger.hpp"
dns_tracker dns_udp_tracker;
extern dns_tracker dns_udp_tracker;
ssize_t
raw_sendto_dns_hook_func(void *sock, const struct sockaddr *from,
@ -35,12 +35,6 @@ llarp_sendto_dns_hook_func(void *sock, const struct sockaddr *from,
return llarp_ev_udp_sendto(udp, from, buffer, length);
}
bool
forward_dns_request(std::string request)
{
return true;
}
// FIXME: we need an DNS answer not a sockaddr
// otherwise ttl, type and class can't be relayed correctly
void
@ -55,7 +49,7 @@ writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr *from,
return;
}
const size_t BUFFER_SIZE = 1024;
const size_t BUFFER_SIZE = 1024 + (request->question.name.size() * 2);
char buf[BUFFER_SIZE];
memset(buf, 0, BUFFER_SIZE);
char *write_buffer = buf;
@ -121,7 +115,6 @@ void
handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from,
dnsd_question_request *request)
{
// lock_t lock(m_dnsd_Mutex);
const size_t HDR_OFFSET = 12;
const char *p_buffer = buffer;
@ -129,12 +122,10 @@ handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from,
llarp::LogDebug("dnsd rcode ", rcode);
dns_msg_header *msg = decode_hdr(p_buffer);
// llarp::LogInfo("DNS_MSG size", sizeof(dns_msg));
p_buffer += HDR_OFFSET;
request->id = msg->id;
std::string m_qName = "";
int length = *p_buffer++;
// llarp::LogInfo("qNamLen", length);
while(length != 0)
{
for(int i = 0; i < length; i++)
@ -150,9 +141,6 @@ handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from,
request->question.type = get16bits(p_buffer);
request->question.qClass = get16bits(p_buffer);
// request->m_qName = m_qName;
// request->m_qType = request->question.type;
// request->m_qClass = request->question.qClass;
llarp::LogDebug("qName ", request->question.name);
llarp::LogDebug("qType ", request->question.type);
llarp::LogDebug("qClass ", request->question.qClass);
@ -167,7 +155,6 @@ handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from,
if(request->context->intercept)
{
sockaddr *intercept = request->context->intercept(request->question.name, request->context);
// if(!forward_dns_request(m_qName))
if(intercept != nullptr)
{
// told that hook will handle overrides
@ -177,52 +164,61 @@ handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from,
}
}
sockaddr *hostRes = nullptr;
// FIXME: check request's context
if (!request->context)
{
llarp::LogError("dnsd request context was not a dnsd context");
sockaddr *fromCopy = new sockaddr(*from);
writesend_dnss_response(nullptr, fromCopy, request);
return;
}
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;
}
if(request->llarp)
{
// llarp::Addr anIp;
// llarp::LogInfo("Checking server request ", request);
struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
struct dns_tracker *tracker = (struct dns_tracker *)udp->user;
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());
// make async request
llarp_resolve_host(&dnsd->client, m_qName.c_str(), &handle_dnsc_result,
(void *)request);
}
else
{
hostRes = raw_resolve_host(m_qName.c_str());
llarp::Addr anIp(*hostRes);
llarp::LogDebug("DNSc got ", anIp);
// 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);
// make raw/sync request
raw_resolve_host(&dnsd->client, m_qName.c_str(), &handle_dnsc_result,
(void *)request);
}
}
void
llarp_handle_dnsd_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *paddr, const void *buf,
const struct sockaddr *saddr, const void *buf,
ssize_t sz)
{
// lock_t lock(m_dnsd3_Mutex);
// llarp_link *link = static_cast< llarp_link * >(udp->user);
llarp::LogDebug("llarp Received Bytes ", sz);
if (!dns_udp_tracker.dnsd)
{
llarp::LogError("No tracker set in dnsd context");
return;
}
// create new request
dnsd_question_request *llarp_dns_request = new dnsd_question_request;
// llarp::LogInfo("Creating server request ", &llarp_dns_request);
// llarp::LogInfo("Server UDP address ", udp);
llarp_dns_request->context = dns_udp_tracker.dnsd;
// make a copy of the sockaddr
llarp_dns_request->from = new sockaddr(*paddr);
llarp_dns_request->user = (void *)udp;
llarp_dns_request->llarp = true;
// set sock hook
llarp_dns_request->hook = &llarp_sendto_dns_hook_func;
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->hook = &llarp_sendto_dns_hook_func; // set sock hook
// llarp::LogInfo("Server request's UDP ", llarp_dns_request->user);
handle_recvfrom((char *)buf, sz, llarp_dns_request->from, llarp_dns_request);
@ -232,13 +228,18 @@ void
raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr, const void *buf,
ssize_t sz)
{
llarp::LogInfo("raw Received Bytes ", sz);
if (!dns_udp_tracker.dnsd)
{
llarp::LogError("No tracker set in dnsd context");
return;
}
dnsd_question_request *llarp_dns_request = new dnsd_question_request;
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;
handle_recvfrom((char *)buf, sz, saddr, llarp_dns_request);
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->hook = &raw_sendto_dns_hook_func;
handle_recvfrom((char *)buf, sz, llarp_dns_request->from, llarp_dns_request);
}
bool
@ -256,8 +257,9 @@ llarp_dnsd_init(struct dnsd_context *dnsd, struct llarp_ev_loop *netloop,
dnsd->udp.tick = nullptr;
dns_udp_tracker.dnsd = dnsd;
dnsd->intercept = nullptr;
dnsd->tracker = &dns_udp_tracker; // register global tracker with context
dnsd->intercept = nullptr; // set default intercepter
// configure dns client
if(!llarp_dnsc_init(&dnsd->client, &dnsd->udp, dnsc_hostname, dnsc_port))
@ -265,7 +267,14 @@ llarp_dnsd_init(struct dnsd_context *dnsd, struct llarp_ev_loop *netloop,
llarp::LogError("Couldnt init dns client");
return false;
}
return llarp_ev_add_udp(netloop, &dnsd->udp, (const sockaddr *)&bindaddr)
if (netloop)
{
return llarp_ev_add_udp(netloop, &dnsd->udp, (const sockaddr *)&bindaddr)
!= -1;
}
else
{
return true;
}
}

View File

@ -6,11 +6,14 @@
#include "dns.hpp" // question and dnsc
#include "dnsc.hpp"
// fwd declaration
struct dnsd_context;
/// sendto hook functor
typedef ssize_t (*sendto_dns_hook_func)(void *sock, const struct sockaddr *from,
const void *buffer, size_t length);
/// DNS server query request
struct dnsd_question_request
{
/// sock type
@ -28,14 +31,19 @@ struct dnsd_question_request
dnsd_context *context; // or you can access it via user (udp)
};
/// intercept query hook functor
// we could have passed in the source sockaddr in case you wanted to
// handle the response yourself
typedef sockaddr *(*intercept_query_hook)(std::string name, struct dnsd_context *context);
/// DNS Server context
struct dnsd_context
{
/// DNS daemon socket to listen on
struct llarp_udp_io udp;
/// tracker
struct dns_tracker *tracker;
/// upstream DNS client context to use
dnsc_context client;
/// custom data for intercept query hook
void *user;
@ -43,9 +51,10 @@ struct dnsd_context
intercept_query_hook intercept;
};
/// udp event handler
void
llarp_handle_dnsd_recvfrom(struct llarp_udp_io *udp,
const struct sockaddr *paddr, const void *buf,
const struct sockaddr *saddr, const void *buf,
ssize_t sz);
/// initialize dns subsystem and bind socket
@ -55,6 +64,7 @@ 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);
/// shutdowns any events, and deallocates for this context
bool
llarp_dnsd_stop(struct dnsd_context *dnsd);