main.cc 4.16 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 89 90
usage() {
    cerr << "Usage: b10-auth [-p port] [-4|-6]" << endl;
    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;
98
    bool use_ipv4 = true, use_ipv6 = true;
99

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

125
    if (argc - optind > 0) {
126
        usage();
127 128
    }

129
    if (!use_ipv4 && !use_ipv6) {
130 131 132 133
        cerr << "-4 and -6 can't coexist" << endl;
        usage();
    }

134
    // initialize command channel
135
    int ret = 0;
136
    try {
137
        string specfile;
138
        if (getenv("B10_FROM_SOURCE")) {
139 140
            specfile = string(getenv("B10_FROM_SOURCE")) +
                "/src/bin/auth/auth.spec";
141
        } else {
142
            specfile = string(AUTH_SPECFILE_LOCATION);
143
        }
144

145 146 147 148 149 150 151 152
        auth_server = new AuthSrv;
        auth_server->setVerbose(verbose_mode);

        ModuleCCSession cs(specfile, my_config_handler, my_command_handler);

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

153 154 155
        io_service = new asio_link::IOService(auth_server, port, use_ipv4,
                                              use_ipv6);
        io_service->run();
156 157
    } catch (const std::exception& ex) {
        cerr << ex.what() << endl;
158
        ret = 1;
159
    }
160

161
    delete io_service;
162 163
    delete auth_server;
    return (ret);
164
}