dhcp4_srv.cc 7.55 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
#include <dhcp4/dhcp4_srv.h>
#include <asiolink/io_address.h>
20
#include <dhcp/option4_addrlst.h>
21
22
23
24
25
26

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

27
28
29
30
// This is the hardcoded lease. Currently this is a skeleton server that only
// grants this option.
#define HARDCODED_LEASE "10.3.2.222"

31
32
// #define ECHO_SERVER

33
34
Dhcpv4Srv::Dhcpv4Srv(uint16_t port) {
    cout << "Initialization: opening sockets on port " << port << endl;
35
36
37
38
39
40

    // 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
41
42
    IfaceMgr::instance().printIfaces();

43
    // uncomment this once #1238, #992 and #1239 are merged
Tomek Mrugalski's avatar
Tomek Mrugalski committed
44
    IfaceMgr::instance().openSockets4(port);
45
46
47

    setServerID();

Tomek Mrugalski's avatar
Tomek Mrugalski committed
48
    shutdown_ = false;
49
50
51
52
}

Dhcpv4Srv::~Dhcpv4Srv() {
    cout << "DHCPv4 server shutdown." << endl;
Tomek Mrugalski's avatar
Tomek Mrugalski committed
53
    IfaceMgr::instance().closeSockets();
54
55
56
57
}

bool
Dhcpv4Srv::run() {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
58
    while (!shutdown_) {
59
60
61
62
        boost::shared_ptr<Pkt4> query; // client's message
        boost::shared_ptr<Pkt4> rsp;   // server's response

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

64
#if defined(ECHO_SERVER)
Tomek Mrugalski's avatar
Tomek Mrugalski committed
65
        query->repack();
66
67
        IfaceMgr::instance().send(query);
        continue;
68
69
70
#endif

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

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
            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;
            }

100
            cout << "Received message type " << int(query->getType()) << endl;
101

102
103
            // TODO: print out received packets only if verbose (or debug)
            // mode is enabled
104
105
106
            cout << query->toText();

            if (rsp) {
107
108
109
110
111
112
113
114
115
                if (rsp->getRemoteAddr().toText() == "0.0.0.0") {
                    rsp->setRemoteAddr(query->getRemoteAddr());
                }
                if (!rsp->getHops()) {
                    rsp->setRemotePort(DHCP4_CLIENT_PORT);
                } else {
                    rsp->setRemotePort(DHCP4_SERVER_PORT);
                }

116
117
118
119
120
                rsp->setLocalAddr(query->getLocalAddr());
                rsp->setLocalPort(DHCP4_SERVER_PORT);
                rsp->setIface(query->getIface());
                rsp->setIndex(query->getIndex());

Tomek Mrugalski's avatar
Tomek Mrugalski committed
121
122
                cout << "Replying with message type "
                     << static_cast<int>(rsp->getType()) << ":" << endl;
123
124
125
                cout << rsp->toText();
                cout << "----" << endl;
                if (rsp->pack()) {
126
                    cout << "Packet assembled correctly." << endl;
127
                }
128
#if 1
129
                // uncomment this once ticket 1240 is merged.
130
                IfaceMgr::instance().send(rsp);
131
132
133
134
135
136
137
138
139
140
141
142
143
#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() {
144
145
146
    /// TODO implement this for real once interface detection (ticket 1237)
    /// is done. Use hardcoded server-id for now.

147
#if 0
148
    // uncomment this once ticket 1350 is merged.
149
150
151
152
153
154
155
    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
156
Dhcpv4Srv::processDiscover(boost::shared_ptr<Pkt4>& discover) {
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
    boost::shared_ptr<Pkt4> offer = boost::shared_ptr<Pkt4>(new Pkt4(DHCPOFFER, discover->getTransid()));


    offer->setIface(discover->getIface());
    offer->setIndex(discover->getIndex());
    offer->setCiaddr(discover->getCiaddr());

    offer->setSiaddr(IOAddress("0.0.0.0")); // explictly set this to 0

    offer->setHops(discover->getHops());

    // copy MAC address
    vector<uint8_t> mac;
    mac.resize(Pkt4::MAX_CHADDR_LEN);
    memcpy(&mac[0], discover->getChaddr(), Pkt4::MAX_CHADDR_LEN);
    offer->setHWAddr(discover->getHtype(),
                     discover->getHlen(),
                     mac);

    // relay address
    offer->setGiaddr(discover->getGiaddr());
    if (discover->getGiaddr().toText() != "0.0.0.0") {
        // relayed traffic
        offer->setRemoteAddr(discover->getGiaddr());
    } else {
        // direct traffic
        offer->setRemoteAddr(discover->getRemoteAddr());
    }

    // TODO: Implement actual lease assignment here
    offer->setYiaddr(IOAddress(HARDCODED_LEASE));

    // add Message Type Option (type 53)
    boost::shared_ptr<Option> opt;
    std::vector<uint8_t> tmp;
    tmp.push_back(static_cast<int>(DHCPOFFER));
    opt = boost::shared_ptr<Option>(new Option(Option::V4, DHO_DHCP_MESSAGE_TYPE, tmp));
    offer->addOption(opt);

    // DHCP Server Identifier (type 54)
    opt = boost::shared_ptr<Option>(new Option4AddrLst(54, IOAddress("10.3.1.1")));
    offer->addOption(opt);

    // IP Address Lease time (type 51)

    // Subnet mask (type 1)
    opt = boost::shared_ptr<Option>(new Option4AddrLst(1, IOAddress("255.255.255.0")));
    offer->addOption(opt);

    // Router (type 3)
    opt = boost::shared_ptr<Option>(new Option4AddrLst(3, IOAddress("10.3.2.2")));
    offer->addOption(opt);

    // Domain name (type 15)

    // DNS servers (type 6)
    opt = boost::shared_ptr<Option>(new Option4AddrLst(6, IOAddress("8.8.8.8")));
    offer->addOption(opt);

    return (offer);
217
218
219
}

boost::shared_ptr<Pkt4>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
220
Dhcpv4Srv::processRequest(boost::shared_ptr<Pkt4>& request) {
221
    /// TODO: Currently implemented echo mode. Implement this for real
222
223
224
    return (request);
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
225
void Dhcpv4Srv::processRelease(boost::shared_ptr<Pkt4>& release) {
226
227
228
    /// TODO: Implement this.
    cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
}
229

Tomek Mrugalski's avatar
Tomek Mrugalski committed
230
void Dhcpv4Srv::processDecline(boost::shared_ptr<Pkt4>& decline) {
231
232
233
234
    /// TODO: Implement this.
    cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
}

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