summaryrefslogtreecommitdiffstats
path: root/src/hooks/dhcp/run_script/run_script.h
blob: c66c82e4b7fa96edac5ab18aa8127d8ca90ad009 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (C) 2021 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef RUN_SCRIPT_H
#define RUN_SCRIPT_H

#include <asiolink/process_spawn.h>
#include <dhcp/duid.h>
#include <dhcp/hwaddr.h>
#include <dhcp/option6_ia.h>
#include <dhcp/pkt4.h>
#include <dhcp/pkt6.h>
#include <dhcpsrv/lease.h>
#include <dhcpsrv/subnet.h>
#include <hooks/library_handle.h>
#include <string>

namespace isc {
namespace run_script {

/// @brief Run Script implementation.
class RunScriptImpl {
public:
    /// @brief Constructor.
    RunScriptImpl();

    /// @brief Destructor.
    ~RunScriptImpl() = default;

    /// @brief Sets IO service to be used by the @ref ProcessSpawn instance.
    ///
    /// @param io_service The IOService object, used for all ASIO operations.
    static void setIOService(const isc::asiolink::IOServicePtr& io_service) {
        io_service_ = io_service;
    }

    /// @brief Gets IO service to be used by the @ref ProcessSpawn instance.
    ///
    /// @return The IOService object, used for all ASIO operations.
    static isc::asiolink::IOServicePtr getIOService() {
        return (io_service_);
    }

    /// @brief Extract boolean data and append to environment.
    ///
    /// @param value The value to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractBoolean(isc::asiolink::ProcessEnvVars& vars,
                               const bool value,
                               const std::string& prefix = "",
                               const std::string& suffix = "");

    /// @brief Extract integer data and append to environment.
    ///
    /// @param value The value to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractInteger(isc::asiolink::ProcessEnvVars& vars,
                               const uint64_t value,
                               const std::string& prefix = "",
                               const std::string& suffix = "");

