basic_resolver.hpp 44 KB

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