client.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef __CLIENT_H
  15. #define __CLIENT_H 1
  16. #include <string>
  17. #include <ostream>
  18. #include <boost/noncopyable.hpp>
  19. #include <acl/ip_check.h>
  20. namespace isc {
  21. namespace asiolink {
  22. class IOMessage;
  23. class IOEndpoint;
  24. }
  25. namespace acl {
  26. struct IPAddress;
  27. }
  28. namespace server_common {
  29. /// A DNS client with a single request context.
  30. ///
  31. /// The \c Client class represents a DNS client with information of one
  32. /// DNS request (e.g., a query). The information includes the source and
  33. /// destination IP addresses of the request, information of the DNS request
  34. /// message such as the query name or (if provided) TSIG key information.
  35. ///
  36. /// A \c Client class object is expected to be constructed on receiving a
  37. /// new request with lower level information such as IP addresses and is
  38. /// updated with DNS specific information as the server processes the request.
  39. /// It is also expected to be used as the primary interface for request
  40. /// processing such as query handling or access control.
  41. ///
  42. /// Furthermore, to minimize the overhead, this class would be further
  43. /// extended so that it can be reusable with an additional method to reset
  44. /// the internal information.
  45. ///
  46. /// In the current initial implementation, however, it only contains the
  47. /// lower level information in the form of \c IOMessage object and cannot
  48. /// be reused (it must be constructed for every new request). Also, the
  49. /// only actual usage of this class at this moment is for ACL handling.
  50. ///
  51. /// A \c Client class object is generally assumed to be valid throughout
  52. /// the processing of a single request, and then be destructed or (when
  53. /// supported) reset. To avoid it is copied and held accidentally beyond
  54. /// the expected valid period, it is intentionally made non copyable.
  55. ///
  56. /// Notes about other possibilities: we may want to abstract it further,
  57. /// so that it can also be used for DHCP. In that case, we'd subclass a
  58. /// base client class for DNS specific clients and DHCP specific clients.
  59. /// We might also want to separate DNS clients for authoritative servers
  60. /// and clients for the resolver, especially because the former could be
  61. /// simpler with performance optimizations.
  62. class Client : boost::noncopyable {
  63. public:
  64. ///
  65. /// \name Constructors and Destructor
  66. ///
  67. //@{
  68. /// The constructor.
  69. ///
  70. /// This initial version of constructor takes an \c IOMessage object
  71. /// that is supposed to represent a DNS request message sent from an
  72. /// external client (but the constructor does not perform any assumption
  73. /// check on the given \c IOMessage).
  74. ///
  75. /// If and when we extend the behavior and responsibility
  76. /// of this class, this version of constructor will probably be
  77. /// deprecated.
  78. ///
  79. /// \c request_message must be valid throughout the lifetime of the client.
  80. ///
  81. /// \exception None
  82. /// \param request_message Refers to \c IOMessage corresponding to some
  83. /// DNS request message.
  84. explicit Client(const isc::asiolink::IOMessage& request_message);
  85. /// The destructor
  86. ~Client();
  87. //@}
  88. /// Return the client's endpoint of the request.
  89. ///
  90. /// This should be identical to the result of \c getRemoteEndpoint()
  91. /// called on \c request_message passed to the constructor.
  92. ///
  93. /// \exception None
  94. const isc::asiolink::IOEndpoint& getRequestSourceEndpoint() const;
  95. /// Return the IP address part of the client request's endpoint.
  96. ///
  97. /// The resulting \c IPAddress can be constructed using
  98. /// \c getRequestSourceEndpoint(), and in that sense this method is
  99. /// redundant. But this implementation internally constructs the
  100. /// \c IPAddress on construction and always returns a reference to it,
  101. /// and should be more efficient. It is provided so that it can be
  102. /// called multiple times in a complicated ACL with minimum cost.
  103. ///
  104. /// \exception None
  105. const isc::acl::IPAddress& getRequestSourceIPAddress() const;
  106. /// Convert the Client to a string.
  107. ///
  108. /// (In the initial implementation) the format of the resulting string
  109. /// is as follows:
  110. /// \code <IP address>#<port>
  111. /// \endcode
  112. /// The IP address is the textual representation of the client's IP
  113. /// address, which is the source address of the request the client has
  114. /// sent. The port is the UDP or TCP of the client's end of the request.
  115. ///
  116. /// \exception std::bad_alloc Internal resource allocation fails
  117. std::string toText() const;
  118. private:
  119. struct ClientImpl;
  120. ClientImpl* impl_;
  121. };
  122. /// \brief Insert the \c Client as a string into stream.
  123. ///
  124. /// This method convert \c client into a string and inserts it into the
  125. /// output stream \c os.
  126. ///
  127. /// \param os A \c std::ostream object on which the insertion operation is
  128. /// performed.
  129. /// \param edns A reference to an \c Client object output by the operation.
  130. /// \return A reference to the same \c std::ostream object referenced by
  131. /// parameter \c os after the insertion operation.
  132. std::ostream& operator<<(std::ostream& os, const Client& client);
  133. }
  134. }
  135. #endif // __CLIENT_H
  136. // Local Variables:
  137. // mode: c++
  138. // End: