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

17
#include <dhcp4/ctrl_dhcp4_srv.h>
18
19
#include <dhcp4/dhcp4_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
/// This file contains entry point (main() function) for standard DHCPv4 server
/// component for BIND10 framework. It parses command-line arguments and
/// instantiates ControlledDhcpv4Srv class that is responsible for establishing
/// connection with msgq (receiving commands and configuration) and also
/// creating Dhcpv4 server object as well.
///
/// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
/// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
37

38
namespace {
39

40
41
const char* const DHCP4_NAME = "b10-dhcp4";

42
43
void
usage() {
44
45
46
47
    cerr << "Usage: " << DHCP4_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 "
Tomek Mrugalski's avatar
Tomek Mrugalski committed
48
         << "(useful for testing only)" << endl;
49
    exit(EXIT_FAILURE);
50
51
52
53
54
55
}
} // end of anonymous namespace

int
main(int argc, char* argv[]) {
    int ch;
56
57
    int port_number = DHCP4_SERVER_PORT; // The default. any other values are
                                         // useful for testing only.
58
59
    bool stand_alone = false;  // Should be connect to BIND10 msgq?
    bool verbose_mode = false; // Should server be verbose?
60

61
    while ((ch = getopt(argc, argv, "vsp:")) != -1) {
62
63
64
65
        switch (ch) {
        case 'v':
            verbose_mode = true;
            break;
66

67
68
69
        case 's':
            stand_alone = true;
            break;
70

71
        case 'p':
72
73
74
75
76
77
78
79
            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) {
80
81
82
83
84
                cerr << "Failed to parse port number: [" << optarg
                     << "], 1-65535 allowed." << endl;
                usage();
            }
            break;
85

86
87
88
89
90
        default:
            usage();
        }
    }

91
    // Check for extraneous parameters.
92
    if (argc > optind) {
93
94
95
        usage();
    }

96
    // Initialize logging.  If verbose, we'll use maximum verbosity.
97
    // If standalone is enabled, do not buffer initial log messages
98
99
    isc::log::initLogger(DHCP4_NAME,
                         (verbose_mode ? isc::log::DEBUG : isc::log::INFO),
100
                         isc::log::MAX_DEBUG_LEVEL, NULL, !stand_alone);
101
102
103
104
    LOG_INFO(dhcp4_logger, DHCP4_STARTING);
    LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_START_INFO)
              .arg(getpid()).arg(port_number).arg(verbose_mode ? "yes" : "no")
              .arg(stand_alone ? "yes" : "no" );
105

106

107
    int ret = EXIT_SUCCESS;
108
    try {
109
        ControlledDhcpv4Srv server(port_number);
110
111
        if (!stand_alone) {
            try {
112
                server.establishSession();
113
            } catch (const std::exception& ex) {
114
                LOG_ERROR(dhcp4_logger, DHCP4_SESSION_FAIL).arg(ex.what());
115
116
117
118
                // Let's continue. It is useful to have the ability to run
                // DHCP server in stand-alone mode, e.g. for testing
            }
        } else {
119
            std::cout << "[XX] STANDALONE" << std::endl;
120
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_STANDALONE);
121
        }
122
        server.run();
123
124
        LOG_INFO(dhcp4_logger, DHCP4_SHUTDOWN);

125
    } catch (const std::exception& ex) {
126
        LOG_FATAL(dhcp4_logger, DHCP4_SERVER_FAILED).arg(ex.what());
127
        ret = EXIT_FAILURE;
128
129
130
131
    }

    return (ret);
}