dhcp4_srv.h 6.42 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.

#ifndef DHCPV4_SRV_H
#define DHCPV4_SRV_H

#include <boost/noncopyable.hpp>
19
#include <dhcp/dhcp4.h>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <dhcp/pkt4.h>
#include <dhcp/option.h>
#include <iostream>

namespace isc {

namespace dhcp {
/// @brief DHCPv4 server service.
///
/// This singleton class represents DHCPv4 server. It contains all
/// top-level methods and routines necessary for server operation.
/// In particular, it instantiates IfaceMgr, loads or generates DUID
/// that is going to be used as server-identifier, receives incoming
/// packets, processes them, manages leases assignment and generates
/// appropriate responses.
class Dhcpv4Srv : public boost::noncopyable {

Tomek Mrugalski's avatar
Tomek Mrugalski committed
37
    public:
38 39 40 41 42
    /// @brief Default constructor.
    ///
    /// Instantiates necessary services, required to run DHCPv6 server.
    /// In particular, creates IfaceMgr that will be responsible for
    /// network interaction. Will instantiate lease manager, and load
Tomek Mrugalski's avatar
Tomek Mrugalski committed
43 44 45 46 47
    /// old or create new DUID. It is possible to specify alternate
    /// port on which DHCPv4 server will listen on. That is mostly useful
    /// for testing purposes.
    ///
    /// @param port specifies port number to listen on
48
    Dhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT);
49 50 51 52 53 54 55 56 57 58 59 60 61 62

    /// @brief Destructor. Used during DHCPv6 service shutdown.
    ~Dhcpv4Srv();

    /// @brief Main server processing loop.
    ///
    /// Main server processing loop. Receives incoming packets, verifies
    /// their correctness, generates appropriate answer (if needed) and
    /// transmits respones.
    ///
    /// @return true, if being shut down gracefully, fail if experienced
    ///         critical error.
    bool run();

63 64 65
    /// @brief instructs server to shut down.
    void shutdown();

66 67 68 69 70 71 72
protected:
    /// @brief Processes incoming DISCOVER and returns response.
    ///
    /// Processes received DISCOVER message and verifies that its sender
    /// should be served. In particular, a lease is selected and sent
    /// as an offer to a client if it should be served.
    ///
73
    /// @param discover DISCOVER message received from client
74 75
    ///
    /// @return OFFER message or NULL
76
    Pkt4Ptr processDiscover(Pkt4Ptr& discover);
77 78 79 80 81

    /// @brief Processes incoming REQUEST and returns REPLY response.
    ///
    /// Processes incoming REQUEST message and verifies that its sender
    /// should be served. In particular, verifies that requested lease
Tomek Mrugalski's avatar
Tomek Mrugalski committed
82
    /// is valid, not expired, not reserved, not used by other client and
83 84 85 86 87 88 89
    /// that requesting client is allowed to use it.
    ///
    /// Returns ACK message, NACK message, or NULL
    ///
    /// @param request a message received from client
    ///
    /// @return ACK or NACK message
90
    Pkt4Ptr processRequest(Pkt4Ptr& request);
91 92 93 94 95 96 97

    /// @brief Stub function that will handle incoming RELEASE messages.
    ///
    /// In DHCPv4, server does not respond to RELEASE messages, therefore
    /// this function does not return anything.
    ///
    /// @param release message received from client
98
    void processRelease(Pkt4Ptr& release);
99 100 101 102

    /// @brief Stub function that will handle incoming DHCPDECLINE messages.
    ///
    /// @param decline message received from client
103
    void processDecline(Pkt4Ptr& decline);
104 105 106

    /// @brief Stub function that will handle incoming INFORM messages.
    ///
107
    /// @param inform message received from client
108
    Pkt4Ptr processInform(Pkt4Ptr& inform);
109

110 111 112 113 114 115 116
    /// @brief Copies default parameters from client's to server's message
    ///
    /// Some fields are copied from client's message into server's response,
    /// e.g. client HW address, number of hops, transaction-id etc.
    ///
    /// @param question any message sent by client
    /// @param answer any message server is going to send as response
117
    void copyDefaultFields(const Pkt4Ptr& question, Pkt4Ptr& answer);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
118 119 120 121

    /// @brief Appends options requested by client.
    ///
    /// This method assigns options that were requested by client
Tomek Mrugalski's avatar
Tomek Mrugalski committed
122
    /// (sent in PRL) or are enforced by server.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
123 124
    ///
    /// @param msg outgoing message (options will be added here)
125
    void appendRequestedOptions(Pkt4Ptr& msg);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
126 127 128 129 130 131 132 133 134 135 136

    /// @brief Assigns a lease and appends corresponding options
    ///
    /// This method chooses the most appropriate lease for reqesting
    /// client and assigning it. Options corresponding to the lease
    /// are added to specific message.
    ///
    /// Note: Lease manager is not implemented yet, so this method
    /// used fixed, hardcoded lease.
    ///
    /// @param msg OFFER or ACK message (lease options will be added here)
137
    void tryAssignLease(Pkt4Ptr& msg);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
138

139 140 141 142
    /// @brief Appends default options to a message
    ///
    /// @param msg message object (options will be added to it)
    /// @param msg_type specifies message type
143
    void appendDefaultOptions(Pkt4Ptr& msg, uint8_t msg_type);
144

145 146 147
    /// @brief Returns server-intentifier option
    ///
    /// @return server-id option
148
    OptionPtr getServerID() { return serverid_; }
149 150 151 152 153 154 155 156 157 158 159 160 161

    /// @brief Sets server-identifier.
    ///
    /// This method attempts to set server-identifier DUID. It tries to
    /// load previously stored IP from configuration. If there is no previously
    /// stored server identifier, it will pick up one address from configured
    /// and supported network interfaces.
    ///
    /// @throws isc::Unexpected Failed to obtain server identifier (i.e. no
    //          previously stored configuration and no network interfaces available)
    void setServerID();

    /// server DUID (to be sent in server-identifier option)
162
    OptionPtr serverid_;
163 164 165

    /// indicates if shutdown is in progress. Setting it to true will
    /// initiate server shutdown procedure.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
166
    volatile bool shutdown_;
167 168 169 170 171 172
};

}; // namespace isc::dhcp
}; // namespace isc

#endif // DHCP4_SRV_H