]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/randombackend/randombackend.cc
Standardize license text in all PDNS files
[thirdparty/pdns.git] / modules / randombackend / randombackend.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
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>
34
35 /* FIRST PART */
36 class RandomBackend : public DNSBackend
37 {
38 public:
39 RandomBackend(const string &suffix="")
40 {
41 setArgPrefix("random"+suffix);
42 d_ourname=DNSName(getArg("hostname"));
43 d_ourdomain = d_ourname;
44 d_ourdomain.chopOff();
45 }
46
47 bool list(const DNSName &target, int id, bool include_disabled) {
48 return false; // we don't support AXFR
49 }
50
51 void lookup(const QType &type, const DNSName &qdomain, DNSPacket *p, int zoneId)
52 {
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";
56 } else {
57 d_answer="";
58 }
59 } else if (qdomain == d_ourname) {
60 if(type.getCode() == QType::A || type.getCode() == QType::ANY) {
61 ostringstream os;
62 os<<Utility::random()%256<<"."<<Utility::random()%256<<"."<<Utility::random()%256<<"."<<Utility::random()%256;
63 d_answer=os.str(); // our random ip address
64 } else {
65 d_answer="";
66 }
67 } else {
68 d_answer="";
69 }
70 }
71
72 bool get(DNSResourceRecord &rr)
73 {
74 if(!d_answer.empty()) {
75 if(d_answer.find("ns1.") == 0){
76 rr.qname=d_ourdomain;
77 rr.qtype=QType::SOA;
78 } else {
79 rr.qname=d_ourname;
80 rr.qtype=QType::A;
81 }
82 rr.ttl=5; // 5 seconds
83 rr.auth = 1; // it may be random.. but it is auth!
84 rr.content=d_answer;
85
86 d_answer=""; // this was the last answer
87 return true;
88 }
89 return false;
90 }
91
92 private:
93 string d_answer;
94 DNSName d_ourname;
95 DNSName d_ourdomain;
96 };
97
98 /* SECOND PART */
99
100 class RandomFactory : public BackendFactory
101 {
102 public:
103 RandomFactory() : BackendFactory("random") {}
104 void declareArguments(const string &suffix="")
105 {
106 declare(suffix,"hostname","Hostname which is to be random","random.example.com");
107 }
108 DNSBackend *make(const string &suffix="")
109 {
110 return new RandomBackend(suffix);
111 }
112 };
113
114 /* THIRD PART */
115
116 class RandomLoader
117 {
118 public:
119 RandomLoader()
120 {
121 BackendMakers().report(new RandomFactory);
122 L << Logger::Info << "[randombackend] This is the random backend version " VERSION
123 #ifndef REPRODUCIBLE
124 << " (" __DATE__ " " __TIME__ ")"
125 #endif
126 << " reporting" << endl;
127 }
128 };
129
130 static RandomLoader randomLoader;