Blame src/error.c

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