main.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <exceptions/exceptions.h>
  26. #include <dns/buffer.h>
  27. #include <dns/message.h>
  28. #include <dns/messagerenderer.h>
  29. #include <cc/session.h>
  30. #include <cc/data.h>
  31. #include <config/ccsession.h>
  32. #include <xfr/xfrout_client.h>
  33. #include <auth/spec_config.h>
  34. #include <auth/common.h>
  35. #include <auth/auth_srv.h>
  36. #include <auth/asio_link.h>
  37. using namespace std;
  38. using namespace isc::data;
  39. using namespace isc::cc;
  40. using namespace isc::config;
  41. using namespace isc::dns;
  42. using namespace isc::xfr;
  43. namespace {
  44. bool verbose_mode = false;
  45. const string PROGRAM = "Auth";
  46. const char* DNSPORT = "5300";
  47. /* need global var for config/command handlers.
  48. * todo: turn this around, and put handlers in the authserver
  49. * class itself? */
  50. AuthSrv *auth_server;
  51. asio_link::IOService* io_service;
  52. ElementPtr
  53. my_config_handler(ElementPtr new_config) {
  54. return auth_server->updateConfig(new_config);
  55. }
  56. ElementPtr
  57. my_command_handler(const string& command, const ElementPtr args) {
  58. ElementPtr answer = createAnswer();
  59. if (command == "print_message") {
  60. cout << args << endl;
  61. /* let's add that message to our answer as well */
  62. answer->get("result")->add(args);
  63. } else if (command == "shutdown") {
  64. io_service->stop();
  65. }
  66. return answer;
  67. }
  68. void
  69. usage() {
  70. cerr << "Usage: b10-auth [-a address] [-p port] [-4|-6] [-nv]" << endl;
  71. exit(1);
  72. }
  73. } // end of anonymous namespace
  74. int
  75. main(int argc, char* argv[]) {
  76. int ch;
  77. const char* port = DNSPORT;
  78. const char* address = NULL;
  79. bool use_ipv4 = true, use_ipv6 = true, cache = true;
  80. while ((ch = getopt(argc, argv, "46a:np:v")) != -1) {
  81. switch (ch) {
  82. case '4':
  83. // Note that -4 means "ipv4 only", we need to set "use_ipv6" here,
  84. // not "use_ipv4". We could use something like "ipv4_only", but
  85. // we found the negatively named variable could confuse the code
  86. // logic.
  87. use_ipv6 = false;
  88. break;
  89. case '6':
  90. // The same note as -4 applies.
  91. use_ipv4 = false;
  92. break;
  93. case 'n':
  94. cache = false;
  95. break;
  96. case 'a':
  97. address = optarg;
  98. break;
  99. case 'p':
  100. port = optarg;
  101. break;
  102. case 'v':
  103. verbose_mode = true;
  104. break;
  105. case '?':
  106. default:
  107. usage();
  108. }
  109. }
  110. if (argc - optind > 0) {
  111. usage();
  112. }
  113. if (!use_ipv4 && !use_ipv6) {
  114. cerr << "[b10-auth] Error: -4 and -6 can't coexist" << endl;
  115. usage();
  116. }
  117. if ((!use_ipv4 || !use_ipv6) && address != NULL) {
  118. cerr << "[b10-auth] Error: -4|-6 and -a can't coexist" << endl;
  119. usage();
  120. }
  121. int ret = 0;
  122. // XXX: we should eventually pass io_service here.
  123. Session* cc_session = NULL;
  124. Session* xfrin_session = NULL;
  125. bool xfrin_session_established = false; // XXX (see Trac #287)
  126. ModuleCCSession* config_session = NULL;
  127. XfroutClient xfrout_client(UNIX_SOCKET_FILE);
  128. try {
  129. string specfile;
  130. if (getenv("B10_FROM_BUILD")) {
  131. specfile = string(getenv("B10_FROM_BUILD")) +
  132. "/src/bin/auth/auth.spec";
  133. } else {
  134. specfile = string(AUTH_SPECFILE_LOCATION);
  135. }
  136. auth_server = new AuthSrv(cache, xfrout_client);
  137. auth_server->setVerbose(verbose_mode);
  138. cout << "[b10-auth] Server created." << endl;
  139. if (address != NULL) {
  140. // XXX: we can only specify at most one explicit address.
  141. // This also means the server cannot run in the dual address
  142. // family mode if explicit addresses need to be specified.
  143. // We don't bother to fix this problem, however. The -a option
  144. // is a short term workaround until we support dynamic listening
  145. // port allocation.
  146. io_service = new asio_link::IOService(auth_server, *port,
  147. *address);
  148. } else {
  149. io_service = new asio_link::IOService(auth_server, *port,
  150. use_ipv4, use_ipv6);
  151. }
  152. cout << "[b10-auth] IOService created." << endl;
  153. cc_session = new Session(io_service->get_io_service());
  154. cout << "[b10-auth] Configuration session channel created." << endl;
  155. config_session = new ModuleCCSession(specfile, *cc_session,
  156. my_config_handler,
  157. my_command_handler);
  158. cout << "[b10-auth] Configuration channel established." << endl;
  159. xfrin_session = new Session(io_service->get_io_service());
  160. cout << "[b10-auth] Xfrin session channel created." << endl;
  161. xfrin_session->establish(NULL);
  162. xfrin_session_established = true;
  163. cout << "[b10-auth] Xfrin session channel established." << endl;
  164. // XXX: with the current interface to asio_link we have to create
  165. // auth_server before io_service while Session needs io_service.
  166. // In a next step of refactoring we should make asio_link independent
  167. // from auth_server, and create io_service, auth_server, and
  168. // sessions in that order.
  169. auth_server->setXfrinSession(xfrin_session);
  170. auth_server->setConfigSession(config_session);
  171. auth_server->updateConfig(ElementPtr());
  172. cout << "[b10-auth] Server started." << endl;
  173. io_service->run();
  174. } catch (const std::exception& ex) {
  175. cerr << "[b10-auth] Initialization failed: " << ex.what() << endl;
  176. ret = 1;
  177. }
  178. if (xfrin_session_established) {
  179. xfrin_session->disconnect();
  180. }
  181. delete xfrin_session;
  182. delete config_session;
  183. delete cc_session;
  184. delete io_service;
  185. delete auth_server;
  186. return (ret);
  187. }