parkinglot.cc 9.02 KB
Newer Older
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
// Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// $Id$

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <stdlib.h>

#include <set>
#include <iostream>

#include <dns/buffer.h>
#include <dns/name.h>
#include <dns/rrset.h>
#include <dns/message.h>

31 32
#include <cc/cpp/data.h>

33 34 35
#include "common.h"
#include "parkinglot.h"

36 37
#include <boost/foreach.hpp>

38 39 40 41 42
using namespace std;

using namespace isc::dns;
using namespace isc::dns::Rdata::IN;
using namespace isc::dns::Rdata::Generic;
Jelte Jansen's avatar
Jelte Jansen committed
43
using namespace isc::data;
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
void
ParkingLot::addARecord(std::string data) {
    a_records.push_back(Rdata::RdataPtr(new A(data)));
}

void
ParkingLot::addAAAARecord(std::string data) {
    aaaa_records.push_back(Rdata::RdataPtr(new AAAA(data)));
}

void
ParkingLot::addNSRecord(std::string data) {
    ns_records.push_back(Rdata::RdataPtr(new NS(data)));
}

void
ParkingLot::setSOARecord(isc::dns::Rdata::RdataPtr soa_record) {
}

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

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

77
ParkingLot::ParkingLot(int port) {
78 79
    setDefaultZoneData();
    /*ns1 = Rdata::RdataPtr(new NS("ns1.parking.example"));
80 81
    ns2 = Rdata::RdataPtr(new NS("ns2.parking.example"));
    ns3 = Rdata::RdataPtr(new NS("ns3.parking.example"));
82 83
    a = Rdata::RdataPtr(new A("127.0.0.1"));
    aaaa = Rdata::RdataPtr(new AAAA("::1"));
84
    */
85
    soa = Rdata::RdataPtr(new SOA("parking.example", "noc.parking.example",
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                                        1, 1800, 900, 604800, TTL(86400)));

    int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (s < 0)
        throw FatalError("failed to open socket");

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(port);

    socklen_t sa_len = sizeof(sin);
#ifdef HAVE_SIN_LEN
    sin.sin_len = sa_len;
#endif

    if (bind(s, (struct sockaddr *)&sin, sa_len) < 0)
        throw FatalError("could not bind socket");

    sock = s;
}

void
ParkingLot::processMessage() {
    Name authors_name("authors.bind");
111
    Name version_name("version.bind");
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    struct sockaddr_storage ss;
    socklen_t sa_len = sizeof(ss);
    struct sockaddr* sa = static_cast<struct sockaddr*>((void*)&ss);
    int s = sock;
    Message msg;

    if (msg.getBuffer().recvFrom(s, sa, &sa_len) > 0) {
        try {
            msg.fromWire();
        } catch (...) {
            cerr << "parse failed" << endl;
            return;
        }

        cout << "received a message:\n" << msg.toText() << endl;

        if (msg.getSection(SECTION_QUESTION).size() != 1)
            return;

        msg.makeResponse();
        msg.setAA(true);

        RRsetPtr query = msg.getSection(SECTION_QUESTION)[0];

        string name = query->getName().toText(true);
137 138 139
        if (query->getClass() == RRClass::CH &&
            query->getType() == RRType::TXT &&
            query->getName() == authors_name) {
140
            msg.setRcode(Message::RCODE_NOERROR);
JINMEI Tatuya's avatar
JINMEI Tatuya committed
141 142 143 144 145 146 147 148 149
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH, 
                                         RRType::TXT, TTL(0), 
                                         TXT("Han Feng")));
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH, 
                                         RRType::TXT, TTL(0), 
                                         TXT("Kazunori Fujiwara")));
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH, 
                                         RRType::TXT, TTL(0), 
                                         TXT("Michael Graff")));
150 151
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH,
                                         RRType::TXT, TTL(0),
JINMEI Tatuya's avatar
JINMEI Tatuya committed
152
                                         TXT("Evan Hunt")));
153 154
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH,
                                         RRType::TXT, TTL(0),
