|
Packit Service |
31306d |
/*
|
|
Packit Service |
31306d |
* error.c - functions for ssh error handling
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* This file is part of the SSH Library
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* Copyright (c) 2003-2008 by Aris Adamantiadis
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* The SSH Library is free software; you can redistribute it and/or modify
|
|
Packit Service |
31306d |
* it under the terms of the GNU Lesser General Public License as published by
|
|
Packit Service |
31306d |
* the Free Software Foundation; either version 2.1 of the License, or (at your
|
|
Packit Service |
31306d |
* option) any later version.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* The SSH Library is distributed in the hope that it will be useful, but
|
|
Packit Service |
31306d |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
Packit Service |
31306d |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
Packit Service |
31306d |
* License for more details.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
31306d |
* along with the SSH Library; see the file COPYING. If not, write to
|
|
Packit Service |
31306d |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
|
|
Packit Service |
31306d |
* MA 02111-1307, USA.
|
|
Packit Service |
31306d |
*/
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
#include "config.h"
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
#include <stdio.h>
|
|
Packit Service |
31306d |
#include <stdarg.h>
|
|
Packit Service |
31306d |
#include "libssh/priv.h"
|
|
Packit Service |
31306d |
#include "libssh/session.h"
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
/**
|
|
Packit Service |
31306d |
* @defgroup libssh_error The SSH error functions.
|
|
Packit Service |
31306d |
* @ingroup libssh
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* Functions for error handling.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @{
|
|
Packit Service |
31306d |
*/
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
/**
|
|
Packit Service |
31306d |
* @internal
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @brief Registers an error with a description.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param error The place to store the error.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param code The class of error.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param descr The description, which can be a format string.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param ... The arguments for the format string.
|
|
Packit Service |
31306d |
*/
|
|
Packit Service |
31306d |
void _ssh_set_error(void *error,
|
|
Packit Service |
31306d |
int code,
|
|
Packit Service |
31306d |
const char *function,
|
|
Packit Service |
31306d |
const char *descr, ...)
|
|
Packit Service |
31306d |
{
|
|
Packit Service |
31306d |
struct ssh_common_struct *err = error;
|
|
Packit Service |
31306d |
va_list va;
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
va_start(va, descr);
|
|
Packit Service |
31306d |
vsnprintf(err->error.error_buffer, ERROR_BUFFERLEN, descr, va);
|
|
Packit Service |
31306d |
va_end(va);
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
err->error.error_code = code;
|
|
Packit Service |
31306d |
if (ssh_get_log_level() >= SSH_LOG_WARN) {
|
|
Packit Service |
31306d |
ssh_log_function(SSH_LOG_WARN,
|
|
Packit Service |
31306d |
function,
|
|
Packit Service |
31306d |
err->error.error_buffer);
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
/**
|
|
Packit Service |
31306d |
* @internal
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @brief Registers an out of memory error
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param error The place to store the error.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
*/
|
|
Packit Service |
31306d |
void _ssh_set_error_oom(void *error, const char *function)
|
|
Packit Service |
31306d |
{
|
|
Packit Service |
31306d |
struct error_struct *err = error;
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
snprintf(err->error_buffer, sizeof(err->error_buffer),
|
|
Packit Service |
31306d |
"%s: Out of memory", function);
|
|
Packit Service |
31306d |
err->error_code = SSH_FATAL;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
/**
|
|
Packit Service |
31306d |
* @internal
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @brief Registers an invalid argument error
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param error The place to store the error.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param function The function the error happened in.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
*/
|
|
Packit Service |
31306d |
void _ssh_set_error_invalid(void *error, const char *function)
|
|
Packit Service |
31306d |
{
|
|
Packit Service |
31306d |
_ssh_set_error(error, SSH_FATAL, function,
|
|
Packit Service |
31306d |
"Invalid argument in %s", function);
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
/**
|
|
Packit Service |
31306d |
* @internal
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @brief Reset the error code and message
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param error The place to reset the error.
|
|
Packit Service |
31306d |
*/
|
|
Packit Service |
31306d |
void ssh_reset_error(void *error)
|
|
Packit Service |
31306d |
{
|
|
Packit Service |
31306d |
struct ssh_common_struct *err = error;
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
ZERO_STRUCT(err->error.error_buffer);
|
|
Packit Service |
31306d |
err->error.error_code = 0;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
/**
|
|
Packit Service |
31306d |
* @brief Retrieve the error text message from the last error.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param error An ssh_session or ssh_bind.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @return A static string describing the error.
|
|
Packit Service |
31306d |
*/
|
|
Packit Service |
31306d |
const char *ssh_get_error(void *error) {
|
|
Packit Service |
31306d |
struct error_struct *err = error;
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
return err->error_buffer;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
/**
|
|
Packit Service |
31306d |
* @brief Retrieve the error code from the last error.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* @param error An ssh_session or ssh_bind.
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* \return SSH_NO_ERROR No error occurred\n
|
|
Packit Service |
31306d |
* SSH_REQUEST_DENIED The last request was denied but situation is
|
|
Packit Service |
31306d |
* recoverable\n
|
|
Packit Service |
31306d |
* SSH_FATAL A fatal error occurred. This could be an unexpected
|
|
Packit Service |
31306d |
* disconnection\n
|
|
Packit Service |
31306d |
*
|
|
Packit Service |
31306d |
* Other error codes are internal but can be considered same than
|
|
Packit Service |
31306d |
* SSH_FATAL.
|
|
Packit Service |
31306d |
*/
|
|
Packit Service |
31306d |
int ssh_get_error_code(void *error) {
|
|
Packit Service |
31306d |
struct error_struct *err = error;
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
return err->error_code;
|
|
Packit Service |
31306d |
}
|
|
Packit Service |
31306d |
|
|
Packit Service |
31306d |
/** @} */
|