From 48efc5cc3de315556266cd12925a508ce65083f2 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Tue, 13 Aug 2019 11:23:35 -0300 Subject: [PATCH] Debugging windows code. --- sdk/lib3270-delayed.pc.in | 2 +- src/core/init.c | 4 +++- src/core/windows/connect.c | 47 +++++++++++++++++++++++++++++++++++++++++++---- src/core/windows/log.c | 10 +--------- src/core/windows/util.c | 29 +++++++++++++++++++++++++++++ src/include/lib3270-internals.h | 3 +++ src/include/lib3270.h | 8 ++++++++ src/ssl/ctx_init.c | 5 ++++- src/ssl/windows/ldap.c | 4 ---- src/testprogram/testprogram.c | 7 +++++++ 10 files changed, 99 insertions(+), 20 deletions(-) diff --git a/sdk/lib3270-delayed.pc.in b/sdk/lib3270-delayed.pc.in index a89648e..1a25675 100644 --- a/sdk/lib3270-delayed.pc.in +++ b/sdk/lib3270-delayed.pc.in @@ -13,5 +13,5 @@ Description: @PACKAGE_DESCRIPTION@ Version: @PACKAGE_VERSION@ Libs: -L\@libdir@ -l@LIB3270_NAME@.delayed Libs.private: @LIBS@ @LIBSSL_LIBS@ @LIBICONV@ @INTL_LIBS@ @LDAP_LIBS@ @LIBCURL_LIBS@ -Cflags: -I@includedir@ +Cflags: -I@includedir@ -DLIB3270_NAME=@LIB3270_NAME@ diff --git a/src/core/init.c b/src/core/init.c index 31a31c0..df8a46b 100644 --- a/src/core/init.c +++ b/src/core/init.c @@ -70,6 +70,7 @@ #ifdef _WIN32 /// @brief Windows Event Log Handler. HANDLE hEventLog = 0; +HANDLE hModule = 0; #endif // _WIN32 /** @@ -135,11 +136,12 @@ int lib3270_unloaded(void) #if defined WIN32 -BOOL WINAPI DllMain(HANDLE GNUC_UNUSED(hinst), DWORD dwcallpurpose, LPVOID GNUC_UNUSED(lpvResvd)) +BOOL WINAPI DllMain(HANDLE hInstance, DWORD dwcallpurpose, LPVOID GNUC_UNUSED(lpvResvd)) { switch(dwcallpurpose) { case DLL_PROCESS_ATTACH: + hModule = hInstance; hEventLog = RegisterEventSource(NULL, LIB3270_STRINGIZE_VALUE_OF(LIB3270_NAME)); get_version_info(); lib3270_loaded(); diff --git a/src/core/windows/connect.c b/src/core/windows/connect.c index ddb98d8..1fd2cd5 100644 --- a/src/core/windows/connect.c +++ b/src/core/windows/connect.c @@ -91,7 +91,7 @@ static void net_connected(H3270 *hSession, int GNUC_UNUSED(fd), LIB3270_IO_FLAG LIB3270_NOTIFY_ERROR, _( "Connection failed" ), buffer, - _( "%s"), lib3270_win32_strerror(err) + _( "%s (rc=%d)"), strerror(err), err ); trace("%s",__FUNCTION__); return; @@ -150,6 +150,7 @@ static void sockstart(H3270 *session) struct resolver { + int rc; int convert; const char * message; }; @@ -174,8 +175,11 @@ static void sockstart(H3270 *session) status_resolving(hSession); int rc = getaddrinfo(hSession->host.current, hSession->host.srvc, &hints, &result); + debug("getaddrinfo(%s,%s) returns %d",hSession->host.current,hSession->host.srvc,rc); + if(rc != 0) { + ((struct resolver *) host)->rc = rc; ((struct resolver *) host)->message = gai_strerror(rc); ((struct resolver *) host)->convert = 1; return -1; @@ -188,6 +192,7 @@ static void sockstart(H3270 *session) hSession->sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if(hSession->sock < 0) { + ((struct resolver *) host)->rc = errno; ((struct resolver *) host)->message = strerror(errno); continue; } @@ -196,6 +201,7 @@ static void sockstart(H3270 *session) if(connect(hSession->sock, rp->ai_addr, rp->ai_addrlen)) { SOCK_CLOSE(hSession); + ((struct resolver *) host)->rc = errno; ((struct resolver *) host)->message = strerror(errno); continue; } @@ -225,6 +231,37 @@ int net_reconnect(H3270 *hSession, int seconds) char msg[4096]; strncpy(msg,host.message,4095); + debug("host.message=\"%s\"",host.message); + + { + // Register on event log + lib3270_autoptr(char) username = lib3270_get_user_name(); + + snprintf(msg,sizeof(msg),"rc=%d",host.rc); + + const char *outMsg[] = { + username, + "networking", + message, + host.message, + msg + }; + + ReportEvent( + hEventLog, + EVENTLOG_ERROR_TYPE, + 1, + 0, + NULL, + (sizeof(outMsg)/sizeof(outMsg[0])), + 0, + outMsg, + NULL + ); + + } + + #ifdef HAVE_ICONV if(host.convert) { @@ -232,7 +269,7 @@ int net_reconnect(H3270 *hSession, int seconds) size_t out = 4096; size_t in = strlen(host.message); - iconv_t hConv = iconv_open(lib3270_win32_local_charset(),"UTF-8"); + iconv_t hConv = iconv_open("UTF-8",lib3270_win32_local_charset()); if(iconv( hConv, &host.message,&in, @@ -250,8 +287,10 @@ int net_reconnect(H3270 *hSession, int seconds) LIB3270_NOTIFY_ERROR, _( "Connection error" ), message, - "%s", - NULL); + "%s (rc=%d)", + msg, + host.rc + ); } else diff --git a/src/core/windows/log.c b/src/core/windows/log.c index 6681974..a01c9e4 100644 --- a/src/core/windows/log.c +++ b/src/core/windows/log.c @@ -43,15 +43,7 @@ void default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int rc, const char *fmt, va_list arg_ptr) { - char username[UNLEN + 1]; - DWORD szName = sizeof(username); - - memset(username,0,szName); - - if(!GetUserName(username, &szName)) { - strncpy(username,"?",UNLEN); - } - + lib3270_autoptr(char) username = lib3270_get_user_name(); lib3270_autoptr(char) msg = lib3270_vsprintf(fmt,arg_ptr); const char *outMsg[] = { diff --git a/src/core/windows/util.c b/src/core/windows/util.c index 804f198..8151877 100644 --- a/src/core/windows/util.c +++ b/src/core/windows/util.c @@ -34,6 +34,7 @@ #include #include +#include #include #include "winversc.h" @@ -267,3 +268,31 @@ LIB3270_EXPORT char * lib3270_build_data_filename(const char *name) return lib3270_strdup_printf("%s\\%s",wc_fn,name); } + +LIB3270_EXPORT char * lib3270_get_installation_path() +{ + char lpFilename[4096]; + + memset(lpFilename,0,sizeof(lpFilename)); + DWORD szPath = GetModuleFileName(hModule,lpFilename,sizeof(lpFilename)); + lpFilename[szPath] = 0; + + char * ptr = strrchr(lpFilename,'\\'); + if(ptr) + ptr[1] = 0; + + return strdup(lpFilename); +} + +char * lib3270_get_user_name() +{ + char username[UNLEN + 1]; + DWORD szName = UNLEN; + + memset(username,0,UNLEN + 1); + GetUserName(username, &szName); + + return strdup(username); + +} + diff --git a/src/include/lib3270-internals.h b/src/include/lib3270-internals.h index 7daf1ca..30fdedf 100644 --- a/src/include/lib3270-internals.h +++ b/src/include/lib3270-internals.h @@ -713,6 +713,7 @@ struct _h3270 #ifdef _WIN32 /// @brief Windows Event Log Handler. LIB3270_INTERNAL HANDLE hEventLog; +LIB3270_INTERNAL HANDLE hModule; #endif // _WIN32 #ifdef HAVE_SYSLOG @@ -827,5 +828,7 @@ LIB3270_INTERNAL int non_blocking(H3270 *session, Boolean on); /// @brief Default log writer. LIB3270_INTERNAL void default_log_writer(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr); + LIB3270_INTERNAL char * lib3270_get_user_name(); + #endif diff --git a/src/include/lib3270.h b/src/include/lib3270.h index 7a7c7f7..e01a056 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -1398,6 +1398,14 @@ */ LIB3270_EXPORT char * lib3270_win32_translate_error_code(int lasterror); + /** + * @brief Get lib3270's installation path. + * + * @return Full path for the lib3270 installation path (release it with lib3270_free) + * + */ + LIB3270_EXPORT char * lib3270_get_installation_path(); + #endif // WIn32 /** diff --git a/src/ssl/ctx_init.c b/src/ssl/ctx_init.c index e5601c4..d142d5b 100644 --- a/src/ssl/ctx_init.c +++ b/src/ssl/ctx_init.c @@ -133,7 +133,10 @@ int ssl_ctx_init(H3270 *hSession, SSL_ERROR_MESSAGE * message) #ifdef _WIN32 { - lib3270_autoptr(char) certpath = lib3270_build_data_filename("certs"); + lib3270_autoptr(char) appdir = lib3270_get_installation_path(); + lib3270_autoptr(char) certpath = lib3270_strdup_printf("%s\\certs",appdir); + + debug("Searching certs from \"%s\".", certpath); if(SSL_CTX_load_verify_locations(ssl_ctx,NULL,certpath)) { diff --git a/src/ssl/windows/ldap.c b/src/ssl/windows/ldap.c index bc76c3f..2eeefeb 100644 --- a/src/ssl/windows/ldap.c +++ b/src/ssl/windows/ldap.c @@ -90,8 +90,6 @@ static inline void lib3270_autoptr_cleanup_BerElement(BerElement **ber) X509_CRL * get_crl_using_ldap(H3270 *hSession, SSL_ERROR_MESSAGE * message, const char *consturl) { - debug("********************************************************* %s",__FUNCTION__); - X509_CRL * x509_crl = NULL; int rc = 0; @@ -266,8 +264,6 @@ X509_CRL * get_crl_using_ldap(H3270 *hSession, SSL_ERROR_MESSAGE * message, cons ldap_value_free_len(value); - debug("********************************************************* %s",__FUNCTION__); - return x509_crl; } diff --git a/src/testprogram/testprogram.c b/src/testprogram/testprogram.c index 3970e2a..1fd0156 100644 --- a/src/testprogram/testprogram.c +++ b/src/testprogram/testprogram.c @@ -68,6 +68,13 @@ int main(int argc, char *argv[]) printf("HOST URL: %s\tHOST CRL: %s\n",lib3270_get_url(h),lib3270_get_crl_url(h)); +#ifdef _WIN32 + { + lib3270_autoptr(char) apppath = lib3270_get_installation_path(); + printf("Application path: \"%s\"\n",apppath); + } +#endif // _WIN32 + if(lib3270_set_url(h,NULL)) lib3270_set_url(h,"tn3270://fandezhi.efglobe.com"); -- libgit2 0.21.2