File: sortlist.cc

package info (click to toggle)
pdns-recursor 4.1.11-1+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,936 kB
  • sloc: cpp: 54,211; javascript: 26,587; sh: 11,872; makefile: 453; xml: 37
file content (74 lines) | stat: -rw-r--r-- 2,110 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "sortlist.hh"
#include "dnsrecords.hh"

void SortList::clear()
{
  d_sortlist.clear();
}

int SortList::getMaxOrder(const Netmask& formask) const
{
  int order=0;
  
  auto place = d_sortlist.lookup(formask);
  if(place && place->first == formask) {
    for(const auto& o : place->second.d_orders) 
      order = std::max(order, o->second); // aki, shouldn't this be o.second?
  }
  
  return order;
}

void SortList::addEntry(const Netmask& formask, const Netmask& valmask, int order)
{
  if(order < 0) {
    order=getMaxOrder(formask);
    ++order;
  }
  //  cout<<"Adding for netmask "<<formask.toString()<<" the order instruction that "<<valmask.toString()<<" is order "<<order<<endl;
  d_sortlist.insert(formask).second.d_orders.insert(valmask).second=order;
}

std::unique_ptr<SortListOrderCmp> SortList::getOrderCmp(const ComboAddress& who) const
{
  if(!d_sortlist.match(who)) {
    return std::unique_ptr<SortListOrderCmp>();
  }
  auto fnd = d_sortlist.lookup(who);
  //  cerr<<"Returning sort order for "<<who.toString()<<", have "<<fnd->second.d_orders.size()<<" entries"<<endl;
  return make_unique<SortListOrderCmp>(fnd->second);
}

// call this with **stable_sort**
bool SortListOrderCmp::operator()(const DNSRecord& ar, const DNSRecord& br) const
{
  bool aAddr = (ar.d_type == QType::A || ar.d_type == QType::AAAA);
  bool bAddr = (br.d_type == QType::A || br.d_type == QType::AAAA);

  // anything address related is always 'larger', rest is equal
  
  if(aAddr && !bAddr)
    return false;
  else if(!aAddr && bAddr)
    return true;
  else if(!aAddr && !bAddr)
    return false;
  

  int aOrder=std::numeric_limits<int>::max();
  int bOrder=aOrder;

  ComboAddress a=getAddr(ar), b=getAddr(br);
  
  if(d_slo.d_orders.match(a))
    aOrder = d_slo.d_orders.lookup(a)->second;
  else {
    //    cout<<"Could not find anything for "<<a.toString()<<" in our orders!"<<endl;
  }
  if(d_slo.d_orders.match(b))
    bOrder = d_slo.d_orders.lookup(b)->second;
  else {
    //    cout<<"Could not find anything for "<<b.toString()<<" in our orders!"<<endl;
  }
  return aOrder < bOrder;
}