main.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (C) 2011-2012 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 <iostream>
  16. #include <boost/lexical_cast.hpp>
  17. #include <dhcp6/ctrl_dhcp6_srv.h>
  18. #include <dhcp6/dhcp6_log.h>
  19. #include <log/logger_support.h>
  20. using namespace isc::dhcp;
  21. using namespace std;
  22. /// This file contains entry point (main() function) for standard DHCPv6 server
  23. /// component for BIND10 framework. It parses command-line arguments and
  24. /// instantiates ControlledDhcpv6Srv class that is responsible for establishing
  25. /// connection with msgq (receiving commands and configuration) and also
  26. /// creating Dhcpv6 server object as well.
  27. ///
  28. /// For detailed explanation or relations between main(), ControlledDhcpv6Srv,
  29. /// Dhcpv6Srv and other classes, see \ref dhcpv6Session.
  30. namespace {
  31. const char* const DHCP6_NAME = "b10-dhcp6";
  32. void
  33. usage() {
  34. cerr << "Usage: " << DHCP6_NAME << " [-v] [-s] [-p number]" << endl;
  35. cerr << " -v: verbose output" << endl;
  36. cerr << " -s: stand-alone mode (don't connect to BIND10)" << endl;
  37. cerr << " -p number: specify non-standard port number 1-65535 "
  38. << "(useful for testing only)" << endl;
  39. exit(EXIT_FAILURE);
  40. }
  41. } // end of anonymous namespace
  42. int
  43. main(int argc, char* argv[]) {
  44. int ch;
  45. int port_number = DHCP6_SERVER_PORT; // The default. Any other values are
  46. // useful for testing only.
  47. bool stand_alone = false; // Should be connect to BIND10 msgq?
  48. bool verbose_mode = false; // Should server be verbose?
  49. while ((ch = getopt(argc, argv, "vsp:")) != -1) {
  50. switch (ch) {
  51. case 'v':
  52. verbose_mode = true;
  53. break;
  54. case 's':
  55. stand_alone = 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. default:
  72. usage();
  73. }
  74. }
  75. // Check for extraneous parameters.
  76. if (argc > optind) {
  77. usage();
  78. }
  79. // Initialize logging. If verbose, we'll use maximum verbosity.
  80. isc::log::initLogger(DHCP6_NAME,
  81. (verbose_mode ? isc::log::DEBUG : isc::log::INFO),
  82. isc::log::MAX_DEBUG_LEVEL, NULL);
  83. LOG_INFO(dhcp6_logger, DHCP6_STARTING);
  84. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_START_INFO)
  85. .arg(getpid()).arg(port_number).arg(verbose_mode ? "yes" : "no")
  86. .arg(stand_alone ? "yes" : "no" );
  87. int ret = EXIT_SUCCESS;
  88. try {
  89. ControlledDhcpv6Srv server(port_number);
  90. if (!stand_alone) {
  91. try {
  92. server.establishSession();
  93. } catch (const std::exception& ex) {
  94. LOG_ERROR(dhcp6_logger, DHCP6_SESSION_FAIL).arg(ex.what());
  95. // Let's continue. It is useful to have the ability to run
  96. // DHCP server in stand-alone mode, e.g. for testing
  97. }
  98. } else {
  99. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_STANDALONE);
  100. }
  101. server.run();
  102. LOG_INFO(dhcp6_logger, DHCP6_SHUTDOWN);
  103. } catch (const std::exception& ex) {
  104. LOG_FATAL(dhcp6_logger, DHCP6_SERVER_FAILED).arg(ex.what());
  105. ret = EXIT_FAILURE;
  106. }
  107. return (ret);
  108. }