Add libssh2_trace_sethandler() to the API (even more)

This commit is contained in:
Dave McCaldon 2010-01-15 22:58:44 +01:00 committed by Daniel Stenberg
parent 474e38119b
commit 44eba0c993
7 changed files with 80 additions and 23 deletions

3
NEWS
View File

@ -1,5 +1,8 @@
Version 1.2.3 (unreleased) Version 1.2.3 (unreleased)
o Added libssh2_trace_sethandler()
o Added the direct_tcpip.c example
o Fixed memory leak in userauth_publickey
o Added support for authentication via SSH-Agent. By Daiki Ueno. o Added support for authentication via SSH-Agent. By Daiki Ueno.
Version 1.2.2 (November 16, 2009) Version 1.2.2 (November 16, 2009)

View File

@ -97,6 +97,7 @@ dist_man_MANS = \
libssh2_sftp_unlink_ex.3 \ libssh2_sftp_unlink_ex.3 \
libssh2_sftp_write.3 \ libssh2_sftp_write.3 \
libssh2_trace.3 \ libssh2_trace.3 \
libssh2_trace_sethandler.3 \
libssh2_userauth_authenticated.3 \ libssh2_userauth_authenticated.3 \
libssh2_userauth_hostbased_fromfile_ex.3 \ libssh2_userauth_hostbased_fromfile_ex.3 \
libssh2_userauth_keyboard_interactive_ex.3 \ libssh2_userauth_keyboard_interactive_ex.3 \

View File

@ -1,19 +1,26 @@
.\" $Id: libssh2_trace_sethandler.3,v 1.1 2008/12/26 07:46:45 bagder Exp $ .\" $Id: libssh2_trace_sethandler.3,v 1.1 2008/12/26 07:46:45 bagder Exp $
.\" .\"
.TH libssh2_trace_sethandler 3 "03 Nov 2009" "libssh2 1.2" "libssh2 manual" .TH libssh2_trace_sethandler 3 "15 Jan 2010" "libssh2 1.2.3" "libssh2 manual"
.SH NAME .SH NAME
libssh2_trace_sethandler - set a trace output handler libssh2_trace_sethandler - set a trace output handler
.SH SYNOPSIS .SH SYNOPSIS
.nf
#include <libssh2.h> #include <libssh2.h>
typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*, const char*, size_t); typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION *session,
const char *data,
LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session, libssh2_trace_handler_func callback); size_t length);
int libssh2_trace_sethandler(LIBSSH2_SESSION *session,
libssh2_trace_handler_func callback);
.SH DESCRIPTION .SH DESCRIPTION
This function can be called to install a trace output handler for your application. By default, when libssh2_trace_sethandler installs a trace output handler for your application.
tracing has been switched on via a call to libssh2_trace(), any output is written to stderr. By calling By default, when tracing has been switched on via a call to libssh2_trace(),
this method and passing a function pointer that matches the libssh2_trace_handler_func prototype, libssh2 all output is written to stderr. By calling this method and passing a
will call back as it generates trace output. This can be used to capture the trace output and put it into function pointer that matches the libssh2_trace_handler_func prototype,
a log file or diagnostic window. This function has no effect unless libssh2 was built to support libssh2 will call back as it generates trace output. This can be used to
this option, and a typical "release build" might not. capture the trace output and put it into a log file or diagnostic window.
This function has no effect unless libssh2 was built to support this option,
and a typical "release build" might not.
.SH AVAILABILITY
Added in libssh2 version 1.2.3

View File

@ -987,6 +987,11 @@ LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask);
#define LIBSSH2_TRACE_PUBLICKEY (1<<8) #define LIBSSH2_TRACE_PUBLICKEY (1<<8)
#define LIBSSH2_TRACE_SOCKET (1<<9) #define LIBSSH2_TRACE_SOCKET (1<<9)
typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*, const char *,
size_t);
LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session,
libssh2_trace_handler_func callback);
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif

View File

@ -735,6 +735,7 @@ struct _LIBSSH2_SESSION
struct transportpacket packet; struct transportpacket packet;
#ifdef LIBSSH2DEBUG #ifdef LIBSSH2DEBUG
int showmask; /* what debug/trace messages to display */ int showmask; /* what debug/trace messages to display */
libssh2_trace_handler_func tracehandler; /* callback to display trace messages */
#endif #endif
/* State variables used in libssh2_banner_send() */ /* State variables used in libssh2_banner_send() */

