main.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <stdlib.h>
  20. #include <set>
  21. #include <iostream>
  22. #include <boost/foreach.hpp>
  23. #include <dns/buffer.h>
  24. #include <dns/name.h>
  25. #include <dns/rrset.h>
  26. #include <dns/message.h>
  27. #include <cc/session.h>
  28. #include <cc/data.h>
  29. #include <config/ccsession.h>
  30. #include "common.h"
  31. #include "config.h"
  32. #include "auth_srv.h"
  33. #include <boost/foreach.hpp>
  34. using namespace std;
  35. const string PROGRAM = "Auth";
  36. const int DNSPORT = 5300;
  37. /* need global var for config/command handlers.
  38. * todo: turn this around, and put handlers in the authserver
  39. * class itself? */
  40. AuthSrv auth = AuthSrv(DNSPORT);
  41. static void
  42. usage() {
  43. cerr << "Usage: b10-auth [-p port]" << endl;
  44. exit(1);
  45. }
  46. isc::data::ElementPtr
  47. my_config_handler(isc::data::ElementPtr config)
  48. {
  49. return auth.updateConfig(config);
  50. }
  51. isc::data::ElementPtr
  52. my_command_handler(isc::data::ElementPtr command) {
  53. isc::data::ElementPtr answer = isc::config::createAnswer(0);
  54. cout << "[XX] Handle command: " << endl << command->str() << endl;
  55. if (command->get(0)->stringValue() == "print_message")
  56. {
  57. cout << command->get(1)->get("message") << endl;
  58. /* let's add that message to our answer as well */
  59. answer->get("result")->add(command->get(1));
  60. }
  61. return answer;
  62. }
  63. int
  64. main(int argc, char* argv[]) {
  65. int ch;
  66. int port = DNSPORT;
  67. while ((ch = getopt(argc, argv, "p:")) != -1) {
  68. switch (ch) {
  69. case 'p':
  70. port = atoi(optarg);
  71. break;
  72. case '?':
  73. default:
  74. usage();
  75. }
  76. }
  77. if (argc - optind > 0)
  78. usage();
  79. // initialize command channel
  80. try {
  81. std::string specfile;
  82. if (getenv("B10_FROM_SOURCE")) {
  83. specfile = std::string(getenv("B10_FROM_SOURCE")) + "/src/bin/auth/auth.spec";
  84. } else {
  85. specfile = std::string(AUTH_SPECFILE_LOCATION);
  86. }
  87. isc::config::ModuleCCSession cs = isc::config::ModuleCCSession(specfile,
  88. my_config_handler,
  89. my_command_handler);
  90. // main server loop
  91. fd_set fds;
  92. int ps = auth.getSocket();
  93. int ss = cs.getSocket();
  94. int nfds = max(ps, ss) + 1;
  95. int counter = 0;
  96. cout << "Server started." << endl;
  97. while (true) {
  98. FD_ZERO(&fds);
  99. FD_SET(ps, &fds);
  100. FD_SET(ss, &fds);
  101. int n = select(nfds, &fds, NULL, NULL, NULL);
  102. if (n < 0)
  103. throw FatalError("select error");
  104. if (FD_ISSET(ps, &fds)) {
  105. ++counter;
  106. auth.processMessage();
  107. }
  108. /* isset not really necessary, but keep it for now */
  109. if (FD_ISSET(ss, &fds)) {
  110. cs.check_command();
  111. }
  112. }
  113. } catch (isc::cc::SessionError se) {
  114. cout << se.what() << endl;
  115. exit(1);
  116. }
  117. return (0);
  118. }