-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move shuffle functions to new file and put some symbols in the
pdns namespace.
- Loading branch information
Showing
10 changed files
with
168 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../shuffle.cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../shuffle.hh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* This file is part of PowerDNS or dnsdist. | ||
* Copyright -- PowerDNS.COM B.V. and its contributors | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of version 2 of the GNU General Public License as | ||
* published by the Free Software Foundation. | ||
* | ||
* In addition, for the avoidance of any doubt, permission is granted to | ||
* link this program with OpenSSL and to (re)distribute the binaries | ||
* produced as the result of such linking. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
#ifdef HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
|
||
#include <string> | ||
|
||
#include "shuffle.hh" | ||
#include "dns_random.hh" | ||
#include "dnsparser.hh" | ||
|
||
// shuffle, maintaining some semblance of order | ||
void pdns::shuffle(std::vector<DNSZoneRecord>& rrs) | ||
{ | ||
std::vector<DNSZoneRecord>::iterator first, second; | ||
for(first=rrs.begin();first!=rrs.end();++first) | ||
if(first->dr.d_place==DNSResourceRecord::ANSWER && first->dr.d_type != QType::CNAME) // CNAME must come first | ||
break; | ||
for(second=first;second!=rrs.end();++second) | ||
if(second->dr.d_place!=DNSResourceRecord::ANSWER) | ||
break; | ||
|
||
dns_random_engine r; | ||
if(second-first > 1) | ||
shuffle(first, second, r); | ||
|
||
// now shuffle the additional records | ||
for(first=second;first!=rrs.end();++first) | ||
if(first->dr.d_place==DNSResourceRecord::ADDITIONAL && first->dr.d_type != QType::CNAME) // CNAME must come first | ||
break; | ||
for(second=first;second!=rrs.end();++second) | ||
if(second->dr.d_place!=DNSResourceRecord::ADDITIONAL) | ||
break; | ||
|
||
if(second-first>1) | ||
shuffle(first, second, r); | ||
|
||
// we don't shuffle the rest | ||
} | ||
|
||
|
||
// shuffle, maintaining some semblance of order | ||
void pdns::shuffle(std::vector<DNSRecord>& rrs) | ||
{ | ||
std::vector<DNSRecord>::iterator first, second; | ||
for(first=rrs.begin();first!=rrs.end();++first) | ||
if(first->d_place==DNSResourceRecord::ANSWER && first->d_type != QType::CNAME) // CNAME must come first | ||
break; | ||
for(second=first;second!=rrs.end();++second) | ||
if(second->d_place!=DNSResourceRecord::ANSWER || second->d_type == QType::RRSIG) // leave RRSIGs at the end | ||
break; | ||
|
||
dns_random_engine r; | ||
if(second-first>1) | ||
shuffle(first, second, r); | ||
|
||
// now shuffle the additional records | ||
for(first=second;first!=rrs.end();++first) | ||
if(first->d_place==DNSResourceRecord::ADDITIONAL && first->d_type != QType::CNAME) // CNAME must come first | ||
break; | ||
for(second=first; second!=rrs.end(); ++second) | ||
if(second->d_place!=DNSResourceRecord::ADDITIONAL) | ||
break; | ||
|
||
if(second-first>1) | ||
shuffle(first, second, r); | ||
|
||
// we don't shuffle the rest | ||
} | ||
|
||
static uint16_t mapTypesToOrder(uint16_t type) | ||
{ | ||
if (type == QType::CNAME) | ||
return 0; | ||
if (type == QType::RRSIG) | ||
return 65535; | ||
else | ||
return 1; | ||
} | ||
|
||
// make sure rrs is sorted in d_place order to avoid surprises later | ||
// then shuffle the parts that desire shuffling | ||
void pdns::orderAndShuffle(vector<DNSRecord>& rrs) | ||
{ | ||
std::stable_sort(rrs.begin(), rrs.end(), [](const DNSRecord&a, const DNSRecord& b) { | ||
return std::make_tuple(a.d_place, mapTypesToOrder(a.d_type)) < std::make_tuple(b.d_place, mapTypesToOrder(b.d_type)); | ||
}); | ||
shuffle(rrs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* This file is part of PowerDNS or dnsdist. | ||
* Copyright -- PowerDNS.COM B.V. and its contributors | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of version 2 of the GNU General Public License as | ||
* published by the Free Software Foundation. | ||
* | ||
* In addition, for the avoidance of any doubt, permission is granted to | ||
* link this program with OpenSSL and to (re)distribute the binaries | ||
* produced as the result of such linking. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
#pragma once | ||
#include <vector> | ||
|
||
struct DNSRecord; | ||
struct DNSZoneRecord; | ||
|
||
namespace pdns { | ||
void shuffle(std::vector<DNSRecord>& rrs); | ||
void shuffle(std::vector<DNSZoneRecord>& rrs); | ||
void orderAndShuffle(std::vector<DNSRecord>& rrs); | ||
} | ||
|