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.
7 *
8 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
9 *
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.
13 *
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.
18 *
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
23 */
24
25#ifndef ERROR_H
26#define ERROR_H
27
28#include "basic.h"
29
30/* #define ABORT_ON_ERROR */
31
32#ifdef ENABLE_PKCS11
33#define ERR_BUF_SIZE 8192
34#else
35#define ERR_BUF_SIZE 1280
36#endif
37
38struct gc_arena;
39
40/*
41 * Where should messages be printed before syslog is opened?
42 * Not used if OPENVPN_DEBUG_COMMAND_LINE is defined.
43 */
44#define OPENVPN_MSG_FP   stdout
45#define OPENVPN_ERROR_FP stderr
46
47/*
48 * Exit status codes
49 */
50
51#define OPENVPN_EXIT_STATUS_GOOD                    0
52#define OPENVPN_EXIT_STATUS_ERROR                   1
53#define OPENVPN_EXIT_STATUS_USAGE                   1
54#define OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE  1
55
56/*
57 * Special command line debugging mode.
58 * If OPENVPN_DEBUG_COMMAND_LINE
59 * is defined, contents of argc/argv will
60 * be dumped to OPENVPN_DEBUG_FILE as well
61 * as all other OpenVPN messages.
62 */
63
64/* #define OPENVPN_DEBUG_COMMAND_LINE */
65#define OPENVPN_DEBUG_FILE PACKAGE ".log"
66
67/* String and Error functions */
68
69#ifdef WIN32
70# define openvpn_errno()             GetLastError()
71# define openvpn_strerror(e, gc)     strerror_win32(e, gc)
72  const char *strerror_win32 (DWORD errnum, struct gc_arena *gc);
73#else
74# define openvpn_errno()             errno
75# define openvpn_strerror(x, gc)     strerror(x)
76#endif
77
78/*
79 * These globals should not be accessed directly,
80 * but rather through macros or inline functions defined below.
81 */
82extern unsigned int x_debug_level;
83extern int x_msg_line_num;
84
85/* msg() flags */
86
87#define M_DEBUG_LEVEL     (0x0F)	 /* debug level mask */
88
89#define M_FATAL           (1<<4)	 /* exit program */
90#define M_NONFATAL        (1<<5)	 /* non-fatal error */
91#define M_WARN	          (1<<6)	 /* call syslog with LOG_WARNING */
92#define M_DEBUG           (1<<7)
93
94#define M_ERRNO           (1<<8)	 /* show errno description */
95
96#ifdef ENABLE_CRYPTO_OPENSSL
97#  define M_SSL             (1<<10)	 /* show SSL error */
98#endif
99
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 */
107
108/* flag combinations which are frequently used */
109#define M_ERR     (M_FATAL | M_ERRNO)
110#define M_SSLERR  (M_FATAL | M_SSL)
111#define M_USAGE   (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR)
112#define M_CLIENT  (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX)
113
114/*
115 * Mute levels are designed to avoid large numbers of
116 * mostly similar messages clogging the log file.
117 *
118 * A mute level of 0 is always printed.
119 */
120#define MUTE_LEVEL_SHIFT 24
121#define MUTE_LEVEL_MASK 0xFF
122
123#define ENCODE_MUTE_LEVEL(mute_level) (((mute_level) & MUTE_LEVEL_MASK) << MUTE_LEVEL_SHIFT)
124#define DECODE_MUTE_LEVEL(flags) (((flags) >> MUTE_LEVEL_SHIFT) & MUTE_LEVEL_MASK)
125
126/*
127 * log_level:  verbosity level n (--verb n) must be >= log_level to print.
128 * mute_level: don't print more than n (--mute n) consecutive messages at
129 *             a given mute level, or if 0 disable muting and print everything.
130 *
131 * Mask map:
132 * Bits 0-3:   log level
133 * Bits 4-23:  M_x flags
134 * Bits 24-31: mute level
135 */
136#define LOGLEV(log_level, mute_level, other) ((log_level) | ENCODE_MUTE_LEVEL(mute_level) | other)
137
138/*
139 * If compiler supports variable arguments in macros, define
140 * msg() as a macro for optimization win.
141 */
142
143bool dont_mute (unsigned int flags); /* check muting filter */
144
145#define MSG_TEST(flags) (unlikely((((unsigned int)flags) & M_DEBUG_LEVEL) <= x_debug_level) && dont_mute (flags))
146
147#if defined(HAVE_CPP_VARARG_MACRO_ISO) && !defined(__LCLINT__)
148# define HAVE_VARARG_MACROS
149# define msg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); } while (false)
150# ifdef ENABLE_DEBUG
151#  define dmsg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); } while (false)
152# else
153#  define dmsg(flags, ...)
154# endif
155#elif defined(HAVE_CPP_VARARG_MACRO_GCC) && !defined(__LCLINT__)
156# define HAVE_VARARG_MACROS
157# define msg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); } while (false)
158# ifdef ENABLE_DEBUG
159#  define dmsg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); } while (false)
160# else
161#  define dmsg(flags, args...)
162# endif
163#else
164# if !PEDANTIC
165#  ifdef _MSC_VER
166#   pragma message("this compiler appears to lack vararg macros which will cause a significant degradation in efficiency")
167#  else
168#   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)
169#  endif
170# endif
171# define msg x_msg
172# define dmsg x_msg
173#endif
174
175void x_msg (const unsigned int flags, const char *format, ...)
176#ifdef __GNUC__
177#if __USE_MINGW_ANSI_STDIO
178	__attribute__ ((format (gnu_printf, 2, 3)))
179#else
180	__attribute__ ((format (__printf__, 2, 3)))
181#endif
182#endif
183    ; /* should be called via msg above */
184
185void x_msg_va (const unsigned int flags, const char *format, va_list arglist);
186
187/*
188 * Function prototypes
189 */
190
191void error_reset (void);
192
193/* route errors to stderr that would normally go to stdout */
194void errors_to_stderr (void);
195
196void set_suppress_timestamps (bool suppressed);
197
198#define SDL_CONSTRAIN (1<<0)
199bool set_debug_level (const int level, const unsigned int flags);
200
201bool set_mute_cutoff (const int cutoff);
202
203int get_debug_level (void);
204int get_mute_cutoff (void);
205
206const char *msg_flags_string (const unsigned int flags, struct gc_arena *gc);
207
208/*
209 * File to print messages to before syslog is opened.
210 */
211FILE *msg_fp(const unsigned int flags);
212
213/* Fatal logic errors */
214#define ASSERT(x) do { if (!(x)) assert_failed(__FILE__, __LINE__); } while (false)
215
216void assert_failed (const char *filename, int line);
217
218#ifdef ENABLE_DEBUG
219void crash (void); /* force a segfault (debugging only) */
220#endif
221
222/* Inline functions */
223
224static inline bool
225check_debug_level (unsigned int level)
226{
227  return (level & M_DEBUG_LEVEL) <= x_debug_level;
228}
229
230/* Call if we forked */
231void msg_forked (void);
232
233/* syslog output */
234
235void open_syslog (const char *pgmname, bool stdio_to_null);
236void close_syslog ();
237
238/* log file output */
239void redirect_stdout_stderr (const char *file, bool append);
240
241#ifdef WIN32
242/* get original stderr handle, even if redirected by --log/--log-append */
243HANDLE get_orig_stderr (void);
244#endif
245
246/* exit program */
247void openvpn_exit (const int status);
248
249/* exit program on out of memory error */
250void out_of_memory (void);
251
252/*
253 * Check the return status of read/write routines.
254 */
255
256struct link_socket;
257struct tuntap;
258
259extern unsigned int x_cs_info_level;
260extern unsigned int x_cs_verbose_level;
261extern unsigned int x_cs_err_delay_ms;
262
263void reset_check_status (void);
264void set_check_status (unsigned int info_level, unsigned int verbose_level);
265
266void x_check_status (int status,
267		     const char *description,
268		     struct link_socket *sock,
269		     struct tuntap *tt);
270
271static inline void
272check_status (int status, const char *description, struct link_socket *sock, struct tuntap *tt)
273{
274  if (status < 0 || check_debug_level (x_cs_verbose_level))
275    x_check_status (status, description, sock, tt);
276}
277
278static inline void
279set_check_status_error_delay (unsigned int milliseconds)
280{
281  x_cs_err_delay_ms = milliseconds;
282}
283
284/*
285 * In multiclient mode, put a client-specific prefix
286 * before each message.
287 *
288 * TODO: x_msg_prefix should be thread-local
289 */
290
291extern const char *x_msg_prefix;
292
293void msg_thread_init (void);
294void msg_thread_uninit (void);
295
296static inline void
297msg_set_prefix (const char *prefix)
298{
299    x_msg_prefix = prefix;
300}
301
302static inline const char *
303msg_get_prefix (void)
304{
305    return x_msg_prefix;
306}
307
308/*
309 * Allow MSG to be redirected through a virtual_output object
310 */
311
312struct virtual_output;
313
314extern const struct virtual_output *x_msg_virtual_output;
315
316static inline void
317msg_set_virtual_output (const struct virtual_output *vo)
318{
319  x_msg_virtual_output = vo;
320}
321
322static inline const struct virtual_output *
323msg_get_virtual_output (void)
324{
325  return x_msg_virtual_output;
326}
327
328/*
329 * Return true if this is a system error
330 * which can be safely ignored.
331 */
332static inline bool
333ignore_sys_error (const int err)
334{
335  /* I/O operation pending */
336#ifdef WIN32
337  if (err == WSAEWOULDBLOCK || err == WSAEINVAL)
338    return true;
339#else
340  if (err == EAGAIN)
341    return true;
342#endif
343
344#if 0 /* if enabled, suppress ENOBUFS errors */
345#ifdef ENOBUFS
346  /* No buffer space available */
347  if (err == ENOBUFS)
348    return true;
349#endif
350#endif
351
352  return false;
353}
354
355#include "errlevel.h"
356
357#endif
358