    /// @brief Extract string data and append to environment.
    ///
    /// @param value The value to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractString(isc::asiolink::ProcessEnvVars& vars,
                              const std::string& value,
                              const std::string& prefix = "",
                              const std::string& suffix = "");

    /// @brief Extract HWAddr data and append to environment.
    ///
    /// @param hwaddr The hwaddr to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractHWAddr(isc::asiolink::ProcessEnvVars& vars,
                              const isc::dhcp::HWAddrPtr& hwaddr,
                              const std::string& prefix = "",
                              const std::string& suffix = "");

    /// @brief Extract DUID data and append to environment.
    ///
    /// @param duid The duid to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractDUID(isc::asiolink::ProcessEnvVars& vars,
                            const isc::dhcp::DuidPtr duid,
                            const std::string& prefix = "",
                            const std::string& suffix = "");

    /// @brief Extract Option data and append to environment.
    ///
    /// @param option The option to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractOption(isc::asiolink::ProcessEnvVars& vars,
                              const isc::dhcp::OptionPtr option,
                              const std::string& prefix = "",
                              const std::string& suffix = "");

    /// @brief Extract Option SubOption data and append to environment.
    ///
    /// @param option The parent option of the suboption to be exported to
    /// target script environment.
    /// @param code The code of the suboption.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractSubOption(isc::asiolink::ProcessEnvVars& vars,
                                 const isc::dhcp::OptionPtr option,
                                 uint16_t code,
                                 const std::string& prefix = "",
                                 const std::string& suffix = "");

    /// @brief Extract Option6IA data and append to environment.
    ///
    /// @param option6IA The option6IA to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractOptionIA(isc::asiolink::ProcessEnvVars& vars,
                                const isc::dhcp::Option6IAPtr option6IA,
                                const std::string& prefix = "",
                                const std::string& suffix = "");

    /// @brief Extract Subnet4 data and append to environment.
    ///
    /// @param subnet4 The subnet4 to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractSubnet4(isc::asiolink::ProcessEnvVars& vars,
                               const isc::dhcp::Subnet4Ptr subnet4,
                               const std::string& prefix = "",
                               const std::string& suffix = "");

    /// @brief Extract Subnet6 data and append to environment.
    ///
    /// @param subnet6 The subnet6 to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractSubnet6(isc::asiolink::ProcessEnvVars& vars,
                               const isc::dhcp::Subnet6Ptr subnet6,
                               const std::string& prefix = "",
                               const std::string& suffix = "");

    /// @brief Extract Lease4 data and append to environment.
    ///
    /// @param lease4 The lease4 to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractLease4(isc::asiolink::ProcessEnvVars& vars,
                              const isc::dhcp::Lease4Ptr& lease4,
                              const std::string& prefix = "",
                              const std::string& suffix = "");

    /// @brief Extract Lease6 data and append to environment.
    ///
    /// @param lease6 The lease6 to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractLease6(isc::asiolink::ProcessEnvVars& vars,
                              const isc::dhcp::Lease6Ptr& lease6,
                              const std::string& prefix = "",
                              const std::string& suffix = "");

    /// @brief Extract Lease4Collection data and append to environment.
    ///
    /// @param leases4 The leases4 to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractLeases4(isc::asiolink::ProcessEnvVars& vars,
                               const isc::dhcp::Lease4CollectionPtr& leases4,
                               const std::string& prefix = "",
                               const std::string& suffix = "");

    /// @brief Extract Lease6Collection data and append to environment.
    ///
    /// @param leases6 The leases6 to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractLeases6(isc::asiolink::ProcessEnvVars& vars,
                               const isc::dhcp::Lease6CollectionPtr& leases6,
                               const std::string& prefix = "",
                               const std::string& suffix = "");

    /// @brief Extract Pkt4 data and append to environment.
    ///
    /// @param pkt4 The pkt4 to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractPkt4(isc::asiolink::ProcessEnvVars& vars,
                            const isc::dhcp::Pkt4Ptr& pkt4,
                            const std::string& prefix = "",
                            const std::string& suffix = "");

    /// @brief Extract Pkt6 data and append to environment.
    ///
    /// @param pkt6 The pkt6 to be exported to target script environment.
    /// @param prefix The prefix for the name of the environment variable.
    /// @param suffix The suffix for the name of the environment variable.
    static void extractPkt6(isc::asiolink::ProcessEnvVars& vars,
                            const isc::dhcp::Pkt6Ptr& pkt6,
                            const std::string& prefix = "",
                            const std::string& suffix = "");

    /// @brief Run Script with specified arguments and environment parameters.
    ///
    /// @param args The arguments for the target script.
    /// @param vars The environment variables made available for the target
    /// script.
    void runScript(const isc::asiolink::ProcessArgs& args,
                   const isc::asiolink::ProcessEnvVars& vars);

    /// @brief Set name of the target script.
    ///
    /// @param name The name of the target script.
    void setName(const std::string& name) {
        name_ = name;
    }

    /// @brief Get name of the target script.
    ///
    /// @return The name of the target script.
    std::string getName() const {
        return (name_);
    }

    /// @brief Set the synchronous call mode for the target script.
    ///
    /// @param name The synchronous call mode for the target script.
    void setSync(const bool sync) {
        sync_ = sync;
    }

    /// @brief Get the synchronous call mode for the target script.
    ///
    /// @return The synchronous call mode for the target script.
    bool getSync() const {
        return (sync_);
    }

    /// @brief This function parses and applies configuration parameters.
    void configure(isc::hooks::LibraryHandle& handle);

private:
    /// @brief Script name.
    std::string name_;

    /// @brief Sync flag
    ///
    /// When set to true, the call to @ref runScript blocks until the script
    /// exits, otherwise the call will return immediately after the script is
    /// started.
    bool sync_;

    /// @brief The IOService object, used for all ASIO operations.
    static isc::asiolink::IOServicePtr io_service_;
};

/// @brief The type of shared pointers to Run Script implementations.
typedef boost::shared_ptr<RunScriptImpl> RunScriptImplPtr;

} // namespace run_script
} // namespace isc
#endif