2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "pdns/utility.hh"
26 #include "pdns/dnsbackend.hh"
27 #include "pdns/dns.hh"
28 #include "pdns/dnsbackend.hh"
29 #include "pdns/dnspacket.hh"
30 #include "pdns/pdnsexception.hh"
31 #include "pdns/logger.hh"
32 #include "pdns/version.hh"
33 #include <boost/algorithm/string.hpp>
36 class RandomBackend
: public DNSBackend
39 RandomBackend(const string
&suffix
="")
41 setArgPrefix("random"+suffix
);
42 d_ourname
=DNSName(getArg("hostname"));
43 d_ourdomain
= d_ourname
;
44 d_ourdomain
.chopOff();
47 bool list(const DNSName
&target
, int id
, bool include_disabled
) {
48 return false; // we don't support AXFR
51 void lookup(const QType
&type
, const DNSName
&qdomain
, DNSPacket
*p
, int zoneId
)
53 if(qdomain
== d_ourdomain
){
54 if(type
.getCode() == QType::SOA
|| type
.getCode() == QType::ANY
) {
55 d_answer
="ns1." + d_ourdomain
.toString() + " hostmaster." + d_ourdomain
.toString() + " 1234567890 86400 7200 604800 300";
59 } else if (qdomain
== d_ourname
) {
60 if(type
.getCode() == QType::A
|| type
.getCode() == QType::ANY
) {
62 os
<<Utility::random()%256<<"."<<Utility::random()%256<<"."<<Utility::random()%256<<"."<<Utility::random()%256;
63 d_answer
=os
.str(); // our random ip address
72 bool get(DNSResourceRecord
&rr
)
74 if(!d_answer
.empty()) {
75 if(d_answer
.find("ns1.") == 0){
82 rr
.ttl
=5; // 5 seconds
83 rr
.auth
= 1; // it may be random.. but it is auth!
86 d_answer
=""; // this was the last answer
100 class RandomFactory
: public BackendFactory
103 RandomFactory() : BackendFactory("random") {}
104 void declareArguments(const string
&suffix
="")
106 declare(suffix
,"hostname","Hostname which is to be random","random.example.com");
108 DNSBackend
*make(const string
&suffix
="")
110 return new RandomBackend(suffix
);
121 BackendMakers().report(new RandomFactory
);
122 L
<< Logger::Info
<< "[randombackend] This is the random backend version " VERSION
124 << " (" __DATE__
" " __TIME__
")"
126 << " reporting" << endl
;
130 static RandomLoader randomLoader
;