forked from SDL-Hercules-390/hyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.c
4770 lines (4002 loc) · 165 KB
/
console.c
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CONSOLE.C (C) Copyright Roger Bowler and others, 1999-2016 */
/* Hercules Console Device Handler */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
/*-------------------------------------------------------------------*/
/* This module contains device handling functions for console */
/* devices for the Hercules ESA/390 emulator. */
/* */
/* Telnet support is provided for three console device classes: */
/* */
/* local non-SNA 3270 display consoles via tn3270 */
/* local non-SNA 3270 printers via tn3270 */
/* 1052 and 3215 console printer keyboards via regular telnet */
/* */
/* This module also takes care of the differences between the */
/* remote 3270 and local non-SNA 3270 devices. In particular */
/* the support of command chaining, which is not supported on */
/* the remote 3270 implementation on which telnet 3270 is based. */
/* */
/* In the local non-SNA environment a chained read or write will */
/* continue at the buffer address where the previous command ended. */
/* In order to achieve this, this module will keep track of the */
/* buffer location, and adjust the buffer address on chained read */
/* and write operations. */
/* */
/* When using VTAM with local non-SNA 3270 devices, ensure that */
/* enough bufferspace is available when doing IND$FILE type */
/* filetransfers. Code IOBUF=(,3992) in ATCSTRxx, and/or BUFNUM=xxx */
/* on the LBUILD LOCAL statement defining the 3270 device. */
/* */
/*-------------------------------------------------------------------*/
#include "hstdinc.h"
#include "hercules.h"
#include "hexterns.h"
#include "devtype.h"
#include "opcode.h"
#include "sr.h"
#include "cnsllogo.h"
#include "hexdumpe.h"
/*-------------------------------------------------------------------*/
/* Tweakable build constants */
/*-------------------------------------------------------------------*/
#define BUFLEN_3270 65536 /* 3270 Send/Receive buffer */
#define BUFLEN_1052 150 /* 1052 Send/Receive buffer */
//#define FIX_QWS_BUG_FOR_MCS_CONSOLES /* (see pos_to_buff_offset) */
/*-------------------------------------------------------------------*/
/* TELNET DEBUGGING */
/*-------------------------------------------------------------------*/
//#define TN_DEBUG_NEGOTIATIONS /* #define to log WILL/WONT/etc */
//#define TN_DEBUG_SIMPLE_IAC /* #define to log simple IACs */
//#define TN_DEBUG_NOP_IAC /* #define to log NOP IACs */
/*-------------------------------------------------------------------*/
/* CONSOLE.C DEBUGGING */
/*-------------------------------------------------------------------*/
#define DEBUG_NOTHING 0 /* no debugging */
#define DEBUG_STATUS_ONLY 1 /* debug status only */
#define DEBUG_PROGRESS_TOO 2 /* debug progress too */
#define DEBUG_EVERYTHING 3 /* debug buffers too */
#define DEBUG_LVL DEBUG_NOTHING /* Choose your poison */
#if DEBUG_LVL == DEBUG_NOTHING
#define CONERROR WRMSG
#define CONDEBUG1 1 ? ((void)0) : WRMSG
#define CONDEBUG2 1 ? ((void)0) : WRMSG
#define DUMPBUF(_msg,_addr,_len,_ebcdic)
#elif DEBUG_LVL == DEBUG_STATUS_ONLY
#define CONERROR WRMSG
#define CONDEBUG1 WRMSG
#define CONDEBUG2 1 ? ((void)0) : WRMSG
#define DUMPBUF(_msg,_addr,_len,_ebcdic)
#elif DEBUG_LVL == DEBUG_PROGRESS_TOO
#define CONERROR WRMSG
#define CONDEBUG1 WRMSG
#define CONDEBUG2 WRMSG
#define DUMPBUF(_msg,_addr,_len,_ebcdic)
#elif DEBUG_LVL == DEBUG_EVERYTHING
#define CONERROR WRMSG
#define CONDEBUG1 WRMSG
#define CONDEBUG2 WRMSG
#define DUMPBUF( _msg, _addr, _len, _ebcdic ) \
dumpbuf( #_msg "D +", _addr, _len, _ebcdic )
static void dumpbuf( const char* pfx, const void* addr, int len, BYTE ebcdic )
{
if (len)
{
char* dump = NULL;
const char* ccaddr = (const char*) addr;
if (ebcdic)
hexdumpe16( pfx, &dump, ccaddr, 0, len, 0, 4, 4 );
else
hexdumpa16( pfx, &dump, ccaddr, 0, len, 0, 4, 4 );
if (dump)
{
LOGMSG( "%s", dump );
free( dump );
}
else
{
// "COMM: error in function %s: %s"
WRMSG( HHC01034, "E", "dumpbuf()", strerror( errno ));
}
}
}
#endif // DEBUG_EVERYTHING
/*-------------------------------------------------------------------*/
/* Ivan Warren 20040227 */
/* */
/* This table is used by channel.c to determine if a CCW code */
/* is an immediate command or not. */
/* */
/* The table is addressed in the DEVHND structure as 'DEVIMM immed' */
/* */
/* 0: ("false") Command is *NOT* an immediate command */
/* 1: ("true") Command *IS* an immediate command */
/* */
/* Note: An immediate command is defined as a command which returns */
/* CE (channel end) during initialization (that is, no data is */
/* actually transfered). In this case, IL is not indicated for a */
/* Format 0 or Format 1 CCW when IL Suppression Mode is in effect. */
/* */
/*-------------------------------------------------------------------*/
static BYTE constty_immed [256] =
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
{ 0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0, /* 00 */ // 03, 0B
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 10 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 20 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 30 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 60 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 80 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* A0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* B0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* C0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* D0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* E0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* F0 */
static BYTE loc3270_immed [256] =
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
{ 0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1, /* 00 */ // 03, 0B, 0F
0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0, /* 10 */ // 1B
0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0, /* 20 */ // 2B
0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0, /* 30 */ // 3B
0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0, /* 40 */ // 4B
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 60 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 80 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* A0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* B0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* C0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* D0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* E0 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; /* F0 */
/*-------------------------------------------------------------------*/
/* Static variables and forward references */
/*-------------------------------------------------------------------*/
static int console_cnslcnt = 0;
static void* console_connection_handler( void* arg );
static BYTE solicit_3270_data( DEVBLK* dev, BYTE cmd );
static void loc3270_input( TELNET* tn, const BYTE* buffer, U32 size );
static void constty_input( TELNET* tn, const BYTE* buffer, U32 size );
static void negotiate_ttype( TELNET* tn );
/*-------------------------------------------------------------------*/
/* Telnet options negotiation table */
/*-------------------------------------------------------------------*/
/* */
/* The Telopt Options table specifies which TELNET options your */
/* application supports both locally and/or remotely. */
/* */
/* This table is comprised of telnet_telopt_t structures, one for */
/* each supported option. Each entry specifies the option and */
/* whether it is supported locally or remotely. */
/* */
/* The first column after the option (us) denotes whether or not */
/* your application wishes the option to be enabled locally and */
/* should be set to TELNET_WILL if yes and or TELNET_WONT if not. */
/* */
/* The second column (them) denotes whether or not you wish the */
/* option to be enabled on the remote end, and should be set to */
/* TELNET_DO if yes or TELNET_DONT if not. */
/* */
/* Options not defined in the table are handled automatically by */
/* libtelnet as TELNET_WONT and TELNET_DONT (i.e. libtelnet will */
/* automatically reply with a TELNET_DONT or TELNET_WONT reply */
/* for any TELNET_WILL or TELNET_DO sent by the remote side). */
/* */
/* You should therefore only define table entries for options you */
/* wish to possibly support (possibly have enabled) either locally */
/* or remotely (us = TELNET_WILL or them = TELNET_DO). */
/* */
/* Note that TELNET_WILL does not mean the option is enabled on */
/* your end (locally), only that you wish to enable it. It must */
/* not actually be enabled until the remote end first responds to */
/* your TELNET_WILL request with a TELNET_DO reply (at which point */
/* you will receive a TELNET_EV_DO event informing you it is now */
/* okay to go ahead and enable that option. */
/* */
/* Correspondingly, TELNET_DO table entries for the remote side, */
/* indicating that you'd like that option to be enabled at their */
/* end, must not be treated as enabled until they first respond */
/* to your TELNET_DO request with a TELNET_WILL response (at which */
/* point you will be notified via a TELNET_EV_WILL event informing */
/* you that the option is now enabled on their end. */
/* */
/* Further note that TELNET_WILL/TELNET_WONT/TELNET_DO/TELNET_DONT */
/* table entry values are not by any means carved in stone. Any of */
/* the telnet options can be changed at any time by simply calling */
/* the telnet_negotiate() function with your new WILL/WONT/DO/DONT */
/* value for that option and then handling the corresponding event */
/* appropriately depending on how the other side responds. */
/* */
/*-------------------------------------------------------------------*/
static const telnet_telopt_t telnet_opts[] =
{
//--------------------------------------------------------------
// PROGRAMMING NOTE: the TELNET_TELOPT_BINARY, TELNET_TELOPT_EOR
// and TELNET_TELOPT_ECHO options are negotiated manually for each
// connection (via the libtelnet "telnet_negotiate" function) once
// the terminal type (TTYPE) is known. For 3270 terminals begining
// with "IBM-" we operate in BINARY+EOR mode. For all other types
// we don't and negotiate the TELNET_TELOPT_ECHO option instead.
// Refer the the "negotiate_ttype" function for more information.
//--------------------------------------------------------------
// (option) (us) (them)
{ TELNET_TELOPT_TTYPE, TELNET_WONT, TELNET_DO }, // (BOTH)
// { TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO }, // TTY
// { TELNET_TELOPT_BINARY, TELNET_WILL, TELNET_DO }, // TN3270
// { TELNET_TELOPT_EOR, TELNET_WILL, TELNET_DO }, // TN3270
{ -1, 0, 0 } /***** REQUIRED END OF TABLE MARKER *****/
};
/*-------------------------------------------------------------------*/
/* (event handler helper macro) */
/*-------------------------------------------------------------------*/
#define LOG_WARNING_OR_ERROR_EVENT( which, tn, ev ) \
\
/* "%s COMM: libtelnet %s: %s in %s() at %s(%d): %s" */ \
WRMSG \
( \
HHC02907, "I", \
(tn)->clientid, \
(which), \
telnet_err_name( (ev)->error.err ), \
(ev)->error.func, \
(ev)->error.file, \
(ev)->error.line, \
(ev)->error.msg \
)
/*-------------------------------------------------------------------*/
/* Telnet event handler */
/*-------------------------------------------------------------------*/
static void telnet_ev_handler( telnet_t* telnet, telnet_event_t* ev,
void* user_data )
{
TELNET* tn = (TELNET*) user_data;
#ifdef TN_DEBUG_NEGOTIATIONS
switch (ev->type)
{
case TELNET_EV_DO:
case TELNET_EV_DONT:
case TELNET_EV_WILL:
case TELNET_EV_WONT:
// "%s COMM: negotiating %-14s %s"
WRMSG( HHC90511, "D", tn->clientid,
telnet_evt_name( ev->neg.type ),
telnet_opt_name( ev->neg.telopt ));
break;
default:
break;
}
#endif // TN_DEBUG_NEGOTIATIONS
switch (ev->type)
{
/* Raw data needs to be sent to peer */
case TELNET_EV_SEND:
// "%s COMM: sending %d bytes"
CONDEBUG2( HHC90500, "D", tn->clientid, ev->data.size );
DUMPBUF( HHC90500, ev->data.buffer, ev->data.size, tn->do_tn3270 ? 1 : 0 );
if (write_socket( tn->csock, ev->data.buffer, ev->data.size ) <= 0)
{
tn->send_err = TRUE;
// "%s COMM: send() failed: %s"
WRMSG( HHC02900, "E", tn->clientid, strerror( errno ));
}
break;
/* Non-telnet data received from peer */
case TELNET_EV_DATA:
if (tn->do_tn3270)
loc3270_input( tn, ev->data.buffer, ev->data.size );
else
constty_input( tn, ev->data.buffer, ev->data.size );
break;
/* Enable local option */
case TELNET_EV_DO:
/* Enable BINARY mode when requested */
if (ev->neg.telopt == TELNET_TELOPT_BINARY)
{
tn->do_bin = 1;
}
/* Enable "Suppress Go Aheads" when asked */
else if (ev->neg.telopt == TELNET_TELOPT_SGA)
{
; // (ignore; we always suppress go-aheads anyway)
}
/* Refuse to enable TTYPE option when asked */
else if (ev->neg.telopt == TELNET_TELOPT_TTYPE)
{
/* We can't enable it since we don't HAVE a Terminal Type! */
// "%s COMM: refusing client demand to %s %s"
WRMSG( HHC02901, "W", tn->clientid, "enable", "TTYPE" );
telnet_negotiate( tn->ctl, TELNET_WONT, TELNET_TELOPT_TTYPE );
tn->neg_fail = TRUE;
}
/* Enable EOR mode when requested */
else if (ev->neg.telopt == TELNET_TELOPT_EOR)
{
tn->do_eor = 1;
}
/* Refuse to enable any other options we don't support */
else
{
// "%s COMM: refusing client demand to %s %s"
WRMSG( HHC02901, "W", tn->clientid, "enable",
telnet_opt_name( ev->neg.telopt ) );
telnet_negotiate( tn->ctl, TELNET_WONT, ev->neg.telopt );
tn->neg_fail = TRUE;
}
break;
/* Disable local option */
case TELNET_EV_DONT:
/* Disable BINARY mode when requested (if possible) */
if (ev->neg.telopt == TELNET_TELOPT_BINARY)
{
if (tn->do_tn3270)
{
/* REFUSE! TN3270 mode requires it! */
// "%s COMM: refusing client demand to %s %s"
WRMSG( HHC02901, "W", tn->clientid, "disable", "BINARY mode" );
telnet_negotiate( tn->ctl, TELNET_WILL, TELNET_TELOPT_BINARY );
tn->neg_fail = TRUE;
}
else
{
/* Otherwise do as requested */
if (tn->do_bin)
{
tn->do_bin = 0;
telnet_negotiate( tn->ctl, TELNET_WONT, TELNET_TELOPT_BINARY );
}
}
}
/* ALWAYS refuse to disable "Suppress Go Aheads" */
else if (ev->neg.telopt == TELNET_TELOPT_SGA)
{
// "%s COMM: refusing client demand to %s %s"
WRMSG( HHC02901, "W", tn->clientid, "disable", "Suppress Go Aheads" );
telnet_negotiate( tn->ctl, TELNET_WILL, TELNET_TELOPT_SGA );
tn->neg_fail = TRUE;
}
/* This should never occur since we never said we wanted to enable
it, but if they want to demand we disable it anyway, then fine. */
else if (ev->neg.telopt == TELNET_TELOPT_TTYPE)
{
; // (ignore)
}
/* Disable EOR mode when requested (if possible) */
else if (ev->neg.telopt == TELNET_TELOPT_EOR)
{
if (tn->do_tn3270)
{
/* REFUSE! TN3270 mode requires it! */
// "%s COMM: refusing client demand to %s %s"
WRMSG( HHC02901, "W", tn->clientid, "disable", "EOR mode" );
telnet_negotiate( tn->ctl, TELNET_WILL, TELNET_TELOPT_EOR );
tn->neg_fail = TRUE;
}
else
{
/* Otherwise do as requested */
if (tn->do_eor)
{
tn->do_eor = 0;
telnet_negotiate( tn->ctl, TELNET_WONT, TELNET_TELOPT_EOR );
}
}
}
break;
/* Remote option enabled */
case TELNET_EV_WILL:
if (ev->neg.telopt == TELNET_TELOPT_BINARY)
{
; // (ignore)
}
else if (ev->neg.telopt == TELNET_TELOPT_SGA)
{
; // (ignore; we expect them to always suppress go-aheads anyway)
}
/* Ask them to SEND us their TTYPE when they're willing to */
else if (ev->neg.telopt == TELNET_TELOPT_TTYPE)
{
telnet_ttype_send( tn->ctl );
}
else if (ev->neg.telopt == TELNET_TELOPT_EOR)
{
; // (ignore)
}
break;
/* Remote option disabled */
case TELNET_EV_WONT:
/* Fail negotiations if they refused TN3270 BINARY mode */
if (ev->neg.telopt == TELNET_TELOPT_BINARY)
{
if (tn->do_tn3270)
{
/* TN3270 mode requires it! */
// "%s COMM: client refused to %s %s"
WRMSG( HHC02902, "W", tn->clientid, "enable", "BINARY mode" );
tn->neg_fail = TRUE;
}
}
/* Fail negotiations if they refuse to Suppress Go Aheads */
else if (ev->neg.telopt == TELNET_TELOPT_SGA)
{
// "%s COMM: client refused to %s %s"
WRMSG( HHC02902, "W", tn->clientid, "enable", "Suppress Go Aheads" );
tn->neg_fail = TRUE;
}
/* Fail negotiations if they refuse to send their TTYPE */
else if (ev->neg.telopt == TELNET_TELOPT_TTYPE)
{
// "%s COMM: client refused to %s %s"
WRMSG( HHC02902, "W", tn->clientid, "enable", "TTYPE" );
tn->neg_fail = TRUE;
}
/* Fail negotiations if they refused TN3270 EOR mode */
else if (ev->neg.telopt == TELNET_TELOPT_EOR)
{
if (tn->do_tn3270)
{
/* TN3270 mode requires it! */
// "%s COMM: client refused to %s %s"
WRMSG( HHC02902, "W", tn->clientid, "enable", "EOR mode" );
tn->neg_fail = TRUE;
}
}
break;
/* TTYPE command has been received */
case TELNET_EV_TTYPE:
if (ev->ttype.cmd == TELNET_TTYPE_IS)
{
/* Save terminal type */
STRLCPY( tn->ttype, ev->ttype.name );
/* Finish negotiations based on terminal type */
negotiate_ttype( tn );
}
/* Respond with NULL Terminal Type when asked to send it */
else if (ev->ttype.cmd == TELNET_TTYPE_SEND)
{
static const char* ttype = "";
telnet_ttype_is( telnet, ttype );
}
break;
/* Respond to particular subnegotiations */
case TELNET_EV_SUBNEGOTIATION:
break;
/* Generic IAC command received */
case TELNET_EV_IAC:
#ifdef TN_DEBUG_SIMPLE_IAC
#ifndef TN_DEBUG_NOP_IAC
if (ev->iac.cmd != TELNET_NOP)
#endif
// "%s COMM: received IAC %s"
WRMSG( HHC90512, "D", tn->clientid, telnet_cmd_name( ev->iac.cmd ));
#endif
/* End of record */
if (ev->iac.cmd == TELNET_EOR)
{
if (tn->do_eor)
tn->got_eor = TRUE;
}
/* Break */
else if (ev->iac.cmd == TELNET_BREAK)
{
if (!tn->do_tn3270)
tn->got_break = TRUE;
}
/* Interrupt Process */
else if (ev->iac.cmd == TELNET_IP)
{
if (!tn->do_tn3270)
tn->got_break = TRUE;
}
/* Erase character */
else if (ev->iac.cmd == TELNET_EC)
{
if (!tn->do_tn3270)
{
if (tn->dev && tn->dev->keybdrem > 0)
tn->dev->keybdrem--;
}
}
/* Erase line */
else if (ev->iac.cmd == TELNET_EL)
{
if (!tn->do_tn3270)
{
if (tn->dev)
tn->dev->keybdrem = 0;
}
}
break;
/* Recoverable error has occured */
case TELNET_EV_WARNING:
LOG_WARNING_OR_ERROR_EVENT( "WARNING", tn, ev );
/* Remember negotiation errors */
if (ev->error.err == TELNET_ENEGOTIATION)
{
// "%s COMM: libtelnet negotiation error"
WRMSG( HHC02905, "W", tn->clientid );
tn->neg_fail = TRUE;
}
else
// "%s COMM: libtelnet error"
WRMSG( HHC02903, "W", tn->clientid );
break;
/* Non-recoverable error has occured */
case TELNET_EV_ERROR:
LOG_WARNING_OR_ERROR_EVENT( "ERROR", tn, ev );
// "%s COMM: libtelnet FATAL error"
WRMSG( HHC02904, "E", tn->clientid );
break;
default:
// "%s COMM: Unsupported libtelnet event '%s'"
WRMSG( HHC02906, "W", tn->clientid, telnet_evt_name( ev->type ));
break;
}
} /* end function telnet_ev_handler */
/*-------------------------------------------------------------------*/
/* MinGW (Windows, but *not* MSVC) */
/*-------------------------------------------------------------------*/
#if defined( WIN32 ) && !defined( _MSVC_ ) && !defined( HDL_USE_LIBTOOL )
SYSBLK *psysblk;
#define sysblk (*psysblk)
#endif
/*-------------------------------------------------------------------*/
/* SEND DATA TO TELNET CLIENT */
/*-------------------------------------------------------------------*/
static BYTE sendto_client( TELNET* tn, const BYTE* buf, unsigned int len )
{
BYTE success = TRUE;
unsigned int sendbuf_needed;
if (len > 0)
{
tn->send_err = FALSE;
if (tn->devclass == 'K')
{
/* PROGRAMMING NOTE: the constty_execute_ccw() function
that calls us ensures the buffer it passes to us is
always null terminated so the below telnet_printf
does not print garbage past the end of actual data.
*/
telnet_printf( tn->ctl, "%s", buf );
}
else // (tn->devclass == 'D' || tn->devclass == 'P')
{
/* Adjust one shot send buffer */
sendbuf_needed = (len * 2) + 2;
if (sendbuf_needed > tn->sendbuf_size)
{
if (tn->sendbuf_size) free( tn->sendbuf );
tn->sendbuf_size = sendbuf_needed;
tn->sendbuf = (char*) malloc( tn->sendbuf_size );
}
telnet_send_one_shot( tn->ctl, buf, len, tn->sendbuf );
}
success = !tn->send_err;
}
return success;
}
/*-------------------------------------------------------------------*/
/* RAISE ATTENTION INTERRUPT */
/*-------------------------------------------------------------------*/
static void raise_device_attention( DEVBLK* dev, BYTE unitstat )
{
/* NOTE: the device lock must *NOT* be held! */
#if defined( _FEATURE_INTEGRATED_3270_CONSOLE )
if (dev == sysblk.sysgdev)
{
sclp_sysg_attention();
return;
}
#endif
if (dev->tn->devclass != 'P' && !INITIAL_POWERON_370())
{
int rc = device_attention( dev, unitstat );
// "%s COMM: device attention %s; rc=%d"
CONDEBUG1( HHC90506, "D", dev->tn->clientid,
(rc == 0 ? "raised" : "REJECTED"), rc );
}
}
/*-------------------------------------------------------------------*/
/* FINISH INITIALIZING THE CONSOLE DEVICE */
/*-------------------------------------------------------------------*/
static int finish_console_init( DEVBLK* dev )
{
UNREFERENCED( dev );
if (!console_cnslcnt && !sysblk.cnsltid)
{
int rc;
console_cnslcnt++; // No serialisation needed just yet, as
// the console thread is not yet active
if ((rc = create_thread( &sysblk.cnsltid, JOINABLE,
console_connection_handler, NULL,
CON_CONN_THREAD_NAME )))
{
// "Error in function create_thread(): %s"
WRMSG( HHC00102, "E", strerror( rc ));
return 1;
}
}
else
console_cnslcnt++;
return 0;
}
/*-------------------------------------------------------------------*/
/* DISCONNECT TELNET CLIENT */
/*-------------------------------------------------------------------*/
static void disconnect_telnet_client( TELNET* tn )
{
/* PROGRAMMING NOTE: do not call this function once a DEVBLK
has been chosen. Only use it to disconnect a client before
negotiations have been completed (before a DEVBLK has been
chosen). Once a DEVBLK has been chosen, you should use the
disconnect_console_device function instead.
*/
if (tn)
{
telnet_closesocket( tn->csock );
telnet_free( tn->ctl );
/* Free one shot send buffer if necessary */
if (tn->sendbuf_size) free( tn->sendbuf );
tn->sendbuf_size = 0;
// "%s COMM: disconnected"
CONDEBUG1( HHC90504, "D", tn->clientid );
free( tn );
}
}
/*-------------------------------------------------------------------*/
/* DISCONNECT CONSOLE DEVICE */
/*-------------------------------------------------------------------*/
static void disconnect_console_device( DEVBLK* dev )
{
/* PROGRAMMING NOTE: this function should only be called to
disconnect a 3270/TTY console device from a telnet client
such as when a serious I/O error occurs. It physically
closes the device and marks it available for reuse.
*/
dev->connected = 0;
dev->fd = -1;
disconnect_telnet_client( dev->tn );
dev->tn = NULL;
}
/*-------------------------------------------------------------------*/
/* FINISH CLOSING THE CONSOLE DEVICE */
/*-------------------------------------------------------------------*/
static void finish_console_close( DEVBLK* dev )
{
/* PROGRAMMING NOTE: this function should never be called
directly. It is a helper function for the device handler
close function.
*/
disconnect_console_device( dev );
console_cnslcnt--;
SIGNAL_CONSOLE_THREAD();
if (!console_cnslcnt)
{
release_lock( &dev->lock );
{
join_thread( sysblk.cnsltid, NULL);
}
obtain_lock (&dev->lock );
sysblk.cnsltid = 0;
}
}
/*-------------------------------------------------------------------*/
/* CLOSE THE 3270 DEVICE */
/*-------------------------------------------------------------------*/
static int loc3270_close_device( DEVBLK* dev )
{
/* PROGRAMMING NOTE: this function is the device handler's
close function. It should only be called when detaching
a 3270 console device. To disconnect a 3270 console from
whatever telnet client is connected to it, you should use
the disconnect_console_device function instead.
*/
#if defined(_FEATURE_INTEGRATED_3270_CONSOLE)
if (dev == sysblk.sysgdev)
sysblk.sysgdev = NULL;
#endif
finish_console_close( dev );
return 0;
}
/*-------------------------------------------------------------------*/
/* CLOSE THE 1052/3215 DEVICE */
/*-------------------------------------------------------------------*/
static int constty_close_device( DEVBLK* dev )
{
/* PROGRAMMING NOTE: this function is the device handler's
close function. It should only be called when detaching
a TTY console device. To disconnect a TTY console from
whatever telnet client is connected to it, you should use
the disconnect_console_device function instead.
*/
finish_console_close (dev );
return 0;
}
/*-------------------------------------------------------------------*/
/* INITIALIZE 3270 DEVICE */
/*-------------------------------------------------------------------*/
static int loc3270_init_handler( DEVBLK* dev, int argc, char* argv[] )
{
int ac = 0;
BYTE dt;
/* Close the existing file in case we're re-initialising */
if (dev->fd >= 0)
(dev->hnd->close)( dev );
/* reset excp count */
dev->excps = 0;
/* Indicate that this is a console device */
dev->console = 1;
/* Reset device dependent flags */
dev->connected = 0;
/* Set number of sense bytes */
dev->numsense = 1;
/* Set the size of the device buffer */
dev->bufsize = BUFLEN_3270;
if (!sscanf( dev->typname, "%hx", &dev->devtype ))
dev->devtype = 0x3270;
#if defined( _FEATURE_INTEGRATED_3270_CONSOLE )
/* Extra initialisation for the SYSG console */
if (strcasecmp( dev->typname, "SYSG" ) == 0)
{
if (sysblk.sysgdev != NULL)
{
// "%1d:%04X COMM: duplicate SYSG console definition"
WRMSG( HHC01025, "E", LCSS_DEVNUM );
return -1;
}
dev->pmcw.flag5 &= ~PMCW5_V; // (not a regular device)
}
#endif
/* Initialize the device identifier bytes */
dev->devid[0] = 0xFF; /* First byte is always 0xFF */
dev->devid[1] = 0x32; /* Control unit type is 3274-1D */
dev->devid[2] = 0x74; /* Control unit type is 3274-1D */
dev->devid[3] = 0x1D; /* Control unit type is 3274-1D */
dev->devid[4] = 0x32; /* Device type major */
dt = (dev->devtype & 0xFF); /* Device type minor */
if (dt == 0x70) /* Device statement type = 3270? */
{
dev->devid[5] = 0x78; /* then it's a 3278-2 by default */
dev->devid[6] = 0x02; /* then it's a 3278-2 by default */
}
else
{
/* Not a 3270 display. Use the device type
they specified on their device statement,
e.g. 3287, and force it to be a model 1.
*/
dev->devid[5] = dt; /* Device statement type */
dev->devid[6] = 0x01; /* Model 1 */
}
dev->numdevid = 7;
dev->filename[0] = 0;
dev->acc_ipaddr = 0;
dev->acc_ipmask = 0;
if (argc > 0) // group name?
{
if ('*' == argv[ac][0] && '\0' == argv[ac][1])
; // NOP (not really a group name; an '*' is
// simply used as an argument place holder)
else
{
if (0
|| strlen( argv[ac] ) <= 0
|| strlen( argv[ac] ) >= 9
)
{
// "%1d:%04X COMM: %s has an invalid GROUP name length or format; must be a valid luname or poolname"
WRMSG( HHC01091, "E", LCSS_DEVNUM, argv[ac] );
return -1;
}
else
{
char group[9];
int i;
int rc;
strupper( group, argv[ac] );
for (i=1, rc=0; i < (int) strlen( group ) && rc == 0; i++)
if (!isalnum( group[i] ))
rc = -1;
if (rc == 0 && isalpha( group[0] ))
STRLCPY( dev->filename, group );
else
{
// "%1d:%04X COMM: %s has an invalid GROUP name length or format; must be a valid luname or poolname"
WRMSG(HHC01091, "E", LCSS_DEVNUM, argv[ac] );
return -1;
}
}
}
argc--;
ac++;
if (argc > 0) // ip address?
{
if ((dev->acc_ipaddr = inet_addr( argv[ac] )) == (in_addr_t)(-1))
{
// "%1d:%04X COMM: option %s value %s invalid"
WRMSG( HHC01007, "E", LCSS_DEVNUM, "IP address", argv[ac] );
return -1;
}
else
{
argc--;
ac++;
if (argc <= 0) // ip addr mask?
dev->acc_ipmask = (in_addr_t)(-1);
else
{
char mask[16] = {0};
static const char* badmask = "0.0.0.0";
if (0
|| inet_pton( AF_INET, argv[ac], &dev->acc_ipmask ) <= 0
|| str_eq( badmask, inet_ntop( AF_INET, &dev->acc_ipmask,
mask, (int) sizeof( mask )))
)
{
// "%1d:%04X COMM: option %s value %s invalid"
WRMSG( HHC01007, "E", LCSS_DEVNUM, "mask", argv[ac] );
return -1;
}
argc--;
ac++;
if (argc > 0) // too many args?
{
// "%1d:%04X COMM: unrecognized parameter %s"
WRMSG( HHC01019, "E", LCSS_DEVNUM, argv[ac] );
return -1;
}