main.cc 9.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

15 16
#include <config.h>

17 18 19
#include <resolver/spec_config.h>
#include <resolver/resolver.h>
#include "resolver_log.h"
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
20
#include "common.h"
21

22
#include <asiodns/asiodns.h>
23 24 25 26
#include <asiolink/asiolink.h>

#include <exceptions/exceptions.h>

27
#include <util/buffer.h>
28
#include <dns/rcode.h>
29 30 31 32 33 34 35
#include <dns/message.h>
#include <dns/messagerenderer.h>

#include <cc/session.h>
#include <cc/data.h>
#include <config/ccsession.h>

Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
36 37
#include <server_common/socket_request.h>

38 39
#include <xfr/xfrout_client.h>

40 41
#include <auth/common.h>

42 43 44
#include <cache/resolver_cache.h>
#include <nsas/nameserver_address_store.h>

45
#include <log/logger_support.h>
46
#include <log/logger_level.h>
47
#include "resolver_log.h"
48 49 50 51 52 53 54 55 56 57 58 59 60 61

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

#include <string>
#include <iostream>

#include <boost/foreach.hpp>
Michal Vaner's avatar
Michal Vaner committed
62

63 64 65
using namespace std;
using namespace isc::cc;
using namespace isc::config;
Michal Vaner's avatar
Michal Vaner committed
66
using namespace isc::data;
67 68
using namespace isc::asiodns;
using namespace isc::asiolink;
69 70 71

namespace {

72
static const string PROGRAM = "Resolver";
73

74
IOService io_service;
75
static boost::shared_ptr<Resolver> resolver;
76 77 78

ConstElementPtr
my_config_handler(ConstElementPtr new_config) {
79
    return (resolver->updateConfig(new_config));
80 81 82 83 84 85
}

ConstElementPtr
my_command_handler(const string& command, ConstElementPtr args) {
    ConstElementPtr answer = createAnswer();

86 87 88 89 90 91 92
    try {
        if (command == "print_message") {
            LOG_INFO(resolver_logger, RESOLVER_PRINT_COMMAND).arg(args);
            /* let's add that message to our answer as well */
            answer = createAnswer(0, args);
        } else if (command == "shutdown") {
            // Is the pid argument provided?
93
            if (args && args->contains("pid")) {
94
                // If it is, we check it is the same as our PID
95 96 97
                const int pid(args->get("pid")->intValue());
                const pid_t my_pid(getpid());
                if (my_pid != pid) {
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
98 99
                    // It is not for us (this is expected, see auth/command.cc
                    // and the ShutdownCommand there).
100
                    return (answer);
101
                }
102
            }
103 104
            LOG_DEBUG(resolver_logger, RESOLVER_DBG_INIT,
                      RESOLVER_SHUTDOWN_RECEIVED);
105
            io_service.stop();
106
        }
Michal Vaner's avatar
Michal Vaner committed
107

108 109 110
        return (answer);
    } catch (const std::exception& e) {
        return (createAnswer(1, e.what()));
111 112 113 114 115
    }
}

void
usage() {
116
    cerr << "Usage:  b10-resolver [-u user] [-v]" << endl;
117
    cerr << "\t-v: verbose output" << endl;
118 119 120 121 122 123
    exit(1);
}
} // end of anonymous namespace

