From 620a685af213f0dc2e6b41d821ff786dab0b6ffe Mon Sep 17 00:00:00 2001 From: Sara Golemon Date: Thu, 16 Dec 2004 22:44:28 +0000 Subject: [PATCH] Add libssh2_session_last_error() --- README | 1 + include/libssh2.h | 4 +++- src/session.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/README b/README index 9108095..7260c07 100644 --- a/README +++ b/README @@ -28,6 +28,7 @@ Version 0.2 Added libssh2_session_abstract() for retreiving &session->abstract + Added libssh2_session_last_error() for retreiving error codes/messages Version 0.1 ----------- diff --git a/include/libssh2.h b/include/libssh2.h index 2fdcf69..eff0d82 100644 --- a/include/libssh2.h +++ b/include/libssh2.h @@ -43,7 +43,7 @@ #include #define LIBSSH2_VERSION "0.1" -#define LIBSSH2_APINO 200412091526 +#define LIBSSH2_APINO 200412161442 /* Part of every banner, user specified or not */ #define LIBSSH2_SSH_BANNER "SSH-2.0-libssh2_" LIBSSH2_VERSION @@ -234,6 +234,8 @@ LIBSSH2_API void libssh2_session_methods(LIBSSH2_SESSION *session, char **kex, char **comp_cs, char **comp_sc, char **lang_cs, char **lang_sc); +LIBSSH2_API int libssh2_session_last_error(LIBSSH2_SESSION *session, char **errmsg, int *errmsg_len, int want_buf); + /* Userauth API */ LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session, char *username, int username_len); LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session); diff --git a/src/session.c b/src/session.c index b2d8088..80315ff 100644 --- a/src/session.c +++ b/src/session.c @@ -493,3 +493,58 @@ LIBSSH2_API void **libssh2_session_abstract(LIBSSH2_SESSION *session) return &session->abstract; } /* }}} */ + +/* {{{ libssh2_session_last_error + * Returns error code and populates an error string into errmsg + * If want_buf is non-zero then the string placed into errmsg must be freed by the calling program + * Otherwise it is assumed to be owned by libssh2 + */ +LIBSSH2_API int libssh2_session_last_error(LIBSSH2_SESSION *session, char **errmsg, int *errmsg_len, int want_buf) +{ + /* No error to report */ + if (!session->err_code) { + if (errmsg) { + if (want_buf) { + *errmsg = LIBSSH2_ALLOC(session, 1); + if (*errmsg) { + **errmsg = 0; + } + } else { + *errmsg = ""; + } + } + if (errmsg_len) { + *errmsg_len = 0; + } + return 0; + } + + if (errmsg) { + char *serrmsg = session->err_msg ? session->err_msg : ""; + int ownbuf = session->err_msg ? session->err_should_free : 0; + + if (want_buf) { + if (ownbuf) { + /* Just give the calling program the buffer */ + *errmsg = serrmsg; + session->err_should_free = 0; + } else { + /* Make a copy so the calling program can own it */ + *errmsg = LIBSSH2_ALLOC(session, session->err_msglen + 1); + if (*errmsg) { + memcpy(*errmsg, session->err_msg, session->err_msglen); + (*errmsg)[session->err_msglen] = 0; + } + } + } else { + *errmsg = serrmsg; + } + } + + if (errmsg_len) { + *errmsg_len = session->err_msglen; + } + + return session->err_code; +} +/* }}} */