main.cc 4.48 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
17
//
// 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>
#include <iostream>
#include <log/dummylog.h>
18
#include <log/logger_support.h>
19
#include <dhcp4/ctrl_dhcp4_srv.h>
20
#include <boost/lexical_cast.hpp>
21
22
23
24

using namespace std;
using namespace isc::dhcp;

25
26
27
28
29
30
31
32
/// 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.
33

34
namespace {
35

36
37
const char* const DHCP4_NAME = "b10-dhcp4";

38
39
40
41
42
void
usage() {
    cerr << "Usage:  b10-dhcp4 [-v]"
         << endl;
    cerr << "\t-v: verbose output" << endl;
43
    cerr << "\t-s: stand-alone mode (don't connect to BIND10)" << endl;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
44
45
    cerr << "\t-p number: specify non-standard port number 1-65535 "
         << "(useful for testing only)" << endl;
46
    exit(EXIT_FAILURE);
47
48
49
50
51
52
}
} // end of anonymous namespace

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

58
    while ((ch = getopt(argc, argv, "vsp:")) != -1) {
59
60
61
62
63
        switch (ch) {
        case 'v':
            verbose_mode = true;
            isc::log::denabled = true;
            break;
64
65
66
        case 's':
            stand_alone = true;
            break;
67
        case 'p':
68
69
70
71
72
73
74
75
            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) {
76
77
78
79
80
                cerr << "Failed to parse port number: [" << optarg
                     << "], 1-65535 allowed." << endl;
                usage();
            }
            break;
81
82
83
84
85
86
        case ':':
        default:
            usage();
        }
    }

87
88
89
90
91
    // 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);

92
93
94
    cout << "b10-dhcp4: My pid=" << getpid() << ", binding to port "
         << port_number << ", verbose " << (verbose_mode?"yes":"no")
         << ", stand-alone=" << (stand_alone?"yes":"no") << endl;
95
96
97
98
99

    if (argc - optind > 0) {
        usage();
    }

100
    int ret = EXIT_SUCCESS;
101
102

    try {
103

104
105
        cout << "[b10-dhcp4] Initiating DHCPv4 server operation." << endl;

Tomek Mrugalski's avatar
Tomek Mrugalski committed
106
        /// @todo: pass verbose to the actul server once logging is implemented
107
        ControlledDhcpv4Srv server(port_number);
108
109
110

        if (!stand_alone) {
            try {
111
                server.establishSession();
112
113
114
115
116
117
118
119
120
121
            } catch (const std::exception& ex) {
                cerr << "Failed to establish BIND10 session. "
                    "Running in stand-alone mode:" << ex.what() << endl;
                // Let's continue. It is useful to have the ability to run
                // DHCP server in stand-alone mode, e.g. for testing
            }
        } else {
            cout << "Skipping connection to the BIND10 msgq." << endl;
        }

122
        server.run();
123
    } catch (const std::exception& ex) {
124
        cerr << "[b10-dhcp4] Server failed: " << ex.what() << endl;
125
        ret = EXIT_FAILURE;
126
127
128
129
    }

    return (ret);
}