main.cc 4.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright (C) 2009-2011  Internet Systems Consortium, Inc. ("ISC")
//
// 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 <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>

#include <cassert>
#include <iostream>

28
29
30
#include <cc/session.h>
#include <cc/data.h>

31
32
33
34
35
36
37
38
39
#include <exceptions/exceptions.h>
#include <cc/session.h>
#include <config/ccsession.h>

#include <util/buffer.h>
#include <log/dummylog.h>

#include <dhcp4/spec_config.h>
#include <dhcp4/dhcp4_srv.h>
40
#include <dhcp/iface_mgr.h>
41

42
43
44
45
46
#include <asiolink/asiolink.h>
#include <log/logger_support.h>

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

47
48
49
using namespace std;
using namespace isc::util;
using namespace isc::dhcp;
50
51
52
53
54
using namespace isc::util;
using namespace isc::data;
using namespace isc::cc;
using namespace isc::config;
using namespace isc::asiolink;
55
56
57
58
59
60
61
62
63
64
65
66
67
68

namespace {

bool verbose_mode = false;

void
usage() {
    cerr << "Usage:  b10-dhcp4 [-v]"
         << endl;
    cerr << "\t-v: verbose output" << endl;
    exit(1);
}
} // end of anonymous namespace

69
70
// Global objects are ugly, but that is the most convenient way of
// having it accessible from handlers.
71
72
IOService io_service;

73
74
75
// The same applies to global pointers. Ugly, but useful.
Dhcpv4Srv* server = NULL;

76
77
ConstElementPtr
dhcp4_config_handler(ConstElementPtr new_config) {
78
    cout << "b10-dhcp4: Received new config:" << new_config->str() << endl;
79
    ConstElementPtr answer = isc::config::createAnswer(0,
80
                             "Thanks for sending config.");
81
82
83
84
85
    return (answer);
}

ConstElementPtr
dhcp4_command_handler(const string& command, ConstElementPtr args) {
86
    cout << "b10-dhcp4: Received new command: [" << command << "], args="
87
         << args->str() << endl;
88
    if (command == "shutdown") {
89
90
91
        if (server) {
            server->shutdown();
        }
92
93
94
95
96
97
98
99
100
        io_service.stop();
        ConstElementPtr answer = isc::config::createAnswer(0,
                                 "Shutting down.");
        return (answer);
    }

    ConstElementPtr answer = isc::config::createAnswer(1,
                             "Unrecognized command.");

101
102
103
    return (answer);
}

104
105
106
void session_reader(void) {
    io_service.run_one();
}
107
108
109
110
111
112
113
114
115
116
117
118
119
120

void establish_session() {

    Session* cc_session = NULL;
    ModuleCCSession* config_session = NULL;

    string specfile;
    if (getenv("B10_FROM_BUILD")) {
        specfile = string(getenv("B10_FROM_BUILD")) +
            "/src/bin/auth/dhcp4.spec";
    } else {
        specfile = string(DHCP4_SPECFILE_LOCATION);
    }

121
    cout << "b10-dhcp4: my specfile is " << specfile << endl;
122
123
124
125
126
127
128

    cc_session = new Session(io_service.get_io_service());

    config_session = new ModuleCCSession(specfile, *cc_session,
                                         dhcp4_config_handler,
                                         dhcp4_command_handler, false);

129
130
    config_session->start();

131
132
133
134
135
136
137
138
139
    int ctrl_socket = cc_session->getSocketDesc();
    cout << "b10-dhcp4: Control session started, socket="
         << ctrl_socket << endl;

    IfaceMgr::instance().set_session_socket(ctrl_socket, session_reader);

    // cout << "b10-dhcp4: About to call io_service.run()" << endl;
    // io_service.run();
    // cout << "b10-dhcp4: Returned from io_service.run()" << endl;
140
141
}

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
int
main(int argc, char* argv[]) {
    int ch;

    while ((ch = getopt(argc, argv, ":v")) != -1) {
        switch (ch) {
        case 'v':
            verbose_mode = true;
            isc::log::denabled = true;
            break;
        case ':':
        default:
            usage();
        }
    }

158
159
160
161
162
163
    // 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);


164
165
166
167
168
169
170
171
172
    cout << "My pid=" << getpid() << endl;

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

    int ret = 0;

    try {
173
174

        establish_session();
175
176
177

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

178
179
180
        server = new Dhcpv4Srv();

        server->run();
181

182
        delete server;
183
184

    } catch (const std::exception& ex) {
185
        cerr << "[b10-dhcp4] Server failed: " << ex.what() << endl;
186
187
188
189
190
        ret = 1;
    }

    return (ret);
}