5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 - 2008 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 #include "silcruntime.h"
24 SilcResult silc_get_errno(void)
26 SilcTls tls = silc_thread_get_tls();
36 void silc_set_errno(SilcResult error)
38 SilcTls tls = silc_thread_get_tls();
41 /* Try to create Tls */
42 tls = silc_thread_tls_init();
47 tls->error_reason[0] = '\0';
51 /* Set error, cannot fail. */
53 void silc_set_errno_nofail(SilcResult error)
55 SilcTls tls = silc_thread_get_tls();
60 tls->error_reason[0] = '\0';
64 /* Set errno and reason for error. */
66 void silc_set_errno_reason(SilcResult error, const char *format, ...)
68 SilcTls tls = silc_thread_get_tls();
72 /* Try to create Tls */
73 tls = silc_thread_tls_init();
79 silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
85 /* Set errno and reason for error, cannot fail. */
87 void silc_set_errno_reason_nofail(SilcResult error, const char *format, ...)
89 SilcTls tls = silc_thread_get_tls();
96 silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
102 /* Set error from POSIX errno. */
104 void silc_set_errno_posix(int error)
113 silc_set_errno(SILC_ERR_INTERRUPTED);
116 silc_set_errno(SILC_ERR_BAD_FD);
119 silc_set_errno(SILC_ERR_PERMISSION_DENIED);
122 silc_set_errno(SILC_ERR_BAD_ADDRESS);
124 case WSA_INVALID_HANDLE:
126 silc_set_errno(SILC_ERR_BAD_SOCKET);
128 case WSA_INVALID_PARAMETER:
130 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
132 case WSA_NOT_ENOUGH_MEMORY:
133 silc_set_errno(SILC_ERR_OUT_OF_MEMORY);
136 silc_set_errno(SILC_ERR_WOULD_BLOCK);
138 case WSAEOPNOTSUPPORT:
139 silc_set_errno(SILC_ERR_NOT_SUPPORTED);
142 silc_set_errno(SILC_ERR_ADDR_IN_USE);
145 silc_set_errno(SILC_ERR_NET_DOWN);
148 case WSAEHOSTUNREACH:
149 silc_set_errno(SILC_ERR_UNREACHABLE);
152 silc_set_errno(SILC_ERR_RESET);
154 case WSAECONNABORTED:
155 silc_set_errno(SILC_ERR_ABORTED);
158 silc_set_errno(SILC_ERR_TIMEOUT);
160 case WSAECONNREFUSED:
161 silc_set_errno(SILC_ERR_REFUSED);
164 silc_set_errno(SILC_ERR_HOST_DOWN);
167 silc_set_errno(SILC_ERR);
172 #endif /* SILC_WIN32 */
174 /* POSIX, etc. errors */
178 silc_set_errno(SILC_ERR_OUT_OF_MEMORY);
183 silc_set_errno(SILC_ERR_WOULD_BLOCK);
188 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
193 silc_set_errno(SILC_ERR_INTERRUPTED);
198 silc_set_errno(SILC_ERR_IO);
203 silc_set_errno(SILC_ERR_BROKEN_PIPE);
208 silc_set_errno(SILC_ERR_NO_SUCH_FILE);
213 silc_set_errno(SILC_ERR_ALREADY_EXISTS);
218 silc_set_errno(SILC_ERR_NOT_DIRECTORY);
223 silc_set_errno(SILC_ERR_IS_DIRECTORY);
228 silc_set_errno(SILC_ERR_BUSY);
233 silc_set_errno(SILC_ERR_NO_SUCH_DEVICE);
238 silc_set_errno(SILC_ERR_NO_SPACE_LEFT);
243 silc_set_errno(SILC_ERR_READ_ONLY);
248 silc_set_errno(SILC_ERR_BAD_FD);
251 #if defined(EADDRINUSE)
253 silc_set_errno(SILC_ERR_ADDR_IN_USE);
256 #if defined(ECONNREFUSED)
258 silc_set_errno(SILC_ERR_REFUSED);
261 #if defined(ECONNABORTED)
263 silc_set_errno(SILC_ERR_ABORTED);
266 #if defined(ECONNRESET)
268 silc_set_errno(SILC_ERR_RESET);
271 #if defined(ENETUNREACH)
273 silc_set_errno(SILC_ERR_UNREACHABLE);
276 #if defined(EHOSTUNREACH)
278 silc_set_errno(SILC_ERR_UNREACHABLE);
281 #if defined(ENETDOWN)
283 silc_set_errno(SILC_ERR_NET_DOWN);
286 #if defined(ETIMEDOUT)
288 silc_set_errno(SILC_ERR_TIMEOUT);
291 #if defined(EHOSTDOWN)
293 silc_set_errno(SILC_ERR_HOST_DOWN);
297 silc_set_errno(SILC_ERR);
302 /* Get last reason for error */
304 const char *silc_errno_reason(void)
306 SilcTls tls = silc_thread_get_tls();
308 if (!tls || tls->error_reason[0] == '\0')
309 return (const char *)"";
311 return tls->error_reason;
314 const char *silc_errno_strings[] =
320 "Allocation by zero",
321 "Too large allocation",
324 "Feature not supported",
325 "Operation not permitted",
334 "Prohibited character",
335 "Bad character encoding",
336 "Unsupported character encoding",
338 "Bad memory address",
339 "Bad buffer encoding",
358 "No such file or directory",
362 "Directory not empty",
363 "Device or resource busy",
365 "No space left on device",
369 "Bad file descriptor",
390 "Unknown IP address",
392 "Destination unreachable",
393 "Connection refused",
394 "Connection aborted",
395 "Connection reset by peer",
400 "Address already in use",
420 "Badly placed parenthesis",
421 "Bad hexadecimal number",
422 "Bad match register number",
423 "Badly placed special character",
424 "Regular expression too complex",
425 "Bad regular expression opcode",
429 /* Map error to string */
431 const char *silc_errno_string(SilcResult error)
433 if (error < 0 || error >= SILC_ERR_MAX)
434 return (const char *)"";
436 return silc_errno_strings[error];