PageRenderTime 186ms CodeModel.GetById 21ms RepoModel.GetById 2ms app.codeStats 0ms

/src/error.c

http://github.com/substack/libssh
C | 124 lines | 28 code | 12 blank | 84 comment | 0 complexity | 14a109ef3329d9e5947114524b339bd6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * error.c - functions for ssh error handling
  3. *
  4. * This file is part of the SSH Library
  5. *
  6. * Copyright (c) 2003-2008 by Aris Adamantiadis
  7. *
  8. * The SSH Library is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * The SSH Library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  16. * License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with the SSH Library; see the file COPYING. If not, write to
  20. * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  21. * MA 02111-1307, USA.
  22. */
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include "libssh/priv.h"
  26. /**
  27. * @defgroup libssh_error The SSH error functions.
  28. * @ingroup libssh
  29. *
  30. * Functions for error handling.
  31. *
  32. * @{
  33. */
  34. /**
  35. * @internal
  36. *
  37. * @brief Registers an error with a description.
  38. *
  39. * @param error The place to store the error.
  40. *
  41. * @param code The class of error.
  42. *
  43. * @param descr The description, which can be a format string.
  44. *
  45. * @param ... The arguments for the format string.
  46. */
  47. void ssh_set_error(void *error, int code, const char *descr, ...) {
  48. struct error_struct *err = error;
  49. va_list va;
  50. va_start(va, descr);
  51. vsnprintf(err->error_buffer, ERROR_BUFFERLEN, descr, va);
  52. va_end(va);
  53. err->error_code = code;
  54. ssh_log(error,SSH_LOG_RARE,"Error : %s",err->error_buffer);
  55. }
  56. /**
  57. * @internal
  58. *
  59. * @brief Registers an out of memory error
  60. *
  61. * @param error The place to store the error.
  62. *
  63. */
  64. void ssh_set_error_oom(void *error) {
  65. struct error_struct *err = error;
  66. strcpy(err->error_buffer, "Out of memory");
  67. err->error_code = SSH_FATAL;
  68. }
  69. /**
  70. * @internal
  71. *
  72. * @brief Registers an invalid argument error
  73. *
  74. * @param error The place to store the error.
  75. *
  76. * @param function The function the error happened in.
  77. *
  78. */
  79. void ssh_set_error_invalid(void *error, const char *function) {
  80. ssh_set_error(error, SSH_FATAL, "Invalid argument in %s", function);
  81. }
  82. /**
  83. * @brief Retrieve the error text message from the last error.
  84. *
  85. * @param error The SSH session pointer.
  86. *
  87. * @return A static string describing the error.
  88. */
  89. const char *ssh_get_error(void *error) {
  90. struct error_struct *err = error;
  91. return err->error_buffer;
  92. }
  93. /**
  94. * @brief Retrieve the error code from the last error.
  95. *
  96. * @param error The SSH session pointer.
  97. *
  98. * \return SSH_NO_ERROR No error occurred\n
  99. * SSH_REQUEST_DENIED The last request was denied but situation is
  100. * recoverable\n
  101. * SSH_FATAL A fatal error occurred. This could be an unexpected
  102. * disconnection\n
  103. *
  104. * Other error codes are internal but can be considered same than
  105. * SSH_FATAL.
  106. */
  107. int ssh_get_error_code(void *error) {
  108. struct error_struct *err = error;
  109. return err->error_code;
  110. }
  111. /** @} */
  112. /* vim: set ts=4 sw=4 et cindent: */