hello-dns/tdns/dns-storage.cc

121 lines
3.0 KiB
C++
Raw Normal View History

2018-04-09 04:20:11 +07:00
#include "dns-storage.hh"
using namespace std;
2018-04-10 01:49:37 +07:00
bool dnsname::makeRelative(const dnsname& root)
{
auto us = d_name, them=root.d_name;
while(!them.empty()) {
if(us.empty())
return false;
if(us.back() == them.back()) {
us.pop_back();
them.pop_back();
}
else
return false;
}
d_name = us;
return true;
}
2018-04-10 05:05:41 +07:00
const DNSNode* DNSNode::find(dnsname& name, dnsname& last, const DNSNode** passedZonecut, dnsname* zonecutname) const
2018-04-09 04:20:11 +07:00
{
cout<<"find for '"<<name<<"', last is now '"<<last<<"'"<<endl;
2018-04-10 05:05:41 +07:00
if(!last.empty() && rrsets.count(DNSType::NS)) {
cout<<" passed a zonecut, making note of this"<<endl;
if(passedZonecut)
*passedZonecut=this;
if(zonecutname)
*zonecutname=last;
2018-04-09 04:20:11 +07:00
}
if(name.empty()) {
cout<<"Empty lookup, returning this node or 0"<<endl;
if(!zone && rrsets.empty()) // only root zone can have this
return 0;
else
return this;
}
cout<<"Children at this node: ";
for(const auto& c: children) cout <<"'"<<c.first<<"' ";
cout<<endl;
auto iter = children.find(name.back());
cout<<"Looked for child called '"<<name.back()<<"'"<<endl;
if(iter == children.end()) {
cout<<"Found nothing, trying wildcard"<<endl;
iter = children.find("*");
if(iter == children.end()) {
cout<<"Still nothing, returning leaf"<<endl;
return this;
}
else {
2018-04-10 04:04:13 +07:00
cout<<" Had wildcard match, picking that, matching all labels"<<endl;
while(name.size() > 1) {
last.push_front(name.back());
name.pop_back();
}
2018-04-09 04:20:11 +07:00
}
}
2018-04-10 04:04:13 +07:00
cout<<" Had match, continuing to child '"<<iter->first<<"'"<<endl;
2018-04-09 04:20:11 +07:00
last.push_front(name.back());
name.pop_back();
2018-04-10 05:05:41 +07:00
return iter->second.find(name, last, passedZonecut, zonecutname);
2018-04-09 04:20:11 +07:00
}
DNSNode* DNSNode::add(dnsname name)
{
2018-04-10 04:04:13 +07:00
cout<<"Add called for '"<<name<<"'"<<endl;
2018-04-09 04:20:11 +07:00
if(name.size() == 1) {
cout<<" Last label, done with add. ";
if(children.count(name.front()))
cout<<"Label was present already"<<endl;
else
cout<<"Added label as new child"<<endl;
2018-04-09 04:20:11 +07:00
return &children[name.front()];
}
auto back = name.back();
name.pop_back();
auto iter = children.find(back);
if(iter == children.end()) {
2018-04-10 04:04:13 +07:00
cout<<"Inserting new child for "<<back<<", continuing add there"<<endl;
2018-04-09 04:20:11 +07:00
return children[back].add(name);
}
return iter->second.add(name);
}
dnsname operator+(const dnsname& a, const dnsname& b)
{
dnsname ret=a;
for(const auto& l : b.d_name)
ret.d_name.push_back(l);
return ret;
}
void DNSNode::visit(std::function<void(const dnsname& name, const DNSNode*)> visitor, dnsname name) const
{
visitor(name, this);
for(const auto& c : children)
c.second.visit(visitor, dnsname{c.first}+name);
}
// this should perform escaping rules!
std::ostream & operator<<(std::ostream &os, const dnslabel& d)
{
os<<d.d_s;
return os;
}
2018-04-09 04:20:11 +07:00
std::ostream & operator<<(std::ostream &os, const dnsname& d)
{
2018-04-10 04:04:13 +07:00
for(const auto& l : d.d_name)
2018-04-09 04:20:11 +07:00
os<<l<<".";
return os;
}
2018-04-11 17:45:26 +07:00
void DNSNode::addRRs(std::unique_ptr<RRGen>&&a)
{
rrsets[a->getType()].add(std::move(a));
}