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