dns_service.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2011 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 <asiolink/io_service.h>
  16. #include <asio/ip/address.hpp>
  17. #include <asio.hpp>
  18. #include <asiolink/tcp_server.h>
  19. #include <asiolink/udp_server.h>
  20. #include <log/dummylog.h>
  21. #include <boost/lexical_cast.hpp>
  22. using isc::log::dlog;
  23. namespace asiolink {
  24. class SimpleCallback;
  25. class DNSLookup;
  26. class DNSAnswer;
  27. namespace {
  28. asio::ip::address
  29. convertAddr(const std::string& address) {
  30. asio::error_code err;
  31. asio::ip::address addr = asio::ip::address::from_string(address, err);
  32. if (err) {
  33. isc_throw(IOError, "Invalid IP address '" << &address << "': "
  34. << err.message());
  35. }
  36. return (addr);
  37. }
  38. }
  39. class DNSServiceImpl {
  40. public:
  41. DNSServiceImpl(IOService& io_service, const char& port,
  42. const asio::ip::address* v4addr,
  43. const asio::ip::address* v6addr,
  44. SimpleCallback* checkin, DNSLookup* lookup,
  45. DNSAnswer* answer);
  46. IOService& io_service_;
  47. typedef boost::shared_ptr<UDPServer> UDPServerPtr;
  48. typedef boost::shared_ptr<TCPServer> TCPServerPtr;
  49. typedef boost::shared_ptr<DNSServer> DNSServerPtr;
  50. std::vector<DNSServerPtr> servers_;
  51. SimpleCallback *checkin_;
  52. DNSLookup *lookup_;
  53. DNSAnswer *answer_;
  54. void addServer(uint16_t port, const asio::ip::address& address) {
  55. try {
  56. dlog(std::string("Initialize TCP server at ") + address.to_string() + ":" + boost::lexical_cast<std::string>(port));
  57. TCPServerPtr tcpServer(new TCPServer(io_service_.get_io_service(),
  58. address, port, checkin_, lookup_, answer_));
  59. (*tcpServer)();
  60. servers_.push_back(tcpServer);
  61. dlog(std::string("Initialize UDP server at ") + address.to_string() + ":" + boost::lexical_cast<std::string>(port));
  62. UDPServerPtr udpServer(new UDPServer(io_service_.get_io_service(),
  63. address, port, checkin_, lookup_, answer_));
  64. (*udpServer)();
  65. servers_.push_back(udpServer);
  66. }
  67. catch (const asio::system_error& err) {
  68. // We need to catch and convert any ASIO level exceptions.
  69. // This can happen for unavailable address, binding a privilege port
  70. // without the privilege, etc.
  71. isc_throw(IOError, "Failed to initialize network servers: " <<
  72. err.what());
  73. }
  74. }
  75. void addServer(const char& port, const asio::ip::address& address) {
  76. uint16_t portnum;
  77. try {
  78. // XXX: SunStudio with stlport4 doesn't reject some invalid
  79. // representation such as "-1" by lexical_cast<uint16_t>, so
  80. // we convert it into a signed integer of a larger size and perform
  81. // range check ourselves.
  82. const int32_t portnum32 = boost::lexical_cast<int32_t>(&port);
  83. if (portnum32 < 0 || portnum32 > 65535) {
  84. isc_throw(IOError, "Invalid port number '" << &port);
  85. }
  86. portnum = portnum32;
  87. } catch (const boost::bad_lexical_cast& ex) {
  88. isc_throw(IOError, "Invalid port number '" << &port << "': " <<
  89. ex.what());
  90. }
  91. addServer(portnum, address);
  92. }
  93. };
  94. DNSServiceImpl::DNSServiceImpl(IOService& io_service,
  95. const char& port,
  96. const asio::ip::address* const v4addr,
  97. const asio::ip::address* const v6addr,
  98. SimpleCallback* checkin,
  99. DNSLookup* lookup,
  100. DNSAnswer* answer) :
  101. io_service_(io_service),
  102. checkin_(checkin),
  103. lookup_(lookup),
  104. answer_(answer)
  105. {
  106. if (v4addr) {
  107. addServer(port, *v4addr);
  108. }
  109. if (v6addr) {
  110. addServer(port, *v6addr);
  111. }
  112. }
  113. DNSService::DNSService(IOService& io_service,
  114. const char& port, const char& address,
  115. SimpleCallback* checkin,
  116. DNSLookup* lookup,
  117. DNSAnswer* answer) :
  118. impl_(new DNSServiceImpl(io_service, port, NULL, NULL, checkin, lookup,
  119. answer)), io_service_(io_service)
  120. {
  121. addServer(port, &address);
  122. }
  123. DNSService::DNSService(IOService& io_service,
  124. const char& port,
  125. const bool use_ipv4, const bool use_ipv6,
  126. SimpleCallback* checkin,
  127. DNSLookup* lookup,
  128. DNSAnswer* answer) :
  129. impl_(NULL), io_service_(io_service)
  130. {
  131. const asio::ip::address v4addr_any =
  132. asio::ip::address(asio::ip::address_v4::any());
  133. const asio::ip::address* const v4addrp = use_ipv4 ? &v4addr_any : NULL;
  134. const asio::ip::address v6addr_any =
  135. asio::ip::address(asio::ip::address_v6::any());
  136. const asio::ip::address* const v6addrp = use_ipv6 ? &v6addr_any : NULL;
  137. impl_ = new DNSServiceImpl(io_service, port, v4addrp, v6addrp, checkin, lookup, answer);
  138. }
  139. DNSService::DNSService(IOService& io_service, SimpleCallback* checkin,
  140. DNSLookup* lookup, DNSAnswer *answer) :
  141. impl_(new DNSServiceImpl(io_service, *"0", NULL, NULL, checkin, lookup,
  142. answer)), io_service_(io_service)
  143. {
  144. }
  145. DNSService::~DNSService() {
  146. delete impl_;
  147. }
  148. void
  149. DNSService::addServer(const char& port, const std::string& address) {
  150. impl_->addServer(port, convertAddr(address));
  151. }
  152. void
  153. DNSService::addServer(uint16_t port, const std::string& address) {
  154. impl_->addServer(port, convertAddr(address));
  155. }
  156. void
  157. DNSService::clearServers() {
  158. // FIXME: This does not work, it does not close the socket.
  159. // How is it done?
  160. impl_->servers_.clear();
  161. }
  162. } // namespace asiolink