basic_resolver.hpp 44 KB

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