forked from hercules-390/hyperion
/
ctcadpt.c
1329 lines (1092 loc) · 43.1 KB
/
ctcadpt.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
/* CTCADPT.C (c) Copyright James A. Pierson, 2002-2012 */
/* (c) Copyright Roger Bowler, 2000-2012 */
/* (c) Copyright Willem Konynenberg, 2000-2009 */
/* (c) Copyright Vic Cross, 2001-2009 */
/* Hercules Channel-to-Channel Emulation Support */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
// Hercules Channel-to-Channel Emulation Support
// ====================================================================
//
// vmnet - (c) Copyright Willem Konynenberg, 2000-2009
// CTCT - (c) Copyright Vic Cross, 2001-2009
//
// Notes:
// This module contains the remaining CTC emulation modes that
// have not been moved to seperate modules. There is also logic
// to allow old style 3088 device definitions for compatibility
// and may be removed in a future release.
//
// Please read README.NETWORKING for more info.
//
#include "hstdinc.h"
#define _CTCADPT_C_
#define _HENGINE_DLL_
#include "hercules.h"
#include "devtype.h"
#include "ctcadpt.h"
#include "opcode.h"
#include "devtype.h"
// ====================================================================
// Declarations
// ====================================================================
static int CTCT_Init( DEVBLK *dev, int argc, char *argv[] );
static void CTCT_Read( DEVBLK* pDEVBLK, U32 sCount,
BYTE* pIOBuf, BYTE* pUnitStat,
U32* pResidual, BYTE* pMore );
static void CTCT_Write( DEVBLK* pDEVBLK, U32 sCount,
BYTE* pIOBuf, BYTE* pUnitStat,
U32* pResidual );
static void* CTCT_ListenThread( void* argp );
static int VMNET_Init( DEVBLK *dev, int argc, char *argv[] );
static int VMNET_Write( DEVBLK *dev, BYTE *iobuf,
U32 count, BYTE *unitstat );
static int VMNET_Read( DEVBLK *dev, BYTE *iobuf,
U32 count, BYTE *unitstat );
// --------------------------------------------------------------------
// Definitions for CTC general data blocks
// --------------------------------------------------------------------
typedef struct _CTCG_PARMBLK
{
int listenfd;
struct sockaddr_in addr;
DEVBLK* dev;
}
CTCG_PARMBLK;
// --------------------------------------------------------------------
// Device Handler Information Block
// --------------------------------------------------------------------
DEVHND ctcadpt_device_hndinfo =
{
&CTCX_Init, /* Device Initialisation */
&CTCX_ExecuteCCW, /* Device CCW execute */
&CTCX_Close, /* Device Close */
&CTCX_Query, /* Device Query */
NULL, /* Device Extended Query */
NULL, /* Device Start channel pgm */
NULL, /* Device End channel pgm */
NULL, /* Device Resume channel pgm */
NULL, /* Device Suspend channel pgm */
NULL, /* Device Halt channel pgm */
NULL, /* Device Read */
NULL, /* Device Write */
NULL, /* Device Query used */
NULL, /* Device Reserve */
NULL, /* Device Release */
NULL, /* Device Attention */
NULL, /* Immediate CCW Codes */
NULL, /* Signal Adapter Input */
NULL, /* Signal Adapter Output */
NULL, /* Signal Adapter Sync */
NULL, /* Signal Adapter Output Mult */
NULL, /* QDIO subsys desc */
NULL, /* QDIO set subchan ind */
NULL, /* Hercules suspend */
NULL /* Hercules resume */
};
DEVHND ctct_device_hndinfo =
{
&CTCT_Init, /* Device Initialisation */
&CTCX_ExecuteCCW, /* Device CCW execute */
&CTCX_Close, /* Device Close */
&CTCX_Query, /* Device Query */
NULL, /* Device Extended Query */
NULL, /* Device Start channel pgm */
NULL, /* Device End channel pgm */
NULL, /* Device Resume channel pgm */
NULL, /* Device Suspend channel pgm */
NULL, /* Device Halt channel pgm */
NULL, /* Device Read */
NULL, /* Device Write */
NULL, /* Device Query used */
NULL, /* Device Reserve */
NULL, /* Device Release */
NULL, /* Device Attention */
NULL, /* Immediate CCW Codes */
NULL, /* Signal Adapter Input */
NULL, /* Signal Adapter Output */
NULL, /* Signal Adapter Sync */
NULL, /* Signal Adapter Output Mult */
NULL, /* QDIO subsys desc */
NULL, /* QDIO set subchan ind */
NULL, /* Hercules suspend */
NULL /* Hercules resume */
};
DEVHND vmnet_device_hndinfo =
{
&VMNET_Init, /* Device Initialisation */
&CTCX_ExecuteCCW, /* Device CCW execute */
&CTCX_Close, /* Device Close */
&CTCX_Query, /* Device Query */
NULL, /* Device Extended Query */
NULL, /* Device Start channel pgm */
NULL, /* Device End channel pgm */
NULL, /* Device Resume channel pgm */
NULL, /* Device Suspend channel pgm */
NULL, /* Device Halt channel pgm */
NULL, /* Device Read */
NULL, /* Device Write */
NULL, /* Device Query used */
NULL, /* Device Reserve */
NULL, /* Device Release */
NULL, /* Device Attention */
NULL, /* Immediate CCW Codes */
NULL, /* Signal Adapter Input */
NULL, /* Signal Adapter Output */
NULL, /* Signal Adapter Sync */
NULL, /* Signal Adapter Output Mult */
NULL, /* QDIO subsys desc */
NULL, /* QDIO set subchan ind */
NULL, /* Hercules suspend */
NULL /* Hercules resume */
};
extern DEVHND ctci_device_hndinfo;
extern DEVHND lcs_device_hndinfo;
// ====================================================================
// Primary Module Entry Points
// ====================================================================
// --------------------------------------------------------------------
// Device Initialization Handler (Generic)
// --------------------------------------------------------------------
int CTCX_Init( DEVBLK* pDEVBLK, int argc, char *argv[] )
{
pDEVBLK->devtype = 0x3088;
pDEVBLK->excps = 0;
// The first argument is the device emulation type
if( argc < 1 )
{
WRMSG (HHC00915, "E", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum );
return -1;
}
if((pDEVBLK->hnd = hdl_ghnd(argv[0])))
{
if(pDEVBLK->hnd->init == &CTCX_Init)
return -1;
free(pDEVBLK->typname);
pDEVBLK->typname = strdup(argv[0]);
return (pDEVBLK->hnd->init)( pDEVBLK, --argc, ++argv );
}
WRMSG (HHC00970, "E", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, argv[0]);
return -1;
}
// -------------------------------------------------------------------
// Query the device definition (Generic)
// -------------------------------------------------------------------
void CTCX_Query( DEVBLK* pDEVBLK,
char** ppszClass,
int iBufLen,
char* pBuffer )
{
BEGIN_DEVICE_CLASS_QUERY( "CTCA", pDEVBLK, ppszClass, iBufLen, pBuffer );
snprintf( pBuffer, iBufLen-1, "%s IO[%" I64_FMT "u]", pDEVBLK->filename, pDEVBLK->excps );
}
// -------------------------------------------------------------------
// Close the device (Generic)
// -------------------------------------------------------------------
int CTCX_Close( DEVBLK* pDEVBLK )
{
// Close the device file (if not already closed)
if( pDEVBLK->fd >= 0 )
{
if (socket_is_socket( pDEVBLK->fd ))
close_socket( pDEVBLK->fd );
else
close( pDEVBLK->fd );
pDEVBLK->fd = -1; // indicate we're now closed
}
return 0;
}
// -------------------------------------------------------------------
// Execute a Channel Command Word (Generic)
// -------------------------------------------------------------------
void CTCX_ExecuteCCW( DEVBLK* pDEVBLK, BYTE bCode,
BYTE bFlags, BYTE bChained,
U32 sCount, BYTE bPrevCode,
int iCCWSeq, BYTE* pIOBuf,
BYTE* pMore, BYTE* pUnitStat,
U32* pResidual )
{
int iNum; // Number of bytes to move
BYTE bOpCode; // CCW opcode with modifier
// bits masked off
UNREFERENCED( bFlags );
UNREFERENCED( bChained );
UNREFERENCED( bPrevCode );
UNREFERENCED( iCCWSeq );
// Intervention required if the device file is not open
if( pDEVBLK->fd < 0 &&
!IS_CCW_SENSE( bCode ) &&
!IS_CCW_CONTROL( bCode ) )
{
pDEVBLK->sense[0] = SENSE_IR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Mask off the modifier bits in the CCW bOpCode
if( ( bCode & 0x07 ) == 0x07 )
bOpCode = 0x07;
else if( ( bCode & 0x03 ) == 0x02 )
bOpCode = 0x02;
else if( ( bCode & 0x0F ) == 0x0C )
bOpCode = 0x0C;
else if( ( bCode & 0x03 ) == 0x01 )
bOpCode = pDEVBLK->ctcxmode ? ( bCode & 0x83 ) : 0x01;
else if( ( bCode & 0x1F ) == 0x14 )
bOpCode = 0x14;
else if( ( bCode & 0x47 ) == 0x03 )
bOpCode = 0x03;
else if( ( bCode & 0xC7 ) == 0x43 )
bOpCode = 0x43;
else
bOpCode = bCode;
// Process depending on CCW bOpCode
switch (bOpCode)
{
case 0x01: // 0MMMMM01 WRITE
//------------------------------------------------------------
// WRITE
//------------------------------------------------------------
// Return normal status if CCW count is zero
if( sCount == 0 )
{
*pUnitStat = CSW_CE | CSW_DE;
break;
}
// Write data and set unit status and residual byte count
switch( pDEVBLK->ctctype )
{
case CTC_CTCT:
CTCT_Write( pDEVBLK, sCount, pIOBuf, pUnitStat, pResidual );
break;
case CTC_VMNET:
*pResidual = sCount - VMNET_Write( pDEVBLK, pIOBuf,
sCount, pUnitStat );
break;
}
break;
case 0x81: // 1MMMMM01 WEOF
//------------------------------------------------------------
// WRITE EOF
//------------------------------------------------------------
// Return normal status
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x02: // MMMMMM10 READ
case 0x0C: // MMMM1100 RDBACK
// -----------------------------------------------------------
// READ & READ BACKWARDS
// -----------------------------------------------------------
// Read data and set unit status and residual byte count
switch( pDEVBLK->ctctype )
{
case CTC_CTCT:
CTCT_Read( pDEVBLK, sCount, pIOBuf, pUnitStat, pResidual, pMore );
break;
case CTC_VMNET:
*pResidual = sCount - VMNET_Read( pDEVBLK, pIOBuf,
sCount, pUnitStat );
break;
}
break;
case 0x07: // MMMMM111 CTL
// -----------------------------------------------------------
// CONTROL
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x03: // M0MMM011 NOP
// -----------------------------------------------------------
// CONTROL NO-OPERATON
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x43: // 00XXX011 SBM
// -----------------------------------------------------------
// SET BASIC MODE
// -----------------------------------------------------------
// Command reject if in basic mode
if( pDEVBLK->ctcxmode == 0 )
{
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
break;
}
// Reset extended mode and return normal status
pDEVBLK->ctcxmode = 0;
*pResidual = 0;
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xC3: // 11000011 SEM
// -----------------------------------------------------------
// SET EXTENDED MODE
// -----------------------------------------------------------
pDEVBLK->ctcxmode = 1;
*pResidual = 0;
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xE3: // 11100011
// -----------------------------------------------------------
// PREPARE (PREP)
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x14: // XXX10100 SCB
// -----------------------------------------------------------
// SENSE COMMAND BYTE
// -----------------------------------------------------------
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0x04: // 00000100 SENSE
// -----------------------------------------------------------
// SENSE
// -----------------------------------------------------------
// Command reject if in basic mode
if( pDEVBLK->ctcxmode == 0 )
{
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
break;
}
// Calculate residual byte count
iNum = ( sCount < pDEVBLK->numsense ) ?
sCount : pDEVBLK->numsense;
*pResidual = sCount - iNum;
if( sCount < pDEVBLK->numsense )
*pMore = 1;
// Copy device sense bytes to channel I/O buffer
memcpy( pIOBuf, pDEVBLK->sense, iNum );
// Clear the device sense bytes
memset( pDEVBLK->sense, 0, sizeof( pDEVBLK->sense ) );
// Return unit status
*pUnitStat = CSW_CE | CSW_DE;
break;
case 0xE4: // 11100100 SID
// -----------------------------------------------------------
// SENSE ID
// -----------------------------------------------------------
// Calculate residual byte count
iNum = ( sCount < pDEVBLK->numdevid ) ?
sCount : pDEVBLK->numdevid;
*pResidual = sCount - iNum;
if( sCount < pDEVBLK->numdevid )
*pMore = 1;
// Copy device identifier bytes to channel I/O buffer
memcpy( pIOBuf, pDEVBLK->devid, iNum );
// Return unit status
*pUnitStat = CSW_CE | CSW_DE;
break;
default:
// ------------------------------------------------------------
// INVALID OPERATION
// ------------------------------------------------------------
// Set command reject sense byte, and unit check status
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
}
}
// ====================================================================
// CTCT Support
// ====================================================================
//
// CTCT_Init
//
static int CTCT_Init( DEVBLK *dev, int argc, char *argv[] )
{
char str[80]; // Thread name
int rc; // Return code
int mtu; // MTU size (binary)
int lport; // Listen port (binary)
int rport; // Destination port (binary)
char* listenp; // Listening port number
char* remotep; // Destination port number
char* mtusize; // MTU size (characters)
char* remaddr; // Remote IP address
struct in_addr ipaddr; // Work area for IP address
BYTE c; // Character work area
TID tid; // Thread ID for server
CTCG_PARMBLK parm; // Parameters for the server
char address[20]=""; // temp space for IP address
dev->devtype = 0x3088;
dev->excps = 0;
dev->ctctype = CTC_CTCT;
SetSIDInfo( dev, 0x3088, 0x08, 0x3088, 0x01 );
// Check for correct number of arguments
if (argc != 4)
{
WRMSG (HHC00915, "E", SSID_TO_LCSS(dev->ssid), dev->devnum );
return -1;
}
// The first argument is the listening port number
listenp = *argv++;
if( strlen( listenp ) > 5 ||
sscanf( listenp, "%u%c", &lport, &c ) != 1 ||
lport < 1024 || lport > 65534 )
{
WRMSG(HHC00916, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "port number", listenp );
return -1;
}
// The second argument is the IP address or hostname of the
// remote side of the point-to-point link
remaddr = *argv++;
if( inet_aton( remaddr, &ipaddr ) == 0 )
{
struct hostent *hp;
if( ( hp = gethostbyname( remaddr ) ) != NULL )
{
memcpy( &ipaddr, hp->h_addr, hp->h_length );
strlcpy( address, inet_ntoa( ipaddr ), sizeof(address) );
remaddr = address;
}
else
{
WRMSG(HHC00916, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "IP address", remaddr );
return -1;
}
}
// The third argument is the destination port number
remotep = *argv++;
if( strlen( remotep ) > 5 ||
sscanf( remotep, "%u%c", &rport, &c ) != 1 ||
rport < 1024 || rport > 65534 )
{
WRMSG(HHC00916, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "port number", remotep );
return -1;
}
// The fourth argument is the maximum transmission unit (MTU) size
mtusize = *argv;
if( strlen( mtusize ) > 5 ||
sscanf( mtusize, "%u%c", &mtu, &c ) != 1 ||
mtu < 46 || mtu > 65536 )
{
WRMSG(HHC00916, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "MTU size", mtusize );
return -1;
}
// Set the device buffer size equal to the MTU size
dev->bufsize = mtu;
// Initialize the file descriptor for the socket connection
// It's a little confusing, but we're using a couple of the
// members of the server paramter structure to initiate the
// outgoing connection. Saves a couple of variable declarations,
// though. If we feel strongly about it, we can declare separate
// variables...
// make a TCP socket
parm.listenfd = socket( AF_INET, SOCK_STREAM, 0 );
if( parm.listenfd < 0 )
{
WRMSG (HHC00900, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "socket()", strerror( HSO_errno ) );
CTCX_Close( dev );
return -1;
}
// bind socket to our local port
// (might seem like overkill, and usually isn't done, but doing this
// bind() to the local port we configure gives the other end a chance
// at validating the connection request)
memset( &(parm.addr), 0, sizeof( parm.addr ) );
parm.addr.sin_family = AF_INET;
parm.addr.sin_port = htons(lport);
parm.addr.sin_addr.s_addr = htonl(INADDR_ANY);
rc = bind( parm.listenfd,
(struct sockaddr *)&parm.addr,
sizeof( parm.addr ) );
if( rc < 0 )
{
WRMSG( HHC00900, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "bind()", strerror( HSO_errno ) );
CTCX_Close( dev );
return -1;
}
// initiate a connection to the other end
memset( &(parm.addr), 0, sizeof( parm.addr ) );
parm.addr.sin_family = AF_INET;
parm.addr.sin_port = htons(rport);
parm.addr.sin_addr = ipaddr;
rc = connect( parm.listenfd,
(struct sockaddr *)&parm.addr,
sizeof( parm.addr ) );
// if connection was not successful, start a server
if( rc < 0 )
{
// used to pass parameters to the server thread
CTCG_PARMBLK* arg;
WRMSG(HHC00971, "I", SSID_TO_LCSS(dev->ssid), dev->devnum, remaddr, remotep );
// probably don't need to do this, not sure...
close_socket( parm.listenfd );
parm.listenfd = socket( AF_INET, SOCK_STREAM, 0 );
if( parm.listenfd < 0 )
{
WRMSG(HHC00900, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "socket()", strerror( HSO_errno ) );
CTCX_Close( dev );
return -1;
}
// set up the listening port
memset( &(parm.addr), 0, sizeof( parm.addr ) );
parm.addr.sin_family = AF_INET;
parm.addr.sin_port = htons(lport);
parm.addr.sin_addr.s_addr = htonl(INADDR_ANY);
if( bind( parm.listenfd,
(struct sockaddr *)&parm.addr,
sizeof( parm.addr ) ) < 0 )
{
WRMSG(HHC00900, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "bind()", strerror( HSO_errno ) );
CTCX_Close( dev );
return -1;
}
if( listen( parm.listenfd, 1 ) < 0 )
{
WRMSG(HHC00900, "E", SSID_TO_LCSS(dev->ssid), dev->devnum, "listen()", strerror( HSO_errno ) );
CTCX_Close( dev );
return -1;
}
// we are listening, so create a thread to accept connection
arg = malloc( sizeof( CTCG_PARMBLK ) );
memcpy( arg, &parm, sizeof( parm ) );
arg->dev = dev;
snprintf(str,sizeof(str),"CTCT %4.4X ListenThread",dev->devnum);
str[sizeof(str)-1]=0;
rc = create_thread( &tid, JOINABLE, CTCT_ListenThread, arg, str );
if(rc)
WRMSG(HHC00102, "E", strerror(rc));
}
else // successfully connected (outbound) to the other end
{
WRMSG(HHC00972, "I", SSID_TO_LCSS(dev->ssid), dev->devnum, remaddr, remotep );
dev->fd = parm.listenfd;
}
// for cosmetics, since we are successfully connected or serving,
// fill in some details for the panel.
snprintf( dev->filename, sizeof(dev->filename), "%s:%s", remaddr, remotep );
dev->filename[sizeof(dev->filename)-1] = '\0';
return 0;
}
//
// CTCT_Write
//
static void CTCT_Write( DEVBLK* pDEVBLK, U32 sCount,
BYTE* pIOBuf, BYTE* pUnitStat,
U32* pResidual )
{
PCTCIHDR pFrame; // -> Frame header
PCTCISEG pSegment; // -> Segment in buffer
U16 sOffset; // Offset of next frame
U16 sSegLen; // Current segment length
U16 sDataLen; // Length of IP Frame data
int iPos; // Offset into buffer
U16 i; // Array subscript
int rc; // Return code
BYTE szStackID[33]; // VSE IP stack identity
U32 iStackCmd; // VSE IP stack command
// Check that CCW count is sufficient to contain block header
if( sCount < sizeof( CTCIHDR ) )
{
WRMSG(HHC00906, "E", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, sCount );
pDEVBLK->sense[0] = SENSE_DC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Fix-up frame pointer
pFrame = (PCTCIHDR)pIOBuf;
// Extract the frame length from the header
FETCH_HW( sOffset, pFrame->hwOffset );
// Check for special VSE TCP/IP stack command packet
if( sOffset == 0 && sCount == 40 )
{
// Extract the 32-byte stack identity string
for( i = 0;
i < sizeof( szStackID ) - 1 && i < sCount - 4;
i++)
szStackID[i] = guest_to_host( pIOBuf[i+4] );
szStackID[i] = '\0';
// Extract the stack command word
FETCH_FW( iStackCmd, *((FWORD*)&pIOBuf[36]) );
// Display stack command and discard the packet
WRMSG(HHC00907, "I", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, szStackID, iStackCmd );
*pUnitStat = CSW_CE | CSW_DE;
*pResidual = 0;
return;
}
// Check for special L/390 initialization packet
if( sOffset == 0 )
{
// Return normal status and discard the packet
*pUnitStat = CSW_CE | CSW_DE;
*pResidual = 0;
return;
}
#if 0
// Notes: It appears that TurboLinux has gotten sloppy in their
// ways. They are now giving us buffer sizes that are
// greater than the CCW count, but the segment size
// is within the count.
// Check that the frame offset is valid
if( sOffset < sizeof( CTCIHDR ) || sOffset > sCount )
{
logmsg( _("CTC101W %4.4X: Write buffer contains invalid "
"frame offset %u\n"),
pDEVBLK->devnum, sOffset );
pDEVBLK->sense[0] = SENSE_CR;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
#endif
// Adjust the residual byte count
*pResidual -= sizeof( CTCIHDR );
// Process each segment in the buffer
for( iPos = sizeof( CTCIHDR );
iPos < sOffset;
iPos += sSegLen )
{
// Check that the segment is fully contained within the block
if( iPos + sizeof( CTCISEG ) > sOffset )
{
WRMSG(HHC00908, "E", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, iPos );
pDEVBLK->sense[0] = SENSE_DC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Fix-up segment header in the I/O buffer
pSegment = (PCTCISEG)(pIOBuf + iPos);
// Extract the segment length from the segment header
FETCH_HW( sSegLen, pSegment->hwLength );
// Check that the segment length is valid
if( ( sSegLen < sizeof( CTCISEG ) ) ||
( (U32)iPos + sSegLen > sOffset ) ||
( (U32)iPos + sSegLen > sCount ) )
{
WRMSG(HHC00909, "E", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, sSegLen, iPos );
pDEVBLK->sense[0] = SENSE_DC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Calculate length of IP frame data
sDataLen = sSegLen - sizeof( CTCISEG );
// Trace the IP packet before sending
if( pDEVBLK->ccwtrace || pDEVBLK->ccwstep )
{
WRMSG(HHC00934, "I", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, pDEVBLK->filename );
if( pDEVBLK->ccwtrace )
packet_trace( pSegment->bData, sDataLen, '>' );
}
// Write the IP packet
rc = write_socket( pDEVBLK->fd, pSegment->bData, sDataLen );
if( rc < 0 )
{
WRMSG(HHC00936, "E", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, pDEVBLK->filename,
strerror( HSO_errno ) );
pDEVBLK->sense[0] = SENSE_EC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Adjust the residual byte count
*pResidual -= sSegLen;
// We are done if current segment satisfies CCW count
if( (U32)iPos + sSegLen == sCount )
{
*pResidual -= sSegLen;
*pUnitStat = CSW_CE | CSW_DE;
return;
}
}
// Set unit status and residual byte count
*pUnitStat = CSW_CE | CSW_DE;
*pResidual = 0;
}
//
// CTCT_Read
//
static void CTCT_Read( DEVBLK* pDEVBLK, U32 sCount,
BYTE* pIOBuf, BYTE* pUnitStat,
U32* pResidual, BYTE* pMore )
{
PCTCIHDR pFrame = NULL; // -> Frame header
PCTCISEG pSegment = NULL; // -> Segment in buffer
fd_set rfds; // Read FD_SET
int iRetVal; // Return code from 'select'
int iLength = 0;
static struct timeval tv; // Timeout time for 'select'
// Limit how long we should wait for data to come in
FD_ZERO( &rfds );
FD_SET( pDEVBLK->fd, &rfds );
tv.tv_sec = CTC_READ_TIMEOUT_SECS;
tv.tv_usec = 0;
iRetVal = select( pDEVBLK->fd + 1, &rfds, NULL, NULL, &tv );
switch( iRetVal )
{
case 0:
*pUnitStat = CSW_CE | CSW_DE | CSW_UC | CSW_SM;
pDEVBLK->sense[0] = 0;
return;
case -1:
if( HSO_errno == HSO_EINTR )
return;
WRMSG(HHC00973, "E", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, pDEVBLK->filename,
strerror( HSO_errno ) );
pDEVBLK->sense[0] = SENSE_EC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
default:
break;
}
// Read an IP packet from the TUN device
iLength = read_socket( pDEVBLK->fd, pDEVBLK->buf, pDEVBLK->bufsize );
// Check for other error condition
if( iLength < 0 )
{
WRMSG(HHC00973, "E", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, pDEVBLK->filename,
strerror( HSO_errno ) );
pDEVBLK->sense[0] = SENSE_EC;
*pUnitStat = CSW_CE | CSW_DE | CSW_UC;
return;
}
// Trace the packet received from the TUN device
if( pDEVBLK->ccwtrace || pDEVBLK->ccwstep )
{
WRMSG(HHC00913, "I", SSID_TO_LCSS(pDEVBLK->ssid), pDEVBLK->devnum, iLength, "TUN" );
packet_trace( pDEVBLK->buf, iLength, '<' );
}
// Fix-up Frame pointer
pFrame = (PCTCIHDR)pIOBuf;
// Fix-up Segment pointer
pSegment = (PCTCISEG)( pIOBuf + sizeof( CTCIHDR ) );
// Initialize segment
memset( pSegment, 0, iLength + sizeof( CTCISEG ) );
// Update next frame offset
STORE_HW( pFrame->hwOffset,
(U16)(iLength + sizeof( CTCIHDR ) + sizeof( CTCISEG )) );
// Store segment length
STORE_HW( pSegment->hwLength, (U16)(iLength + sizeof( CTCISEG )) );
// Store Frame type
STORE_HW( pSegment->hwType, ETH_TYPE_IP );
// Copy data
memcpy( pSegment->bData, pDEVBLK->buf, iLength );
// Fix-up frame pointer and terminate block
pFrame = (PCTCIHDR)( pIOBuf + sizeof( CTCIHDR ) +
sizeof( CTCISEG ) + iLength );
STORE_HW( pFrame->hwOffset, 0x0000 );
// Calculate #of bytes returned including two slack bytes
iLength += sizeof( CTCIHDR ) + sizeof( CTCISEG ) + 2;
if( sCount < (U32)iLength )
{
*pMore = 1;
*pResidual = 0;
iLength = sCount;
}
else
{
*pMore = 0;
*pResidual -= iLength;
}
// Set unit status
*pUnitStat = CSW_CE | CSW_DE;
}
//
// CTCT_ListenThread
//
static void* CTCT_ListenThread( void* argp )
{
int connfd;
socklen_t servlen;
char str[80];
CTCG_PARMBLK parm;
// set up the parameters passed via create_thread
parm = *((CTCG_PARMBLK*) argp);
free( argp );
for( ; ; )
{
servlen = sizeof(parm.addr);
// await a connection
connfd = accept( parm.listenfd,
(struct sockaddr *)&parm.addr,
&servlen );
MSGBUF( str, "%s:%d",
inet_ntoa( parm.addr.sin_addr ),
ntohs( parm.addr.sin_port ) );
if( strcmp( str, parm.dev->filename ) != 0 )
{
WRMSG(HHC00974, "E", SSID_TO_LCSS(parm.dev->ssid), parm.dev->devnum,
parm.dev->filename, str);
close_socket( connfd );
}
else
{
parm.dev->fd = connfd;
}
// Ok, so having done that we're going to loop back to the
// accept(). This was meant to handle the connection failing
// at the other end; this end will be ready to accept another
// connection. Although this will happen, I'm sure you can