main.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (C) 2011-2012, 2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <dhcp4/ctrl_dhcp4_srv.h>
  16. #include <dhcp4/dhcp4_log.h>
  17. #include <log/logger_support.h>
  18. #include <log/logger_manager.h>
  19. #include <boost/lexical_cast.hpp>
  20. #include <iostream>
  21. using namespace isc::dhcp;
  22. using namespace std;
  23. /// This file contains entry point (main() function) for standard DHCPv4 server
  24. /// component for BIND10 framework. It parses command-line arguments and
  25. /// instantiates ControlledDhcpv4Srv class that is responsible for establishing
  26. /// connection with msgq (receiving commands and configuration) and also
  27. /// creating Dhcpv4 server object as well.
  28. ///
  29. /// For detailed explanation or relations between main(), ControlledDhcpv4Srv,
  30. /// Dhcpv4Srv and other classes, see \ref dhcpv4Session.
  31. namespace {
  32. const char* const DHCP4_NAME = "kea-dhcp4";
  33. const char* const DHCP4_LOGGER_NAME = "kea";
  34. void
  35. usage() {
  36. cerr << "Usage: " << DHCP4_NAME << " [-v] [-p number] [-c file]" << endl;
  37. cerr << " -v: verbose output" << endl;
  38. cerr << " -p number: specify non-standard port number 1-65535 "
  39. << "(useful for testing only)" << endl;
  40. cerr << " -c file: specify configuration file" << endl;
  41. exit(EXIT_FAILURE);
  42. }
  43. } // end of anonymous namespace
  44. int
  45. main(int argc, char* argv[]) {
  46. int ch;
  47. int port_number = DHCP4_SERVER_PORT; // The default. any other values are
  48. // useful for testing only.
  49. bool verbose_mode = false; // Should server be verbose?
  50. // The standard config file
  51. std::string config_file("");
  52. while ((ch = getopt(argc, argv, "vp:c:")) != -1) {
  53. switch (ch) {
  54. case 'v':
  55. verbose_mode = true;
  56. break;
  57. case 'p':
  58. try {
  59. port_number = boost::lexical_cast<int>(optarg);
  60. } catch (const boost::bad_lexical_cast &) {
  61. cerr << "Failed to parse port number: [" << optarg
  62. << "], 1-65535 allowed." << endl;
  63. usage();
  64. }
  65. if (port_number <= 0 || port_number > 65535) {
  66. cerr << "Failed to parse port number: [" << optarg
  67. << "], 1-65535 allowed." << endl;
  68. usage();
  69. }
  70. break;
  71. case 'c': // config file
  72. config_file = optarg;
  73. break;
  74. default:
  75. usage();
  76. }
  77. }
  78. // Check for extraneous parameters.
  79. if (argc > optind) {
  80. usage();
  81. }
  82. int ret = EXIT_SUCCESS;
  83. try {
  84. // Initialize logging. If verbose, we'll use maximum verbosity.
  85. // If standalone is enabled, do not buffer initial log messages
  86. Daemon::loggerInit(DHCP4_LOGGER_NAME, verbose_mode);
  87. LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_START_INFO)
  88. .arg(getpid()).arg(port_number).arg(verbose_mode ? "yes" : "no");
  89. LOG_INFO(dhcp4_logger, DHCP4_STARTING);
  90. // Create the server instance.
  91. ControlledDhcpv4Srv server(port_number);
  92. try {
  93. // Initialize the server.
  94. server.init(config_file);
  95. } catch (const std::exception& ex) {
  96. LOG_ERROR(dhcp4_logger, DHCP4_INIT_FAIL).arg(ex.what());
  97. // We should not continue if were told to configure (either read
  98. // config file or establish Bundy control session).
  99. isc::log::LoggerManager log_manager;
  100. log_manager.process();
  101. cerr << "Failed to initialize server: " << ex.what() << endl;
  102. return (EXIT_FAILURE);
  103. }
  104. // And run the main loop of the server.
  105. server.run();
  106. LOG_INFO(dhcp4_logger, DHCP4_SHUTDOWN);
  107. } catch (const std::exception& ex) {
  108. LOG_FATAL(dhcp4_logger, DHCP4_SERVER_FAILED).arg(ex.what());
  109. ret = EXIT_FAILURE;
  110. }
  111. return (ret);
  112. }