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  if (tuntap_defined(tt))
692  {
693  extended_msg = tap_win_getinfo(tt, &gc);
694  }
695 #endif
696  if (!ignore_sys_error(my_errno))
697  {
698  if (extended_msg)
699  {
700  msg(x_cs_info_level, "%s %s [%s]: %s (code=%d)", description,
701  sock ? proto2ascii(sock->info.proto, sock->info.af, true) : "",
702  extended_msg, strerror(my_errno), my_errno);
703  }
704  else
705  {
706  msg(x_cs_info_level, "%s %s: %s (code=%d)", description,
707  sock ? proto2ascii(sock->info.proto, sock->info.af, true) : "",
708  strerror(my_errno), 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
766  if (status == OPENVPN_EXIT_STATUS_ERROR)
767  {
768  abort();
769  }
770 #endif
771 
772  if (status == OPENVPN_EXIT_STATUS_GOOD)
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 ENABLE_DEBUG
812 void
813 crash(void)
814 {
815  char *null = NULL;
816  *null = 0;
817 }
818 #endif
819 
820 #ifdef _WIN32
821 
822 const char *
823 strerror_win32(DWORD errnum, struct gc_arena *gc)
824 {
825  /*
826  * This code can be omitted, though often the Windows
827  * WSA error messages are less informative than the
828  * Posix equivalents.
829  */
830 #if 1
831  switch (errnum)
832  {
833  /*
834  * When the TAP-Windows driver returns STATUS_UNSUCCESSFUL, this code
835  * gets returned to user space.
836  */
837  case ERROR_GEN_FAILURE:
838  return "General failure (ERROR_GEN_FAILURE)";
839 
840  case ERROR_IO_PENDING:
841  return "I/O Operation in progress (ERROR_IO_PENDING)";
842 
843  case WSA_IO_INCOMPLETE:
844  return "I/O Operation in progress (WSA_IO_INCOMPLETE)";
845 
846  case WSAEINTR:
847  return "Interrupted system call (WSAEINTR)";
848 
849  case WSAEBADF:
850  return "Bad file number (WSAEBADF)";
851 
852  case WSAEACCES:
853  return "Permission denied (WSAEACCES)";
854 
855  case WSAEFAULT:
856  return "Bad address (WSAEFAULT)";
857 
858  case WSAEINVAL:
859  return "Invalid argument (WSAEINVAL)";
860 
861  case WSAEMFILE:
862  return "Too many open files (WSAEMFILE)";
863 
864  case WSAEWOULDBLOCK:
865  return "Operation would block (WSAEWOULDBLOCK)";
866 
867  case WSAEINPROGRESS:
868  return "Operation now in progress (WSAEINPROGRESS)";
869 
870  case WSAEALREADY:
871  return "Operation already in progress (WSAEALREADY)";
872 
873  case WSAEDESTADDRREQ:
874  return "Destination address required (WSAEDESTADDRREQ)";
875 
876  case WSAEMSGSIZE:
877  return "Message too long (WSAEMSGSIZE)";
878 
879  case WSAEPROTOTYPE:
880  return "Protocol wrong type for socket (WSAEPROTOTYPE)";
881 
882  case WSAENOPROTOOPT:
883  return "Bad protocol option (WSAENOPROTOOPT)";
884 
885  case WSAEPROTONOSUPPORT:
886  return "Protocol not supported (WSAEPROTONOSUPPORT)";
887 
888  case WSAESOCKTNOSUPPORT:
889  return "Socket type not supported (WSAESOCKTNOSUPPORT)";
890 
891  case WSAEOPNOTSUPP:
892  return "Operation not supported on socket (WSAEOPNOTSUPP)";
893 
894  case WSAEPFNOSUPPORT:
895  return "Protocol family not supported (WSAEPFNOSUPPORT)";
896 
897  case WSAEAFNOSUPPORT:
898  return "Address family not supported by protocol family (WSAEAFNOSUPPORT)";
899 
900  case WSAEADDRINUSE:
901  return "Address already in use (WSAEADDRINUSE)";
902 
903  case WSAENETDOWN:
904  return "Network is down (WSAENETDOWN)";
905 
906  case WSAENETUNREACH:
907  return "Network is unreachable (WSAENETUNREACH)";
908 
909  case WSAENETRESET:
910  return "Net dropped connection or reset (WSAENETRESET)";
911 
912  case WSAECONNABORTED:
913  return "Software caused connection abort (WSAECONNABORTED)";
914 
915  case WSAECONNRESET:
916  return "Connection reset by peer (WSAECONNRESET)";
917 
918  case WSAENOBUFS:
919  return "No buffer space available (WSAENOBUFS)";
920 
921  case WSAEISCONN:
922  return "Socket is already connected (WSAEISCONN)";
923 
924  case WSAENOTCONN:
925  return "Socket is not connected (WSAENOTCONN)";
926 
927  case WSAETIMEDOUT:
928  return "Connection timed out (WSAETIMEDOUT)";
929 
930  case WSAECONNREFUSED:
931  return "Connection refused (WSAECONNREFUSED)";
932 
933  case WSAELOOP:
934  return "Too many levels of symbolic links (WSAELOOP)";
935 
936  case WSAENAMETOOLONG:
937  return "File name too long (WSAENAMETOOLONG)";
938 
939  case WSAEHOSTDOWN:
940  return "Host is down (WSAEHOSTDOWN)";
941 
942  case WSAEHOSTUNREACH:
943  return "No Route to Host (WSAEHOSTUNREACH)";
944 
945  case WSAENOTEMPTY:
946  return "Directory not empty (WSAENOTEMPTY)";
947 
948  case WSAEPROCLIM:
949  return "Too many processes (WSAEPROCLIM)";
950 
951  case WSAEUSERS:
952  return "Too many users (WSAEUSERS)";
953 
954  case WSAEDQUOT:
955  return "Disc Quota Exceeded (WSAEDQUOT)";
956 
957  case WSAESTALE:
958  return "Stale NFS file handle (WSAESTALE)";
959 
960  case WSASYSNOTREADY:
961  return "Network SubSystem is unavailable (WSASYSNOTREADY)";
962 
963  case WSAVERNOTSUPPORTED:
964  return "WINSOCK DLL Version out of range (WSAVERNOTSUPPORTED)";
965 
966  case WSANOTINITIALISED:
967  return "Successful WSASTARTUP not yet performed (WSANOTINITIALISED)";
968 
969  case WSAEREMOTE:
970  return "Too many levels of remote in path (WSAEREMOTE)";
971 
972  case WSAHOST_NOT_FOUND:
973  return "Host not found (WSAHOST_NOT_FOUND)";
974 
975  default:
976  break;
977  }
978 #endif /* if 1 */
979 
980  /* format a windows error message */
981  {
982  char message[256];
983  struct buffer out = alloc_buf_gc(256, gc);
984  const int status = FormatMessage(
985  FORMAT_MESSAGE_IGNORE_INSERTS
986  | FORMAT_MESSAGE_FROM_SYSTEM
987  | FORMAT_MESSAGE_ARGUMENT_ARRAY,
988  NULL,
989  errnum,
990  0,
991  message,
992  sizeof(message),
993  NULL);
994  if (!status)
995  {
996  buf_printf(&out, "[Unknown Win32 Error]");
997  }
998  else
999  {
1000  char *cp;
1001  for (cp = message; *cp != '\0'; ++cp)
1002  {
1003  if (*cp == '\n' || *cp == '\r')
1004  {
1005  *cp = ' ';
1006  }
1007  }
1008 
1009  buf_printf(&out, "%s", message);
1010  }
1011 
1012  return BSTR(&out);
1013  }
1014 }
1015 
1016 #endif /* ifdef _WIN32 */
#define M_NONFATAL
Definition: error.h:95
void uninit_win32(void)
Definition: win32.c:116
static bool forked
Definition: error.c:94
static bool std_redir
Definition: error.c:73
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)
Definition: win32.c:1157
void open_syslog(const char *pgmname, bool stdio_to_null)
Definition: error.c:460
void x_check_status(int status, const char *description, struct link_socket *sock, struct tuntap *tt)
Definition: error.c:661
static HANDLE orig_stderr
Definition: error.c:502
void set_std_files_to_null(bool stdin_only)
Definition: misc.c:58
Definition: tun.h:151
void usage_small(void)
Definition: options.c:4314
int x_msg_line_num
Definition: error.c:212
HANDLE get_orig_stderr(void)
Definition: error.c:505
void assert_failed(const char *filename, int line, const char *condition)
Definition: error.c:435
#define M_NOPREFIX
Definition: error.h:102
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
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:688
#define openvpn_errno()
Definition: error.h:74
static bool machine_readable_output
Definition: error.c:80
#define M_OPTERR
Definition: error.h:105
#define OPENVPN_ERROR_FP
Definition: error.h:49
#define M_INFO
Definition: errlevel.h:55
void tun_abort(void)
Definition: init.c:2125
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:484
void error_reset(void)
Definition: error.c:163
void openvpn_exit(const int status)
Definition: error.c:737
#define M_MSG_VIRT_OUT
Definition: error.h:104
#define SDL_CONSTRAIN
Definition: error.h:203
const char * msg_flags_string(const unsigned int flags, struct gc_arena *gc)
Definition: error.c:785
unsigned int x_cs_verbose_level
Definition: error.c:635
static int mute_cutoff
Definition: error.c:57
static bool suppress_timestamps
Definition: error.c:83
#define OPENVPN_MSG_FP
Definition: error.h:48
static const char * msg_get_prefix(void)
Definition: error.h:336
#define M_NOMUTE
Definition: error.h:101
void msg_forked(void)
Definition: error.c:101
bool buf_printf(struct buffer *buf, const char *format,...)
Definition: buffer.c:245
void remove_pid_file(void)
Definition: init.c:4708
void out_of_memory(void)
Definition: error.c:453
#define ASSERT(x)
Definition: error.h:221
bool dont_mute(unsigned int flags)
Check muting filter.
Definition: error.c:400
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:245
const char * tap_win_getinfo(const struct tuntap *tt, struct gc_arena *gc)
Definition: tun.c:6589
#define M_NOLF
Definition: error.h:106
static bool tuntap_defined(const struct tuntap *tt)
Definition: tun.h:223
#define OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE
Definition: error.h:58
int get_debug_level(void)
Definition: error.c:139
#define S_IWUSR
Definition: config-msvc.h:144
list flags
static FILE * default_out
Definition: error.c:97
bool openvpn_snprintf(char *str, size_t size, const char *format,...)
Definition: buffer.c:299
static int mute_category
Definition: error.c:59
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
static const struct virtual_output * msg_get_virtual_output(void)
Definition: error.h:356
#define PACKAGE_NAME
Definition: config.h:730
void errors_to_stderr(void)
Definition: error.c:187
const char * x_msg_prefix
Definition: error.c:724
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:408
static int mute_count
Definition: error.c:58
#define M_DEBUG
Definition: error.h:97
const char * time_string(time_t t, int usec, bool show_usec, struct gc_arena *gc)
Definition: otime.c:114
void reset_check_status(void)
Definition: error.c:639
static SERVICE_STATUS status
Definition: automatic.c:43
#define vsnprintf
void redirect_stdout_stderr(const char *file, bool append)
Definition: error.c:520
#define S_IRUSR
Definition: config-msvc.h:143
FILE * msg_fp(const unsigned int flags)
Definition: error.c:196
#define M_ERRNO
Definition: error.h:99
#define msg
Definition: error.h:173
#define M_NOIPREFIX
Definition: error.h:107
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
Definition: error.c:823
#define DEBUG_LEVEL_USEC_TIME
Definition: errlevel.h:33
#define OPENVPN_DEBUG_FILE
Definition: error.h:69
const struct virtual_output * x_msg_virtual_output
Definition: error.c:730
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:646
static bool msg_test(unsigned int flags)
Return true if flags represent an enabled, not muted log level.
Definition: error.h:252
__int64 int64_t
Definition: config-msvc.h:160
unsigned int x_debug_level
Definition: error.c:54
void plugin_abort(void)
Definition: plugin.c:885
#define M_USAGE_SMALL
Definition: error.h:103
#define M_ERR
Definition: error.h:110
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:94
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:70
#define M_WARN
Definition: error.h:96
static bool ignore_sys_error(const int err)
Definition: error.h:366
#define ERR_BUF_SIZE
Definition: error.h:39
#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:56
unsigned int x_cs_err_delay_ms
Definition: error.c:636
#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:3235
unsigned int x_cs_info_level
Definition: error.c:634
#define OPENVPN_EXIT_STATUS_GOOD
Definition: error.h:55
bool set_mute_cutoff(const int cutoff)
Definition: error.c:125
#define DECODE_MUTE_LEVEL(flags)
Definition: error.h:124