main.cc 4.35 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

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

#include <iostream>

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

28
29
30
31
32
33
34
35
/// 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.
36

37
namespace {
38

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

41
42
void
usage() {
43
44
45
46
    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
47
         << "(useful for testing only)" << endl;
48
    exit(EXIT_FAILURE);
49
50
51
52
53
54
}
} // end of anonymous namespace

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

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

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

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

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

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

95
96
97
98
    // Initialize logging.  If verbose, we'll use maximum verbosity.
    isc::log::initLogger(DHCP4_NAME,
                         (verbose_mode ? isc::log::DEBUG : isc::log::INFO),
                         isc::log::MAX_DEBUG_LEVEL, NULL);
99
100
101
102
    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" );
103

104

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

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

    return (ret);
}