basic_resolver_query.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // ip/basic_resolver_query.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 ASIO_IP_BASIC_RESOLVER_QUERY_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <string>
  17. #include "asio/detail/socket_ops.hpp"
  18. #include "asio/ip/resolver_query_base.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. namespace ip {
  22. /// An query to be passed to a resolver.
  23. /**
  24. * The asio::ip::basic_resolver_query class template describes a query
  25. * that can be passed to a resolver.
  26. *
  27. * @par Thread Safety
  28. * @e Distinct @e objects: Safe.@n
  29. * @e Shared @e objects: Unsafe.
  30. */
  31. template <typename InternetProtocol>
  32. class basic_resolver_query
  33. : public resolver_query_base
  34. {
  35. public:
  36. /// The protocol type associated with the endpoint query.
  37. typedef InternetProtocol protocol_type;
  38. /// Construct with specified service name for any protocol.
  39. /**
  40. * This constructor is typically used to perform name resolution for local
  41. * service binding.
  42. *
  43. * @param service_name A string identifying the requested service. This may
  44. * be a descriptive name or a numeric string corresponding to a port number.
  45. *
  46. * @param resolve_flags A set of flags that determine how name resolution
  47. * should be performed. The default flags are suitable for local service
  48. * binding.
  49. *
  50. * @note On POSIX systems, service names are typically defined in the file
  51. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  52. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  53. * may use additional locations when resolving service names.
  54. */
  55. basic_resolver_query(const std::string& service_name,
  56. resolver_query_base::flags resolve_flags = passive | address_configured)
  57. : hints_(),
  58. host_name_(),
  59. service_name_(service_name)
  60. {
  61. typename InternetProtocol::endpoint endpoint;
  62. hints_.ai_flags = static_cast<int>(resolve_flags);
  63. hints_.ai_family = PF_UNSPEC;
  64. hints_.ai_socktype = endpoint.protocol().type();
  65. hints_.ai_protocol = endpoint.protocol().protocol();
  66. hints_.ai_addrlen = 0;
  67. hints_.ai_canonname = 0;
  68. hints_.ai_addr = 0;
  69. hints_.ai_next = 0;
  70. }
  71. /// Construct with specified service name for a given protocol.
  72. /**
  73. * This constructor is typically used to perform name resolution for local
  74. * service binding with a specific protocol version.
  75. *
  76. * @param protocol A protocol object, normally representing either the IPv4 or
  77. * IPv6 version of an internet protocol.
  78. *
  79. * @param service_name A string identifying the requested service. This may
  80. * be a descriptive name or a numeric string corresponding to a port number.
  81. *
  82. * @param resolve_flags A set of flags that determine how name resolution
  83. * should be performed. The default flags are suitable for local service
  84. * binding.
  85. *
  86. * @note On POSIX systems, service names are typically defined in the file
  87. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  88. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  89. * may use additional locations when resolving service names.
  90. */
  91. basic_resolver_query(const protocol_type& protocol,
  92. const std::string& service_name,
  93. resolver_query_base::flags resolve_flags = passive | address_configured)
  94. : hints_(),
  95. host_name_(),
  96. service_name_(service_name)
  97. {
  98. hints_.ai_flags = static_cast<int>(resolve_flags);
  99. hints_.ai_family = protocol.family();
  100. hints_.ai_socktype = protocol.type();
  101. hints_.ai_protocol = protocol.protocol();
  102. hints_.ai_addrlen = 0;
  103. hints_.ai_canonname = 0;
  104. hints_.ai_addr = 0;
  105. hints_.ai_next = 0;
  106. }
  107. /// Construct with specified host name and service name for any protocol.
  108. /**
  109. * This constructor is typically used to perform name resolution for
  110. * communication with remote hosts.
  111. *
  112. * @param host_name A string identifying a location. May be a descriptive name
  113. * or a numeric address string. If an empty string and the passive flag has
  114. * been specified, the resolved endpoints are suitable for local service
  115. * binding. If an empty string and passive is not specified, the resolved
  116. * endpoints will use the loopback address.
  117. *
  118. * @param service_name A string identifying the requested service. This may
  119. * be a descriptive name or a numeric string corresponding to a port number.
  120. * May be an empty string, in which case all resolved endpoints will have a
  121. * port number of 0.
  122. *
  123. * @param resolve_flags A set of flags that determine how name resolution
  124. * should be performed. The default flags are suitable for communication with
  125. * remote hosts.
  126. *
  127. * @note On POSIX systems, host names may be locally defined in the file
  128. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  129. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  130. * resolution is performed using DNS. Operating systems may use additional
  131. * locations when resolving host names (such as NETBIOS names on Windows).
  132. *
  133. * On POSIX systems, service names are typically defined in the file
  134. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  135. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  136. * may use additional locations when resolving service names.
  137. */
  138. basic_resolver_query(const std::string& host_name,
  139. const std::string& service_name,
  140. resolver_query_base::flags resolve_flags = address_configured)
  141. : hints_(),
  142. host_name_(host_name),
  143. service_name_(service_name)
  144. {
  145. typename InternetProtocol::endpoint endpoint;
  146. hints_.ai_flags = static_cast<int>(resolve_flags);
  147. hints_.ai_family = PF_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_name A string identifying a location. May be a descriptive name
  164. * or a numeric address string. If an empty string and the passive flag has
  165. * been specified, the resolved endpoints are suitable for local service
  166. * binding. If an empty string and passive is not specified, the resolved
  167. * endpoints will use the loopback address.
  168. *
  169. * @param service_name A string identifying the requested service. This may
  170. * be a descriptive name or a numeric string corresponding to a port number.
  171. * May be an empty string, in which case all resolved endpoints will have a
  172. * port 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_name, const std::string& service_name,
  191. resolver_query_base::flags resolve_flags = address_configured)
  192. : hints_(),
  193. host_name_(host_name),
  194. service_name_(service_name)
  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 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. asio::detail::addrinfo_type hints_;
  222. std::string host_name_;
  223. std::string service_name_;
  224. };
  225. } // namespace ip
  226. } // namespace asio
  227. #include "asio/detail/pop_options.hpp"
  228. #endif // ASIO_IP_BASIC_RESOLVER_QUERY_HPP