mirror of https://github.com/oxen-io/lokinet
add dnslib
parent
baa94ec345
commit
1d1bde2e88
@ -0,0 +1,5 @@
|
||||
#include <llarp/dns/name.hpp>
|
||||
#include <llarp/dns/rr.hpp>
|
||||
#include <llarp/dns/serialize.hpp>
|
||||
#include <llarp/dns/message.hpp>
|
||||
#include <llarp/dns/server.hpp>
|
@ -0,0 +1,60 @@
|
||||
#ifndef LLARP_DNS_MESSAGE_HPP
|
||||
#define LLARP_DNS_MESSAGE_HPP
|
||||
#include <llarp/dns/serialize.hpp>
|
||||
#include <llarp/dns/rr.hpp>
|
||||
#include <llarp/dns/question.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
using MsgID_t = uint16_t;
|
||||
using Fields_t = uint16_t;
|
||||
using Count_t = uint16_t;
|
||||
|
||||
struct MessageHeader : public Serialize
|
||||
{
|
||||
MsgID_t id;
|
||||
Fields_t fields;
|
||||
Count_t qd_count;
|
||||
Count_t an_count;
|
||||
Count_t ns_count;
|
||||
Count_t ar_count;
|
||||
|
||||
bool
|
||||
Encode(llarp_buffer_t* buf) const override;
|
||||
|
||||
bool
|
||||
Decode(llarp_buffer_t* buf) override;
|
||||
};
|
||||
|
||||
struct Message : public Serialize
|
||||
{
|
||||
void
|
||||
UpdateHeader();
|
||||
|
||||
Message&
|
||||
AddNXReply();
|
||||
|
||||
Message&
|
||||
AddINReply(llarp::huint32_t addr);
|
||||
|
||||
Message&
|
||||
AddAReply(std::string name);
|
||||
|
||||
bool
|
||||
Encode(llarp_buffer_t* buf) const override;
|
||||
|
||||
bool
|
||||
Decode(llarp_buffer_t* buf) override;
|
||||
|
||||
MessageHeader hdr;
|
||||
std::vector< Question > questions;
|
||||
std::vector< ResourceRecord > answers;
|
||||
std::vector< ResourceRecord > authorities;
|
||||
std::vector< ResourceRecord > additional;
|
||||
};
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,28 @@
|
||||
#ifndef LLARP_DNS_NAME_HPP
|
||||
#define LLARP_DNS_NAME_HPP
|
||||
|
||||
#include <string>
|
||||
#include <llarp/buffer.h>
|
||||
#include <llarp/net_int.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
using Name_t = std::string;
|
||||
|
||||
/// decode name from buffer
|
||||
bool
|
||||
DecodeName(llarp_buffer_t* buf, Name_t& name);
|
||||
|
||||
/// encode name to buffer
|
||||
bool
|
||||
EncodeName(llarp_buffer_t* buf, const Name_t& name);
|
||||
|
||||
bool
|
||||
DecodePTR(const Name_t& name, huint32_t& ip);
|
||||
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,4 @@
|
||||
#ifndef LLARP_DNS_QUERY_HPP
|
||||
#define LLARP_DNS_QUERY_HPP
|
||||
|
||||
#endif
|
@ -0,0 +1,29 @@
|
||||
#ifndef LLARP_DNS_QUESTION_HPP
|
||||
#define LLARP_DNS_QUESTION_HPP
|
||||
#include <llarp/dns/serialize.hpp>
|
||||
#include <llarp/dns/name.hpp>
|
||||
#include <llarp/net_int.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
using QType_t = llarp::huint16_t;
|
||||
using QClass_t = llarp::huint16_t;
|
||||
|
||||
struct Question : public Serialize
|
||||
{
|
||||
bool
|
||||
Encode(llarp_buffer_t* buf) const override;
|
||||
|
||||
bool
|
||||
Decode(llarp_buffer_t* buf) override;
|
||||
|
||||
Name_t qname;
|
||||
QType_t qtype;
|
||||
QClass_t qclass;
|
||||
};
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,35 @@
|
||||
#ifndef LLARP_DNS_RR_HPP
|
||||
#define LLARP_DNS_RR_HPP
|
||||
#include <llarp/dns/name.hpp>
|
||||
#include <llarp/dns/serialize.hpp>
|
||||
#include <llarp/net_int.hpp>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
using RRClass_t = llarp::huint16_t;
|
||||
using RRType_t = llarp::huint16_t;
|
||||
using RR_RData_t = std::vector< byte_t >;
|
||||
using RR_TTL_t = llarp::huint32_t;
|
||||
|
||||
struct ResourceRecord : public Serialize
|
||||
{
|
||||
bool
|
||||
Encode(llarp_buffer_t* buf) const override;
|
||||
|
||||
bool
|
||||
Decode(llarp_buffer_t* buf) override;
|
||||
|
||||
Name_t rr_name;
|
||||
RRType_t rr_type;
|
||||
RRClass_t rr_class;
|
||||
RR_TTL_t ttl;
|
||||
RR_RData_t rData;
|
||||
};
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,39 @@
|
||||
#ifndef LLARP_DNS_SERIALIZE_HPP
|
||||
#define LLARP_DNS_SERIALIZE_HPP
|
||||
#include <llarp/buffer.h>
|
||||
#include <vector>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
/// base type for serializable dns entities
|
||||
struct Serialize
|
||||
{
|
||||
/// encode entity to buffer
|
||||
virtual bool
|
||||
Encode(llarp_buffer_t* buf) const = 0;
|
||||
|
||||
/// decode entity from buffer
|
||||
virtual bool
|
||||
Decode(llarp_buffer_t* buf) = 0;
|
||||
};
|
||||
|
||||
template < typename Int_t >
|
||||
bool
|
||||
EncodeInt(llarp_buffer_t* buf, const Int_t& i);
|
||||
|
||||
template < typename Int_t >
|
||||
bool
|
||||
DecodeInt(llarp_buffer_t* buf, Int_t& i);
|
||||
|
||||
bool
|
||||
EncodeRData(llarp_buffer_t* buf, const std::vector< byte_t >& rdata);
|
||||
|
||||
bool
|
||||
DecodeRData(llarp_buffer_t* buf, std::vector< byte_t >& rdata);
|
||||
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,93 @@
|
||||
#ifndef LLARP_DNS_SERVER_HPP
|
||||
#define LLARP_DNS_SERVER_HPP
|
||||
#include <llarp/string_view.hpp>
|
||||
#include <llarp/dns/message.hpp>
|
||||
|
||||
#include <llarp/ev.h>
|
||||
#include <llarp/net.hpp>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
/// handler of dns query hooking
|
||||
struct IQueryHandler
|
||||
{
|
||||
/// return true if we should hook this message
|
||||
virtual bool
|
||||
ShouldHookDNSMessage(const Message& msg) const = 0;
|
||||
|
||||
/// handle a hooked message
|
||||
virtual bool
|
||||
HandleHookedDNSMessage(Message query,
|
||||
std::function< void(Message) > sendReply) = 0;
|
||||
};
|
||||
|
||||
struct Proxy
|
||||
{
|
||||
Proxy(llarp_ev_loop* loop, IQueryHandler* handler);
|
||||
|
||||
bool
|
||||
Start(const llarp::Addr& addr,
|
||||
const std::vector< llarp::Addr >& resolvers);
|
||||
|
||||
void
|
||||
Stop();
|
||||
|
||||
private:
|
||||
/// low level packet handler
|
||||
static void
|
||||
HandleUDPRecv(llarp_udp_io*, const struct sockaddr*, llarp_buffer_t);
|
||||
|
||||
/// low level ticker
|
||||
static void
|
||||
HandleTick(llarp_udp_io*);
|
||||
|
||||
void
|
||||
Tick(llarp_time_t now);
|
||||
|
||||
void
|
||||
HandlePkt(llarp::Addr from, llarp_buffer_t* buf);
|
||||
|
||||
void
|
||||
SendMessageTo(llarp::Addr to, Message msg);
|
||||
|
||||
llarp::Addr
|
||||
PickRandomResolver() const;
|
||||
|
||||
private:
|
||||
llarp_udp_io m_UDP;
|
||||
llarp_ev_loop* m_Loop;
|
||||
IQueryHandler* m_QueryHandler;
|
||||
std::vector< llarp::Addr > m_Resolvers;
|
||||
|
||||
struct TX
|
||||
{
|
||||
MsgID_t txid;
|
||||
llarp::Addr from;
|
||||
|
||||
bool
|
||||
operator==(const TX& other) const
|
||||
{
|
||||
return txid == other.txid && from == other.from;
|
||||
}
|
||||
|
||||
struct Hash
|
||||
{
|
||||
size_t
|
||||
operator()(const TX& t) const noexcept
|
||||
{
|
||||
return t.txid ^ llarp::Addr::Hash()(t.from);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// maps tx to who to send reply to
|
||||
std::unordered_map< TX, llarp::Addr, TX::Hash > m_Forwarded;
|
||||
};
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,23 @@
|
||||
#ifndef LLARP_DNS_STRING_HPP
|
||||
#define LLARP_DNS_STRING_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
using name_t = std::string;
|
||||
|
||||
/// decode name from buffer
|
||||
bool
|
||||
decode_name(llarp_buffer_t* buf, name_t& name);
|
||||
|
||||
/// encode name to buffer
|
||||
bool
|
||||
encode_name(llarp_buffer_t* buf, const name_t& name);
|
||||
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,199 @@
|
||||
#ifndef LLARP_NET_INT_HPP
|
||||
#define LLARP_NET_INT_HPP
|
||||
|
||||
// for addrinfo
|
||||
#ifndef _WIN32
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#else
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#define inet_aton(x, y) inet_pton(AF_INET, x, y)
|
||||
#endif
|
||||
|
||||
#include <stdlib.h> // for itoa
|
||||
#include <iostream>
|
||||
#include <llarp/net.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
// clang-format off
|
||||
|
||||
struct huint32_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)}; }
|
||||
|
||||
constexpr huint32_t
|
||||
operator ~() const { return huint32_t{uint32_t(~h)}; }
|
||||
|
||||
inline huint32_t operator ++() { ++h; return *this; }
|
||||
inline huint32_t operator --() { --h; return *this; }
|
||||
|
||||
constexpr bool operator <(huint32_t x) const { return h < x.h; }
|
||||
constexpr bool operator ==(huint32_t x) const { return h == x.h; }
|
||||
|
||||
friend std::ostream&
|
||||
operator<<(std::ostream& out, const huint32_t& a)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
struct Hash
|
||||
{
|
||||
inline size_t
|
||||
operator ()(huint32_t x) const
|
||||
{
|
||||
return std::hash< uint32_t >{}(x.h);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
struct nuint32_t
|
||||
{
|
||||
uint32_t 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)}; }
|
||||
constexpr nuint32_t
|
||||
operator ^(nuint32_t x) const { return nuint32_t{uint32_t(n ^ x.n)}; }
|
||||
|
||||
constexpr nuint32_t
|
||||
operator ~() const { return nuint32_t{uint32_t(~n)}; }
|
||||
|
||||
inline nuint32_t operator ++() { ++n; return *this; }
|
||||
inline nuint32_t operator --() { --n; return *this; }
|
||||
|
||||
constexpr bool operator <(nuint32_t x) const { return n < x.n; }
|
||||
constexpr bool operator ==(nuint32_t x) const { return n == x.n; }
|
||||
|
||||
friend std::ostream&
|
||||
operator<<(std::ostream& out, const nuint32_t& a)
|
||||
{
|
||||
char tmp[INET_ADDRSTRLEN] = {0};
|
||||
if(inet_ntop(AF_INET, (void*)&a.n, tmp, sizeof(tmp)))
|
||||
{
|
||||
out << tmp;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
struct Hash
|
||||
{
|
||||
inline size_t
|
||||
operator ()(nuint32_t x) const
|
||||
{
|
||||
return std::hash< uint32_t >{}(x.n);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
struct huint16_t
|
||||
{
|
||||
uint16_t h;
|
||||
|
||||
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)}; }
|
||||
|
||||
inline huint16_t operator ++() { ++h; return *this; }
|
||||
inline huint16_t operator --() { --h; return *this; }
|
||||
|
||||
constexpr bool operator <(huint16_t x) const { return h < x.h; }
|
||||
constexpr bool operator ==(huint16_t x) const { return h == x.h; }
|
||||
|
||||
friend std::ostream&
|
||||
operator<<(std::ostream& out, const huint16_t& a)
|
||||
{
|
||||
return out << a.h;
|
||||
}
|
||||
|
||||
struct Hash
|
||||
{
|
||||
inline size_t
|
||||
operator ()(huint16_t x) const
|
||||
{
|
||||
return std::hash< uint16_t >{}(x.h);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
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; }
|
||||
|
||||
constexpr bool operator <(nuint16_t x) const { return n < x.n; }
|
||||
constexpr bool operator ==(nuint16_t x) const { return n == x.n; }
|
||||
|
||||
friend std::ostream&
|
||||
operator<<(std::ostream& out, const nuint16_t& a)
|
||||
{
|
||||
return out << ntohs(a.n);
|
||||
}
|
||||
|
||||
struct Hash
|
||||
{
|
||||
inline size_t
|
||||
operator ()(nuint16_t x) const
|
||||
{
|
||||
return std::hash< uint16_t >{}(x.n);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// clang-format on
|
||||
|
||||
static inline nuint32_t
|
||||
xhtonl(huint32_t x)
|
||||
{
|
||||
return nuint32_t{htonl(x.h)};
|
||||
}
|
||||
|
||||
static inline huint32_t
|
||||
xntohl(nuint32_t x)
|
||||
{
|
||||
return huint32_t{ntohl(x.n)};
|
||||
}
|
||||
|
||||
static inline nuint16_t
|
||||
xhtons(huint16_t x)
|
||||
{
|
||||
return nuint16_t{htons(x.h)};
|
||||
}
|
||||
|
||||
static inline huint16_t
|
||||
xntohs(nuint16_t x)
|
||||
{
|
||||
return huint16_t{ntohs(x.n)};
|
||||
}
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,162 @@
|
||||
#include <llarp/dns/message.hpp>
|
||||
#include <llarp/endian.hpp>
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
bool
|
||||
MessageHeader::Encode(llarp_buffer_t* buf) const
|
||||
{
|
||||
if(!EncodeInt(buf, id))
|
||||
return false;
|
||||
if(!EncodeInt(buf, fields))
|
||||
return false;
|
||||
if(!EncodeInt(buf, qd_count))
|
||||
return false;
|
||||
if(!EncodeInt(buf, an_count))
|
||||
return false;
|
||||
if(!EncodeInt(buf, ns_count))
|
||||
return false;
|
||||
return EncodeInt(buf, ar_count);
|
||||
}
|
||||
|
||||
bool
|
||||
MessageHeader::Decode(llarp_buffer_t* buf)
|
||||
{
|
||||
if(!DecodeInt(buf, id))
|
||||
return false;
|
||||
if(!DecodeInt(buf, fields))
|
||||
return false;
|
||||
if(!DecodeInt(buf, qd_count))
|
||||
return false;
|
||||
if(!DecodeInt(buf, an_count))
|
||||
return false;
|
||||
if(!DecodeInt(buf, ns_count))
|
||||
return false;
|
||||
return DecodeInt(buf, ar_count);
|
||||
}
|
||||
|
||||
bool
|
||||
Message::Encode(llarp_buffer_t* buf) const
|
||||
{
|
||||
if(!hdr.Encode(buf))
|
||||
return false;
|
||||
|
||||
for(const auto& question : questions)
|
||||
if(!question.Encode(buf))
|
||||
return false;
|
||||
|
||||
for(const auto& answer : answers)
|
||||
if(!answer.Encode(buf))
|
||||
return false;
|
||||
|
||||
for(const auto& auth : authorities)
|
||||
if(!auth.Encode(buf))
|
||||
return false;
|
||||
|
||||
for(const auto& rr : additional)
|
||||
if(!rr.Encode(buf))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
Message::Decode(llarp_buffer_t* buf)
|
||||
{
|
||||
if(!hdr.Decode(buf))
|
||||
return false;
|
||||
questions.resize(hdr.qd_count);
|
||||
answers.resize(hdr.an_count);
|
||||
authorities.resize(hdr.ns_count);
|
||||
additional.resize(hdr.ar_count);
|
||||
|
||||
for(auto& qd : questions)
|
||||
if(!qd.Decode(buf))
|
||||
return false;
|
||||
|
||||
for(auto& an : answers)
|
||||
if(!an.Decode(buf))
|
||||
return false;
|
||||
|
||||
for(auto& ns : authorities)
|
||||
if(!ns.Decode(buf))
|
||||
return false;
|
||||
|
||||
for(auto& ar : additional)
|
||||
if(!ar.Decode(buf))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
Message::UpdateHeader()
|
||||
{
|
||||
hdr.qd_count = questions.size();
|
||||
hdr.an_count = answers.size();
|
||||
hdr.ns_count = authorities.size();
|
||||
hdr.ar_count = additional.size();
|
||||
}
|
||||
|
||||
Message&
|
||||
Message::AddINReply(llarp::huint32_t ip)
|
||||
{
|
||||
if(questions.size())
|
||||
{
|
||||
hdr.fields |= (1 << 15);
|
||||
const auto& question = questions[0];
|
||||
answers.emplace_back();
|
||||
auto& rec = answers.back();
|
||||
rec.rr_name = question.qname;
|
||||
rec.rr_type.h = 1;
|
||||
rec.rr_class = question.qclass;
|
||||
rec.ttl.h = 1;
|
||||
rec.rData.resize(4);
|
||||
htobe32buf(rec.rData.data(), ip.h);
|
||||
UpdateHeader();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Message&
|
||||
Message::AddAReply(std::string name)
|
||||
{
|
||||
if(questions.size())
|
||||
{
|
||||
hdr.fields |= (1 << 15);
|
||||
const auto& question = questions[0];
|
||||
answers.emplace_back();
|
||||
auto& rec = answers.back();
|
||||
rec.rr_name = question.qname;
|
||||
rec.rr_type = question.qtype;
|
||||
rec.rr_class.h = 1;
|
||||
rec.ttl.h = 1;
|
||||
rec.rData.resize(name.size());
|
||||
memcpy(rec.rData.data(), name.c_str(), rec.rData.size());
|
||||
UpdateHeader();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Message&
|
||||
Message::AddNXReply()
|
||||
{
|
||||
if(questions.size())
|
||||
{
|
||||
hdr.fields |= (1 << 15) | (1 << 3);
|
||||
const auto& question = questions[0];
|
||||
answers.emplace_back();
|
||||
auto& nx = answers.back();
|
||||
nx.rr_name = question.qname;
|
||||
nx.rr_type = question.qtype;
|
||||
nx.rr_class = question.qclass;
|
||||
nx.ttl.h = 1;
|
||||
nx.rData.resize(1);
|
||||
nx.rData.data()[0] = 0;
|
||||
UpdateHeader();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
@ -0,0 +1,82 @@
|
||||
#include <llarp/dns/name.hpp>
|
||||
#include <llarp/net.hpp>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
bool
|
||||
DecodeName(llarp_buffer_t* buf, Name_t& name)
|
||||
{
|
||||
if(llarp_buffer_size_left(*buf) < 1)
|
||||
return false;
|
||||
std::stringstream ss;
|
||||
size_t l;
|
||||
do
|
||||
{
|
||||
l = *buf->cur;
|
||||
buf->cur++;
|
||||
if(llarp_buffer_size_left(*buf) < l)
|
||||
return false;
|
||||
if(l)
|
||||
ss << Name_t((const char*)buf->cur, l);
|
||||
ss << ".";
|
||||
} while(l);
|
||||
name = ss.str();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
EncodeName(llarp_buffer_t* buf, const Name_t& name)
|
||||
{
|
||||
std::stringstream ss(name);
|
||||
if(name.size() == 0 || name[name.size() - 1] != '.')
|
||||
ss << ".";
|
||||
std::string part;
|
||||
while(std::getline(ss, part, '.'))
|
||||
{
|
||||
uint8_t l;
|
||||
if(part.size() > 63)
|
||||
return false;
|
||||
l = part.size();
|
||||
*buf->cur = l;
|
||||
buf->cur++;
|
||||
if(llarp_buffer_size_left(*buf) < l)
|
||||
return false;
|
||||
memcpy(buf->cur, part.c_str(), l);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
DecodePTR(const Name_t& name, huint32_t& ip)
|
||||
{
|
||||
auto pos = name.find(".in-addr.arpa.");
|
||||
if(pos == Name_t::npos)
|
||||
return false;
|
||||
std::string sub = name.substr(0, pos - 1);
|
||||
if(std::count(sub.begin(), sub.end(), '.') == 4)
|
||||
{
|
||||
uint8_t a, b, c, d;
|
||||
pos = sub.find('.');
|
||||
d = atoi(sub.substr(0, pos).c_str());
|
||||
sub = sub.substr(pos);
|
||||
pos = sub.find('.');
|
||||
c = atoi(sub.substr(0, pos).c_str());
|
||||
sub = sub.substr(pos);
|
||||
pos = sub.find('.');
|
||||
b = atoi(sub.substr(0, pos).c_str());
|
||||
sub = sub.substr(pos);
|
||||
pos = sub.find('.');
|
||||
a = atoi(sub.substr(0, pos).c_str());
|
||||
ip = llarp::ipaddr_ipv4_bits(a, b, c, d);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
@ -0,0 +1,27 @@
|
||||
#include <llarp/dns/question.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
bool
|
||||
Question::Encode(llarp_buffer_t* buf) const
|
||||
{
|
||||
if(!EncodeName(buf, qname))
|
||||
return false;
|
||||
if(!EncodeInt(buf, qtype))
|
||||
return false;
|
||||
return EncodeInt(buf, qclass);
|
||||
}
|
||||
|
||||
bool
|
||||
Question::Decode(llarp_buffer_t* buf)
|
||||
{
|
||||
if(!DecodeName(buf, qname))
|
||||
return false;
|
||||
if(!DecodeInt(buf, qtype))
|
||||
return false;
|
||||
return DecodeInt(buf, qclass);
|
||||
}
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
@ -0,0 +1,35 @@
|
||||
#include <llarp/dns/rr.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
bool
|
||||
ResourceRecord::Encode(llarp_buffer_t* buf) const
|
||||
{
|
||||
if(!EncodeName(buf, rr_name))
|
||||
return false;
|
||||
if(!EncodeInt(buf, rr_type))
|
||||
return false;
|
||||
if(!EncodeInt(buf, rr_class))
|
||||
return false;
|
||||
if(!EncodeInt(buf, ttl))
|
||||
return false;
|
||||
return EncodeRData(buf, rData);
|
||||
}
|
||||
|
||||
bool
|
||||
ResourceRecord::Decode(llarp_buffer_t* buf)
|
||||
{
|
||||
if(!DecodeName(buf, rr_name))
|
||||
return false;
|
||||
if(!DecodeInt(buf, rr_type))
|
||||
return false;
|
||||
if(!DecodeInt(buf, rr_class))
|
||||
return false;
|
||||
if(!DecodeInt(buf, ttl))
|
||||
return false;
|
||||
return DecodeRData(buf, rData);
|
||||
}
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
@ -0,0 +1,76 @@
|
||||
#include <llarp/dns/serialize.hpp>
|
||||
#include <llarp/net_int.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
template <>
|
||||
bool
|
||||
DecodeInt(llarp_buffer_t* buf, llarp::huint32_t& i)
|
||||
{
|
||||
return llarp_buffer_read_uint32(buf, &i.h);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool
|
||||
EncodeInt(llarp_buffer_t* buf, const llarp::huint32_t& i)
|
||||
{
|
||||
return llarp_buffer_put_uint32(buf, i.h);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool
|
||||
DecodeInt(llarp_buffer_t* buf, llarp::huint16_t& i)
|
||||
{
|
||||
return llarp_buffer_read_uint16(buf, &i.h);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool
|
||||
EncodeInt(llarp_buffer_t* buf, const llarp::huint16_t& i)
|
||||
{
|
||||
return llarp_buffer_put_uint16(buf, i.h);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool
|
||||
DecodeInt(llarp_buffer_t* buf, uint16_t& i)
|
||||
{
|
||||
return llarp_buffer_read_uint16(buf, &i);
|
||||
}
|
||||
|
||||
template <>
|
||||
bool
|
||||
EncodeInt(llarp_buffer_t* buf, const uint16_t& i)
|
||||
{
|
||||
return llarp_buffer_put_uint16(buf, i);
|
||||
}
|
||||
|
||||
bool
|
||||
EncodeRData(llarp_buffer_t* buf, const std::vector< byte_t >& v)
|
||||
{
|
||||
if(v.size() > 65536)
|
||||
return false;
|
||||
llarp::huint16_t len;
|
||||
len.h = v.size();
|
||||
return EncodeInt(buf, len) && llarp_buffer_write(buf, v.data(), v.size());
|
||||
}
|
||||
|
||||
bool
|
||||
DecodeRData(llarp_buffer_t* buf, std::vector< byte_t >& v)
|
||||
{
|
||||
llarp::huint16_t len = {0};
|
||||
if(!DecodeInt(buf, len))
|
||||
return false;
|
||||
size_t left = llarp_buffer_size_left(*buf);
|
||||
if(left < len.h)
|
||||
return false;
|
||||
v.resize(size_t(len.h));
|
||||
memcpy(v.data(), buf->cur, len.h);
|
||||
buf->cur += len.h;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
@ -0,0 +1,112 @@
|
||||
#include <llarp/dns/server.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace dns
|
||||
{
|
||||
Proxy::Proxy(llarp_ev_loop* loop, IQueryHandler* h)
|
||||
: m_Loop(loop), m_QueryHandler(h)
|
||||
{
|
||||
m_UDP.user = this;
|
||||
m_UDP.tick = &HandleTick;
|
||||
m_UDP.recvfrom = &HandleUDPRecv;
|
||||
}
|
||||
|
||||
void
|
||||
Proxy::Stop()
|
||||
{
|
||||
}
|
||||
|
||||
bool
|
||||
Proxy::Start(const llarp::Addr& addr,
|
||||
const std::vector< llarp::Addr >& resolvers)
|
||||
{
|
||||
m_Resolvers.clear();
|
||||
m_Resolvers = resolvers;
|
||||
if(m_Resolvers.size() == 0)
|
||||
{
|
||||
llarp::LogError("no upstream dns provide specified");
|
||||
return false;
|
||||
}
|
||||
return llarp_ev_add_udp(m_Loop, &m_UDP, addr) == 0;
|
||||
}
|
||||
|
||||
void
|
||||
Proxy::HandleUDPRecv(llarp_udp_io* u, const sockaddr* from,
|
||||
llarp_buffer_t buf)
|
||||
{
|
||||
static_cast< Proxy* >(u->user)->HandlePkt(*from, &buf);
|
||||
}
|
||||
|
||||
llarp::Addr
|
||||
Proxy::PickRandomResolver() const
|
||||
{
|
||||
size_t sz = m_Resolvers.size();
|
||||
if(sz == 0)
|
||||
return llarp::Addr("1.1.1.1", 52);
|
||||
if(sz == 1)
|
||||
return m_Resolvers[0];
|
||||
auto itr = m_Resolvers.begin();
|
||||
std::advance(itr, llarp_randint() % sz);
|
||||
return *itr;
|
||||
}
|
||||
|
||||
void
|
||||
Proxy::HandleTick(llarp_udp_io*)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
Proxy::SendMessageTo(llarp::Addr to, Message msg)
|
||||
{
|
||||
byte_t tmp[1500] = {0};
|
||||
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
||||
if(msg.Encode(&buf))
|
||||
{
|
||||
buf.sz = buf.cur - buf.base;
|
||||
buf.cur = buf.base;
|
||||
llarp_ev_udp_sendto(&m_UDP, to, buf);
|
||||
}
|
||||
else
|
||||
llarp::LogWarn("failed to encode dns message when sending");
|
||||
}
|
||||
|
||||
void
|
||||
Proxy::HandlePkt(llarp::Addr from, llarp_buffer_t* pkt)
|
||||
{
|
||||
Message msg;
|
||||
if(!msg.Decode(pkt))
|
||||
{
|
||||
llarp::LogWarn("failed to handle dns message from ", from);
|
||||
llarp::DumpBuffer(*pkt);
|
||||
return;
|
||||
}
|
||||
if(m_QueryHandler && m_QueryHandler->ShouldHookDNSMessage(msg))
|
||||
{
|
||||
if(!m_QueryHandler->HandleHookedDNSMessage(
|
||||
msg,
|
||||
std::bind(&Proxy::SendMessageTo, this, from,
|
||||
std::placeholders::_1)))
|
||||
{
|
||||
llarp::LogWarn("failed to handle hooked dns");
|
||||
}
|
||||
return;
|
||||
}
|
||||
TX tx = {msg.hdr.id, from};
|
||||
auto itr = m_Forwarded.find(tx);
|
||||
if(itr == m_Forwarded.end())
|
||||
{
|
||||
// new forwarded query
|
||||
tx.from = PickRandomResolver();
|
||||
m_Forwarded[tx] = from;
|
||||
SendMessageTo(tx.from, msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
SendMessageTo(itr->second, msg);
|
||||
m_Forwarded.erase(itr);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dns
|
||||
} // namespace llarp
|
@ -0,0 +1,84 @@
|
||||
#include <llarp/dns/dns.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
#include <algorithm>
|
||||
|
||||
struct DNSLibTest : public ::testing::Test
|
||||
{
|
||||
byte_t mem[1500] = {0};
|
||||
llarp_buffer_t buf;
|
||||
|
||||
void
|
||||
SetUp()
|
||||
{
|
||||
buf = llarp::StackBuffer< decltype(mem) >(mem);
|
||||
}
|
||||
|
||||
void
|
||||
TearDown()
|
||||
{
|
||||
Rewind();
|
||||
}
|
||||
|
||||
void
|
||||
Rewind()
|
||||
{
|
||||
buf.cur = buf.base;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(DNSLibTest, TestEncodeDecode_RData)
|
||||
{
|
||||
static constexpr size_t rdatasize = 32;
|
||||
llarp::dns::RR_RData_t rdata(rdatasize);
|
||||
std::fill(rdata.begin(), rdata.end(), 'a');
|
||||
|
||||
llarp::dns::RR_RData_t other_rdata;
|
||||
|
||||
ASSERT_TRUE(llarp::dns::EncodeRData(&buf, rdata));
|
||||
ASSERT_TRUE(buf.cur - buf.base == rdatasize + sizeof(uint16_t));
|
||||
Rewind();
|
||||
ASSERT_TRUE(llarp::dns::DecodeRData(&buf, other_rdata));
|
||||
ASSERT_TRUE(rdata == other_rdata);
|
||||
};
|
||||
|
||||
TEST_F(DNSLibTest, TestEncode_huint16)
|
||||
{
|
||||
llarp::huint16_t i = {0x1122};
|
||||
ASSERT_TRUE(llarp::dns::EncodeInt(&buf, i));
|
||||
ASSERT_TRUE(buf.cur - buf.base == sizeof(uint16_t));
|
||||
ASSERT_TRUE(buf.base[0] == 0x11);
|
||||
ASSERT_TRUE(buf.base[1] == 0x22);
|
||||
};
|
||||
|
||||
TEST_F(DNSLibTest, TestEncode_huint32)
|
||||
{
|
||||
llarp::huint32_t i = {0x11223344};
|
||||
ASSERT_TRUE(llarp::dns::EncodeInt(&buf, i));
|
||||
ASSERT_TRUE(buf.cur - buf.base == sizeof(uint32_t));
|
||||
ASSERT_TRUE(buf.base[0] == 0x11);
|
||||
ASSERT_TRUE(buf.base[1] == 0x22);
|
||||
ASSERT_TRUE(buf.base[2] == 0x33);
|
||||
ASSERT_TRUE(buf.base[3] == 0x44);
|
||||
};
|
||||
|
||||
TEST_F(DNSLibTest, TestDecode_huint16)
|
||||
{
|
||||
llarp::huint16_t i = {0};
|
||||
buf.base[0] = 0x11;
|
||||
buf.base[1] = 0x22;
|
||||
ASSERT_TRUE(llarp::dns::DecodeInt(&buf, i));
|
||||
ASSERT_TRUE(buf.cur - buf.base == sizeof(uint16_t));
|
||||
ASSERT_TRUE(i.h == 0x1122);
|
||||
};
|
||||
|
||||
TEST_F(DNSLibTest, TestDecode_huint32)
|
||||
{
|
||||
llarp::huint32_t i = {0};
|
||||
buf.base[0] = 0x11;
|
||||
buf.base[1] = 0x22;
|
||||
buf.base[2] = 0x33;
|
||||
buf.base[3] = 0x44;
|
||||
ASSERT_TRUE(llarp::dns::DecodeInt(&buf, i));
|
||||
ASSERT_TRUE(buf.cur - buf.base == sizeof(uint32_t));
|
||||
ASSERT_TRUE(i.h == 0x11223344);
|
||||
};
|
Loading…
Reference in New Issue