Line | Count | Source |
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 | | |
24 | | #include "config.h" |
25 | | |
26 | | #include <stdio.h> |
27 | | #include <stdarg.h> |
28 | | #include "libssh/priv.h" |
29 | | #include "libssh/session.h" |
30 | | |
31 | | /** |
32 | | * @defgroup libssh_error The SSH error functions |
33 | | * @ingroup libssh |
34 | | * |
35 | | * Functions for error handling. |
36 | | * |
37 | | * @{ |
38 | | */ |
39 | | |
40 | | /** |
41 | | * @internal |
42 | | * |
43 | | * @brief Registers an error with a description. |
44 | | * |
45 | | * @param error The place to store the error. |
46 | | * |
47 | | * @param code The class of error. |
48 | | * |
49 | | * @param descr The description, which can be a format string. |
50 | | * |
51 | | * @param ... The arguments for the format string. |
52 | | */ |
53 | | void _ssh_set_error(void *error, |
54 | | int code, |
55 | | const char *function, |
56 | | const char *descr, ...) |
57 | 37.9k | { |
58 | 37.9k | struct ssh_common_struct *err = error; |
59 | 37.9k | va_list va; |
60 | | |
61 | 37.9k | va_start(va, descr); |
62 | 37.9k | vsnprintf(err->error.error_buffer, ERROR_BUFFERLEN, descr, va); |
63 | 37.9k | va_end(va); |
64 | | |
65 | 37.9k | err->error.error_code = code; |
66 | 37.9k | if (ssh_get_log_level() == SSH_LOG_TRACE) { |
67 | 14 | ssh_log_function(SSH_LOG_TRACE, |
68 | 14 | function, |
69 | 14 | err->error.error_buffer); |
70 | 14 | } |
71 | 37.9k | } |
72 | | |
73 | | /** |
74 | | * @internal |
75 | | * |
76 | | * @brief Registers an out of memory error |
77 | | * |
78 | | * @param error The place to store the error. |
79 | | * |
80 | | */ |
81 | | void _ssh_set_error_oom(void *error, const char *function) |
82 | 387 | { |
83 | 387 | struct error_struct *err = error; |
84 | | |
85 | 387 | snprintf(err->error_buffer, sizeof(err->error_buffer), |
86 | 387 | "%s: Out of memory", function); |
87 | 387 | err->error_code = SSH_FATAL; |
88 | 387 | } |
89 | | |
90 | | /** |
91 | | * @internal |
92 | | * |
93 | | * @brief Registers an invalid argument error |
94 | | * |
95 | | * @param error The place to store the error. |
96 | | * |
97 | | * @param function The function the error happened in. |
98 | | * |
99 | | */ |
100 | | void _ssh_set_error_invalid(void *error, const char *function) |
101 | 12.6k | { |
102 | 12.6k | _ssh_set_error(error, SSH_FATAL, function, |
103 | 12.6k | "Invalid argument in %s", function); |
104 | 12.6k | } |
105 | | |
106 | | /** |
107 | | * @internal |
108 | | * |
109 | | * @brief Reset the error code and message |
110 | | * |
111 | | * @param error The place to reset the error. |
112 | | */ |
113 | | void ssh_reset_error(void *error) |
114 | 0 | { |
115 | 0 | struct ssh_common_struct *err = error; |
116 | |
|
117 | 0 | ZERO_STRUCT(err->error.error_buffer); |
118 | 0 | err->error.error_code = 0; |
119 | 0 | } |
120 | | |
121 | | /** |
122 | | * @brief Retrieve the error text message from the last error. |
123 | | * |
124 | | * @param error An ssh_session or ssh_bind. |
125 | | * |
126 | | * @return A static string describing the error. |
127 | | */ |
128 | 2.38k | const char *ssh_get_error(void *error) { |
129 | 2.38k | struct error_struct *err = error; |
130 | | |
131 | 2.38k | return err->error_buffer; |
132 | 2.38k | } |
133 | | |
134 | | /** |
135 | | * @brief Retrieve the error code from the last error. |
136 | | * |
137 | | * @param error An ssh_session or ssh_bind. |
138 | | * |
139 | | * \return SSH_NO_ERROR No error occurred\n |
140 | | * SSH_REQUEST_DENIED The last request was denied but situation is |
141 | | * recoverable\n |
142 | | * SSH_FATAL A fatal error occurred. This could be an unexpected |
143 | | * disconnection\n |
144 | | * |
145 | | * Other error codes are internal but can be considered the same as |
146 | | * SSH_FATAL. |
147 | | */ |
148 | 0 | int ssh_get_error_code(void *error) { |
149 | 0 | struct error_struct *err = error; |
150 | |
|
151 | 0 | return err->error_code; |
152 | 0 | } |
153 | | |
154 | | /** @} */ |