hook every dns for .loki and .snode when applicable

make {n,h}uint{32,16}_t templated type.
pull/576/head
Jeff Becker 5 years ago
parent 59e6a4bc3d
commit e060082441
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -178,7 +178,7 @@ namespace llarp
if(isV6)
{
rec.rr_type = qTypeAAAA;
ip.SIIT(rec.rData);
ip.ToV6(rec.rData);
}
else
{

@ -65,6 +65,12 @@ namespace llarp
return qname.substr(0, qname.find_last_of('.'));
}
bool
Question::HasTLD(const std::string& tld) const
{
return qname.rfind(tld) == (qname.size() - tld.size()) - 1;
}
std::ostream&
Question::print(std::ostream& stream, int level, int spaces) const
{

@ -44,6 +44,10 @@ namespace llarp
/// return qname with no trailing .
std::string
Name() const;
/// determine if we are using this TLD
bool
HasTLD(const std::string& tld) const;
};
inline std::ostream&

@ -73,11 +73,10 @@ namespace llarp
|| msg.questions[0].qtype == dns::qTypeCNAME
|| msg.questions[0].qtype == dns::qTypeAAAA)
{
if(msg.questions[0].IsName("localhost.loki")
|| msg.questions[0].IsName("random.snode"))
if(msg.questions[0].IsName("localhost.loki"))
return true;
if(msg.questions[0].HasTLD(".snode"))
return true;
service::Address addr;
return addr.FromString(msg.questions[0].Name(), ".snode");
}
else
return false;

@ -367,7 +367,6 @@ namespace llarp
return true;
}
else
// forward dns
msg.AddNXReply();
reply(msg);
@ -416,18 +415,11 @@ namespace llarp
llarp::service::Address addr;
if(msg.questions.size() == 1)
{
// hook random.snode
if(msg.questions[0].IsName("random.snode"))
/// hook every .loki
if(msg.questions[0].HasTLD(".loki"))
return true;
// hook localhost.loki
if(msg.questions[0].IsName("localhost.loki"))
return true;
const std::string name = msg.questions[0].Name();
// hook .loki
if(addr.FromString(name, ".loki"))
return true;
// hook .snode
if(addr.FromString(name, ".snode"))
/// hook every .snode
if(msg.questions[0].HasTLD(".snode"))
return true;
// hook any ranges we own
if(msg.questions[0].qtype == llarp::dns::qTypePTR)

@ -82,6 +82,26 @@ namespace llarp
{
namespace net
{
/// a network layer packet
struct NetPacket
{
virtual ~NetPacket(){};
virtual byte_t
Version() const = 0;
virtual byte_t
IPProto() const = 0;
virtual llarp_buffer_t
Buffer() = 0;
virtual llarp_buffer_t
ConstBuffer() const = 0;
};
/// an IPv4 Packet
/// TODO: make it implement NetPacket
struct IPv4Packet
{
static constexpr size_t MaxSize = 1500;

@ -5,15 +5,12 @@
#include <net/net.h>
#include <net/net.hpp>
#include <util/string_view.hpp>
#include <net/net_int.hpp>
#include <string>
namespace llarp
{
// fwd declr
struct huint32_t;
struct nuint32_t;
// real work
struct Addr
{

@ -1 +1,48 @@
#include <net/net_int.hpp>
namespace llarp
{
template <>
void
huint32_t::ToV6(V6Container& c)
{
c.resize(16);
std::fill(c.begin(), c.end(), 0);
htobe32buf(c.data() + 12, h);
c[11] = 0xff;
c[10] = 0xff;
}
template <>
std::string
huint32_t::ToString() const
{
uint32_t n = htonl(h);
char tmp[INET_ADDRSTRLEN] = {0};
if(!inet_ntop(AF_INET, (void*)&n, tmp, sizeof(tmp)))
return "";
return tmp;
}
template <>
std::string
nuint32_t::ToString() const
{
char tmp[INET_ADDRSTRLEN] = {0};
if(!inet_ntop(AF_INET, (void*)&n, tmp, sizeof(tmp)))
return "";
return tmp;
}
template<>
std::string
huint16_t::ToString() const
{
return std::to_string(h);
}
template<>
std::string
nuint16_t::ToString() const
{
return std::to_string(ntohs(n));
}
} // namespace llarp

@ -17,180 +17,170 @@
#include <stdlib.h> // for itoa
#include <iostream>
#include <util/endian.hpp>
#include <vector>
namespace llarp
{
// clang-format off
struct huint32_t
template < typename UInt_t >
struct huint_t
{
uint32_t h;
constexpr huint32_t
operator &(huint32_t x) const { return huint32_t{uint32_t(h & x.h)}; }
constexpr huint32_t
operator |(huint32_t x) const { return huint32_t{uint32_t(h | x.h)}; }
constexpr huint32_t
operator ^(huint32_t x) const { return huint32_t{uint32_t(h ^ x.h)}; }
UInt_t h;
constexpr huint32_t
operator ~() const { return huint32_t{uint32_t(~h)}; }
constexpr huint_t operator&(huint_t x) const
{
return huint_t{UInt_t(h & x.h)};
}
inline huint32_t operator ++() { ++h; return *this; }
inline huint32_t operator --() { --h; return *this; }
constexpr huint_t
operator|(huint_t x) const
{
return huint_t{UInt_t(h | x.h)};
}
constexpr bool operator <(huint32_t x) const { return h < x.h; }
constexpr bool operator ==(huint32_t x) const { return h == x.h; }
constexpr huint_t
operator^(huint_t x) const
{
return huint_t{UInt_t(h ^ x.h)};
}
friend std::ostream&
operator<<(std::ostream& out, const huint32_t& a)
constexpr huint_t
operator~() const
{
uint32_t n = htonl(a.h);
char tmp[INET_ADDRSTRLEN] = {0};
if(inet_ntop(AF_INET, (void*)&n, tmp, sizeof(tmp)))
{
out << tmp;
}
return out;
return huint_t{UInt_t(~h)};
}
template<typename Container>
void SIIT(Container & c)
inline huint_t
operator++()
{
c.resize(16);
std::fill(c.begin(), c.end(), 0);
htobe32buf(c.data() + 12, h);
c[11] = 0xff;
c[10] = 0xff;
++h;
return *this;
}
std::string ToString() const
inline huint_t
operator--()
{
uint32_t n = htonl(h);
char tmp[INET_ADDRSTRLEN] = {0};
if(!inet_ntop(AF_INET, (void*)&n, tmp, sizeof(tmp)))
return "";
return tmp;
--h;
return *this;
}
constexpr bool
operator<(huint_t x) const
{
return h < x.h;
}
constexpr bool
operator==(huint_t x) const
{
return h == x.h;
}
struct Hash
{
inline size_t
operator ()(huint32_t x) const
operator()(huint_t x) const
{
return std::hash< uint32_t >{}(x.h);
return std::hash< UInt_t >{}(x.h);
}
};
};
struct nuint32_t
{
uint32_t n;
using V6Container = std::vector< uint8_t >;
void
ToV6(V6Container& c);
constexpr nuint32_t
operator &(nuint32_t x) const { return nuint32_t{uint32_t(n & x.n)}; }
constexpr nuint32_t
operator |(nuint32_t x) const { return nuint32_t{uint32_t(n | x.n)}; }
constexpr nuint32_t
operator ^(nuint32_t x) const { return nuint32_t{uint32_t(n ^ x.n)}; }
std::string
ToString() const;
constexpr nuint32_t
operator ~() const { return nuint32_t{uint32_t(~n)}; }
friend std::ostream&
operator<<(std::ostream& out, const huint_t& i)
{
return out << i.ToString();
}
};
inline nuint32_t operator ++() { ++n; return *this; }
inline nuint32_t operator --() { --n; return *this; }
using huint128_t = huint_t< __uint128_t >;
using huint32_t = huint_t< uint32_t >;
using huint16_t = huint_t< uint16_t >;
constexpr bool operator <(nuint32_t x) const { return n < x.n; }
constexpr bool operator ==(nuint32_t x) const { return n == x.n; }
template < typename UInt_t >
struct nuint_t
{
UInt_t n;
friend std::ostream&
operator<<(std::ostream& out, const nuint32_t& a)
constexpr nuint_t operator&(nuint_t x) const
{
char tmp[INET_ADDRSTRLEN] = {0};
if(inet_ntop(AF_INET, (void*)&a.n, tmp, sizeof(tmp)))
{
out << tmp;
}
return out;
return nuint_t{UInt_t(n & x.n)};
}
struct Hash
constexpr nuint_t
operator|(nuint_t x) const
{
inline size_t
operator ()(nuint32_t x) const
{
return std::hash< uint32_t >{}(x.n);
}
};
};
return nuint_t{UInt_t(n | x.n)};
}
struct huint16_t
{
uint16_t h;
constexpr nuint_t
operator^(nuint_t x) const
{
return nuint_t{UInt_t(n ^ x.n)};
}
constexpr huint16_t
operator &(huint16_t x) const { return huint16_t{uint16_t(h & x.h)}; }
constexpr huint16_t
operator |(huint16_t x) const { return huint16_t{uint16_t(h | x.h)}; }
constexpr huint16_t
operator ~() const { return huint16_t{uint16_t(~h)}; }
constexpr nuint_t
operator~() const
{
return nuint_t{UInt_t(~n)};
}
inline huint16_t operator ++() { ++h; return *this; }
inline huint16_t operator --() { --h; return *this; }
inline nuint_t
operator++()
{
++n;
return *this;
}
inline nuint_t
operator--()
{
--n;
return *this;
}
constexpr bool operator <(huint16_t x) const { return h < x.h; }
constexpr bool operator ==(huint16_t x) const { return h == x.h; }
constexpr bool
operator<(nuint_t x) const
{
return n < x.n;
}
friend std::ostream&
operator<<(std::ostream& out, const huint16_t& a)
constexpr bool
operator==(nuint_t x) const
{
return out << a.h;
return n == x.n;
}
struct Hash
{
inline size_t
operator ()(huint16_t x) const
operator()(nuint_t x) const
{
return std::hash< uint16_t >{}(x.h);
return std::hash< UInt_t >{}(x.n);
}
};
};
struct nuint16_t
{
uint16_t n;
constexpr nuint16_t
operator &(nuint16_t x) const { return nuint16_t{uint16_t(n & x.n)}; }
constexpr nuint16_t
operator |(nuint16_t x) const { return nuint16_t{uint16_t(n | x.n)}; }
constexpr nuint16_t
operator ~() const { return nuint16_t{uint16_t(~n)}; }
inline nuint16_t operator ++() { ++n; return *this; }
inline nuint16_t operator --() { --n; return *this; }
using V6Container = std::vector< uint8_t >;
void
ToV6(V6Container& c);
constexpr bool operator <(nuint16_t x) const { return n < x.n; }
constexpr bool operator ==(nuint16_t x) const { return n == x.n; }
std::string
ToString() const;
friend std::ostream&
operator<<(std::ostream& out, const nuint16_t& a)
operator<<(std::ostream& out, const nuint_t& i)
{
return out << ntohs(a.n);
return out << i.ToString();
}
struct Hash
{
inline size_t
operator ()(nuint16_t x) const
{
return std::hash< uint16_t >{}(x.n);
}
};
};
// clang-format on
using nuint128_t = nuint_t< __uint128_t >;
using nuint32_t = nuint_t< uint32_t >;
using nuint16_t = nuint_t< uint16_t >;
static inline nuint32_t
xhtonl(huint32_t x)

@ -1118,18 +1118,18 @@ namespace llarp
}
m_PendingTraffic[remote].emplace_back(data, t);
// no converstation
return EnsurePathToService(remote,
[&](Address r, OutboundContext* c) {
if(c)
{
c->UpdateIntroSet(true);
for(auto& pending : m_PendingTraffic[r])
c->AsyncEncryptAndSendTo(
pending.Buffer(), pending.protocol);
}
m_PendingTraffic.erase(r);
},
5000, true);
return EnsurePathToService(
remote,
[&](Address r, OutboundContext* c) {
if(c)
{
c->UpdateIntroSet(true);
for(auto& pending : m_PendingTraffic[r])
c->AsyncEncryptAndSendTo(pending.Buffer(), pending.protocol);
}
m_PendingTraffic.erase(r);
},
5000, true);
}
void

@ -70,7 +70,7 @@ namespace llarp
Introduction::print(std::ostream& stream, int level, int spaces) const
{
Printer printer(stream, level, spaces);
printer.printAttribute("k", router);
printer.printAttribute("k", RouterID(router));
printer.printAttribute("p", pathID);
printer.printAttribute("v", version);
printer.printAttribute("x", expiresAt);

@ -11,6 +11,7 @@
struct DNSLibTest : public ::testing::Test
{
const std::string tld = ".loki";
std::array< byte_t, 1500 > mem;
llarp_buffer_t buf;
@ -27,6 +28,23 @@ struct DNSLibTest : public ::testing::Test
}
};
TEST_F(DNSLibTest, TestHasTLD)
{
llarp::dns::Question question;
question.qname = "a.loki.";
ASSERT_TRUE(question.HasTLD(tld));
question.qname = "a.loki..";
ASSERT_FALSE(question.HasTLD(tld));
question.qname = "bepis.loki.";
ASSERT_TRUE(question.HasTLD(tld));
question.qname = "bepis.logi.";
ASSERT_FALSE(question.HasTLD(tld));
question.qname = "a.net.";
ASSERT_FALSE(question.HasTLD(tld));
question.qname = "a.boki.";
ASSERT_FALSE(question.HasTLD(tld));
};
TEST_F(DNSLibTest, TestPTR)
{
llarp::huint32_t ip = {0};

Loading…
Cancel
Save