mirror of https://github.com/oxen-io/lokinet
"fixed" DNS multithreaded
This commit is contained in:
parent
1d77bb9acc
commit
eadca4b4fc
|
@ -42,42 +42,14 @@ main(int argc, char *argv[])
|
|||
llarp_ev_loop_alloc(&netloop);
|
||||
|
||||
struct dnsd_context dnsd;
|
||||
if (!llarp_dnsd_init(&dnsd, netloop, "*", 53, SERVER, PORT))
|
||||
if (!llarp_dnsd_init(&dnsd, netloop, "*", 1053, SERVER, PORT))
|
||||
{
|
||||
llarp::LogError("Couldnt init dns daemon");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
// configure main netloop
|
||||
llarp_udp_io *udp = new llarp_udp_io;
|
||||
|
||||
llarp::Addr p_addr;
|
||||
sockaddr_in ip4addr;
|
||||
sockaddr *addr = nullptr;
|
||||
addr = (sockaddr *)&ip4addr; // point addr to ip4addr
|
||||
llarp::Zero(addr, sizeof(sockaddr_in)); // zero out ip4addr
|
||||
addr->sa_family = AF_INET;
|
||||
|
||||
// FIXME: make configureable
|
||||
ip4addr.sin_port = htons(1053);
|
||||
p_addr = *addr; // copy addr into llarp address
|
||||
udp->user = nullptr;
|
||||
udp->tick = nullptr;
|
||||
udp->recvfrom = &llarp_handle_recvfrom;
|
||||
llarp::LogDebug("bind DNS Server to ", addr);
|
||||
if(llarp_ev_add_udp(netloop, udp, p_addr) == -1)
|
||||
{
|
||||
llarp::LogError("failed to bind to ", addr);
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
//struct dnsc_context *dns_clinet_context = new dnsc_context;
|
||||
//llarp_dnsc_init(dns_clinet_context, netloop, SERVER, PORT);
|
||||
|
||||
|
||||
// singlethreaded
|
||||
if (1)
|
||||
if (0)
|
||||
{
|
||||
llarp::LogInfo("singlethread start");
|
||||
worker = llarp_init_same_process_threadpool();
|
||||
|
@ -87,12 +59,13 @@ main(int argc, char *argv[])
|
|||
}
|
||||
else
|
||||
{
|
||||
llarp::LogInfo("multithreaded start");
|
||||
// create 2 workers
|
||||
worker = llarp_init_threadpool(2, "llarp-worker");
|
||||
uint num_llarpworkers = 2;
|
||||
uint num_nethreads = 8;
|
||||
llarp::LogInfo("multithreaded start with ", num_llarpworkers, " llarp-workers and ", num_nethreads, " networkers");
|
||||
// create workers
|
||||
worker = llarp_init_threadpool(num_llarpworkers, "llarp-worker");
|
||||
logic = llarp_init_logic();
|
||||
auto netio = netloop;
|
||||
int num_nethreads = 2;
|
||||
std::vector< std::thread > netio_threads;
|
||||
while(num_nethreads--)
|
||||
{
|
||||
|
|
|
@ -14,12 +14,67 @@
|
|||
typedef std::mutex mtx_t;
|
||||
typedef std::lock_guard< mtx_t > lock_t;
|
||||
|
||||
#include <string>
|
||||
struct dns_msg_header
|
||||
{
|
||||
uint id;
|
||||
uint qr;
|
||||
uint opcode;
|
||||
uint aa;
|
||||
uint tc;
|
||||
uint rd;
|
||||
uint ra;
|
||||
uint rcode;
|
||||
|
||||
uint qdCount;
|
||||
uint anCount;
|
||||
uint nsCount;
|
||||
uint arCount;
|
||||
};
|
||||
|
||||
struct dns_msg_question
|
||||
{
|
||||
std::string name;
|
||||
uint type;
|
||||
uint qClass;
|
||||
};
|
||||
|
||||
struct dns_msg_answer
|
||||
{
|
||||
std::string name;
|
||||
uint type;
|
||||
uint aClass;
|
||||
u_int32_t ttl;
|
||||
uint rdLen;
|
||||
uint rData;
|
||||
};
|
||||
|
||||
// fwd declr
|
||||
struct dns_query;
|
||||
struct dnsc_context;
|
||||
struct dnsd_context;
|
||||
struct dns_request;
|
||||
struct dns_client_request;
|
||||
|
||||
typedef bool (*intercept_query_hook)(struct dnsc_context *, const dns_query *);
|
||||
|
||||
// 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
|
||||
|
||||
#include <map>
|
||||
|
||||
struct dns_tracker
|
||||
{
|
||||
uint c_responses;
|
||||
uint c_requests;
|
||||
std::map<uint, dns_client_request *> client_request;
|
||||
dnsd_context *dnsd;
|
||||
std::map<uint, dns_request *> daemon_request;
|
||||
};
|
||||
|
||||
struct dnsc_context
|
||||
{
|
||||
/// DNS server hostname to use
|
||||
|
@ -30,8 +85,9 @@ struct dnsc_context
|
|||
// FIXME: ipv6 it
|
||||
sockaddr *server;
|
||||
// where to create the new sockets
|
||||
struct llarp_ev_loop *netloop;
|
||||
//struct llarp_ev_loop *netloop;
|
||||
// FIXME: UDP socket pooling (or maybe at the libev level)
|
||||
struct llarp_udp_io *udp;
|
||||
};
|
||||
|
||||
struct dnsd_context
|
||||
|
@ -48,7 +104,7 @@ struct dnsd_context
|
|||
/// initialize dns subsystem and bind socket
|
||||
/// returns true on bind success otherwise returns false
|
||||
bool
|
||||
llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_ev_loop *netloop,
|
||||
llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_udp_io *udp,
|
||||
const char *dnsc_hostname, uint16_t dnsc_port);
|
||||
|
||||
bool
|
||||
|
@ -58,7 +114,7 @@ bool
|
|||
llarp_dnsc_unbind(struct dns_client_request *request);
|
||||
|
||||
bool
|
||||
llarp_dnsc_bind(struct llarp_ev_loop *netloop, struct dns_client_request *request);
|
||||
llarp_dnsc_bind(struct llarp_udp_io *udp, struct dns_client_request *request);
|
||||
|
||||
/// initialize dns subsystem and bind socket
|
||||
/// returns true on bind success otherwise returns false
|
||||
|
@ -100,6 +156,7 @@ struct dns_client_request
|
|||
/// Source: UDP port to use
|
||||
// FIXME: separate socket for now
|
||||
struct llarp_udp_io udp;
|
||||
// maybe a link to dnsc_context
|
||||
};
|
||||
|
||||
bool
|
||||
|
@ -112,6 +169,14 @@ bool llarp_resolve_host(struct dnsc_context *dns, const char *url,
|
|||
void
|
||||
llarp_host_resolved(dns_client_request *request);
|
||||
|
||||
void
|
||||
llarp_handle_recvfrom(struct llarp_udp_io *udp, const struct sockaddr *paddr,
|
||||
const void *buf, ssize_t sz);
|
||||
|
||||
void
|
||||
llarp_handle_dns_recvfrom(struct llarp_udp_io *udp, const struct sockaddr *saddr,
|
||||
const void *buf, ssize_t sz);
|
||||
|
||||
void
|
||||
llarp_handle_recvfrom(struct llarp_udp_io *udp, const struct sockaddr *saddr,
|
||||
const void *buf, ssize_t sz);
|
||||
|
|
179
llarp/dnsc.cpp
179
llarp/dnsc.cpp
|
@ -21,6 +21,9 @@
|
|||
#define SERVER "8.8.8.8"
|
||||
#define PORT 53
|
||||
|
||||
mtx_t m_dnsc_Mutex;
|
||||
mtx_t m_dnsc2_Mutex;
|
||||
|
||||
struct sockaddr *resolveHost(const char *url) {
|
||||
char* sUrl = strdup(url);
|
||||
struct dns_query dnsQuery;
|
||||
|
@ -43,7 +46,7 @@ struct sockaddr *resolveHost(const char *url) {
|
|||
|
||||
char * word;
|
||||
unsigned int i;
|
||||
llarp::LogDebug("Asking DNS server %s about %s\n", SERVER, url);
|
||||
llarp::LogDebug("Asking DNS server ", SERVER, " about ", url);
|
||||
//dnsQuery.reqType = 0x01;
|
||||
|
||||
word = strtok(sUrl, ".");
|
||||
|
@ -103,7 +106,7 @@ struct sockaddr *resolveHost(const char *url) {
|
|||
llarp::LogWarn("Error Sending Request");
|
||||
return nullptr;
|
||||
}
|
||||
//printf("Sent\n");
|
||||
//llarp::LogInfo("Sent\n");
|
||||
|
||||
memset(&buffer, 0, DNC_BUF_SIZE);
|
||||
ret = recvfrom(sockfd, buffer, DNC_BUF_SIZE, 0, (struct sockaddr*)&addr, &size);
|
||||
|
@ -199,24 +202,36 @@ void
|
|||
llarp_handle_dnsclient_recvfrom(struct llarp_udp_io *udp, 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);
|
||||
struct dns_client_request *request = (struct dns_client_request *)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::LogInfo("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 dns_client_request *request = tracker->client_request[hdr->id];
|
||||
|
||||
if (!request)
|
||||
{
|
||||
llarp::LogError("User data to DNS Client response not a dns_client_request");
|
||||
// we can't call back the hook
|
||||
return;
|
||||
}
|
||||
llarp_dnsc_unbind(request);
|
||||
//llarp_dnsc_unbind(request);
|
||||
|
||||
if (sz < 0) {
|
||||
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);
|
||||
//unsigned char *castBuf = (unsigned char *)buf;
|
||||
//auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
|
||||
|
||||
//hexdump("received packet", &buffer, ret);
|
||||
|
||||
|
@ -236,7 +251,15 @@ llarp_handle_dnsclient_recvfrom(struct llarp_udp_io *udp, const struct sockaddr
|
|||
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::LogInfo("msg id ", msg->id);
|
||||
llarp::LogInfo("msg qr ", msg->qr);
|
||||
llarp::LogInfo("msg op ", msg->opcode);
|
||||
llarp::LogInfo("msg rc ", msg->rcode);
|
||||
|
||||
//tempBuf[0] = buffer[4];
|
||||
//tempBuf[1] = buffer[5];
|
||||
//tempBuf[2] = '\0';
|
||||
|
@ -245,37 +268,69 @@ llarp_handle_dnsclient_recvfrom(struct llarp_udp_io *udp, const struct sockaddr
|
|||
|
||||
//QDCOUNT = (uint16_t) strtol(tempBuf, NULL, 16);
|
||||
QDCOUNT = (uint16_t) buffer[4] * 0x100 + buffer[5];
|
||||
//llarp::LogDebug("entries in question section: %u\n", QDCOUNT);
|
||||
llarp::LogInfo("entries in question section: ", QDCOUNT);
|
||||
ANCOUNT = (uint16_t) buffer[6] * 0x100 + buffer[7];
|
||||
//llarp::LogDebug("records in answer section: %u\n", ANCOUNT);
|
||||
llarp::LogInfo("records in answer section: ", ANCOUNT);
|
||||
NSCOUNT = (uint16_t) buffer[8] * 0x100 + buffer[9];
|
||||
//llarp::LogDebug("name server resource record count: %u\n", NSCOUNT);
|
||||
llarp::LogInfo("name server resource record count: ", NSCOUNT);
|
||||
ARCOUNT = (uint16_t) buffer[10] * 0x100 + buffer[11];
|
||||
//llarp::LogDebug("additional records count: %u\n", ARCOUNT);
|
||||
llarp::LogInfo("additional records count: ", ARCOUNT);
|
||||
|
||||
llarp::LogInfo("msg qdc ", msg->qdCount);
|
||||
llarp::LogInfo("msg anc ", msg->anCount);
|
||||
llarp::LogInfo("msg nsc ", msg->nsCount);
|
||||
llarp::LogInfo("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_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::LogDebug("query class: %u\n", QCLASS);
|
||||
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::LogDebug("answer type: %u\n", ATYPE);
|
||||
llarp::LogInfo("answer type: ", ATYPE);
|
||||
ACLASS = (uint16_t) buffer[length + 3] * 0x100 + buffer[length + 4];
|
||||
//llarp::LogDebug("answer class: %u\n", ACLASS);
|
||||
llarp::LogInfo("answer class: ", 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);
|
||||
llarp::LogInfo("seconds to cache: ", TTL);
|
||||
RDLENGTH = (uint16_t) buffer[length + 9] * 0x100 + buffer[length + 10];
|
||||
//llarp::LogDebug("bytes in answer: %u\n", RDLENGTH);
|
||||
llarp::LogInfo("bytes in answer: ", RDLENGTH);
|
||||
|
||||
MSGID = (uint16_t) buffer[0] * 0x100 + buffer[1];
|
||||
//llarp::LogDebug("answer msg id: %u\n", MSGID);
|
||||
|
||||
llarp::LogInfo("ans class ", answer->aClass);
|
||||
llarp::LogInfo("ans type ", answer->type);
|
||||
llarp::LogInfo("ans ttl ", answer->ttl);
|
||||
llarp::LogInfo("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);
|
||||
*/
|
||||
|
||||
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");
|
||||
llarp::LogWarn("nameserver ", SERVER, " returned SERVFAIL:");
|
||||
llarp::LogWarn(" 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 %s returned NXDOMAIN for %s:\n", SERVER, dnsQuery->url);
|
||||
llarp::LogWarn(" the domain name referenced in the query does not exist\n");
|
||||
llarp::LogWarn("nameserver ", SERVER, " returned NXDOMAIN for: ", dnsQuery->url);
|
||||
llarp::LogWarn(" the domain name referenced in the query does not exist");
|
||||
request->resolved(request);
|
||||
return;
|
||||
}
|
||||
|
@ -284,8 +339,8 @@ llarp_handle_dnsclient_recvfrom(struct llarp_udp_io *udp, const struct sockaddr
|
|||
|
||||
/* search for and print IPv4 addresses */
|
||||
if (dnsQuery->reqType == 0x01) {
|
||||
llarp::LogInfo("DNS server's answer is: (type#=%u):", ATYPE);
|
||||
printf("IPv4 address(es) for %s:\n", dnsQuery->url);
|
||||
llarp::LogInfo("DNS server's answer is: (type#=", ATYPE, "):");
|
||||
llarp::LogInfo("IPv4 address(es) for ", dnsQuery->url, ":");
|
||||
for (unsigned int i = 0 ; i < sz ; i++) {
|
||||
if (buffer[i] == 0xC0 && buffer[i+3] == 0x01) {
|
||||
ip++; i += 12; /* ! += buf[i+1]; */
|
||||
|
@ -313,7 +368,7 @@ llarp_handle_dnsclient_recvfrom(struct llarp_udp_io *udp, const struct sockaddr
|
|||
ip[2]=buffer[i + 2];
|
||||
ip[3]=buffer[i + 3];
|
||||
llarp::Addr test(request->result);
|
||||
llarp::LogInfo("IP Address ", test);
|
||||
llarp::LogInfo(test);
|
||||
|
||||
//return g_addr;
|
||||
//request->result = g_addr;
|
||||
|
@ -324,37 +379,83 @@ llarp_handle_dnsclient_recvfrom(struct llarp_udp_io *udp, const struct sockaddr
|
|||
}
|
||||
|
||||
if (!ip) {
|
||||
llarp::LogWarn(" No IPv4 address found in the DNS response!\n");
|
||||
llarp::LogWarn(" No IPv4 address found in the DNS response!");
|
||||
request->resolved(request);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void build_dns_query(struct dns_query *dnsQuery)
|
||||
void
|
||||
llarp_handle_dns_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);
|
||||
//castBuf += 12;
|
||||
llarp::LogInfo("msg id ", hdr->id);
|
||||
llarp::LogInfo("msg qr ", hdr->qr);
|
||||
if (hdr->qr)
|
||||
{
|
||||
llarp_handle_dnsclient_recvfrom(udp, saddr, buf, sz);
|
||||
} else {
|
||||
llarp_handle_recvfrom(udp, saddr, buf, sz);
|
||||
}
|
||||
/*
|
||||
llarp::LogInfo("msg op ", hdr->opcode);
|
||||
llarp::LogInfo("msg rc ", hdr->rcode);
|
||||
|
||||
for(uint i = 0; i < hdr->qdCount; i++)
|
||||
{
|
||||
dns_msg_question *question = decode_question((const char*)castBuf);
|
||||
llarp::LogInfo("Read a question");
|
||||
castBuf += question->name.length() + 8;
|
||||
}
|
||||
|
||||
for(uint i = 0; i < hdr->anCount; i++)
|
||||
{
|
||||
dns_msg_answer *answer = decode_answer((const char*)castBuf);
|
||||
llarp::LogInfo("Read an answer");
|
||||
castBuf += answer->name.length() + 4 + 4 + 4 + answer->rdLen;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void build_dns_query(struct dns_query *dnsQuery, uint id)
|
||||
{
|
||||
dnsQuery->length = 12;
|
||||
dnsQuery->reqType = 0x01;
|
||||
dnsQuery->request[ 0] = 0xDB;
|
||||
dnsQuery->request[ 1] = 0x42;
|
||||
// ID
|
||||
//buffer[0] = (value & 0xFF00) >> 8;
|
||||
//buffer[1] = value & 0xFF;
|
||||
llarp::LogInfo("building request ", id);
|
||||
|
||||
dnsQuery->request[ 0] = (id & 0xFF00) >> 8;
|
||||
dnsQuery->request[ 1] = (id & 0x00FF) >> 0;
|
||||
// field
|
||||
dnsQuery->request[ 2] = 0x01;
|
||||
dnsQuery->request[ 3] = 0x00;
|
||||
// questions
|
||||
dnsQuery->request[ 4] = 0x00;
|
||||
dnsQuery->request[ 5] = 0x01;
|
||||
// answers
|
||||
dnsQuery->request[ 6] = 0x00;
|
||||
dnsQuery->request[ 7] = 0x00;
|
||||
// ns
|
||||
dnsQuery->request[ 8] = 0x00;
|
||||
dnsQuery->request[ 9] = 0x00;
|
||||
// ar
|
||||
dnsQuery->request[10] = 0x00;
|
||||
dnsQuery->request[11] = 0x00;
|
||||
|
||||
char * word;
|
||||
//llarp::LogDebug("Asking DNS server %s about %s\n", SERVER, dnsQuery->url);
|
||||
//llarp::LogDebug("Asking DNS server %s about %s", SERVER, dnsQuery->url);
|
||||
|
||||
char * strTemp = strdup(dnsQuery->url);
|
||||
word = strtok(strTemp, ".");
|
||||
while (word) {
|
||||
//llarp::LogDebug("parsing hostname: \"%s\" is %zu characters\n", word, strlen(word));
|
||||
//llarp::LogDebug("parsing hostname: \"%s\" is %zu characters", word, strlen(word));
|
||||
dnsQuery->request[dnsQuery->length++] = strlen(word);
|
||||
for (unsigned int i = 0; i < strlen(word); i++) {
|
||||
dnsQuery->request[dnsQuery->length++] = word[i];
|
||||
|
@ -380,20 +481,24 @@ bool llarp_resolve_host(struct dnsc_context *dnsc, const char *url, resolve_dns_
|
|||
request->found = false;
|
||||
|
||||
// request address holds through the packet recv
|
||||
request->udp.user = request;
|
||||
//request->udp.user = request;
|
||||
|
||||
// finish setting up our query
|
||||
struct dns_query *dnsQuery = &request->query;
|
||||
build_dns_query(dnsQuery);
|
||||
dns_tracker *tracker = (dns_tracker *)dnsc->udp->user;
|
||||
build_dns_query(dnsQuery, ++tracker->c_requests);
|
||||
|
||||
// build a socket per request because we're multithreaded and will have many inflight
|
||||
/*
|
||||
if (!llarp_dnsc_bind(dnsc->netloop, request))
|
||||
{
|
||||
llarp::LogWarn("Error binding");
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
tracker->client_request[tracker->c_requests] = request;
|
||||
|
||||
ssize_t ret = llarp_ev_udp_sendto(&request->udp, dnsc->server, dnsQuery->request, dnsQuery->length);
|
||||
ssize_t ret = llarp_ev_udp_sendto(dnsc->udp, dnsc->server, dnsQuery->request, dnsQuery->length);
|
||||
if (ret < 0) {
|
||||
llarp::LogWarn("Error Sending Request");
|
||||
return false;
|
||||
|
@ -411,14 +516,18 @@ llarp_host_resolved(dns_client_request *request)
|
|||
bool
|
||||
llarp_dnsc_bind(struct llarp_ev_loop *netloop, struct dns_client_request *request)
|
||||
{
|
||||
lock_t lock(m_dnsc2_Mutex);
|
||||
// just grab a random open port
|
||||
struct sockaddr_in srcaddr;
|
||||
srcaddr.sin_addr.s_addr = inet_addr("0.0.0.0");
|
||||
srcaddr.sin_family = AF_INET;
|
||||
// we'll need to make sure the port is >1024 if not root
|
||||
//llarp::LogInfo("outgoing port check ", ntohs(srcaddr.sin_port));
|
||||
if (ntohs(srcaddr.sin_port) < 1024)
|
||||
srcaddr.sin_port = htons(ntohs(srcaddr.sin_port) + 1024);
|
||||
|
||||
request->udp.user = request;
|
||||
request->udp.recvfrom = &llarp_handle_dnsclient_recvfrom;
|
||||
request->udp.recvfrom = &llarp_handle_dns_recvfrom;
|
||||
request->udp.tick = nullptr;
|
||||
|
||||
// create new listener for this request
|
||||
|
@ -434,7 +543,7 @@ llarp_dnsc_unbind(struct dns_client_request *request)
|
|||
}
|
||||
|
||||
bool
|
||||
llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_ev_loop *netloop,
|
||||
llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_udp_io *udp,
|
||||
const char *dnsc_hostname, uint16_t dnsc_port)
|
||||
{
|
||||
sockaddr_in *trgaddr = new sockaddr_in;
|
||||
|
@ -442,7 +551,7 @@ llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_ev_loop *netloop,
|
|||
trgaddr->sin_port = htons(dnsc_port);
|
||||
trgaddr->sin_family = AF_INET;
|
||||
dnsc->server = (sockaddr *)trgaddr;
|
||||
dnsc->netloop = netloop;
|
||||
dnsc->udp = udp;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
100
llarp/dnsd.cpp
100
llarp/dnsd.cpp
|
@ -4,6 +4,10 @@
|
|||
#include "ev.hpp"
|
||||
#include <string>
|
||||
|
||||
mtx_t m_dnsd_Mutex;
|
||||
mtx_t m_dnsd2_Mutex;
|
||||
mtx_t m_dnsd3_Mutex;
|
||||
|
||||
int get16bits(const char*& buffer) throw ()
|
||||
{
|
||||
int value = static_cast<unsigned char> (buffer[0]);
|
||||
|
@ -13,6 +17,17 @@ int get16bits(const char*& buffer) throw ()
|
|||
return value;
|
||||
}
|
||||
|
||||
// uint32_t
|
||||
uint32_t get32bits(const char*& buffer) throw ()
|
||||
{
|
||||
uint32_t value = uint32_t((unsigned char)(buffer[0]) << 24 |
|
||||
(unsigned char)(buffer[1]) << 16 |
|
||||
(unsigned char)(buffer[2]) << 8 |
|
||||
(unsigned char)(buffer[3]));
|
||||
buffer += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
void put16bits(char*& buffer, uint value) throw ()
|
||||
{
|
||||
buffer[0] = (value & 0xFF00) >> 8;
|
||||
|
@ -29,18 +44,21 @@ void put32bits(char*& buffer, unsigned long value) throw ()
|
|||
buffer += 4;
|
||||
}
|
||||
|
||||
dns_msg *decode_hdr(const char *buffer)
|
||||
dns_msg_header *decode_hdr(const char *buffer)
|
||||
{
|
||||
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;
|
||||
|
||||
dns_msg_header *hdr = new dns_msg_header;
|
||||
hdr->id = get16bits(buffer);
|
||||
uint fields = get16bits(buffer);
|
||||
//hdr->qr = fields & 0x8000;
|
||||
hdr->qr = (fields >> 7)& 0x1;
|
||||
hdr->opcode = fields & 0x7800;
|
||||
hdr->aa = fields & 0x0400;
|
||||
hdr->tc = fields & 0x0200;
|
||||
hdr->rd = fields & 0x0100;
|
||||
|
||||
hdr->ra = fields & 0x8000;
|
||||
// z, ad, cd
|
||||
hdr->rcode = (buffer[3] & 0x0F);
|
||||
hdr->qdCount = get16bits(buffer);
|
||||
hdr->anCount = get16bits(buffer);
|
||||
hdr->nsCount = get16bits(buffer);
|
||||
|
@ -48,6 +66,37 @@ dns_msg *decode_hdr(const char *buffer)
|
|||
return hdr;
|
||||
}
|
||||
|
||||
dns_msg_question *decode_question(const char *buffer)
|
||||
{
|
||||
dns_msg_question *question = new dns_msg_question;
|
||||
std::string m_qName = "";
|
||||
int length = *buffer++;
|
||||
//llarp::LogInfo("qNamLen", length);
|
||||
while (length != 0) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
char c = *buffer++;
|
||||
m_qName.append(1, c);
|
||||
}
|
||||
length = *buffer++;
|
||||
if (length != 0) m_qName.append(1,'.');
|
||||
}
|
||||
question->name = m_qName;
|
||||
question->type = get16bits(buffer);
|
||||
question->qClass = get16bits(buffer);
|
||||
return question;
|
||||
}
|
||||
|
||||
dns_msg_answer *decode_answer(const char *buffer)
|
||||
{
|
||||
dns_msg_answer *answer = new dns_msg_answer;
|
||||
answer->type = get16bits(buffer);
|
||||
answer->aClass = get16bits(buffer);
|
||||
answer->ttl = get32bits(buffer);
|
||||
answer->rdLen = get16bits(buffer);
|
||||
answer->rData = get16bits(buffer);
|
||||
return answer;
|
||||
}
|
||||
|
||||
void code_domain(char*& buffer, const std::string& domain) throw()
|
||||
{
|
||||
|
||||
|
@ -111,6 +160,7 @@ bool forward_dns_request(std::string request)
|
|||
void
|
||||
writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr *from, dns_request *request)
|
||||
{
|
||||
lock_t lock(m_dnsd2_Mutex);
|
||||
if (!hostRes)
|
||||
{
|
||||
llarp::LogWarn("Failed to resolve");
|
||||
|
@ -174,14 +224,18 @@ void handle_dnsc_result(dns_client_request *client_request)
|
|||
llarp_host_resolved(client_request);
|
||||
}
|
||||
|
||||
// our generic version
|
||||
void
|
||||
handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from, dns_request *request)
|
||||
{
|
||||
lock_t lock(m_dnsd_Mutex);
|
||||
const size_t HDR_OFFSET = 12;
|
||||
const char* p_buffer = buffer;
|
||||
|
||||
|
||||
dns_msg *msg = decode_hdr(p_buffer);
|
||||
int rcode = (buffer[3] & 0x0F);
|
||||
llarp::LogInfo("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;
|
||||
|
@ -233,7 +287,9 @@ handle_recvfrom(const char *buffer, ssize_t nbytes, const struct sockaddr *from,
|
|||
//llarp::Addr anIp;
|
||||
//llarp::LogInfo("Checking server request ", request);
|
||||
struct llarp_udp_io *udp = (struct llarp_udp_io *)request->user;
|
||||
dnsd_context *dnsd = (dnsd_context *)udp->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);
|
||||
|
@ -247,15 +303,19 @@ void
|
|||
llarp_handle_recvfrom(struct llarp_udp_io *udp, const struct sockaddr *paddr,
|
||||
const void *buf, ssize_t sz)
|
||||
{
|
||||
lock_t lock(m_dnsd3_Mutex);
|
||||
//llarp_link *link = static_cast< llarp_link * >(udp->user);
|
||||
llarp::LogInfo("llarp Received Bytes ", sz);
|
||||
dns_request *llarp_dns_request = new dns_request;
|
||||
//llarp::LogInfo("Creating server request ", &llarp_dns_request);
|
||||
//llarp::LogInfo("Server UDP address ", udp);
|
||||
|
||||
llarp_dns_request->from = (struct sockaddr *)new sockaddr(*paddr);
|
||||
// make a copy of the sockaddr
|
||||
llarp_dns_request->from = new sockaddr(*paddr);
|
||||
llarp_dns_request->user = (void *)udp;
|
||||
// set sock hook
|
||||
llarp_dns_request->hook = &llarp_sendto_dns_hook_func;
|
||||
|
||||
//llarp::LogInfo("Server request's UDP ", llarp_dns_request->user);
|
||||
handle_recvfrom((char *)buf, sz, llarp_dns_request->from, llarp_dns_request);
|
||||
}
|
||||
|
@ -272,6 +332,8 @@ raw_handle_recvfrom(int *sockfd, const struct sockaddr *saddr,
|
|||
handle_recvfrom((char *)buf, sz, saddr, llarp_dns_request);
|
||||
}
|
||||
|
||||
dns_tracker dns_udp_tracker;
|
||||
|
||||
bool
|
||||
llarp_dnsd_init(struct dnsd_context *dnsd, struct llarp_ev_loop *netloop,
|
||||
const char *dnsd_ifname, uint16_t dnsd_port,
|
||||
|
@ -282,12 +344,14 @@ llarp_dnsd_init(struct dnsd_context *dnsd, struct llarp_ev_loop *netloop,
|
|||
bindaddr.sin_family = AF_INET;
|
||||
bindaddr.sin_port = htons(dnsd_port);
|
||||
|
||||
dnsd->udp.user = dnsd;
|
||||
dnsd->udp.recvfrom = &llarp_handle_recvfrom;
|
||||
dnsd->udp.user = &dns_udp_tracker;
|
||||
dnsd->udp.recvfrom = &llarp_handle_dns_recvfrom;
|
||||
dnsd->udp.tick = nullptr;
|
||||
|
||||
dns_udp_tracker.dnsd = dnsd;
|
||||
|
||||
// configure dns client
|
||||
if (!llarp_dnsc_init(&dnsd->client, netloop, dnsc_hostname, dnsc_port))
|
||||
if (!llarp_dnsc_init(&dnsd->client, &dnsd->udp, dnsc_hostname, dnsc_port))
|
||||
{
|
||||
llarp::LogError("Couldnt init dns client");
|
||||
return false;
|
||||
|
|
|
@ -2,24 +2,6 @@
|
|||
#define LIBLLARP_DNSD_HPP
|
||||
|
||||
#include "dnsc.hpp"
|
||||
#include <string>
|
||||
|
||||
struct dns_msg
|
||||
{
|
||||
uint id;
|
||||
uint qr;
|
||||
uint opcode;
|
||||
uint aa;
|
||||
uint tc;
|
||||
uint rd;
|
||||
uint ra;
|
||||
uint rcode;
|
||||
|
||||
uint qdCount;
|
||||
uint anCount;
|
||||
uint nsCount;
|
||||
uint arCount;
|
||||
};
|
||||
|
||||
typedef ssize_t (*sendto_dns_hook_func)(void *sock, const struct sockaddr *from, const void *buffer, size_t length);
|
||||
|
||||
|
@ -36,5 +18,8 @@ struct dns_request
|
|||
sendto_dns_hook_func hook; // sendto hook tbh
|
||||
};
|
||||
|
||||
dns_msg_header *decode_hdr(const char *buffer);
|
||||
dns_msg_question *decode_question(const char *buffer);
|
||||
dns_msg_answer *decode_answer(const char *buffer);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue