From 186a2cfa7f215d0281695dfbda883446324e7206 Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Thu, 5 Dec 2013 18:36:56 +0000 Subject: [PATCH] Melhorando processo de conexão ao host --- connect.c | 56 +++++++++++++++++++++++++++++++++++++++----------------- globals.h | 2 -- iocalls.c | 2 +- kybd.c | 37 ++++++++++++------------------------- lib3270.cbp | 3 +++ session.c | 8 -------- ssl.c | 5 +++-- telnet.c | 4 ++++ testprogram.c | 7 +++---- 9 files changed, 65 insertions(+), 59 deletions(-) diff --git a/connect.c b/connect.c index a6c8aba..21d5867 100644 --- a/connect.c +++ b/connect.c @@ -60,6 +60,7 @@ #include "trace_dsc.h" #include "utilc.h" #include "telnetc.h" +#include "screen.h" #include /*---[ Implement ]-------------------------------------------------------------------------------*/ @@ -116,7 +117,6 @@ static void net_connected(H3270 *hSession) hSession->excepting = 1; hSession->reading = 1; -/* #if defined(HAVE_LIBSSL) if(hSession->ssl_con && hSession->secure == LIB3270_SSL_UNDEFINED) { @@ -124,10 +124,12 @@ static void net_connected(H3270 *hSession) return; } #endif -*/ lib3270_setup_session(hSession); + + lib3270_set_connected(hSession); + } #if defined(_WIN32) /*[*/ @@ -167,7 +169,7 @@ static void net_connected(H3270 *hSession) } #endif /*]*/ - LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc) + LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc, LIB3270_CONNECT_OPTION opt) { int s; struct addrinfo hints; @@ -219,7 +221,7 @@ static void net_connected(H3270 *hSession) _( "Can't determine value for environment variable \"%s\" " ), hostname); lib3270_set_disconnected(hSession); - return -1; + return ENOENT; } hostname = name; } @@ -230,18 +232,39 @@ static void net_connected(H3270 *hSession) if(s != 0) { + char buffer[4096]; + + snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc); + lib3270_popup_dialog( hSession, LIB3270_NOTIFY_ERROR, _( "Connection error" ), - _( "Can't resolve hostname." ), + buffer, "%s", gai_strerror(s)); lib3270_set_disconnected(hSession); - return -1; + return ENOENT; } - status_changed(hSession,LIB3270_STATUS_CONNECTING); + +#if !defined(_WIN32) + /* don't share the socket with our children */ + (void) fcntl(hSession->sock, F_SETFD, 1); +#endif + + hSession->ssl_host = 0; + +#if defined(HAVE_LIBSSL) + if(opt&LIB3270_CONNECT_OPTION_SSL) + { + hSession->ssl_host = 1; + ssl_init(hSession); + } +#endif + + /* connect */ + status_connecting(hSession,1); for(rp = result; hSession->sock < 0 && rp != NULL; rp = rp->ai_next) { @@ -265,7 +288,7 @@ static void net_connected(H3270 *hSession) lib3270_popup_dialog( hSession, LIB3270_NOTIFY_CRITICAL, N_( "Network startup error" ), - N_( "Cannot create socket handle" ), + N_( "Cannot create socket event" ), "%s", lib3270_win32_strerror(GetLastError()) ); _exit(1); } @@ -281,8 +304,6 @@ static void net_connected(H3270 *hSession) _exit(1); } - - WSASetLastError(0); u_long iMode=1; trace("sock=%d",hSession->sock); @@ -301,10 +322,13 @@ static void net_connected(H3270 *hSession) int err = WSAGetLastError(); if(err != WSAEWOULDBLOCK) { + char buffer[4096]; + snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc); + lib3270_popup_dialog( hSession, LIB3270_NOTIFY_ERROR, _( "Connection error" ), - _( "Can't connect to host." ), + buffer, "%s", lib3270_win32_strerror(err)); SOCK_CLOSE(hSession); } @@ -318,10 +342,13 @@ static void net_connected(H3270 *hSession) { if( errno != EINPROGRESS ) { + char buffer[4096]; + snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc); + lib3270_popup_dialog( hSession, LIB3270_NOTIFY_ERROR, _( "Connection error" ), - _( "Can't connect to host." ), + buffer, "%s", strerror(errno)); SOCK_CLOSE(hSession); @@ -359,11 +386,6 @@ static void net_connected(H3270 *hSession) return -1; } -#if !defined(_WIN32) - /* don't share the socket with our children */ - (void) fcntl(hSession->sock, F_SETFD, 1); -#endif - // Connecting, set callbacks, wait for connection lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True); diff --git a/globals.h b/globals.h index 526f5b7..b35c256 100644 --- a/globals.h +++ b/globals.h @@ -247,8 +247,6 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession); LIB3270_INTERNAL void check_session_handle(H3270 **hSession); #endif // DEBUG -LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc); - LIB3270_INTERNAL int non_blocking(H3270 *session, Boolean on); #if defined(HAVE_LIBSSL) /*[*/ diff --git a/iocalls.c b/iocalls.c index 9b71e82..5dceec6 100644 --- a/iocalls.c +++ b/iocalls.c @@ -174,7 +174,7 @@ static void * internal_add_timeout(unsigned long interval_ms, H3270 *session, vo timeout_t *t; timeout_t *prev = TN; - trace("%s session=%p proc=%p",__FUNCTION__,session,proc); + trace("%s session=%p proc=%p interval=%ld",__FUNCTION__,session,proc,interval_ms); t_new = (timeout_t *) lib3270_malloc(sizeof(timeout_t)); diff --git a/kybd.c b/kybd.c index 5dc967c..3d15120 100644 --- a/kybd.c +++ b/kybd.c @@ -449,10 +449,13 @@ void kybd_connect(H3270 *session, int connected, void *dunno) lib3270_kybdlock_clear(session, -1); - if (connected) { + if (connected) + { /* Wait for any output or a WCC(restore) from the host */ kybdlock_set(session,KL_AWAITING_FIRST); - } else { + } + else + { kybdlock_set(session,KL_NOT_CONNECTED); (void) flush_ta(session); } @@ -1163,24 +1166,11 @@ void do_reset(H3270 *hSession, Boolean explicit) * If explicit (from the keyboard) and there is typeahead or * a half-composed key, simply flush it. */ - if (explicit -#if defined(X3270_FT) /*[*/ - || lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE -#endif /*]*/ - ) { - - if (flush_ta(hSession)) - return; - -/* - Boolean half_reset = False; + if (explicit || lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE) + { if (flush_ta(hSession)) - half_reset = True; - - if (half_reset) return; -*/ } /* Always clear insert mode. */ @@ -1201,15 +1191,12 @@ void do_reset(H3270 *hSession, Boolean explicit) * If explicit (from the keyboard), unlock the keyboard now. * Otherwise (from the host), schedule a deferred keyboard unlock. */ - if (explicit -#if defined(X3270_FT) /*[*/ - || lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE -#endif /*]*/ - || (!hSession->unlock_delay) // && !sms_in_macro()) - || (hSession->unlock_delay_time != 0 && (time(NULL) - hSession->unlock_delay_time) > 1)) { + if (explicit || lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE || (!hSession->unlock_delay) || (hSession->unlock_delay_time != 0 && (time(NULL) - hSession->unlock_delay_time) > 1)) + { lib3270_kybdlock_clear(hSession,-1); - } else if (hSession->kybdlock & - (KL_DEFERRED_UNLOCK | KL_OIA_TWAIT | KL_OIA_LOCKED | KL_AWAITING_FIRST)) { + } + else if (hSession->kybdlock & (KL_DEFERRED_UNLOCK | KL_OIA_TWAIT | KL_OIA_LOCKED | KL_AWAITING_FIRST)) + { lib3270_kybdlock_clear(hSession,~KL_DEFERRED_UNLOCK); kybdlock_set(hSession,KL_DEFERRED_UNLOCK); hSession->unlock_id = AddTimeOut(UNLOCK_MS, hSession, defer_unlock); diff --git a/lib3270.cbp b/lib3270.cbp index f1ebfb2..73ba00d 100644 --- a/lib3270.cbp +++ b/lib3270.cbp @@ -190,6 +190,9 @@ + + diff --git a/session.c b/session.c index 61b32a1..0a2bb08 100644 --- a/session.c +++ b/session.c @@ -332,14 +332,6 @@ H3270 * lib3270_session_new(const char *model) if(screen_init(hSession)) return NULL; -/* - trace("Charset: %s",hSession->charset.host); - if (charset_init(hSession,hSession->charset.host) != CS_OKAY) - { - Warning(hSession, _( "Cannot find charset \"%s\", using defaults" ), hSession->charset.host); - (void) charset_init(hSession,CN); - } -*/ trace("%s: Initializing KYBD",__FUNCTION__); lib3270_register_schange(hSession,LIB3270_STATE_CONNECT,kybd_connect,NULL); lib3270_register_schange(hSession,LIB3270_STATE_3270_MODE,kybd_in3270,NULL); diff --git a/ssl.c b/ssl.c index 7bbe658..bfdd5e1 100644 --- a/ssl.c +++ b/ssl.c @@ -325,13 +325,14 @@ int ssl_init(H3270 *hSession) /* Callback for tracing protocol negotiation. */ void ssl_info_callback(INFO_CONST SSL *s, int where, int ret) { -// H3270 *hSession = lib3270_get_default_session_handle(); // TODO: Find a better way! H3270 *hSession = (H3270 *) SSL_get_ex_data(s,ssl_3270_ex_index); #ifdef DEBUG - trace("%s: hsession=%p, session=%p",__FUNCTION__,hSession,lib3270_get_default_session_handle()); if(hSession != lib3270_get_default_session_handle()) + { + trace("%s: hsession=%p, session=%p",__FUNCTION__,hSession,lib3270_get_default_session_handle()); exit(-1); + } #endif // DEBUG switch(where) diff --git a/telnet.c b/telnet.c index 4a094fd..3d29a86 100644 --- a/telnet.c +++ b/telnet.c @@ -46,6 +46,10 @@ #endif // !ANDROID #include +#if defined(HAVE_LIBSSL) + #include + #include +#endif #include "globals.h" #include diff --git a/testprogram.c b/testprogram.c index a49f9d7..fe7f2ce 100644 --- a/testprogram.c +++ b/testprogram.c @@ -30,14 +30,13 @@ int main(int numpar, char *param[]) session = h = lib3270_session_new(""); printf("3270 session %p created\n]",h); - lib3270_set_toggle(session,LIB3270_TOGGLE_DS_TRACE,1); +// lib3270_set_toggle(session,LIB3270_TOGGLE_DS_TRACE,1); // pthread_create(&thread, NULL, mainloop, NULL); // pthread_detach(thread); - lib3270_connect_host(h, "$HOST3270", "8023"); -// lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet"); -// lib3270_connect_host(h, "127.0.0.1", "9090"); + lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet", LIB3270_CONNECT_OPTION_DEFAULTS); +// lib3270_connect_host(h, "127.0.0.1", "9090", LIB3270_CONNECT_OPTION_DEFAULTS); mainloop(0); -- libgit2 0.21.2