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