2018-12-12 00:58:08 +00:00
|
|
|
#include <dns/question.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-02-24 23:46:37 +00:00
|
|
|
#include <util/printer.hpp>
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dns
|
|
|
|
{
|
2018-12-04 16:16:43 +00:00
|
|
|
Question::Question(Question&& other)
|
|
|
|
: qname(std::move(other.qname))
|
|
|
|
, qtype(std::move(other.qtype))
|
|
|
|
, qclass(std::move(other.qclass))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Question::Question(const Question& other)
|
|
|
|
: qname(other.qname), qtype(other.qtype), qclass(other.qclass)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
bool
|
|
|
|
Question::Encode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
if(!EncodeName(buf, qname))
|
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buf->put_uint16(qtype))
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
return buf->put_uint16(qclass);
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Question::Decode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
if(!DecodeName(buf, qname))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to decode name");
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buf->read_uint16(qtype))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to decode type");
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buf->read_uint16(qclass))
|
2018-12-04 16:16:43 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to decode class");
|
2018-12-03 22:22:59 +00:00
|
|
|
return false;
|
2018-12-04 16:16:43 +00:00
|
|
|
}
|
|
|
|
return true;
|
2018-12-03 22:22:59 +00:00
|
|
|
}
|
2019-02-24 23:46:37 +00:00
|
|
|
|
2019-04-26 12:11:34 +00:00
|
|
|
bool
|
|
|
|
Question::IsName(const std::string& other) const
|
|
|
|
{
|
|
|
|
// does other have a . at the end?
|
|
|
|
if(other.find_last_of('.') == (other.size() - 1))
|
|
|
|
return other == qname;
|
2019-07-06 17:03:40 +00:00
|
|
|
// no, add it and retry
|
|
|
|
return IsName(other + ".");
|
2019-04-26 12:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Question::Name() const
|
|
|
|
{
|
|
|
|
return qname.substr(0, qname.find_last_of('.'));
|
|
|
|
}
|
|
|
|
|
2019-05-01 13:40:10 +00:00
|
|
|
bool
|
|
|
|
Question::HasTLD(const std::string& tld) const
|
|
|
|
{
|
2019-05-05 13:51:48 +00:00
|
|
|
return qname.find(tld) != std::string::npos
|
|
|
|
&& qname.rfind(tld) == (qname.size() - tld.size()) - 1;
|
2019-05-01 13:40:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
std::ostream&
|
|
|
|
Question::print(std::ostream& stream, int level, int spaces) const
|
|
|
|
{
|
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
printer.printAttribute("qname", qname);
|
|
|
|
printer.printAttributeAsHex("qtype", qtype);
|
|
|
|
printer.printAttributeAsHex("qclass", qclass);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
} // namespace dns
|
|
|
|
} // namespace llarp
|