ctrl_dhcp4_srv.h 6.52 KB
Newer Older
1
// Copyright (C) 2012-2014  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 CTRL_DHCPV4_SRV_H
#define CTRL_DHCPV4_SRV_H

#include <asiolink/asiolink.h>
19
#include <cc/data.h>
20
21
#include <cc/session.h>
#include <config/ccsession.h>
22
#include <dhcp4/dhcp4_srv.h>
23
24
25
26
27
28

namespace isc {
namespace dhcp {

/// @brief Controlled version of the DHCPv4 server
///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
29
30
31
32
/// This is a class that is responsible for DHCPv4 server being controllable.
/// It does various things, depending on the configuration backend.
/// For Bundy backend it establishes a connection with msqg and later receives
/// commands over it. For Kea backend, it reads configuration file from disk.
33
34
35
36
37
///
/// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
/// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
class ControlledDhcpv4Srv : public isc::dhcp::Dhcpv4Srv {
public:
38
39
40
41

    /// @brief Constructor
    ///
    /// @param port UDP port to be opened for DHCP traffic
42
    ControlledDhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT);
43

44
45
46
    /// @brief Destructor.
    ~ControlledDhcpv4Srv();

47
    /// @brief Initializes the server.
48
    ///
49
50
51
52
53
    /// Depending on the configuration backend, it establishes msgq session,
    /// reads the JSON file from disk or may perform any other setup
    /// operation. For specific details, see actual implementation in
    /// *_backend.cc
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
54
55
    /// This method may throw if initialization fails. Exception types may be
    /// specific to used configuration backend.
56
    void init(const std::string& config_file);
57

58
    /// @brief Performs cleanup, immediately before termination
59
    ///
60
    /// This method performs final clean up, just before the Dhcpv4Srv object
61
62
    /// is destroyed. The actual behavior is backend dependent. For Bundy
    /// backend, it terminates existing session with msgq. After calling
63
    /// it, no further messages over msgq (commands or configuration updates)
64
    /// may be received. For JSON backend, it is no-op.
65
    ///
66
67
    /// For specific details, see actual implementation in *_backend.cc
    void cleanup();
68
69
70
71

    /// @brief Initiates shutdown procedure for the whole DHCPv4 server.
    void shutdown();

72
    /// @brief Command processor
73
74
75
76
77
78
    ///
    /// This method is uniform for all config backends. It processes received
    /// command (as a string + JSON arguments). Internally, it's just a
    /// wrapper that calls process*Command() methods and catches exceptions
    /// in them.
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
79
80
81
82
83
    /// Currently supported commands are:
    /// - shutdown
    /// - libreload
    /// - config-reload
    ///
84
    /// @note It never throws.
85
    ///
Francis Dupont's avatar
Francis Dupont committed
86
    /// @param command Text representation of the command (e.g. "shutdown")
87
    /// @param args Optional parameters
88
89
90
    ///
    /// @return status of the command
    static isc::data::ConstElementPtr
91
    processCommand(const std::string& command, isc::data::ConstElementPtr args);
92

93
    /// @brief Configuration processor
94
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
95
    /// This is a method for handling incoming configuration updates.
96
97
    /// This method should be called by all configuration backends when the
    /// server is starting up or when configuration has changed.
98
    ///
99
100
101
    /// As pointer to this method is used a callback in ASIO used in
    /// ModuleCCSession, it has to be static.
    ///
102
103
104
105
    /// @param new_config textual representation of the new configuration
    ///
    /// @return status of the config update
    static isc::data::ConstElementPtr
106
    processConfig(isc::data::ConstElementPtr new_config);
107

108
    /// @brief Returns pointer to the sole instance of Dhcpv4Srv
109
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
110
    /// @return server instance (may return NULL, if called before server is spawned)
111
112
113
114
115
116
117
    static ControlledDhcpv4Srv* getInstance() {
        return (server_);
    }


protected:
    /// @brief Static pointer to the sole instance of the DHCP server.
118
    ///
119
120
121
    /// This is required for config and command handlers to gain access to
    /// the server
    static ControlledDhcpv4Srv* server_;
122

123
124
    /// @brief Callback that will be called from iface_mgr when data
    /// is received over control socket.
125
    ///
126
127
128
129
    /// This static callback method is called from IfaceMgr::receive6() method,
    /// when there is a new command or configuration sent over control socket
    /// (that was sent from msgq if backend is Bundy, or some yet unspecified
    /// sender if the backend is JSON file).
130
131
    static void sessionReader(void);

132
    /// @brief IOService object, used for all ASIO operations.
133
134
    isc::asiolink::IOService io_service_;

135
    /// @brief Handler for processing 'shutdown' command
136
137
138
139
140
141
142
143
144
145
146
    ///
    /// This handler processes shutdown command, which initializes shutdown
    /// procedure.
    /// @param command (parameter ignored)
    /// @param args (parameter ignored)
    ///
    /// @return status of the command
    isc::data::ConstElementPtr
    commandShutdownHandler(const std::string& command,
                           isc::data::ConstElementPtr args);

147
    /// @brief Handler for processing 'libreload' command
148
149
150
151
152
153
154
155
156
157
158
    ///
    /// This handler processes libreload command, which unloads all hook
    /// libraries and reloads them.
    ///
    /// @param command (parameter ignored)
    /// @param args (parameter ignored)
    ///
    /// @return status of the command
    isc::data::ConstElementPtr
    commandLibReloadHandler(const std::string& command,
                            isc::data::ConstElementPtr args);
159

160
    /// @brief Handler for processing 'config-reload' command
161
162
163
164
165
166
167
168
169
170
171
    ///
    /// This handler processes config-reload command, which processes
    /// configuration specified in args parameter.
    ///
    /// @param command (parameter ignored)
    /// @param args configuration to be processed
    ///
    /// @return status of the command
    isc::data::ConstElementPtr
    commandConfigReloadHandler(const std::string& command,
                               isc::data::ConstElementPtr args);
172
173
174
175
176
177
};

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

#endif