blob: 964bc333b0511d5e3c6b9b8722f0a513727d6c6c [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Adam Tkac35e6d4c2010-04-23 14:12:18 +00002 * Copyright (C) 2004 Red Hat Inc.
3 * Copyright (C) 2010 TigerVNC Team
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
Adam Tkac35e6d4c2010-04-23 14:12:18 +000020
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
Pierre Ossman5ad4d062014-07-07 14:13:46 +020025#include <stdio.h>
26#include <stdarg.h>
27
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#include <rdr/Exception.h>
Adam Tkac35e6d4c2010-04-23 14:12:18 +000029#include <rdr/TLSException.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#ifdef _WIN32
31#include <tchar.h>
32#include <winsock2.h>
33#include <windows.h>
34#endif
35
Adam Tkac75586e62008-12-03 15:03:08 +000036#include <string.h>
37
Adam Tkac35e6d4c2010-04-23 14:12:18 +000038#ifdef HAVE_GNUTLS
39#include <gnutls/gnutls.h>
40#endif
41
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042using namespace rdr;
43
Adam Tkac20e0d712008-11-14 14:48:21 +000044Exception::Exception(const char *format, ...) {
45 va_list ap;
46
47 va_start(ap, format);
48 (void) vsnprintf(str_, len, format, ap);
49 va_end(ap);
50}
51
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052SystemException::SystemException(const char* s, int err_)
Pierre Ossmanc92081e2014-07-21 16:10:03 +020053 : Exception("%s", s), err(err_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054{
55 strncat(str_, ": ", len-1-strlen(str_));
56#ifdef _WIN32
57 // Windows error messages are crap, so use unix ones for common errors.
58 const char* msg = 0;
59 switch (err) {
60 case WSAECONNREFUSED: msg = "Connection refused"; break;
61 case WSAETIMEDOUT: msg = "Connection timed out"; break;
62 case WSAECONNRESET: msg = "Connection reset by peer"; break;
63 case WSAECONNABORTED: msg = "Connection aborted"; break;
64 }
65 if (msg) {
66 strncat(str_, msg, len-1-strlen(str_));
67 } else {
68#ifdef UNICODE
69 WCHAR* tmsg = new WCHAR[len-strlen(str_)];
70 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
71 0, err, 0, tmsg, len-1-strlen(str_), 0);
72 WideCharToMultiByte(CP_ACP, 0, tmsg, wcslen(tmsg)+1,
73 str_+strlen(str_), len-strlen(str_), 0, 0);
74 delete [] tmsg;
75#else
76 char* currStr = str_+strlen(str_);
77 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
78 0, err, 0, currStr, len-1-strlen(str_), 0);
79#endif
80 int l = strlen(str_);
81 if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
82 str_[l-2] = 0;
83 }
84
85#else
86 strncat(str_, strerror(err), len-1-strlen(str_));
87#endif
88 strncat(str_, " (", len-1-strlen(str_));
89 char buf[20];
90#ifdef WIN32
91 if (err < 0)
92 sprintf(buf, "%x", err);
93 else
94#endif
95 sprintf(buf,"%d",err);
96 strncat(str_, buf, len-1-strlen(str_));
97 strncat(str_, ")", len-1-strlen(str_));
98}
Adam Tkac35e6d4c2010-04-23 14:12:18 +000099