dns_service.cc 7.2 KB

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