main.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (C) 2009 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. // $Id$
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <sys/select.h>
  18. #include <netdb.h>
  19. #include <netinet/in.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <cassert>
  23. #include <iostream>
  24. #include <boost/foreach.hpp>
  25. #include <asiolink/asiolink.h>
  26. #include <exceptions/exceptions.h>
  27. #include <dns/buffer.h>
  28. #include <dns/message.h>
  29. #include <dns/messagerenderer.h>
  30. #include <cc/session.h>
  31. #include <cc/data.h>
  32. #include <config/ccsession.h>
  33. #include <xfr/xfrout_client.h>
  34. #include <auth/change_user.h>
  35. #include <auth/common.h>
  36. #include <recurse/spec_config.h>
  37. #include <recurse/recursor.h>
  38. using namespace std;
  39. using namespace isc::data;
  40. using namespace isc::cc;
  41. using namespace isc::config;
  42. using namespace isc::dns;
  43. using namespace isc::xfr;
  44. using namespace asiolink;
  45. namespace {
  46. static bool verbose_mode = false;
  47. // Default port current 5300 for testing purposes
  48. static const string PROGRAM = "Recurse";
  49. IOService io_service;
  50. static Recursor *recursor;
  51. ConstElementPtr
  52. my_config_handler(ConstElementPtr new_config) {
  53. return (recursor->updateConfig(new_config));
  54. }
  55. ConstElementPtr
  56. my_command_handler(const string& command, ConstElementPtr args) {
  57. ConstElementPtr answer = createAnswer();
  58. if (command == "print_message") {
  59. cout << args << endl;
  60. /* let's add that message to our answer as well */
  61. answer = createAnswer(0, args);
  62. } else if (command == "shutdown") {
  63. io_service.stop();
  64. }
  65. return (answer);
  66. }
  67. void
  68. usage() {
  69. cerr << "Usage: b10-recurse [-u user] [-v]" << endl;
  70. cerr << "\t-u: change process UID to the specified user" << endl;
  71. cerr << "\t-v: verbose output" << endl;
  72. exit(1);
  73. }
  74. } // end of anonymous namespace
  75. int
  76. main(int argc, char* argv[]) {
  77. int ch;
  78. const char* uid = NULL;
  79. while ((ch = getopt(argc, argv, "u:v")) != -1) {
  80. switch (ch) {
  81. case 'u':
  82. uid = optarg;
  83. break;
  84. case 'v':
  85. verbose_mode = true;
  86. break;
  87. case '?':
  88. default:
  89. usage();
  90. }
  91. }
  92. if (argc - optind > 0) {
  93. usage();
  94. }
  95. int ret = 0;
  96. // XXX: we should eventually pass io_service here.
  97. Session* cc_session = NULL;
  98. ModuleCCSession* config_session = NULL;
  99. try {
  100. string specfile;
  101. if (getenv("B10_FROM_BUILD")) {
  102. specfile = string(getenv("B10_FROM_BUILD")) +
  103. "/src/bin/recurse/recurse.spec";
  104. } else {
  105. specfile = string(RECURSE_SPECFILE_LOCATION);
  106. }
  107. recursor = new Recursor();
  108. recursor->setVerbose(verbose_mode);
  109. cout << "[b10-recurse] Server created." << endl;
  110. SimpleCallback* checkin = recursor->getCheckinProvider();
  111. DNSLookup* lookup = recursor->getDNSLookupProvider();
  112. DNSAnswer* answer = recursor->getDNSAnswerProvider();
  113. DNSService dns_service(io_service, checkin, lookup, answer);
  114. recursor->setDNSService(dns_service);
  115. cout << "[b10-recurse] IOService created." << endl;
  116. cc_session = new Session(io_service.get_io_service());
  117. cout << "[b10-recurse] Configuration session channel created." << endl;
  118. config_session = new ModuleCCSession(specfile, *cc_session,
  119. my_config_handler,
  120. my_command_handler);
  121. cout << "[b10-recurse] Configuration channel established." << endl;
  122. // FIXME: This does not belong here, but inside Boss
  123. if (uid != NULL) {
  124. changeUser(uid);
  125. }
  126. recursor->setConfigSession(config_session);
  127. recursor->updateConfig(config_session->getFullConfig());
  128. cout << "[b10-recurse] Server started." << endl;
  129. io_service.run();
  130. } catch (const std::exception& ex) {
  131. cerr << "[b10-recurse] Server failed: " << ex.what() << endl;
  132. ret = 1;
  133. }
  134. delete config_session;
  135. delete cc_session;
  136. delete recursor;
  137. return (ret);
  138. }