pluginmain.c
18.2 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
* "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. 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 pluginmain.c e possui - linhas de código.
*
* Contatos:
*
* perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
* erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
*
* Agradecimento:
*
* Hélio Passos
*
*/
#include "server.h"
#include <windows.h>
#include <pw3270/plugin.h>
#include <pw3270/v3270.h>
#include <pw3270/ipcpackets.h>
#include <lib3270/actions.h>
#include <lib3270/charset.h>
/*--[ Defines ]--------------------------------------------------------------------------------------*/
#pragma pack(1)
enum PIPE_STATE
{
PIPE_STATE_WAITING,
PIPE_STATE_READ,
PIPE_STATE_PENDING_READ,
PIPE_STATE_UNDEFINED
};
typedef struct _pipe_source
{
GSource gsrc;
HANDLE hPipe;
enum PIPE_STATE state;
OVERLAPPED overlap;
unsigned char buffer[PIPE_BUFFER_LENGTH+1];
} pipe_source;
#pragma pack()
/*--[ Globals ]--------------------------------------------------------------------------------------*/
static const gchar control_char = '@';
/*--[ Implement ]------------------------------------------------------------------------------------*/
static void IO_accept(pipe_source *source)
{
set_active(FALSE);
if(ConnectNamedPipe(source->hPipe,&source->overlap))
{
popup_lasterror("%s",_( "Error in ConnectNamedPipe" ));
return;
}
switch(GetLastError())
{
// The overlapped connection in progress.
case ERROR_IO_PENDING:
// trace("%s: ERROR_IO_PENDING",__FUNCTION__);
source->state = PIPE_STATE_WAITING;
break;
// Client is already connected, so signal an event.
case ERROR_PIPE_CONNECTED:
trace("%s: ERROR_PIPE_CONNECTED",__FUNCTION__);
set_active(TRUE);
if(SetEvent(source->overlap.hEvent))
break;
// If an error occurs during the connect operation...
default:
popup_lasterror("%s", _( "ConnectNamedPipe failed" ));
}
}
static gboolean IO_prepare(GSource *source, gint *timeout)
{
/*
* Called before all the file descriptors are polled.
* If the source can determine that it is ready here
* (without waiting for the results of the poll() call)
* it should return TRUE.
*
* It can also return a timeout_ value which should be the maximum
* timeout (in milliseconds) which should be passed to the poll() call.
* The actual timeout used will be -1 if all sources returned -1,
* or it will be the minimum of all the timeout_ values
* returned which were >= 0.
*
*/
if(WaitForSingleObject(((pipe_source *) source)->overlap.hEvent,0) == WAIT_OBJECT_0)
return TRUE;
*timeout = 10;
return FALSE;
}
static gboolean IO_check(GSource *source)
{
/*
* Called after all the file descriptors are polled.
* The source should return TRUE if it is ready to be dispatched.
* Note that some time may have passed since the previous prepare
* function was called, so the source should be checked again here.
*
*/
if(WaitForSingleObject(((pipe_source *) source)->overlap.hEvent,0) == WAIT_OBJECT_0)
return TRUE;
return FALSE;
}
static void send_text(pipe_source *source, char *text)
{
struct hllapi_packet_text *pkt;
DWORD szBlock;
int f;
if(text)
{
szBlock = sizeof(struct hllapi_packet_text)+strlen(text);
pkt = (struct hllapi_packet_text *) g_malloc0(szBlock);
pkt->packet_id = 0;
strcpy(pkt->text,text);
lib3270_free(text);
}
else
{
szBlock = sizeof(struct hllapi_packet_text);
pkt = (struct hllapi_packet_text *) g_malloc0(szBlock);
pkt->packet_id = errno ? errno : -1;
}
trace("szBlock=%d text=\"%s\"",szBlock, ( (struct hllapi_packet_text *) pkt)->text);
for(f=0;f< (int) szBlock;f++)
{
trace("rsp(%d)= %d \"%s\"",f,* (((char *) pkt)+f),((char *) pkt)+f);
}
WriteFile(source->hPipe,pkt,szBlock,&szBlock,NULL);
g_free(pkt);
}
static void send_result(pipe_source *source, int rc)
{
struct hllapi_packet_result pkt = { rc };
DWORD wrote = sizeof(pkt);
WriteFile(source->hPipe,&pkt,wrote,&wrote,NULL);
}
static int do_file_transfer(struct hllapi_packet_file_transfer * source)
{
const gchar * local = (const char *) source->text;
const gchar * remote = (const char *) (local+strlen(local)+1);
return v3270_transfer_file( v3270_get_default_widget(),
(LIB3270_FT_OPTION) source->options,
local,
remote,
source->lrecl,
source->blksize,
source->primspace,
source->secspace,
source->dft );
}
static void process_input(pipe_source *source, DWORD cbRead)
{
trace("%s id=%d",__FUNCTION__,((struct hllapi_packet_query *) source->buffer)->packet_id);
switch(((struct hllapi_packet_query *) source->buffer)->packet_id)
{
case HLLAPI_PACKET_CONNECT:
send_result(source,lib3270_connect( lib3270_get_default_session_handle(),
((struct hllapi_packet_connect *) source->buffer)->wait));
break;
case HLLAPI_PACKET_SET_HOST:
send_result(source,lib3270_set_url( lib3270_get_default_session_handle(),
((struct hllapi_packet_text *) source->buffer)->text) != NULL);
break;
case HLLAPI_PACKET_DISCONNECT:
send_result(source,lib3270_disconnect(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_IS_CONNECTED:
send_result(source,lib3270_in_tn3270e(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_IS_READY:
send_result(source,lib3270_is_ready(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_ENTER:
send_result(source,lib3270_enter(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_PRINT:
send_result(source,lib3270_print(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_ERASE:
send_result(source,lib3270_erase(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_ERASE_EOF:
send_result(source,lib3270_eraseeof(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_ERASE_EOL:
send_result(source,lib3270_eraseeol(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_ERASE_INPUT:
send_result(source,lib3270_eraseinput(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_PFKEY:
send_result(source,lib3270_pfkey( lib3270_get_default_session_handle(),
((struct hllapi_packet_keycode *) source->buffer)->keycode));
break;
case HLLAPI_PACKET_PAKEY:
send_result(source,lib3270_pakey( lib3270_get_default_session_handle(),
((struct hllapi_packet_keycode *) source->buffer)->keycode));
break;
case HLLAPI_PACKET_SET_CURSOR_POSITION:
send_result(source,lib3270_set_cursor_position( lib3270_get_default_session_handle(),
((struct hllapi_packet_cursor *) source->buffer)->row,
((struct hllapi_packet_cursor *) source->buffer)->col));
break;
case HLLAPI_PACKET_SET_TEXT_AT:
send_result(source,lib3270_set_text_at( lib3270_get_default_session_handle(),
((struct hllapi_packet_text_at *) source->buffer)->row,
((struct hllapi_packet_text_at *) source->buffer)->col,
(unsigned char *) ((struct hllapi_packet_text_at *) source->buffer)->text));
break;
case HLLAPI_PACKET_GET_TEXT_AT:
send_text(source,lib3270_get_text_at( lib3270_get_default_session_handle(),
((struct hllapi_packet_at *) source->buffer)->row,
((struct hllapi_packet_at *) source->buffer)->col,
((struct hllapi_packet_at *) source->buffer)->len));
break;
case HLLAPI_PACKET_GET_TEXT_AT_OFFSET:
send_text(source,lib3270_get_text( lib3270_get_default_session_handle(),
((struct hllapi_packet_query_offset *) source->buffer)->addr,
((struct hllapi_packet_query_offset *) source->buffer)->len));
break;
case HLLAPI_PACKET_CMP_TEXT_AT:
send_result(source,lib3270_cmp_text_at( lib3270_get_default_session_handle(),
((struct hllapi_packet_text_at *) source->buffer)->row,
((struct hllapi_packet_text_at *) source->buffer)->col,
((struct hllapi_packet_text_at *) source->buffer)->text));
break;
case HLLAPI_PACKET_INPUT_STRING:
send_result(source,lib3270_input_string(lib3270_get_default_session_handle(),
(unsigned char *) ((struct hllapi_packet_text *) source->buffer)->text));
break;
case HLLAPI_PACKET_EMULATE_INPUT:
send_result(source,lib3270_emulate_input(lib3270_get_default_session_handle(),
(const char *) ((struct hllapi_packet_emulate_input *) source->buffer)->text,
(int) ((struct hllapi_packet_emulate_input *) source->buffer)->len,
(int) ((struct hllapi_packet_emulate_input *) source->buffer)->pasting));
break;
case HLLAPI_PACKET_SET_CURSOR:
send_result(source,lib3270_set_cursor_address(lib3270_get_default_session_handle(),
((struct hllapi_packet_addr *) source->buffer)->addr));
break;
case HLLAPI_PACKET_GET_CURSOR:
send_result(source,lib3270_get_cursor_address(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_GET_CSTATE:
send_result(source,lib3270_get_connection_state(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_GET_PROGRAM_MESSAGE:
send_result(source,lib3270_get_program_message(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_SET_TOGGLE:
send_result(source,lib3270_set_toggle(lib3270_get_default_session_handle(),
(LIB3270_TOGGLE) ((struct hllapi_packet_set *) source->buffer)->id,
((struct hllapi_packet_set *) source->buffer)->value));
break;
case HLLAPI_PACKET_FIELD_START:
send_result(source,lib3270_get_field_start(lib3270_get_default_session_handle(),
((struct hllapi_packet_addr *) source->buffer)->addr));
break;
case HLLAPI_PACKET_FIELD_LEN:
send_result(source,lib3270_get_field_len(lib3270_get_default_session_handle(),
((struct hllapi_packet_addr *) source->buffer)->addr));
break;
case HLLAPI_PACKET_NEXT_UNPROTECTED:
send_result(source,lib3270_get_next_unprotected(lib3270_get_default_session_handle(),
((struct hllapi_packet_addr *) source->buffer)->addr));
break;
case HLLAPI_PACKET_IS_PROTECTED:
send_result(source,lib3270_get_is_protected(lib3270_get_default_session_handle(),
((struct hllapi_packet_addr *) source->buffer)->addr));
break;
case HLLAPI_PACKET_IS_PROTECTED_AT:
send_result(source,lib3270_get_is_protected_at( lib3270_get_default_session_handle(),
((struct hllapi_packet_query_at *) source->buffer)->row,
((struct hllapi_packet_query_at *) source->buffer)->col));
break;
case HLLAPI_PACKET_QUIT:
gtk_main_quit();
send_result(source,0);
break;
case HLLAPI_PACKET_SET_HOST_CHARSET:
send_result(source,lib3270_set_host_charset( lib3270_get_default_session_handle(),
(const char *) ((struct hllapi_packet_set_text *) source->buffer)->text));
break;
case HLLAPI_PACKET_ASC2EBC:
send_text(source,(char *) lib3270_asc2ebc(
lib3270_get_default_session_handle(),
(unsigned char *) ((struct hllapi_packet_set_text *) source->buffer)->text,-1
));
break;
case HLLAPI_PACKET_EBC2ASC:
send_text(source,(char *) lib3270_ebc2asc(
lib3270_get_default_session_handle(),
(unsigned char *) ((struct hllapi_packet_set_text *) source->buffer)->text,-1
));
break;
case HLLAPI_PACKET_FILE_TRANSFER:
send_result(source,do_file_transfer((struct hllapi_packet_file_transfer *) source));
break;
case HLLAPI_PACKET_GET_HOST_CHARSET:
trace("%s","HLLAPI_PACKET_GET_HOST_CHARSET");
send_text(source,(char *) lib3270_get_host_charset(lib3270_get_default_session_handle()));
break;
case HLLAPI_PACKET_ACTION:
send_result(source,lib3270_action(lib3270_get_default_session_handle(),
(const char *) ((struct hllapi_packet_text *) source->buffer)->text));
break;
default:
send_result(source, EINVAL);
g_message("Invalid remote request (id=%d)",source->buffer[0]);
}
}
static void read_input_pipe(pipe_source *source)
{
DWORD cbRead = 0;
if(ReadFile(source->hPipe,source->buffer,PIPE_BUFFER_LENGTH,&cbRead,&source->overlap) && cbRead > 0)
process_input(source,cbRead);
// The read operation is still pending.
switch(GetLastError())
{
case 0:
break;
case ERROR_IO_PENDING:
// trace("%s: PIPE_STATE_PENDING_READ",__FUNCTION__);
source->state = PIPE_STATE_PENDING_READ;
break;
case ERROR_PIPE_LISTENING:
// trace("%s: ERROR_PIPE_LISTENING",__FUNCTION__);
source->state = PIPE_STATE_READ;
break;
case ERROR_BROKEN_PIPE:
trace("%s: ERROR_BROKEN_PIPE",__FUNCTION__);
if(!DisconnectNamedPipe(source->hPipe))
{
set_active(FALSE);
popup_lasterror("%s",_( "Error in DisconnectNamedPipe" ));
}
else
{
IO_accept(source);
}
break;
case ERROR_PIPE_NOT_CONNECTED:
trace("%s: ERROR_PIPE_NOT_CONNECTED",__FUNCTION__);
set_active(FALSE);
break;
default:
if(source->hPipe != INVALID_HANDLE_VALUE)
popup_lasterror("%s",_( "Error receiving message from pipe" ) );
}
}
static gboolean IO_dispatch(GSource *source, GSourceFunc callback, gpointer data)
{
/*
* Called to dispatch the event source,
* after it has returned TRUE in either its prepare or its check function.
* The dispatch function is passed in a callback function and data.
* The callback function may be NULL if the source was never connected
* to a callback using g_source_set_callback(). The dispatch function
* should call the callback function with user_data and whatever additional
* parameters are needed for this type of event source.
*/
BOOL fSuccess;
DWORD cbRead = 0;
// DWORD dwErr = 0;
fSuccess = GetOverlappedResult(((pipe_source *) source)->hPipe,&((pipe_source *) source)->overlap,&cbRead,FALSE );
// trace("%s: source=%p data=%p Result=%s cbRead=%d",__FUNCTION__,source,data,fSuccess ? "Success" : "Unsuccess",(int) cbRead);
switch(((pipe_source *) source)->state)
{
case PIPE_STATE_WAITING:
if(fSuccess)
{
trace("Pipe connected (cbRet=%d)",(int) cbRead);
set_active(TRUE);
((pipe_source *) source)->state = PIPE_STATE_READ;
}
else
{
popup_lasterror("%s", _( "Pipe connection failed" ));
}
break;
case PIPE_STATE_READ:
// trace("Reading pipe (cbRead=%d)",(int) cbRead);
read_input_pipe( (pipe_source *) source);
break;
case PIPE_STATE_PENDING_READ:
if(fSuccess && cbRead > 0)
process_input((pipe_source *) source,cbRead);
((pipe_source *) source)->state = PIPE_STATE_READ;
break;
case PIPE_STATE_UNDEFINED:
break;
//#ifdef DEBUG
// default:
// trace("%s: source=%p data=%p Unexpected mode %d",__FUNCTION__,source,data,((pipe_source *) source)->state);
//#endif
}
return TRUE;
}
static void IO_finalize(GSource *source)
{
// trace("%s: source=%p",__FUNCTION__,source);
if( ((pipe_source *) source)->hPipe != INVALID_HANDLE_VALUE)
{
CloseHandle(((pipe_source *) source)->hPipe);
((pipe_source *) source)->hPipe = INVALID_HANDLE_VALUE;
}
}
static gboolean IO_closure(gpointer data)
{
// trace("%s: data=%p",__FUNCTION__,data);
return 0;
}
void popup_lasterror(const gchar *fmt, ...)
{
char buffer[4096];
va_list arg_ptr;
int sz;
DWORD errcode = GetLastError();
char *ptr;
LPVOID lpMsgBuf = 0;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
for(ptr= (char *) lpMsgBuf;*ptr && *ptr != '\n';ptr++);
*ptr = 0;
va_start(arg_ptr, fmt);
vsnprintf(buffer,4095,fmt,arg_ptr);
va_end(arg_ptr);
sz = strlen(buffer);
snprintf(buffer+sz,4096-sz,": %s\n(rc=%d)",lpMsgBuf,(int) errcode);
printf("%s\n",buffer);
#ifdef DEBUG
fprintf(stderr,"%s\n",buffer);
fflush(stderr);
#endif
LocalFree(lpMsgBuf);
}
LIB3270_EXPORT int pw3270_plugin_start(GtkWidget *window)
{
char id;
for(id='A';id < 'Z';id++)
{
gchar * pipename = g_strdup_printf("\\\\.\\pipe\\%s_%c",pw3270_get_session_name(window),id);
gchar * ptr;
HANDLE hPipe;
for(ptr=pipename;*ptr;ptr++)
*ptr = g_ascii_tolower(*ptr);
hPipe = CreateNamedPipe( TEXT(pipename), // pipe name
PIPE_ACCESS_DUPLEX | // read/write access
FILE_FLAG_OVERLAPPED, // overlapped mode
PIPE_TYPE_MESSAGE | // pipe type
PIPE_READMODE_MESSAGE | // pipe mode
PIPE_WAIT, // blocking mode
1, // number of instances
PIPE_BUFFER_LENGTH, // output buffer size
PIPE_BUFFER_LENGTH, // input buffer size
NMPWAIT_USE_DEFAULT_WAIT, // client time-out
NULL); // default security attributes
trace("%s = %p",pipename,hPipe);
g_free(pipename);
if(hPipe != INVALID_HANDLE_VALUE)
{
static GSourceFuncs pipe_source_funcs =
{
IO_prepare,
IO_check,
IO_dispatch,
IO_finalize,
IO_closure,
NULL
};
pipe_source * source;
gchar * session = g_strdup_printf("%s:%c",pw3270_get_session_name(window),id);
pw3270_set_session_name(window,session);
g_free(session);
source = (pipe_source *) g_source_new(&pipe_source_funcs,sizeof(pipe_source));
source->hPipe = hPipe;
source->state = PIPE_STATE_WAITING;
source->overlap.hEvent = CreateEvent( NULL,TRUE,TRUE,NULL);
g_source_attach((GSource *) source,NULL);
IO_accept(source);
return 0;
}
}
popup_lasterror( "%s", _( "Can´t create remote control pipe" ));
return -1;
}
LIB3270_EXPORT int pw3270_plugin_stop(GtkWidget *window)
{
return 0;
}
G_GNUC_INTERNAL void set_active(gboolean on)
{
trace("%s(%s)",__FUNCTION__,on ? "Active" : "Inactive");
v3270_set_script(v3270_get_default_widget(),'H',on);
}