main.cc 5.2 KB
Newer Older
1
// Copyright (C) 2011-2012  Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
// 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 <config.h>

17
18
19
#include <dhcp6/ctrl_dhcp6_srv.h>
#include <dhcp6/dhcp6_log.h>
#include <log/logger_support.h>
20
#include <log/logger_manager.h>
21

22
23
24
25
#include <boost/lexical_cast.hpp>

#include <iostream>

26
using namespace isc::dhcp;
27
using namespace std;
28

29
30
31
32
33
34
35
36
37
/// This file contains entry point (main() function) for standard DHCPv6 server
/// component for BIND10 framework. It parses command-line arguments and
/// instantiates ControlledDhcpv6Srv class that is responsible for establishing
/// connection with msgq (receiving commands and configuration) and also
/// creating Dhcpv6 server object as well.
///
/// For detailed explanation or relations between main(), ControlledDhcpv6Srv,
/// Dhcpv6Srv and other classes, see \ref dhcpv6Session.

38
namespace {
39
40
41
42
43
44
45
46
47
48
// @todo: Replace the next line by extraction from configuration parameters
// This is the "dbconfig" string for the MySQL database.  It is likely
// that a long-term solution will be to create the instance of the lease manager
// somewhere other than the Dhcpv6Srv constructor, to give time to extract
// the connection string from the configuration database.
#ifdef HAVE_MYSQL
const char* DBCONFIG = "type=mysql name=kea user=kea password=kea host=localhost";
#else
const char* DBCONFIG = "type=memfile";
#endif
49

50
const char* const DHCP6_NAME = "b10-dhcp6";
51
52
53

void
usage() {
54
55
56
57
    cerr << "Usage: " << DHCP6_NAME << " [-v] [-s] [-p number]" << endl;
    cerr << "  -v: verbose output" << endl;
    cerr << "  -s: stand-alone mode (don't connect to BIND10)" << endl;
    cerr << "  -p number: specify non-standard port number 1-65535 "
58
         << "(useful for testing only)" << endl;
59
    exit(EXIT_FAILURE);
60
61
62
63
64
65
}
} // end of anonymous namespace

int
main(int argc, char* argv[]) {
    int ch;
66
67
    int port_number = DHCP6_SERVER_PORT; // The default. Any other values are
                                         // useful for testing only.
68
    bool stand_alone = false;  // Should be connect to BIND10 msgq?
69
    bool verbose_mode = false; // Should server be verbose?
70

71
    while ((ch = getopt(argc, argv, "vsp:")) != -1) {
72
73
74
75
        switch (ch) {
        case 'v':
            verbose_mode = true;
            break;
76

77
78
79
        case 's':
            stand_alone = true;
            break;
80

81
        case 'p':
82
83
84
85
86
87
88
89
            try {
                port_number = boost::lexical_cast<int>(optarg);
            } catch (const boost::bad_lexical_cast &) {
                cerr << "Failed to parse port number: [" << optarg
                     << "], 1-65535 allowed." << endl;
                usage();
            }
            if (port_number <= 0 || port_number > 65535) {
90
91
92
93
94
                cerr << "Failed to parse port number: [" << optarg
                     << "], 1-65535 allowed." << endl;
                usage();
            }
            break;
95

96
97
98
99
100
        default:
            usage();
        }
    }

101
102
103
104
105
    // Check for extraneous parameters.
    if (argc > optind) {
        usage();
    }

106
    // Initialize logging.  If verbose, we'll use maximum verbosity.
107
    // If standalone is enabled, do not buffer initial log messages
108
109
    isc::log::initLogger(DHCP6_NAME,
                         (verbose_mode ? isc::log::DEBUG : isc::log::INFO),
110
                         isc::log::MAX_DEBUG_LEVEL, NULL, !stand_alone);
111
112
113
114
    LOG_INFO(dhcp6_logger, DHCP6_STARTING);
    LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_START_INFO)
              .arg(getpid()).arg(port_number).arg(verbose_mode ? "yes" : "no")
              .arg(stand_alone ? "yes" : "no" );
115

116
    int ret = EXIT_SUCCESS;
117
    try {
118
        ControlledDhcpv6Srv server(port_number, DBCONFIG);
119
120
        if (!stand_alone) {
            try {
121
                server.establishSession();
122
            } catch (const std::exception& ex) {
123
                LOG_ERROR(dhcp6_logger, DHCP6_SESSION_FAIL).arg(ex.what());
124
                // Let's continue. It is useful to have the ability to run
125
                // DHCP server in stand-alone mode, e.g. for testing
126
127
128
129
                // We do need to make sure logging is no longer buffered
                // since then it would not print until dhcp6 is stopped
                isc::log::LoggerManager log_manager;
                log_manager.process();
130
131
            }
        } else {
132
            LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_STANDALONE);
133
        }
134
        server.run();
135
        LOG_INFO(dhcp6_logger, DHCP6_SHUTDOWN);
136
137

    } catch (const std::exception& ex) {
138
        LOG_FATAL(dhcp6_logger, DHCP6_SERVER_FAILED).arg(ex.what());
139
        ret = EXIT_FAILURE;
140
141
142
143
    }

    return (ret);
}