connect.c
7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
* (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
* aplicativos mainframe. Registro no INPI sob o nome G3270.
*
* Copyright (C) <2008> <Banco do Brasil S.A.>
*
* Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
* os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
* Free Software Foundation.
*
* Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
* GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
* A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
* obter mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
* programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Este programa está nomeado como - e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
*/
#include <config.h>
#include <internals.h>
#include <errno.h>
#include <lib3270/trace.h>
#include <lib3270/toggle.h>
#include "kybdc.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#define SOCK_CLOSE(s) close(s->connection.sock); s->connection.sock = -1;
#include <stdlib.h>
#include "hostc.h"
#include "trace_dsc.h"
#include "telnetc.h"
#include "screen.h"
#include <lib3270/internals.h>
#include <lib3270/log.h>
#include <lib3270/trace.h>
/*---[ Implement ]-------------------------------------------------------------------------------*/
static void net_connected(H3270 *hSession, int GNUC_UNUSED(fd), LIB3270_IO_FLAG GNUC_UNUSED(flag), void GNUC_UNUSED(*dunno))
{
int err;
socklen_t len = sizeof(err);
if(hSession->xio.write) {
trace("%s write=%p",__FUNCTION__,hSession->xio.write);
lib3270_remove_poll(hSession, hSession->xio.write);
hSession->xio.write = NULL;
}
if(getsockopt(hSession->connection.sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len) < 0)
{
lib3270_disconnect(hSession);
lib3270_popup_dialog(
hSession,
LIB3270_NOTIFY_ERROR,
_( "Network error" ),
_( "Unable to get connection state." ),
_( "%s" ), strerror(errno)
);
return;
}
else if(err)
{
char buffer[4096];
snprintf(buffer,4095,_( "Can't connect to %s" ), lib3270_get_url(hSession) );
lib3270_disconnect(hSession);
lib3270_popup_dialog(
hSession,
LIB3270_NOTIFY_ERROR,
_( "Connection failed" ),
buffer,
_( "%s" ), strerror(err)
);
return;
}
hSession->xio.except = lib3270_add_poll_fd(hSession,hSession->connection.sock,LIB3270_IO_FLAG_EXCEPTION,net_exception,0);
hSession->xio.read = lib3270_add_poll_fd(hSession,hSession->connection.sock,LIB3270_IO_FLAG_READ,net_input,0);
#if defined(HAVE_LIBSSL)
if(hSession->ssl.con && hSession->ssl.state == LIB3270_SSL_UNDEFINED)
{
if(ssl_negotiate(hSession))
return;
}
#endif
lib3270_setup_session(hSession);
lib3270_set_connected_initial(hSession);
}
struct resolver
{
const char * message;
};
static int background_connect(H3270 *hSession, void *host)
{
struct addrinfo hints;
struct addrinfo * result = NULL;
struct addrinfo * rp = NULL;
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_UNSPEC; // Allow IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // Stream socket
hints.ai_flags = AI_PASSIVE; // For wildcard IP address
hints.ai_protocol = 0; // Any protocol
status_resolving(hSession);
int rc = getaddrinfo(hSession->host.current, hSession->host.srvc, &hints, &result);
if(rc != 0)
{
((struct resolver *) host)->message = gai_strerror(rc);
return -1;
}
status_connecting(hSession);
for(rp = result; hSession->connection.sock < 0 && rp != NULL; rp = rp->ai_next)
{
hSession->connection.sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(hSession->connection.sock < 0)
{
((struct resolver *) host)->message = strerror(errno);
continue;
}
// Connected!
if(connect(hSession->connection.sock, rp->ai_addr, rp->ai_addrlen))
{
SOCK_CLOSE(hSession);
((struct resolver *) host)->message = strerror(errno);
continue;
}
}
freeaddrinfo(result);
return 0;
}
int net_reconnect(H3270 *hSession, int seconds)
{
struct resolver host;
memset(&host,0,sizeof(host));
// Connect to host
if(lib3270_run_task(hSession, background_connect, &host) || hSession->connection.sock < 0)
{
char buffer[4096];
snprintf(buffer,4095,_( "Can't connect to %s:%s"), hSession->host.current, hSession->host.srvc);
lib3270_popup_dialog( hSession,
LIB3270_NOTIFY_ERROR,
_( "Connection error" ),
buffer,
"%s",
host.message);
lib3270_set_disconnected(hSession);
return errno = ENOTCONN;
}
/* don't share the socket with our children */
(void) fcntl(hSession->connection.sock, F_SETFD, 1);
hSession->ever_3270 = False;
#if defined(HAVE_LIBSSL)
debug("%s: TLS/SSL is %s",__FUNCTION__,hSession->ssl.enabled ? "ENABLED" : "DISABLED")
trace_dsn(hSession,"TLS/SSL is %s\n", hSession->ssl.enabled ? "enabled" : "disabled" );
if(hSession->ssl.enabled)
{
hSession->ssl.host = 1;
if(ssl_init(hSession))
return errno = ENOTCONN;
}
#endif // HAVE_LIBSSL
// set options for inline out-of-band data and keepalives
int optval = 1;
if (setsockopt(hSession->connection.sock, SOL_SOCKET, SO_OOBINLINE, (char *)&optval,sizeof(optval)) < 0)
{
int rc = errno;
lib3270_popup_dialog( hSession,
LIB3270_NOTIFY_ERROR,
_( "Connection error" ),
_( "setsockopt(SO_OOBINLINE) has failed" ),
"%s",
strerror(rc));
SOCK_CLOSE(hSession);
return rc;
}
optval = lib3270_get_toggle(hSession,LIB3270_TOGGLE_KEEP_ALIVE) ? 1 : 0;
if (setsockopt(hSession->connection.sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(optval)) < 0)
{
int rc = errno;
char buffer[4096];
snprintf(buffer,4095,_( "Can't %s network keep-alive" ), optval ? _( "enable" ) : _( "disable" ));
lib3270_popup_dialog( hSession,
LIB3270_NOTIFY_ERROR,
_( "Connection error" ),
buffer,
"%s",
strerror(rc));
SOCK_CLOSE(hSession);
return rc;
}
else
{
trace_dsn(hSession,"Network keep-alive is %s\n",optval ? "enabled" : "disabled" );
}
/*
#if defined(OMTU)
else if (setsockopt(hSession->sock, SOL_SOCKET, SO_SNDBUF, (char *)&mtu,sizeof(mtu)) < 0)
{
popup_a_sockerr(hSession, _( "setsockopt(%s)" ), "SO_SNDBUF");
SOCK_CLOSE(hSession);
}
#endif
*/
// Connecting, set callbacks, wait for connection
lib3270_set_cstate(hSession, LIB3270_PENDING);
lib3270_st_changed(hSession, LIB3270_STATE_HALF_CONNECT, True);
hSession->xio.write = lib3270_add_poll_fd(hSession,hSession->connection.sock,LIB3270_IO_FLAG_WRITE,net_connected,0);
// hSession->ns_write_id = AddOutput(hSession->sock, hSession, net_connected);
trace("%s: Connection in progress",__FUNCTION__);
if(seconds)
{
time_t end = time(0)+seconds;
while(time(0) < end)
{
lib3270_main_iterate(hSession,1);
switch(hSession->connection.state)
{
case LIB3270_PENDING:
case LIB3270_CONNECTED_INITIAL:
case LIB3270_CONNECTED_ANSI:
case LIB3270_CONNECTED_3270:
case LIB3270_CONNECTED_INITIAL_E:
case LIB3270_CONNECTED_NVT:
case LIB3270_CONNECTED_SSCP:
case LIB3270_RESOLVING:
break;
case LIB3270_NOT_CONNECTED:
return errno = ENOTCONN;
case LIB3270_CONNECTED_TN3270E:
if(!hSession->starting)
return 0;
break;
default:
lib3270_write_log(hSession,"connect", "%s: State changed to unexpected state %d",__FUNCTION__,hSession->connection.state);
return errno = EINVAL;
}
}
lib3270_disconnect(hSession);
lib3270_write_log(hSession,"connect", "%s: %s",__FUNCTION__,strerror(ETIMEDOUT));
return errno = ETIMEDOUT;
}
return 0;
}