int
main(int argc, char* argv[]) {
124
    bool verbose = false;
125 126
    int ch;

127
    while ((ch = getopt(argc, argv, "u:v")) != -1) {
128 129
        switch (ch) {
        case 'v':
130
            verbose = true;
131 132 133 134 135 136 137 138 139 140 141
            break;
        case '?':
        default:
            usage();
        }
    }

    if (argc - optind > 0) {
        usage();
    }

142 143
    // Until proper logging comes along, initialize the logging with the
    // temporary initLogger() code.  If verbose, we'll use maximum verbosity.
144
    isc::log::initLogger(RESOLVER_NAME,
145
                         (verbose ? isc::log::DEBUG : isc::log::INFO),
146
                         isc::log::MAX_DEBUG_LEVEL, NULL);
147 148 149 150 151

    // Print the starting message
    string cmdline = argv[0];
    for (int i = 1; i < argc; ++ i) {
        cmdline = cmdline + " " + argv[i];
Michal Vaner's avatar
Michal Vaner committed
152
    }
153
    LOG_INFO(resolver_logger, RESOLVER_STARTING).arg(cmdline);
Michal Vaner's avatar
Michal Vaner committed
154

155 156 157 158 159 160 161 162
    int ret = 0;

    Session* cc_session = NULL;
    ModuleCCSession* config_session = NULL;
    try {
        string specfile;
        if (getenv("B10_FROM_BUILD")) {
            specfile = string(getenv("B10_FROM_BUILD")) +
163
                "/src/bin/resolver/resolver.spec";
164
        } else {
165
            specfile = string(RESOLVER_SPECFILE_LOCATION);
166 167
        }

168
        resolver = boost::shared_ptr<Resolver>(new Resolver());
169
        LOG_DEBUG(resolver_logger, RESOLVER_DBG_INIT, RESOLVER_CREATED);
170

171 172 173
        SimpleCallback* checkin = resolver->getCheckinProvider();
        DNSLookup* lookup = resolver->getDNSLookupProvider();
        DNSAnswer* answer = resolver->getDNSAnswerProvider();
174

175 176
        isc::nsas::NameserverAddressStore nsas(resolver);
        resolver->setNameserverAddressStore(nsas);
Michal Vaner's avatar
Michal Vaner committed
177

178 179
        isc::cache::ResolverCache cache;
        resolver->setCache(cache);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        
        // TODO priming query, remove root from direct
        // Fake a priming query result here (TODO2 how to flag non-expiry?)
        // propagation to runningquery. And check for forwarder mode?
        isc::dns::QuestionPtr root_question(new isc::dns::Question(
                                            isc::dns::Name("."),
                                            isc::dns::RRClass::IN(),
                                            isc::dns::RRType::NS()));
        isc::dns::RRsetPtr root_ns_rrset(new isc::dns::RRset(isc::dns::Name("."), 
                                         isc::dns::RRClass::IN(),
                                         isc::dns::RRType::NS(),
                                         isc::dns::RRTTL(8888)));
        root_ns_rrset->addRdata(isc::dns::rdata::createRdata(isc::dns::RRType::NS(),
                                                             isc::dns::RRClass::IN(),
                                                             "l.root-servers.net."));
        isc::dns::RRsetPtr root_a_rrset(new isc::dns::RRset(isc::dns::Name("l.root-servers.net"), 
                                        isc::dns::RRClass::IN(),
                                        isc::dns::RRType::A(),
                                        isc::dns::RRTTL(8888)));
        root_a_rrset->addRdata(isc::dns::rdata::createRdata(isc::dns::RRType::A(),
                                                             isc::dns::RRClass::IN(),
                                                             "199.7.83.42"));
        isc::dns::RRsetPtr root_aaaa_rrset(new isc::dns::RRset(isc::dns::Name("l.root-servers.net"), 
                                        isc::dns::RRClass::IN(),
                                        isc::dns::RRType::AAAA(),
                                        isc::dns::RRTTL(8888)));
        root_aaaa_rrset->addRdata(isc::dns::rdata::createRdata(isc::dns::RRType::AAAA(),
                                                             isc::dns::RRClass::IN(),
                                                             "2001:500:3::42"));
        isc::dns::MessagePtr priming_result(new isc::dns::Message(isc::dns::Message::RENDER));
210
        priming_result->setRcode(isc::dns::Rcode::NOERROR());
211 212 213 214 215 216 217 218 219
        priming_result->addQuestion(root_question);
        priming_result->addRRset(isc::dns::Message::SECTION_ANSWER, root_ns_rrset);
        priming_result->addRRset(isc::dns::Message::SECTION_ADDITIONAL, root_a_rrset);
        priming_result->addRRset(isc::dns::Message::SECTION_ADDITIONAL, root_aaaa_rrset);
        cache.update(*priming_result);
        cache.update(root_ns_rrset);
        cache.update(root_a_rrset);
        cache.update(root_aaaa_rrset);
        
Michal Vaner's avatar
Michal Vaner committed
220
        DNSService dns_service(io_service, checkin, lookup, answer);
221
        resolver->setDNSService(dns_service);
222
        LOG_DEBUG(resolver_logger, RESOLVER_DBG_INIT, RESOLVER_SERVICE_CREATED);
223

224
        cc_session = new Session(io_service.get_io_service());
225
        isc::server_common::initSocketRequestor(*cc_session, RESOLVER_NAME);
226 227

        // We delay starting listening to new commands/config just before we
228
        // go into the main loop.   See auth/main.cc for the rationale.
229 230
        config_session = new ModuleCCSession(specfile, *cc_session,
                                             my_config_handler,
231
                                             my_command_handler, false);
232
        LOG_DEBUG(resolver_logger, RESOLVER_DBG_INIT, RESOLVER_CONFIG_CHANNEL);
233

234
        resolver->setConfigSession(config_session);
235 236 237
        // Install all initial configurations.  If loading configuration
        // fails, it will be logged, but we start the server anyway, giving
        // the user a second chance to correct the configuration.
238 239 240 241
        // By setting the 'startup' parameter to true, we ensure most of
        // the default configuration will be installed even if listen_on
        // fails.
        resolver->updateConfig(config_session->getFullConfig(), true);
242
        LOG_DEBUG(resolver_logger, RESOLVER_DBG_INIT, RESOLVER_CONFIG_LOADED);
243

244 245 246
        // Now start asynchronous read.
        config_session->start();

247
        LOG_INFO(resolver_logger, RESOLVER_STARTED);
248
        io_service.run();
249
    } catch (const std::exception& ex) {
250
        LOG_FATAL(resolver_logger, RESOLVER_FAILED).arg(ex.what());
251 252 253 254 255 256
        ret = 1;
    }

    delete config_session;
    delete cc_session;

257
    LOG_INFO(resolver_logger, RESOLVER_SHUTDOWN);
258 259
    return (ret);
}