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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include <ws2tcpip.h>
  32. #else
  33. #include <netdb.h>
  34. #endif
  35. #include <string.h>
  36. #ifdef HAVE_GNUTLS
  37. #include <gnutls/gnutls.h>
  38. #endif
  39. using namespace rdr;
  40. Exception::Exception(const char *format, ...) {
  41. va_list ap;
  42. va_start(ap, format);
  43. (void) vsnprintf(str_, len, format, ap);
  44. va_end(ap);
  45. }
  46. GAIException::GAIException(const char* s, int err)
  47. : Exception("%s", s)
  48. {
  49. strncat(str_, ": ", len-1-strlen(str_));
  50. #ifdef _WIN32
  51. wchar_t *currStr = new wchar_t[len-strlen(str_)];
  52. wcsncpy(currStr, gai_strerrorW(err), len-1-strlen(str_));
  53. WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_),
  54. len-1-strlen(str_), 0, 0);
  55. delete [] currStr;
  56. #else
  57. strncat(str_, gai_strerror(err), len-1-strlen(str_));
  58. #endif
  59. strncat(str_, " (", len-1-strlen(str_));
  60. char buf[20];
  61. #ifdef WIN32
  62. if (err < 0)
  63. sprintf(buf, "%x", err);
  64. else
  65. #endif
  66. sprintf(buf,"%d",err);
  67. strncat(str_, buf, len-1-strlen(str_));
  68. strncat(str_, ")", len-1-strlen(str_));
  69. }
  70. SystemException::SystemException(const char* s, int err_)
  71. : Exception("%s", s), err(err_)
  72. {
  73. strncat(str_, ": ", len-1-strlen(str_));
  74. #ifdef _WIN32
  75. wchar_t *currStr = new wchar_t[len-strlen(str_)];
  76. FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  77. 0, err, 0, currStr, len-1-strlen(str_), 0);
  78. WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_),
  79. len-1-strlen(str_), 0, 0);
  80. delete [] currStr;
  81. int l = strlen(str_);
  82. if ((l >= 2) && (str_[l-2] == '\r') && (str_[l-1] == '\n'))
  83. str_[l-2] = 0;
  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. }