2018-08-08 17:01:32 +00:00
|
|
|
#include <llarp/endian.h>
|
2018-09-24 13:09:01 +00:00
|
|
|
#include <llarp/dnsd.hpp> // for llarp_handle_dnsd_recvfrom, dnsc
|
|
|
|
#include <llarp/logger.hpp>
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-08-02 12:51:49 +00:00
|
|
|
/*
|
|
|
|
<domain-name> is a domain name represented as a series of labels, and
|
|
|
|
terminated by a label with zero length. <character-string> is a single
|
|
|
|
length octet followed by that number of characters. <character-string>
|
|
|
|
is treated as binary information, and can be up to 256 characters in
|
|
|
|
length (including the length octet).
|
|
|
|
*/
|
2018-08-08 12:40:54 +00:00
|
|
|
std::string
|
|
|
|
getDNSstring(const char *buffer)
|
2018-08-02 12:51:49 +00:00
|
|
|
{
|
|
|
|
std::string str = "";
|
2018-08-08 12:40:54 +00:00
|
|
|
uint8_t length = *buffer++;
|
|
|
|
// printf("dnsStringLen[%d]\n", length);
|
|
|
|
// llarp::LogInfo("dnsStringLen ", length);
|
|
|
|
if(!length)
|
|
|
|
return str;
|
2018-08-02 12:51:49 +00:00
|
|
|
while(length != 0)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
char c = *buffer++;
|
|
|
|
str.append(1, c);
|
|
|
|
}
|
|
|
|
length = *buffer++;
|
|
|
|
if(length != 0)
|
|
|
|
str.append(1, '.');
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-10-09 12:40:53 +00:00
|
|
|
void
|
|
|
|
code_domain(char *&buffer, const std::string &domain) throw()
|
|
|
|
{
|
|
|
|
std::string::size_type start(0);
|
|
|
|
std::string::size_type end; // indexes
|
|
|
|
// llarp::LogInfo("domain [", domain, "]");
|
|
|
|
while((end = domain.find('.', start)) != std::string::npos)
|
|
|
|
{
|
|
|
|
*buffer++ = end - start; // label length octet
|
|
|
|
for(std::string::size_type i = start; i < end; i++)
|
|
|
|
{
|
|
|
|
*buffer++ = domain[i]; // label octets
|
|
|
|
// llarp::LogInfo("Writing ", domain[i], " at ", i);
|
|
|
|
}
|
|
|
|
start = end + 1; // Skip '.'
|
|
|
|
}
|
2018-10-11 12:22:29 +00:00
|
|
|
|
2018-10-09 12:40:53 +00:00
|
|
|
// llarp::LogInfo("start ", start, " domain size ", domain.size());
|
2018-10-11 12:22:29 +00:00
|
|
|
|
2018-10-09 12:40:53 +00:00
|
|
|
*buffer++ = domain.size() - start; // last label length octet
|
|
|
|
for(size_t i = start; i < domain.size(); i++)
|
|
|
|
{
|
|
|
|
*buffer++ = domain[i]; // last label octets
|
|
|
|
// llarp::LogInfo("Writing ", domain[i], " at ", i);
|
|
|
|
}
|
2018-10-11 12:22:29 +00:00
|
|
|
|
2018-10-09 12:40:53 +00:00
|
|
|
*buffer++ = 0;
|
|
|
|
}
|
|
|
|
|
2018-08-06 12:02:00 +00:00
|
|
|
// lets just remove uint
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define uint UINT
|
|
|
|
#endif
|
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
extern "C"
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-07-27 04:07:22 +00:00
|
|
|
uint16_t
|
|
|
|
get16bits(const char *&buffer) throw()
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-08-08 17:01:32 +00:00
|
|
|
uint16_t value = bufbe16toh(buffer);
|
2018-07-27 04:07:22 +00:00
|
|
|
buffer += 2;
|
|
|
|
return value;
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
2018-07-27 04:07:22 +00:00
|
|
|
|
|
|
|
uint32_t
|
|
|
|
get32bits(const char *&buffer) throw()
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-08-08 17:01:32 +00:00
|
|
|
uint32_t value = bufbe32toh(buffer);
|
2018-07-27 04:07:22 +00:00
|
|
|
buffer += 4;
|
|
|
|
return value;
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
2018-07-27 04:07:22 +00:00
|
|
|
|
|
|
|
dns_msg_header *
|
|
|
|
decode_hdr(const char *buffer)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-07-27 04:07:22 +00:00
|
|
|
dns_msg_header *hdr = new dns_msg_header;
|
|
|
|
hdr->id = get16bits(buffer);
|
2018-08-08 12:40:54 +00:00
|
|
|
uint16_t fields = get16bits(buffer);
|
2018-07-27 04:07:22 +00:00
|
|
|
uint8_t lFields = (fields & 0x00FF) >> 0;
|
|
|
|
uint8_t hFields = (fields & 0xFF00) >> 8;
|
|
|
|
// hdr->qr = fields & 0x8000;
|
|
|
|
hdr->qr = (hFields >> 7) & 0x1;
|
|
|
|
hdr->opcode = fields & 0x7800;
|
|
|
|
hdr->aa = fields & 0x0400;
|
|
|
|
hdr->tc = fields & 0x0200;
|
|
|
|
hdr->rd = fields & 0x0100;
|
|
|
|
|
2018-10-13 15:42:47 +00:00
|
|
|
hdr->ra = (lFields >> 7) & 0x1;
|
|
|
|
hdr->z = (lFields >> 6) & 0x1;
|
|
|
|
hdr->ad = (lFields >> 5) & 0x1;
|
|
|
|
hdr->cd = (lFields >> 4) & 0x1;
|
2018-07-27 04:07:22 +00:00
|
|
|
hdr->rcode = lFields & 0xf;
|
|
|
|
|
|
|
|
hdr->qdCount = get16bits(buffer);
|
|
|
|
hdr->anCount = get16bits(buffer);
|
|
|
|
hdr->nsCount = get16bits(buffer);
|
|
|
|
hdr->arCount = get16bits(buffer);
|
|
|
|
return hdr;
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
2018-07-27 04:07:22 +00:00
|
|
|
|
|
|
|
dns_msg_question *
|
|
|
|
decode_question(const char *buffer)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-10-13 15:42:47 +00:00
|
|
|
// char *start = (char *)buffer;
|
2018-07-27 04:07:22 +00:00
|
|
|
dns_msg_question *question = new dns_msg_question;
|
2018-08-02 12:51:49 +00:00
|
|
|
std::string m_qName = getDNSstring(buffer);
|
2018-10-13 15:42:47 +00:00
|
|
|
buffer += m_qName.length() + 2; // + length byte & ending terminator
|
|
|
|
// printf("Now0 at [%d]\n", buffer - start);
|
2018-08-08 12:40:54 +00:00
|
|
|
// buffer += m_qName.size() + 1;
|
2018-08-02 12:51:49 +00:00
|
|
|
/*
|
2018-07-27 04:07:22 +00:00
|
|
|
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, '.');
|
|
|
|
}
|
2018-08-02 12:51:49 +00:00
|
|
|
*/
|
2018-10-13 15:42:47 +00:00
|
|
|
question->name = m_qName;
|
|
|
|
question->type = get16bits(buffer);
|
|
|
|
// printf("Now1 at [%d]\n", buffer - start);
|
2018-07-27 04:07:22 +00:00
|
|
|
question->qClass = get16bits(buffer);
|
2018-10-13 15:42:47 +00:00
|
|
|
// printf("Now2 at [%d]\n", buffer - start);
|
2018-07-27 04:07:22 +00:00
|
|
|
return question;
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
dns_msg_answer *
|
|
|
|
decode_answer(const char *buffer)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-07-27 04:07:22 +00:00
|
|
|
dns_msg_answer *answer = new dns_msg_answer;
|
2018-08-02 12:51:49 +00:00
|
|
|
// skip for now until we can handle compressed labels
|
|
|
|
/*
|
|
|
|
std::string aName = getDNSstring((char *)buffer);
|
|
|
|
buffer += aName.size() + 1;
|
|
|
|
*/
|
2018-09-23 16:57:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
llarp_buffer_t bob;
|
|
|
|
bob.base = (unsigned char *)buffer;
|
|
|
|
bob.sz = 12;
|
|
|
|
llarp::DumpBuffer(bob);
|
|
|
|
*/
|
2018-09-24 13:09:01 +00:00
|
|
|
|
2018-09-23 16:57:53 +00:00
|
|
|
char hex_buffer[12 * 3 + 1];
|
|
|
|
hex_buffer[12 * 3] = 0;
|
|
|
|
for(unsigned int j = 0; j < 10; j++)
|
|
|
|
sprintf(&hex_buffer[3 * j], "%02X ", buffer[j]);
|
|
|
|
llarp::LogDebug("First 12 bytes: ", hex_buffer);
|
|
|
|
|
2018-08-08 12:40:54 +00:00
|
|
|
uint8_t first = *buffer++;
|
2018-09-23 16:57:53 +00:00
|
|
|
// llarp::LogInfo("decode - first", std::to_string(first));
|
2018-08-02 12:51:49 +00:00
|
|
|
// SOA hack
|
2018-09-23 16:57:53 +00:00
|
|
|
if(first != 12 && first != 14) // 0x0c (c0 0c) 0
|
2018-08-02 12:51:49 +00:00
|
|
|
{
|
2018-09-20 10:07:16 +00:00
|
|
|
llarp::LogDebug("decode - first isnt 12, stepping back");
|
2018-08-08 12:40:54 +00:00
|
|
|
buffer--; // rewind buffer one byte
|
2018-08-02 12:51:49 +00:00
|
|
|
}
|
2018-08-08 12:40:54 +00:00
|
|
|
answer->type = get16bits(buffer);
|
2018-07-27 04:07:22 +00:00
|
|
|
// assert(answer->type < 259);
|
|
|
|
if(answer->type > 259)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-07-27 04:07:22 +00:00
|
|
|
llarp::LogWarn("Answer type is off the charts");
|
|
|
|
}
|
|
|
|
answer->aClass = get16bits(buffer);
|
|
|
|
answer->ttl = get32bits(buffer);
|
|
|
|
answer->rdLen = get16bits(buffer);
|
|
|
|
if(answer->rdLen == 4)
|
|
|
|
{
|
|
|
|
answer->rData = new uint8_t[answer->rdLen];
|
|
|
|
memcpy(answer->rData, buffer, answer->rdLen);
|
2018-09-20 10:07:16 +00:00
|
|
|
buffer += answer->rdLen; // advance the length
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
2018-07-27 04:07:22 +00:00
|
|
|
else
|
|
|
|
{
|
2018-09-23 16:57:53 +00:00
|
|
|
llarp::LogDebug("Got type ", answer->type);
|
2018-08-02 12:51:49 +00:00
|
|
|
switch(answer->type)
|
|
|
|
{
|
2018-09-20 10:07:16 +00:00
|
|
|
case 5:
|
|
|
|
buffer += answer->rdLen; // advance the length
|
|
|
|
break;
|
2018-08-08 12:40:54 +00:00
|
|
|
case 6: // type 6 = SOA
|
2018-08-02 12:51:49 +00:00
|
|
|
{
|
|
|
|
// 2 names, then 4x 32bit
|
2018-10-09 12:40:53 +00:00
|
|
|
// why risk any crashes
|
2018-10-13 15:42:47 +00:00
|
|
|
if(answer->rdLen < 24)
|
2018-10-09 12:40:53 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("Weird SOA is less than 24 bytes: ", answer->rdLen);
|
|
|
|
}
|
|
|
|
/*
|
2018-08-08 12:40:54 +00:00
|
|
|
std::string mname = getDNSstring((char *)buffer);
|
|
|
|
std::string rname = getDNSstring((char *)buffer);
|
|
|
|
uint32_t serial = get32bits(buffer);
|
|
|
|
uint32_t refresh = get32bits(buffer);
|
|
|
|
uint32_t retry = get32bits(buffer);
|
|
|
|
uint32_t expire = get32bits(buffer);
|
|
|
|
uint32_t minimum = get32bits(buffer);
|
2018-08-02 12:51:49 +00:00
|
|
|
llarp::LogInfo("mname : ", mname);
|
|
|
|
llarp::LogInfo("rname : ", rname);
|
|
|
|
llarp::LogDebug("serial : ", serial);
|
|
|
|
llarp::LogDebug("refresh : ", refresh);
|
|
|
|
llarp::LogDebug("retry : ", retry);
|
|
|
|
llarp::LogDebug("expire : ", expire);
|
|
|
|
llarp::LogDebug("minimum : ", minimum);
|
2018-10-09 12:40:53 +00:00
|
|
|
*/
|
2018-08-02 12:51:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-09-23 16:57:53 +00:00
|
|
|
case 12:
|
|
|
|
{
|
|
|
|
std::string revname = getDNSstring((char *)buffer);
|
|
|
|
llarp::LogInfo("revDNSname: ", revname);
|
|
|
|
answer->rData = new uint8_t[answer->rdLen + 1];
|
|
|
|
memcpy(answer->rData, revname.c_str(), answer->rdLen);
|
|
|
|
}
|
|
|
|
break;
|
2018-08-02 12:51:49 +00:00
|
|
|
default:
|
2018-09-20 10:07:16 +00:00
|
|
|
buffer += answer->rdLen; // advance the length
|
|
|
|
llarp::LogWarn("Unknown Type ", answer->type);
|
|
|
|
break;
|
2018-08-02 12:51:49 +00:00
|
|
|
}
|
2018-07-27 04:07:22 +00:00
|
|
|
}
|
|
|
|
return answer;
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
void
|
|
|
|
put16bits(char *&buffer, uint16_t value) throw()
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-08-08 17:01:32 +00:00
|
|
|
htobe16buf(buffer, value);
|
2018-07-27 04:07:22 +00:00
|
|
|
buffer += 2;
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
void
|
|
|
|
put32bits(char *&buffer, uint32_t value) throw()
|
|
|
|
{
|
2018-08-08 17:01:32 +00:00
|
|
|
htobe32buf(buffer, value);
|
2018-07-27 04:07:22 +00:00
|
|
|
buffer += 4;
|
|
|
|
}
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
void
|
|
|
|
llarp_handle_dns_recvfrom(struct llarp_udp_io *udp,
|
2018-09-20 10:07:16 +00:00
|
|
|
const struct sockaddr *addr, const void *buf,
|
2018-07-27 04:07:22 +00:00
|
|
|
ssize_t sz)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-07-27 04:07:22 +00:00
|
|
|
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::LogDebug("msg id ", hdr->id);
|
|
|
|
llarp::LogDebug("msg qr ", (uint8_t)hdr->qr);
|
2018-10-03 10:49:57 +00:00
|
|
|
if(!udp)
|
|
|
|
{
|
|
|
|
llarp::LogError("no udp passed in to handler");
|
|
|
|
}
|
|
|
|
if(!addr)
|
|
|
|
{
|
|
|
|
llarp::LogError("no source addr passed in to handler");
|
|
|
|
}
|
2018-07-27 04:07:22 +00:00
|
|
|
if(hdr->qr)
|
|
|
|
{
|
|
|
|
llarp::LogDebug("handling as dnsc answer");
|
2018-09-20 10:07:16 +00:00
|
|
|
llarp_handle_dnsc_recvfrom(udp, addr, buf, sz);
|
2018-07-27 04:07:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogDebug("handling as dnsd question");
|
2018-09-20 10:07:16 +00:00
|
|
|
llarp_handle_dnsd_recvfrom(udp, addr, buf, sz);
|
2018-07-27 04:07:22 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
llarp::LogInfo("msg op ", hdr->opcode);
|
|
|
|
llarp::LogInfo("msg rc ", hdr->rcode);
|
|
|
|
|
2018-08-06 12:02:00 +00:00
|
|
|
for(uint8_t i = 0; i < hdr->qdCount; i++)
|
2018-07-27 04:07:22 +00:00
|
|
|
{
|
|
|
|
dns_msg_question *question = decode_question((const char*)castBuf);
|
|
|
|
llarp::LogInfo("Read a question");
|
|
|
|
castBuf += question->name.length() + 8;
|
|
|
|
}
|
|
|
|
|
2018-08-06 12:02:00 +00:00
|
|
|
for(uint8_t i = 0; i < hdr->anCount; i++)
|
2018-07-27 04:07:22 +00:00
|
|
|
{
|
|
|
|
dns_msg_answer *answer = decode_answer((const char*)castBuf);
|
|
|
|
llarp::LogInfo("Read an answer");
|
|
|
|
castBuf += answer->name.length() + 4 + 4 + 4 + answer->rdLen;
|
|
|
|
}
|
|
|
|
*/
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
2018-08-01 09:04:40 +00:00
|
|
|
}
|