OpenVPN
error.c
Go to the documentation of this file.
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-2023 OpenVPN 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 along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include "syshead.h"
31 
32 #include "error.h"
33 #include "buffer.h"
34 #include "init.h"
35 #include "misc.h"
36 #include "win32.h"
37 #include "socket.h"
38 #include "tun.h"
39 #include "otime.h"
40 #include "perf.h"
41 #include "status.h"
42 #include "integer.h"
43 #include "ps.h"
44 #include "mstats.h"
45 
46 
47 #if SYSLOG_CAPABILITY
48 #ifndef LOG_OPENVPN
49 #define LOG_OPENVPN LOG_DAEMON
50 #endif
51 #endif
52 
53 /* Globals */
54 unsigned int x_debug_level; /* GLOBAL */
55 
56 /* Mute state */
57 static int mute_cutoff; /* GLOBAL */
58 static int mute_count; /* GLOBAL */
59 static int mute_category; /* GLOBAL */
60 
61 /*
62  * Output mode priorities are as follows:
63  *
64  * (1) --log-x overrides everything
65  * (2) syslog is used if --daemon is defined and not --log-x
66  * (3) if OPENVPN_DEBUG_COMMAND_LINE is defined, output
67  * to constant logfile name.
68  * (4) Output to stdout.
69  */
70 
71 /* If true, indicates that stdin/stdout/stderr
72  * have been redirected due to --log */
73 static bool std_redir; /* GLOBAL */
74 
75 /* Should messages be written to the syslog? */
76 static bool use_syslog; /* GLOBAL */
77 
78 /* Should stdout/stderr be be parsable and always be prefixed with time
79  * and message flags */
80 static bool machine_readable_output; /* GLOBAL */
81 
82 /* Should timestamps be included on messages to stdout/stderr? */
83 static bool suppress_timestamps; /* GLOBAL */
84 
85 /* The program name passed to syslog */
86 #if SYSLOG_CAPABILITY
87 static char *pgmname_syslog; /* GLOBAL */
88 #endif
89 
90 /* If non-null, messages should be written here (used for debugging only) */
91 static FILE *msgfp; /* GLOBAL */
92 
93 /* If true, we forked from main OpenVPN process */
94 static bool forked; /* GLOBAL */
95 
96 /* our default output targets */
97 static FILE *default_out; /* GLOBAL */
98 static FILE *default_err; /* GLOBAL */
99 
100 void
102 {
103  forked = true;
104 }
105 
106 bool
107 set_debug_level(const int level, const unsigned int flags)
108 {
109  const int ceiling = 15;
110 
111  if (level >= 0 && level <= ceiling)
112  {
113  x_debug_level = level;
114  return true;
115  }
116  else if (flags & SDL_CONSTRAIN)
117  {
118  x_debug_level = constrain_int(level, 0, ceiling);
119  return true;
120  }
121  return false;
122 }
123 
124 bool
125 set_mute_cutoff(const int cutoff)
126 {
127  if (cutoff >= 0)
128  {
129  mute_cutoff = cutoff;
130  return true;
131  }
132  else
133  {
134  return false;
135  }
136 }
137 
138 int
140 {
141  return x_debug_level;
142 }
143 
144 int
146 {
147  return mute_cutoff;
148 }
149 
150 void
151 set_suppress_timestamps(bool suppressed)
152 {
153  suppress_timestamps = suppressed;
154 }
155 
156 void
158 {
159  machine_readable_output = parsable;
160 }
161 
162 void
164 {
165  use_syslog = std_redir = false;
166  suppress_timestamps = false;
167  machine_readable_output = false;
168  x_debug_level = 1;
169  mute_cutoff = 0;
170  mute_count = 0;
171  mute_category = 0;
174 
175 #ifdef OPENVPN_DEBUG_COMMAND_LINE
176  msgfp = fopen(OPENVPN_DEBUG_FILE, "w");
177  if (!msgfp)
178  {
180  }
181 #else /* ifdef OPENVPN_DEBUG_COMMAND_LINE */
182  msgfp = NULL;
183 #endif
184 }
185 
186 void
188 {
190 }
191 
192 /*
193  * Return a file to print messages to before syslog is opened.
194  */
195 FILE *
196 msg_fp(const unsigned int flags)
197 {
198  FILE *fp = msgfp;
199  if (!fp)
200  {
201  fp = (flags & (M_FATAL|M_USAGE_SMALL)) ? default_err : default_out;
202  }
203  if (!fp)
204  {
206  }
207  return fp;
208 }
209 
210 #define SWAP { tmp = m1; m1 = m2; m2 = tmp; }
211 
212 int x_msg_line_num; /* GLOBAL */
213 
214 void
215 x_msg(const unsigned int flags, const char *format, ...)
216 {
217  va_list arglist;
218  va_start(arglist, format);
219  x_msg_va(flags, format, arglist);
220  va_end(arglist);
221 }
222 
223 static const char *
224 openvpn_strerror(int err, bool crt_error, struct gc_arena *gc)
225 {
226 #ifdef _WIN32
227  if (!crt_error)
228  {
229  return strerror_win32(err, gc);
230  }
231 #endif
232  return strerror(err);
233 }
234 
235 void
236 x_msg_va(const unsigned int flags, const char *format, va_list arglist)
237 {
238  struct gc_arena gc;
239 #if SYSLOG_CAPABILITY
240  int level;
241 #endif
242  char *m1;
243  char *m2;
244  char *tmp;
245  int e;
246  const char *prefix;
247  const char *prefix_sep;
248 
249  void usage_small(void);
250 
251  /* the macro has checked this otherwise */
252  if (!msg_test(flags))
253  {
254  return;
255  }
256 
257  bool crt_error = false;
258  e = openvpn_errno_maybe_crt(&crt_error);
259 
260  /*
261  * Apply muting filter.
262  */
263  /* the macro has checked this otherwise */
264  if (!dont_mute(flags))
265  {
266  return;
267  }
268 
269  gc_init(&gc);
270 
271  m1 = (char *) gc_malloc(ERR_BUF_SIZE, false, &gc);
272  m2 = (char *) gc_malloc(ERR_BUF_SIZE, false, &gc);
273 
274  vsnprintf(m1, ERR_BUF_SIZE, format, arglist);
275  m1[ERR_BUF_SIZE - 1] = 0; /* windows vsnprintf needs this */
276 
277  if ((flags & M_ERRNO) && e)
278  {
279  openvpn_snprintf(m2, ERR_BUF_SIZE, "%s: %s (errno=%d)",
280  m1, openvpn_strerror(e, crt_error, &gc), e);
281  SWAP;
282  }
283 
284  if (flags & M_OPTERR)
285  {
286  openvpn_snprintf(m2, ERR_BUF_SIZE, "Options error: %s", m1);
287  SWAP;
288  }
289 
290 #if SYSLOG_CAPABILITY
291  if (flags & (M_FATAL|M_NONFATAL|M_USAGE_SMALL))
292  {
293  level = LOG_ERR;
294  }
295  else if (flags & M_WARN)
296  {
297  level = LOG_WARNING;
298  }
299  else
300  {
301  level = LOG_NOTICE;
302  }
303 #endif
304 
305  /* set up client prefix */
306  if (flags & M_NOIPREFIX)
307  {
308  prefix = NULL;
309  }
310  else
311  {
312  prefix = msg_get_prefix();
313  }
314  prefix_sep = " ";
315  if (!prefix)
316  {
317  prefix_sep = prefix = "";
318  }
319 
320  /* virtual output capability used to copy output to management subsystem */
321  if (!forked)
322  {
323  const struct virtual_output *vo = msg_get_virtual_output();
324  if (vo)
325  {
326  openvpn_snprintf(m2, ERR_BUF_SIZE, "%s%s%s",
327  prefix,
328  prefix_sep,
329  m1);
330  virtual_output_print(vo, flags, m2);
331  }
332  }
333 
334  if (!(flags & M_MSG_VIRT_OUT))
335  {
336  if (use_syslog && !std_redir && !forked)
337  {
338 #if SYSLOG_CAPABILITY
339  syslog(level, "%s%s%s",
340  prefix,
341  prefix_sep,
342  m1);
343 #endif
344  }
345  else
346  {
347  FILE *fp = msg_fp(flags);
348  const bool show_usec = check_debug_level(DEBUG_LEVEL_USEC_TIME);
349 
351  {
352  struct timeval tv;
353  gettimeofday(&tv, NULL);
354 
355  fprintf(fp, "%" PRIi64 ".%06ld %x %s%s%s%s",
356  (int64_t)tv.tv_sec,
357  (long)tv.tv_usec,
358  flags,
359  prefix,
360  prefix_sep,
361  m1,
362  "\n");
363 
364  }
365  else if ((flags & M_NOPREFIX) || suppress_timestamps)
366  {
367  fprintf(fp, "%s%s%s%s",
368  prefix,
369  prefix_sep,
370  m1,
371  (flags&M_NOLF) ? "" : "\n");
372  }
373  else
374  {
375  fprintf(fp, "%s %s%s%s%s",
376  time_string(0, 0, show_usec, &gc),
377  prefix,
378  prefix_sep,
379  m1,
380  (flags&M_NOLF) ? "" : "\n");
381  }
382  fflush(fp);
383  ++x_msg_line_num;
384  }
385  }
386 
387  if (flags & M_FATAL)
388  {
389  msg(M_INFO, "Exiting due to fatal error");
390  }
391 
392  if (flags & M_FATAL)
393  {
394  openvpn_exit(OPENVPN_EXIT_STATUS_ERROR); /* exit point */
395 
396  }
397  if (flags & M_USAGE_SMALL)
398  {
399  usage_small();
400  }
401 
402  gc_free(&gc);
403 }
404 
405 /*
406  * Apply muting filter.
407  */
408 bool
409 dont_mute(unsigned int flags)
410 {
411  bool ret = true;
412  if (mute_cutoff > 0 && !(flags & M_NOMUTE))
413  {
414  const int mute_level = DECODE_MUTE_LEVEL(flags);
415  if (mute_level > 0 && mute_level == mute_category)
416  {
417  if (mute_count == mute_cutoff)
418  {
419  msg(M_INFO | M_NOMUTE, "NOTE: --mute triggered...");
420  }
421  if (++mute_count > mute_cutoff)
422  {
423  ret = false;
424  }
425  }
426  else
427  {
428  const int suppressed = mute_count - mute_cutoff;
429  if (suppressed > 0)
430  {
431  msg(M_INFO | M_NOMUTE,
432  "%d variation(s) on previous %d message(s) suppressed by --mute",
433  suppressed,
434  mute_cutoff);
435  }
436  mute_count = 1;
437  mute_category = mute_level;
438  }
439  }
440  return ret;
441 }
442 
443 void
444 assert_failed(const char *filename, int line, const char *condition)
445 {
446  if (condition)
447  {
448  msg(M_FATAL, "Assertion failed at %s:%d (%s)", filename, line, condition);
449  }
450  else
451  {
452  msg(M_FATAL, "Assertion failed at %s:%d", filename, line);
453  }
454  _exit(1);
455 }
456 
457 /*
458  * Fail memory allocation. Don't use msg() because it tries
459  * to allocate memory as part of its operation.
460  */
461 void
463 {
464  fprintf(stderr, PACKAGE_NAME ": Out of Memory\n");
465  exit(1);
466 }
467 
468 void
469 open_syslog(const char *pgmname, bool stdio_to_null)
470 {
471 #if SYSLOG_CAPABILITY
472  if (!msgfp && !std_redir)
473  {
474  if (!use_syslog)
475  {
476  pgmname_syslog = string_alloc(pgmname ? pgmname : PACKAGE, NULL);
477  openlog(pgmname_syslog, LOG_PID, LOG_OPENVPN);
478  use_syslog = true;
479 
480  /* Better idea: somehow pipe stdout/stderr output to msg() */
481  if (stdio_to_null)
482  {
483  set_std_files_to_null(false);
484  }
485  }
486  }
487 #else /* if SYSLOG_CAPABILITY */
488  msg(M_WARN, "Warning on use of --daemon: this operating system lacks daemon logging features, therefore when I become a daemon, I won't be able to log status or error messages");
489 #endif
490 }
491 
492 void
494 {
495 #if SYSLOG_CAPABILITY
496  if (use_syslog)
497  {
498  closelog();
499  use_syslog = false;
500  free(pgmname_syslog);
501  pgmname_syslog = NULL;
502  }
503 #endif
504 }
505 
506 #ifdef _WIN32
507 static int orig_stderr;
508 
509 int
511 {
512  return orig_stderr ? orig_stderr : _fileno(stderr);
513 }
514 #endif
515 
516 void
517 redirect_stdout_stderr(const char *file, bool append)
518 {
519 #if defined(_WIN32)
520  if (!std_redir)
521  {
522  struct gc_arena gc = gc_new();
523  HANDLE log_handle;
524  int log_fd;
525 
526  SECURITY_ATTRIBUTES saAttr;
527  saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
528  saAttr.bInheritHandle = TRUE;
529  saAttr.lpSecurityDescriptor = NULL;
530 
531  log_handle = CreateFileW(wide_string(file, &gc),
532  GENERIC_WRITE,
533  FILE_SHARE_READ,
534  &saAttr,
535  append ? OPEN_ALWAYS : CREATE_ALWAYS,
536  FILE_ATTRIBUTE_NORMAL,
537  NULL);
538 
539  gc_free(&gc);
540 
541  if (log_handle == INVALID_HANDLE_VALUE)
542  {
543  msg(M_WARN|M_ERRNO, "Warning: cannot open --log file: %s", file);
544  return;
545  }
546 
547  /* append to logfile? */
548  if (append)
549  {
550  if (SetFilePointer(log_handle, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
551  {
552  msg(M_ERR, "Error: cannot seek to end of --log file: %s", file);
553  }
554  }
555 
556  /* save original stderr for password prompts */
557  orig_stderr = _dup(_fileno(stderr));
558  if (orig_stderr == -1)
559  {
560  msg(M_WARN | M_ERRNO, "Warning: cannot duplicate stderr, password prompts will appear in log file instead of console.");
561  orig_stderr = _fileno(stderr);
562  }
563 
564  /* direct stdout/stderr to point to log_handle */
565  log_fd = _open_osfhandle((intptr_t)log_handle, _O_TEXT);
566  if (log_fd == -1)
567  {
568  msg(M_ERR, "Error: --log redirect failed due to _open_osfhandle failure");
569  }
570 
571  /* open log_handle as FILE stream */
572  ASSERT(msgfp == NULL);
573  msgfp = _fdopen(log_fd, "wt");
574  if (msgfp == NULL)
575  {
576  msg(M_ERR, "Error: --log redirect failed due to _fdopen");
577  }
578 
579  /* redirect C-library stdout/stderr to log file */
580  if (_dup2(log_fd, 1) == -1 || _dup2(log_fd, 2) == -1)
581  {
582  msg(M_WARN, "Error: --log redirect of stdout/stderr failed");
583  }
584 
585  std_redir = true;
586  }
587 #elif defined(HAVE_DUP2)
588  if (!std_redir)
589  {
590  int out = open(file,
591  O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC),
592  S_IRUSR | S_IWUSR);
593 
594  if (out < 0)
595  {
596  msg(M_WARN|M_ERRNO, "Warning: Error redirecting stdout/stderr to --log file: %s", file);
597  return;
598  }
599 
600  if (dup2(out, 1) == -1)
601  {
602  msg(M_ERR, "--log file redirection error on stdout");
603  }
604  if (dup2(out, 2) == -1)
605  {
606  msg(M_ERR, "--log file redirection error on stderr");
607  }
608 
609  if (out > 2)
610  {
611  close(out);
612  }
613 
614  std_redir = true;
615  }
616 
617 #else /* if defined(_WIN32) */
618  msg(M_WARN, "WARNING: The --log option is not supported on this OS because it lacks the dup2 function");
619 #endif /* if defined(_WIN32) */
620 }
621 
622 /*
623  * Functions used to check return status
624  * of I/O operations.
625  */
626 
627 unsigned int x_cs_info_level; /* GLOBAL */
628 unsigned int x_cs_verbose_level; /* GLOBAL */
629 unsigned int x_cs_err_delay_ms; /* GLOBAL */
630 
631 void
633 {
634  x_cs_info_level = 0;
635  x_cs_verbose_level = 0;
636 }
637 
638 void
639 set_check_status(unsigned int info_level, unsigned int verbose_level)
640 {
641  x_cs_info_level = info_level;
642  x_cs_verbose_level = verbose_level;
643 }
644 
645 /*
646  * Called after most socket or tun/tap operations, via the inline
647  * function check_status().
648  *
649  * Decide if we should print an error message, and see if we can
650  * extract any useful info from the error, such as a Path MTU hint
651  * from the OS.
652  */
653 void
655  const char *description,
656  struct link_socket *sock,
657  struct tuntap *tt)
658 {
659  const char *extended_msg = NULL;
660 
661  bool crt_error = false;
662  int my_errno = openvpn_errno_maybe_crt(&crt_error);
663 
664  msg(x_cs_verbose_level, "%s %s returned %d",
665  sock ? proto2ascii(sock->info.proto, sock->info.af, true) : "",
666  description,
667  status);
668 
669  if (status < 0)
670  {
671  struct gc_arena gc = gc_new();
672 #if EXTENDED_SOCKET_ERROR_CAPABILITY
673  /* get extended socket error message and possible PMTU hint from OS */
674  if (sock)
675  {
676  int mtu;
677  extended_msg = format_extended_socket_error(sock->sd, &mtu, &gc);
678  if (mtu > 0 && sock->mtu != mtu)
679  {
680  sock->mtu = mtu;
681  sock->info.mtu_changed = true;
682  }
683  }
684 #endif /* EXTENDED_SOCKET_ERROR_CAPABILITY */
685 
686 #ifdef _WIN32
687  /* get possible driver error from TAP-Windows driver */
688  if (tuntap_defined(tt))
689  {
690  extended_msg = tap_win_getinfo(tt, &gc);
691  }
692 #endif
693 
694  if (!ignore_sys_error(my_errno, crt_error))
695  {
696  if (extended_msg)
697  {
698  msg(x_cs_info_level, "%s %s [%s]: %s (fd=" SOCKET_PRINTF ",code=%d)", description,
699  sock ? proto2ascii(sock->info.proto, sock->info.af, true) : "",
700  extended_msg, openvpn_strerror(my_errno, crt_error, &gc),
701  sock ? sock->sd : -1, my_errno);
702  }
703  else
704  {
705  msg(x_cs_info_level, "%s %s: %s (fd=" SOCKET_PRINTF ",code=%d)", description,
706  sock ? proto2ascii(sock->info.proto, sock->info.af, true) : "",
707  openvpn_strerror(my_errno, crt_error, &gc),
708  sock ? sock->sd : -1, my_errno);
709  }
710 
711  if (x_cs_err_delay_ms)
712  {
714  }
715  }
716  gc_free(&gc);
717  }
718 }
719 
720 /*
721  * In multiclient mode, put a client-specific prefix
722  * before each message.
723  */
724 const char *x_msg_prefix; /* GLOBAL */
725 
726 /*
727  * Allow MSG to be redirected through a virtual_output object
728  */
729 
730 const struct virtual_output *x_msg_virtual_output; /* GLOBAL */
731 
732 /*
733  * Exiting.
734  */
735 
736 void
738 {
739  if (!forked)
740  {
741  tun_abort();
742 
743 #ifdef _WIN32
744  uninit_win32();
745 #endif
746  remove_pid_file();
747 
748  close_syslog();
749 
750 #ifdef ENABLE_PLUGIN
751  plugin_abort();
752 #endif
753 
754 #if PORT_SHARE
755  if (port_share)
756  {
757  port_share_abort(port_share);
758  }
759 #endif
760 
761 #ifdef ENABLE_MEMSTATS
762  mstats_close();
763 #endif
764 
765 #ifdef ABORT_ON_ERROR
767  {
768  abort();
769  }
770 #endif
771 
773  {
775  }
776  }
777 
778  exit(status);
779 }
780 
781 /*
782  * Translate msg flags into a string
783  */
784 const char *
785 msg_flags_string(const unsigned int flags, struct gc_arena *gc)
786 {
787  struct buffer out = alloc_buf_gc(16, gc);
788  if (flags == M_INFO)
789  {
790  buf_printf(&out, "I");
791  }
792  if (flags & M_FATAL)
793  {
794  buf_printf(&out, "F");
795  }
796  if (flags & M_NONFATAL)
797  {
798  buf_printf(&out, "N");
799  }
800  if (flags & M_WARN)
801  {
802  buf_printf(&out, "W");
803  }
804  if (flags & M_DEBUG)
805  {
806  buf_printf(&out, "D");
807  }
808  return BSTR(&out);
809 }
810 
811 #ifdef _WIN32
812 
813 const char *
814 strerror_win32(DWORD errnum, struct gc_arena *gc)
815 {
816  /*
817  * This code can be omitted, though often the Windows
818  * WSA error messages are less informative than the
819  * Posix equivalents.
820  */
821 #if 1
822  switch (errnum)
823  {
824  /*
825  * When the TAP-Windows driver returns STATUS_UNSUCCESSFUL, this code
826  * gets returned to user space.
827  */
828  case ERROR_GEN_FAILURE:
829  return "General failure (ERROR_GEN_FAILURE)";
830 
831  case ERROR_IO_PENDING:
832  return "I/O Operation in progress (ERROR_IO_PENDING)";
833 
834  case WSA_IO_INCOMPLETE:
835  return "I/O Operation in progress (WSA_IO_INCOMPLETE)";
836 
837  case WSAEINTR:
838  return "Interrupted system call (WSAEINTR)";
839 
840  case WSAEBADF:
841  return "Bad file number (WSAEBADF)";
842 
843  case WSAEACCES:
844  return "Permission denied (WSAEACCES)";
845 
846  case WSAEFAULT:
847  return "Bad address (WSAEFAULT)";
848 
849  case WSAEINVAL:
850  return "Invalid argument (WSAEINVAL)";
851 
852  case WSAEMFILE:
853  return "Too many open files (WSAEMFILE)";
854 
855  case WSAEWOULDBLOCK:
856  return "Operation would block (WSAEWOULDBLOCK)";
857 
858  case WSAEINPROGRESS:
859  return "Operation now in progress (WSAEINPROGRESS)";
860 
861  case WSAEALREADY:
862  return "Operation already in progress (WSAEALREADY)";
863 
864  case WSAEDESTADDRREQ:
865  return "Destination address required (WSAEDESTADDRREQ)";
866 
867  case WSAEMSGSIZE:
868  return "Message too long (WSAEMSGSIZE)";
869 
870  case WSAEPROTOTYPE:
871  return "Protocol wrong type for socket (WSAEPROTOTYPE)";
872 
873  case WSAENOPROTOOPT:
874  return "Bad protocol option (WSAENOPROTOOPT)";
875 
876  case WSAEPROTONOSUPPORT:
877  return "Protocol not supported (WSAEPROTONOSUPPORT)";
878 
879  case WSAESOCKTNOSUPPORT:
880  return "Socket type not supported (WSAESOCKTNOSUPPORT)";
881 
882  case WSAEOPNOTSUPP:
883  return "Operation not supported on socket (WSAEOPNOTSUPP)";
884 
885  case WSAEPFNOSUPPORT:
886  return "Protocol family not supported (WSAEPFNOSUPPORT)";
887 
888  case WSAEAFNOSUPPORT:
889  return "Address family not supported by protocol family (WSAEAFNOSUPPORT)";
890 
891  case WSAEADDRINUSE:
892  return "Address already in use (WSAEADDRINUSE)";
893 
894  case WSAENETDOWN:
895  return "Network is down (WSAENETDOWN)";
896 
897  case WSAENETUNREACH:
898  return "Network is unreachable (WSAENETUNREACH)";
899 
900  case WSAENETRESET:
901  return "Net dropped connection or reset (WSAENETRESET)";
902 
903  case WSAECONNABORTED:
904  return "Software caused connection abort (WSAECONNABORTED)";
905 
906  case WSAECONNRESET:
907  return "Connection reset by peer (WSAECONNRESET)";
908 
909  case WSAENOBUFS:
910  return "No buffer space available (WSAENOBUFS)";
911 
912  case WSAEISCONN:
913  return "Socket is already connected (WSAEISCONN)";
914 
915  case WSAENOTCONN:
916  return "Socket is not connected (WSAENOTCONN)";
917 
918  case WSAETIMEDOUT:
919  return "Connection timed out (WSAETIMEDOUT)";
920 
921  case WSAECONNREFUSED:
922  return "Connection refused (WSAECONNREFUSED)";
923 
924  case WSAELOOP:
925  return "Too many levels of symbolic links (WSAELOOP)";
926 
927  case WSAENAMETOOLONG:
928  return "File name too long (WSAENAMETOOLONG)";
929 
930  case WSAEHOSTDOWN:
931  return "Host is down (WSAEHOSTDOWN)";
932 
933  case WSAEHOSTUNREACH:
934  return "No Route to Host (WSAEHOSTUNREACH)";
935 
936  case WSAENOTEMPTY:
937  return "Directory not empty (WSAENOTEMPTY)";
938 
939  case WSAEPROCLIM:
940  return "Too many processes (WSAEPROCLIM)";
941 
942  case WSAEUSERS:
943  return "Too many users (WSAEUSERS)";
944 
945  case WSAEDQUOT:
946  return "Disc Quota Exceeded (WSAEDQUOT)";
947 
948  case WSAESTALE:
949  return "Stale NFS file handle (WSAESTALE)";
950 
951  case WSASYSNOTREADY:
952  return "Network SubSystem is unavailable (WSASYSNOTREADY)";
953 
954  case WSAVERNOTSUPPORTED:
955  return "WINSOCK DLL Version out of range (WSAVERNOTSUPPORTED)";
956 
957  case WSANOTINITIALISED:
958  return "Successful WSASTARTUP not yet performed (WSANOTINITIALISED)";
959 
960  case WSAEREMOTE:
961  return "Too many levels of remote in path (WSAEREMOTE)";
962 
963  case WSAHOST_NOT_FOUND:
964  return "Host not found (WSAHOST_NOT_FOUND)";
965 
966  default:
967  break;
968  }
969 #endif /* if 1 */
970 
971  /* format a windows error message */
972  {
973  char message[256];
974  struct buffer out = alloc_buf_gc(256, gc);
975  const int status = FormatMessage(
976  FORMAT_MESSAGE_IGNORE_INSERTS
977  | FORMAT_MESSAGE_FROM_SYSTEM
978  | FORMAT_MESSAGE_ARGUMENT_ARRAY,
979  NULL,
980  errnum,
981  0,
982  message,
983  sizeof(message),
984  NULL);
985  if (!status)
986  {
987  buf_printf(&out, "[Unknown Win32 Error]");
988  }
989  else
990  {
991  char *cp;
992  for (cp = message; *cp != '\0'; ++cp)
993  {
994  if (*cp == '\n' || *cp == '\r')
995  {
996  *cp = ' ';
997  }
998  }
999 
1000  buf_printf(&out, "%s", message);
1001  }
1002 
1003  return BSTR(&out);
1004  }
1005 }
1006 
1007 #endif /* ifdef _WIN32 */
x_msg
void x_msg(const unsigned int flags, const char *format,...)
Definition: error.c:215
M_INFO
#define M_INFO
Definition: errlevel.h:55
orig_stderr
static int orig_stderr
Definition: error.c:507
env_set::gc
struct gc_arena * gc
Definition: env_set.h:43
msg_test
static bool msg_test(unsigned int flags)
Return true if flags represent an enabled, not muted log level.
Definition: error.h:233
error.h
M_OPTERR
#define M_OPTERR
Definition: error.h:106
plugin_abort
void plugin_abort(void)
Definition: plugin.c:906
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1011
M_ERRNO
#define M_ERRNO
Definition: error.h:100
SOCKET_PRINTF
#define SOCKET_PRINTF
Definition: syshead.h:444
PACKAGE_NAME
#define PACKAGE_NAME
Definition: config.h:492
strerror_win32
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
Definition: error.c:814
usage_small
void usage_small(void)
Definition: options.c:4803
M_FATAL
#define M_FATAL
Definition: error.h:95
win32.h
DEBUG_LEVEL_USEC_TIME
#define DEBUG_LEVEL_USEC_TIME
Definition: errlevel.h:33
msgfp
static FILE * msgfp
Definition: error.c:91
x_msg_virtual_output
const struct virtual_output * x_msg_virtual_output
Definition: error.c:730
x_msg_va
void x_msg_va(const unsigned int flags, const char *format, va_list arglist)
Definition: error.c:236
proto2ascii
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
Definition: socket.c:3129
M_NONFATAL
#define M_NONFATAL
Definition: error.h:96
M_NOMUTE
#define M_NOMUTE
Definition: error.h:102
out_of_memory
void out_of_memory(void)
Definition: error.c:462
BSTR
#define BSTR(buf)
Definition: buffer.h:129
OPENVPN_EXIT_STATUS_GOOD
#define OPENVPN_EXIT_STATUS_GOOD
Definition: error.h:59
dont_mute
bool dont_mute(unsigned int flags)
Check muting filter.
Definition: error.c:409
alloc_buf_gc
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Definition: buffer.c:90
config-msvc.h
OPENVPN_ERROR_FP
#define OPENVPN_ERROR_FP
Definition: error.h:53
perf_output_results
static void perf_output_results(void)
Definition: perf.h:86
set_debug_level
bool set_debug_level(const int level, const unsigned int flags)
Definition: error.c:107
x_msg_prefix
const char * x_msg_prefix
Definition: error.c:724
OPENVPN_DEBUG_FILE
#define OPENVPN_DEBUG_FILE
Definition: error.h:73
set_check_status
void set_check_status(unsigned int info_level, unsigned int verbose_level)
Definition: error.c:639
x_debug_level
unsigned int x_debug_level
Definition: error.c:54
PACKAGE
#define PACKAGE
Definition: config.h:486
M_NOPREFIX
#define M_NOPREFIX
Definition: error.h:103
tap_win_getinfo
const char * tap_win_getinfo(const struct tuntap *tt, struct gc_arena *gc)
Definition: tun.c:6766
S_IWUSR
#define S_IWUSR
Definition: config-msvc.h:71
close_syslog
void close_syslog(void)
Definition: error.c:493
default_out
static FILE * default_out
Definition: error.c:97
x_cs_err_delay_ms
unsigned int x_cs_err_delay_ms
Definition: error.c:629
reset_check_status
void reset_check_status(void)
Definition: error.c:632
tun_abort
void tun_abort(void)
Definition: init.c:2172
S_IRUSR
#define S_IRUSR
Definition: config-msvc.h:70
mute_count
static int mute_count
Definition: error.c:58
tuntap_defined
static bool tuntap_defined(const struct tuntap *tt)
Definition: tun.h:238
string_alloc
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:695
x_cs_info_level
unsigned int x_cs_info_level
Definition: error.c:627
ERR_BUF_SIZE
#define ERR_BUF_SIZE
Definition: error.h:41
ASSERT
#define ASSERT(x)
Definition: error.h:201
msg_forked
void msg_forked(void)
Definition: error.c:101
set_machine_readable_output
void set_machine_readable_output(bool parsable)
Definition: error.c:157
set_suppress_timestamps
void set_suppress_timestamps(bool suppressed)
Definition: error.c:151
ignore_sys_error
static bool ignore_sys_error(const int err, bool crt_error)
Definition: error.h:347
tun.h
M_USAGE_SMALL
#define M_USAGE_SMALL
Definition: error.h:104
get_mute_cutoff
int get_mute_cutoff(void)
Definition: error.c:145
DECODE_MUTE_LEVEL
#define DECODE_MUTE_LEVEL(flags)
Definition: error.h:125
virtual_output_print
static void virtual_output_print(const struct virtual_output *vo, const unsigned int flags, const char *str)
Definition: status.h:39
init.h
misc.h
get_debug_level
int get_debug_level(void)
Definition: error.c:139
M_WARN
#define M_WARN
Definition: error.h:97
wide_string
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)
Definition: win32-util.c:43
msg_fp
FILE * msg_fp(const unsigned int flags)
Definition: error.c:196
msg_get_prefix
static const char * msg_get_prefix(void)
Definition: error.h:317
M_ERR
#define M_ERR
Definition: error.h:111
SDL_CONSTRAIN
#define SDL_CONSTRAIN
Definition: error.h:183
SWAP
#define SWAP
Definition: error.c:210
std_redir
static bool std_redir
Definition: error.c:73
x_cs_verbose_level
unsigned int x_cs_verbose_level
Definition: error.c:628
get_orig_stderr
int get_orig_stderr()
Definition: error.c:510
open_syslog
void open_syslog(const char *pgmname, bool stdio_to_null)
Definition: error.c:469
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
virtual_output
Definition: status.h:32
errors_to_stderr
void errors_to_stderr(void)
Definition: error.c:187
OPENVPN_EXIT_STATUS_ERROR
#define OPENVPN_EXIT_STATUS_ERROR
Definition: error.h:60
openvpn_exit
void openvpn_exit(const int status)
Definition: error.c:737
set_mute_cutoff
bool set_mute_cutoff(const int cutoff)
Definition: error.c:125
use_syslog
static bool use_syslog
Definition: error.c:76
mute_cutoff
static int mute_cutoff
Definition: error.c:57
suppress_timestamps
static bool suppress_timestamps
Definition: error.c:83
ps.h
buffer.h
x_msg_line_num
int x_msg_line_num
Definition: error.c:212
assert_failed
void assert_failed(const char *filename, int line, const char *condition)
Definition: error.c:444
syshead.h
forked
static bool forked
Definition: error.c:94
uninit_win32
void uninit_win32(void)
Definition: win32.c:125
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
x_check_status
void x_check_status(int status, const char *description, struct link_socket *sock, struct tuntap *tt)
Definition: error.c:654
openvpn_errno_maybe_crt
static int openvpn_errno_maybe_crt(bool *crt_error)
Definition: error.h:385
OPENVPN_MSG_FP
#define OPENVPN_MSG_FP
Definition: error.h:52
M_DEBUG
#define M_DEBUG
Definition: error.h:98
msg_flags_string
const char * msg_flags_string(const unsigned int flags, struct gc_arena *gc)
Definition: error.c:785
perf.h
check_debug_level
static bool check_debug_level(unsigned int level)
Definition: error.h:226
error_reset
void error_reset(void)
Definition: error.c:163
machine_readable_output
static bool machine_readable_output
Definition: error.c:80
msg_get_virtual_output
static const struct virtual_output * msg_get_virtual_output(void)
Definition: error.h:337
redirect_stdout_stderr
void redirect_stdout_stderr(const char *file, bool append)
Definition: error.c:517
mstats.h
set_std_files_to_null
void set_std_files_to_null(bool stdin_only)
Definition: misc.c:58
gc_malloc
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition: buffer.c:382
otime.h
openvpn_snprintf
bool openvpn_snprintf(char *str, size_t size, const char *format,...)
Definition: buffer.c:296
status
static SERVICE_STATUS status
Definition: interactive.c:56
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1019
openvpn_strerror
static const char * openvpn_strerror(int err, bool crt_error, struct gc_arena *gc)
Definition: error.c:224
tuntap
Definition: tun.h:171
socket.h
M_MSG_VIRT_OUT
#define M_MSG_VIRT_OUT
Definition: error.h:105
config.h
time_string
const char * time_string(time_t t, int usec, bool show_usec, struct gc_arena *gc)
Definition: otime.c:110
gc_init
static void gc_init(struct gc_arena *a)
Definition: buffer.h:998
M_NOIPREFIX
#define M_NOIPREFIX
Definition: error.h:108
default_err
static FILE * default_err
Definition: error.c:98
OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE
#define OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE
Definition: error.h:62
constrain_int
static int constrain_int(int x, int min, int max)
Definition: integer.h:95
mute_category
static int mute_category
Definition: error.c:59
M_NOLF
#define M_NOLF
Definition: error.h:107
remove_pid_file
void remove_pid_file(void)
Definition: init.c:5013
msg
#define msg(flags,...)
Definition: error.h:150
platform_sleep_milliseconds
void platform_sleep_milliseconds(unsigned int n)
Definition: platform.c:476
integer.h
status.h
buf_printf
bool buf_printf(struct buffer *buf, const char *format,...)
Definition: buffer.c:242