main.cc 3.47 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
18
//
// 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 <exceptions/exceptions.h>
#include <log/dummylog.h>
19
#include <log/logger_support.h>
20
#include <dhcp4/ctrl_dhcp4_srv.h>
21
#include <dhcp4/dhcp4_srv.h>
22
#include <dhcp/dhcp4.h>
23
24
25
26

using namespace std;
using namespace isc::dhcp;

27
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
44
45
46
void
usage() {
    cerr << "Usage:  b10-dhcp4 [-v]"
         << endl;
    cerr << "\t-v: verbose output" << endl;
47
48
    cerr << "\t-p number: specify non-standard port number 1-65535 (useful for testing only)" << endl;
    exit(EXIT_FAILURE);
49
50
51
52
53
54
}
} // end of anonymous namespace

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

59
    while ((ch = getopt(argc, argv, "vp:")) != -1) {
60
61
62
63
64
        switch (ch) {
        case 'v':
            verbose_mode = true;
            isc::log::denabled = true;
            break;
65
66
67
68
69
70
71
72
        case 'p':
            port_number = strtol(optarg, NULL, 10);
            if (port_number == 0) {
                cerr << "Failed to parse port number: [" << optarg
                     << "], 1-65535 allowed." << endl;
                usage();
            }
            break;
73
74
75
76
77
78
        case ':':
        default:
            usage();
        }
    }

79
80
81
82
83
    // 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);

84
    cout << "b10-dhcp4: My pid=" << getpid() << ", binding to port "
85
         << port_number << ", verbose " << (verbose_mode?"yes":"no") << endl;
86
87
88
89
90

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

91
    int ret = EXIT_SUCCESS;
92
93

    try {
94

95
96
        cout << "[b10-dhcp4] Initiating DHCPv4 server operation." << endl;

97
        ControlledDhcpv4Srv* server = new ControlledDhcpv4Srv(port_number);
98
99
100
        server->run();
        delete server;
        server = NULL;
101
102

    } catch (const std::exception& ex) {
103
        cerr << "[b10-dhcp4] Server failed: " << ex.what() << endl;
104
        ret = EXIT_FAILURE;
105
106
107
108
    }

    return (ret);
}