asio_link.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // Copyright (C) 2010 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. // $Id$
  15. #ifndef __ASIO_LINK_H
  16. #define __ASIO_LINK_H 1
  17. // IMPORTANT NOTE: only very few ASIO headers files can be included in
  18. // this file. In particular, asio.hpp should never be included here.
  19. // See the description of the namespace below.
  20. #include <unistd.h>
  21. #include <asio/ip/address.hpp>
  22. #include <functional>
  23. #include <string>
  24. #include <boost/function.hpp>
  25. #include <exceptions/exceptions.h>
  26. namespace asio {
  27. // forward declaration for IOService::get_io_service() below
  28. class io_service;
  29. }
  30. class AuthSrv;
  31. /// \namespace asio_link
  32. /// \brief A wrapper interface for the ASIO library.
  33. ///
  34. /// The \c asio_link namespace is used to define a set of wrapper interfaces
  35. /// for the ASIO library.
  36. ///
  37. /// BIND 10 uses the non-Boost version of ASIO because it's header-only,
  38. /// i.e., does not require a separate library object to be linked, and thus
  39. /// lowers the bar for introduction.
  40. ///
  41. /// But the advantage comes with its own costs: since the header-only version
  42. /// includes more definitions in public header files, it tends to trigger
  43. /// more compiler warnings for our own sources, and, depending on the
  44. /// compiler options, may make the build fail.
  45. ///
  46. /// We also found it may be tricky to use ASIO and standard C++ libraries
  47. /// in a single translation unit, i.e., a .cc file: depending on the order
  48. /// of including header files, ASIO may or may not work on some platforms.
  49. ///
  50. /// This wrapper interface is intended to centralize these
  51. /// problematic issues in a single sub module. Other BIND 10 modules should
  52. /// simply include \c asio_link.h and use the wrapper API instead of
  53. /// including ASIO header files and using ASIO-specific classes directly.
  54. ///
  55. /// This wrapper may be used for other IO libraries if and when we want to
  56. /// switch, but generality for that purpose is not the primary goal of
  57. /// this module. The resulting interfaces are thus straightforward mapping
  58. /// to the ASIO counterparts.
  59. ///
  60. /// Notes to developers:
  61. /// Currently the wrapper interface is specific to the authoritative
  62. /// server implementation. But the plan is to generalize it and have
  63. /// other modules use it.
  64. ///
  65. /// One obvious drawback of this approach is performance overhead
  66. /// due to the additional layer. We should eventually evaluate the cost
  67. /// of the wrapper abstraction in benchmark tests. Another drawback is
  68. /// that the wrapper interfaces don't provide all features of ASIO
  69. /// (at least for the moment). We should also re-evaluate the
  70. /// maintenance overhead of providing necessary wrappers as we develop
  71. /// more.
  72. ///
  73. /// On the other hand, we may be able to exploit the wrapper approach to
  74. /// simplify the interfaces (by limiting the usage) and unify performance
  75. /// optimization points.
  76. ///
  77. /// As for optimization, we may want to provide a custom allocator for
  78. /// the placeholder of callback handlers:
  79. /// http://think-async.com/Asio/asio-1.3.1/doc/asio/reference/asio_handler_allocate.html
  80. namespace asio_link {
  81. struct IOServiceImpl;
  82. /// \brief An exception that is thrown if an error occurs within the IO
  83. /// module. This is mainly intended to be a wrapper exception class for
  84. /// ASIO specific exceptions.
  85. class IOError : public isc::Exception {
  86. public:
  87. IOError(const char* file, size_t line, const char* what) :
  88. isc::Exception(file, line, what) {}
  89. };
  90. /// \brief The \c IOAddress class represents an IP addresses (version
  91. /// agnostic)
  92. ///
  93. /// This class is a wrapper for the ASIO \c ip::address class.
  94. class IOAddress {
  95. public:
  96. ///
  97. /// \name Constructors and Destructor
  98. ///
  99. /// This class is copyable. We use default versions of copy constructor
  100. /// and the assignment operator.
  101. /// We use the default destructor.
  102. //@{
  103. /// \brief Constructor from string.
  104. ///
  105. /// This constructor converts a textual representation of IPv4 and IPv6
  106. /// addresses into an IOAddress object.
  107. /// If \c address_str is not a valid representation of any type of
  108. /// address, an exception of class \c IOError will be thrown.
  109. /// This constructor allocates memory for the object, and if that fails
  110. /// a corresponding standard exception will be thrown.
  111. ///
  112. /// \param address_str Textual representation of address.
  113. IOAddress(const std::string& address_str);
  114. /// \brief Constructor from an ASIO \c ip::address object.
  115. ///
  116. /// This constructor is intended to be used within the wrapper
  117. /// implementation; user applications of the wrapper API won't use it.
  118. ///
  119. /// This constructor never throws an exception.
  120. ///
  121. /// \param asio_address The ASIO \c ip::address to be converted.
  122. IOAddress(const asio::ip::address& asio_adress);
  123. //@}
  124. /// \brief Convert the address to a string.
  125. ///
  126. /// This method is basically expected to be exception free, but
  127. /// generating the string will involve resource allocation,
  128. /// and if it fails the corresponding standard exception will be thrown.
  129. ///
  130. /// \return A string representation of the address.
  131. std::string toText() const;
  132. private:
  133. asio::ip::address asio_address_;
  134. };
  135. /// \brief The \c IOEndpoint class is an abstract base class to represent
  136. /// a communication endpoint.
  137. ///
  138. /// This class is a wrapper for the ASIO endpoint classes such as
  139. /// \c ip::tcp::endpoint and \c ip::udp::endpoint.
  140. ///
  141. /// Derived class implementations are completely hidden within the
  142. /// implementation. User applications only get access to concrete
  143. /// \c IOEndpoint objects via the abstract interfaces.
  144. class IOEndpoint {
  145. ///
  146. /// \name Constructors and Destructor
  147. ///
  148. /// Note: The copy constructor and the assignment operator are
  149. /// intentionally defined as private, making this class non-copyable.
  150. //@{
  151. private:
  152. IOEndpoint(const IOEndpoint& source);
  153. IOEndpoint& operator=(const IOEndpoint& source);
  154. protected:
  155. /// \brief The default constructor.
  156. ///
  157. /// This is intentionally defined as \c protected as this base class
  158. /// should never be instantiated (except as part of a derived class).
  159. IOEndpoint() {}
  160. public:
  161. /// The destructor.
  162. virtual ~IOEndpoint() {}
  163. //@}
  164. /// \brief Returns the address of the endpoint.
  165. ///
  166. /// This method returns an IOAddress object corresponding to \c this
  167. /// endpoint.
  168. /// Note that the return value is a real object, not a reference or
  169. /// a pointer.
  170. /// This is aligned with the interface of the ASIO counterpart:
  171. /// the \c address() method of \c ip::xxx::endpoint classes returns
  172. /// an \c ip::address object.
  173. /// This also means handling the address of an endpoint using this method
  174. /// can be expensive. If the address information is necessary in a
  175. /// performance sensitive context and there's a more efficient interface
  176. /// for that purpose, it's probably better to avoid using this method.
  177. ///
  178. /// This method never throws an exception.
  179. ///
  180. /// \return A copy of \c IOAddress object corresponding to the endpoint.
  181. virtual IOAddress getAddress() const = 0;
  182. /// \brief A polymorphic factory of endpoint from address and port.
  183. ///
  184. /// This method creates a new instance of (a derived class of)
  185. /// \c IOEndpoint object that identifies the pair of given address
  186. /// and port.
  187. /// The appropriate derived class is chosen based on the specified
  188. /// transport protocol. If the \c protocol doesn't specify a protocol
  189. /// supported in this implementation, an exception of class \c IOError
  190. /// will be thrown.
  191. ///
  192. /// Memory for the created object will be dynamically allocated. It's
  193. /// caller's responsibility to \c delete it later.
  194. /// If resource allocation for the new object fails, a corresponding
  195. /// standard exception will be thrown.
  196. ///
  197. /// \param protocol The transport protocol used for the endpoint.
  198. /// Currently, only \c IPPROTO_UDP and \c IPPROTO_TCP can be specified.
  199. /// \param address The (IP) address of the endpoint.
  200. /// \param port The transport port number of the endpoint
  201. /// \return A pointer to a newly created \c IOEndpoint object.
  202. static const IOEndpoint* create(const int protocol,
  203. const IOAddress& address,
  204. const unsigned short port);
  205. };
  206. /// \brief The \c IOSocket class is an abstract base class to represent
  207. /// various types of network sockets.
  208. ///
  209. /// This class is a wrapper for the ASIO socket classes such as
  210. /// \c ip::tcp::socket and \c ip::udp::socket.
  211. ///
  212. /// Derived class implementations are completely hidden within the
  213. /// implementation. User applications only get access to concrete
  214. /// \c IOSocket objects via the abstract interfaces.
  215. /// We may revisit this decision when we generalize the wrapper and more
  216. /// modules use it. Also, at that point we may define a separate (visible)
  217. /// derived class for testing purposes rather than providing factory methods
  218. /// (i.e., getDummy variants below).
  219. class IOSocket {
  220. ///
  221. /// \name Constructors and Destructor
  222. ///
  223. /// Note: The copy constructor and the assignment operator are
  224. /// intentionally defined as private, making this class non-copyable.
  225. //@{
  226. private:
  227. IOSocket(const IOSocket& source);
  228. IOSocket& operator=(const IOSocket& source);
  229. protected:
  230. /// \brief The default constructor.
  231. ///
  232. /// This is intentionally defined as \c protected as this base class
  233. /// should never be instantiated (except as part of a derived class).
  234. IOSocket() {}
  235. public:
  236. /// The destructor.
  237. virtual ~IOSocket() {}
  238. //@}
  239. /// \brief Return the "native" representation of the socket.
  240. ///
  241. /// In practice, this is the file descriptor of the socket for
  242. /// UNIX-like systems so the current implementation simply uses
  243. /// \c int as the type of the return value.
  244. /// We may have to need revisit this decision later.
  245. ///
  246. /// In general, the application should avoid using this method;
  247. /// it essentially discloses an implementation specific "handle" that
  248. /// can change the internal state of the socket (consider the
  249. /// application closes it, for example).
  250. /// But we sometimes need to perform very low-level operations that
  251. /// requires the native representation. Passing the file descriptor
  252. /// to a different process is one example.
  253. /// This method is provided as a necessary evil for such limited purposes.
  254. ///
  255. /// This method never throws an exception.
  256. ///
  257. /// \return The native representation of the socket. This is the socket
  258. /// file descriptor for UNIX-like systems.
  259. virtual int getNative() const = 0;
  260. /// \brief Return the transport protocol of the socket.
  261. ///
  262. /// Currently, it returns \c IPPROTO_UDP for UDP sockets, and
  263. /// \c IPPROTO_TCP for TCP sockets.
  264. ///
  265. /// This method never throws an exception.
  266. ///
  267. /// \return IPPROTO_UDP for UDP sockets
  268. /// \return IPPROTO_TCP for TCP sockets
  269. virtual int getProtocol() const = 0;
  270. /// \brief Return a non-usable "dummy" UDP socket for testing.
  271. ///
  272. /// This is a class method that returns a "mock" of UDP socket.
  273. /// This is not associated with any actual socket, and its only
  274. /// responsibility is to return \c IPPROTO_UDP from \c getProtocol().
  275. /// The only feasible usage of this socket is for testing so that
  276. /// the test code can prepare some "UDP data" even without opening any
  277. /// actual socket.
  278. ///
  279. /// This method never throws an exception.
  280. ///
  281. /// \return A reference to an \c IOSocket object whose \c getProtocol()
  282. /// returns \c IPPROTO_UDP.
  283. static IOSocket& getDummyUDPSocket();
  284. /// \brief Return a non-usable "dummy" TCP socket for testing.
  285. ///
  286. /// See \c getDummyUDPSocket(). This method is its TCP version.
  287. ///
  288. /// \return A reference to an \c IOSocket object whose \c getProtocol()
  289. /// returns \c IPPROTO_TCP.
  290. static IOSocket& getDummyTCPSocket();
  291. };
  292. /// \brief The \c IOMessage class encapsulates an incoming message received
  293. /// on a socket.
  294. ///
  295. /// An \c IOMessage object represents a tuple of a chunk of data
  296. /// (a UDP packet or some segment of TCP stream), the socket over which the
  297. /// data is passed, the information about the other end point of the
  298. /// communication, and perhaps more.
  299. ///
  300. /// The current design and interfaces of this class is tentative.
  301. /// It only provides a minimal level of support that is necessary for
  302. /// the current implementation of the authoritative server.
  303. /// A future version of this class will definitely support more.
  304. class IOMessage {
  305. ///
  306. /// \name Constructors and Destructor
  307. ///
  308. /// Note: The copy constructor and the assignment operator are
  309. /// intentionally defined as private, making this class non-copyable.
  310. //@{
  311. private:
  312. IOMessage(const IOMessage& source);
  313. IOMessage& operator=(const IOMessage& source);
  314. public:
  315. /// \brief Constructor from message information.
  316. ///
  317. /// This constructor needs to handle the ASIO \c ip::address class,
  318. /// and is intended to be used within this wrapper implementation.
  319. /// Once the \c IOMessage object is created, the application can
  320. /// get access to the information via the wrapper interface such as
  321. /// \c getRemoteAddress().
  322. ///
  323. /// This constructor never throws an exception.
  324. ///
  325. /// \param data A pointer to the message data.
  326. /// \param data_size The size of the message data in bytes.
  327. /// \param io_socket The socket over which the data is given.
  328. /// \param remote_endpoint The other endpoint of the socket, that is,
  329. /// the sender of the message.
  330. IOMessage(const void* data, const size_t data_size, IOSocket& io_socket,
  331. const IOEndpoint& remote_endpoint);
  332. //@}
  333. /// \brief Returns a pointer to the received data.
  334. const void* getData() const { return (data_); }
  335. /// \brief Returns the size of the received data in bytes.
  336. size_t getDataSize() const { return (data_size_); }
  337. /// \brief Returns the socket on which the message arrives.
  338. const IOSocket& getSocket() const { return (io_socket_); }
  339. /// \brief Returns the endpoint that sends the message.
  340. const IOEndpoint& getRemoteEndpoint() const { return (remote_endpoint_); }
  341. private:
  342. const void* data_;
  343. const size_t data_size_;
  344. IOSocket& io_socket_;
  345. const IOEndpoint& remote_endpoint_;
  346. };
  347. /// \brief The \c IOService class is a wrapper for the ASIO \c io_service
  348. /// class.
  349. ///
  350. /// Currently, the interface of this class is very specific to the
  351. /// authoritative server implementation as indicated in the signature of
  352. /// the constructor, but the plan is to generalize it so that other BIND 10
  353. /// modules can use this interface, too.
  354. class IOService {
  355. ///
  356. /// \name Constructors and Destructor
  357. ///
  358. /// These are currently very specific to the authoritative server
  359. /// implementation.
  360. ///
  361. /// Note: The copy constructor and the assignment operator are
  362. /// intentionally defined as private, making this class non-copyable.
  363. //@{
  364. private:
  365. IOService(const IOService& source);
  366. IOService& operator=(const IOService& source);
  367. public:
  368. /// \brief The constructor with a specific IP address and port on which
  369. /// the services listen on.
  370. IOService(AuthSrv* auth_server, const char& port, const char& address);
  371. /// \brief The constructor with a specific port on which the services
  372. /// listen on.
  373. ///
  374. /// It effectively listens on "any" IPv4 and/or IPv6 addresses.
  375. /// IPv4/IPv6 services will be available if and only if \c use_ipv4
  376. /// or \c use_ipv6 is \c true, respectively.
  377. IOService(AuthSrv* auth_server, const char& port,
  378. const bool use_ipv4, const bool use_ipv6);
  379. /// \brief The destructor.
  380. ~IOService();
  381. //@}
  382. /// \brief Start the underlying event loop.
  383. ///
  384. /// This method does not return control to the caller until
  385. /// the \c stop() method is called via some handler.
  386. void run();
  387. /// \brief Stop the underlying event loop.
  388. ///
  389. /// This will return the control to the caller of the \c run() method.
  390. void stop();
  391. /// \brief Return the native \c io_service object used in this wrapper.
  392. ///
  393. /// This is a short term work around to support other BIND 10 modules
  394. /// that share the same \c io_service with the authoritative server.
  395. /// It will eventually be removed once the wrapper interface is
  396. /// generalized.
  397. asio::io_service& get_io_service();
  398. /// \brief A functor(-like) class that specifies a custom call back
  399. /// invoked from the event loop instead of the embedded authoritative
  400. /// server callbacks.
  401. ///
  402. /// Currently, the callback is intended to be used only for testing
  403. /// purposes. But we'll need a generic callback type like this to
  404. /// generalize the wrapper interface.
  405. typedef boost::function<void(const IOMessage& io_message)> IOCallBack;
  406. /// \brief Set the custom call back invoked from the event loop.
  407. ///
  408. /// Right now this method is only for testing, but will eventually be
  409. /// generalized.
  410. void setCallBack(IOCallBack callback);
  411. private:
  412. IOServiceImpl* impl_;
  413. };
  414. } // asio_link
  415. #endif // __ASIO_LINK_H
  416. // Local Variables:
  417. // mode: c++
  418. // End: