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