File: sortlist.cc

package info (click to toggle)
pdns-recursor 4.0.4-1+deb9u3~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 5,484 kB
  • sloc: cpp: 36,380; sh: 11,771; makefile: 305; xml: 37
file content (81 lines) | stat: -rw-r--r-- 2,293 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
75
76
77
78
79
80
81
#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);
}

bool SortListOrderCmp::operator()(const ComboAddress& a, const ComboAddress& b) const
{
  int aOrder=std::numeric_limits<int>::max();
  int bOrder=aOrder;

  if(d_slo.d_orders.match(a))
    aOrder = d_slo.d_orders.lookup(a)->second;
  if(d_slo.d_orders.match(b))
    bOrder = d_slo.d_orders.lookup(b)->second;

  return aOrder < bOrder;
}

bool SortListOrderCmp::operator()(const DNSRecord& ar, const DNSRecord& br) const
{
  if(ar.d_type < br.d_type)
    return true;
  if(ar.d_type > br.d_type)
    return false;

  if(ar.d_type != QType::A && ar.d_type != QType::AAAA) 
    return false;  // all other types are equal among themselves

  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;
}