]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/sortlist.cc
Merge pull request #14021 from Habbie/auth-lua-join-whitespace
[thirdparty/pdns.git] / pdns / sortlist.cc
1 #include "sortlist.hh"
2 #include "dnsrecords.hh"
3
4 void SortList::clear()
5 {
6 d_sortlist.clear();
7 }
8
9 int 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
22 void 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;
29 d_sortlist.insert(formask).second.d_orders.insert(valmask).second=order;
30 }
31
32 std::unique_ptr<SortListOrderCmp> SortList::getOrderCmp(const ComboAddress& who) const
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
42 // call this with **stable_sort**
43 bool SortListOrderCmp::operator()(const DNSRecord& ar, const DNSRecord& br) const
44 {
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);
47
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)
55 return false;
56
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 }