main.cc 4.32 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
40
#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>

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

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

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

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

68
69
ConstElementPtr
dhcp4_config_handler(ConstElementPtr new_config) {
70
    cout << "b10-dhcp4: Received new config:" << new_config->str() << endl;
71
72
73
74
75
76
77
    ConstElementPtr answer = isc::config::createAnswer(0,
                             "All good here. Thanks for sending config.");
    return (answer);
}

ConstElementPtr
dhcp4_command_handler(const string& command, ConstElementPtr args) {
78
    cout << "b10-dhcp4: Received new command: [" << command << "], args="
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
         << args->str() << endl;
    ConstElementPtr answer = isc::config::createAnswer(0,
                             "All good here. Thanks for asking.");
    return (answer);
}


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);
    }

99
    cout << "b10-dhcp4: my specfile is " << specfile << endl;
100
101
102
103
104
105
106
107

    IOService io_service;

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

    config_session = new ModuleCCSession(specfile, *cc_session,
                                         dhcp4_config_handler,
                                         dhcp4_command_handler, false);
108
    cout << "b10-dhcp4: hasQueuedMsgs()=" << config_session->hasQueuedMsgs() << endl;
109

110
111
112
113
114
115
    config_session->start();
    cout << "b10-dhcp4: After session start." << endl;

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

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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();
        }
    }

134
135
136
137
138
139
    // 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);


140
141
142
143
144
145
146
147
148
    cout << "My pid=" << getpid() << endl;

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

    int ret = 0;

    try {
149
150

        establish_session();
151
152
153
154
155
156
157
158

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

        Dhcpv4Srv* srv = new Dhcpv4Srv();

        srv->run();

    } catch (const std::exception& ex) {
159
        cerr << "[b10-dhcp4] Server failed: " << ex.what() << endl;
160
161
162
163
164
        ret = 1;
    }

    return (ret);
}