dns_service.cc 6.6 KB

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