Commit 186a2cfa7f215d0281695dfbda883446324e7206

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

Melhorando processo de conexão ao host

connect.c
... ... @@ -60,6 +60,7 @@
60 60 #include "trace_dsc.h"
61 61 #include "utilc.h"
62 62 #include "telnetc.h"
  63 +#include "screen.h"
63 64 #include <lib3270/internals.h>
64 65  
65 66 /*---[ Implement ]-------------------------------------------------------------------------------*/
... ... @@ -116,7 +117,6 @@ static void net_connected(H3270 *hSession)
116 117 hSession->excepting = 1;
117 118 hSession->reading = 1;
118 119  
119   -/*
120 120 #if defined(HAVE_LIBSSL)
121 121 if(hSession->ssl_con && hSession->secure == LIB3270_SSL_UNDEFINED)
122 122 {
... ... @@ -124,10 +124,12 @@ static void net_connected(H3270 *hSession)
124 124 return;
125 125 }
126 126 #endif
127   -*/
128 127  
129 128 lib3270_setup_session(hSession);
130 129  
  130 +
  131 + lib3270_set_connected(hSession);
  132 +
131 133 }
132 134  
133 135 #if defined(_WIN32) /*[*/
... ... @@ -167,7 +169,7 @@ static void net_connected(H3270 *hSession)
167 169 }
168 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 174 int s;
173 175 struct addrinfo hints;
... ... @@ -219,7 +221,7 @@ static void net_connected(H3270 *hSession)
219 221 _( "Can't determine value for environment variable \"%s\" " ),
220 222 hostname);
221 223 lib3270_set_disconnected(hSession);
222   - return -1;
  224 + return ENOENT;
223 225 }
224 226 hostname = name;
225 227 }
... ... @@ -230,18 +232,39 @@ static void net_connected(H3270 *hSession)
230 232  
231 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 239 lib3270_popup_dialog( hSession,
234 240 LIB3270_NOTIFY_ERROR,
235 241 _( "Connection error" ),
236   - _( "Can't resolve hostname." ),
  242 + buffer,
237 243 "%s",
238 244 gai_strerror(s));
239 245  
240 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 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 288 lib3270_popup_dialog( hSession,
266 289 LIB3270_NOTIFY_CRITICAL,
267 290 N_( "Network startup error" ),
268   - N_( "Cannot create socket handle" ),
  291 + N_( "Cannot create socket event" ),
269 292 "%s", lib3270_win32_strerror(GetLastError()) );
270 293 _exit(1);
271 294 }
... ... @@ -281,8 +304,6 @@ static void net_connected(H3270 *hSession)
281 304 _exit(1);
282 305 }
283 306  
284   -
285   -
286 307 WSASetLastError(0);
287 308 u_long iMode=1;
288 309 trace("sock=%d",hSession->sock);
... ... @@ -301,10 +322,13 @@ static void net_connected(H3270 *hSession)
301 322 int err = WSAGetLastError();
302 323 if(err != WSAEWOULDBLOCK)
303 324 {
  325 + char buffer[4096];
  326 + snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc);
  327 +
