Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 2 | * Copyright (C) 2004 Red Hat Inc. |
| 3 | * Copyright (C) 2010 TigerVNC Team |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 4 | * |
| 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 Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
Pierre Ossman | 5ad4d06 | 2014-07-07 14:13:46 +0200 | [diff] [blame^] | 25 | #include <stdio.h> |
| 26 | #include <stdarg.h> |
| 27 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 28 | #include <rdr/Exception.h> |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 29 | #include <rdr/TLSException.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 30 | #ifdef _WIN32 |
| 31 | #include <tchar.h> |
| 32 | #include <winsock2.h> |
| 33 | #include <windows.h> |
| 34 | #endif |
| 35 | |
Adam Tkac | 75586e6 | 2008-12-03 15:03:08 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Adam Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 38 | #ifdef HAVE_GNUTLS |
| 39 | #include <gnutls/gnutls.h> |
| 40 | #endif |
| 41 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | using namespace rdr; |
| 43 | |
Adam Tkac | 20e0d71 | 2008-11-14 14:48:21 +0000 | [diff] [blame] | 44 | Exception::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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 52 | SystemException::SystemException(const char* s, int err_) |
| 53 | : Exception(s), err(err_) |
| 54 | { |
| 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 Tkac | 35e6d4c | 2010-04-23 14:12:18 +0000 | [diff] [blame] | 99 | |