Commit 186a2cfa7f215d0281695dfbda883446324e7206

Authored by perry.werneck@gmail.com
1 parent 89cac419

Melhorando processo de conexão ao host

@@ -60,6 +60,7 @@ @@ -60,6 +60,7 @@
60 #include "trace_dsc.h" 60 #include "trace_dsc.h"
61 #include "utilc.h" 61 #include "utilc.h"
62 #include "telnetc.h" 62 #include "telnetc.h"
  63 +#include "screen.h"
63 #include <lib3270/internals.h> 64 #include <lib3270/internals.h>
64 65
65 /*---[ Implement ]-------------------------------------------------------------------------------*/ 66 /*---[ Implement ]-------------------------------------------------------------------------------*/
@@ -116,7 +117,6 @@ static void net_connected(H3270 *hSession) @@ -116,7 +117,6 @@ static void net_connected(H3270 *hSession)
116 hSession->excepting = 1; 117 hSession->excepting = 1;
117 hSession->reading = 1; 118 hSession->reading = 1;
118 119
119 -/*  
120 #if defined(HAVE_LIBSSL) 120 #if defined(HAVE_LIBSSL)
121 if(hSession->ssl_con && hSession->secure == LIB3270_SSL_UNDEFINED) 121 if(hSession->ssl_con && hSession->secure == LIB3270_SSL_UNDEFINED)
122 { 122 {
@@ -124,10 +124,12 @@ static void net_connected(H3270 *hSession) @@ -124,10 +124,12 @@ static void net_connected(H3270 *hSession)
124 return; 124 return;
125 } 125 }
126 #endif 126 #endif
127 -*/  
128 127
129 lib3270_setup_session(hSession); 128 lib3270_setup_session(hSession);
130 129
  130 +
  131 + lib3270_set_connected(hSession);
  132 +
131 } 133 }
132 134
133 #if defined(_WIN32) /*[*/ 135 #if defined(_WIN32) /*[*/
@@ -167,7 +169,7 @@ static void net_connected(H3270 *hSession) @@ -167,7 +169,7 @@ static void net_connected(H3270 *hSession)
167 } 169 }
168 #endif /*]*/ 170 #endif /*]*/
169 171
170 - LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc) 172 + LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc, LIB3270_CONNECT_OPTION opt)
171 { 173 {
172 int s; 174 int s;
173 struct addrinfo hints; 175 struct addrinfo hints;
@@ -219,7 +221,7 @@ static void net_connected(H3270 *hSession) @@ -219,7 +221,7 @@ static void net_connected(H3270 *hSession)
219 _( "Can't determine value for environment variable \"%s\" " ), 221 _( "Can't determine value for environment variable \"%s\" " ),
220 hostname); 222 hostname);
221 lib3270_set_disconnected(hSession); 223 lib3270_set_disconnected(hSession);
222 - return -1; 224 + return ENOENT;
223 } 225 }
224 hostname = name; 226 hostname = name;
225 } 227 }
@@ -230,18 +232,39 @@ static void net_connected(H3270 *hSession) @@ -230,18 +232,39 @@ static void net_connected(H3270 *hSession)
230 232
231 if(s != 0) 233 if(s != 0)
232 { 234 {
  235 + char buffer[4096];
  236 +
  237 + snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc);
  238 +
233 lib3270_popup_dialog( hSession, 239 lib3270_popup_dialog( hSession,
234 LIB3270_NOTIFY_ERROR, 240 LIB3270_NOTIFY_ERROR,
235 _( "Connection error" ), 241 _( "Connection error" ),
236 - _( "Can't resolve hostname." ), 242 + buffer,
237 "%s", 243 "%s",
238 gai_strerror(s)); 244 gai_strerror(s));
239 245
240 lib3270_set_disconnected(hSession); 246 lib3270_set_disconnected(hSession);
241 - return -1; 247 + return ENOENT;
242 } 248 }
243 249
244 - status_changed(hSession,LIB3270_STATUS_CONNECTING); 250 +
  251 +#if !defined(_WIN32)
  252 + /* don't share the socket with our children */
  253 + (void) fcntl(hSession->sock, F_SETFD, 1);
  254 +#endif
  255 +
  256 + hSession->ssl_host = 0;
  257 +
  258 +#if defined(HAVE_LIBSSL)
  259 + if(opt&LIB3270_CONNECT_OPTION_SSL)
  260 + {
  261 + hSession->ssl_host = 1;
  262 + ssl_init(hSession);
  263 + }
  264 +#endif
  265 +
  266 + /* connect */
  267 + status_connecting(hSession,1);
