2018-12-12 02:52:51 +00:00
|
|
|
#include <dnsd.hpp> // for llarp_handle_dnsd_recvfrom, dnsc
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <util/buffer.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/endian.hpp>
|
|
|
|
#include <util/logger.hpp>
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
void
|
|
|
|
hexDump(const char *buffer, uint16_t size)
|
|
|
|
{
|
2018-11-09 12:25:14 +00:00
|
|
|
// would rather heap allocate than use VLA
|
2019-02-11 14:43:48 +00:00
|
|
|
std::vector< char > hex_buffer(size * 3 + 1);
|
2018-11-03 13:19:18 +00:00
|
|
|
hex_buffer[size * 3] = 0;
|
|
|
|
for(unsigned int j = 0; j < size; j++)
|
|
|
|
sprintf(&hex_buffer[3 * j], "%02X ", buffer[j]);
|
2019-02-11 14:43:48 +00:00
|
|
|
std::string str(hex_buffer.begin(), hex_buffer.end());
|
2018-11-03 13:19:18 +00:00
|
|
|
llarp::LogInfo("First ", size, " bytes: ", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hexDumpAt(const char *const buffer, uint32_t pos, uint16_t size)
|
|
|
|
{
|
2018-11-09 12:25:14 +00:00
|
|
|
// would rather heap allocate than use VLA
|
2019-02-11 14:43:48 +00:00
|
|
|
std::vector< char > hex_buffer(size * 3 + 1);
|
2018-11-03 13:19:18 +00:00
|
|
|
hex_buffer[size * 3] = 0;
|
|
|
|
for(unsigned int j = 0; j < size; j++)
|
|
|
|
sprintf(&hex_buffer[3 * j], "%02X ", buffer[pos + j]);
|
2019-02-11 14:43:48 +00:00
|
|
|
std::string str(hex_buffer.begin(), hex_buffer.end());
|
2018-11-03 13:19:18 +00:00
|
|
|
llarp::LogInfo(pos, " ", size, " bytes: ", str);
|
|
|
|
}
|
|
|
|
|
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
|
2018-11-03 13:19:18 +00:00
|
|
|
getDNSstring(const char *const buffer, uint32_t *pos)
|
2018-08-02 12:51:49 +00:00
|
|
|
{
|
2018-11-03 13:19:18 +00:00
|
|
|
std::string str = "";
|
|
|
|
const char *moveable = buffer;
|
|
|
|
moveable += *pos;
|
|
|
|
uint8_t length = *moveable++;
|
|
|
|
(*pos)++;
|
|
|
|
if(length == 0xc0)
|
|
|
|
{
|
|
|
|
uint8_t cPos = *moveable++;
|
|
|
|
(*pos)++;
|
|
|
|
uint32_t cPos32 = cPos;
|
|
|
|
// llarp::LogInfo("Found reference at ", std::to_string(cPos));
|
|
|
|
return getDNSstring(buffer, &cPos32);
|
|
|
|
}
|
|
|
|
// hexDump(moveable, length);
|
|
|
|
// hexDump(buffer, length);
|
2018-08-08 12:40:54 +00:00
|
|
|
// printf("dnsStringLen[%d]\n", length);
|
2018-11-03 13:19:18 +00:00
|
|
|
// llarp::LogInfo("dnsStringLen ", std::to_string(length));
|
2018-08-08 12:40:54 +00:00
|
|
|
if(!length)
|
|
|
|
return str;
|
2018-08-02 12:51:49 +00:00
|
|
|
while(length != 0)
|
|
|
|
{
|
2018-11-03 13:19:18 +00:00
|
|
|
// llarp::LogInfo("Reading ", std::to_string(length));
|
2018-08-02 12:51:49 +00:00
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
{
|
2018-11-03 13:19:18 +00:00
|
|
|
char c = *moveable++;
|
|
|
|
(*pos)++;
|
2018-08-02 12:51:49 +00:00
|
|
|
str.append(1, c);
|
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
if(*moveable == '\xc0')
|
|
|
|
{
|
|
|
|
moveable++;
|
|
|
|
(*pos)++; // forward one char
|
|
|
|
uint8_t cPos = *moveable; // read char
|
|
|
|
uint32_t cPos32 = cPos;
|
|
|
|
// llarp::LogInfo("Remaining [", str, "] is an reference at ", (int)cPos);
|
|
|
|
std::string addl = getDNSstring(buffer, &cPos32);
|
|
|
|
// llarp::LogInfo("Addl str [", addl, "]");
|
|
|
|
str += "." + addl;
|
|
|
|
// llarp::LogInfo("Final str [", str, "]");
|
|
|
|
(*pos)++; // move past reference
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
length = *moveable++;
|
|
|
|
(*pos)++;
|
|
|
|
if(length > 64)
|
|
|
|
llarp::LogError("bug detected");
|
|
|
|
// else
|
|
|
|
// hexDump(buffer, length);
|
|
|
|
// llarp::LogInfo("NextLen ", std::to_string(length));
|
2018-08-02 12:51:49 +00:00
|
|
|
if(length != 0)
|
|
|
|
str.append(1, '.');
|
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
// llarp::LogInfo("Returning ", str);
|
2018-08-02 12:51:49 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-10-09 12:40:53 +00:00
|
|
|
void
|
2019-07-30 23:42:13 +00:00
|
|
|
code_domain(char *&buffer, const std::string &domain) noexcept
|
2018-10-09 12:40:53 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
*buffer++ = 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 23:08:02 +00:00
|
|
|
void
|
2019-07-30 23:42:13 +00:00
|
|
|
vcode_domain(std::vector< byte_t > &bytes, const std::string &domain) noexcept
|
2018-11-18 23:08:02 +00:00
|
|
|
{
|
|
|
|
std::string::size_type start(0);
|
|
|
|
std::string::size_type end; // indexes
|
|
|
|
// llarp::LogInfo("domain [", domain, "]");
|
|
|
|
while((end = domain.find('.', start)) != std::string::npos)
|
|
|
|
{
|
|
|
|
bytes.push_back(end - start); // label length octet
|
|
|
|
for(std::string::size_type i = start; i < end; i++)
|
|
|
|
{
|
|
|
|
bytes.push_back(domain[i]); // label octets
|
|
|
|
// llarp::LogInfo("Writing ", domain[i], " at ", i);
|
|
|
|
}
|
|
|
|
start = end + 1; // Skip '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
// llarp::LogInfo("start ", start, " domain size ", domain.size());
|
|
|
|
|
|
|
|
bytes.push_back(domain.size() - start); // last label length octet
|
|
|
|
for(size_t i = start; i < domain.size(); i++)
|
|
|
|
{
|
|
|
|
bytes.push_back(domain[i]); // last label octets
|
|
|
|
// llarp::LogInfo("Writing ", domain[i], " at ", i);
|
|
|
|
}
|
|
|
|
bytes.push_back(0); // end it
|
|
|
|
}
|
|
|
|
|
|
|
|
// expects host order
|
|
|
|
void
|
2019-07-30 23:42:13 +00:00
|
|
|
vput16bits(std::vector< byte_t > &bytes, uint16_t value) noexcept
|
2018-11-18 23:08:02 +00:00
|
|
|
{
|
|
|
|
char buf[2] = {0};
|
|
|
|
char *write_buffer = buf;
|
|
|
|
htobe16buf(write_buffer, value);
|
|
|
|
bytes.push_back(buf[0]);
|
|
|
|
bytes.push_back(buf[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// expects host order
|
|
|
|
void
|
2019-07-30 23:42:13 +00:00
|
|
|
vput32bits(std::vector< byte_t > &bytes, uint32_t value) noexcept
|
2018-11-18 23:08:02 +00:00
|
|
|
{
|
|
|
|
char buf[4] = {0};
|
|
|
|
char *write_buffer = buf;
|
|
|
|
htobe32buf(write_buffer, value);
|
|
|
|
bytes.push_back(buf[0]);
|
|
|
|
bytes.push_back(buf[1]);
|
|
|
|
bytes.push_back(buf[2]);
|
|
|
|
bytes.push_back(buf[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_writeType(std::vector< byte_t > &bytes, llarp::dns::record *record)
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto *type1a = dynamic_cast< llarp::dns::type_1a * >(record);
|
2018-11-18 23:08:02 +00:00
|
|
|
if(type1a)
|
|
|
|
{
|
|
|
|
std::vector< byte_t > more_bytes = type1a->to_bytes();
|
|
|
|
llarp::LogDebug("[1]Adding ", more_bytes.size());
|
|
|
|
bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end());
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
auto *type2ns = dynamic_cast< llarp::dns::type_2ns * >(record);
|
2018-11-18 23:08:02 +00:00
|
|
|
if(type2ns)
|
|
|
|
{
|
|
|
|
std::vector< byte_t > more_bytes = type2ns->to_bytes();
|
|
|
|
llarp::LogDebug("[2]Adding ", more_bytes.size());
|
|
|
|
bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end());
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
auto *type5cname = dynamic_cast< llarp::dns::type_5cname * >(record);
|
2018-11-18 23:08:02 +00:00
|
|
|
if(type5cname)
|
|
|
|
{
|
|
|
|
std::vector< byte_t > more_bytes = type5cname->to_bytes();
|
|
|
|
llarp::LogDebug("[5]Adding ", more_bytes.size());
|
|
|
|
bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end());
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
auto *type12ptr = dynamic_cast< llarp::dns::type_12ptr * >(record);
|
2018-11-18 23:08:02 +00:00
|
|
|
if(type12ptr)
|
|
|
|
{
|
|
|
|
std::vector< byte_t > more_bytes = type12ptr->to_bytes();
|
|
|
|
llarp::LogDebug("[12]Adding ", more_bytes.size());
|
|
|
|
bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end());
|
|
|
|
}
|
2019-07-30 23:42:13 +00:00
|
|
|
auto *type15mx = dynamic_cast< llarp::dns::type_15mx * >(record);
|
2018-11-18 23:08:02 +00:00
|
|
|
if(type15mx)
|
|
|
|
{
|
|
|
|
std::vector< byte_t > more_bytes = type15mx->to_bytes();
|
|
|
|
llarp::LogDebug("[15]Adding ", more_bytes.size());
|
|
|
|
bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end());
|
|
|
|
}
|
2019-07-30 23:42:13 +00:00
|
|
|
auto *type16txt = dynamic_cast< llarp::dns::type_16txt * >(record);
|
2018-11-18 23:08:02 +00:00
|
|
|
if(type16txt)
|
|
|
|
{
|
|
|
|
std::vector< byte_t > more_bytes = type16txt->to_bytes();
|
|
|
|
llarp::LogDebug("[15]Adding ", more_bytes.size());
|
|
|
|
bytes.insert(bytes.end(), more_bytes.begin(), more_bytes.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector< byte_t >
|
|
|
|
packet2bytes(dns_packet &in)
|
|
|
|
{
|
|
|
|
std::vector< byte_t > write_buffer;
|
2018-12-01 14:35:11 +00:00
|
|
|
vput16bits(write_buffer, in.header.id);
|
2018-11-18 23:08:02 +00:00
|
|
|
|
2018-12-01 14:35:11 +00:00
|
|
|
int fields = (in.header.qr << 15); // QR => message type, 1 = response
|
|
|
|
fields += (in.header.opcode << 14); // I think opcode is always 0
|
2018-12-02 18:07:07 +00:00
|
|
|
fields += in.header.rcode; // response code (3 => not found, 0 = Ok)
|
2018-11-18 23:08:02 +00:00
|
|
|
vput16bits(write_buffer, fields);
|
|
|
|
|
|
|
|
// don't pull these from the header, trust what we actually have more
|
|
|
|
vput16bits(write_buffer, in.questions.size()); // QD (number of questions)
|
|
|
|
vput16bits(write_buffer, in.answers.size()); // AN (number of answers)
|
2018-11-22 00:39:09 +00:00
|
|
|
vput16bits(write_buffer, in.auth_rrs.size()); // NS (number of auth RRs)
|
2018-11-18 23:08:02 +00:00
|
|
|
vput16bits(write_buffer,
|
|
|
|
in.additional_rrs.size()); // AR (number of Additional RRs)
|
|
|
|
|
|
|
|
for(auto &it : in.questions)
|
|
|
|
{
|
|
|
|
// code question
|
|
|
|
vcode_domain(write_buffer, it->name);
|
|
|
|
vput16bits(write_buffer, it->type);
|
|
|
|
vput16bits(write_buffer, it->qClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto &it : in.answers)
|
|
|
|
{
|
|
|
|
// code answers
|
|
|
|
vcode_domain(write_buffer, it->name);
|
|
|
|
vput16bits(write_buffer, it->type);
|
|
|
|
vput16bits(write_buffer, it->aClass);
|
|
|
|
vput32bits(write_buffer, 1); // ttl
|
|
|
|
dns_writeType(write_buffer, it->record.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto &it : in.auth_rrs)
|
|
|
|
{
|
|
|
|
// code answers
|
|
|
|
vcode_domain(write_buffer, it->name);
|
|
|
|
vput16bits(write_buffer, it->type);
|
|
|
|
vput16bits(write_buffer, it->aClass);
|
|
|
|
vput32bits(write_buffer, 1); // ttl
|
|
|
|
dns_writeType(write_buffer, it->record.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto &it : in.additional_rrs)
|
|
|
|
{
|
|
|
|
// code answers
|
|
|
|
vcode_domain(write_buffer, it->name);
|
|
|
|
vput16bits(write_buffer, it->type);
|
|
|
|
vput16bits(write_buffer, it->aClass);
|
|
|
|
vput32bits(write_buffer, 1); // ttl
|
|
|
|
dns_writeType(write_buffer, it->record.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return write_buffer;
|
|
|
|
}
|
2018-07-30 04:38:14 +00:00
|
|
|
|
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
|
2019-07-30 23:42:13 +00:00
|
|
|
get16bits(const char *&buffer) noexcept
|
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
|
2019-07-30 23:42:13 +00:00
|
|
|
get32bits(const char *&buffer) noexcept
|
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
|
|
|
|
2018-12-01 14:35:11 +00:00
|
|
|
bool
|
2018-12-02 18:07:07 +00:00
|
|
|
decode_hdr(llarp_buffer_t *buffer, dns_msg_header *hdr)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-11-21 23:23:19 +00:00
|
|
|
uint16_t fields;
|
|
|
|
|
2018-12-01 14:35:11 +00:00
|
|
|
// reads as HOST byte order
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buffer->read_uint16(hdr->id))
|
2018-12-01 14:35:11 +00:00
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buffer->read_uint16(fields))
|
2018-12-01 14:35:11 +00:00
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buffer->read_uint16(hdr->qdCount))
|
2018-12-01 14:35:11 +00:00
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buffer->read_uint16(hdr->anCount))
|
2018-12-01 14:35:11 +00:00
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buffer->read_uint16(hdr->nsCount))
|
2018-12-01 14:35:11 +00:00
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buffer->read_uint16(hdr->arCount))
|
2018-12-01 14:35:11 +00:00
|
|
|
return false;
|
2018-11-22 00:39:09 +00:00
|
|
|
|
2018-11-21 23:23:19 +00:00
|
|
|
// decode fields into hdr
|
2018-11-22 00:39:09 +00:00
|
|
|
uint8_t lFields = (fields & 0x00FF) >> 0;
|
|
|
|
uint8_t hFields = (fields & 0xFF00) >> 8;
|
|
|
|
|
2018-11-21 23:23:19 +00:00
|
|
|
// process high byte
|
2018-07-27 04:07:22 +00:00
|
|
|
// 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-11-21 23:23:19 +00:00
|
|
|
// process low byte
|
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;
|
2018-12-01 14:35:11 +00:00
|
|
|
return true;
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
2018-07-27 04:07:22 +00:00
|
|
|
|
|
|
|
dns_msg_question *
|
2018-11-03 13:19:18 +00:00
|
|
|
decode_question(const char *buffer, uint32_t *pos)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto *question = new dns_msg_question;
|
2018-11-03 13:19:18 +00:00
|
|
|
|
|
|
|
std::string m_qName = getDNSstring(buffer, pos);
|
2018-11-03 15:32:48 +00:00
|
|
|
llarp::LogDebug("Got question name: ", m_qName);
|
2018-11-03 13:19:18 +00:00
|
|
|
|
|
|
|
const char *moveable = buffer;
|
|
|
|
moveable += *pos; // advance to position
|
2018-10-13 15:42:47 +00:00
|
|
|
question->name = m_qName;
|
2018-11-18 23:08:02 +00:00
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
question->type = get16bits(moveable);
|
|
|
|
(*pos) += 2;
|
|
|
|
question->qClass = get16bits(moveable);
|
|
|
|
(*pos) += 2;
|
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 *
|
2018-11-03 13:19:18 +00:00
|
|
|
decode_answer(const char *const buffer, uint32_t *pos)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
auto *answer = new dns_msg_answer;
|
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-11-03 13:19:18 +00:00
|
|
|
const char *moveable = buffer;
|
|
|
|
// llarp::LogDebug("Advancing to pos ", std::to_string(*pos));
|
|
|
|
moveable += (*pos); // advance to position
|
|
|
|
|
2018-11-18 23:08:02 +00:00
|
|
|
// hexDump(moveable, 12);
|
|
|
|
// hexDumpAt(buffer, *pos, 12);
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
if(*moveable == '\xc0')
|
|
|
|
{
|
|
|
|
// hexDump(moveable, 2);
|
|
|
|
moveable++;
|
|
|
|
(*pos)++;
|
|
|
|
uint8_t readAt = *moveable;
|
|
|
|
uint32_t readAt32 = readAt;
|
2018-11-03 15:32:48 +00:00
|
|
|
// llarp::LogInfo("Ref, skip. Read ", readAt32);
|
2018-11-03 13:19:18 +00:00
|
|
|
// hexDumpAt(buffer, readAt, 2);
|
|
|
|
answer->name = getDNSstring(buffer, &readAt32);
|
|
|
|
moveable++;
|
|
|
|
(*pos)++;
|
|
|
|
// hexDump(moveable, 10);
|
|
|
|
// hexDumpAt(buffer, *pos, 10);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get DNSString?
|
2018-11-13 01:19:01 +00:00
|
|
|
llarp::LogWarn("Need to parse string, implement me");
|
|
|
|
/*
|
|
|
|
uint32_t readAt32 = *pos;
|
|
|
|
answer->name = getDNSstring(buffer, &readAt32);
|
2018-11-18 23:08:02 +00:00
|
|
|
llarp::LogInfo("Parsed string ", answer->name, " read ",
|
|
|
|
std::to_string(readAt32)); moveable += readAt32; (*pos) += readAt32;
|
2018-11-13 01:19:01 +00:00
|
|
|
*/
|
2018-11-18 23:08:02 +00:00
|
|
|
// moveable++; (*pos)++;
|
2018-11-03 13:19:18 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
hexDump(moveable, 10);
|
|
|
|
uint8_t first = *moveable++; (*pos)++;
|
|
|
|
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-11-03 13:19:18 +00:00
|
|
|
moveable--; (*pos)--; // rewind buffer one byte
|
2018-08-02 12:51:49 +00:00
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
*/
|
|
|
|
// hexDump(moveable, 10);
|
|
|
|
|
|
|
|
answer->type = get16bits(moveable);
|
|
|
|
(*pos) += 2;
|
|
|
|
llarp::LogDebug("Answer Type: ", answer->type);
|
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");
|
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
answer->aClass = get16bits(moveable);
|
|
|
|
(*pos) += 2;
|
|
|
|
llarp::LogDebug("Answer Class: ", answer->aClass);
|
|
|
|
answer->ttl = get32bits(moveable);
|
|
|
|
(*pos) += 4;
|
|
|
|
llarp::LogDebug("Answer TTL: ", answer->ttl);
|
|
|
|
answer->rdLen = get16bits(moveable);
|
|
|
|
(*pos) += 2;
|
2018-11-06 14:06:09 +00:00
|
|
|
/*
|
2018-11-03 13:19:18 +00:00
|
|
|
llarp::LogDebug("Answer rdL: ", answer->rdLen, " at ",
|
|
|
|
std::to_string(*pos));
|
2018-11-06 14:06:09 +00:00
|
|
|
*/
|
2018-11-03 13:19:18 +00:00
|
|
|
// uint32_t cPos = moveable - buffer;
|
|
|
|
// llarp::LogInfo("pos at ", std::to_string(*pos), " calculated: ",
|
|
|
|
// std::to_string(cPos));
|
|
|
|
|
|
|
|
// hexDump(moveable, answer->rdLen);
|
|
|
|
// hexDumpAt(buffer, *pos, answer->rdLen);
|
2018-07-27 04:07:22 +00:00
|
|
|
if(answer->rdLen == 4)
|
|
|
|
{
|
2018-11-11 15:24:08 +00:00
|
|
|
answer->rData.resize(answer->rdLen);
|
|
|
|
memcpy(answer->rData.data(), moveable, answer->rdLen);
|
2018-11-06 14:06:09 +00:00
|
|
|
/*
|
2018-11-03 13:19:18 +00:00
|
|
|
llarp::LogDebug("Read ", std::to_string(answer->rData[0]), ".",
|
|
|
|
std::to_string(answer->rData[1]), ".",
|
|
|
|
std::to_string(answer->rData[2]), ".",
|
|
|
|
std::to_string(answer->rData[3]));
|
2018-11-06 14:06:09 +00:00
|
|
|
*/
|
2018-11-03 13:19:18 +00:00
|
|
|
moveable += answer->rdLen;
|
|
|
|
(*pos) += answer->rdLen; // advance the length
|
2018-11-22 00:39:09 +00:00
|
|
|
|
2018-11-21 23:23:19 +00:00
|
|
|
answer->record = std::make_unique< llarp::dns::type_1a >();
|
2018-11-18 23:08:02 +00:00
|
|
|
answer->record->parse(answer->rData);
|
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-11-03 13:19:18 +00:00
|
|
|
// FIXME: move this out of here, this shouldn't be responsible for decode
|
2018-08-02 12:51:49 +00:00
|
|
|
switch(answer->type)
|
|
|
|
{
|
2018-11-21 23:23:19 +00:00
|
|
|
case LLARP_DNS_RECTYPE_NS: // NS
|
2018-11-18 23:08:02 +00:00
|
|
|
{
|
|
|
|
std::string ns = getDNSstring(buffer, pos);
|
|
|
|
answer->rData.resize(ns.size());
|
|
|
|
memcpy(answer->rData.data(), ns.c_str(),
|
|
|
|
ns.size()); // raw copy rData
|
|
|
|
|
2018-11-14 13:36:30 +00:00
|
|
|
// don't really need to do anything here
|
|
|
|
moveable += answer->rdLen;
|
2018-11-18 23:08:02 +00:00
|
|
|
//(*pos) += answer->rdLen; // advance the length
|
2018-11-22 00:39:09 +00:00
|
|
|
|
2018-11-21 23:23:19 +00:00
|
|
|
answer->record = std::make_unique< llarp::dns::type_2ns >();
|
2018-11-18 23:08:02 +00:00
|
|
|
answer->record->parse(answer->rData);
|
|
|
|
}
|
2018-11-14 13:36:30 +00:00
|
|
|
break;
|
2018-11-21 23:23:19 +00:00
|
|
|
case LLARP_DNS_RECTYPE_CNAME: // CNAME
|
2018-11-18 23:08:02 +00:00
|
|
|
{
|
|
|
|
std::string cname = getDNSstring(buffer, pos);
|
|
|
|
llarp::LogDebug("CNAME ", cname);
|
|
|
|
answer->rData.resize(cname.size());
|
|
|
|
memcpy(answer->rData.data(), cname.c_str(), cname.size());
|
|
|
|
|
2018-11-03 13:19:18 +00:00
|
|
|
moveable += answer->rdLen;
|
2018-11-18 23:08:02 +00:00
|
|
|
//(*pos) += answer->rdLen; // advance the length
|
2018-11-22 00:39:09 +00:00
|
|
|
|
2018-11-21 23:23:19 +00:00
|
|
|
answer->record = std::make_unique< llarp::dns::type_5cname >();
|
2018-11-18 23:08:02 +00:00
|
|
|
answer->record->parse(answer->rData);
|
|
|
|
}
|
|
|
|
break;
|
2018-11-21 23:23:19 +00:00
|
|
|
case LLARP_DNS_RECTYPE_SOA: // 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-11-03 13:19:18 +00:00
|
|
|
moveable += answer->rdLen;
|
|
|
|
(*pos) += answer->rdLen; // advance the length
|
2018-08-02 12:51:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-11-21 23:23:19 +00:00
|
|
|
case LLARP_DNS_RECTYPE_PTR:
|
2018-09-23 16:57:53 +00:00
|
|
|
{
|
2018-11-03 13:19:18 +00:00
|
|
|
std::string revname = getDNSstring(buffer, pos);
|
2018-09-23 16:57:53 +00:00
|
|
|
llarp::LogInfo("revDNSname: ", revname);
|
2018-11-18 23:08:02 +00:00
|
|
|
// answer->rData = new uint8_t[answer->rdLen + 1];
|
|
|
|
answer->rData.resize(revname.size());
|
|
|
|
memcpy(answer->rData.data(), revname.c_str(), revname.size());
|
|
|
|
// answer->rData = (uint8_t *)strdup(revname.c_str()); // safer? nope
|
2018-11-03 13:19:18 +00:00
|
|
|
moveable += answer->rdLen;
|
2018-11-13 01:19:01 +00:00
|
|
|
//(*pos) += answer->rdLen; // advance the length
|
2018-11-18 23:08:02 +00:00
|
|
|
|
2018-11-21 23:23:19 +00:00
|
|
|
answer->record = std::make_unique< llarp::dns::type_12ptr >();
|
2018-11-18 23:08:02 +00:00
|
|
|
answer->record->parse(answer->rData);
|
2018-11-03 13:19:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-11-21 23:23:19 +00:00
|
|
|
case LLARP_DNS_RECTYPE_MX:
|
2018-11-03 13:19:18 +00:00
|
|
|
{
|
|
|
|
uint16_t priority = get16bits(moveable);
|
|
|
|
(*pos) += 2;
|
|
|
|
std::string revname = getDNSstring(buffer, pos);
|
|
|
|
llarp::LogInfo("MX: ", revname, " @ ", priority);
|
|
|
|
// answer->rData = new uint8_t[revname.length() + 1];
|
|
|
|
// memcpy(answer->rData, revname.c_str(), answer->rdLen);
|
2018-11-11 15:24:08 +00:00
|
|
|
answer->rData.resize(revname.size());
|
|
|
|
memcpy(answer->rData.data(), revname.c_str(), revname.size());
|
2018-11-03 13:19:18 +00:00
|
|
|
moveable += answer->rdLen - 2; // advance the length
|
|
|
|
// llarp::LogInfo("leaving at ", std::to_string(*pos));
|
|
|
|
// hexDumpAt(buffer, *pos, 5);
|
|
|
|
// hexDump(moveable, 5);
|
2018-11-18 23:08:02 +00:00
|
|
|
|
2018-11-21 23:23:19 +00:00
|
|
|
answer->record = std::make_unique< llarp::dns::type_15mx >();
|
2018-11-18 23:08:02 +00:00
|
|
|
answer->record->parse(answer->rData);
|
2018-11-03 13:19:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-11-21 23:23:19 +00:00
|
|
|
case LLARP_DNS_RECTYPE_TXT:
|
2018-11-03 13:19:18 +00:00
|
|
|
{
|
|
|
|
// hexDump(buffer, 5);
|
|
|
|
// std::string revname = getDNSstring((char *)buffer);
|
|
|
|
llarp::LogInfo("TXT: size: ", answer->rdLen);
|
2018-11-11 15:24:08 +00:00
|
|
|
answer->rData.resize(answer->rdLen);
|
|
|
|
memcpy(answer->rData.data(), moveable + 1, answer->rdLen);
|
2018-11-03 13:19:18 +00:00
|
|
|
moveable += answer->rdLen;
|
|
|
|
(*pos) += answer->rdLen; // advance the length
|
2018-11-18 23:08:02 +00:00
|
|
|
|
2018-11-21 23:23:19 +00:00
|
|
|
answer->record = std::make_unique< llarp::dns::type_16txt >();
|
2018-11-18 23:08:02 +00:00
|
|
|
answer->record->parse(answer->rData);
|
2018-09-23 16:57:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-11-03 13:19:18 +00:00
|
|
|
// type 28 AAAA
|
2018-08-02 12:51:49 +00:00
|
|
|
default:
|
2018-11-03 13:19:18 +00:00
|
|
|
moveable += answer->rdLen;
|
|
|
|
(*pos) += answer->rdLen; // advance the length
|
2018-09-20 10:07:16 +00:00
|
|
|
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
|
2019-07-30 23:42:13 +00:00
|
|
|
put16bits(char *&buffer, uint16_t value) noexcept
|
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
|
2019-07-30 23:42:13 +00:00
|
|
|
put32bits(char *&buffer, uint32_t value) noexcept
|
2018-07-27 04:07:22 +00:00
|
|
|
{
|
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,
|
2019-02-03 00:48:10 +00:00
|
|
|
const struct sockaddr *addr, ManagedBuffer buf)
|
2018-07-22 03:34:28 +00:00
|
|
|
{
|
2018-07-27 04:07:22 +00:00
|
|
|
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
|
2018-12-01 14:35:11 +00:00
|
|
|
dns_msg_header hdr;
|
2019-02-02 23:12:42 +00:00
|
|
|
if(!decode_hdr(&buf.underlying, &hdr))
|
2018-12-01 14:35:11 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to decode dns header");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// rewind
|
2019-02-02 23:12:42 +00:00
|
|
|
buf.underlying.cur = buf.underlying.base;
|
2018-12-01 14:35:11 +00:00
|
|
|
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-12-01 14:35:11 +00:00
|
|
|
if(hdr.qr)
|
2018-07-27 04:07:22 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("handling as dnsc answer");
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_handle_dnsc_recvfrom(udp, addr, buf);
|
2018-07-27 04:07:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogDebug("handling as dnsd question");
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_handle_dnsd_recvfrom(udp, addr, buf);
|
2018-07-27 04:07:22 +00:00
|
|
|
}
|
2018-07-22 03:34:28 +00:00
|
|
|
}
|
2018-08-01 09:04:40 +00:00
|
|
|
}
|