basic_resolver.hpp 44 KB

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