dns_service.cc 6.4 KB

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