blob: fd40582f649fef5098153835c249ed5241b7e849 [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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000025#include <rdr/Exception.h>
Adam Tkac35e6d4c2010-04-23 14:12:18 +000026#include <rdr/TLSException.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027#ifdef _WIN32
28#include <tchar.h>
29#include <winsock2.h>
30#include <windows.h>
31#endif
32
Adam Tkac20e0d712008-11-14 14:48:21 +000033#include <os/print.h>
34
Adam Tkac75586e62008-12-03 15:03:08 +000035#include <string.h>
36
Adam Tkac35e6d4c2010-04-23 14:12:18 +000037#ifdef HAVE_GNUTLS
38#include <gnutls/gnutls.h>
39#endif
40
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041using namespace rdr;
42
Adam Tkac20e0d712008-11-14 14:48:21 +000043Exception::Exception(const char *format, ...) {
44 va_list ap;
45
46 va_start(ap, format);
47 (void) vsnprintf(str_, len, format, ap);
48 va_end(ap);
49}
50
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051SystemException::SystemException(const char* s, int err_)
52 : Exception(s), err(err_)
53{
54 strncat(str_, ": ", len-1-strlen(str_));
55#ifdef _WIN32
56 // Windows error messages are crap, so use unix ones for common errors.
57 const char* msg = 0;
58 switch (err) {
59 case WSAECONNREFUSED: msg = "Connection refused"; break;
60 case WSAETIMEDOUT: msg = "Connection timed out"; break;
61 case WSAECONNRESET: msg = "Connection reset by peer"; break;
62 case WSAECONNABORTED: msg = "Connection aborted"; break;
63 }
64 if (msg) {
65 strncat(str_, msg, len-1-strlen(str_));
66 } else {
67#ifdef UNICODE
68 WCHAR* tmsg = new WCHAR[len-strlen(str_)];
69 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
70 0, err, 0, tmsg, len-1-strlen(str_), 0);
71 WideCharToMultiByte(CP_ACP, 0, tmsg, wcslen(tmsg)+1,
72 str_+strlen(str_), len-strlen(str_), 0, 0);
73 delete [] tmsg;
74#else
75 char* currStr = str_+strlen(str_);
76 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
77 0, err, 0, currStr, len-1-strlen(str_), 0);
78#endif
79 int l = strlen(str_);
80 if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
81 str_[l-2] = 0;
82 }
83
84#else
85 strncat(str_, strerror(err), len-1-strlen(str_));
86#endif
87 strncat(str_, " (", len-1-strlen(str_));
88 char buf[20];
89#ifdef WIN32
90 if (err < 0)
91 sprintf(buf, "%x", err);
92 else
93#endif
94 sprintf(buf,"%d",err);
95 strncat(str_, buf, len-1-strlen(str_));
96 strncat(str_, ")", len-1-strlen(str_));
97}
Adam Tkac35e6d4c2010-04-23 14:12:18 +000098