main.cc 4.45 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 43 44 45 46 47
#include <xfr/xfrout_client.h>

#include <auth/spec_config.h>
#include <auth/common.h>
#include <auth/auth_srv.h>
#include <auth/asio_link.h>
48 49

using namespace std;
50 51 52
using namespace isc::data;
using namespace isc::cc;
using namespace isc::config;
53
using namespace isc::dns;
54
using namespace isc::xfr;
55

56 57
namespace {

JINMEI Tatuya's avatar
JINMEI Tatuya committed
58 59
bool verbose_mode = false;

60
const string PROGRAM = "Auth";
61
const char* DNSPORT = "5300";
62 63 64 65

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

68
asio_link::IOService* io_service;
69

JINMEI Tatuya's avatar
JINMEI Tatuya committed
70
ElementPtr
71
my_config_handler(ElementPtr new_config) {
72 73 74
    return auth_server->updateConfig(new_config);
}

JINMEI Tatuya's avatar
JINMEI Tatuya committed
75
ElementPtr
76
my_command_handler(const string& command, const ElementPtr args) {
77
    ElementPtr answer = createAnswer();
78

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

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

int
main(int argc, char* argv[]) {
    int ch;
100
    const char* port = DNSPORT;
101
    bool use_ipv4 = true, use_ipv6 = true;
102

103
    while ((ch = getopt(argc, argv, "46p:v")) != -1) {
104
        switch (ch) {
105
        case '4':
106 107 108 109 110
            // 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;
111 112
            break;
        case '6':
113 114
            // The same note as -4 applies.
            use_ipv4 = false;
115
            break;
116
        case 'p':
117
            port = optarg;
118
            break;
119 120 121
        case 'v':
            verbose_mode = true;
            break;
122 123 124 125 126 127
        case '?':
        default:
            usage();
        }
    }

128
    if (argc - optind > 0) {
129
        usage();
130 131
    }

132
    if (!use_ipv4 && !use_ipv6) {
133
        cerr << "[b10-auth] Error: -4 and -6 can't coexist" << endl;
134 135 136
        usage();
    }

137
    // initialize command channel
138
    int ret = 0;
139 140

    XfroutClient xfrout_client(UNIX_SOCKET_FILE);
141
    try {
142
        string specfile;
143 144
        if (getenv("B10_FROM_BUILD")) {
            specfile = string(getenv("B10_FROM_BUILD")) +
145
                "/src/bin/auth/auth.spec";
146
        } else {
147
            specfile = string(AUTH_SPECFILE_LOCATION);
148
        }
149

150
        auth_server = new AuthSrv(xfrout_client);
151 152
        auth_server->setVerbose(verbose_mode);

153 154 155
        io_service = new asio_link::IOService(auth_server, port, use_ipv4,
                                              use_ipv6);

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

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

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

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