You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Exception.cxx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2004 Red Hat Inc.
  3. * Copyright (C) 2010 TigerVNC Team
  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. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <rdr/Exception.h>
  26. #include <rdr/TLSException.h>
  27. #ifdef _WIN32
  28. #include <tchar.h>
  29. #include <winsock2.h>
  30. #include <windows.h>
  31. #endif
  32. #include <string.h>
  33. #ifdef HAVE_GNUTLS
  34. #include <gnutls/gnutls.h>
  35. #endif
  36. using namespace rdr;
  37. Exception::Exception(const char *format, ...) {
  38. va_list ap;
  39. va_start(ap, format);
  40. (void) vsnprintf(str_, len, format, ap);
  41. va_end(ap);
  42. }
  43. SystemException::SystemException(const char* s, int err_)
  44. : Exception("%s", s), err(err_)
  45. {
  46. strncat(str_, ": ", len-1-strlen(str_));
  47. #ifdef _WIN32
  48. // Windows error messages are crap, so use unix ones for common errors.
  49. const char* msg = 0;
  50. switch (err) {
  51. case WSAECONNREFUSED: msg = "Connection refused"; break;
  52. case WSAETIMEDOUT: msg = "Connection timed out"; break;
  53. case WSAECONNRESET: msg = "Connection reset by peer"; break;
  54. case WSAECONNABORTED: msg = "Connection aborted"; break;
  55. }
  56. if (msg) {
  57. strncat(str_, msg, len-1-strlen(str_));
  58. } else {
  59. #ifdef UNICODE
  60. WCHAR* tmsg = new WCHAR[len-strlen(str_)];
  61. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  62. 0, err, 0, tmsg, len-1-strlen(str_), 0);
  63. WideCharToMultiByte(CP_ACP, 0, tmsg, wcslen(tmsg)+1,
  64. str_+strlen(str_), len-strlen(str_), 0, 0);
  65. delete [] tmsg;
  66. #else
  67. char* currStr = str_+strlen(str_);
  68. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  69. 0, err, 0, currStr, len-1-strlen(str_), 0);
  70. #endif
  71. int l = strlen(str_);
  72. if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
  73. str_[l-2] = 0;
  74. }
  75. #else
  76. strncat(str_, strerror(err), len-1-strlen(str_));
  77. #endif
  78. strncat(str_, " (", len-1-strlen(str_));
  79. char buf[20];
  80. #ifdef WIN32
  81. if (err < 0)
  82. sprintf(buf, "%x", err);
  83. else
  84. #endif
  85. sprintf(buf,"%d",err);
  86. strncat(str_, buf, len-1-strlen(str_));
  87. strncat(str_, ")", len-1-strlen(str_));
  88. }