main.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <log/dummylog.h>
  17. #include <log/logger_support.h>
  18. #include <dhcp6/ctrl_dhcp6_srv.h>
  19. #include <boost/lexical_cast.hpp>
  20. using namespace std;
  21. using namespace isc::dhcp;
  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: b10-dhcp6 [-v]"
  35. << endl;
  36. cerr << "\t-v: verbose output" << endl;
  37. cerr << "\t-s: stand-alone mode (don't connect to BIND10)" << endl;
  38. cerr << "\t-p number: specify non-standard port number 1-65535 "
  39. << "(useful for testing only)" << endl;
  40. exit(EXIT_FAILURE);
  41. }
  42. } // end of anonymous namespace
  43. int
  44. main(int argc, char* argv[]) {
  45. int ch;
  46. int port_number = DHCP6_SERVER_PORT; // The default. Any other values are
  47. // useful for testing only.
  48. bool verbose_mode = false; // Should server be verbose?
  49. bool stand_alone = false; // should be connect to BIND10 msgq?
  50. while ((ch = getopt(argc, argv, "vsp:")) != -1) {
  51. switch (ch) {
  52. case 'v':
  53. verbose_mode = true;
  54. isc::log::denabled = true;
  55. break;
  56. case 's':
  57. stand_alone = true;
  58. break;
  59. case 'p':
  60. try {
  61. port_number = boost::lexical_cast<int>(optarg);
  62. } catch (const boost::bad_lexical_cast &) {
  63. cerr << "Failed to parse port number: [" << optarg
  64. << "], 1-65535 allowed." << endl;
  65. usage();
  66. }
  67. if (port_number <= 0 || port_number > 65535) {
  68. cerr << "Failed to parse port number: [" << optarg
  69. << "], 1-65535 allowed." << endl;
  70. usage();
  71. }
  72. break;
  73. case ':':
  74. default:
  75. usage();
  76. }
  77. }
  78. // Initialize logging. If verbose, we'll use maximum verbosity.
  79. isc::log::initLogger(DHCP6_NAME,
  80. (verbose_mode ? isc::log::DEBUG : isc::log::INFO),
  81. isc::log::MAX_DEBUG_LEVEL, NULL);
  82. cout << "b10-dhcp6: My pid=" << getpid() << ", binding to port "
  83. << port_number << ", verbose " << (verbose_mode?"yes":"no")
  84. << ", stand-alone=" << (stand_alone?"yes":"no") << endl;
  85. if (argc - optind > 0) {
  86. usage();
  87. }
  88. int ret = EXIT_SUCCESS;
  89. try {
  90. cout << "b10-dhcp6: Initiating DHCPv6 server operation." << endl;
  91. /// @todo: pass verbose to the actual server once logging is implemented
  92. ControlledDhcpv6Srv server(port_number);
  93. if (!stand_alone) {
  94. try {
  95. server.establishSession();
  96. } catch (const std::exception& ex) {
  97. cerr << "Failed to establish BIND10 session. "
  98. "Running in stand-alone mode:" << ex.what() << endl;
  99. // Let's continue. It is useful to have the ability to run
  100. // DHCP server in stand-alone mode, e.g. for testing
  101. }
  102. } else {
  103. cout << "Skipping connection to the BIND10 msgq." << endl;
  104. }
  105. server.run();
  106. } catch (const std::exception& ex) {
  107. cerr << "[b10-dhcp6] Server failed: " << ex.what() << endl;
  108. ret = EXIT_FAILURE;
  109. }
  110. return (ret);
  111. }