2005-07-05 01:21:44 +00:00
|
|
|
/*
|
2009-03-29 20:19:18 +00:00
|
|
|
* error.c - functions for ssh error handling
|
|
|
|
*
|
|
|
|
* This file is part of the SSH Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 by Aris Adamantiadis
|
|
|
|
*
|
|
|
|
* The SSH Library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SSH Library is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with the SSH Library; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
|
|
|
* MA 02111-1307, USA.
|
|
|
|
*/
|
2005-07-05 01:21:44 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "libssh/priv.h"
|
2011-07-19 22:16:28 +02:00
|
|
|
#include "libssh/session.h"
|
2009-03-29 20:19:18 +00:00
|
|
|
|
2009-05-05 07:25:07 +00:00
|
|
|
/**
|
2010-04-04 15:10:39 +02:00
|
|
|
* @defgroup libssh_error The SSH error functions.
|
|
|
|
* @ingroup libssh
|
|
|
|
*
|
|
|
|
* Functions for error handling.
|
2009-05-05 07:25:07 +00:00
|
|
|
*
|
2006-11-12 00:14:55 +00:00
|
|
|
* @{
|
|
|
|
*/
|
2005-07-05 01:21:44 +00:00
|
|
|
|
2009-05-05 07:25:07 +00:00
|
|
|
/**
|
2009-05-05 07:26:19 +00:00
|
|
|
* @internal
|
|
|
|
*
|
2009-05-05 07:25:07 +00:00
|
|
|
* @brief Registers an error with a description.
|
|
|
|
*
|
2009-10-04 14:03:25 +02:00
|
|
|
* @param error The place to store the error.
|
2009-05-05 07:25:07 +00:00
|
|
|
*
|
|
|
|
* @param code The class of error.
|
|
|
|
*
|
|
|
|
* @param descr The description, which can be a format string.
|
|
|
|
*
|
|
|
|
* @param ... The arguments for the format string.
|
|
|
|
*/
|
2011-09-17 10:28:39 +02:00
|
|
|
void _ssh_set_error(void *error,
|
|
|
|
int code,
|
|
|
|
const char *function,
|
|
|
|
const char *descr, ...)
|
|
|
|
{
|
|
|
|
struct ssh_common_struct *err = error;
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, descr);
|
|
|
|
vsnprintf(err->error.error_buffer, ERROR_BUFFERLEN, descr, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
err->error.error_code = code;
|
2013-07-21 11:26:55 +02:00
|
|
|
if (ssh_get_log_level() >= SSH_LOG_WARN) {
|
|
|
|
ssh_log_function(SSH_LOG_WARN,
|
|
|
|
function,
|
|
|
|
err->error.error_buffer);
|
|
|
|
}
|
2005-07-05 01:21:44 +00:00
|
|
|
}
|
|
|
|
|
2009-10-04 14:03:25 +02:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @brief Registers an out of memory error
|
|
|
|
*
|
|
|
|
* @param error The place to store the error.
|
|
|
|
*
|
|
|
|
*/
|
2011-09-17 10:28:39 +02:00
|
|
|
void _ssh_set_error_oom(void *error, const char *function)
|
|
|
|
{
|
|
|
|
struct error_struct *err = error;
|
2009-10-04 14:03:25 +02:00
|
|
|
|
2011-09-17 10:28:39 +02:00
|
|
|
snprintf(err->error_buffer, sizeof(err->error_buffer),
|
|
|
|
"%s: Out of memory", function);
|
|
|
|
err->error_code = SSH_FATAL;
|
2009-10-04 14:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
2009-10-05 00:03:47 +02:00
|
|
|
* @brief Registers an invalid argument error
|
2009-10-04 14:03:25 +02:00
|
|
|
*
|
|
|
|
* @param error The place to store the error.
|
|
|
|
*
|
|
|
|
* @param function The function the error happened in.
|
|
|
|
*
|
|
|
|
*/
|
2011-09-17 10:28:39 +02:00
|
|
|
void _ssh_set_error_invalid(void *error, const char *function)
|
|
|
|
{
|
|
|
|
_ssh_set_error(error, SSH_FATAL, function,
|
|
|
|
"Invalid argument in %s", function);
|
2009-10-04 14:03:25 +02:00
|
|
|
}
|
|
|
|
|
2009-05-05 07:27:16 +00:00
|
|
|
/**
|
2009-05-05 07:29:36 +00:00
|
|
|
* @brief Retrieve the error text message from the last error.
|
2009-05-05 07:27:16 +00:00
|
|
|
*
|
2014-01-21 13:29:29 -06:00
|
|
|
* @param error An ssh_session or ssh_bind.
|
2009-05-05 07:27:16 +00:00
|
|
|
*
|
|
|
|
* @return A static string describing the error.
|
2006-11-12 00:14:55 +00:00
|
|
|
*/
|
2009-05-05 07:27:16 +00:00
|
|
|
const char *ssh_get_error(void *error) {
|
|
|
|
struct error_struct *err = error;
|
|
|
|
|
|
|
|
return err->error_buffer;
|
2005-07-05 01:21:44 +00:00
|
|
|
}
|
|
|
|
|
2009-05-05 07:29:36 +00:00
|
|
|
/**
|
|
|
|
* @brief Retrieve the error code from the last error.
|
|
|
|
*
|
2014-01-21 13:29:29 -06:00
|
|
|
* @param error An ssh_session or ssh_bind.
|
2009-05-05 07:29:36 +00:00
|
|
|
*
|
2009-09-26 01:34:14 +02:00
|
|
|
* \return SSH_NO_ERROR No error occurred\n
|
2009-05-05 07:29:36 +00:00
|
|
|
* SSH_REQUEST_DENIED The last request was denied but situation is
|
|
|
|
* recoverable\n
|
2009-09-26 01:34:14 +02:00
|
|
|
* SSH_FATAL A fatal error occurred. This could be an unexpected
|
2009-05-05 07:29:36 +00:00
|
|
|
* disconnection\n
|
|
|
|
*
|
2009-09-26 01:34:14 +02:00
|
|
|
* Other error codes are internal but can be considered same than
|
2009-05-05 07:29:36 +00:00
|
|
|
* SSH_FATAL.
|
2006-11-12 00:14:55 +00:00
|
|
|
*/
|
2009-05-05 07:29:36 +00:00
|
|
|
int ssh_get_error_code(void *error) {
|
|
|
|
struct error_struct *err = error;
|
|
|
|
|
|
|
|
return err->error_code;
|
2005-07-05 01:21:44 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 21:32:08 +02:00
|
|
|
/** @} */
|
2010-04-04 15:10:39 +02:00
|
|
|
|
|
|
|
/* vim: set ts=4 sw=4 et cindent: */
|