fixes, fully translated tomato, with english dictionary and Polish translation
[tomato.git] / release / src / router / openvpn / error.h
blob8974483befa5f6203d3676dd8ea993179c6093ed
1 /*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
8 * Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifndef ERROR_H
26 #define ERROR_H
28 #include "basic.h"
29 #include "thread.h"
31 /* #define ABORT_ON_ERROR */
33 #ifdef ENABLE_PKCS11
34 #define ERR_BUF_SIZE 8192
35 #else
36 #define ERR_BUF_SIZE 1280
37 #endif
39 struct gc_arena;
42 * Where should messages be printed before syslog is opened?
43 * Not used if OPENVPN_DEBUG_COMMAND_LINE is defined.
45 #define OPENVPN_MSG_FP stdout
46 #define OPENVPN_ERROR_FP stderr
49 * Exit status codes
52 #define OPENVPN_EXIT_STATUS_GOOD 0
53 #define OPENVPN_EXIT_STATUS_ERROR 1
54 #define OPENVPN_EXIT_STATUS_USAGE 1
55 #define OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE 1
58 * Special command line debugging mode.
59 * If OPENVPN_DEBUG_COMMAND_LINE
60 * is defined, contents of argc/argv will
61 * be dumped to OPENVPN_DEBUG_FILE as well
62 * as all other OpenVPN messages.
65 /* #define OPENVPN_DEBUG_COMMAND_LINE */
66 #define OPENVPN_DEBUG_FILE PACKAGE ".log"
68 /* String and Error functions */
70 #ifdef WIN32
71 # define openvpn_errno() GetLastError()
72 # define openvpn_errno_socket() WSAGetLastError()
73 # define openvpn_strerror(e, gc) strerror_win32(e, gc)
74 const char *strerror_win32 (DWORD errnum, struct gc_arena *gc);
75 #else
76 # define openvpn_errno() errno
77 # define openvpn_errno_socket() errno
78 # define openvpn_strerror(x, gc) strerror(x)
79 #endif
82 * These globals should not be accessed directly,
83 * but rather through macros or inline functions defined below.
85 extern unsigned int x_debug_level;
86 extern int x_msg_line_num;
88 /* msg() flags */
90 #define M_DEBUG_LEVEL (0x0F) /* debug level mask */
92 #define M_FATAL (1<<4) /* exit program */
93 #define M_NONFATAL (1<<5) /* non-fatal error */
94 #define M_WARN (1<<6) /* call syslog with LOG_WARNING */
95 #define M_DEBUG (1<<7)
97 #define M_ERRNO (1<<8) /* show errno description */
98 #define M_ERRNO_SOCK (1<<9) /* show socket errno description */
99 #define M_SSL (1<<10) /* show SSL error */
100 #define M_NOMUTE (1<<11) /* don't do mute processing */
101 #define M_NOPREFIX (1<<12) /* don't show date/time prefix */
102 #define M_USAGE_SMALL (1<<13) /* fatal options error, call usage_small */
103 #define M_MSG_VIRT_OUT (1<<14) /* output message through msg_status_output callback */
104 #define M_OPTERR (1<<15) /* print "Options error:" prefix */
105 #define M_NOLF (1<<16) /* don't print new line */
106 #define M_NOIPREFIX (1<<17) /* don't print instance prefix */
108 /* flag combinations which are frequently used */
109 #define M_ERR (M_FATAL | M_ERRNO)
110 #define M_SOCKERR (M_FATAL | M_ERRNO_SOCK)
111 #define M_SSLERR (M_FATAL | M_SSL)
112 #define M_USAGE (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR)
113 #define M_CLIENT (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX)
116 * Mute levels are designed to avoid large numbers of
117 * mostly similar messages clogging the log file.
119 * A mute level of 0 is always printed.
121 #define MUTE_LEVEL_SHIFT 24
122 #define MUTE_LEVEL_MASK 0xFF
124 #define ENCODE_MUTE_LEVEL(mute_level) (((mute_level) & MUTE_LEVEL_MASK) << MUTE_LEVEL_SHIFT)
125 #define DECODE_MUTE_LEVEL(flags) (((flags) >> MUTE_LEVEL_SHIFT) & MUTE_LEVEL_MASK)
128 * log_level: verbosity level n (--verb n) must be >= log_level to print.
129 * mute_level: don't print more than n (--mute n) consecutive messages at
130 * a given mute level, or if 0 disable muting and print everything.
132 * Mask map:
133 * Bits 0-3: log level
134 * Bits 4-23: M_x flags
135 * Bits 24-31: mute level
137 #define LOGLEV(log_level, mute_level, other) ((log_level) | ENCODE_MUTE_LEVEL(mute_level) | other)
140 * If compiler supports variable arguments in macros, define
141 * msg() as a macro for optimization win.
144 bool dont_mute (unsigned int flags); /* check muting filter */
146 #define MSG_TEST(flags) (unlikely((((unsigned int)flags) & M_DEBUG_LEVEL) <= x_debug_level) && dont_mute (flags))
148 #if defined(HAVE_CPP_VARARG_MACRO_ISO) && !defined(__LCLINT__)
149 # define HAVE_VARARG_MACROS
150 # define msg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); } while (false)
151 # ifdef ENABLE_DEBUG
152 # define dmsg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); } while (false)
153 # else
154 # define dmsg(flags, ...)
155 # endif
156 #elif defined(HAVE_CPP_VARARG_MACRO_GCC) && !defined(__LCLINT__)
157 # define HAVE_VARARG_MACROS
158 # define msg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); } while (false)
159 # ifdef ENABLE_DEBUG
160 # define dmsg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); } while (false)
161 # else
162 # define dmsg(flags, args...)
163 # endif
164 #else
165 # if !PEDANTIC
166 # ifdef _MSC_VER
167 # pragma message("this compiler appears to lack vararg macros which will cause a significant degradation in efficiency")
168 # else
169 # warning this compiler appears to lack vararg macros which will cause a significant degradation in efficiency (you can ignore this warning if you are using LCLINT)
170 # endif
171 # endif
172 # define msg x_msg
173 # define dmsg x_msg
174 #endif
176 void x_msg (const unsigned int flags, const char *format, ...)
177 #ifdef __GNUC__
178 __attribute__ ((format (printf, 2, 3)))
179 #endif
180 ; /* should be called via msg above */
183 * Function prototypes
186 void error_reset (void);
188 /* route errors to stderr that would normally go to stdout */
189 void errors_to_stderr (void);
191 void set_suppress_timestamps (bool suppressed);
193 #define SDL_CONSTRAIN (1<<0)
194 bool set_debug_level (const int level, const unsigned int flags);
196 bool set_mute_cutoff (const int cutoff);
198 int get_debug_level (void);
199 int get_mute_cutoff (void);
201 const char *msg_flags_string (const unsigned int flags, struct gc_arena *gc);
204 * File to print messages to before syslog is opened.
206 FILE *msg_fp(const unsigned int flags);
208 /* Fatal logic errors */
209 #define ASSERT(x) do { if (!(x)) assert_failed(__FILE__, __LINE__); } while (false)
211 void assert_failed (const char *filename, int line);
213 #ifdef ENABLE_DEBUG
214 void crash (void); /* force a segfault (debugging only) */
215 #endif
217 /* Inline functions */
219 static inline bool
220 check_debug_level (unsigned int level)
222 return (level & M_DEBUG_LEVEL) <= x_debug_level;
225 /* Call if we forked */
226 void msg_forked (void);
228 /* syslog output */
230 void open_syslog (const char *pgmname, bool stdio_to_null);
231 void close_syslog ();
233 /* log file output */
234 void redirect_stdout_stderr (const char *file, bool append);
236 #ifdef WIN32
237 /* get original stderr handle, even if redirected by --log/--log-append */
238 HANDLE get_orig_stderr (void);
239 #endif
241 /* exit program */
242 void openvpn_exit (const int status);
245 * Check the return status of read/write routines.
248 struct link_socket;
249 struct tuntap;
251 extern unsigned int x_cs_info_level;
252 extern unsigned int x_cs_verbose_level;
253 extern unsigned int x_cs_err_delay_ms;
255 void reset_check_status (void);
256 void set_check_status (unsigned int info_level, unsigned int verbose_level);
258 void x_check_status (int status,
259 const char *description,
260 struct link_socket *sock,
261 struct tuntap *tt);
263 static inline void
264 check_status (int status, const char *description, struct link_socket *sock, struct tuntap *tt)
266 if (status < 0 || check_debug_level (x_cs_verbose_level))
267 x_check_status (status, description, sock, tt);
270 static inline void
271 set_check_status_error_delay (unsigned int milliseconds)
273 x_cs_err_delay_ms = milliseconds;
277 * In multiclient mode, put a client-specific prefix
278 * before each message.
280 * TODO: x_msg_prefix should be thread-local
283 extern const char *x_msg_prefix;
285 #ifdef USE_PTHREAD
286 extern pthread_key_t x_msg_prefix_key;
287 #endif
289 void msg_thread_init (void);
290 void msg_thread_uninit (void);
292 static inline void
293 msg_set_prefix (const char *prefix)
295 #ifdef USE_PTHREAD
296 if (openvpn_thread_enabled ())
298 ASSERT (!pthread_setspecific (x_msg_prefix_key, prefix));
300 else
301 #endif
302 x_msg_prefix = prefix;
305 static inline const char *
306 msg_get_prefix (void)
308 #ifdef USE_PTHREAD
309 if (openvpn_thread_enabled ())
310 return (const char *) pthread_getspecific (x_msg_prefix_key);
311 else
312 #endif
313 return x_msg_prefix;
317 * Allow MSG to be redirected through a virtual_output object
320 struct virtual_output;
322 extern const struct virtual_output *x_msg_virtual_output;
324 static inline void
325 msg_set_virtual_output (const struct virtual_output *vo)
327 x_msg_virtual_output = vo;
330 static inline const struct virtual_output *
331 msg_get_virtual_output (void)
333 return x_msg_virtual_output;
337 * Return true if this is a system error
338 * which can be safely ignored.
340 static inline bool
341 ignore_sys_error (const int err)
343 /* I/O operation pending */
344 #ifdef WIN32
345 if (err == WSAEWOULDBLOCK || err == WSAEINVAL)
346 return true;
347 #else
348 if (err == EAGAIN)
349 return true;
350 #endif
352 #if 0 /* if enabled, suppress ENOBUFS errors */
353 #ifdef ENOBUFS
354 /* No buffer space available */
355 if (err == ENOBUFS)
356 return true;
357 #endif
358 #endif
360 if (err == EINTR)
361 return true;
363 return false;
366 #include "errlevel.h"
368 #endif