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