JINMEI Tatuya's avatar
JINMEI Tatuya committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                                         TXT("Jelte Jansen")));
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH, 
                                         RRType::TXT, TTL(0), 
                                         TXT("Jin Jian")));
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH,
                                         RRType::TXT, TTL(0),
                                         TXT("JINMEI Tatuya")));
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH, 
                                         RRType::TXT, TTL(0), 
                                         TXT("Naoki Kambe")));
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH, 
                                         RRType::TXT, TTL(0), 
                                         TXT("Shane Kerr"))); 
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH, 
                                         RRType::TXT, TTL(0), 
                                         TXT("Zhang Likun")));
171 172 173
            msg.addRR(SECTION_ANSWER, RR(authors_name, RRClass::CH, 
                                         RRType::TXT, TTL(0), 
                                         TXT("Jeremy C. Reed"))); 
174 175 176
            msg.addRR(SECTION_AUTHORITY, RR(authors_name, RRClass::CH,
                                            RRType::NS, TTL(0),
                                            NS("authors.bind")));
177 178 179 180 181 182 183 184 185 186
        } else if (query->getClass() == RRClass::CH &&
                   query->getType() == RRType::TXT &&
                   query->getName() == version_name) {            
            msg.setRcode(Message::RCODE_NOERROR);
            msg.addRR(SECTION_ANSWER, RR(version_name, RRClass::CH,
                                         RRType::TXT, TTL(0),
                                         TXT("10.0.0s20091030")));
            msg.addRR(SECTION_AUTHORITY, RR(version_name, RRClass::CH,
                                            RRType::NS, TTL(0),
                                            NS("version.bind")));
187 188 189 190
        } else if (zones.contains(name)) {
            msg.setRcode(Message::RCODE_NOERROR);
            RRset* nsset = new RRset(query->getName(), RRClass::IN,
                                     RRType::NS, TTL(3600));
191 192 193
            BOOST_FOREACH(isc::dns::Rdata::RdataPtr ns, ns_records) {
                nsset->addRdata(ns);
            }
194 195 196 197 198 199

            if (query->getType() == RRType::NS)
                msg.addRRset(SECTION_ANSWER, RRsetPtr(nsset));
            else if (query->getType() == RRType::A) {
                msg.addRRset(SECTION_AUTHORITY, RRsetPtr(nsset));

200 201 202 203
                BOOST_FOREACH(isc::dns::Rdata::RdataPtr a, a_records) {
                    RR arr(query->getName(), RRClass::IN, RRType::A, TTL(3600), a);
                    msg.addRR(SECTION_ANSWER, arr);
                }
204 205
            } else if (query->getType() == RRType::AAAA) {
                msg.addRRset(SECTION_AUTHORITY, RRsetPtr(nsset));
206 207 208 209 210
                BOOST_FOREACH(isc::dns::Rdata::RdataPtr aaaa, aaaa_records) {
                    RR aaaarr(query->getName(), RRClass::IN, RRType::AAAA,
                              TTL(3600), aaaa);
                    msg.addRR(SECTION_ANSWER, aaaarr);
                }
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
            } else {
                RR soarr(query->getName(), RRClass::IN, RRType::SOA,
                         TTL(3600), soa);
                msg.addRR(SECTION_AUTHORITY, soarr);
            }
        } else {
            msg.setRcode(Message::RCODE_NXDOMAIN);
        }

        msg.toWire();
        cout << "sending a response (" <<
            boost::lexical_cast<string>(msg.getBuffer().getSize())
                  << " bytes):\n" << msg.toText() << endl;
        msg.getBuffer().sendTo(s, *sa, sa_len);
    }
}
227 228

void
229
ParkingLot::command(pair<string,ElementPtr> cmd) {
Jelte Jansen's avatar
Jelte Jansen committed
230
    if (cmd.first == "shutdown")
231
        exit(0);
232 233 234 235 236 237
    else if (cmd.first == "config_update") {
        // what to do with port settings?
        ElementPtr zonelist_el = (cmd.second)->get("zones");
        // We could walk through both lists and remove and serve
        // accordingly, or simply clear all and add everything
        zones.clear_zones();
238 239
        BOOST_FOREACH(ElementPtr zone, zonelist_el->listValue()) {
            zones.serve(zone->stringValue());
240 241
        }
    }
242
}
243 244 245 246 247

void
ParkingLot::serve(std::string zone_name) {
    zones.serve(zone_name);
}