2018-07-16 12:48:04 +00:00
|
|
|
#include "dnsd.hpp"
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
#include <netdb.h> /* getaddrinfo, getnameinfo */
|
|
|
|
#include <stdlib.h> /* exit */
|
|
|
|
#include <string.h> /* memset */
|
2018-07-16 12:48:04 +00:00
|
|
|
#include <sys/socket.h>
|
2018-07-16 21:26:16 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h> /* close */
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2018-07-16 21:26:16 +00:00
|
|
|
#include <netinet/in.h>
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
#include <llarp/dns.h>
|
2018-07-16 21:26:16 +00:00
|
|
|
#include "logger.hpp"
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
// FIXME: make configurable
|
|
|
|
#define SERVER "8.8.8.8"
|
|
|
|
#define PORT 53
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
struct sockaddr *
|
|
|
|
resolveHost(const char *url)
|
|
|
|
{
|
|
|
|
char *sUrl = strdup(url);
|
2018-07-16 12:48:04 +00:00
|
|
|
struct dns_query dnsQuery;
|
|
|
|
dnsQuery.length = 12;
|
|
|
|
dnsQuery.url = sUrl;
|
|
|
|
dnsQuery.reqType = 0x01;
|
2018-07-16 21:26:16 +00:00
|
|
|
// 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;
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery.request[10] = 0x00;
|
|
|
|
dnsQuery.request[11] = 0x00;
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
char *word;
|
2018-07-16 12:48:04 +00:00
|
|
|
unsigned int i;
|
|
|
|
llarp::LogDebug("Asking DNS server %s about %s\n", SERVER, url);
|
2018-07-16 21:26:16 +00:00
|
|
|
// dnsQuery.reqType = 0x01;
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
word = strtok(sUrl, ".");
|
2018-07-16 21:26:16 +00:00
|
|
|
while(word)
|
|
|
|
{
|
|
|
|
llarp::LogDebug("parsing hostname: \"%s\" is %zu characters\n", word,
|
|
|
|
strlen(word));
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery.request[dnsQuery.length++] = strlen(word);
|
2018-07-16 21:26:16 +00:00
|
|
|
for(i = 0; i < strlen(word); i++)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery.request[dnsQuery.length++] = word[i];
|
|
|
|
}
|
|
|
|
word = strtok(NULL, ".");
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
dnsQuery.request[dnsQuery.length++] = 0x00; // End of the host name
|
|
|
|
dnsQuery.request[dnsQuery.length++] =
|
|
|
|
0x00; // 0x0001 - Query is a Type A query (host address)
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery.request[dnsQuery.length++] = dnsQuery.reqType;
|
2018-07-16 21:26:16 +00:00
|
|
|
dnsQuery.request[dnsQuery.length++] =
|
|
|
|
0x00; // 0x0001 - Query is class IN (Internet address)
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery.request[dnsQuery.length++] = 0x01;
|
|
|
|
|
|
|
|
struct sockaddr_in addr;
|
2018-07-16 21:26:16 +00:00
|
|
|
// int socket;
|
2018-07-16 12:48:04 +00:00
|
|
|
ssize_t ret;
|
|
|
|
int rcode;
|
|
|
|
socklen_t size;
|
|
|
|
int ip = 0;
|
|
|
|
int length;
|
|
|
|
unsigned char buffer[DNC_BUF_SIZE];
|
2018-07-16 21:26:16 +00:00
|
|
|
// 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
|
2018-07-16 12:48:04 +00:00
|
|
|
uint16_t MSGID;
|
|
|
|
|
|
|
|
int sockfd;
|
|
|
|
|
|
|
|
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
2018-07-16 21:26:16 +00:00
|
|
|
if(sockfd < 0)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn("Error creating socket!\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-16 21:26:16 +00:00
|
|
|
// socket = sockfd;
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
2018-07-16 21:26:16 +00:00
|
|
|
addr.sin_family = AF_INET;
|
2018-07-16 12:48:04 +00:00
|
|
|
addr.sin_addr.s_addr = inet_addr(SERVER);
|
2018-07-16 21:26:16 +00:00
|
|
|
addr.sin_port = htons(PORT);
|
|
|
|
size = sizeof(addr);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// hexdump("sending packet", &dnsQuery.request, dnsQuery.length);
|
|
|
|
ret = sendto(sockfd, dnsQuery.request, dnsQuery.length, 0,
|
|
|
|
(struct sockaddr *)&addr, size);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn("Error Sending Request");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-16 21:26:16 +00:00
|
|
|
// printf("Sent\n");
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
memset(&buffer, 0, DNC_BUF_SIZE);
|
2018-07-16 21:26:16 +00:00
|
|
|
ret = recvfrom(sockfd, buffer, DNC_BUF_SIZE, 0, (struct sockaddr *)&addr,
|
|
|
|
&size);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn("Error Receiving Response");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// hexdump("received packet", &buffer, ret);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
rcode = (buffer[3] & 0x0F);
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// tempBuf[0] = buffer[4];
|
|
|
|
// tempBuf[1] = buffer[5];
|
|
|
|
// tempBuf[2] = '\0';
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// printf("%0x %0x %0x %0x\n", buffer[4], buffer[5], tempBuf[0], tempBuf[1]);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// QDCOUNT = (uint16_t) strtol(tempBuf, NULL, 16);
|
|
|
|
QDCOUNT = (uint16_t)buffer[4] * 0x100 + buffer[5];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("entries in question section: %u\n", QDCOUNT);
|
2018-07-16 21:26:16 +00:00
|
|
|
ANCOUNT = (uint16_t)buffer[6] * 0x100 + buffer[7];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("records in answer section: %u\n", ANCOUNT);
|
2018-07-16 21:26:16 +00:00
|
|
|
NSCOUNT = (uint16_t)buffer[8] * 0x100 + buffer[9];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("name server resource record count: %u\n", NSCOUNT);
|
2018-07-16 21:26:16 +00:00
|
|
|
ARCOUNT = (uint16_t)buffer[10] * 0x100 + buffer[11];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("additional records count: %u\n", ARCOUNT);
|
|
|
|
|
|
|
|
llarp::LogDebug("query type: %u\n", dnsQuery.reqType);
|
2018-07-16 21:26:16 +00:00
|
|
|
QCLASS = (uint16_t)dnsQuery.request[dnsQuery.length - 2] * 0x100
|
|
|
|
+ dnsQuery.request[dnsQuery.length - 1];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("query class: %u\n", QCLASS);
|
2018-07-16 21:26:16 +00:00
|
|
|
length = dnsQuery.length + 1; // to skip 0xc00c
|
|
|
|
ATYPE = (uint16_t)buffer[length + 1] * 0x100 + buffer[length + 2];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("answer type: %u\n", ATYPE);
|
2018-07-16 21:26:16 +00:00
|
|
|
ACLASS = (uint16_t)buffer[length + 3] * 0x100 + buffer[length + 4];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("answer class: %u\n", ACLASS);
|
2018-07-16 21:26:16 +00:00
|
|
|
TTL = (uint32_t)buffer[length + 5] * 0x1000000 + buffer[length + 6] * 0x10000
|
|
|
|
+ buffer[length + 7] * 0x100 + buffer[length + 8];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("seconds to cache: %u\n", TTL);
|
2018-07-16 21:26:16 +00:00
|
|
|
RDLENGTH = (uint16_t)buffer[length + 9] * 0x100 + buffer[length + 10];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("bytes in answer: %u\n", RDLENGTH);
|
2018-07-16 21:26:16 +00:00
|
|
|
MSGID = (uint16_t)buffer[0] * 0x100 + buffer[1];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("answer msg id: %u\n", MSGID);
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
if(rcode == 2)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn("nameserver %s returned SERVFAIL:\n", SERVER);
|
2018-07-16 21:26:16 +00:00
|
|
|
llarp::LogWarn(
|
|
|
|
" the name server was unable to process this query due to a\n "
|
|
|
|
"problem with the name server.\n");
|
2018-07-16 12:48:04 +00:00
|
|
|
return nullptr;
|
2018-07-16 21:26:16 +00:00
|
|
|
}
|
|
|
|
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");
|
2018-07-16 12:48:04 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* search for and print IPv4 addresses */
|
2018-07-16 21:26:16 +00:00
|
|
|
if(dnsQuery.reqType == 0x01)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("DNS server's answer is: (type#=%u):", ATYPE);
|
2018-07-16 21:26:16 +00:00
|
|
|
// 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]);
|
2018-07-16 12:48:04 +00:00
|
|
|
struct sockaddr *g_addr = new sockaddr;
|
2018-07-16 21:26:16 +00:00
|
|
|
g_addr->sa_family = AF_INET;
|
2018-07-16 23:26:58 +00:00
|
|
|
// g_addr->sa_len = sizeof(in_addr);
|
|
|
|
struct in_addr *addr = &((struct sockaddr_in *)g_addr)->sin_addr;
|
2018-07-16 21:26:16 +00:00
|
|
|
unsigned char *ip;
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// have ip point to s_addr
|
|
|
|
ip = (unsigned char *)&(addr->s_addr);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
ip[0] = buffer[i + 0];
|
|
|
|
ip[1] = buffer[i + 1];
|
|
|
|
ip[2] = buffer[i + 2];
|
|
|
|
ip[3] = buffer[i + 3];
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
return g_addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
if(!ip)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn(" No IPv4 address found in the DNS response!\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-16 21:26:16 +00:00
|
|
|
llarp_handle_dnsclient_recvfrom(struct llarp_udp_io *udp,
|
|
|
|
const struct sockaddr *saddr, const void *buf,
|
|
|
|
ssize_t sz)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
struct dns_client_request *request = (struct dns_client_request *)udp->user;
|
2018-07-16 21:26:16 +00:00
|
|
|
if(!request)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-16 21:26:16 +00:00
|
|
|
llarp::LogError(
|
|
|
|
"User data to DNS Client response not a dns_client_request");
|
2018-07-16 12:48:04 +00:00
|
|
|
// we can't call back the hook
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// it's corrupt by here...
|
2018-07-19 21:08:11 +00:00
|
|
|
// dns_request *server_request = (dns_request *)request->user;
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// unsigned char buffer[DNC_BUF_SIZE];
|
2018-07-16 12:48:04 +00:00
|
|
|
unsigned char *buffer = (unsigned char *)buf;
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// memset(&buffer, 0, DNC_BUF_SIZE);
|
|
|
|
// ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr*)&addr,
|
|
|
|
// &size);
|
|
|
|
if(sz < 0)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn("Error Receiving DNS Client Response");
|
|
|
|
request->resolved(request, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// hexdump("received packet", &buffer, ret);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
llarp_ev_close_udp(udp);
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
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
|
2018-07-16 12:48:04 +00:00
|
|
|
uint16_t MSGID;
|
|
|
|
int rcode;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
struct dns_query *dnsQuery = &request->query;
|
|
|
|
|
|
|
|
rcode = (buffer[3] & 0x0F);
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// tempBuf[0] = buffer[4];
|
|
|
|
// tempBuf[1] = buffer[5];
|
|
|
|
// tempBuf[2] = '\0';
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// printf("%0x %0x %0x %0x\n", buffer[4], buffer[5], tempBuf[0], tempBuf[1]);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// QDCOUNT = (uint16_t) strtol(tempBuf, NULL, 16);
|
|
|
|
QDCOUNT = (uint16_t)buffer[4] * 0x100 + buffer[5];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("entries in question section: %u\n", QDCOUNT);
|
2018-07-16 21:26:16 +00:00
|
|
|
ANCOUNT = (uint16_t)buffer[6] * 0x100 + buffer[7];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("records in answer section: %u\n", ANCOUNT);
|
2018-07-16 21:26:16 +00:00
|
|
|
NSCOUNT = (uint16_t)buffer[8] * 0x100 + buffer[9];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("name server resource record count: %u\n", NSCOUNT);
|
2018-07-16 21:26:16 +00:00
|
|
|
ARCOUNT = (uint16_t)buffer[10] * 0x100 + buffer[11];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("additional records count: %u\n", ARCOUNT);
|
|
|
|
|
|
|
|
llarp::LogDebug("query type: %u\n", dnsQuery->reqType);
|
2018-07-16 21:26:16 +00:00
|
|
|
QCLASS = (uint16_t)dnsQuery->request[dnsQuery->length - 2] * 0x100
|
|
|
|
+ dnsQuery->request[dnsQuery->length - 1];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("query class: %u\n", QCLASS);
|
2018-07-16 21:26:16 +00:00
|
|
|
length = dnsQuery->length + 1; // to skip 0xc00c
|
|
|
|
ATYPE = (uint16_t)buffer[length + 1] * 0x100 + buffer[length + 2];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("answer type: %u\n", ATYPE);
|
2018-07-16 21:26:16 +00:00
|
|
|
ACLASS = (uint16_t)buffer[length + 3] * 0x100 + buffer[length + 4];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("answer class: %u\n", ACLASS);
|
2018-07-16 21:26:16 +00:00
|
|
|
TTL = (uint32_t)buffer[length + 5] * 0x1000000 + buffer[length + 6] * 0x10000
|
|
|
|
+ buffer[length + 7] * 0x100 + buffer[length + 8];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("seconds to cache: %u\n", TTL);
|
2018-07-16 21:26:16 +00:00
|
|
|
RDLENGTH = (uint16_t)buffer[length + 9] * 0x100 + buffer[length + 10];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("bytes in answer: %u\n", RDLENGTH);
|
2018-07-16 21:26:16 +00:00
|
|
|
MSGID = (uint16_t)buffer[0] * 0x100 + buffer[1];
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("answer msg id: %u\n", MSGID);
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
if(rcode == 2)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn("nameserver %s returned SERVFAIL:\n", SERVER);
|
2018-07-16 21:26:16 +00:00
|
|
|
llarp::LogWarn(
|
|
|
|
" the name server was unable to process this query due to a\n "
|
|
|
|
"problem with the name server.\n");
|
2018-07-16 12:48:04 +00:00
|
|
|
request->resolved(request, nullptr);
|
|
|
|
return;
|
2018-07-16 21:26:16 +00:00
|
|
|
}
|
|
|
|
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");
|
2018-07-16 12:48:04 +00:00
|
|
|
request->resolved(request, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ip = 0;
|
|
|
|
|
|
|
|
/* search for and print IPv4 addresses */
|
2018-07-16 21:26:16 +00:00
|
|
|
if(dnsQuery->reqType == 0x01)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogInfo("DNS server's answer is: (type#=%u):", ATYPE);
|
|
|
|
printf("IPv4 address(es) for %s:\n", dnsQuery->url);
|
2018-07-16 21:26:16 +00:00
|
|
|
for(unsigned int i = 0; i < sz; 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]);
|
2018-07-16 12:48:04 +00:00
|
|
|
struct sockaddr *g_addr = new sockaddr;
|
2018-07-16 21:26:16 +00:00
|
|
|
g_addr->sa_family = AF_INET;
|
2018-07-16 23:26:58 +00:00
|
|
|
// g_addr->sa_len = sizeof(in_addr);
|
|
|
|
struct in_addr *addr = &((struct sockaddr_in *)g_addr)->sin_addr;
|
2018-07-16 21:26:16 +00:00
|
|
|
unsigned char *ip;
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// have ip point to s_addr
|
|
|
|
ip = (unsigned char *)&(addr->s_addr);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
ip[0] = buffer[i + 0];
|
|
|
|
ip[1] = buffer[i + 1];
|
|
|
|
ip[2] = buffer[i + 2];
|
|
|
|
ip[3] = buffer[i + 3];
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// return g_addr;
|
2018-07-16 12:48:04 +00:00
|
|
|
request->resolved(request, g_addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
if(!ip)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn(" No IPv4 address found in the DNS response!\n");
|
|
|
|
request->resolved(request, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
void
|
|
|
|
build_dns_query(struct dns_query *dnsQuery)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
2018-07-16 21:26:16 +00:00
|
|
|
dnsQuery->length = 12;
|
|
|
|
// dnsQuery->url = sUrl;
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery->reqType = 0x01;
|
2018-07-16 21:26:16 +00:00
|
|
|
// 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;
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery->request[10] = 0x00;
|
|
|
|
dnsQuery->request[11] = 0x00;
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
char *word;
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogDebug("Asking DNS server %s about %s\n", SERVER, dnsQuery->url);
|
2018-07-16 21:26:16 +00:00
|
|
|
|
|
|
|
char *strTemp = strdup(dnsQuery->url);
|
|
|
|
word = strtok(strTemp, ".");
|
|
|
|
while(word)
|
|
|
|
{
|
|
|
|
llarp::LogDebug("parsing hostname: \"%s\" is %zu characters\n", word,
|
|
|
|
strlen(word));
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery->request[dnsQuery->length++] = strlen(word);
|
2018-07-16 21:26:16 +00:00
|
|
|
for(unsigned int i = 0; i < strlen(word); i++)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery->request[dnsQuery->length++] = word[i];
|
|
|
|
}
|
|
|
|
word = strtok(NULL, ".");
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
dnsQuery->request[dnsQuery->length++] = 0x00; // End of the host name
|
|
|
|
dnsQuery->request[dnsQuery->length++] =
|
|
|
|
0x00; // 0x0001 - Query is a Type A query (host address)
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery->request[dnsQuery->length++] = dnsQuery->reqType;
|
2018-07-16 21:26:16 +00:00
|
|
|
dnsQuery->request[dnsQuery->length++] =
|
|
|
|
0x00; // 0x0001 - Query is class IN (Internet address)
|
2018-07-16 12:48:04 +00:00
|
|
|
dnsQuery->request[dnsQuery->length++] = 0x01;
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
bool
|
|
|
|
llarp_dns_resolve(dns_client_request *request)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
struct dns_query *dnsQuery = &request->query;
|
|
|
|
build_dns_query(dnsQuery);
|
|
|
|
struct sockaddr_in addr;
|
2018-07-16 21:26:16 +00:00
|
|
|
// int socket;
|
2018-07-16 12:48:04 +00:00
|
|
|
ssize_t ret;
|
2018-07-19 21:08:11 +00:00
|
|
|
// socklen_t size;
|
2018-07-16 21:26:16 +00:00
|
|
|
// unsigned char tempBuf[3];
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
2018-07-16 21:26:16 +00:00
|
|
|
addr.sin_family = AF_INET;
|
2018-07-16 12:48:04 +00:00
|
|
|
addr.sin_addr.s_addr = inet_addr(SERVER);
|
2018-07-16 21:26:16 +00:00
|
|
|
addr.sin_port = htons(PORT);
|
2018-07-19 21:08:11 +00:00
|
|
|
// size = sizeof(addr);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
llarp_udp_io *udp = (llarp_udp_io *)request->sock;
|
2018-07-16 21:26:16 +00:00
|
|
|
// llarp::LogDebug("dns client set to use ");
|
2018-07-16 23:26:58 +00:00
|
|
|
// XXX: udp user pointer should be set before binding to socket and once
|
2018-07-16 21:26:16 +00:00
|
|
|
udp->user = request;
|
|
|
|
|
|
|
|
// hexdump("sending packet", &dnsQuery.request, dnsQuery.length);
|
|
|
|
// ret = sendto(sockfd, dnsQuery.request, dnsQuery.length, 0, (struct
|
|
|
|
// sockaddr*)&addr, size);
|
|
|
|
ret = llarp_ev_udp_sendto(udp, (sockaddr *)&addr, dnsQuery->request,
|
|
|
|
dnsQuery->length);
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogWarn("Error Sending Request");
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-19 21:08:11 +00:00
|
|
|
// dns_request *test = (dns_request *)request->user;
|
2018-07-16 21:26:16 +00:00
|
|
|
|
|
|
|
// printf("Sent\n");
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp::LogInfo("Request sent, awaiting response");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
bool
|
|
|
|
llarp_resolve_host(struct llarp_ev_loop *netloop, const char *url,
|
|
|
|
resolve_dns_hook_func resolved, void *user)
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in s_addr;
|
2018-07-16 21:26:16 +00:00
|
|
|
s_addr.sin_family = AF_INET;
|
2018-07-16 12:48:04 +00:00
|
|
|
s_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
|
|
|
|
|
|
|
|
llarp_udp_io *udp = new llarp_udp_io;
|
2018-07-16 21:26:16 +00:00
|
|
|
udp->tick = nullptr;
|
|
|
|
udp->user = nullptr;
|
|
|
|
udp->impl = nullptr;
|
|
|
|
udp->parent = netloop; // add_udp will do this...
|
|
|
|
// llarp::LogDebug("dns client set to use ");
|
2018-07-16 12:48:04 +00:00
|
|
|
udp->recvfrom = &llarp_handle_dnsclient_recvfrom;
|
|
|
|
|
|
|
|
if(llarp_ev_add_udp(netloop, udp, (sockaddr *)&s_addr) == -1)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to bind resolver to");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dns_client_request *request = new dns_client_request;
|
2018-07-16 21:26:16 +00:00
|
|
|
request->sock = (void *)udp;
|
|
|
|
request->user = user;
|
|
|
|
request->query.url = strdup(url);
|
|
|
|
request->resolved = resolved;
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp_dns_resolve(request);
|
|
|
|
return true;
|
|
|
|
}
|