]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/sortlist.cc
Merge pull request #7908 from omoerbeek/rec-4.1.14-changelog
[thirdparty/pdns.git] / pdns / sortlist.cc
CommitLineData
3e61e7f7 1#include "sortlist.hh"
2#include "dnsrecords.hh"
3
4void SortList::clear()
5{
6 d_sortlist.clear();
7}
8
9int SortList::getMaxOrder(const Netmask& formask) const
10{
11 int order=0;
12
13 auto place = d_sortlist.lookup(formask);
14 if(place && place->first == formask) {
15 for(const auto& o : place->second.d_orders)
16 order = std::max(order, o->second); // aki, shouldn't this be o.second?
17 }
18
19 return order;
20}
21
22void SortList::addEntry(const Netmask& formask, const Netmask& valmask, int order)
23{
24 if(order < 0) {
25 order=getMaxOrder(formask);
26 ++order;
27 }
28 // cout<<"Adding for netmask "<<formask.toString()<<" the order instruction that "<<valmask.toString()<<" is order "<<order<<endl;
6b4a4800 29 d_sortlist.insert(formask).second.d_orders.insert(valmask).second=order;
3e61e7f7 30}
31
19ad5533 32std::unique_ptr<SortListOrderCmp> SortList::getOrderCmp(const ComboAddress& who) const
3e61e7f7 33{
34 if(!d_sortlist.match(who)) {
35 return std::unique_ptr<SortListOrderCmp>();
36 }
37 auto fnd = d_sortlist.lookup(who);
38 // cerr<<"Returning sort order for "<<who.toString()<<", have "<<fnd->second.d_orders.size()<<" entries"<<endl;
39 return make_unique<SortListOrderCmp>(fnd->second);
40}
41
20d84f77 42// call this with **stable_sort**
3e61e7f7 43bool SortListOrderCmp::operator()(const DNSRecord& ar, const DNSRecord& br) const
44{
20d84f77 45 bool aAddr = (ar.d_type == QType::A || ar.d_type == QType::AAAA);
46 bool bAddr = (br.d_type == QType::A || br.d_type == QType::AAAA);
3e61e7f7 47
20d84f77 48 // anything address related is always 'larger', rest is equal
49
50 if(aAddr && !bAddr)
51 return false;
52 else if(!aAddr && bAddr)
53 return true;
54 else if(!aAddr && !bAddr)
98e9bc21 55 return false;
20d84f77 56
3e61e7f7 57
58 int aOrder=std::numeric_limits<int>::max();
59 int bOrder=aOrder;
60
61 ComboAddress a=getAddr(ar), b=getAddr(br);
62
63 if(d_slo.d_orders.match(a))
64 aOrder = d_slo.d_orders.lookup(a)->second;
65 else {
66 // cout<<"Could not find anything for "<<a.toString()<<" in our orders!"<<endl;
67 }
68 if(d_slo.d_orders.match(b))
69 bOrder = d_slo.d_orders.lookup(b)->second;
70 else {
71 // cout<<"Could not find anything for "<<b.toString()<<" in our orders!"<<endl;
72 }
73 return aOrder < bOrder;
74}