error.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // error.hpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_ERROR_HPP
  11. #define BOOST_ASIO_ERROR_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 <boost/cerrno.hpp>
  17. #include <boost/system/error_code.hpp>
  18. #include <boost/system/system_error.hpp>
  19. #if defined(BOOST_ASIO_WINDOWS) \
  20. || defined(__CYGWIN__) \
  21. || defined(BOOST_ASIO_WINDOWS_RUNTIME)
  22. # include <winerror.h>
  23. #else
  24. # include <cerrno>
  25. # include <netdb.h>
  26. #endif
  27. #if defined(GENERATING_DOCUMENTATION)
  28. /// INTERNAL ONLY.
  29. # define BOOST_ASIO_NATIVE_ERROR(e) implementation_defined
  30. /// INTERNAL ONLY.
  31. # define BOOST_ASIO_SOCKET_ERROR(e) implementation_defined
  32. /// INTERNAL ONLY.
  33. # define BOOST_ASIO_NETDB_ERROR(e) implementation_defined
  34. /// INTERNAL ONLY.
  35. # define BOOST_ASIO_GETADDRINFO_ERROR(e) implementation_defined
  36. /// INTERNAL ONLY.
  37. # define BOOST_ASIO_WIN_OR_POSIX(e_win, e_posix) implementation_defined
  38. #elif defined(BOOST_ASIO_WINDOWS_RUNTIME)
  39. # define BOOST_ASIO_NATIVE_ERROR(e) __HRESULT_FROM_WIN32(e)
  40. # define BOOST_ASIO_SOCKET_ERROR(e) __HRESULT_FROM_WIN32(WSA ## e)
  41. # define BOOST_ASIO_NETDB_ERROR(e) __HRESULT_FROM_WIN32(WSA ## e)
  42. # define BOOST_ASIO_GETADDRINFO_ERROR(e) __HRESULT_FROM_WIN32(WSA ## e)
  43. # define BOOST_ASIO_WIN_OR_POSIX(e_win, e_posix) e_win
  44. #elif defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  45. # define BOOST_ASIO_NATIVE_ERROR(e) e
  46. # define BOOST_ASIO_SOCKET_ERROR(e) WSA ## e
  47. # define BOOST_ASIO_NETDB_ERROR(e) WSA ## e
  48. # define BOOST_ASIO_GETADDRINFO_ERROR(e) WSA ## e
  49. # define BOOST_ASIO_WIN_OR_POSIX(e_win, e_posix) e_win
  50. #else
  51. # define BOOST_ASIO_NATIVE_ERROR(e) e
  52. # define BOOST_ASIO_SOCKET_ERROR(e) e
  53. # define BOOST_ASIO_NETDB_ERROR(e) e
  54. # define BOOST_ASIO_GETADDRINFO_ERROR(e) e
  55. # define BOOST_ASIO_WIN_OR_POSIX(e_win, e_posix) e_posix
  56. #endif
  57. #include <boost/asio/detail/push_options.hpp>
  58. namespace boost {
  59. namespace asio {
  60. namespace error {
  61. enum basic_errors
  62. {
  63. /// Permission denied.
  64. access_denied = BOOST_ASIO_SOCKET_ERROR(EACCES),
  65. /// Address family not supported by protocol.
  66. address_family_not_supported = BOOST_ASIO_SOCKET_ERROR(EAFNOSUPPORT),
  67. /// Address already in use.
  68. address_in_use = BOOST_ASIO_SOCKET_ERROR(EADDRINUSE),
  69. /// Transport endpoint is already connected.
  70. already_connected = BOOST_ASIO_SOCKET_ERROR(EISCONN),
  71. /// Operation already in progress.
  72. already_started = BOOST_ASIO_SOCKET_ERROR(EALREADY),
  73. /// Broken pipe.
  74. broken_pipe = BOOST_ASIO_WIN_OR_POSIX(
  75. BOOST_ASIO_NATIVE_ERROR(ERROR_BROKEN_PIPE),
  76. BOOST_ASIO_NATIVE_ERROR(EPIPE)),
  77. /// A connection has been aborted.
  78. connection_aborted = BOOST_ASIO_SOCKET_ERROR(ECONNABORTED),
  79. /// Connection refused.
  80. connection_refused = BOOST_ASIO_SOCKET_ERROR(ECONNREFUSED),
  81. /// Connection reset by peer.
  82. connection_reset = BOOST_ASIO_SOCKET_ERROR(ECONNRESET),
  83. /// Bad file descriptor.
  84. bad_descriptor = BOOST_ASIO_SOCKET_ERROR(EBADF),
  85. /// Bad address.
  86. fault = BOOST_ASIO_SOCKET_ERROR(EFAULT),
  87. /// No route to host.
  88. host_unreachable = BOOST_ASIO_SOCKET_ERROR(EHOSTUNREACH),
  89. /// Operation now in progress.
  90. in_progress = BOOST_ASIO_SOCKET_ERROR(EINPROGRESS),
  91. /// Interrupted system call.
  92. interrupted = BOOST_ASIO_SOCKET_ERROR(EINTR),
  93. /// Invalid argument.
  94. invalid_argument = BOOST_ASIO_SOCKET_ERROR(EINVAL),
  95. /// Message too long.
  96. message_size = BOOST_ASIO_SOCKET_ERROR(EMSGSIZE),
  97. /// The name was too long.
  98. name_too_long = BOOST_ASIO_SOCKET_ERROR(ENAMETOOLONG),
  99. /// Network is down.
  100. network_down = BOOST_ASIO_SOCKET_ERROR(ENETDOWN),
  101. /// Network dropped connection on reset.
  102. network_reset = BOOST_ASIO_SOCKET_ERROR(ENETRESET),
  103. /// Network is unreachable.
  104. network_unreachable = BOOST_ASIO_SOCKET_ERROR(ENETUNREACH),
  105. /// Too many open files.
  106. no_descriptors = BOOST_ASIO_SOCKET_ERROR(EMFILE),
  107. /// No buffer space available.
  108. no_buffer_space = BOOST_ASIO_SOCKET_ERROR(ENOBUFS),
  109. /// Cannot allocate memory.
  110. no_memory = BOOST_ASIO_WIN_OR_POSIX(
  111. BOOST_ASIO_NATIVE_ERROR(ERROR_OUTOFMEMORY),
  112. BOOST_ASIO_NATIVE_ERROR(ENOMEM)),
  113. /// Operation not permitted.
  114. no_permission = BOOST_ASIO_WIN_OR_POSIX(
  115. BOOST_ASIO_NATIVE_ERROR(ERROR_ACCESS_DENIED),
  116. BOOST_ASIO_NATIVE_ERROR(EPERM)),
  117. /// Protocol not available.
  118. no_protocol_option = BOOST_ASIO_SOCKET_ERROR(ENOPROTOOPT),
  119. /// No such device.
  120. no_such_device = BOOST_ASIO_WIN_OR_POSIX(
  121. BOOST_ASIO_NATIVE_ERROR(ERROR_BAD_UNIT),
  122. BOOST_ASIO_NATIVE_ERROR(ENODEV)),
  123. /// Transport endpoint is not connected.
  124. not_connected = BOOST_ASIO_SOCKET_ERROR(ENOTCONN),
  125. /// Socket operation on non-socket.
  126. not_socket = BOOST_ASIO_SOCKET_ERROR(ENOTSOCK),
  127. /// Operation cancelled.
  128. operation_aborted = BOOST_ASIO_WIN_OR_POSIX(
  129. BOOST_ASIO_NATIVE_ERROR(ERROR_OPERATION_ABORTED),
  130. BOOST_ASIO_NATIVE_ERROR(ECANCELED)),
  131. /// Operation not supported.
  132. operation_not_supported = BOOST_ASIO_SOCKET_ERROR(EOPNOTSUPP),
  133. /// Cannot send after transport endpoint shutdown.
  134. shut_down = BOOST_ASIO_SOCKET_ERROR(ESHUTDOWN),
  135. /// Connection timed out.
  136. timed_out = BOOST_ASIO_SOCKET_ERROR(ETIMEDOUT),
  137. /// Resource temporarily unavailable.
  138. try_again = BOOST_ASIO_WIN_OR_POSIX(
  139. BOOST_ASIO_NATIVE_ERROR(ERROR_RETRY),
  140. BOOST_ASIO_NATIVE_ERROR(EAGAIN)),
  141. /// The socket is marked non-blocking and the requested operation would block.
  142. would_block = BOOST_ASIO_SOCKET_ERROR(EWOULDBLOCK)
  143. };
  144. enum netdb_errors
  145. {
  146. /// Host not found (authoritative).
  147. host_not_found = BOOST_ASIO_NETDB_ERROR(HOST_NOT_FOUND),
  148. /// Host not found (non-authoritative).
  149. host_not_found_try_again = BOOST_ASIO_NETDB_ERROR(TRY_AGAIN),
  150. /// The query is valid but does not have associated address data.
  151. no_data = BOOST_ASIO_NETDB_ERROR(NO_DATA),
  152. /// A non-recoverable error occurred.
  153. no_recovery = BOOST_ASIO_NETDB_ERROR(NO_RECOVERY)
  154. };
  155. enum addrinfo_errors
  156. {
  157. /// The service is not supported for the given socket type.
  158. service_not_found = BOOST_ASIO_WIN_OR_POSIX(
  159. BOOST_ASIO_NATIVE_ERROR(WSATYPE_NOT_FOUND),
  160. BOOST_ASIO_GETADDRINFO_ERROR(EAI_SERVICE)),
  161. /// The socket type is not supported.
  162. socket_type_not_supported = BOOST_ASIO_WIN_OR_POSIX(
  163. BOOST_ASIO_NATIVE_ERROR(WSAESOCKTNOSUPPORT),
  164. BOOST_ASIO_GETADDRINFO_ERROR(EAI_SOCKTYPE))
  165. };
  166. enum misc_errors
  167. {
  168. /// Already open.
  169. already_open = 1,
  170. /// End of file or stream.
  171. eof,
  172. /// Element not found.
  173. not_found,
  174. /// The descriptor cannot fit into the select system call's fd_set.
  175. fd_set_failure
  176. };
  177. #if !defined(BOOST_ASIO_ERROR_LOCATION) \
  178. && !defined(BOOST_ASIO_DISABLE_ERROR_LOCATION) \
  179. && defined(BOOST_ASIO_HAS_BOOST_CONFIG) \
  180. && (BOOST_VERSION >= 107900)
  181. # define BOOST_ASIO_ERROR_LOCATION(e) \
  182. do { \
  183. BOOST_STATIC_CONSTEXPR boost::source_location loc \
  184. = BOOST_CURRENT_LOCATION; \
  185. (e).assign((e), &loc); \
  186. } while (false)
  187. #else // !defined(BOOST_ASIO_ERROR_LOCATION)
  188. // && !defined(BOOST_ASIO_DISABLE_ERROR_LOCATION)
  189. // && defined(BOOST_ASIO_HAS_BOOST_CONFIG)
  190. // && (BOOST_VERSION >= 107900)
  191. # define BOOST_ASIO_ERROR_LOCATION(e) (void)0
  192. #endif // !defined(BOOST_ASIO_ERROR_LOCATION)
  193. // && !defined(BOOST_ASIO_DISABLE_ERROR_LOCATION)
  194. // && defined(BOOST_ASIO_HAS_BOOST_CONFIG)
  195. // && (BOOST_VERSION >= 107900)
  196. inline void clear(boost::system::error_code& ec)
  197. {
  198. ec = boost::system::error_code();
  199. }
  200. inline const boost::system::error_category& get_system_category()
  201. {
  202. return boost::system::system_category();
  203. }
  204. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  205. extern BOOST_ASIO_DECL
  206. const boost::system::error_category& get_netdb_category();
  207. extern BOOST_ASIO_DECL
  208. const boost::system::error_category& get_addrinfo_category();
  209. #else // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  210. inline const boost::system::error_category& get_netdb_category()
  211. {
  212. return get_system_category();
  213. }
  214. inline const boost::system::error_category& get_addrinfo_category()
  215. {
  216. return get_system_category();
  217. }
  218. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  219. extern BOOST_ASIO_DECL
  220. const boost::system::error_category& get_misc_category();
  221. static const boost::system::error_category&
  222. system_category BOOST_ASIO_UNUSED_VARIABLE
  223. = boost::asio::error::get_system_category();
  224. static const boost::system::error_category&
  225. netdb_category BOOST_ASIO_UNUSED_VARIABLE
  226. = boost::asio::error::get_netdb_category();
  227. static const boost::system::error_category&
  228. addrinfo_category BOOST_ASIO_UNUSED_VARIABLE
  229. = boost::asio::error::get_addrinfo_category();
  230. static const boost::system::error_category&
  231. misc_category BOOST_ASIO_UNUSED_VARIABLE
  232. = boost::asio::error::get_misc_category();
  233. } // namespace error
  234. } // namespace asio
  235. } // namespace boost
  236. namespace boost {
  237. namespace system {
  238. template<> struct is_error_code_enum<boost::asio::error::basic_errors>
  239. {
  240. static const bool value = true;
  241. };
  242. template<> struct is_error_code_enum<boost::asio::error::netdb_errors>
  243. {
  244. static const bool value = true;
  245. };
  246. template<> struct is_error_code_enum<boost::asio::error::addrinfo_errors>
  247. {
  248. static const bool value = true;
  249. };
  250. template<> struct is_error_code_enum<boost::asio::error::misc_errors>
  251. {
  252. static const bool value = true;
  253. };
  254. } // namespace system
  255. } // namespace boost
  256. namespace boost {
  257. namespace asio {
  258. namespace error {
  259. inline boost::system::error_code make_error_code(basic_errors e)
  260. {
  261. return boost::system::error_code(
  262. static_cast<int>(e), get_system_category());
  263. }
  264. inline boost::system::error_code make_error_code(netdb_errors e)
  265. {
  266. return boost::system::error_code(
  267. static_cast<int>(e), get_netdb_category());
  268. }
  269. inline boost::system::error_code make_error_code(addrinfo_errors e)
  270. {
  271. return boost::system::error_code(
  272. static_cast<int>(e), get_addrinfo_category());
  273. }
  274. inline boost::system::error_code make_error_code(misc_errors e)
  275. {
  276. return boost::system::error_code(
  277. static_cast<int>(e), get_misc_category());
  278. }
  279. } // namespace error
  280. namespace stream_errc {
  281. // Simulates the proposed stream_errc scoped enum.
  282. using error::eof;
  283. using error::not_found;
  284. } // namespace stream_errc
  285. namespace socket_errc {
  286. // Simulates the proposed socket_errc scoped enum.
  287. using error::already_open;
  288. using error::not_found;
  289. } // namespace socket_errc
  290. namespace resolver_errc {
  291. // Simulates the proposed resolver_errc scoped enum.
  292. using error::host_not_found;
  293. const error::netdb_errors try_again = error::host_not_found_try_again;
  294. using error::service_not_found;
  295. } // namespace resolver_errc
  296. } // namespace asio
  297. } // namespace boost
  298. #include <boost/asio/detail/pop_options.hpp>
  299. #undef BOOST_ASIO_NATIVE_ERROR
  300. #undef BOOST_ASIO_SOCKET_ERROR
  301. #undef BOOST_ASIO_NETDB_ERROR
  302. #undef BOOST_ASIO_GETADDRINFO_ERROR
  303. #undef BOOST_ASIO_WIN_OR_POSIX
  304. #if defined(BOOST_ASIO_HEADER_ONLY)
  305. # include <boost/asio/impl/error.ipp>
  306. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  307. #endif // BOOST_ASIO_ERROR_HPP