dhcp4_srv.cc 5.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright (C) 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 <dhcp/dhcp4.h>
#include <dhcp/pkt4.h>
17
#include <dhcp/iface_mgr.h>
18
19
20
21
22
23
24
25
#include <dhcp4/dhcp4_srv.h>
#include <asiolink/io_address.h>

using namespace std;
using namespace isc;
using namespace isc::dhcp;
using namespace isc::asiolink;

26
27
// #define ECHO_SERVER

28
29
Dhcpv4Srv::Dhcpv4Srv(uint16_t port) {
    cout << "Initialization: opening sockets on port " << port << endl;
30
31
32
33
34
35

    // first call to instance() will create IfaceMgr (it's a singleton)
    // it may throw something if things go wrong
    IfaceMgr::instance();

    /// @todo: instantiate LeaseMgr here once it is imlpemented.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
36
37
    IfaceMgr::instance().printIfaces();

38
    // uncomment this once #1238, #992 and #1239 are merged
Tomek Mrugalski's avatar
Tomek Mrugalski committed
39
    IfaceMgr::instance().openSockets4(port);
40
41
42

    setServerID();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
43
    shutdown_ = false;
44
45
46
47
}

Dhcpv4Srv::~Dhcpv4Srv() {
    cout << "DHCPv4 server shutdown." << endl;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
48
    IfaceMgr::instance().closeSockets();
49
50
51
52
}

bool
Dhcpv4Srv::run() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
53
    while (!shutdown_) {
54
55
56
57
        boost::shared_ptr<Pkt4> query; // client's message
        boost::shared_ptr<Pkt4> rsp;   // server's response

        query = IfaceMgr::instance().receive4();
58

59
#if defined(ECHO_SERVER)
Tomek Mrugalski's avatar
Tomek Mrugalski committed
60
        query->repack();
61
62
        IfaceMgr::instance().send(query);
        continue;
63
64
65
#endif

        if (query) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
66
67
68
69
            try {
                query->unpack();
            } catch (const std::exception& e) {
                /// TODO: Printout reasons of failed parsing
70
                cout << "Failed to parse incoming packet " << endl;
71
72
                continue;
            }
73

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
            switch (query->getType()) {
            case DHCPDISCOVER:
                rsp = processDiscover(query);
                break;
            case DHCPREQUEST:
                rsp = processRequest(query);
                break;
            case DHCPRELEASE:
                processRelease(query);
                break;
            case DHCPDECLINE:
                processDecline(query);
                break;
            case DHCPINFORM:
                processInform(query);
                break;
            default:
                cout << "Unknown pkt type received:"
                     << query->getType() << endl;
            }

95
            cout << "Received message type " << int(query->getType()) << endl;
96

97
98
            // TODO: print out received packets only if verbose (or debug)
            // mode is enabled
99
100
101
            cout << query->toText();

            if (rsp) {
102
103
104
105
106
107
108
                rsp->setRemoteAddr(query->getRemoteAddr());
                rsp->setLocalAddr(query->getLocalAddr());
                rsp->setRemotePort(DHCP4_CLIENT_PORT);
                rsp->setLocalPort(DHCP4_SERVER_PORT);
                rsp->setIface(query->getIface());
                rsp->setIndex(query->getIndex());

Tomek Mrugalski's avatar
Tomek Mrugalski committed
109
110
                cout << "Replying with message type "
                     << static_cast<int>(rsp->getType()) << ":" << endl;
111
112
113
                cout << rsp->toText();
                cout << "----" << endl;
                if (rsp->pack()) {
114
                    cout << "Packet assembled correctly." << endl;
115
                }
116
#if 1
117
                // uncomment this once ticket 1240 is merged.
118
                IfaceMgr::instance().send(rsp);
119
120
121
122
123
124
125
126
127
128
129
130
131
#endif
            }
        }

        // TODO add support for config session (see src/bin/auth/main.cc)
        //      so this daemon can be controlled from bob
    }

    return (true);
}

void
Dhcpv4Srv::setServerID() {
132
133
134
    /// TODO implement this for real once interface detection (ticket 1237)
    /// is done. Use hardcoded server-id for now.

135
#if 0
136
    // uncomment this once ticket 1350 is merged.
137
138
139
140
141
142
143
    IOAddress srvId("127.0.0.1");
    serverid_ = boost::shared_ptr<Option>(
      new Option4AddrLst(Option::V4, DHO_DHCP_SERVER_IDENTIFIER, srvId));
#endif
}

boost::shared_ptr<Pkt4>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
144
Dhcpv4Srv::processDiscover(boost::shared_ptr<Pkt4>& discover) {
145
    /// TODO: Currently implemented echo mode. Implement this for real
146
147
148
149
    return (discover);
}

boost::shared_ptr<Pkt4>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
150
Dhcpv4Srv::processRequest(boost::shared_ptr<Pkt4>& request) {
151
    /// TODO: Currently implemented echo mode. Implement this for real
152
153
154
    return (request);
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
155
void Dhcpv4Srv::processRelease(boost::shared_ptr<Pkt4>& release) {
156
157
158
    /// TODO: Implement this.
    cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
}
159

Tomek Mrugalski's avatar
Tomek Mrugalski committed
160
void Dhcpv4Srv::processDecline(boost::shared_ptr<Pkt4>& decline) {
161
162
163
164
    /// TODO: Implement this.
    cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
165
boost::shared_ptr<Pkt4> Dhcpv4Srv::processInform(boost::shared_ptr<Pkt4>& inform) {
166
    /// TODO: Currently implemented echo mode. Implement this for real
167
168
    return (inform);
}