dns_service.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <unistd.h> // for some IPC/network system calls
  16. #include <netinet/in.h>
  17. #include <sys/socket.h>
  18. #include <boost/lexical_cast.hpp>
  19. #include <log/dummylog.h>
  20. #include <asio.hpp>
  21. #include <dns_service.h>
  22. #include <asiolink/io_service.h>
  23. #include <tcp_server.h>
  24. #include <udp_server.h>
  25. #include <sync_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* answe);
  54. IOService& io_service_;
  55. typedef boost::shared_ptr<UDPServer> UDPServerPtr;
  56. typedef boost::shared_ptr<SyncUDPServer> SyncUDPServerPtr;
  57. typedef boost::shared_ptr<TCPServer> TCPServerPtr;
  58. typedef boost::shared_ptr<DNSServer> DNSServerPtr;
  59. std::vector<DNSServerPtr> servers_;
  60. SimpleCallback *checkin_;
  61. DNSLookup *lookup_;
  62. DNSAnswer *answer_;
  63. template<class Ptr, class Server> void addServerFromFD(int fd, int af) {
  64. Ptr server(new Server(io_service_.get_io_service(), fd, af, checkin_,
  65. lookup_, answer_));
  66. (*server)();
  67. servers_.push_back(server);
  68. }
  69. void addServer(uint16_t port, const asio::ip::address& address) {
  70. try {
  71. dlog(std::string("Initialize TCP server at ") +
  72. address.to_string() + ":" +
  73. boost::lexical_cast<std::string>(port));
  74. TCPServerPtr tcpServer(new TCPServer(io_service_.get_io_service(),
  75. address, port, checkin_,
  76. lookup_, answer_));
  77. (*tcpServer)();
  78. servers_.push_back(tcpServer);
  79. dlog(std::string("Initialize UDP server at ") +
  80. address.to_string() + ":" +
  81. boost::lexical_cast<std::string>(port));
  82. UDPServerPtr udpServer(new UDPServer(io_service_.get_io_service(),
  83. address, port, checkin_, lookup_, answer_));
  84. (*udpServer)();
  85. servers_.push_back(udpServer);
  86. } catch (const asio::system_error& err) {
  87. // We need to catch and convert any ASIO level exceptions.
  88. // This can happen for unavailable address, binding a privilege port
  89. // without the privilege, etc.
  90. isc_throw(IOError, "Failed to initialize network servers: " <<
  91. err.what());
  92. }
  93. }
  94. void addServer(const char& port, const asio::ip::address& address) {
  95. uint16_t portnum;
  96. try {
  97. // XXX: SunStudio with stlport4 doesn't reject some invalid
  98. // representation such as "-1" by lexical_cast<uint16_t>, so
  99. // we convert it into a signed integer of a larger size and perform
  100. // range check ourselves.
  101. const int32_t portnum32 = boost::lexical_cast<int32_t>(&port);
  102. if (portnum32 < 0 || portnum32 > 65535) {
  103. isc_throw(IOError, "Invalid port number '" << &port);
  104. }
  105. portnum = portnum32;
  106. } catch (const boost::bad_lexical_cast& ex) {
  107. isc_throw(IOError, "Invalid port number '" << &port << "': " <<
  108. ex.what());
  109. }
  110. addServer(portnum, address);
  111. }
  112. };
  113. DNSServiceImpl::DNSServiceImpl(IOService& io_service,
  114. const char& port,
  115. const asio::ip::address* const v4addr,
  116. const asio::ip::address* const v6addr,
  117. SimpleCallback* checkin,
  118. DNSLookup* lookup,
  119. DNSAnswer* answer) :
  120. io_service_(io_service),
  121. checkin_(checkin),
  122. lookup_(lookup),
  123. answer_(answer)
  124. {
  125. if (v4addr) {
  126. addServer(port, *v4addr);
  127. }
  128. if (v6addr) {
  129. addServer(port, *v6addr);
  130. }
  131. }
  132. DNSService::DNSService(IOService& io_service,
  133. const char& port, const char& address,
  134. SimpleCallback* checkin,
  135. DNSLookup* lookup,
  136. DNSAnswer* answer) :
  137. impl_(new DNSServiceImpl(io_service, port, NULL, NULL, checkin, lookup,
  138. answer)),
  139. io_service_(io_service)
  140. {
  141. addServer(port, &address);
  142. }
  143. DNSService::DNSService(IOService& io_service,
  144. const char& port,
  145. const bool use_ipv4, const bool use_ipv6,
  146. SimpleCallback* checkin,
  147. DNSLookup* lookup,
  148. DNSAnswer* answer) :
  149. impl_(NULL), io_service_(io_service)
  150. {
  151. const asio::ip::address v4addr_any =
  152. asio::ip::address(asio::ip::address_v4::any());
  153. const asio::ip::address* const v4addrp = use_ipv4 ? &v4addr_any : NULL;
  154. const asio::ip::address v6addr_any =
  155. asio::ip::address(asio::ip::address_v6::any());
  156. const asio::ip::address* const v6addrp = use_ipv6 ? &v6addr_any : NULL;
  157. impl_ = new DNSServiceImpl(io_service, port, v4addrp, v6addrp, checkin,
  158. lookup, answer);
  159. }
  160. DNSService::DNSService(IOService& io_service, SimpleCallback* checkin,
  161. DNSLookup* lookup, DNSAnswer *answer) :
  162. impl_(new DNSServiceImpl(io_service, *"0", NULL, NULL, checkin, lookup,
  163. answer)), io_service_(io_service)
  164. {
  165. }
  166. DNSService::~DNSService() {
  167. delete impl_;
  168. }
  169. void
  170. DNSService::addServer(const char& port, const std::string& address) {
  171. impl_->addServer(port, convertAddr(address));
  172. }
  173. void
  174. DNSService::addServer(uint16_t port, const std::string& address) {
  175. impl_->addServer(port, convertAddr(address));
  176. }
  177. void DNSService::addServerTCPFromFD(int fd, int af) {
  178. impl_->addServerFromFD<DNSServiceImpl::TCPServerPtr, TCPServer>(fd, af);
  179. }
  180. void DNSService::addServerUDPFromFD(int fd, int af, ServerFlag options) {
  181. if ((options & SERVER_SYNC_OK) != 0) {
  182. impl_->addServerFromFD<DNSServiceImpl::SyncUDPServerPtr,
  183. SyncUDPServer>(fd, af);
  184. } else {
  185. impl_->addServerFromFD<DNSServiceImpl::UDPServerPtr, UDPServer>(
  186. fd, af);
  187. }
  188. }
  189. void
  190. DNSService::clearServers() {
  191. BOOST_FOREACH(const DNSServiceImpl::DNSServerPtr& s, impl_->servers_) {
  192. s->stop();
  193. }
  194. impl_->servers_.clear();
  195. }
  196. } // namespace asiodns
  197. } // namespace isc