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