304 328 lib3270_popup_dialog( hSession,
305 329 LIB3270_NOTIFY_ERROR,
306 330 _( "Connection error" ),
307   - _( "Can't connect to host." ),
  331 + buffer,
308 332 "%s", lib3270_win32_strerror(err));
309 333 SOCK_CLOSE(hSession);
310 334 }
... ... @@ -318,10 +342,13 @@ static void net_connected(H3270 *hSession)
318 342 {
319 343 if( errno != EINPROGRESS )
320 344 {
  345 + char buffer[4096];
  346 + snprintf(buffer,4095,_( "Can't connect to %s:%s"), hostname, srvc);
  347 +
321 348 lib3270_popup_dialog( hSession,
322 349 LIB3270_NOTIFY_ERROR,
323 350 _( "Connection error" ),
324   - _( "Can't connect to host." ),
  351 + buffer,
325 352 "%s",
326 353 strerror(errno));
327 354 SOCK_CLOSE(hSession);
... ... @@ -359,11 +386,6 @@ static void net_connected(H3270 *hSession)
359 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 389 // Connecting, set callbacks, wait for connection
368 390 lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True);
369 391  
... ...
globals.h
... ... @@ -247,8 +247,6 @@ LIB3270_INTERNAL void lib3270_sock_disconnect(H3270 *hSession);
247 247 LIB3270_INTERNAL void check_session_handle(H3270 **hSession);
248 248 #endif // DEBUG
249 249  
250   -LIB3270_EXPORT int lib3270_connect_host(H3270 *hSession, const char *hostname, const char *srvc);
251   -
252 250 LIB3270_INTERNAL int non_blocking(H3270 *session, Boolean on);
253 251  
254 252 #if defined(HAVE_LIBSSL) /*[*/
... ...
iocalls.c
... ... @@ -174,7 +174,7 @@ static void * internal_add_timeout(unsigned long interval_ms, H3270 *session, vo
174 174 timeout_t *t;
175 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 179 t_new = (timeout_t *) lib3270_malloc(sizeof(timeout_t));
180 180  
... ...
kybd.c
... ... @@ -449,10 +449,13 @@ void kybd_connect(H3270 *session, int connected, void *dunno)
449 449  
450 450 lib3270_kybdlock_clear(session, -1);
451 451  
452   - if (connected) {
  452 + if (connected)
  453 + {
453 454 /* Wait for any output or a WCC(restore) from the host */
454 455 kybdlock_set(session,KL_AWAITING_FIRST);
455   - } else {
  456 + }
  457 + else
  458 + {
456 459 kybdlock_set(session,KL_NOT_CONNECTED);
457 460 (void) flush_ta(session);
458 461 }
... ... @@ -1163,24 +1166,11 @@ void do_reset(H3270 *hSession, Boolean explicit)
1163 1166 * If explicit (from the keyboard) and there is typeahead or
1164 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 1172 if (flush_ta(hSession))
1179   - half_reset = True;
1180   -
1181   - if (half_reset)
1182 1173 return;
1183   -*/
1184 1174 }
1185 1175  
1186 1176 /* Always clear insert mode. */
... ... @@ -1201,15 +1191,12 @@ void do_reset(H3270 *hSession, Boolean explicit)
1201 1191 * If explicit (from the keyboard), unlock the keyboard now.
1202 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 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 1200 lib3270_kybdlock_clear(hSession,~KL_DEFERRED_UNLOCK);
1214 1201 kybdlock_set(hSession,KL_DEFERRED_UNLOCK);
1215 1202 hSession->unlock_id = AddTimeOut(UNLOCK_MS, hSession, defer_unlock);
... ...
lib3270.cbp
... ... @@ -190,6 +190,9 @@
190 190 <Unit filename="sfc.h" />
191 191 <Unit filename="shlobj_missing.h" />
192 192 <Unit filename="sources.mak" />
  193 + <Unit filename="ssl.c">
  194 + <Option compilerVar="CC" />
  195 + </Unit>
193 196 <Unit filename="state.c">
194 197 <Option compilerVar="CC" />
195 198 </Unit>
... ...
session.c
... ... @@ -332,14 +332,6 @@ H3270 * lib3270_session_new(const char *model)
332 332 if(screen_init(hSession))
333 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 335 trace("%s: Initializing KYBD",__FUNCTION__);
344 336 lib3270_register_schange(hSession,LIB3270_STATE_CONNECT,kybd_connect,NULL);
345 337 lib3270_register_schange(hSession,LIB3270_STATE_3270_MODE,kybd_in3270,NULL);
... ...
ssl.c
... ... @@ -325,13 +325,14 @@ int ssl_init(H3270 *hSession)
325 325 /* Callback for tracing protocol negotiation. */
326 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 328 H3270 *hSession = (H3270 *) SSL_get_ex_data(s,ssl_3270_ex_index);
330 329  
331 330 #ifdef DEBUG
332   - trace("%s: hsession=%p, session=%p",__FUNCTION__,hSession,lib3270_get_default_session_handle());
333 331 if(hSession != lib3270_get_default_session_handle())
  332 + {
  333 + trace("%s: hsession=%p, session=%p",__FUNCTION__,hSession,lib3270_get_default_session_handle());
334 334 exit(-1);
  335 + }
335 336 #endif // DEBUG
336 337  
337 338 switch(where)
... ...
telnet.c
... ... @@ -46,6 +46,10 @@
46 46 #endif // !ANDROID
47 47  
48 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 54 #include "globals.h"
51 55 #include <errno.h>
... ...
testprogram.c
... ... @@ -30,14 +30,13 @@ int main(int numpar, char *param[])
30 30 session = h = lib3270_session_new("");
31 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 35 // pthread_create(&thread, NULL, mainloop, NULL);
36 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 41 mainloop(0);
43 42  
... ...