basic_resolver_query.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // ip/basic_resolver_query.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_IP_BASIC_RESOLVER_QUERY_HPP
  11. #define BOOST_ASIO_IP_BASIC_RESOLVER_QUERY_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <string>
  17. #include <boost/asio/detail/socket_ops.hpp>
  18. #include <boost/asio/ip/resolver_query_base.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace ip {
  23. /// An query to be passed to a resolver.
  24. /**
  25. * The boost::asio::ip::basic_resolver_query class template describes a query
  26. * that can be passed to a resolver.
  27. *
  28. * @par Thread Safety
  29. * @e Distinct @e objects: Safe.@n
  30. * @e Shared @e objects: Unsafe.
  31. */
  32. template <typename InternetProtocol>
  33. class basic_resolver_query
  34. : public resolver_query_base
  35. {
  36. public:
  37. /// The protocol type associated with the endpoint query.
  38. typedef InternetProtocol protocol_type;
  39. /// Construct with specified service name for any protocol.
  40. /**
  41. * This constructor is typically used to perform name resolution for local
  42. * service binding.
  43. *
  44. * @param service A string identifying the requested service. This may be a
  45. * descriptive name or a numeric string corresponding to a port number.
  46. *
  47. * @param resolve_flags A set of flags that determine how name resolution
  48. * should be performed. The default flags are suitable for local service
  49. * binding.
  50. *
  51. * @note On POSIX systems, service names are typically defined in the file
  52. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  53. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  54. * may use additional locations when resolving service names.
  55. */
  56. basic_resolver_query(const std::string& service,
  57. resolver_query_base::flags resolve_flags = passive | address_configured)
  58. : hints_(),
  59. host_name_(),
  60. service_name_(service)
  61. {
  62. typename InternetProtocol::endpoint endpoint;
  63. hints_.ai_flags = static_cast<int>(resolve_flags);
  64. hints_.ai_family = PF_UNSPEC;
  65. hints_.ai_socktype = endpoint.protocol().type();
  66. hints_.ai_protocol = endpoint.protocol().protocol();
  67. hints_.ai_addrlen = 0;
  68. hints_.ai_canonname = 0;
  69. hints_.ai_addr = 0;
  70. hints_.ai_next = 0;
  71. }
  72. /// Construct with specified service name for a given protocol.
  73. /**
  74. * This constructor is typically used to perform name resolution for local
  75. * service binding with a specific protocol version.
  76. *
  77. * @param protocol A protocol object, normally representing either the IPv4 or
  78. * IPv6 version of an internet protocol.
  79. *
  80. * @param service A string identifying the requested service. This may be a
  81. * descriptive name or a numeric string corresponding to a port number.
  82. *
  83. * @param resolve_flags A set of flags that determine how name resolution
  84. * should be performed. The default flags are suitable for local service
  85. * binding.
  86. *
  87. * @note On POSIX systems, service names are typically defined in the file
  88. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  89. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  90. * may use additional locations when resolving service names.
  91. */
  92. basic_resolver_query(const protocol_type& protocol,
  93. const std::string& service,
  94. resolver_query_base::flags resolve_flags = passive | address_configured)
  95. : hints_(),
  96. host_name_(),
  97. service_name_(service)
  98. {
  99. hints_.ai_flags = static_cast<int>(resolve_flags);
  100. hints_.ai_family = protocol.family();
  101. hints_.ai_socktype = protocol.type();
  102. hints_.ai_protocol = protocol.protocol();
  103. hints_.ai_addrlen = 0;
  104. hints_.ai_canonname = 0;
  105. hints_.ai_addr = 0;
  106. hints_.ai_next = 0;
  107. }
  108. /// Construct with specified host name and service name for any protocol.
  109. /**
  110. * This constructor is typically used to perform name resolution for
  111. * communication with remote hosts.
  112. *
  113. * @param host A string identifying a location. May be a descriptive name or
  114. * a numeric address string. If an empty string and the passive flag has been
  115. * specified, the resolved endpoints are suitable for local service binding.
  116. * If an empty string and passive is not specified, the resolved endpoints
  117. * will use the loopback address.
  118. *
  119. * @param service A string identifying the requested service. This may be a
  120. * descriptive name or a numeric string corresponding to a port number. May
  121. * be an empty string, in which case all resolved endpoints will have a port
  122. * number of 0.
  123. *
  124. * @param resolve_flags A set of flags that determine how name resolution
  125. * should be performed. The default flags are suitable for communication with
  126. * remote hosts.
  127. *
  128. * @note On POSIX systems, host names may be locally defined in the file
  129. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  130. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  131. * resolution is performed using DNS. Operating systems may use additional
  132. * locations when resolving host names (such as NETBIOS names on Windows).
  133. *
  134. * On POSIX systems, service names are typically defined in the file
  135. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  136. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  137. * may use additional locations when resolving service names.
  138. */
  139. basic_resolver_query(const std::string& host, const std::string& service,
  140. resolver_query_base::flags resolve_flags = address_configured)
  141. : hints_(),
  142. host_name_(host),
  143. service_name_(service)
  144. {
  145. typename InternetProtocol::endpoint endpoint;
  146. hints_.ai_flags = static_cast<int>(resolve_flags);
  147. hints_.ai_family = BOOST_ASIO_OS_DEF(AF_UNSPEC);
  148. hints_.ai_socktype = endpoint.protocol().type();
  149. hints_.ai_protocol = endpoint.protocol().protocol();
  150. hints_.ai_addrlen = 0;
  151. hints_.ai_canonname = 0;
  152. hints_.ai_addr = 0;
  153. hints_.ai_next = 0;
  154. }
  155. /// Construct with specified host name and service name for a given protocol.
  156. /**
  157. * This constructor is typically used to perform name resolution for
  158. * communication with remote hosts.
  159. *
  160. * @param protocol A protocol object, normally representing either the IPv4 or
  161. * IPv6 version of an internet protocol.
  162. *
  163. * @param host A string identifying a location. May be a descriptive name or
  164. * a numeric address string. If an empty string and the passive flag has been
  165. * specified, the resolved endpoints are suitable for local service binding.
  166. * If an empty string and passive is not specified, the resolved endpoints
  167. * will use the loopback address.
  168. *
  169. * @param service A string identifying the requested service. This may be a
  170. * descriptive name or a numeric string corresponding to a port number. May
  171. * be an empty string, in which case all resolved endpoints will have a port
  172. * number of 0.
  173. *
  174. * @param resolve_flags A set of flags that determine how name resolution
  175. * should be performed. The default flags are suitable for communication with
  176. * remote hosts.
  177. *
  178. * @note On POSIX systems, host names may be locally defined in the file
  179. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  180. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  181. * resolution is performed using DNS. Operating systems may use additional
  182. * locations when resolving host names (such as NETBIOS names on Windows).
  183. *
  184. * On POSIX systems, service names are typically defined in the file
  185. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  186. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  187. * may use additional locations when resolving service names.
  188. */
  189. basic_resolver_query(const protocol_type& protocol,
  190. const std::string& host, const std::string& service,
  191. resolver_query_base::flags resolve_flags = address_configured)
  192. : hints_(),
  193. host_name_(host),
  194. service_name_(service)
  195. {
  196. hints_.ai_flags = static_cast<int>(resolve_flags);
  197. hints_.ai_family = protocol.family();
  198. hints_.ai_socktype = protocol.type();
  199. hints_.ai_protocol = protocol.protocol();
  200. hints_.ai_addrlen = 0;
  201. hints_.ai_canonname = 0;
  202. hints_.ai_addr = 0;
  203. hints_.ai_next = 0;
  204. }
  205. /// Get the hints associated with the query.
  206. const boost::asio::detail::addrinfo_type& hints() const
  207. {
  208. return hints_;
  209. }
  210. /// Get the host name associated with the query.
  211. std::string host_name() const
  212. {
  213. return host_name_;
  214. }
  215. /// Get the service name associated with the query.
  216. std::string service_name() const
  217. {
  218. return service_name_;
  219. }
  220. private:
  221. boost::asio::detail::addrinfo_type hints_;
  222. std::string host_name_;
  223. std::string service_name_;
  224. };
  225. } // namespace ip
  226. } // namespace asio
  227. } // namespace boost
  228. #include <boost/asio/detail/pop_options.hpp>
  229. #endif // BOOST_ASIO_IP_BASIC_RESOLVER_QUERY_HPP