main.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. static const char* DNSPORT = "5300";
  50. IOService io_service;
  51. static Recursor *recursor;
  52. ConstElementPtr
  53. my_config_handler(ConstElementPtr new_config) {
  54. return (recursor->updateConfig(new_config));
  55. }
  56. ConstElementPtr
  57. my_command_handler(const string& command, ConstElementPtr args) {
  58. ConstElementPtr answer = createAnswer();
  59. if (command == "print_message") {
  60. cout << args << endl;
  61. /* let's add that message to our answer as well */
  62. answer = createAnswer(0, args);
  63. } else if (command == "shutdown") {
  64. io_service.stop();
  65. }
  66. return (answer);
  67. }
  68. void
  69. usage() {
  70. cerr << "Usage: b10-recurse -f nameserver [-a address] [-p port] [-u user]"
  71. "[-4|-6] [-v]" << endl;
  72. cerr << "\t-f: specify the nameserver to which queries should be forwarded"
  73. << endl;
  74. cerr << "\t-a: specify the address to listen on (default: all)" << endl;
  75. cerr << "\t-p: specify the port to listen on (default: " << DNSPORT << ")"
  76. << endl;
  77. cerr << "\t-4: listen on all IPv4 addresses (incompatible with -a)" << endl;
  78. cerr << "\t-6: listen on all IPv6 addresses (incompatible with -a)" << endl;
  79. cerr << "\t-u: change process UID to the specified user" << endl;
  80. cerr << "\t-v: verbose output" << endl;
  81. exit(1);
  82. }
  83. } // end of anonymous namespace
  84. int
  85. main(int argc, char* argv[]) {
  86. int ch;
  87. const char* port = DNSPORT;
  88. const char* address = NULL;
  89. const char* forward = NULL;
  90. const char* uid = NULL;
  91. bool use_ipv4 = true, use_ipv6 = true;
  92. while ((ch = getopt(argc, argv, "46a:f:p:u:v")) != -1) {
  93. switch (ch) {
  94. case '4':
  95. // Note that -4 means "ipv4 only", we need to set "use_ipv6" here,
  96. // not "use_ipv4". We could use something like "ipv4_only", but
  97. // we found the negatively named variable could confuse the code
  98. // logic.
  99. use_ipv6 = false;
  100. break;
  101. case '6':
  102. // The same note as -4 applies.
  103. use_ipv4 = false;
  104. break;
  105. case 'a':
  106. address = optarg;
  107. break;
  108. case 'f':
  109. forward = optarg;
  110. break;
  111. case 'p':
  112. port = optarg;
  113. break;
  114. case 'u':
  115. uid = optarg;
  116. break;
  117. case 'v':
  118. verbose_mode = true;
  119. break;
  120. case '?':
  121. default:
  122. usage();
  123. }
  124. }
  125. if (argc - optind > 0) {
  126. usage();
  127. }
  128. if (!use_ipv4 && !use_ipv6) {
  129. cerr << "[b10-auth] Error: Cannot specify both -4 and -6 "
  130. << "at the same time" << endl;
  131. usage();
  132. }
  133. if ((!use_ipv4 || !use_ipv6) && address != NULL) {
  134. cerr << "[b10-auth] Error: Cannot specify -4 or -6 "
  135. << "at the same time as -a" << endl;
  136. usage();
  137. }
  138. if (forward == NULL) {
  139. cerr << "[b10-recurse] No forward name server specified" << endl;
  140. usage();
  141. }
  142. int ret = 0;
  143. // XXX: we should eventually pass io_service here.
  144. Session* cc_session = NULL;
  145. ModuleCCSession* config_session = NULL;
  146. try {
  147. string specfile;
  148. if (getenv("B10_FROM_BUILD")) {
  149. specfile = string(getenv("B10_FROM_BUILD")) +
  150. "/src/bin/recurse/recurse.spec";
  151. } else {
  152. specfile = string(RECURSE_SPECFILE_LOCATION);
  153. }
  154. recursor = new Recursor(*forward);
  155. recursor->setVerbose(verbose_mode);
  156. cout << "[b10-recurse] Server created." << endl;
  157. SimpleCallback* checkin = recursor->getCheckinProvider();
  158. DNSLookup* lookup = recursor->getDNSLookupProvider();
  159. DNSAnswer* answer = recursor->getDNSAnswerProvider();
  160. DNSService* dns_service;
  161. if (address != NULL) {
  162. // XXX: we can only specify at most one explicit address.
  163. // This also means the server cannot run in the dual address
  164. // family mode if explicit addresses need to be specified.
  165. // We don't bother to fix this problem, however. The -a option
  166. // is a short term workaround until we support dynamic listening
  167. // port allocation.
  168. dns_service = new DNSService(io_service, *port, *address,
  169. checkin, lookup, answer);
  170. } else {
  171. dns_service = new DNSService(io_service, *port, use_ipv4, use_ipv6,
  172. checkin, lookup, answer);
  173. }
  174. recursor->setDNSService(*dns_service);
  175. cout << "[b10-recurse] IOService created." << endl;
  176. cc_session = new Session(io_service.get_io_service());
  177. cout << "[b10-recurse] Configuration session channel created." << endl;
  178. config_session = new ModuleCCSession(specfile, *cc_session,
  179. my_config_handler,
  180. my_command_handler);
  181. cout << "[b10-recurse] Configuration channel established." << endl;
  182. if (uid != NULL) {
  183. changeUser(uid);
  184. }
  185. recursor->setConfigSession(config_session);
  186. recursor->updateConfig(ElementPtr());
  187. cout << "[b10-recurse] Server started." << endl;
  188. io_service.run();
  189. } catch (const std::exception& ex) {
  190. cerr << "[b10-recurse] Server failed: " << ex.what() << endl;
  191. ret = 1;
  192. }
  193. delete config_session;
  194. delete cc_session;
  195. delete recursor;
  196. return (ret);
  197. }