Kea 2.2.0
dhcp4/main.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2021 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8#include <kea_version.h>
9
12#include <dhcp4/dhcp4_log.h>
15#include <dhcpsrv/cfgmgr.h>
17#include <log/logger_support.h>
18#include <log/logger_manager.h>
19#include <process/daemon.h>
20
21#include <boost/lexical_cast.hpp>
22
23#include <iostream>
24
25using namespace isc::data;
26using namespace isc::dhcp;
27using namespace isc::process;
28using namespace std;
29
38
39namespace {
40
41const char* const DHCP4_NAME = "kea-dhcp4";
42
46void
47usage() {
48 cerr << "Kea DHCPv4 server, "
49 << "version " << VERSION
50 << " (" << PACKAGE_VERSION_TYPE << ")"
51 << endl;
52 cerr << endl;
53 cerr << "Usage: " << DHCP4_NAME
54 << " -[v|V|W] [-d] [-{c|t} cfgfile] [-p number] [-P number]" << endl;
55 cerr << " -v: print version number and exit" << endl;
56 cerr << " -V: print extended version and exit" << endl;
57 cerr << " -W: display the configuration report and exit" << endl;
58 cerr << " -d: debug mode with extra verbosity (former -v)" << endl;
59 cerr << " -c file: specify configuration file" << endl;
60 cerr << " -t file: check the configuration file syntax and exit" << endl;
61 cerr << " -p number: specify non-standard server port number 1-65535 "
62 << "(useful for testing only)" << endl;
63 cerr << " -P number: specify non-standard client port number 1-65535 "
64 << "(useful for testing only)" << endl;
65 exit(EXIT_FAILURE);
66}
67} // namespace
68
69int
70main(int argc, char* argv[]) {
71 int ch;
72 // The default. Any other values are useful for testing only.
73 int server_port_number = DHCP4_SERVER_PORT;
74 // Not zero values are useful for testing only.
75 int client_port_number = 0;
76 bool verbose_mode = false; // Should server be verbose?
77 bool check_mode = false; // Check syntax
78
79 // The standard config file
80 std::string config_file("");
81
82 while ((ch = getopt(argc, argv, "dvVWc:p:P:t:")) != -1) {
83 switch (ch) {
84 case 'd':
85 verbose_mode = true;
86 break;
87
88 case 'v':
89 cout << Dhcpv4Srv::getVersion(false) << endl;
90 return (EXIT_SUCCESS);
91
92 case 'V':
93 cout << Dhcpv4Srv::getVersion(true) << endl;
94 return (EXIT_SUCCESS);
95
96 case 'W':
97 cout << isc::detail::getConfigReport() << endl;
98 return (EXIT_SUCCESS);
99
100 case 't':
101 check_mode = true;
102 // falls through
103
104 case 'c': // config file
105 config_file = optarg;
106 break;
107
108 case 'p': // server port number
109 try {
110 server_port_number = boost::lexical_cast<int>(optarg);
111 } catch (const boost::bad_lexical_cast &) {
112 cerr << "Failed to parse server port number: [" << optarg
113 << "], 1-65535 allowed." << endl;
114 usage();
115 }
116 if (server_port_number <= 0 || server_port_number > 65535) {
117 cerr << "Failed to parse server port number: [" << optarg
118 << "], 1-65535 allowed." << endl;
119 usage();
120 }
121 break;
122
123 case 'P': // client port number
124 try {
125 client_port_number = boost::lexical_cast<int>(optarg);
126 } catch (const boost::bad_lexical_cast &) {
127 cerr << "Failed to parse client port number: [" << optarg
128 << "], 1-65535 allowed." << endl;
129 usage();
130 }
131 if (client_port_number <= 0 || client_port_number > 65535) {
132 cerr << "Failed to parse client port number: [" << optarg
133 << "], 1-65535 allowed." << endl;
134 usage();
135 }
136 break;
137
138 default:
139 usage();
140 }
141 }
142
143 // Check for extraneous parameters.
144 if (argc > optind) {
145 usage();
146 }
147
148 // Configuration file is required.
149 if (config_file.empty()) {
150 cerr << "Configuration file not specified." << endl;
151 usage();
152 }
153
154 // This is the DHCPv4 server
155 CfgMgr::instance().setFamily(AF_INET);
156
157 if (check_mode) {
158 try {
159 // We need to initialize logging, in case any error messages are to be printed.
160 // This is just a test, so we don't care about lockfile.
161 setenv("KEA_LOCKFILE_DIR", "none", 0);
162 Daemon::setDefaultLoggerName(DHCP4_ROOT_LOGGER_NAME);
163 Daemon::loggerInit(DHCP4_ROOT_LOGGER_NAME, verbose_mode);
164
165 // Check the syntax first.
166 Parser4Context parser;
167 ConstElementPtr json;
168 json = parser.parseFile(config_file, Parser4Context::PARSER_DHCP4);
169 if (!json) {
170 cerr << "No configuration found" << endl;
171 return (EXIT_FAILURE);
172 }
173 if (verbose_mode) {
174 cerr << "Syntax check OK" << endl;
175 }
176
177 // Check the logic next.
178 ConstElementPtr dhcp4 = json->get("Dhcp4");
179 if (!dhcp4) {
180 cerr << "Missing mandatory Dhcp4 element" << endl;
181 return (EXIT_FAILURE);
182 }
183 ControlledDhcpv4Srv server(0);
184 ConstElementPtr answer;
185
186 // Now we pass the Dhcp4 configuration to the server, but
187 // tell it to check the configuration only (check_only = true)
188 answer = configureDhcp4Server(server, dhcp4, true);
189
190 int status_code = 0;
191 answer = isc::config::parseAnswer(status_code, answer);
192 if (status_code == 0) {
193 return (EXIT_SUCCESS);
194 } else {
195 cerr << "Error encountered: " << answer->stringValue() << endl;
196 return (EXIT_FAILURE);
197 }
198 } catch (const std::exception& ex) {
199 cerr << "Syntax check failed with: " << ex.what() << endl;
200 }
201 return (EXIT_FAILURE);
202 }
203
204 int ret = EXIT_SUCCESS;
205 try {
206 // It is important that we set a default logger name because this name
207 // will be used when the user doesn't provide the logging configuration
208 // in the Kea configuration file.
209 Daemon::setDefaultLoggerName(DHCP4_ROOT_LOGGER_NAME);
210
211 // Initialize logging. If verbose, we'll use maximum verbosity.
212 Daemon::loggerInit(DHCP4_ROOT_LOGGER_NAME, verbose_mode);
214 .arg(getpid())
215 .arg(server_port_number)
216 .arg(client_port_number)
217 .arg(verbose_mode ? "yes" : "no");
218
220 .arg(VERSION)
221 .arg(PACKAGE_VERSION_TYPE);
222
223 if (string(PACKAGE_VERSION_TYPE) == "development") {
225 }
226
227 // Create the server instance.
228 ControlledDhcpv4Srv server(server_port_number, client_port_number);
229
230 // Remember verbose-mode
231 server.setVerbose(verbose_mode);
232
233 // Create our PID file.
234 server.setProcName(DHCP4_NAME);
235 server.setConfigFile(config_file);
236 server.createPIDFile();
237
238 try {
239 // Initialize the server.
240 server.init(config_file);
241 } catch (const std::exception& ex) {
242
243 try {
244 // Let's log out what went wrong.
245 isc::log::LoggerManager log_manager;
246 log_manager.process();
247 LOG_ERROR(dhcp4_logger, DHCP4_INIT_FAIL).arg(ex.what());
248 } catch (...) {
249 // The exception thrown during the initialization could
250 // originate from logger subsystem. Therefore LOG_ERROR()
251 // may fail as well.
252 cerr << "Failed to initialize server: " << ex.what() << endl;
253 }
254
255 return (EXIT_FAILURE);
256 }
257
258 // Tell the admin we are ready to process packets
259 LOG_INFO(dhcp4_logger, DHCP4_STARTED).arg(VERSION);
260
261 // And run the main loop of the server.
262 ret = server.run();
263
265
266 } catch (const isc::process::DaemonPIDExists& ex) {
267 // First, we print the error on stderr (that should always work)
268 cerr << DHCP4_NAME << " already running? " << ex.what()
269 << endl;
270
271 // Let's also try to log it using logging system, but we're not
272 // sure if it's usable (the exception may have been thrown from
273 // the logger subsystem)
274 try {
276 .arg(DHCP4_NAME).arg(ex.what());
277 } catch (...) {
278 // Already logged so ignore
279 }
280 ret = EXIT_FAILURE;
281 } catch (const std::exception& ex) {
282 // First, we print the error on stderr (that should always work)
283 cerr << DHCP4_NAME << ": Fatal error during start up: " << ex.what()
284 << endl;
285
286 // Let's also try to log it using logging system, but we're not
287 // sure if it's usable (the exception may have been thrown from
288 // the logger subsystem)
289 try {
291 } catch (...) {
292 // Already logged so ignore
293 }
294 ret = EXIT_FAILURE;
295 } catch (...) {
296 cerr << DHCP4_NAME << ": Fatal error during start up"
297 << endl;
298 ret = EXIT_FAILURE;
299 }
300
301 return (ret);
302}
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Controlled version of the DHCPv4 server.
void init(const std::string &config_file)
Initializes the server.
int run()
Main server processing loop.
Definition: dhcp4_srv.cc:1022
Evaluation context, an interface to the expression evaluation.
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
void process(T start, T finish)
Process Specifications.
Exception thrown when the PID file points to a live PID.
Definition: daemon.h:25
static void setVerbose(const bool verbose)
Sets or clears verbose mode.
Definition: daemon.cc:79
static void setProcName(const std::string &proc_name)
Sets the process name.
Definition: daemon.cc:135
void createPIDFile(int pid=0)
Creates the PID file.
Definition: daemon.cc:203
void setConfigFile(const std::string &config_file)
Sets the configuration file name.
Definition: daemon.cc:110
int main(int argc, char *argv[])
Definition: dhcp4/main.cc:70
Contains declarations for loggers used by the DHCPv4 server component.
void usage()
Print Usage.
Logging initialization functions.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition: macros.h:26
#define LOG_FATAL(LOGGER, MESSAGE)
Macro to conveniently test fatal output and log it.
Definition: macros.h:38
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
std::string getConfigReport()
Definition: cfgrpt.cc:20
const char * DHCP4_ROOT_LOGGER_NAME
Defines the name of the root level (default) logger.
Definition: dhcp4_log.cc:26
const isc::log::MessageID DHCP4_ALREADY_RUNNING
const isc::log::MessageID DHCP4_STARTED
isc::data::ConstElementPtr configureDhcp4Server(Dhcpv4Srv &server, isc::data::ConstElementPtr config_set, bool check_only)
Configure DHCPv4 server (Dhcpv4Srv) with a set of configuration values.
const isc::log::MessageID DHCP4_START_INFO
const isc::log::MessageID DHCP4_SERVER_FAILED
const isc::log::MessageID DHCP4_INIT_FAIL
const isc::log::MessageID DHCP4_SHUTDOWN
const isc::log::MessageID DHCP4_STARTING
isc::log::Logger dhcp4_logger(DHCP4_APP_LOGGER_NAME)
Base logger for DHCPv4 server.
Definition: dhcp4_log.h:90
const isc::log::MessageID DHCP4_DEVELOPMENT_VERSION
const int DBG_DHCP4_START
Debug level used to log information during server startup.
Definition: dhcp4_log.h:24