Kea  1.9.9-git
dhcp6/main.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2020 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 <process/daemon.h>
20 
21 #include <boost/lexical_cast.hpp>
22 
23 #include <iostream>
24 
25 using namespace isc::data;
26 using namespace isc::dhcp;
27 using namespace isc::process;
28 using namespace std;
29 
38 
39 namespace {
40 
41 const char* const DHCP6_NAME = "kea-dhcp6";
42 
46 void
47 usage() {
48  cerr << "Kea DHCPv6 server, "
49  << "version " << VERSION
50  << " (" << PACKAGE_VERSION_TYPE << ")"
51  << endl;
52  cerr << endl;
53  cerr << "Usage: " << DHCP6_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 
69 int
70 main(int argc, char* argv[]) {
71  int ch;
72  // The default. Any other values are useful for testing only.
73  int server_port_number = DHCP6_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 << Dhcpv6Srv::getVersion(false) << endl;
90  return (EXIT_SUCCESS);
91 
92  case 'V':
93  cout << Dhcpv6Srv::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 DHCPv6 server
155  CfgMgr::instance().setFamily(AF_INET6);
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(DHCP6_ROOT_LOGGER_NAME);
163  Daemon::loggerInit(DHCP6_ROOT_LOGGER_NAME, verbose_mode);
164 
165  // Check the syntax first.
166  Parser6Context parser;
167  ConstElementPtr json;
168  json = parser.parseFile(config_file, Parser6Context::PARSER_DHCP6);
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 dhcp6 = json->get("Dhcp6");
179  if (!dhcp6) {
180  cerr << "Missing mandatory Dhcp6 element" << endl;
181  return (EXIT_FAILURE);
182  }
183  ControlledDhcpv6Srv server(0);
184  ConstElementPtr answer;
185 
186  // Now we pass the Dhcp6 configuration to the server, but
187  // tell it to check the configuration only (check_only = true)
188  answer = configureDhcp6Server(server, dhcp6, 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(DHCP6_ROOT_LOGGER_NAME);
210 
211  // Initialize logging. If verbose, we'll use maximum verbosity.
212  Daemon::loggerInit(DHCP6_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  ControlledDhcpv6Srv 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(DHCP6_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(dhcp6_logger, DHCP6_INIT_FAIL).arg(ex.what());
248  } catch (...) {
249  // The exception thrown during the initialization could
250  // originate from logger subsystem. Therefore LOG_ERROR() may
251  // 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(dhcp6_logger, DHCP6_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 << DHCP6_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(DHCP6_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 << DHCP6_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 {
290  LOG_FATAL(dhcp6_logger, DHCP6_SERVER_FAILED).arg(ex.what());
291  } catch (...) {
292  // Already logged so ignore
293  }
294  ret = EXIT_FAILURE;
295  }
296 
297  return (ret);
298 }
#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:512
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:70
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:21
static void setProcName(const std::string &proc_name)
Sets the process name.
Definition: daemon.cc:135
const isc::log::MessageID DHCP6_STARTING
void setConfigFile(const std::string &config_file)
Sets the configuration file name.
Definition: daemon.cc:110
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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
#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:203
isc::log::Logger dhcp6_logger(DHCP6_APP_LOGGER_NAME)
Base logger for DHCPv6 server.
Definition: dhcp6_log.h:87