error.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // error.hpp
  3. // ~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. inline const boost::system::error_category& get_system_category()
  178. {
  179. return boost::system::system_category();
  180. }
  181. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  182. extern BOOST_ASIO_DECL
  183. const boost::system::error_category& get_netdb_category();
  184. extern BOOST_ASIO_DECL
  185. const boost::system::error_category& get_addrinfo_category();
  186. #else // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  187. inline const boost::system::error_category& get_netdb_category()
  188. {
  189. return get_system_category();
  190. }
  191. inline const boost::system::error_category& get_addrinfo_category()
  192. {
  193. return get_system_category();
  194. }
  195. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  196. extern BOOST_ASIO_DECL
  197. const boost::system::error_category& get_misc_category();
  198. static const boost::system::error_category&
  199. system_category BOOST_ASIO_UNUSED_VARIABLE
  200. = boost::asio::error::get_system_category();
  201. static const boost::system::error_category&
  202. netdb_category BOOST_ASIO_UNUSED_VARIABLE
  203. = boost::asio::error::get_netdb_category();
  204. static const boost::system::error_category&
  205. addrinfo_category BOOST_ASIO_UNUSED_VARIABLE
  206. = boost::asio::error::get_addrinfo_category();
  207. static const boost::system::error_category&
  208. misc_category BOOST_ASIO_UNUSED_VARIABLE
  209. = boost::asio::error::get_misc_category();
  210. } // namespace error
  211. } // namespace asio
  212. } // namespace boost
  213. namespace boost {
  214. namespace system {
  215. template<> struct is_error_code_enum<boost::asio::error::basic_errors>
  216. {
  217. static const bool value = true;
  218. };
  219. template<> struct is_error_code_enum<boost::asio::error::netdb_errors>
  220. {
  221. static const bool value = true;
  222. };
  223. template<> struct is_error_code_enum<boost::asio::error::addrinfo_errors>
  224. {
  225. static const bool value = true;
  226. };
  227. template<> struct is_error_code_enum<boost::asio::error::misc_errors>
  228. {
  229. static const bool value = true;
  230. };
  231. } // namespace system
  232. } // namespace boost
  233. namespace boost {
  234. namespace asio {
  235. namespace error {
  236. inline boost::system::error_code make_error_code(basic_errors e)
  237. {
  238. return boost::system::error_code(
  239. static_cast<int>(e), get_system_category());
  240. }
  241. inline boost::system::error_code make_error_code(netdb_errors e)
  242. {
  243. return boost::system::error_code(
  244. static_cast<int>(e), get_netdb_category());
  245. }
  246. inline boost::system::error_code make_error_code(addrinfo_errors e)
  247. {
  248. return boost::system::error_code(
  249. static_cast<int>(e), get_addrinfo_category());
  250. }
  251. inline boost::system::error_code make_error_code(misc_errors e)
  252. {
  253. return boost::system::error_code(
  254. static_cast<int>(e), get_misc_category());
  255. }
  256. } // namespace error
  257. namespace stream_errc {
  258. // Simulates the proposed stream_errc scoped enum.
  259. using error::eof;
  260. using error::not_found;
  261. } // namespace stream_errc
  262. namespace socket_errc {
  263. // Simulates the proposed socket_errc scoped enum.
  264. using error::already_open;
  265. using error::not_found;
  266. } // namespace socket_errc
  267. namespace resolver_errc {
  268. // Simulates the proposed resolver_errc scoped enum.
  269. using error::host_not_found;
  270. const error::netdb_errors try_again = error::host_not_found_try_again;
  271. using error::service_not_found;
  272. } // namespace resolver_errc
  273. } // namespace asio
  274. } // namespace boost
  275. #include <boost/asio/detail/pop_options.hpp>
  276. #undef BOOST_ASIO_NATIVE_ERROR
  277. #undef BOOST_ASIO_SOCKET_ERROR
  278. #undef BOOST_ASIO_NETDB_ERROR
  279. #undef BOOST_ASIO_GETADDRINFO_ERROR
  280. #undef BOOST_ASIO_WIN_OR_POSIX
  281. #if defined(BOOST_ASIO_HEADER_ONLY)
  282. # include <boost/asio/impl/error.ipp>
  283. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  284. #endif // BOOST_ASIO_ERROR_HPP