245 268
246 for(rp = result; hSession->sock < 0 && rp != NULL; rp = rp->ai_next) 269 for(rp = result; hSession->sock < 0 && rp != NULL; rp = rp->ai_next)
247 { 270 {
@@ -265,7 +288,7 @@ static void net_connected(H3270 *hSession) @@ -265,7 +288,7 @@ static void net_connected(H3270 *hSession)
265 lib3270_popup_dialog( hSession, 288 lib3270_popup_dialog( hSession,
266 LIB3270_NOTIFY_CRITICAL, 289 LIB3270_NOTIFY_CRITICAL,
267 N_( "Network startup error" ), 290 N_( "Network startup error" ),
268 - N_( "Cannot create socket handle" ), 291 + N_( "Cannot create socket event" ),
269 "%s", lib3270_win32_strerror(GetLastError()) ); 292 "%s", lib3270_win32_strerror(GetLastError()) );
270 _exit(1); 293 _exit(1);
271 } 294 }
@@ -281,8 +304,6 @@ static void net_connected(H3270 *hSession) @@ -281,8 +304,6 @@ static void net_connected(H3270 *hSession)
281 _exit(1); 304 _exit(1);
282 } 305 }
283 306
284 -  
285 -  
286 WSASetLastError(0); 307 WSASetLastError(0);
287 u_long iMode=1; 308 u_long iMode=1;
288 trace("sock=%d",hSession->sock); 309 trace("sock=%d",hSession->sock);
@@ -301,10 +322,13 @@ static void net_connected(H3270 *hSession) @@ -301,10 +322,13 @@ static void net_connected(H3270 *hSession)
301 int err = WSAGetLastError(); 322 int err = WSAGetLastError();
302 if(err != WSAEWOULDBLOCK) 323 if(err != WSAEWOULDBLOCK)
303 { 324 {
  325 + char buffer[4096];
  326 + snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc);
  327 +
304 lib3270_popup_dialog( hSession, 328 lib3270_popup_dialog( hSession,
305 LIB3270_NOTIFY_ERROR, 329 LIB3270_NOTIFY_ERROR,
306 _( "Connection error" ), 330 _( "Connection error" ),
307 - _( "Can't connect to host." ), 331 + buffer,
308 "%s", lib3270_win32_strerror(err)); 332 "%s", lib3270_win32_strerror(err));
309 SOCK_CLOSE(hSession); 333 SOCK_CLOSE(hSession);
310 } 334 }
@@ -318,10 +342,13 @@ static void net_connected(H3270 *hSession) @@ -318,10 +342,13 @@ static void net_connected(H3270 *hSession)
318 { 342 {
319 if( errno != EINPROGRESS ) 343 if( errno != EINPROGRESS )
320 { 344 {
  345 + char buffer[4096];
  346 + snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc);
  347 +
321 lib3270_popup_dialog( hSession, 348 lib3270_popup_dialog( hSession,
322 LIB3270_NOTIFY_ERROR, 349 LIB3270_NOTIFY_ERROR,
323 _( "Connection error" ), 350 _( "Connection error" ),
324 - _( "Can't connect to host." ), 351 + buffer,
325 "%s", 352 "%s",
326 strerror(errno)); 353 strerror(errno));
327 SOCK_CLOSE(hSession); 354 SOCK_CLOSE(hSession);
@@ -359,11 +386,6 @@ static void net_connected(H3270 *hSession) @@ -359,11 +386,6 @@ static void net_connected(H3270 *hSession)
359 return -1; 386 return -1;
360 } 387 }
361 388
362 -#if !defined(_WIN32)  
363 - /* don't share the socket with our children */  
364 - (void) fcntl(hSession->sock, F_SETFD, 1);  
365 -#endif  
366 -  
367 // Connecting, set callbacks, wait for connection 389 // Connecting, set callbacks, wait for connection
368 lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True); 390 lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True);
369 391
@@ -247,8 +247,6 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession); @@ -247,8 +247,6 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession);
247 LIB3270_INTERNAL void check_session_handle(H3270 **hSession); 247 LIB3270_INTERNAL void check_session_handle(H3270 **hSession);
248 #endif // DEBUG 248 #endif // DEBUG
249 249
250 -LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc);  
251 -  
252 LIB3270_INTERNAL int non_blocking(H3270 *session, Boolean on); 250 LIB3270_INTERNAL int non_blocking(H3270 *session, Boolean on);
253 251
254 #if defined(HAVE_LIBSSL) /*[*/ 252 #if defined(HAVE_LIBSSL) /*[*/
@@ -174,7 +174,7 @@ static void * internal_add_timeout(unsigned long interval_ms, H3270 *session, vo @@ -174,7 +174,7 @@ static void * internal_add_timeout(unsigned long interval_ms, H3270 *session, vo
174 timeout_t *t; 174 timeout_t *t;
175 timeout_t *prev = TN; 175 timeout_t *prev = TN;
176 176
177 - trace("%s session=%p proc=%p",__FUNCTION__,session,proc); 177 + trace("%s session=%p proc=%p interval=%ld",__FUNCTION__,session,proc,interval_ms);
178 178
179 t_new = (timeout_t *) lib3270_malloc(sizeof(timeout_t)); 179 t_new = (timeout_t *) lib3270_malloc(sizeof(timeout_t));
180 180
@@ -449,10 +449,13 @@ void kybd_connect(H3270 *session, int connected, void *dunno) @@ -449,10 +449,13 @@ void kybd_connect(H3270 *session, int connected, void *dunno)
449 449
450 lib3270_kybdlock_clear(session, -1); 450 lib3270_kybdlock_clear(session, -1);
451 451
452 - if (connected) { 452 + if (connected)
  453 + {
453 /* Wait for any output or a WCC(restore) from the host */ 454 /* Wait for any output or a WCC(restore) from the host */
454 kybdlock_set(session,KL_AWAITING_FIRST); 455 kybdlock_set(session,KL_AWAITING_FIRST);
455 - } else { 456 + }
  457 + else
  458 + {
456 kybdlock_set(session,KL_NOT_CONNECTED); 459 kybdlock_set(session,KL_NOT_CONNECTED);
457 (void) flush_ta(session); 460 (void) flush_ta(session);
458 } 461 }
@@ -1163,24 +1166,11 @@ void do_reset(H3270 *hSession, Boolean explicit) @@ -1163,24 +1166,11 @@ void do_reset(H3270 *hSession, Boolean explicit)
1163 * If explicit (from the keyboard) and there is typeahead or 1166 * If explicit (from the keyboard) and there is typeahead or
1164 * a half-composed key, simply flush it. 1167 * a half-composed key, simply flush it.
1165 */ 1168 */
1166 - if (explicit  
1167 -#if defined(X3270_FT) /*[*/  
1168 - || lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE  
1169 -#endif /*]*/  
1170 - ) {  
1171 -  
1172 - if (flush_ta(hSession))  
1173 - return;  
1174 -  
1175 -/*  
1176 - Boolean half_reset = False;  
1177 1169
  1170 + if (explicit || lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE)
  1171 + {
1178 if (flush_ta(hSession)) 1172 if (flush_ta(hSession))
1179 - half_reset = True;  
1180 -  
1181 - if (half_reset)  
1182 return; 1173 return;
1183 -*/  
1184 } 1174 }
1185 1175
1186 /* Always clear insert mode. */ 1176 /* Always clear insert mode. */
@@ -1201,15 +1191,12 @@ void do_reset(H3270 *hSession, Boolean explicit) @@ -1201,15 +1191,12 @@ void do_reset(H3270 *hSession, Boolean explicit)
1201 * If explicit (from the keyboard), unlock the keyboard now. 1191 * If explicit (from the keyboard), unlock the keyboard now.
1202 * Otherwise (from the host), schedule a deferred keyboard unlock. 1192 * Otherwise (from the host), schedule a deferred keyboard unlock.
1203 */ 1193 */
1204 - if (explicit  
1205 -#if defined(X3270_FT) /*[*/  
1206 - || lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE  
1207 -#endif /*]*/  
1208 - || (!hSession->unlock_delay) // && !sms_in_macro())  
1209 - || (hSession->unlock_delay_time != 0 && (time(NULL) - hSession->unlock_delay_time) > 1)) { 1194 + 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))
  1195 + {
1210 lib3270_kybdlock_clear(hSession,-1); 1196 lib3270_kybdlock_clear(hSession,-1);
1211 - } else if (hSession->kybdlock &  
1212 - (KL_DEFERRED_UNLOCK | KL_OIA_TWAIT | KL_OIA_LOCKED | KL_AWAITING_FIRST)) { 1197 + }
  1198 + else if (hSession->kybdlock & (KL_DEFERRED_UNLOCK | KL_OIA_TWAIT | KL_OIA_LOCKED | KL_AWAITING_FIRST))
  1199 + {
1213 lib3270_kybdlock_clear(hSession,~KL_DEFERRED_UNLOCK); 1200 lib3270_kybdlock_clear(hSession,~KL_DEFERRED_UNLOCK);
1214 kybdlock_set(hSession,KL_DEFERRED_UNLOCK); 1201 kybdlock_set(hSession,KL_DEFERRED_UNLOCK);
1215 hSession->unlock_id = AddTimeOut(UNLOCK_MS, hSession, defer_unlock); 1202 hSession->unlock_id = AddTimeOut(UNLOCK_MS, hSession, defer_unlock);
@@ -190,6 +190,9 @@ @@ -190,6 +190,9 @@
190 <Unit filename="sfc.h" /> 190 <Unit filename="sfc.h" />
191 <Unit filename="shlobj_missing.h" /> 191 <Unit filename="shlobj_missing.h" />
192 <Unit filename="sources.mak" /> 192 <Unit filename="sources.mak" />
  193 + <Unit filename="ssl.c">
  194 + <Option compilerVar="CC" />
  195 + </Unit>
193 <Unit filename="state.c"> 196 <Unit filename="state.c">
194 <Option compilerVar="CC" /> 197 <Option compilerVar="CC" />
195 </Unit> 198 </Unit>
@@ -332,14 +332,6 @@ H3270 * lib3270_session_new(const char *model) @@ -332,14 +332,6 @@ H3270 * lib3270_session_new(const char *model)
332 if(screen_init(hSession)) 332 if(screen_init(hSession))
333 return NULL; 333 return NULL;
334 334
335 -/*  
336 - trace("Charset: %s",hSession->charset.host);  
337 - if (charset_init(hSession,hSession->charset.host) != CS_OKAY)  
338 - {  
339 - Warning(hSession, _( "Cannot find charset \"%s\", using defaults" ), hSession->charset.host);  
340 - (void) charset_init(hSession,CN);  
341 - }  
342 -*/  
343 trace("%s: Initializing KYBD",__FUNCTION__); 335 trace("%s: Initializing KYBD",__FUNCTION__);
344 lib3270_register_schange(hSession,LIB3270_STATE_CONNECT,kybd_connect,NULL); 336 lib3270_register_schange(hSession,LIB3270_STATE_CONNECT,kybd_connect,NULL);
345 lib3270_register_schange(hSession,LIB3270_STATE_3270_MODE,kybd_in3270,NULL); 337 lib3270_register_schange(hSession,LIB3270_STATE_3270_MODE,kybd_in3270,NULL);
@@ -325,13 +325,14 @@ int ssl_init(H3270 *hSession) @@ -325,13 +325,14 @@ int ssl_init(H3270 *hSession)
325 /* Callback for tracing protocol negotiation. */ 325 /* Callback for tracing protocol negotiation. */
326 void ssl_info_callback(INFO_CONST SSL *s, int where, int ret) 326 void ssl_info_callback(INFO_CONST SSL *s, int where, int ret)
327 { 327 {
328 -// H3270 *hSession = lib3270_get_default_session_handle(); // TODO: Find a better way!  
329 H3270 *hSession = (H3270 *) SSL_get_ex_data(s,ssl_3270_ex_index); 328 H3270 *hSession = (H3270 *) SSL_get_ex_data(s,ssl_3270_ex_index);
330 329
331 #ifdef DEBUG 330 #ifdef DEBUG
332 - trace("%s: hsession=%p, session=%p",__FUNCTION__,hSession,lib3270_get_default_session_handle());  
333 if(hSession != lib3270_get_default_session_handle()) 331 if(hSession != lib3270_get_default_session_handle())
  332 + {
  333 + trace("%s: hsession=%p, session=%p",__FUNCTION__,hSession,lib3270_get_default_session_handle());
334 exit(-1); 334 exit(-1);
  335 + }
335 #endif // DEBUG 336 #endif // DEBUG
336 337
337 switch(where) 338 switch(where)
@@ -46,6 +46,10 @@ @@ -46,6 +46,10 @@
46 #endif // !ANDROID 46 #endif // !ANDROID
47 47
48 #include <lib3270/config.h> 48 #include <lib3270/config.h>
  49 +#if defined(HAVE_LIBSSL)
  50 + #include <openssl/ssl.h>
  51 + #include <openssl/err.h>
  52 +#endif
49 53
50 #include "globals.h" 54 #include "globals.h"
51 #include <errno.h> 55 #include <errno.h>
@@ -30,14 +30,13 @@ int main(int numpar, char *param[]) @@ -30,14 +30,13 @@ int main(int numpar, char *param[])
30 session = h = lib3270_session_new(""); 30 session = h = lib3270_session_new("");
31 printf("3270 session %p created\n]",h); 31 printf("3270 session %p created\n]",h);
32 32
33 - lib3270_set_toggle(session,LIB3270_TOGGLE_DS_TRACE,1); 33 +// lib3270_set_toggle(session,LIB3270_TOGGLE_DS_TRACE,1);
34 34
35 // pthread_create(&thread, NULL, mainloop, NULL); 35 // pthread_create(&thread, NULL, mainloop, NULL);
36 // pthread_detach(thread); 36 // pthread_detach(thread);
37 37
38 - lib3270_connect_host(h, "$HOST3270", "8023");  
39 -// lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet");  
40 -// lib3270_connect_host(h, "127.0.0.1", "9090"); 38 + lib3270_connect_host(h, "fandezhi.efglobe.com", "telnet", LIB3270_CONNECT_OPTION_DEFAULTS);
  39 +// lib3270_connect_host(h, "127.0.0.1", "9090", LIB3270_CONNECT_OPTION_DEFAULTS);
41 40
42 mainloop(0); 41 mainloop(0);
43 42