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-2005 OpenVPN Solutions LLC <info@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 */
33 #define ERR_BUF_SIZE 1024
38 * Where should messages be printed before syslog is opened?
39 * Not used if OPENVPN_DEBUG_COMMAND_LINE is defined.
41 #define OPENVPN_MSG_FP stdout
47 #define OPENVPN_EXIT_STATUS_GOOD 0
48 #define OPENVPN_EXIT_STATUS_ERROR 1
49 #define OPENVPN_EXIT_STATUS_USAGE 1
50 #define OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE 1
53 * Special command line debugging mode.
54 * If OPENVPN_DEBUG_COMMAND_LINE
55 * is defined, contents of argc/argv will
56 * be dumped to OPENVPN_DEBUG_FILE as well
57 * as all other OpenVPN messages.
60 /* #define OPENVPN_DEBUG_COMMAND_LINE */
61 #define OPENVPN_DEBUG_FILE PACKAGE ".log"
63 /* String and Error functions */
66 # define openvpn_errno() GetLastError()
67 # define openvpn_errno_socket() WSAGetLastError()
68 # define openvpn_strerror(e, gc) strerror_win32(e, gc)
69 const char *strerror_win32 (DWORD errnum
, struct gc_arena
*gc
);
71 # define openvpn_errno() errno
72 # define openvpn_errno_socket() errno
73 # define openvpn_strerror(x, gc) strerror(x)
77 * These globals should not be accessed directly,
78 * but rather through macros or inline functions defined below.
80 extern unsigned int x_debug_level
;
81 extern int x_msg_line_num
;
85 #define M_DEBUG_LEVEL (0x0F) /* debug level mask */
87 #define M_FATAL (1<<4) /* exit program */
88 #define M_NONFATAL (1<<5) /* non-fatal error */
89 #define M_WARN (1<<6) /* call syslog with LOG_WARNING */
90 #define M_DEBUG (1<<7)
92 #define M_ERRNO (1<<8) /* show errno description */
93 #define M_ERRNO_SOCK (1<<9) /* show socket errno description */
94 #define M_SSL (1<<10) /* show SSL error */
95 #define M_NOMUTE (1<<11) /* don't do mute processing */
96 #define M_NOPREFIX (1<<12) /* don't show date/time prefix */
97 #define M_USAGE_SMALL (1<<13) /* fatal options error, call usage_small */
98 #define M_MSG_VIRT_OUT (1<<14) /* output message through msg_status_output callback */
99 #define M_OPTERR (1<<15) /* print "Options error:" prefix */
101 /* flag combinations which are frequently used */
102 #define M_ERR (M_FATAL | M_ERRNO)
103 #define M_SOCKERR (M_FATAL | M_ERRNO_SOCK)
104 #define M_SSLERR (M_FATAL | M_SSL)
105 #define M_USAGE (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR)
106 #define M_CLIENT (M_MSG_VIRT_OUT|M_NOMUTE)
109 * Mute levels are designed to avoid large numbers of
110 * mostly similar messages clogging the log file.
112 * A mute level of 0 is always printed.
114 #define MUTE_LEVEL_SHIFT 16
115 #define MUTE_LEVEL_MASK 0xFF
117 #define ENCODE_MUTE_LEVEL(mute_level) (((mute_level) & MUTE_LEVEL_MASK) << MUTE_LEVEL_SHIFT)
118 #define DECODE_MUTE_LEVEL(flags) (((flags) >> MUTE_LEVEL_SHIFT) & MUTE_LEVEL_MASK)
121 * log_level: verbosity level n (--verb n) must be >= log_level to print.
122 * mute_level: don't print more than n (--mute n) consecutive messages at
123 * a given mute level, or if 0 disable muting and print everything.
125 #define LOGLEV(log_level, mute_level, other) ((log_level) | ENCODE_MUTE_LEVEL(mute_level) | other)
128 * If compiler supports variable arguments in macros, define
129 * msg() as a macro for optimization win.
132 bool dont_mute (unsigned int flags
); /* check muting filter */
134 #define MSG_TEST(flags) (((((unsigned int)flags) & M_DEBUG_LEVEL) <= x_debug_level) && dont_mute (flags))
136 #if defined(HAVE_CPP_VARARG_MACRO_ISO) && !defined(__LCLINT__)
137 # define HAVE_VARARG_MACROS
138 # define msg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); } while (false)
140 # define dmsg(flags, ...) do { if (MSG_TEST(flags)) x_msg((flags), __VA_ARGS__); } while (false)
142 # define dmsg(flags, ...)
144 #elif defined(HAVE_CPP_VARARG_MACRO_GCC) && !defined(__LCLINT__)
145 # define HAVE_VARARG_MACROS
146 # define msg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); } while (false)
148 # define dmsg(flags, args...) do { if (MSG_TEST(flags)) x_msg((flags), args); } while (false)
150 # define dmsg(flags, args...)
155 # pragma message("this compiler appears to lack vararg macros which will cause a significant degradation in efficiency")
157 # 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)
164 void x_msg (const unsigned int flags
, const char *format
, ...)
166 __attribute__ ((format (printf
, 2, 3)))
168 ; /* should be called via msg above */
171 * Function prototypes
174 void error_reset (void);
175 void set_suppress_timestamps (bool suppressed
);
177 #define SDL_CONSTRAIN (1<<0)
178 bool set_debug_level (const int level
, const unsigned int flags
);
180 bool set_mute_cutoff (const int cutoff
);
182 int get_debug_level (void);
183 int get_mute_cutoff (void);
185 const char *msg_flags_string (const unsigned int flags
, struct gc_arena
*gc
);
188 * File to print messages to before syslog is opened.
192 /* Fatal logic errors */
193 #define ASSERT(x) do { if (!(x)) assert_failed(__FILE__, __LINE__); } while (false)
195 void assert_failed (const char *filename
, int line
);
197 /* Inline functions */
200 check_debug_level (unsigned int level
)
202 return (level
& M_DEBUG_LEVEL
) <= x_debug_level
;
207 void open_syslog (const char *pgmname
, bool stdio_to_null
);
208 void close_syslog ();
210 /* log file output */
211 void redirect_stdout_stderr (const char *file
, bool append
);
214 /* get original stderr handle, even if redirected by --log/--log-append */
215 HANDLE
get_orig_stderr (void);
219 void openvpn_exit (const int status
);
222 * Check the return status of read/write routines.
228 extern unsigned int x_cs_info_level
;
229 extern unsigned int x_cs_verbose_level
;
230 extern unsigned int x_cs_err_delay_ms
;
232 void reset_check_status (void);
233 void set_check_status (unsigned int info_level
, unsigned int verbose_level
);
235 void x_check_status (int status
,
236 const char *description
,
237 struct link_socket
*sock
,
241 check_status (int status
, const char *description
, struct link_socket
*sock
, struct tuntap
*tt
)
243 if (status
< 0 || check_debug_level (x_cs_verbose_level
))
244 x_check_status (status
, description
, sock
, tt
);
248 set_check_status_error_delay (unsigned int milliseconds
)
250 x_cs_err_delay_ms
= milliseconds
;
254 * In multiclient mode, put a client-specific prefix
255 * before each message.
257 * TODO: x_msg_prefix should be thread-local
260 extern const char *x_msg_prefix
;
263 extern pthread_key_t x_msg_prefix_key
;
266 void msg_thread_init (void);
267 void msg_thread_uninit (void);
270 msg_set_prefix (const char *prefix
)
273 if (openvpn_thread_enabled ())
275 ASSERT (!pthread_setspecific (x_msg_prefix_key
, prefix
));
279 x_msg_prefix
= prefix
;
282 static inline const char *
283 msg_get_prefix (void)
286 if (openvpn_thread_enabled ())
287 return (const char *) pthread_getspecific (x_msg_prefix_key
);
294 * Allow MSG to be redirected through a virtual_output object
297 struct virtual_output
;
299 extern const struct virtual_output
*x_msg_virtual_output
;
302 msg_set_virtual_output (const struct virtual_output
*vo
)
304 x_msg_virtual_output
= vo
;
307 static inline const struct virtual_output
*
308 msg_get_virtual_output (void)
310 return x_msg_virtual_output
;
314 * Return true if this is a system error
315 * which can be safely ignored.
318 ignore_sys_error (const int err
)
320 /* I/O operation pending */
322 if (err
== WSAEWOULDBLOCK
|| err
== WSAEINVAL
)
329 #if 0 /* if enabled, suppress ENOBUFS errors */
331 /* No buffer space available */
340 #include "errlevel.h"