basic_resolver.hpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. //
  2. // ip/basic_resolver.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_HPP
  11. #define BOOST_ASIO_IP_BASIC_RESOLVER_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/async_result.hpp>
  18. #include <boost/asio/detail/handler_type_requirements.hpp>
  19. #include <boost/asio/detail/io_object_impl.hpp>
  20. #include <boost/asio/detail/non_const_lvalue.hpp>
  21. #include <boost/asio/detail/string_view.hpp>
  22. #include <boost/asio/detail/throw_error.hpp>
  23. #include <boost/asio/error.hpp>
  24. #include <boost/asio/execution_context.hpp>
  25. #include <boost/asio/executor.hpp>
  26. #include <boost/asio/ip/basic_resolver_iterator.hpp>
  27. #include <boost/asio/ip/basic_resolver_query.hpp>
  28. #include <boost/asio/ip/basic_resolver_results.hpp>
  29. #include <boost/asio/ip/resolver_base.hpp>
  30. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  31. # include <boost/asio/detail/winrt_resolver_service.hpp>
  32. #else
  33. # include <boost/asio/detail/resolver_service.hpp>
  34. #endif
  35. #if defined(BOOST_ASIO_HAS_MOVE)
  36. # include <utility>
  37. #endif // defined(BOOST_ASIO_HAS_MOVE)
  38. #include <boost/asio/detail/push_options.hpp>
  39. namespace boost {
  40. namespace asio {
  41. namespace ip {
  42. #if !defined(BOOST_ASIO_IP_BASIC_RESOLVER_FWD_DECL)
  43. #define BOOST_ASIO_IP_BASIC_RESOLVER_FWD_DECL
  44. // Forward declaration with defaulted arguments.
  45. template <typename InternetProtocol, typename Executor = executor>
  46. class basic_resolver;
  47. #endif // !defined(BOOST_ASIO_IP_BASIC_RESOLVER_FWD_DECL)
  48. /// Provides endpoint resolution functionality.
  49. /**
  50. * The basic_resolver class template provides the ability to resolve a query
  51. * to a list of endpoints.
  52. *
  53. * @par Thread Safety
  54. * @e Distinct @e objects: Safe.@n
  55. * @e Shared @e objects: Unsafe.
  56. */
  57. template <typename InternetProtocol, typename Executor>
  58. class basic_resolver
  59. : public resolver_base
  60. {
  61. public:
  62. /// The type of the executor associated with the object.
  63. typedef Executor executor_type;
  64. /// The protocol type.
  65. typedef InternetProtocol protocol_type;
  66. /// The endpoint type.
  67. typedef typename InternetProtocol::endpoint endpoint_type;
  68. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  69. /// (Deprecated.) The query type.
  70. typedef basic_resolver_query<InternetProtocol> query;
  71. /// (Deprecated.) The iterator type.
  72. typedef basic_resolver_iterator<InternetProtocol> iterator;
  73. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  74. /// The results type.
  75. typedef basic_resolver_results<InternetProtocol> results_type;
  76. /// Construct with executor.
  77. /**
  78. * This constructor creates a basic_resolver.
  79. *
  80. * @param ex The I/O executor that the resolver will use, by default, to
  81. * dispatch handlers for any asynchronous operations performed on the
  82. * resolver.
  83. */
  84. explicit basic_resolver(const executor_type& ex)
  85. : impl_(ex)
  86. {
  87. }
  88. /// Construct with execution context.
  89. /**
  90. * This constructor creates a basic_resolver.
  91. *
  92. * @param context An execution context which provides the I/O executor that
  93. * the resolver will use, by default, to dispatch handlers for any
  94. * asynchronous operations performed on the resolver.
  95. */
  96. template <typename ExecutionContext>
  97. explicit basic_resolver(ExecutionContext& context,
  98. typename enable_if<
  99. is_convertible<ExecutionContext&, execution_context&>::value
  100. >::type* = 0)
  101. : impl_(context)
  102. {
  103. }
  104. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  105. /// Move-construct a basic_resolver from another.
  106. /**
  107. * This constructor moves a resolver from one object to another.
  108. *
  109. * @param other The other basic_resolver object from which the move will
  110. * occur.
  111. *
  112. * @note Following the move, the moved-from object is in the same state as if
  113. * constructed using the @c basic_resolver(const executor_type&) constructor.
  114. */
  115. basic_resolver(basic_resolver&& other)
  116. : impl_(std::move(other.impl_))
  117. {
  118. }
  119. /// Move-assign a basic_resolver from another.
  120. /**
  121. * This assignment operator moves a resolver from one object to another.
  122. * Cancels any outstanding asynchronous operations associated with the target
  123. * object.
  124. *
  125. * @param other The other basic_resolver object from which the move will
  126. * occur.
  127. *
  128. * @note Following the move, the moved-from object is in the same state as if
  129. * constructed using the @c basic_resolver(const executor_type&) constructor.
  130. */
  131. basic_resolver& operator=(basic_resolver&& other)
  132. {
  133. impl_ = std::move(other.impl_);
  134. return *this;
  135. }
  136. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  137. /// Destroys the resolver.
  138. /**
  139. * This function destroys the resolver, cancelling any outstanding
  140. * asynchronous wait operations associated with the resolver as if by calling
  141. * @c cancel.
  142. */
  143. ~basic_resolver()
  144. {
  145. }
  146. /// Get the executor associated with the object.
  147. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  148. {
  149. return impl_.get_executor();
  150. }
  151. /// Cancel any asynchronous operations that are waiting on the resolver.
  152. /**
  153. * This function forces the completion of any pending asynchronous
  154. * operations on the host resolver. The handler for each cancelled operation
  155. * will be invoked with the boost::asio::error::operation_aborted error code.
  156. */
  157. void cancel()
  158. {
  159. return impl_.get_service().cancel(impl_.get_implementation());
  160. }
  161. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  162. /// (Deprecated: Use overload with separate host and service parameters.)
  163. /// Perform forward resolution of a query to a list of entries.
  164. /**
  165. * This function is used to resolve a query into a list of endpoint entries.
  166. *
  167. * @param q A query object that determines what endpoints will be returned.
  168. *
  169. * @returns A range object representing the list of endpoint entries. A
  170. * successful call to this function is guaranteed to return a non-empty
  171. * range.
  172. *
  173. * @throws boost::system::system_error Thrown on failure.
  174. */
  175. results_type resolve(const query& q)
  176. {
  177. boost::system::error_code ec;
  178. results_type r = impl_.get_service().resolve(
  179. impl_.get_implementation(), q, ec);
  180. boost::asio::detail::throw_error(ec, "resolve");
  181. return r;
  182. }
  183. /// (Deprecated: Use overload with separate host and service parameters.)
  184. /// Perform forward resolution of a query to a list of entries.
  185. /**
  186. * This function is used to resolve a query into a list of endpoint entries.
  187. *
  188. * @param q A query object that determines what endpoints will be returned.
  189. *
  190. * @param ec Set to indicate what error occurred, if any.
  191. *
  192. * @returns A range object representing the list of endpoint entries. An
  193. * empty range is returned if an error occurs. A successful call to this
  194. * function is guaranteed to return a non-empty range.
  195. */
  196. results_type resolve(const query& q, boost::system::error_code& ec)
  197. {
  198. return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
  199. }
  200. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  201. /// Perform forward resolution of a query to a list of entries.
  202. /**
  203. * This function is used to resolve host and service names into a list of
  204. * endpoint entries.
  205. *
  206. * @param host A string identifying a location. May be a descriptive name or
  207. * a numeric address string. If an empty string and the passive flag has been
  208. * specified, the resolved endpoints are suitable for local service binding.
  209. * If an empty string and passive is not specified, the resolved endpoints
  210. * will use the loopback address.
  211. *
  212. * @param service A string identifying the requested service. This may be a
  213. * descriptive name or a numeric string corresponding to a port number. May
  214. * be an empty string, in which case all resolved endpoints will have a port
  215. * number of 0.
  216. *
  217. * @returns A range object representing the list of endpoint entries. A
  218. * successful call to this function is guaranteed to return a non-empty
  219. * range.
  220. *
  221. * @throws boost::system::system_error Thrown on failure.
  222. *
  223. * @note On POSIX systems, host names may be locally defined in the file
  224. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  225. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  226. * resolution is performed using DNS. Operating systems may use additional
  227. * locations when resolving host names (such as NETBIOS names on Windows).
  228. *
  229. * On POSIX systems, service names are typically defined in the file
  230. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  231. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  232. * may use additional locations when resolving service names.
  233. */
  234. results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
  235. BOOST_ASIO_STRING_VIEW_PARAM service)
  236. {
  237. return resolve(host, service, resolver_base::flags());
  238. }
  239. /// Perform forward resolution of a query to a list of entries.
  240. /**
  241. * This function is used to resolve host and service names into a list of
  242. * endpoint entries.
  243. *
  244. * @param host A string identifying a location. May be a descriptive name or
  245. * a numeric address string. If an empty string and the passive flag has been
  246. * specified, the resolved endpoints are suitable for local service binding.
  247. * If an empty string and passive is not specified, the resolved endpoints
  248. * will use the loopback address.
  249. *
  250. * @param service A string identifying the requested service. This may be a
  251. * descriptive name or a numeric string corresponding to a port number. May
  252. * be an empty string, in which case all resolved endpoints will have a port
  253. * number of 0.
  254. *
  255. * @param ec Set to indicate what error occurred, if any.
  256. *
  257. * @returns A range object representing the list of endpoint entries. An
  258. * empty range is returned if an error occurs. A successful call to this
  259. * function is guaranteed to return a non-empty range.
  260. *
  261. * @note On POSIX systems, host names may be locally defined in the file
  262. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  263. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  264. * resolution is performed using DNS. Operating systems may use additional
  265. * locations when resolving host names (such as NETBIOS names on Windows).
  266. *
  267. * On POSIX systems, service names are typically defined in the file
  268. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  269. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  270. * may use additional locations when resolving service names.
  271. */
  272. results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
  273. BOOST_ASIO_STRING_VIEW_PARAM service, boost::system::error_code& ec)
  274. {
  275. return resolve(host, service, resolver_base::flags(), ec);
  276. }
  277. /// Perform forward resolution of a query to a list of entries.
  278. /**
  279. * This function is used to resolve host and service names into a list of
  280. * endpoint entries.
  281. *
  282. * @param host A string identifying a location. May be a descriptive name or
  283. * a numeric address string. If an empty string and the passive flag has been
  284. * specified, the resolved endpoints are suitable for local service binding.
  285. * If an empty string and passive is not specified, the resolved endpoints
  286. * will use the loopback address.
  287. *
  288. * @param service A string identifying the requested service. This may be a
  289. * descriptive name or a numeric string corresponding to a port number. May
  290. * be an empty string, in which case all resolved endpoints will have a port
  291. * number of 0.
  292. *
  293. * @param resolve_flags A set of flags that determine how name resolution
  294. * should be performed. The default flags are suitable for communication with
  295. * remote hosts. See the @ref resolver_base documentation for the set of
  296. * available flags.
  297. *
  298. * @returns A range object representing the list of endpoint entries. A
  299. * successful call to this function is guaranteed to return a non-empty
  300. * range.
  301. *
  302. * @throws boost::system::system_error Thrown on failure.
  303. *
  304. * @note On POSIX systems, host names may be locally defined in the file
  305. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  306. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  307. * resolution is performed using DNS. Operating systems may use additional
  308. * locations when resolving host names (such as NETBIOS names on Windows).
  309. *
  310. * On POSIX systems, service names are typically defined in the file
  311. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  312. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  313. * may use additional locations when resolving service names.
  314. */
  315. results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
  316. BOOST_ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags)
  317. {
  318. boost::system::error_code ec;
  319. basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
  320. static_cast<std::string>(service), resolve_flags);
  321. results_type r = impl_.get_service().resolve(
  322. impl_.get_implementation(), q, ec);
  323. boost::asio::detail::throw_error(ec, "resolve");
  324. return r;
  325. }
  326. /// Perform forward resolution of a query to a list of entries.
  327. /**
  328. * This function is used to resolve host and service names into a list of
  329. * endpoint entries.
  330. *
  331. * @param host A string identifying a location. May be a descriptive name or
  332. * a numeric address string. If an empty string and the passive flag has been
  333. * specified, the resolved endpoints are suitable for local service binding.
  334. * If an empty string and passive is not specified, the resolved endpoints
  335. * will use the loopback address.
  336. *
  337. * @param service A string identifying the requested service. This may be a
  338. * descriptive name or a numeric string corresponding to a port number. May
  339. * be an empty string, in which case all resolved endpoints will have a port
  340. * number of 0.
  341. *
  342. * @param resolve_flags A set of flags that determine how name resolution
  343. * should be performed. The default flags are suitable for communication with
  344. * remote hosts. See the @ref resolver_base documentation for the set of
  345. * available flags.
  346. *
  347. * @param ec Set to indicate what error occurred, if any.
  348. *
  349. * @returns A range object representing the list of endpoint entries. An
  350. * empty range is returned if an error occurs. A successful call to this
  351. * function is guaranteed to return a non-empty range.
  352. *
  353. * @note On POSIX systems, host names may be locally defined in the file
  354. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  355. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  356. * resolution is performed using DNS. Operating systems may use additional
  357. * locations when resolving host names (such as NETBIOS names on Windows).
  358. *
  359. * On POSIX systems, service names are typically defined in the file
  360. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  361. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  362. * may use additional locations when resolving service names.
  363. */
  364. results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
  365. BOOST_ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags,
  366. boost::system::error_code& ec)
  367. {
  368. basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
  369. static_cast<std::string>(service), resolve_flags);
  370. return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
  371. }
  372. /// Perform forward resolution of a query to a list of entries.
  373. /**
  374. * This function is used to resolve host and service names into a list of
  375. * endpoint entries.
  376. *
  377. * @param protocol A protocol object, normally representing either the IPv4 or
  378. * IPv6 version of an internet protocol.
  379. *
  380. * @param host A string identifying a location. May be a descriptive name or
  381. * a numeric address string. If an empty string and the passive flag has been
  382. * specified, the resolved endpoints are suitable for local service binding.
  383. * If an empty string and passive is not specified, the resolved endpoints
  384. * will use the loopback address.
  385. *
  386. * @param service A string identifying the requested service. This may be a
  387. * descriptive name or a numeric string corresponding to a port number. May
  388. * be an empty string, in which case all resolved endpoints will have a port
  389. * number of 0.
  390. *
  391. * @returns A range object representing the list of endpoint entries. A
  392. * successful call to this function is guaranteed to return a non-empty
  393. * range.
  394. *
  395. * @throws boost::system::system_error Thrown on failure.
  396. *
  397. * @note On POSIX systems, host names may be locally defined in the file
  398. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  399. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  400. * resolution is performed using DNS. Operating systems may use additional
  401. * locations when resolving host names (such as NETBIOS names on Windows).
  402. *
  403. * On POSIX systems, service names are typically defined in the file
  404. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  405. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  406. * may use additional locations when resolving service names.
  407. */
  408. results_type resolve(const protocol_type& protocol,
  409. BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service)
  410. {
  411. return resolve(protocol, host, service, resolver_base::flags());
  412. }
  413. /// Perform forward resolution of a query to a list of entries.
  414. /**
  415. * This function is used to resolve host and service names into a list of
  416. * endpoint entries.
  417. *
  418. * @param protocol A protocol object, normally representing either the IPv4 or
  419. * IPv6 version of an internet protocol.
  420. *
  421. * @param host A string identifying a location. May be a descriptive name or
  422. * a numeric address string. If an empty string and the passive flag has been
  423. * specified, the resolved endpoints are suitable for local service binding.
  424. * If an empty string and passive is not specified, the resolved endpoints
  425. * will use the loopback address.
  426. *
  427. * @param service A string identifying the requested service. This may be a
  428. * descriptive name or a numeric string corresponding to a port number. May
  429. * be an empty string, in which case all resolved endpoints will have a port
  430. * number of 0.
  431. *
  432. * @param ec Set to indicate what error occurred, if any.
  433. *
  434. * @returns A range object representing the list of endpoint entries. An
  435. * empty range is returned if an error occurs. A successful call to this
  436. * function is guaranteed to return a non-empty range.
  437. *
  438. * @note On POSIX systems, host names may be locally defined in the file
  439. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  440. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  441. * resolution is performed using DNS. Operating systems may use additional
  442. * locations when resolving host names (such as NETBIOS names on Windows).
  443. *
  444. * On POSIX systems, service names are typically defined in the file
  445. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  446. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  447. * may use additional locations when resolving service names.
  448. */
  449. results_type resolve(const protocol_type& protocol,
  450. BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
  451. boost::system::error_code& ec)
  452. {
  453. return resolve(protocol, host, service, resolver_base::flags(), ec);
  454. }
  455. /// Perform forward resolution of a query to a list of entries.
  456. /**
  457. * This function is used to resolve host and service names into a list of
  458. * endpoint entries.
  459. *
  460. * @param protocol A protocol object, normally representing either the IPv4 or
  461. * IPv6 version of an internet protocol.
  462. *
  463. * @param host A string identifying a location. May be a descriptive name or
  464. * a numeric address string. If an empty string and the passive flag has been
  465. * specified, the resolved endpoints are suitable for local service binding.
  466. * If an empty string and passive is not specified, the resolved endpoints
  467. * will use the loopback address.
  468. *
  469. * @param service A string identifying the requested service. This may be a
  470. * descriptive name or a numeric string corresponding to a port number. May
  471. * be an empty string, in which case all resolved endpoints will have a port
  472. * number of 0.
  473. *
  474. * @param resolve_flags A set of flags that determine how name resolution
  475. * should be performed. The default flags are suitable for communication with
  476. * remote hosts. See the @ref resolver_base documentation for the set of
  477. * available flags.
  478. *
  479. * @returns A range object representing the list of endpoint entries. A
  480. * successful call to this function is guaranteed to return a non-empty
  481. * range.
  482. *
  483. * @throws boost::system::system_error Thrown on failure.
  484. *
  485. * @note On POSIX systems, host names may be locally defined in the file
  486. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  487. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  488. * resolution is performed using DNS. Operating systems may use additional
  489. * locations when resolving host names (such as NETBIOS names on Windows).
  490. *
  491. * On POSIX systems, service names are typically defined in the file
  492. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  493. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  494. * may use additional locations when resolving service names.
  495. */
  496. results_type resolve(const protocol_type& protocol,
  497. BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
  498. resolver_base::flags resolve_flags)
  499. {
  500. boost::system::error_code ec;
  501. basic_resolver_query<protocol_type> q(
  502. protocol, static_cast<std::string>(host),
  503. static_cast<std::string>(service), resolve_flags);
  504. results_type r = impl_.get_service().resolve(
  505. impl_.get_implementation(), q, ec);
  506. boost::asio::detail::throw_error(ec, "resolve");
  507. return r;
  508. }
  509. /// Perform forward resolution of a query to a list of entries.
  510. /**
  511. * This function is used to resolve host and service names into a list of
  512. * endpoint entries.
  513. *
  514. * @param protocol A protocol object, normally representing either the IPv4 or
  515. * IPv6 version of an internet protocol.
  516. *
  517. * @param host A string identifying a location. May be a descriptive name or
  518. * a numeric address string. If an empty string and the passive flag has been
  519. * specified, the resolved endpoints are suitable for local service binding.
  520. * If an empty string and passive is not specified, the resolved endpoints
  521. * will use the loopback address.
  522. *
  523. * @param service A string identifying the requested service. This may be a
  524. * descriptive name or a numeric string corresponding to a port number. May
  525. * be an empty string, in which case all resolved endpoints will have a port
  526. * number of 0.
  527. *
  528. * @param resolve_flags A set of flags that determine how name resolution
  529. * should be performed. The default flags are suitable for communication with
  530. * remote hosts. See the @ref resolver_base documentation for the set of
  531. * available flags.
  532. *
  533. * @param ec Set to indicate what error occurred, if any.
  534. *
  535. * @returns A range object representing the list of endpoint entries. An
  536. * empty range is returned if an error occurs. A successful call to this
  537. * function is guaranteed to return a non-empty range.
  538. *
  539. * @note On POSIX systems, host names may be locally defined in the file
  540. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  541. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  542. * resolution is performed using DNS. Operating systems may use additional
  543. * locations when resolving host names (such as NETBIOS names on Windows).
  544. *
  545. * On POSIX systems, service names are typically defined in the file
  546. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  547. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  548. * may use additional locations when resolving service names.
  549. */
  550. results_type resolve(const protocol_type& protocol,
  551. BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
  552. resolver_base::flags resolve_flags, boost::system::error_code& ec)
  553. {
  554. basic_resolver_query<protocol_type> q(
  555. protocol, static_cast<std::string>(host),
  556. static_cast<std::string>(service), resolve_flags);
  557. return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
  558. }
  559. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  560. /// (Deprecated: Use overload with separate host and service parameters.)
  561. /// Asynchronously perform forward resolution of a query to a list of entries.
  562. /**
  563. * This function is used to asynchronously resolve a query into a list of
  564. * endpoint entries.
  565. *
  566. * @param q A query object that determines what endpoints will be returned.
  567. *
  568. * @param handler The handler to be called when the resolve operation
  569. * completes. Copies will be made of the handler as required. The function
  570. * signature of the handler must be:
  571. * @code void handler(
  572. * const boost::system::error_code& error, // Result of operation.
  573. * resolver::results_type results // Resolved endpoints as a range.
  574. * ); @endcode
  575. * Regardless of whether the asynchronous operation completes immediately or
  576. * not, the handler will not be invoked from within this function. On
  577. * immediate completion, invocation of the handler will be performed in a
  578. * manner equivalent to using boost::asio::post().
  579. *
  580. * A successful resolve operation is guaranteed to pass a non-empty range to
  581. * the handler.
  582. */
  583. template <typename ResolveHandler>
  584. BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
  585. void (boost::system::error_code, results_type))
  586. async_resolve(const query& q,
  587. BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
  588. {
  589. return boost::asio::async_initiate<ResolveHandler,
  590. void (boost::system::error_code, results_type)>(
  591. initiate_async_resolve(), handler, this, q);
  592. }
  593. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  594. /// Asynchronously perform forward resolution of a query to a list of entries.
  595. /**
  596. * This function is used to resolve host and service names into a list of
  597. * endpoint entries.
  598. *
  599. * @param host A string identifying a location. May be a descriptive name or
  600. * a numeric address string. If an empty string and the passive flag has been
  601. * specified, the resolved endpoints are suitable for local service binding.
  602. * If an empty string and passive is not specified, the resolved endpoints
  603. * will use the loopback address.
  604. *
  605. * @param service A string identifying the requested service. This may be a
  606. * descriptive name or a numeric string corresponding to a port number. May
  607. * be an empty string, in which case all resolved endpoints will have a port
  608. * number of 0.
  609. *
  610. * @param handler The handler to be called when the resolve operation
  611. * completes. Copies will be made of the handler as required. The function
  612. * signature of the handler must be:
  613. * @code void handler(
  614. * const boost::system::error_code& error, // Result of operation.
  615. * resolver::results_type results // Resolved endpoints as a range.
  616. * ); @endcode
  617. * Regardless of whether the asynchronous operation completes immediately or
  618. * not, the handler will not be invoked from within this function. On
  619. * immediate completion, invocation of the handler will be performed in a
  620. * manner equivalent to using boost::asio::post().
  621. *
  622. * A successful resolve operation is guaranteed to pass a non-empty range to
  623. * the handler.
  624. *
  625. * @note On POSIX systems, host names may be locally defined in the file
  626. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  627. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  628. * resolution is performed using DNS. Operating systems may use additional
  629. * locations when resolving host names (such as NETBIOS names on Windows).
  630. *
  631. * On POSIX systems, service names are typically defined in the file
  632. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  633. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  634. * may use additional locations when resolving service names.
  635. */
  636. template <typename ResolveHandler>
  637. BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
  638. void (boost::system::error_code, results_type))
  639. async_resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
  640. BOOST_ASIO_STRING_VIEW_PARAM service,
  641. BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
  642. {
  643. return async_resolve(host, service, resolver_base::flags(),
  644. BOOST_ASIO_MOVE_CAST(ResolveHandler)(handler));
  645. }
  646. /// Asynchronously perform forward resolution of a query to a list of entries.
  647. /**
  648. * This function is used to resolve host and service names into a list of
  649. * endpoint entries.
  650. *
  651. * @param host A string identifying a location. May be a descriptive name or
  652. * a numeric address string. If an empty string and the passive flag has been
  653. * specified, the resolved endpoints are suitable for local service binding.
  654. * If an empty string and passive is not specified, the resolved endpoints
  655. * will use the loopback address.
  656. *
  657. * @param service A string identifying the requested service. This may be a
  658. * descriptive name or a numeric string corresponding to a port number. May
  659. * be an empty string, in which case all resolved endpoints will have a port
  660. * number of 0.
  661. *
  662. * @param resolve_flags A set of flags that determine how name resolution
  663. * should be performed. The default flags are suitable for communication with
  664. * remote hosts. See the @ref resolver_base documentation for the set of
  665. * available flags.
  666. *
  667. * @param handler The handler to be called when the resolve operation
  668. * completes. Copies will be made of the handler as required. The function
  669. * signature of the handler must be:
  670. * @code void handler(
  671. * const boost::system::error_code& error, // Result of operation.
  672. * resolver::results_type results // Resolved endpoints as a range.
  673. * ); @endcode
  674. * Regardless of whether the asynchronous operation completes immediately or
  675. * not, the handler will not be invoked from within this function. On
  676. * immediate completion, invocation of the handler will be performed in a
  677. * manner equivalent to using boost::asio::post().
  678. *
  679. * A successful resolve operation is guaranteed to pass a non-empty range to
  680. * the handler.
  681. *
  682. * @note On POSIX systems, host names may be locally defined in the file
  683. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  684. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  685. * resolution is performed using DNS. Operating systems may use additional
  686. * locations when resolving host names (such as NETBIOS names on Windows).
  687. *
  688. * On POSIX systems, service names are typically defined in the file
  689. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  690. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  691. * may use additional locations when resolving service names.
  692. */
  693. template <typename ResolveHandler>
  694. BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
  695. void (boost::system::error_code, results_type))
  696. async_resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
  697. BOOST_ASIO_STRING_VIEW_PARAM service,
  698. resolver_base::flags resolve_flags,
  699. BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
  700. {
  701. basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
  702. static_cast<std::string>(service), resolve_flags);
  703. return boost::asio::async_initiate<ResolveHandler,
  704. void (boost::system::error_code, results_type)>(
  705. initiate_async_resolve(), handler, this, q);
  706. }
  707. /// Asynchronously perform forward resolution of a query to a list of entries.
  708. /**
  709. * This function is used to resolve host and service names into a list of
  710. * endpoint entries.
  711. *
  712. * @param protocol A protocol object, normally representing either the IPv4 or
  713. * IPv6 version of an internet protocol.
  714. *
  715. * @param host A string identifying a location. May be a descriptive name or
  716. * a numeric address string. If an empty string and the passive flag has been
  717. * specified, the resolved endpoints are suitable for local service binding.
  718. * If an empty string and passive is not specified, the resolved endpoints
  719. * will use the loopback address.
  720. *
  721. * @param service A string identifying the requested service. This may be a
  722. * descriptive name or a numeric string corresponding to a port number. May
  723. * be an empty string, in which case all resolved endpoints will have a port
  724. * number of 0.
  725. *
  726. * @param handler The handler to be called when the resolve operation
  727. * completes. Copies will be made of the handler as required. The function
  728. * signature of the handler must be:
  729. * @code void handler(
  730. * const boost::system::error_code& error, // Result of operation.
  731. * resolver::results_type results // Resolved endpoints as a range.
  732. * ); @endcode
  733. * Regardless of whether the asynchronous operation completes immediately or
  734. * not, the handler will not be invoked from within this function. On
  735. * immediate completion, invocation of the handler will be performed in a
  736. * manner equivalent to using boost::asio::post().
  737. *
  738. * A successful resolve operation is guaranteed to pass a non-empty range to
  739. * the handler.
  740. *
  741. * @note On POSIX systems, host names may be locally defined in the file
  742. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  743. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  744. * resolution is performed using DNS. Operating systems may use additional
  745. * locations when resolving host names (such as NETBIOS names on Windows).
  746. *
  747. * On POSIX systems, service names are typically defined in the file
  748. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  749. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  750. * may use additional locations when resolving service names.
  751. */
  752. template <typename ResolveHandler>
  753. BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
  754. void (boost::system::error_code, results_type))
  755. async_resolve(const protocol_type& protocol,
  756. BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
  757. BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
  758. {
  759. return async_resolve(protocol, host, service, resolver_base::flags(),
  760. BOOST_ASIO_MOVE_CAST(ResolveHandler)(handler));
  761. }
  762. /// Asynchronously perform forward resolution of a query to a list of entries.
  763. /**
  764. * This function is used to resolve host and service names into a list of
  765. * endpoint entries.
  766. *
  767. * @param protocol A protocol object, normally representing either the IPv4 or
  768. * IPv6 version of an internet protocol.
  769. *
  770. * @param host A string identifying a location. May be a descriptive name or
  771. * a numeric address string. If an empty string and the passive flag has been
  772. * specified, the resolved endpoints are suitable for local service binding.
  773. * If an empty string and passive is not specified, the resolved endpoints
  774. * will use the loopback address.
  775. *
  776. * @param service A string identifying the requested service. This may be a
  777. * descriptive name or a numeric string corresponding to a port number. May
  778. * be an empty string, in which case all resolved endpoints will have a port
  779. * number of 0.
  780. *
  781. * @param resolve_flags A set of flags that determine how name resolution
  782. * should be performed. The default flags are suitable for communication with
  783. * remote hosts. See the @ref resolver_base documentation for the set of
  784. * available flags.
  785. *
  786. * @param handler The handler to be called when the resolve operation
  787. * completes. Copies will be made of the handler as required. The function
  788. * signature of the handler must be:
  789. * @code void handler(
  790. * const boost::system::error_code& error, // Result of operation.
  791. * resolver::results_type results // Resolved endpoints as a range.
  792. * ); @endcode
  793. * Regardless of whether the asynchronous operation completes immediately or
  794. * not, the handler will not be invoked from within this function. On
  795. * immediate completion, invocation of the handler will be performed in a
  796. * manner equivalent to using boost::asio::post().
  797. *
  798. * A successful resolve operation is guaranteed to pass a non-empty range to
  799. * the handler.
  800. *
  801. * @note On POSIX systems, host names may be locally defined in the file
  802. * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
  803. * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
  804. * resolution is performed using DNS. Operating systems may use additional
  805. * locations when resolving host names (such as NETBIOS names on Windows).
  806. *
  807. * On POSIX systems, service names are typically defined in the file
  808. * <tt>/etc/services</tt>. On Windows, service names may be found in the file
  809. * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
  810. * may use additional locations when resolving service names.
  811. */
  812. template <typename ResolveHandler>
  813. BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
  814. void (boost::system::error_code, results_type))
  815. async_resolve(const protocol_type& protocol,
  816. BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
  817. resolver_base::flags resolve_flags,
  818. BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
  819. {
  820. basic_resolver_query<protocol_type> q(
  821. protocol, static_cast<std::string>(host),
  822. static_cast<std::string>(service), resolve_flags);
  823. return boost::asio::async_initiate<ResolveHandler,
  824. void (boost::system::error_code, results_type)>(
  825. initiate_async_resolve(), handler, this, q);
  826. }
  827. /// Perform reverse resolution of an endpoint to a list of entries.
  828. /**
  829. * This function is used to resolve an endpoint into a list of endpoint
  830. * entries.
  831. *
  832. * @param e An endpoint object that determines what endpoints will be
  833. * returned.
  834. *
  835. * @returns A range object representing the list of endpoint entries. A
  836. * successful call to this function is guaranteed to return a non-empty
  837. * range.
  838. *
  839. * @throws boost::system::system_error Thrown on failure.
  840. */
  841. results_type resolve(const endpoint_type& e)
  842. {
  843. boost::system::error_code ec;
  844. results_type i = impl_.get_service().resolve(
  845. impl_.get_implementation(), e, ec);
  846. boost::asio::detail::throw_error(ec, "resolve");
  847. return i;
  848. }
  849. /// Perform reverse resolution of an endpoint to a list of entries.
  850. /**
  851. * This function is used to resolve an endpoint into a list of endpoint
  852. * entries.
  853. *
  854. * @param e An endpoint object that determines what endpoints will be
  855. * returned.
  856. *
  857. * @param ec Set to indicate what error occurred, if any.
  858. *
  859. * @returns A range object representing the list of endpoint entries. An
  860. * empty range is returned if an error occurs. A successful call to this
  861. * function is guaranteed to return a non-empty range.
  862. */
  863. results_type resolve(const endpoint_type& e, boost::system::error_code& ec)
  864. {
  865. return impl_.get_service().resolve(impl_.get_implementation(), e, ec);
  866. }
  867. /// Asynchronously perform reverse resolution of an endpoint to a list of
  868. /// entries.
  869. /**
  870. * This function is used to asynchronously resolve an endpoint into a list of
  871. * endpoint entries.
  872. *
  873. * @param e An endpoint object that determines what endpoints will be
  874. * returned.
  875. *
  876. * @param handler The handler to be called when the resolve operation
  877. * completes. Copies will be made of the handler as required. The function
  878. * signature of the handler must be:
  879. * @code void handler(
  880. * const boost::system::error_code& error, // Result of operation.
  881. * resolver::results_type results // Resolved endpoints as a range.
  882. * ); @endcode
  883. * Regardless of whether the asynchronous operation completes immediately or
  884. * not, the handler will not be invoked from within this function. On
  885. * immediate completion, invocation of the handler will be performed in a
  886. * manner equivalent to using boost::asio::post().
  887. *
  888. * A successful resolve operation is guaranteed to pass a non-empty range to
  889. * the handler.
  890. */
  891. template <typename ResolveHandler>
  892. BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
  893. void (boost::system::error_code, results_type))
  894. async_resolve(const endpoint_type& e,
  895. BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
  896. {
  897. return boost::asio::async_initiate<ResolveHandler,
  898. void (boost::system::error_code, results_type)>(
  899. initiate_async_resolve(), handler, this, e);
  900. }
  901. private:
  902. // Disallow copying and assignment.
  903. basic_resolver(const basic_resolver&) BOOST_ASIO_DELETED;
  904. basic_resolver& operator=(const basic_resolver&) BOOST_ASIO_DELETED;
  905. struct initiate_async_resolve
  906. {
  907. template <typename ResolveHandler, typename Query>
  908. void operator()(BOOST_ASIO_MOVE_ARG(ResolveHandler) handler,
  909. basic_resolver* self, const Query& q) const
  910. {
  911. // If you get an error on the following line it means that your handler
  912. // does not meet the documented type requirements for a ResolveHandler.
  913. BOOST_ASIO_RESOLVE_HANDLER_CHECK(
  914. ResolveHandler, handler, results_type) type_check;
  915. boost::asio::detail::non_const_lvalue<ResolveHandler> handler2(handler);
  916. self->impl_.get_service().async_resolve(
  917. self->impl_.get_implementation(), q, handler2.value,
  918. self->impl_.get_implementation_executor());
  919. }
  920. };
  921. # if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  922. boost::asio::detail::io_object_impl<
  923. boost::asio::detail::winrt_resolver_service<InternetProtocol>,
  924. Executor> impl_;
  925. # else
  926. boost::asio::detail::io_object_impl<
  927. boost::asio::detail::resolver_service<InternetProtocol>,
  928. Executor> impl_;
  929. # endif
  930. };
  931. } // namespace ip
  932. } // namespace asio
  933. } // namespace boost
  934. #include <boost/asio/detail/pop_options.hpp>
  935. #endif // BOOST_ASIO_IP_BASIC_RESOLVER_HPP