error.hpp 11 KB

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