diff --git a/src/include/lib3270/trace.h b/src/include/lib3270/trace.h index 6414a0f..6eb79d9 100644 --- a/src/include/lib3270/trace.h +++ b/src/include/lib3270/trace.h @@ -65,7 +65,7 @@ LIB3270_EXPORT void lib3270_get_trace_handler(H3270 *hSession, LIB3270_TRACE_HANDLER *handler, void **userdata); /** - * Write on trace file. + * @brief Write on trace file. * * Write text on trace file, if DStrace is enabled. * @@ -76,7 +76,7 @@ LIB3270_EXPORT void lib3270_write_dstrace(H3270 *session, const char *fmt, ...) LIB3270_AS_PRINTF(2,3); /** - * Write on trace file. + * @brief Write on trace file. * * Write text on trace file, if network trace is enabled. * @@ -87,7 +87,7 @@ LIB3270_EXPORT void lib3270_write_nettrace(H3270 *session, const char *fmt, ...) LIB3270_AS_PRINTF(2,3); /** - * Write on trace file. + * @brief Write on trace file. * * Write text on trace file, if event is enabled. * @@ -97,6 +97,19 @@ */ LIB3270_EXPORT void lib3270_trace_event(H3270 *session, const char *fmt, ...) LIB3270_AS_PRINTF(2,3); + + /** + * @brief Write datablock on trace file. + * + * @param hSession TN3270 Session handle. + * @param msg Message. + * @param data Data block in ASCII. + * @param datalen Length of the data block. + * + */ + LIB3270_EXPORT void lib3270_trace_data(H3270 *hSession, const char *msg, const char *data, size_t datalen); + + #ifdef __cplusplus } #endif diff --git a/src/lib3270/ansi.c b/src/lib3270/ansi.c index 912db09..27da335 100644 --- a/src/lib3270/ansi.c +++ b/src/lib3270/ansi.c @@ -35,7 +35,10 @@ */ #pragma GCC diagnostic ignored "-Wsign-compare" -#pragma GCC diagnostic ignored "-Wstringop-truncation" + +#ifdef _WIN32 + #pragma GCC diagnostic ignored "-Wstringop-truncation" +#endif // _WIN32 #include "private.h" diff --git a/src/lib3270/ssl/linux/getcrl.c b/src/lib3270/ssl/linux/getcrl.c index 4c6a150..8305f9f 100644 --- a/src/lib3270/ssl/linux/getcrl.c +++ b/src/lib3270/ssl/linux/getcrl.c @@ -57,6 +57,7 @@ #include #include #include +#include /*--[ Implement ]------------------------------------------------------------------------------------*/ @@ -113,7 +114,9 @@ static inline void lib3270_autoptr_cleanup_CURL(CURL **ptr) typedef struct _curldata { size_t length; + H3270 * hSession; SSL_ERROR_MESSAGE * message; + char errbuf[CURL_ERROR_SIZE]; unsigned char contents[CRL_DATA_LENGTH]; } CURLDATA; @@ -145,7 +148,15 @@ static size_t internal_curl_write_callback(void *contents, size_t size, size_t n return 0; } - debug("Received %u bytes", (unsigned int) realsize); +#ifdef DEBUG + lib3270_trace_data( + data->hSession, + "Received", + (const char *) contents, + realsize + ); + +#endif // DEBUG memcpy(&(data->contents[data->length]),contents,realsize); data->length += realsize; @@ -153,6 +164,54 @@ static size_t internal_curl_write_callback(void *contents, size_t size, size_t n return realsize; } +static int internal_curl_trace_callback(CURL *handle unused, curl_infotype type, char *data, size_t size, void *userp) +{ + const char * text = NULL; + + switch (type) { + case CURLINFO_TEXT: + lib3270_write_log(((CURLDATA *) userp)->hSession,"curl","%s",data); + return 0; + + case CURLINFO_HEADER_OUT: + text = "=> Send header"; + break; + + case CURLINFO_DATA_OUT: + text = "=> Send data"; + break; + + case CURLINFO_SSL_DATA_OUT: + text = "=> Send SSL data"; + break; + + case CURLINFO_HEADER_IN: + text = "<= Recv header"; + break; + + case CURLINFO_DATA_IN: + text = "<= Recv data"; + break; + + case CURLINFO_SSL_DATA_IN: + text = "<= Recv SSL data"; + break; + + default: + return 0; + + } + + lib3270_trace_data( + ((CURLDATA *) userp)->hSession, + text, + data, + size + ); + + return 0; +} + #endif // HAVE_LIBCURL @@ -363,10 +422,12 @@ X509_CRL * lib3270_get_X509_CRL(H3270 *hSession, SSL_ERROR_MESSAGE * message) // Use CURL to download the CRL lib3270_autoptr(CURLDATA) crl_data = lib3270_malloc(sizeof(CURLDATA)); + lib3270_autoptr(CURL) hCurl = curl_easy_init(); memset(crl_data,0,sizeof(CURLDATA)); crl_data->message = message; + crl_data->hSession = hSession; if(hCurl) { @@ -375,19 +436,41 @@ X509_CRL * lib3270_get_X509_CRL(H3270 *hSession, SSL_ERROR_MESSAGE * message) curl_easy_setopt(hCurl, CURLOPT_URL, consturl); curl_easy_setopt(hCurl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, crl_data->errbuf); + curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, internal_curl_write_callback); curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, (void *) crl_data); + curl_easy_setopt(hCurl, CURLOPT_USERNAME, ""); + + if(lib3270_get_toggle(hSession,LIB3270_TOGGLE_SSL_TRACE)) + { + curl_easy_setopt(hCurl, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(hCurl, CURLOPT_DEBUGFUNCTION, internal_curl_trace_callback); + curl_easy_setopt(hCurl, CURLOPT_DEBUGDATA, (void *) crl_data); + } + res = curl_easy_perform(hCurl); if(res != CURLE_OK) { message->error = hSession->ssl.error = 0; message->title = N_( "Security error" ); - message->text = N_( "Error loading CRL" ); - message->description = curl_easy_strerror(res); + + if(crl_data->errbuf[0]) + { + message->text = curl_easy_strerror(res); + message->description = crl_data->errbuf; + } + else + { + message->text = N_( "Error loading CRL" ); + message->description = curl_easy_strerror(res); + } + lib3270_write_log(hSession,"ssl","%s: %s",consturl, message->description); return NULL; + } char *ct = NULL; diff --git a/src/lib3270/ssl/windows/getcrl.c b/src/lib3270/ssl/windows/getcrl.c index ffb7418..874a247 100644 --- a/src/lib3270/ssl/windows/getcrl.c +++ b/src/lib3270/ssl/windows/getcrl.c @@ -54,6 +54,7 @@ #include #include #include +#include /*--[ Implement ]------------------------------------------------------------------------------------*/ @@ -78,6 +79,7 @@ static inline void lib3270_autoptr_cleanup_CURL(CURL **ptr) typedef struct _curldata { size_t length; + H3270 * hSession; SSL_ERROR_MESSAGE * message; char errbuf[CURL_ERROR_SIZE]; unsigned char contents[CRL_DATA_LENGTH]; @@ -110,7 +112,9 @@ static size_t internal_curl_write_callback(void *contents, size_t size, size_t n unsigned char *ptr = (unsigned char *) contents; - debug("\n------------------------------------\n%s\n", ptr); +#ifdef DEBUG + lib3270_trace_data(data->hSession,"************* Received block",(const char *) contents, realsize); +#endif for(ix = 0; ix < realsize; ix++) { @@ -128,6 +132,53 @@ static size_t internal_curl_write_callback(void *contents, size_t size, size_t n return realsize; } +static int internal_curl_trace_callback(CURL *handle unused, curl_infotype type, char *data, size_t size, void *userp) +{ + const char * text = NULL; + + switch (type) { + case CURLINFO_TEXT: + lib3270_write_log(((CURLDATA *) userp)->hSession,"curl","%s",data); + return 0; + + case CURLINFO_HEADER_OUT: + text = "=> Send header"; + break; + + case CURLINFO_DATA_OUT: + text = "=> Send data"; + break; + + case CURLINFO_SSL_DATA_OUT: + text = "=> Send SSL data"; + break; + + case CURLINFO_HEADER_IN: + text = "<= Recv header"; + break; + + case CURLINFO_DATA_IN: + text = "<= Recv data"; + break; + + case CURLINFO_SSL_DATA_IN: + text = "<= Recv SSL data"; + break; + + default: + return 0; + + } + + lib3270_trace_data( + ((CURLDATA *) userp)->hSession, + text, + data, + size + ); + + return 0; +} #endif // HAVE_LIBCURL @@ -179,6 +230,7 @@ X509_CRL * lib3270_get_X509_CRL(H3270 *hSession, SSL_ERROR_MESSAGE * message) memset(crl_data,0,sizeof(CURLDATA)); crl_data->message = message; + crl_data->hSession = hSession; debug("datablock is %p",crl_data); @@ -189,13 +241,21 @@ X509_CRL * lib3270_get_X509_CRL(H3270 *hSession, SSL_ERROR_MESSAGE * message) curl_easy_setopt(hCurl, CURLOPT_URL, consturl); curl_easy_setopt(hCurl, CURLOPT_FOLLOWLOCATION, 1L); - curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, crl_data); + curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, crl_data->errbuf); curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, internal_curl_write_callback); curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, (void *) crl_data); curl_easy_setopt(hCurl, CURLOPT_USERNAME, ""); + if(lib3270_get_toggle(hSession,LIB3270_TOGGLE_SSL_TRACE)) + { + curl_easy_setopt(hCurl, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(hCurl, CURLOPT_DEBUGFUNCTION, internal_curl_trace_callback); + curl_easy_setopt(hCurl, CURLOPT_DEBUGDATA, (void *) crl_data); + CURLOPT_DEBUGDATA + } + res = curl_easy_perform(hCurl); if(res != CURLE_OK) diff --git a/src/lib3270/trace_ds.c b/src/lib3270/trace_ds.c index d8b8ca3..f70f949 100644 --- a/src/lib3270/trace_ds.c +++ b/src/lib3270/trace_ds.c @@ -302,5 +302,43 @@ void trace_ansi_disc(H3270 *hSession) hSession->trace_skipping = 1; } +void lib3270_trace_data(H3270 *hSession, const char *msg, const char *data, size_t datalen) +{ + // 00000000001111111111222222222233333333334444444444555555555566666666667777777777 + // 01234567890123456789012345678901234567890123456789012345678901234567890123456789 + // xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx . . . . . . . . . . . . . . . . + + size_t ix; + char buffer[80]; + char hexvalue[3]; + + memset(buffer,0,sizeof(buffer)); + + wtrace(hSession, "%s (%u bytes)\n", msg, (unsigned int) datalen); + + for(ix = 0; ix < datalen; ix++) + { + size_t col = (ix%15); + + if(col == 0) + { + if(ix) + wtrace(hSession," %s\n",buffer); + + memset(buffer,' ',79); + buffer[79] = 0; + } + + snprintf(hexvalue,3,"%02x",data[ix]); + memcpy(buffer+(col*3),hexvalue,2); + + if(data[ix] > ' ') + buffer[48 + (col*2)] = data[ix]; + + } + + wtrace(hSession," %s\n",buffer); + +} #endif -- libgit2 0.21.2