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
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
31 /* #define ABORT_ON_ERROR */
34 #define ERR_BUF_SIZE 8192
36 #define ERR_BUF_SIZE 1280
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
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 */
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
);
76 # define openvpn_errno() errno
77 # define openvpn_errno_socket() errno
78 # define openvpn_strerror(x, gc) strerror(x)
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
;
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.
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)
152 # define dmsg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); } while (false)
154 # define dmsg(flags, ...)
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)
160 # define dmsg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); } while (false)
162 # define dmsg(flags, args...)
167 # pragma message("this compiler appears to lack vararg macros which will cause a significant degradation in efficiency")
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)
176 void x_msg (const unsigned int flags
, const char *format
, ...)
178 __attribute__ ((format (printf
, 2, 3)))
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
);
214 void crash (void); /* force a segfault (debugging only) */
217 /* Inline functions */
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);
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
);
237 /* get original stderr handle, even if redirected by --log/--log-append */
238 HANDLE
get_orig_stderr (void);
242 void openvpn_exit (const int status
);
245 * Check the return status of read/write routines.
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
,
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
);
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
;
286 extern pthread_key_t x_msg_prefix_key
;
289 void msg_thread_init (void);
290 void msg_thread_uninit (void);
293 msg_set_prefix (const char *prefix
)
296 if (openvpn_thread_enabled ())
298 ASSERT (!pthread_setspecific (x_msg_prefix_key
, prefix
));
302 x_msg_prefix
= prefix
;
305 static inline const char *
306 msg_get_prefix (void)
309 if (openvpn_thread_enabled ())
310 return (const char *) pthread_getspecific (x_msg_prefix_key
);
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
;
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.
341 ignore_sys_error (const int err
)
343 /* I/O operation pending */
345 if (err
== WSAEWOULDBLOCK
|| err
== WSAEINVAL
)
352 #if 0 /* if enabled, suppress ENOBUFS errors */
354 /* No buffer space available */
366 #include "errlevel.h"