data_source_plot.cc 6.11 KB
Newer Older
1 2 3

#include <boost/foreach.hpp>

4 5
#include <dns/rdataclass.h>
#include <dns/rrttl.h>
6

7 8 9 10 11 12 13 14 15
#include "data_source_plot.h"

namespace isc {
namespace dns {

// this implementation returns fixed records,
// and does not allow update statements

using namespace isc::dns;
16
using namespace isc::dns::rdata;
17 18
using namespace isc::data;

19 20 21 22 23
namespace {
const Name authors_name("authors.bind");
const Name version_name("version.bind");
}

24 25 26 27 28 29 30
void
DataSourceParkingLot::serve(std::string zone_name) {
    zones.serve(zone_name);
}

void
DataSourceParkingLot::addARecord(std::string data) {
31
    a_records.push_back(RdataPtr(new in::A(data)));
32 33 34 35
}

void
DataSourceParkingLot::addAAAARecord(std::string data) {
36
    aaaa_records.push_back(RdataPtr(new in::AAAA(data)));
37 38 39 40
}

void
DataSourceParkingLot::addNSRecord(std::string data) {
41
    ns_records.push_back(RdataPtr(new generic::NS(data)));
42 43 44
}

void
45
DataSourceParkingLot::setSOARecord(RdataPtr soa_record) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
}

void
DataSourceParkingLot::setDefaultZoneData() {
    clearARecords();
    clearAAAARecords();
    clearNSRecords();

    addARecord("127.0.0.1");
    addAAAARecord("::1");
    addNSRecord("ns1.parking.example");
    addNSRecord("ns2.parking.example");
    addNSRecord("ns3.parking.example");
}

DataSourceParkingLot::DataSourceParkingLot() {
    setDefaultZoneData();
63 64 65
    soa = RdataPtr(new generic::SOA(Name("parking.example"),
                                    Name("noc.parking.example"),
                                    1, 1800, 900, 604800, 86400));
66 67
}

68 69 70
bool
DataSourceParkingLot::hasZoneFor(const Name& name, Name &zone_name)
{
71 72 73 74 75 76 77
    if (name == authors_name) {
        zone_name = authors_name;
        return (true);
    } else if (name == version_name) {
        zone_name = version_name;
        return (true);
    }
78
    return zones.findClosest(name, zone_name);
79 80 81
}

SearchResult
82 83 84
DataSourceParkingLot::findRRsets(const isc::dns::Name& zone_name,
                                 const isc::dns::Name& name,
                                 const isc::dns::RRClass& clas,
85 86
                                 const isc::dns::RRType& type) const
{
87 88
    SearchResult result;
    
89 90
    if (clas == RRClass::CH()) {
        if (type == RRType::TXT()) {
91
            if (name == authors_name) {
92
                RRsetPtr rrset = RRsetPtr(new RRset(authors_name, RRClass::CH(),
93
                                                    RRType::TXT(), RRTTL(0)));
94 95 96 97 98 99 100 101 102 103 104
                rrset->addRdata(generic::TXT("Han Feng"));
                rrset->addRdata(generic::TXT("Kazunori Fujiwara"));
                rrset->addRdata(generic::TXT("Michael Graff"));
                rrset->addRdata(generic::TXT("Evan Hunt"));
                rrset->addRdata(generic::TXT("Jelte Jansen"));
                rrset->addRdata(generic::TXT("Jin Jian"));
                rrset->addRdata(generic::TXT("JINMEI Tatuya"));
                rrset->addRdata(generic::TXT("Naoki Kambe"));
                rrset->addRdata(generic::TXT("Shane Kerr")); 
                rrset->addRdata(generic::TXT("Zhang Likun"));
                rrset->addRdata(generic::TXT("Jeremy C. Reed")); 
105 106 107

                result.addRRset(rrset);
                result.setStatus(SearchResult::success);
108
            } else if (name == version_name) {
109
                RRsetPtr rrset = RRsetPtr(new RRset(version_name, RRClass::CH(),
110
                                                    RRType::TXT(), RRTTL(0)));
111
                rrset->addRdata(generic::TXT("BIND10 0.0.1"));
112 113
                result.addRRset(rrset);
                result.setStatus(SearchResult::success);
114
            } else {
115
                result.setStatus(SearchResult::name_not_found);
116
            }
117 118 119 120 121 122 123 124 125 126 127
        } else if (type == RRType::NS()) {
            if (name == authors_name || name == version_name) {
                RRsetPtr rrset = RRsetPtr(new RRset(name, RRClass::CH(),
                                                    RRType::NS(),
                                                    RRTTL(0)));
                rrset->addRdata(generic::NS(name));
                result.addRRset(rrset);
                result.setStatus(SearchResult::success);
            } else {
                result.setStatus(SearchResult::name_not_found);
            }
128
        } else {
129
            result.setStatus(SearchResult::name_not_found);
130
        }
131
    } else if (clas == RRClass::IN()) {
132
        if (zones.contains(name)) {
133
            RRsetPtr rrset = RRsetPtr(new RRset(name, clas, type, RRTTL(3600)));
134
            result.setStatus(SearchResult::success);
135 136
            if (type == RRType::A()) {
                BOOST_FOREACH(RdataPtr a, a_records) {
137
                    rrset->addRdata(a);
138
                }
139 140
            } else if (type == RRType::AAAA()) {
                BOOST_FOREACH(RdataPtr aaaa, aaaa_records) {
141
                    rrset->addRdata(aaaa);
142
                }
143 144
            } else if (type == RRType::NS()) {
                BOOST_FOREACH(RdataPtr ns, ns_records) {
145
                    rrset->addRdata(ns);
146
                }
147
            } else if (type == RRType::SOA()) {
148
                rrset->addRdata(soa);
149
            }
150
            result.addRRset(rrset);
151
        } else {
152 153 154 155 156 157
            // we don't have the name itself. Do we have the zone?
            if (zones.contains(zone_name)) {
                result.setStatus(SearchResult::name_not_found);
            } else {
                result.setStatus(SearchResult::zone_not_found);
            }
158 159
        }
    } else {
160
        result.setStatus(SearchResult::zone_not_found);
161
    }
162
    return result;
163 164
}

165 166 167 168 169 170 171 172
/// Do direct 'search' in database, no extra processing,
/// and add the resulting rrsets to the specified section
/// in the given message
/// returns the status code of the searchresult
/// Once the dns logic is moved from parkinglot to this class,
/// we should probably make this private
SearchResult::status_type
DataSourceParkingLot::addToMessage(Message& msg,
173
             const Section& section,
174 175
             const Name& zone_name,
             const Name& name,
176
             const RRClass& clas,
177
             const RRType& type) const
178 179 180 181 182 183 184 185 186
{
    SearchResult result = findRRsets(zone_name, name, clas, type);
    BOOST_FOREACH(RRsetPtr rrset, result) {
        msg.addRRset(section, rrset);
    }
    return result.getStatus();
}


187 188
}
}