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

17
#include "config.h"
18

19 20 21 22
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
Jeremy C. Reed's avatar
Jeremy C. Reed committed
23
#include <netinet/in.h>
24
#include <stdlib.h>
25
#include <errno.h>
26

27
#include <cassert>
28 29 30
#include <iostream>

#include <boost/foreach.hpp>
31 32

#include <exceptions/exceptions.h>
33

34 35
#include <dns/buffer.h>
#include <dns/message.h>
36
#include <dns/messagerenderer.h>
37

38 39 40
#include <cc/session.h>
#include <cc/data.h>
#include <config/ccsession.h>
41

42
#include "spec_config.h"
43
#include "common.h"
44
#include "auth_srv.h"
45
#include "asio_link.h"
46 47

using namespace std;
48 49 50
using namespace isc::data;
using namespace isc::cc;
using namespace isc::config;
51
using namespace isc::dns;
52

53 54
namespace {

JINMEI Tatuya's avatar
JINMEI Tatuya committed
55 56
bool verbose_mode = false;

57
const string PROGRAM = "Auth";
58
const char* DNSPORT = "5300";
59 60 61 62

/* need global var for config/command handlers.
 * todo: turn this around, and put handlers in the authserver
 * class itself? */
63
AuthSrv *auth_server;
64

65
asio_link::IOService* io_service;
66

JINMEI Tatuya's avatar
JINMEI Tatuya committed
67
ElementPtr
68
my_config_handler(ElementPtr new_config) {
69 70 71
    return auth_server->updateConfig(new_config);
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
72
ElementPtr
73
my_command_handler(const string& command, const ElementPtr args) {
74
    ElementPtr answer = createAnswer();
75

76
    if (command == "print_message") {
77 78 79
        cout << args << endl;
        /* let's add that message to our answer as well */
        answer->get("result")->add(args);
80
    } else if (command == "shutdown") {
81
        io_service->stop();
82
    }
83
    
84 85 86
    return answer;
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
87
void
88
usage() {
Evan Hunt's avatar
Evan Hunt committed
89
    cerr << "Usage: b10-auth [-p port] [-4|-6] [-nv]" << endl;
90
    exit(1);
91
}
JINMEI Tatuya's avatar
JINMEI Tatuya committed
92
} // end of anonymous namespace
93 94 95 96

int
main(int argc, char* argv[]) {
    int ch;
97
    const char* port = DNSPORT;
Evan Hunt's avatar
Evan Hunt committed
98
    const char* address = NULL;
Evan Hunt's avatar
Evan Hunt committed
99
    bool use_ipv4 = true, use_ipv6 = true, cache = true;
100

Evan Hunt's avatar
Evan Hunt committed
101
    while ((ch = getopt(argc, argv, "46np:v")) != -1) {
102
        switch (ch) {
103
        case '4':
104 105 106 107 108
            // Note that -4 means "ipv4 only", we need to set "use_ipv6" here,
            // not "use_ipv4".  We could use something like "ipv4_only", but
            // we found the negatively named variable could confuse the code
            // logic.
            use_ipv6 = false;
109 110
            break;
        case '6':
111 112
            // The same note as -4 applies.
            use_ipv4 = false;
113
            break;
Evan Hunt's avatar
Evan Hunt committed
114 115 116
        case 'n':
            cache = false;
            break;
Evan Hunt's avatar
Evan Hunt committed
117 118 119
        case 'a':
            address = optarg;
            break;
120
        case 'p':
121
            port = optarg;
122
            break;
123 124 125
        case 'v':
            verbose_mode = true;
            break;
126 127 128 129 130 131
        case '?':
        default:
            usage();
        }
    }

132
    if (argc - optind > 0) {
133
        usage();
134 135
    }

136
    if (!use_ipv4 && !use_ipv6) {
137
        cerr << "[b10-auth] Error: -4 and -6 can't coexist" << endl;
138 139 140
        usage();
    }

141
    // initialize command channel
142
    int ret = 0;
143
    try {
144
        string specfile;
145 146
        if (getenv("B10_FROM_BUILD")) {
            specfile = string(getenv("B10_FROM_BUILD")) +
147
                "/src/bin/auth/auth.spec";
148
        } else {
149
            specfile = string(AUTH_SPECFILE_LOCATION);
150
        }
151

Evan Hunt's avatar
Evan Hunt committed
152
        auth_server = new AuthSrv(cache);
153 154
        auth_server->setVerbose(verbose_mode);

Evan Hunt's avatar
Evan Hunt committed
155 156
        io_service = new asio_link::IOService(auth_server, address, port,
                                              use_ipv4, use_ipv6);
157

158 159
        ModuleCCSession cs(specfile, io_service->get_io_service(),
                           my_config_handler, my_command_handler);
160 161 162 163

        auth_server->setConfigSession(&cs);
        auth_server->updateConfig(ElementPtr());

164
        cout << "[b10-auth] Server started." << endl;
165
        io_service->run();
166
    } catch (const std::exception& ex) {
167
        cerr << "[b10-auth] " << ex.what() << endl;
168
        ret = 1;
169
    }
170

171
    delete io_service;
172 173
    delete auth_server;
    return (ret);
174
}