main.cc 7.61 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
// 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.

#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>

Michal Vaner's avatar
Michal Vaner committed
23
#include <string>
24
25
26
27
28
29
30
31
32
#include <iostream>

#include <boost/foreach.hpp>

#include <asiolink/asiolink.h>

#include <exceptions/exceptions.h>

#include <dns/buffer.h>
33
#include <dns/rcode.h>
34
35
36
37
38
39
40
41
42
#include <dns/message.h>
#include <dns/messagerenderer.h>

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

#include <xfr/xfrout_client.h>

43
44
45
#include <auth/change_user.h>
#include <auth/common.h>

46
47
#include <resolver/spec_config.h>
#include <resolver/resolver.h>
48

49
50
51
#include <cache/resolver_cache.h>
#include <nsas/nameserver_address_store.h>

Michal Vaner's avatar
Michal Vaner committed
52
53
#include <log/dummylog.h>

54
55
56
using namespace std;
using namespace isc::cc;
using namespace isc::config;
Michal Vaner's avatar
Michal Vaner committed
57
using namespace isc::data;
Michal Vaner's avatar
Michal Vaner committed
58
using isc::log::dlog;
59
60
61
62
using namespace asiolink;

namespace {

63
static const string PROGRAM = "Resolver";
64

65
IOService io_service;
66
static boost::shared_ptr<Resolver> resolver;
67
68
69

ConstElementPtr
my_config_handler(ConstElementPtr new_config) {
70
    return (resolver->updateConfig(new_config));
71
72
73
74
75
76
77
78
79
80
81
}

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

    if (command == "print_message") {
        cout << args << endl;
        /* let's add that message to our answer as well */
        answer = createAnswer(0, args);
    } else if (command == "shutdown") {
82
        io_service.stop();
83
    }
Michal Vaner's avatar
Michal Vaner committed
84

85
86
87
88
89
    return (answer);
}

void
usage() {
90
    cerr << "Usage:  b10-resolver [-u user] [-v]" << endl;
91
92
    cerr << "\t-u: change process UID to the specified user" << endl;
    cerr << "\t-v: verbose output" << endl;
93
94
95
96
97
98
    exit(1);
}
} // end of anonymous namespace

int
main(int argc, char* argv[]) {
99
    isc::log::dprefix = "b10-resolver";
100
101
102
    int ch;
    const char* uid = NULL;

103
    while ((ch = getopt(argc, argv, "u:v")) != -1) {
104
105
106
107
108
        switch (ch) {
        case 'u':
            uid = optarg;
            break;
        case 'v':
Michal Vaner's avatar
Michal Vaner committed
109
            isc::log::denabled = true;
110
111
112
113
114
115
116
117
118
119
120
            break;
        case '?':
        default:
            usage();
        }
    }

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

Michal Vaner's avatar
Michal Vaner committed
121
122
123
124
125
126
127
128
    if (isc::log::denabled) { // Show the command line
        string cmdline("Command line:");
        for (int i = 0; i < argc; ++ i) {
            cmdline = cmdline + " " + argv[i];
        }
        dlog(cmdline);
    }

129
130
131
132
133
134
135
136
    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")) +
137
                "/src/bin/resolver/resolver.spec";
138
        } else {
139
            specfile = string(RESOLVER_SPECFILE_LOCATION);
140
141
        }

142
        resolver = boost::shared_ptr<Resolver>(new Resolver());
Michal Vaner's avatar
Michal Vaner committed
143
        dlog("Server created.");
144

145
146
147
        SimpleCallback* checkin = resolver->getCheckinProvider();
        DNSLookup* lookup = resolver->getDNSLookupProvider();
        DNSAnswer* answer = resolver->getDNSAnswerProvider();
148

149
150
        isc::nsas::NameserverAddressStore nsas(resolver);
        resolver->setNameserverAddressStore(nsas);
Michal Vaner's avatar
Michal Vaner committed
151

152
153
        isc::cache::ResolverCache cache;
        resolver->setCache(cache);
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        
        // 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));
184
        priming_result->setRcode(isc::dns::Rcode::NOERROR());
185
186
187
188
189
190
191
192
193
        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
194
        DNSService dns_service(io_service, checkin, lookup, answer);
195
        resolver->setDNSService(dns_service);
Michal Vaner's avatar
Michal Vaner committed
196
        dlog("IOService created.");
197

198
        cc_session = new Session(io_service.get_io_service());
Michal Vaner's avatar
Michal Vaner committed
199
        dlog("Configuration session channel created.");
200
201
202
203

        config_session = new ModuleCCSession(specfile, *cc_session,
                                             my_config_handler,
                                             my_command_handler);
Michal Vaner's avatar
Michal Vaner committed
204
        dlog("Configuration channel established.");
205

206
        // FIXME: This does not belong here, but inside Boss
207
208
209
210
        if (uid != NULL) {
            changeUser(uid);
        }

211
        resolver->setConfigSession(config_session);
212
        dlog("Config loaded");
213

Michal Vaner's avatar
Michal Vaner committed
214
        dlog("Server started.");
215
        io_service.run();
216
    } catch (const std::exception& ex) {
Scott Mann's avatar
Scott Mann committed
217
        dlog(string("Server failed: ") + ex.what(),true);
218
219
220
221
222
223
224
225
        ret = 1;
    }

    delete config_session;
    delete cc_session;

    return (ret);
}