Kea 1.5.0
dhcp6/main.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2018 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
11#include <dhcp6/dhcp6_log.h>
14#include <dhcpsrv/cfgmgr.h>
15#include <log/logger_support.h>
16#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 {
40const char* const DHCP6_NAME = "kea-dhcp6";
41
42const char* const DHCP6_LOGGER_NAME = "kea-dhcp6";
43
47void
48usage() {
49 cerr << "Kea DHCPv6 server, version " << VERSION << endl;
50 cerr << endl;
51 cerr << "Usage: " << DHCP6_NAME
52 << " -[v|V|W] [-d] [-{c|t} cfgfile] [-p port_number]" << endl;
53 cerr << " -v: print version number and exit." << endl;
54 cerr << " -V: print extended version and exit" << endl;
55 cerr << " -W: display the configuration report and exit" << endl;
56 cerr << " -d: debug mode with extra verbosity (former -v)" << endl;
57 cerr << " -c file: specify configuration file" << endl;
58 cerr << " -t file: check the configuration file syntax and exit" << endl;
59 cerr << " -p number: specify non-standard port number 1-65535 "
60 << "(useful for testing only)" << endl;
61 exit(EXIT_FAILURE);
62}
63} // end of anonymous namespace
64
65int
66main(int argc, char* argv[]) {
67 int ch;
68 int port_number = DHCP6_SERVER_PORT; // The default. Any other values are
69 // useful for testing only.
70 bool verbose_mode = false; // Should server be verbose?
71 bool check_mode = false; // Check syntax
72
73 // The standard config file
74 std::string config_file("");
75
76 while ((ch = getopt(argc, argv, "dvVWc:p:t:")) != -1) {
77 switch (ch) {
78 case 'd':
79 verbose_mode = true;
80 break;
81
82 case 'v':
83 cout << Dhcpv6Srv::getVersion(false) << endl;
84 return (EXIT_SUCCESS);
85
86 case 'V':
87 cout << Dhcpv6Srv::getVersion(true) << endl;
88 return (EXIT_SUCCESS);
89
90 case 'W':
91 cout << isc::detail::getConfigReport() << endl;
92 return (EXIT_SUCCESS);
93
94 case 't':
95 check_mode = true;
96 // falls through
97
98 case 'c': // config file
99 config_file = optarg;
100 break;
101
102 case 'p': // port number
103 try {
104 port_number = boost::lexical_cast<int>(optarg);
105 } catch (const boost::bad_lexical_cast &) {
106 cerr << "Failed to parse port number: [" << optarg
107 << "], 1-65535 allowed." << endl;
108 usage();
109 }
110 if (port_number <= 0 || port_number > 65535) {
111 cerr << "Failed to parse port number: [" << optarg
112 << "], 1-65535 allowed." << endl;
113 usage();
114 }
115 break;
116
117 default:
118 usage();
119 }
120 }
121
122 // Check for extraneous parameters.
123 if (argc > optind) {
124 usage();
125 }
126
127 // Configuration file is required.
128 if (config_file.empty()) {
129 cerr << "Configuration file not specified." << endl;
130 usage();
131 }
132
133 // This is the DHCPv6 server
134 CfgMgr::instance().setFamily(AF_INET6);
135
136 if (check_mode) {
137 try {
138 // We need to initialize logging, in case any error messages are to be printed.
139 // This is just a test, so we don't care about lockfile.
140 setenv("KEA_LOCKFILE_DIR", "none", 0);
143
144 // Check the syntax first.
145 Parser6Context parser;
146 ConstElementPtr json;
147 json = parser.parseFile(config_file, Parser6Context::PARSER_DHCP6);
148 if (!json) {
149 cerr << "No configuration found" << endl;
150 return (EXIT_FAILURE);
151 }
152 if (verbose_mode) {
153 cerr << "Syntax check OK" << endl;
154 }
155
156 // Check the logic next.
157 ConstElementPtr dhcp6 = json->get("Dhcp6");
158 if (!dhcp6) {
159 cerr << "Missing mandatory Dhcp6 element" << endl;
160 return (EXIT_FAILURE);
161 }
162 ControlledDhcpv6Srv server(0);
163 ConstElementPtr answer;
164
165 // Now we pass the Dhcp6 configuration to the server, but
166 // tell it to check the configuration only (check_only = true)
167 answer = configureDhcp6Server(server, dhcp6, true);
168
169 int status_code = 0;
170 answer = isc::config::parseAnswer(status_code, answer);
171 if (status_code == 0) {
172 return (EXIT_SUCCESS);
173 } else {
174 cerr << "Error encountered: " << answer->stringValue() << endl;
175 return (EXIT_FAILURE);
176 }
177
178
179 return (EXIT_SUCCESS);
180 } catch (const std::exception& ex) {
181 cerr << "Syntax check failed with " << ex.what() << endl;
182 }
183 return (EXIT_FAILURE);
184 }
185
186 int ret = EXIT_SUCCESS;
187 try {
188 // It is important that we set a default logger name because this name
189 // will be used when the user doesn't provide the logging configuration
190 // in the Kea configuration file.
191 Daemon::setDefaultLoggerName(DHCP6_LOGGER_NAME);
192
193 // Initialize logging. If verbose, we'll use maximum verbosity.
194 Daemon::loggerInit(DHCP6_LOGGER_NAME, verbose_mode);
195
196 LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_START_INFO)
197 .arg(getpid()).arg(port_number).arg(verbose_mode ? "yes" : "no");
198
199 LOG_INFO(dhcp6_logger, DHCP6_STARTING).arg(VERSION);
200
201 // Create the server instance.
202 ControlledDhcpv6Srv server(port_number);
203
204 // Remember verbose-mode
205 server.setVerbose(verbose_mode);
206
207 // Create our PID file
208 server.setProcName(DHCP6_NAME);
209 server.setConfigFile(config_file);
210 server.createPIDFile();
211
212 try {
213 // Initialize the server, e.g. establish control session
214 // Read a configuration file
215 server.init(config_file);
216
217 } catch (const std::exception& ex) {
218
219 try {
220 // Let's log out what went wrong.
221 isc::log::LoggerManager log_manager;
222 log_manager.process();
223 LOG_ERROR(dhcp6_logger, DHCP6_INIT_FAIL).arg(ex.what());
224 } catch (...) {
225 // The exception thrown during the initialization could
226 // originate from logger subsystem. Therefore LOG_ERROR() may
227 // fail as well.
228 cerr << "Failed to initialize server: " << ex.what() << endl;
229 }
230
231 return (EXIT_FAILURE);
232 }
233
234 // Tell the admin we are ready to process packets
235 LOG_INFO(dhcp6_logger, DHCP6_STARTED).arg(VERSION);
236
237 // And run the main loop of the server.
238 server.run();
239
240 LOG_INFO(dhcp6_logger, DHCP6_SHUTDOWN);
241
242 } catch (const isc::process::DaemonPIDExists& ex) {
243 // First, we print the error on stderr (that should always work)
244 cerr << DHCP6_NAME << " already running? " << ex.what()
245 << endl;
246
247 // Let's also try to log it using logging system, but we're not
248 // sure if it's usable (the exception may have been thrown from
249 // the logger subsystem)
250 try {
251 LOG_FATAL(dhcp6_logger, DHCP6_ALREADY_RUNNING)
252 .arg(DHCP6_NAME).arg(ex.what());
253 } catch (...) {
254 // Already logged so ignore
255 }
256 ret = EXIT_FAILURE;
257 } catch (const std::exception& ex) {
258
259 // First, we print the error on stderr (that should always work)
260 cerr << DHCP6_NAME << "Fatal error during start up: " << ex.what()
261 << endl;
262
263 // Let's also try to log it using logging system, but we're not
264 // sure if it's usable (the exception may have been thrown from
265 // the logger subsystem)
266 try {
267 LOG_FATAL(dhcp6_logger, DHCP6_SERVER_FAILED).arg(ex.what());
268 } catch (...) {
269 // Already logged so ignore
270 }
271 ret = EXIT_FAILURE;
272 }
273
274 return (ret);
275}
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
void setFamily(uint16_t family)
Sets address family (AF_INET or AF_INET6)
Definition: cfgmgr.h:237
static CfgMgr & instance()
returns a single instance of Configuration Manager
Definition: cfgmgr.cc:25
Controlled version of the DHCPv6 server.
void init(const std::string &config_file)
Initializes the server.
static std::string getVersion(bool extended)
returns Kea version on stdout and exit.
Definition: dhcp6_srv.cc:3654
bool run()
Main server processing loop.
Definition: dhcp6_srv.cc:404
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.
@ PARSER_DHCP6
This parser will parse the content as Dhcp6 config wrapped in a map (that's the regular config file)
void process(T start, T finish)
Process Specifications.
Exception thrown when a the PID file points to a live PID.
Definition: daemon.h:21
static void setVerbose(const bool verbose)
Sets or clears verbose mode.
Definition: daemon.cc:79
static void loggerInit(const char *log_name, bool verbose)
Initializes logger.
Definition: daemon.cc:88
static void setDefaultLoggerName(const std::string &logger)
Sets the default logger name.
Definition: daemon.h:216
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: dhcp6/main.cc:66
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_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:23
std::string getConfigReport()
Definition: cfgrpt.cc:20
isc::data::ConstElementPtr configureDhcp6Server(Dhcpv6Srv &server, isc::data::ConstElementPtr config_set, bool check_only)
Configures DHCPv6 server.
const int DBG_DHCP6_START
Debug level used to log information during server startup.
Definition: dhcp6_log.h:21
const char * DHCP6_ROOT_LOGGER_NAME
Defines the name of the root level (default) logger.
Definition: dhcp6_log.cc:26
isc::log::Logger dhcp6_logger(DHCP6_APP_LOGGER_NAME)
Base logger for DHCPv6 server.
Definition: dhcp6_log.h:87