aboutsummaryrefslogtreecommitdiff
path: root/src/error.c
blob: bd755c4f2dc58ac3c11f74a1e0733279e8386db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * 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.
 */

#include <stdio.h>
#include <stdarg.h>
#include "libssh/priv.h"
#include "libssh/session.h"

/**
 * @defgroup libssh_error The SSH error functions.
 * @ingroup libssh
 *
 * Functions for error handling.
 *
 * @{
 */

/**
 * @internal
 *
 * @brief Registers an error with a description.
 *
 * @param  error       The place to store the error.
 *
 * @param  code        The class of error.
 *
 * @param  descr       The description, which can be a format string.
 *
 * @param  ...         The arguments for the format string.
 */
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;
    if (ssh_get_log_level() >= SSH_LOG_WARN) {
        ssh_log_function(SSH_LOG_WARN,
                         function,
                         err->error.error_buffer);
    }
}

/**
 * @internal
 *
 * @brief Registers an out of memory error
 *
 * @param  error       The place to store the error.
 *
 */
void _ssh_set_error_oom(void *error, const char *function)
{
    struct error_struct *err = error;

    snprintf(err->error_buffer, sizeof(err->error_buffer),
            "%s: Out of memory", function);
    err->error_code = SSH_FATAL;
}

/**
 * @internal
 *
 * @brief Registers an invalid argument error
 *
 * @param  error       The place to store the error.
 *
 * @param  function    The function the error happened in.
 *
 */
void _ssh_set_error_invalid(void *error, const char *function)
{
    _ssh_set_error(error, SSH_FATAL, function,
                   "Invalid argument in %s", function);
}

/**
 * @brief Retrieve the error text message from the last error.
 *
 * @param  error        An ssh_session or ssh_bind.
 *
 * @return A static string describing the error.
 */
const char *ssh_get_error(void *error) {
  struct error_struct *err = error;

  return err->error_buffer;
}

/**
 * @brief Retrieve the error code from the last error.
 *
 * @param  error        An ssh_session or ssh_bind.
 *
 * \return SSH_NO_ERROR       No error occurred\n
 *         SSH_REQUEST_DENIED The last request was denied but situation is
 *                            recoverable\n
 *         SSH_FATAL          A fatal error occurred. This could be an unexpected
 *                            disconnection\n
 *
 *         Other error codes are internal but can be considered same than
 *         SSH_FATAL.
 */
int ssh_get_error_code(void *error) {
  struct error_struct *err = error;

  return err->error_code;
}

/** @} */

/* vim: set ts=4 sw=4 et cindent: */