forked from SDL-Hercules-390/hyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commadpt.c
2609 lines (2482 loc) · 98.6 KB
/
commadpt.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
/*-------------------------------------------------------------------*/
/* Hercules Communication Line Driver */
/* (c) 1999-2006 Roger Bowler & Others */
/* Use of this program is governed by the QPL License */
/* Original Author : Ivan Warren */
/* Prime Maintainer : Ivan Warren */
/*-------------------------------------------------------------------*/
// $Id$
//
// $Log$
// Revision 1.46 2008/11/03 15:31:58 rbowler
// Back out consistent create_thread ATTR modification
//
// Revision 1.45 2008/10/18 09:32:20 fish
// Ensure consistent create_thread ATTR usage
//
// Revision 1.44 2008/03/04 01:10:29 ivan
// Add LEGACYSENSEID config statement to allow X'E4' Sense ID on devices
// that originally didn't support it. Defaults to off for compatibility reasons
//
// Revision 1.43 2008/02/15 22:51:39 rbowler
// Move Solaris specific definition of INADDR_NONE to hostopts.h
//
// Revision 1.42 2008/02/07 00:29:04 rbowler
// Solaris build support by Jeff Savit
//
// Revision 1.41 2007/12/14 17:48:52 rbowler
// Enable SENSE ID CCW for 2703,3410,3420
//
// Revision 1.40 2007/11/21 22:54:14 fish
// Use new BEGIN_DEVICE_CLASS_QUERY macro
//
// Revision 1.39 2006/12/08 09:43:18 jj
// Add CVS message log
//
#include "hstdinc.h"
#include "hercules.h"
#include "devtype.h"
#include "parser.h"
#include "commadpt.h"
#if defined(WIN32) && defined(OPTION_DYNAMIC_LOAD) && !defined(HDL_USE_LIBTOOL) && !defined(_MSVC_)
SYSBLK *psysblk;
#define sysblk (*psysblk)
#endif
/*-------------------------------------------------------------------*/
/* Ivan Warren 20040227 */
/* This table is used by channel.c to determine if a CCW code is an */
/* immediate command or not */
/* The tape is addressed in the DEVHND structure as 'DEVIMM immed' */
/* 0 : Command is NOT an immediate command */
/* 1 : Command is an immediate command */
/* Note : An immediate command is defined as a command which returns */
/* CE (channel end) during initialisation (that is, no data is */
/* actually transfered. In this case, IL is not indicated for a CCW */
/* Format 0 or for a CCW Format 1 when IL Suppression Mode is in */
/* effect */
/*-------------------------------------------------------------------*/
static BYTE commadpt_immed_command[256]=
{ 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
COMMADPT_PEND_TEXT; /* Defined in commadpt.h */
/* Defines commadpt_pendccw_text array */
/*---------------------------------------------------------------*/
/* PARSER TABLES */
/*---------------------------------------------------------------*/
static PARSER ptab[]={
{"lport","%s"},
{"lhost","%s"},
{"rport","%s"},
{"rhost","%s"},
{"dial","%s"},
{"rto","%s"},
{"pto","%s"},
{"eto","%s"},
{"switched","%s"},
{NULL,NULL}
};
enum {
COMMADPT_KW_LPORT=1,
COMMADPT_KW_LHOST,
COMMADPT_KW_RPORT,
COMMADPT_KW_RHOST,
COMMADPT_KW_DIAL,
COMMADPT_KW_READTO,
COMMADPT_KW_POLLTO,
COMMADPT_KW_ENABLETO,
COMMADPT_KW_SWITCHED
} commadpt_kw;
static void logdump(char *txt,DEVBLK *dev,BYTE *bfr,size_t sz)
{
size_t i;
if(!dev->ccwtrace)
{
return;
}
logmsg(_("HHCCA300D %4.4X:%s : Status = TEXT=%s, TRANS=%s, TWS=%s\n"),
dev->devnum,
txt,
dev->commadpt->in_textmode?"YES":"NO",
dev->commadpt->in_xparmode?"YES":"NO",
dev->commadpt->xparwwait?"YES":"NO");
logmsg(_("HHCCA300D %4.4X:%s : Dump of %d (%x) byte(s)\n"),dev->devnum,txt,sz,sz);
for(i=0;i<sz;i++)
{
if(i%16==0)
{
if(i!=0)
{
logmsg("\n");
}
logmsg(_("HHCCA300D %4.4X:%s : %4.4X:"),dev->devnum,txt,i);
}
if(i%4==0)
{
logmsg(" ");
}
logmsg("%2.2X",bfr[i]);
}
logmsg("\n");
}
/*-------------------------------------------------------------------*/
/* Handler utility routines */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Buffer ring management */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Buffer ring management : Init a buffer ring */
/*-------------------------------------------------------------------*/
void commadpt_ring_init(COMMADPT_RING *ring,size_t sz,int trace)
{
ring->bfr=malloc(sz);
ring->sz=sz;
ring->hi=0;
ring->lo=0;
ring->havedata=0;
ring->overflow=0;
if(trace)
{
logmsg("HCCCA999D : Ring buffer for ring %p allocated at %p\n",
ring,
ring->bfr);
}
}
/*-------------------------------------------------------------------*/
/* Buffer ring management : Free a buffer ring */
/*-------------------------------------------------------------------*/
static void commadpt_ring_terminate(COMMADPT_RING *ring,int trace)
{
if(trace)
{
logmsg("HCCCA999D : Ring buffer for ring %p at %p freed\n",
ring,
ring->bfr);
}
if(ring->bfr!=NULL)
{
free(ring->bfr);
ring->bfr=NULL;
}
ring->sz=0;
ring->hi=0;
ring->lo=0;
ring->havedata=0;
ring->overflow=0;
}
/*-------------------------------------------------------------------*/
/* Buffer ring management : Flush a buffer ring */
/*-------------------------------------------------------------------*/
static void commadpt_ring_flush(COMMADPT_RING *ring)
{
ring->hi=0;
ring->lo=0;
ring->havedata=0;
ring->overflow=0;
}
/*-------------------------------------------------------------------*/
/* Buffer ring management : Queue a byte in the ring */
/*-------------------------------------------------------------------*/
inline static void commadpt_ring_push(COMMADPT_RING *ring,BYTE b)
{
ring->bfr[ring->hi++]=b;
if(ring->hi>=ring->sz)
{
ring->hi=0;
}
if(ring->hi==ring->lo)
{
ring->overflow=1;
}
ring->havedata=1;
}
/*-------------------------------------------------------------------*/
/* Buffer ring management : Queue a byte array in the ring */
/*-------------------------------------------------------------------*/
inline static void commadpt_ring_pushbfr(COMMADPT_RING *ring,BYTE *b,size_t sz)
{
size_t i;
for(i=0;i<sz;i++)
{
commadpt_ring_push(ring,b[i]);
}
}
/*-------------------------------------------------------------------*/
/* Buffer ring management : Retrieve a byte from the ring */
/*-------------------------------------------------------------------*/
inline static BYTE commadpt_ring_pop(COMMADPT_RING *ring)
{
register BYTE b;
b=ring->bfr[ring->lo++];
if(ring->lo>=ring->sz)
{
ring->lo=0;
}
if(ring->hi==ring->lo)
{
ring->havedata=0;
}
return b;
}
/*-------------------------------------------------------------------*/
/* Buffer ring management : Retrive a byte array from the ring */
/*-------------------------------------------------------------------*/
inline static size_t commadpt_ring_popbfr(COMMADPT_RING *ring,BYTE *b,size_t sz)
{
size_t i;
for(i=0;i<sz && ring->havedata;i++)
{
b[i]=commadpt_ring_pop(ring);
}
return i;
}
/*-------------------------------------------------------------------*/
/* Free all private structures and buffers */
/*-------------------------------------------------------------------*/
static void commadpt_clean_device(DEVBLK *dev)
{
if(!dev)
{
/*
* Shouldn't happen.. But during shutdown, some weird
* things happen !
*/
return;
}
if(dev->commadpt!=NULL)
{
commadpt_ring_terminate(&dev->commadpt->inbfr,dev->ccwtrace);
commadpt_ring_terminate(&dev->commadpt->outbfr,dev->ccwtrace);
commadpt_ring_terminate(&dev->commadpt->rdwrk,dev->ccwtrace);
commadpt_ring_terminate(&dev->commadpt->pollbfr,dev->ccwtrace);
/* release the CA lock */
release_lock(&dev->commadpt->lock);
free(dev->commadpt);
dev->commadpt=NULL;
if(dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4X:clean : Control block freed\n"),
dev->devnum);
}
}
else
{
if(dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4X:clean : Control block not freed : not allocated\n"),dev->devnum);
}
}
return;
}
/*-------------------------------------------------------------------*/
/* Allocate initial private structures */
/*-------------------------------------------------------------------*/
static int commadpt_alloc_device(DEVBLK *dev)
{
dev->commadpt=malloc(sizeof(COMMADPT));
if(dev->commadpt==NULL)
{
logmsg(_("HHCCA020E %4.4X:Memory allocation failure for main control block\n"),
dev->devnum);
return -1;
}
memset(dev->commadpt,0,sizeof(COMMADPT));
commadpt_ring_init(&dev->commadpt->inbfr,4096,dev->ccwtrace);
commadpt_ring_init(&dev->commadpt->outbfr,4096,dev->ccwtrace);
commadpt_ring_init(&dev->commadpt->pollbfr,4096,dev->ccwtrace);
commadpt_ring_init(&dev->commadpt->rdwrk,65536,dev->ccwtrace);
dev->commadpt->dev=dev;
return 0;
}
/*-------------------------------------------------------------------*/
/* Parsing utilities */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* commadpt_getport : returns a port number or -1 */
/*-------------------------------------------------------------------*/
static int commadpt_getport(char *txt)
{
int pno;
struct servent *se;
pno=atoi(txt);
if(pno==0)
{
se=getservbyname(txt,"tcp");
if(se==NULL)
{
return -1;
}
pno=se->s_port;
}
return(pno);
}
/*-------------------------------------------------------------------*/
/* commadpt_getaddr : set an in_addr_t if ok, else return -1 */
/*-------------------------------------------------------------------*/
static int commadpt_getaddr(in_addr_t *ia,char *txt)
{
struct hostent *he;
he=gethostbyname(txt);
if(he==NULL)
{
return(-1);
}
memcpy(ia,he->h_addr_list[0],4);
return(0);
}
/*-------------------------------------------------------------------*/
/* commadpt_connout : make a tcp outgoing call */
/* return values : 0 -> call succeeded or initiated */
/* <0 -> call failed */
/*-------------------------------------------------------------------*/
static int commadpt_connout(COMMADPT *ca)
{
int rc;
char wbfr[256];
struct sockaddr_in sin;
struct in_addr intmp;
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=ca->rhost;
sin.sin_port=htons(ca->rport);
if(socket_is_socket(ca->sfd))
{
close_socket(ca->sfd);
ca->connect=0;
}
ca->sfd=socket(AF_INET,SOCK_STREAM,0);
/* set socket to NON-blocking mode */
socket_set_blocking_mode(ca->sfd,0);
rc=connect(ca->sfd,(struct sockaddr *)&sin,sizeof(sin));
if(rc<0)
{
if(HSO_errno==HSO_EINPROGRESS)
{
return(0);
}
else
{
strerror_r(HSO_errno,wbfr,256);
intmp.s_addr=ca->rhost;
logmsg(_("HHCCA001I %4.4X:Connect out to %s:%d failed during initial status : %s\n"),
ca->devnum,
inet_ntoa(intmp),
ca->rport,
wbfr);
close_socket(ca->sfd);
ca->connect=0;
return(-1);
}
}
ca->connect=1;
return(0);
}
/*-------------------------------------------------------------------*/
/* commadpt_initiate_userdial : interpret DIAL data and initiate call*/
/* return values : 0 -> call succeeded or initiated */
/* <0 -> call failed */
/*-------------------------------------------------------------------*/
static int commadpt_initiate_userdial(COMMADPT *ca)
{
int dotcount; /* Number of seps (the 4th is the port separator) */
int i; /* work */
int cur; /* Current section */
in_addr_t destip; /* Destination IP address */
U16 destport; /* Destination TCP port */
int incdata; /* Incorrect dial data found */
int goteon; /* EON presence flag */
/* See the DIAL CCW portion in execute_ccw for dial format information */
incdata=0;
goteon=0;
dotcount=0;
cur=0;
destip=0;
for(i=0;i<ca->dialcount;i++)
{
if(goteon)
{
/* EON MUST be last data byte */
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4x : Found data beyond EON\n"),ca->devnum);
}
incdata=1;
break;
}
switch(ca->dialdata[i]&0x0f)
{
case 0x0d: /* SEP */
if(dotcount<4)
{
if(cur>255)
{
incdata=1;
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4x : Found incorrect IP address section at position %d\n"),ca->devnum,dotcount+1);
logmsg(_("HHCCA300D %4.4x : %d greater than 255\n"),ca->devnum,cur);
}
break;
}
destip<<=8;
destip+=cur;
cur=0;
dotcount++;
}
else
{
incdata=1;
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4x : Too many separators in dial data\n"),ca->devnum);
}
break;
}
break;
case 0x0c: /* EON */
goteon=1;
break;
/* A,B,E,F not valid */
case 0x0a:
case 0x0b:
case 0x0e:
case 0x0f:
incdata=1;
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4x : Incorrect dial data byte %2.2x\n"),ca->devnum,ca->dialdata[i]);
}
break;
default:
cur*=10;
cur+=ca->dialdata[i]&0x0f;
break;
}
if(incdata)
{
break;
}
}
if(incdata)
{
return -1;
}
if(dotcount<4)
{
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4x : Not enough separators (only %d found) in dial data\n"),ca->devnum,dotcount);
}
return -1;
}
if(cur>65535)
{
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4x : Destination TCP port %d exceeds maximum of 65535\n"),ca->devnum,cur);
}
return -1;
}
destport=cur;
/* Update RHOST/RPORT */
ca->rport=destport;
ca->rhost=destip;
return(commadpt_connout(ca));
}
/*-------------------------------------------------------------------*/
/* Communication Thread - Read socket data (after POLL request */
/*-------------------------------------------------------------------*/
static int commadpt_read_poll(COMMADPT *ca)
{
BYTE b;
int rc;
while((rc=read_socket(ca->sfd,&b,1))>0)
{
if(b==0x32)
{
continue;
}
if(b==0x37)
{
return(1);
}
}
if(rc>0)
{
/* Store POLL IX in bfr followed by byte */
commadpt_ring_push(&ca->inbfr,ca->pollix);
commadpt_ring_push(&ca->inbfr,b);
return(2);
}
return(0);
}
/*-------------------------------------------------------------------*/
/* Communication Thread - Read socket data */
/*-------------------------------------------------------------------*/
static void commadpt_read(COMMADPT *ca)
{
BYTE bfr[256];
int gotdata;
int rc;
gotdata=0;
while((rc=read_socket(ca->sfd,bfr,256))>0)
{
logdump("RECV",ca->dev,bfr,rc);
commadpt_ring_pushbfr(&ca->inbfr,bfr,(size_t)rc);
gotdata=1;
}
if(!gotdata)
{
if(ca->connect)
{
ca->connect=0;
close_socket(ca->sfd);
ca->sfd=-1;
if(ca->curpending!=COMMADPT_PEND_IDLE)
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
}
}
}
}
/*-------------------------------------------------------------------*/
/* Communication Thread - Set TimeOut */
/*-------------------------------------------------------------------*/
static struct timeval *commadpt_setto(struct timeval *tv,int tmo)
{
if(tmo!=0)
{
if(tmo<0)
{
tv->tv_sec=0;
tv->tv_usec=1;
}
else
{
tv->tv_sec=tmo/1000;
tv->tv_usec=(tmo%1000)*1000;
}
return(tv);
}
return(NULL);
}
/*-------------------------------------------------------------------*/
/* Communication Thread main loop */
/*-------------------------------------------------------------------*/
static void *commadpt_thread(void *vca)
{
COMMADPT *ca; /* Work CA Control Block Pointer */
int sockopt; /* Used for setsocketoption */
struct sockaddr_in sin; /* bind socket address structure */
int devnum; /* device number copy for convenience*/
int rc; /* return code from various rtns */
struct timeval tv; /* select timeout structure */
struct timeval *seltv; /* ptr to the timeout structure */
fd_set rfd,wfd,xfd; /* SELECT File Descriptor Sets */
BYTE pipecom; /* Byte read from IPC pipe */
int tempfd; /* Temporary FileDesc holder */
BYTE b; /* Work data byte */
int writecont; /* Write contention active */
int soerr; /* getsockopt SOERROR value */
socklen_t soerrsz; /* Size for getsockopt */
int maxfd; /* highest FD for select */
int ca_shutdown; /* Thread shutdown internal flag */
int init_signaled; /* Thread initialisation signaled */
int pollact; /* A Poll Command is in progress */
int i; /* Ye Old Loop Counter */
int eintrcount=0; /* Number of times EINTR occured in */
/* a row.. Over 100 : Bail out ! */
/*---------------------END OF DECLARES---------------------------*/
/* fetch the commadpt structure */
ca=(COMMADPT *)vca;
/* Obtain the CA lock */
obtain_lock(&ca->lock);
/* get a work copy of devnum (for messages) */
devnum=ca->devnum;
/* reset shutdown flag */
ca_shutdown=0;
init_signaled=0;
logmsg(_("HHCCA002I %4.4X:Line Communication thread "TIDPAT" started\n"),devnum,thread_id());
pollact=0; /* Initialise Poll activity flag */
/* Determine if we should listen */
/* if this is a DIAL=OUT only line, no listen is necessary */
if(ca->dolisten)
{
/* Create the socket for a listen */
ca->lfd=socket(AF_INET,SOCK_STREAM,0);
if(!socket_is_socket(ca->lfd))
{
logmsg(_("HHCCA003E %4.4X:Cannot obtain socket for incoming calls : %s\n"),devnum,strerror(HSO_errno));
ca->have_cthread=0;
release_lock(&ca->lock);
return NULL;
}
/* Turn blocking I/O off */
/* set socket to NON-blocking mode */
socket_set_blocking_mode(ca->lfd,0);
/* Reuse the address regardless of any */
/* spurious connection on that port */
sockopt=1;
setsockopt(ca->lfd,SOL_SOCKET,SO_REUSEADDR,(GETSET_SOCKOPT_T*)&sockopt,sizeof(sockopt));
/* Bind the socket */
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=ca->lhost;
sin.sin_port=htons(ca->lport);
while(1)
{
rc=bind(ca->lfd,(struct sockaddr *)&sin,sizeof(sin));
if(rc<0)
{
if(HSO_errno==HSO_EADDRINUSE)
{
logmsg(_("HHCCA004W %4.4X:Waiting 5 seconds for port %d to become available\n"),devnum,ca->lport);
/*
* Check for a shutdown condition on entry
*/
if(ca->curpending==COMMADPT_PEND_SHUTDOWN)
{
ca_shutdown=1;
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
/* Set to wait 5 seconds or input on the IPC pipe */
/* whichever comes 1st */
if(!init_signaled)
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
init_signaled=1;
}
FD_ZERO(&rfd);
FD_ZERO(&wfd);
FD_ZERO(&xfd);
FD_SET(ca->pipe[1],&rfd);
tv.tv_sec=5;
tv.tv_usec=0;
release_lock(&ca->lock);
rc=select(ca->pipe[1]+1,&rfd,&wfd,&wfd,&tv);
obtain_lock(&ca->lock);
/*
* Check for a shutdown condition again after the sleep
*/
if(ca->curpending==COMMADPT_PEND_SHUTDOWN)
{
ca_shutdown=1;
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
if(rc!=0)
{
/* Ignore any other command at this stage */
read_pipe(ca->pipe[1],&b,1);
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
}
}
else
{
logmsg(_("HHCCA018E %4.4X:Bind failed : %s\n"),devnum,strerror(HSO_errno));
ca_shutdown=1;
break;
}
}
else
{
break;
}
}
/* Start the listen */
if(!ca_shutdown)
{
listen(ca->lfd,10);
logmsg(_("HHCCA005I %4.4X:Listening on port %d for incoming TCP connections\n"),
devnum,
ca->lport);
ca->listening=1;
}
}
if(!init_signaled)
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
init_signaled=1;
}
/* The MAIN select loop */
/* It will listen on the following sockets : */
/* ca->lfd : The listen socket */
/* ca->sfd :
* read : When a read, prepare or DIAL command is in effect
* write : When a write contention occurs
* ca->pipe[0] : Always
*
* A 3 Seconds timer is started for a read operation
*/
while(!ca_shutdown)
{
FD_ZERO(&rfd);
FD_ZERO(&wfd);
FD_ZERO(&xfd);
maxfd=0;
if(ca->listening)
{
FD_SET(ca->lfd,&rfd);
maxfd=maxfd<ca->lfd?ca->lfd:maxfd;
}
seltv=NULL;
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4X:cthread - Entry - DevExec = %s\n"),devnum,commadpt_pendccw_text[ca->curpending]);
}
writecont=0;
switch(ca->curpending)
{
case COMMADPT_PEND_SHUTDOWN:
ca_shutdown=1;
break;
case COMMADPT_PEND_IDLE:
break;
case COMMADPT_PEND_READ:
if(!ca->connect)
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
if(ca->inbfr.havedata)
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
seltv=commadpt_setto(&tv,ca->rto);
FD_SET(ca->sfd,&rfd);
maxfd=maxfd<ca->sfd?ca->sfd:maxfd;
break;
case COMMADPT_PEND_POLL:
/* Poll active check - provision for write contention */
/* pollact will be reset when NON syn data is received*/
/* or when the read times out */
/* Also prevents WRITE from exiting early */
if(!pollact && !writecont)
{
int gotenq;
pollact=1;
gotenq=0;
/* Send SYN+SYN */
commadpt_ring_push(&ca->outbfr,0x32);
commadpt_ring_push(&ca->outbfr,0x32);
/* Fill the Output ring with POLL Data */
/* Up to 7 chars or ENQ */
for(i=0;i<7;i++)
{
if(!ca->pollbfr.havedata)
{
break;
}
ca->pollused++;
b=commadpt_ring_pop(&ca->pollbfr);
if(b!=0x2D)
{
commadpt_ring_push(&ca->outbfr,b);
}
else
{
gotenq=1;
break;
}
}
if(!gotenq)
{
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4X:Poll Command abort - Poll address >7 Bytes\n"),devnum);
}
ca->badpoll=1;
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
b=commadpt_ring_pop(&ca->pollbfr);
ca->pollix=b;
seltv=commadpt_setto(&tv,ca->pto);
}
if(!writecont && ca->pto!=0)
{
/* Set tv value (have been set earlier) */
seltv=&tv;
/* Set to read data still */
FD_SET(ca->sfd,&rfd);
maxfd=maxfd<ca->sfd?ca->sfd:maxfd;
}
/* DO NOT BREAK - Continue with WRITE processing */
case COMMADPT_PEND_WRITE:
if(!writecont)
{
while(ca->outbfr.havedata)
{
b=commadpt_ring_pop(&ca->outbfr);
if(ca->dev->ccwtrace)
{
logmsg(_("HHCCA300D %4.4X:Writing 1 byte in socket : %2.2X\n"),ca->devnum,b);
}
rc=write_socket(ca->sfd,&b,1);
if(rc!=1)
{
if(0
#ifndef WIN32
|| EAGAIN == errno
#endif
|| HSO_EWOULDBLOCK == HSO_errno
)
{
/* Contending for write */
writecont=1;
FD_SET(ca->sfd,&wfd);
maxfd=maxfd<ca->sfd?ca->sfd:maxfd;
break;
}
else
{
close_socket(ca->sfd);
ca->sfd=-1;
ca->connect=0;
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
}
}
}
else
{
FD_SET(ca->sfd,&wfd);
maxfd=maxfd<ca->sfd?ca->sfd:maxfd;
}
if(!writecont && !pollact)
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
break;
case COMMADPT_PEND_DIAL:
if(ca->connect)
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
rc=commadpt_initiate_userdial(ca);
if(rc!=0 || (rc==0 && ca->connect))
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
FD_SET(ca->sfd,&wfd);
maxfd=maxfd<ca->sfd?ca->sfd:maxfd;
break;
case COMMADPT_PEND_ENABLE:
if(ca->connect)
{
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
switch(ca->dialin+ca->dialout*2)
{
case 0: /* DIAL=NO */
/* callissued is set here when the call */
/* actually failed. But we want to time */
/* a bit for program issuing ENABLES in */
/* a tight loop */
if(ca->callissued)
{
seltv=commadpt_setto(&tv,ca->eto);
break;
}
/* Issue a Connect out */
rc=commadpt_connout(ca);
if(rc==0)
{
/* Call issued */
if(ca->connect)
{
/* Call completed already */
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
}
else
{
/* Call initiated - FD will be ready */
/* for writing when the connect ends */
/* getsockopt/SOERROR will tell if */
/* the call was sucessfull or not */
FD_SET(ca->sfd,&wfd);
maxfd=maxfd<ca->sfd?ca->sfd:maxfd;
ca->callissued=1;
}
}
/* Call did not succeed */
/* Manual says : on a leased line, if DSR is not up */
/* the terminate enable after a timeout.. That is */
/* what the call just did (although the time out */
/* was probably instantaneous) */
/* This is the equivalent of the comm equipment */
/* being offline */
/* INITIATE A 3 SECOND TIMEOUT */
/* to prevent OSes from issuing a loop of ENABLES */
else
{
seltv=commadpt_setto(&tv,ca->eto);
}
break;
default:
case 3: /* DIAL=INOUT */
case 1: /* DIAL=IN */
/* Wait forever */
break;
case 2: /* DIAL=OUT */
/* Makes no sense */
/* line must be enabled through a DIAL command */
ca->curpending=COMMADPT_PEND_IDLE;
signal_condition(&ca->ipc);
break;
}
/* For cases not DIAL=OUT, the listen is already started */
break;
/* The CCW Executor says : DISABLE */