hello-dns/tdns/tdns.cc

416 lines
12 KiB
C++
Raw Normal View History

2018-04-01 23:31:41 +07:00
/* Goal: a fully standards compliant basic authoritative server. In <500 lines.
Non-goals: notifications, slaving zones, name compression, edns,
performance
*/
#include <cstdint>
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <stdexcept>
#include "sclasses.hh"
#include "dns.hh"
2018-04-02 18:25:19 +07:00
#include "safearray.hh"
2018-04-03 18:18:37 +07:00
#include <thread>
2018-04-01 23:31:41 +07:00
using namespace std;
typedef std::string dnslabel;
enum class RCode
{
2018-04-03 18:18:37 +07:00
Noerror = 0, Servfail =2, Nxdomain =3, Notimp = 4, Refused = 5
2018-04-01 23:31:41 +07:00
};
2018-04-03 18:18:37 +07:00
enum class DNSType : uint16_t
2018-04-01 23:31:41 +07:00
{
2018-04-03 18:18:37 +07:00
A = 1, NS = 2, CNAME = 5, SOA=6, AAAA = 28, IXFR = 251, AXFR = 252, ANY = 255
};
enum class DNSSection
{
Question, Answer, Authority, Additional
2018-04-01 23:31:41 +07:00
};
typedef deque<dnslabel> dnsname;
// this should perform escaping rules!
static std::ostream & operator<<(std::ostream &os, const dnsname& d)
{
for(const auto& l : d) {
os<<l<<".";
}
return os;
}
2018-04-03 18:18:37 +07:00
struct RRSet
{
vector<string> contents;
uint32_t ttl{3600};
};
2018-04-01 23:31:41 +07:00
struct DNSNode
{
2018-04-03 18:18:37 +07:00
const DNSNode* find(dnsname& name, dnsname& last, bool* passedZonecut=0) const;
2018-04-01 23:31:41 +07:00
DNSNode* add(dnsname name);
map<dnslabel, DNSNode> children;
2018-04-03 18:18:37 +07:00
map<DNSType, RRSet > rrsets;
2018-04-01 23:31:41 +07:00
DNSNode* zone{0}; // if this is set, this node is a zone
};
2018-04-03 18:18:37 +07:00
const DNSNode* DNSNode::find(dnsname& name, dnsname& last, bool* passedZonecut) const
2018-04-01 23:31:41 +07:00
{
2018-04-02 18:25:19 +07:00
cout<<"find for '"<<name<<"', last is now '"<<last<<"'"<<endl;
2018-04-03 18:18:37 +07:00
if(!last.empty() && passedZonecut && rrsets.count(DNSType::NS)) {
2018-04-02 18:25:19 +07:00
*passedZonecut=true;
}
2018-04-01 23:31:41 +07:00
if(name.empty()) {
2018-04-02 18:25:19 +07:00
cout<<"Empty lookup, returning this node or 0"<<endl;
2018-04-01 23:31:41 +07:00
if(!zone && rrsets.empty()) // only root zone can have this
return 0;
else
return this;
}
2018-04-02 18:25:19 +07:00
cout<<"Children at this node: ";
for(const auto& c: children) cout <<"'"<<c.first<<"' ";
cout<<endl;
2018-04-01 23:31:41 +07:00
auto iter = children.find(name.back());
cout<<"Looked for child called '"<<name.back()<<"'"<<endl;
if(iter == children.end()) {
2018-04-02 18:25:19 +07:00
cout<<"Found nothing, trying wildcard"<<endl;
iter = children.find("*");
if(iter == children.end()) {
cout<<"Still nothing, returning leaf"<<endl;
return this;
}
else {
cout<<"Had wildcard match, following"<<endl;
}
2018-04-01 23:31:41 +07:00
}
2018-04-02 18:25:19 +07:00
cout<<"Had match, continuing to child '"<<iter->first<<"'"<<endl;
2018-04-01 23:31:41 +07:00
last.push_front(name.back());
name.pop_back();
2018-04-02 18:25:19 +07:00
return iter->second.find(name, last, passedZonecut);
2018-04-01 23:31:41 +07:00
}
DNSNode* DNSNode::add(dnsname name)
{
cout<<"Add for '"<<name<<"'"<<endl;
if(name.size() == 1) {
cout<<"Last label, adding "<<name.front()<<endl;
return &children[name.front()];
}
auto back = name.back();
name.pop_back();
auto iter = children.find(back);
if(iter == children.end()) {
cout<<"Inserting new child for "<<back<<endl;
return children[back].add(name);
}
return iter->second.add(name);
}
struct DNSMessage
{
struct dnsheader dh=dnsheader{};
2018-04-02 18:25:19 +07:00
SafeArray<500> payload;
2018-04-01 23:31:41 +07:00
dnsname getName();
2018-04-03 18:18:37 +07:00
void getQuestion(dnsname& name, DNSType& type);
void setQuestion(const dnsname& name, DNSType type);
void putRR(DNSSection section, const dnsname& name, DNSType type, uint32_t ttl, const std::string& rr);
string serialize() const;
}; // __attribute__((packed));
2018-04-01 23:31:41 +07:00
dnsname DNSMessage::getName()
{
dnsname name;
for(;;) {
2018-04-02 18:25:19 +07:00
uint8_t labellen=payload.getUInt8();
2018-04-01 23:31:41 +07:00
if(labellen > 63)
throw std::runtime_error("Got a compressed label");
if(!labellen) // end of dnsname
break;
2018-04-02 18:25:19 +07:00
dnslabel label = payload.getBlob(labellen);
2018-04-01 23:31:41 +07:00
name.push_back(label);
}
return name;
}
2018-04-03 18:18:37 +07:00
void DNSMessage::getQuestion(dnsname& name, DNSType& type)
2018-04-01 23:31:41 +07:00
{
name=getName();
2018-04-03 18:18:37 +07:00
type=(DNSType)payload.getUInt16();
2018-04-01 23:31:41 +07:00
}
2018-04-03 18:18:37 +07:00
void putName(auto& payload, const dnsname& name)
2018-04-01 23:31:41 +07:00
{
for(const auto& l : name) {
2018-04-03 18:18:37 +07:00
if(l.size() > 63)
throw std::runtime_error("Can't emit a label larger than 63 characters");
2018-04-02 18:25:19 +07:00
payload.putUInt8(l.size());
payload.putBlob(l);
2018-04-01 23:31:41 +07:00
}
2018-04-02 18:25:19 +07:00
payload.putUInt8(0);
2018-04-01 23:31:41 +07:00
}
2018-04-03 18:18:37 +07:00
void DNSMessage::putRR(DNSSection section, const dnsname& name, DNSType type, uint32_t ttl, const std::string& content)
2018-04-01 23:31:41 +07:00
{
2018-04-03 18:18:37 +07:00
putName(payload, name);
payload.putUInt16((int)type); payload.putUInt16(1);
2018-04-02 18:25:19 +07:00
payload.putUInt32(ttl);
payload.putUInt16(content.size()); // check for overflow!
payload.putBlob(content);
2018-04-03 18:18:37 +07:00
switch(section) {
case DNSSection::Question:
throw runtime_error("Can't add questions to a DNS Message with putRR");
case DNSSection::Answer:
dh.ancount = htons(ntohs(dh.ancount) + 1);
break;
case DNSSection::Authority:
dh.nscount = htons(ntohs(dh.nscount) + 1);
break;
case DNSSection::Additional:
dh.arcount = htons(ntohs(dh.arcount) + 1);
break;
}
2018-04-01 23:31:41 +07:00
}
2018-04-03 18:18:37 +07:00
void DNSMessage::setQuestion(const dnsname& name, DNSType type)
2018-04-01 23:31:41 +07:00
{
2018-04-03 18:18:37 +07:00
putName(payload, name);
payload.putUInt16((uint16_t)type);
2018-04-02 18:25:19 +07:00
payload.putUInt16(1); // class
2018-04-01 23:31:41 +07:00
}
string DNSMessage::serialize() const
{
2018-04-02 18:25:19 +07:00
return string((const char*)this, (const char*)this + sizeof(dnsheader) + payload.payloadpos);
2018-04-01 23:31:41 +07:00
}
2018-04-03 18:18:37 +07:00
2018-04-01 23:31:41 +07:00
static_assert(sizeof(DNSMessage) == 516, "dnsmessage size must be 516");
2018-04-02 18:25:19 +07:00
std::string serializeDNSName(const dnsname& dn)
{
std::string ret;
for(const auto & l : dn) {
ret.append(1, l.size());
ret+=l;
}
ret.append(1, (char)0);
return ret;
}
2018-04-03 18:18:37 +07:00
std::string serializeSOARecord(const dnsname& mname, const dnsname& rname, uint32_t serial, uint32_t minimum=3600, uint32_t refresh=10800, uint32_t retry=3600, uint32_t expire=604800)
{
SafeArray<256> sa;
putName(sa, mname);
putName(sa, rname);
sa.putUInt32(serial);
sa.putUInt32(refresh);
sa.putUInt32(retry);
sa.putUInt32(expire);
sa.putUInt32(minimum);
return sa.serialize();
}
std::string serializeARecord(const std::string& src)
{
ComboAddress ca(src);
if(ca.sin4.sin_family != AF_INET)
throw std::runtime_error("Could not convert '"+src+"' to an IPv4 address");
auto p = (const char*)&ca.sin4.sin_addr.s_addr;
return std::string(p, p+4);
}
std::string serializeAAAARecord(const std::string& src)
{
ComboAddress ca(src);
if(ca.sin4.sin_family != AF_INET6)
throw std::runtime_error("Could not convert '"+src+"' to an IPv6 address");
auto p = (const char*)ca.sin6.sin6_addr.s6_addr;
return std::string(p, p+16);
}
2018-04-02 18:25:19 +07:00
dnsname operator+(const dnsname& a, const dnsname& b)
{
dnsname ret=a;
for(const auto& l : b)
ret.push_back(l);
return ret;
}
2018-04-03 18:18:37 +07:00
void udpThread(ComboAddress local, const DNSNode* zones)
2018-04-01 23:31:41 +07:00
{
Socket udplistener(local.sin4.sin_family, SOCK_DGRAM);
SBind(udplistener, local);
for(;;) {
ComboAddress remote(local);
DNSMessage dm;
string message = SRecvfrom(udplistener, sizeof(dm), remote);
if(message.size() < sizeof(dnsheader)) {
cerr<<"Dropping query from "<<remote.toStringWithPort()<<", too short"<<endl;
continue;
}
memcpy(&dm, message.c_str(), message.size());
2018-04-03 18:18:37 +07:00
if(dm.dh.qr) {
2018-04-01 23:31:41 +07:00
cerr<<"Dropping non-query from "<<remote.toStringWithPort()<<endl;
2018-04-03 18:18:37 +07:00
continue;
2018-04-01 23:31:41 +07:00
}
dnsname name;
2018-04-03 18:18:37 +07:00
DNSType type;
2018-04-01 23:31:41 +07:00
dm.getQuestion(name, type);
2018-04-03 18:18:37 +07:00
cout<<"Received a query from "<<remote.toStringWithPort()<<" for "<<name<<" and type "<<(int)type<<endl;
2018-04-01 23:31:41 +07:00
2018-04-03 18:18:37 +07:00
2018-04-01 23:31:41 +07:00
DNSMessage response;
response.dh = dm.dh;
response.dh.ad = 0;
response.dh.ra = 0;
response.dh.aa = 0;
response.dh.qr = 1;
response.dh.ancount = response.dh.arcount = response.dh.nscount = 0;
response.setQuestion(name, type);
2018-04-03 18:18:37 +07:00
if(type == DNSType::AXFR) {
cout<<"Query was for AXFR or IXFR over UDP, can't do that"<<endl;
response.dh.rcode = (int)RCode::Servfail;
SSendto(udplistener, response.serialize(), remote);
continue;
}
if(dm.dh.opcode != 0) {
cout<<"Query had non-zero opcode "<<dm.dh.opcode<<", sending NOTIMP"<<endl;
response.dh.rcode = (int)RCode::Notimp;
SSendto(udplistener, response.serialize(), remote);
continue;
}
2018-04-01 23:31:41 +07:00
dnsname zone;
2018-04-03 18:18:37 +07:00
auto fnd = zones->find(name, zone);
2018-04-01 23:31:41 +07:00
if(fnd && fnd->zone) {
2018-04-02 18:25:19 +07:00
cout<<"---\nBest zone: "<<zone<<", name now "<<name<<", loaded: "<<(void*)fnd->zone<<endl;
2018-04-01 23:31:41 +07:00
response.dh.aa = 1;
auto bestzone = fnd->zone;
dnsname searchname(name), lastnode;
2018-04-02 18:25:19 +07:00
bool passedZonecut=false;
auto node = bestzone->find(searchname, lastnode, &passedZonecut);
2018-04-03 18:18:37 +07:00
if(passedZonecut)
response.dh.aa = false;
2018-04-02 18:25:19 +07:00
if(!node) {
2018-04-01 23:31:41 +07:00
cout<<"Found nothing in zone '"<<zone<<"' for lhs '"<<name<<"'"<<endl;
}
2018-04-02 18:25:19 +07:00
else if(!searchname.empty()) {
cout<<"This was a partial match, searchname now "<<searchname<<endl;
2018-04-03 18:18:37 +07:00
2018-04-02 18:25:19 +07:00
for(const auto& rr: node->rrsets) {
2018-04-03 18:18:37 +07:00
cout<<" Have type "<<(int)rr.first<<endl;
2018-04-02 18:25:19 +07:00
}
2018-04-03 18:18:37 +07:00
auto iter = node->rrsets.find(DNSType::NS);
if(iter != node->rrsets.end() && passedZonecut) {
cout<<"Have delegation"<<endl;
const auto& rrset = iter->second;
for(const auto& rr : rrset.contents) {
response.putRR(DNSSection::Answer, lastnode+zone, DNSType::NS, rrset.ttl, rr);
2018-04-02 18:25:19 +07:00
}
// should do additional processing here
}
2018-04-03 18:18:37 +07:00
else {
cout<<"This is an NXDOMAIN situation"<<endl;
const auto& rrset = fnd->zone->rrsets[DNSType::SOA];
response.dh.rcode = (int)RCode::Nxdomain;
response.putRR(DNSSection::Authority, zone, DNSType::SOA, rrset.ttl, rrset.contents[0]);
}
2018-04-02 18:25:19 +07:00
}
2018-04-01 23:31:41 +07:00
else {
2018-04-02 18:25:19 +07:00
cout<<"Found something in zone '"<<zone<<"' for lhs '"<<name<<"', searchname now '"<<searchname<<"', lastnode '"<<lastnode<<"', passedZonecut="<<passedZonecut<<endl;
2018-04-03 18:18:37 +07:00
auto iter = node->rrsets.cbegin();
if(type == DNSType::ANY) {
for(const auto& t : node->rrsets) {
const auto& rrset = t.second;
for(const auto& rr : rrset.contents) {
response.putRR(DNSSection::Answer, lastnode+zone, t.first, rrset.ttl, rr);
}
}
}
else if(iter = node->rrsets.find(type), iter != node->rrsets.end()) {
const auto& rrset = iter->second;
for(const auto& rr : rrset.contents) {
response.putRR(DNSSection::Answer, lastnode+zone, type, rrset.ttl, rr);
2018-04-01 23:31:41 +07:00
}
}
2018-04-03 18:18:37 +07:00
else if(iter = node->rrsets.find(DNSType::CNAME), iter != node->rrsets.end()) {
2018-04-02 18:25:19 +07:00
cout<<"We do have a CNAME!"<<endl;
2018-04-03 18:18:37 +07:00
const auto& rrset = iter->second;
for(const auto& rr : rrset.contents) {
response.putRR(DNSSection::Answer, lastnode+zone, DNSType::CNAME, rrset.ttl, rr);
2018-04-01 23:31:41 +07:00
}
2018-04-03 18:18:37 +07:00
cout<<" We should actually follow this, at least within our zone"<<endl;
}
else {
cout<<"Node exists, qtype doesn't, NOERROR situation, inserting SOA"<<endl;
const auto& rrset = fnd->zone->rrsets[DNSType::SOA];
response.putRR(DNSSection::Answer, zone, DNSType::SOA, rrset.ttl, rrset.contents[0]);
2018-04-01 23:31:41 +07:00
}
}
}
else {
response.dh.rcode = (uint8_t)RCode::Refused;
}
SSendto(udplistener, response.serialize(), remote);
}
2018-04-03 18:18:37 +07:00
}
void loadZones(DNSNode& zones)
{
auto zone = zones.add({"powerdns", "org"});
zone->zone = new DNSNode(); // XXX ICK
zone->zone->rrsets[DNSType::SOA]={{serializeSOARecord({"ns1", "powerdns", "org"}, {"admin", "powerdns", "org"}, 1)}};
zone->zone->rrsets[DNSType::A]={{serializeARecord("1.2.3.4")}, 300};
zone->zone->rrsets[DNSType::AAAA]={{serializeAAAARecord("::1"), serializeAAAARecord("2001::1")}, 900};
zone->zone->rrsets[DNSType::NS]={{serializeDNSName({"ns1", "powerdns", "org"})}, 300};
zone->zone->add({"www"})->rrsets[DNSType::CNAME]={{serializeDNSName({"server1","powerdns","org"})}};
zone->zone->add({"server1"})->rrsets[DNSType::A]={{serializeARecord("213.244.168.210")}};
// zone->zone->add({"*"})->rrsets[(dnstype)DNSType::A]={"\x05\x06\x07\x08"};
zone->zone->add({"fra"})->rrsets[DNSType::NS]={{serializeDNSName({"ns1","fra","powerdns","org"})}};
zone->zone->add({"ns1", "fra"})->rrsets[DNSType::A]={{serializeARecord("12.13.14.15")}, 86400};
}
int main(int argc, char** argv)
{
ComboAddress local(argv[1], 53);
DNSNode zones;
loadZones(zones);
thread udpServer(udpThread, local, &zones);
// thread tcpServer(tcpThread, local, &zones);
udpServer.join();
// tcpServer.join();
2018-04-01 23:31:41 +07:00
}