View File

@ -312,6 +312,13 @@ libssh2_trace(LIBSSH2_SESSION * session, int bitmask)
return 0; return 0;
} }
LIBSSH2_API int
libssh2_trace_sethandler(LIBSSH2_SESSION *session, libssh2_trace_handler_func callback)
{
session->tracehandler = callback;
return 0;
}
void void
_libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...) _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
{ {
@ -353,8 +360,12 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
len += vsnprintf(buffer + len, 1535 - len, format, vargs); len += vsnprintf(buffer + len, 1535 - len, format, vargs);
buffer[len] = '\n'; buffer[len] = '\n';
va_end(vargs); va_end(vargs);
write(2, buffer, len + 1);
if (session->tracehandler) {
(session->tracehandler)(session, buffer, len + 1);
} else {
write(2, buffer, len + 1);
}
} }
#else #else
@ -365,6 +376,14 @@ libssh2_trace(LIBSSH2_SESSION * session, int bitmask)
(void) bitmask; (void) bitmask;
return 0; return 0;
} }
LIBSSH2_API int
libssh2_trace_sethandler(LIBSSH2_SESSION *session, libssh2_trace_handler_func callback)
{
(void) session;
(void) callback;
return 0;
}
#endif #endif
/* init the list head */ /* init the list head */

View File

@ -41,6 +41,7 @@
#include "libssh2_priv.h" #include "libssh2_priv.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h>
#include <assert.h> #include <assert.h>
@ -57,36 +58,56 @@ debugdump(LIBSSH2_SESSION * session,
{ {
size_t i; size_t i;
size_t c; size_t c;
FILE *stream = stderr;
unsigned int width = 0x10; unsigned int width = 0x10;
char buffer[256]; /* Must be enough for width*4 + about 30 or so */
size_t used;
static const char* hex_chars = "0123456789ABCDEF";
if (!(session->showmask & LIBSSH2_TRACE_TRANS)) { if (!(session->showmask & LIBSSH2_TRACE_TRANS)) {
/* not asked for, bail out */ /* not asked for, bail out */
return; return;
} }
fprintf(stream, "=> %s (%d bytes)\n", desc, (int) size); used = snprintf(buffer, sizeof(buffer), "=> %s (%d bytes)\n",
desc, (int) size);
if (session->tracehandler)
(session->tracehandler)(session, buffer, used);
else
write(2 /* stderr */, buffer, used);
for(i = 0; i < size; i += width) { for(i = 0; i < size; i += width) {
fprintf(stream, "%04lx: ", (long)i); used = snprintf(buffer, sizeof(buffer), "%04lx: ", (long)i);
/* hex not disabled, show it */ /* hex not disabled, show it */
for(c = 0; c < width; c++) { for(c = 0; c < width; c++) {
if (i + c < size) if (i + c < size) {
fprintf(stream, "%02x ", ptr[i + c]); buffer[used++] = hex_chars[(ptr[i+c] >> 4) & 0xF];
else buffer[used++] = hex_chars[ptr[i+c] & 0xF];
fputs(" ", stream); }
else {
buffer[used++] = ' ';
buffer[used++] = ' ';
}
buffer[used++] = (c == (width/2)-1) ? ':' : ' ';
} }
buffer[used++] = ':';
buffer[used++] = ' ';
for(c = 0; (c < width) && (i + c < size); c++) { for(c = 0; (c < width) && (i + c < size); c++) {
fprintf(stream, "%c", buffer[used++] = isprint(ptr[i + c]) ?
(ptr[i + c] >= 0x20) && ptr[i + c] : UNPRINTABLE_CHAR;
(ptr[i + c] < 0x80) ? ptr[i + c] : UNPRINTABLE_CHAR);
} }
fputc('\n', stream); /* newline */ buffer[used++] = '\n';
buffer[used] = 0;
if (session->tracehandler)
(session->tracehandler)(session, buffer, used);
else
write(2, buffer, used);
} }
fflush(stream);
} }
#else #else
#define debugdump(a,x,y,z) #define debugdump(a,x,y,z)