-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnpu_nje.c
2603 lines (2413 loc) · 84.2 KB
/
npu_nje.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
/*--------------------------------------------------------------------------
**
** Copyright (c) 2019, Kevin Jordan, Tom Hunter
**
** Name: npu_nje.c
**
** Description:
** Perform emulation of the NJE TIP in an NPU consisting of a
** CDC 2550 HCP running CCP. The NJE TIP is used by the NJF
** application. NJF enables a NOS system to exchange batch jobs
** and output files with other computer systems implementing
** the IBM NJE protocol, typically IBM mainframes running
** MVS with JES2/JES3 or CMS with RSCS.
**
** On the network-facing side, this TIP implements the NJE/TCP
** protocol, as specified in IBM'S specification, "Network Job
** Entry (NJE) Formats and Protocols", available here:
**
** https://www-01.ibm.com/servers/resourcelink/svc00100.nsf/pages/zosv2r3sa320988/$file/hasa600_v2r3.pdf
**
** and the original VMNET protocol definition, available here:
**
** http://www.nic.funet.fi/pub/netinfo/CREN/brfc0002.text
**
** This NJE/TCP implementation is fully compatible and interoperable
** with implementations in the Hercules IBM mainframe emulator
** as well as Funetnje (an NJE/TCP implementation for Unix/Linux)
** and JANET (an NJE/TCP implementation for VAX/VMS).
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
#define DEBUG 0
/*
** -------------
** Include Files
** -------------
*/
#if defined(_WIN32)
#include <Windows.h>
#else
#include <sys/time.h>
#endif
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include "const.h"
#include "types.h"
#include "proto.h"
#include "npu.h"
#if defined(__APPLE__)
#include <execinfo.h>
#endif
/*
** -----------------
** Private Constants
** -----------------
*/
#define MaxRetries 8
#define MaxUplineBlockSize 640
#define MaxWaitTime 15
/*
** Special ASCII characters used by NAM protocol
*/
#define US 0x1f
/*
** Special EBCDIC characters used by NJE protocol
*/
#define SOH 0x01
#define STX 0x02
#define DLE 0x10
#define ENQ 0x2d
#define SYN 0x32
#define NAK 0x3d
#define ACK0 0x70
#define EbcdicBlank 0x40
/*
** NJE Record Control Block (RCB) codes
*/
#define RCB_RTI 0x90
#define RCB_PTI 0xa0
#define RCB_Deny 0xb0
#define RCB_TransComplete 0xc0
#define RCB_RTR 0xd0
#define RCB_SeqErr 0xe0
#define RCB_GCR 0xf0
#define RCB_NJFTIPCommand 0xff
/*
** RCB offsets (from IBM "Network Job Entry (NJE) Formats and Protocols")
*/
#define NCCRCB 0x00
#define NCCSRCB 0x01
#define NCCIDL 0x02
#define NCCINODE 0x03
#define NCCIQUAL 0x0b
#define NCCIEVNT 0x0c
#define NCCIREST 0x10
#define NCCIBFSZ 0x12
#define NCCILPAS 0x14
#define NCCIMPAS 0x1c
#define NCCIFLG 0x24
/*
** NJE Secondary Record Control Block (SRCB) codes
*/
#define SRCB_Signoff 0xc2
#define SRCB_InitialSignon 0xc9
#define SRCB_RespSignon 0xd1
#define SRCB_ResetSignon 0xd2
#define SRCB_AcceptSignon 0xd3
#define SRCB_AddConnection 0xd4
#define SRCB_DeleteConnection 0xd5
#define SRCB_CMDXBZ 0x00 // NJF TIP command: Set Transmission Block Size
#define SRCB_CMDABT 0x01 // NJF TIP command: Abort Transmitter
/*
** Internal NJE status and error codes
*/
#define NjeStatusSYN_NAK 4
#define NjeStatusDLE_ACK0 3
#define NjeStatusSOH_ENQ 2
#define NjeStatusNothingUploaded 1
#define NjeStatusOk 0
#define NjeErrBlockTooShort -1
#define NjeErrBlockTooLong -2
#define NjeErrBadLeader -3
#define NjeErrBadBCB -4
#define NjeErrBadBSN -5
#define NjeErrBadFCS -6
#define NjeErrBadRCB -7
#define NjeErrBadSRCB -8
#define NjeErrBadSCB -9
#define NjeErrRecordTooLong -10
#define NjeErrTooManyRetries -11
#define NjeErrProtocolError -12
/*
** NJE/TCP Data Block Header length and offsets
*/
#define TtbLength 8
#define TtbOffFlags 0
#define TtbOffLength 2
/*
** NJE/TCP Data Block Record Header length and offsets
*/
#define TtrLength 4
#define TtrOffFlags 0
#define TtrOffLength 2
/*
** NJE/TCP Control Record length and offsets
*/
#define CrLength 33
#define CrOffType 0
#define CrOffRHost 8
#define CrOffRIP 16
#define CrOffOHost 20
#define CrOffOIP 28
#define CrOffR 32
/*
** NJE/TCP Control Record NAK reason codes
*/
#define CrNakNoSuchLink 0x01
#define CrNakLinkActive 0x02
#define CrNakAttemptingActiveOpen 0x03
#define CrNakTemporaryFailure 0x04
#if DEBUG
static char *NjeConnStates[] = {
"StNjeDisconnected",
"StNjeRcvOpen",
"StNjeRcvSOH_ENQ",
"StNjeSndOpen",
"StNjeRcvAck",
"StNjeRcvSignon",
"StNjeRcvResponseSignon",
"StNjeExchangeData"
};
static char *CrNakReasons[] =
{
"",
"No such link",
"Link active",
"Link attempting active open",
"Temporary failure"
};
#endif
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
#if DEBUG
#define HexColumn(x) (3 * (x) + 4)
#define AsciiColumn(x) (HexColumn(16) + 2 + (x))
#define LogLineLength (AsciiColumn(16))
#endif
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
#if DEBUG
typedef enum
{
ASCII = 0,
EBCDIC,
DisplayCode
} CharEncoding;
#endif
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static u8 *npuNjeAppendLeader(Pcb *pcbp, u8 *bp);
static int npuNjeAppendRecords(Pcb *pcbp, u8 *bp, int len, u8 blockType);
static void npuNjeAsciiToEbcdic(u8 *ascii, u8 *ebcdic, int len);
static void npuNjeCloseConnection(Pcb *pcbp);
static u8 *npuNjeCloseDownlineBlock(Pcb *pcbp, u8 *dp);
static u8 *npuNjeCollectBlock(Pcb *pcbp, u8 *start, u8 *limit, bool *isComplete, int *size, int *status);
static bool npuNjeConnectTerminal(Pcb *pcbp);
static void npuNjeEbcdicToAscii(u8 *ebcdic, u8 *ascii, int len);
static Pcb *npuNjeFindPcbForCr(char *rhost, u32 rip, char *ohost, u32 oip);
static Tcb *npuNjeFindTcb(Pcb *pcbp);
static void npuNjeFlushOutput(Pcb *pcbp);
static void npuNjeParseControlRecord(u8 *crp, char *rhost, u32 *rip, char *ohost, u32 *oip, u8 *r);
static void npuNjePrepareOutput(Pcb *pcbp);
static int npuNjeSend(Pcb *pcbp, u8 *dp, int len);
static bool npuNjeSendControlRecord(Pcb *pcbp, u8 *crp, char *localName, u32 localIP, char *peerName, u32 peerIP, u8 r);
static void npuNjeSendUplineBlock(Pcb *pcbp, NpuBuffer *bp, u8 *dp, u8 blockType);
static void npuNjeSetTtrLength(Pcb *pcbp);
static void npuNjeTransmitQueuedBlocks(Pcb *pcbp);
static void npuNjeTrim(char *str);
static int npuNjeUploadBlock(Pcb *pcbp, u8 *blkp, int size, u8 *rcb, u8 *srcb);
#if DEBUG
static void npuNjeLogBytes(u8 *bytes, int len, CharEncoding encoding);
static void npuNjeLogFlush(void);
static void npuNjePrintStackTrace(FILE *fp);
#endif
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
#if DEBUG
static FILE *npuNjeLog = NULL;
static char npuNjeLogBuf[LogLineLength + 1];
static int npuNjeLogBytesCol = 0;
#endif
/*
** NJE/TCP Control Record Type strings (EBCDIC)
*/
static u8 CrTypeAck[8] = { 0xc1, 0xc3, 0xd2, 0x40, 0x40, 0x40, 0x40, 0x40 }; // 'ACK '
static u8 CrTypeOpen[8] = { 0xd6, 0xd7, 0xc5, 0xd5, 0x40, 0x40, 0x40, 0x40 }; // 'OPEN '
static u8 CrTypeNak[8] = { 0xd5, 0xc1, 0xd2, 0x40, 0x40, 0x40, 0x40, 0x40 }; // 'NAK '
/*
** Special NJE/TCP blocks
*/
static u8 DLE_ACK0[] =
{
0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, // TTB
0x00, 0x00, 0x00, 0x02, // TTR
DLE, ACK0, // data
0x00, 0x00, 0x00, 0x00 // TTREOB
};
static u8 EmptyBlock[] =
{
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, // TTB
0x00, 0x00, 0x00, 0x00 // TTR
};
static u8 SOH_ENQ[] =
{
0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, // TTB
0x00, 0x00, 0x00, 0x02, // TTR
SOH, ENQ, // data
0x00, 0x00, 0x00, 0x00 // TTREOB
};
static u8 SYN_NAK[] =
{
0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, // TTB
0x00, 0x00, 0x00, 0x02, // TTR
SYN, NAK, // data
0x00, 0x00, 0x00, 0x00 // TTREOB
};
/*
** Special non-transparent NAM messages
*/
static char *ApplicationFailed = "APPLICATION FAILED.";
#define ApplicationFailedLen 19
static char *ApplicationNotPresent = "APPLICATION NOT PRESENT.";
#define ApplicationNotPresentLen 24
static char *ApplicationBusy = "APPLICATION BUSY";
#define ApplicationBusyLen 16
static char *LoggedOut = "LOGGED OUT.";
#define LoggedOutLen 11
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Try to send any queued data.
**
** Parameters: Name Description.
** pcbp PCB pointer
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNjeTryOutput(Pcb *pcbp)
{
NpuBuffer *bp;
time_t currentTime;
int n;
Tcb *tcbp;
currentTime = getSeconds();
tcbp = npuNjeFindTcb(pcbp);
switch (pcbp->controls.nje.state)
{
case StNjeRcvOpen:
case StNjeRcvSOH_ENQ:
case StNjeRcvAck:
case StNjeRcvSignon:
case StNjeRcvResponseSignon:
if (currentTime - pcbp->controls.nje.lastXmit > MaxWaitTime)
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: timeout in state %s\n", pcbp->claPort, NjeConnStates[pcbp->controls.nje.state]);
#endif
npuNjeCloseConnection(pcbp);
return;
}
break;
case StNjeExchangeData:
if ((currentTime - pcbp->controls.nje.lastXmit > pcbp->controls.nje.pingInterval)
&& (pcbp->controls.nje.pingInterval > 0))
{
if ((tcbp != NULL) && (npuBipQueueNotEmpty(&tcbp->outputQ) == FALSE))
{
npuNetSend(tcbp, EmptyBlock, sizeof(EmptyBlock));
}
}
break;
case StNjeSndOpen:
if (npuNjeSendControlRecord(pcbp, CrTypeOpen, npuNetHostID, pcbp->controls.nje.localIP,
pcbp->ncbp->hostName, pcbp->controls.nje.remoteIP, 0))
{
pcbp->controls.nje.state = StNjeRcvAck;
}
break;
}
if (tcbp != NULL)
{
while ((bp = npuBipQueueExtract(&tcbp->outputQ)) != NULL)
{
n = npuNjeSend(pcbp, bp->data + bp->offset, bp->numBytes - bp->offset);
if (n > 0)
{
bp->offset += n;
}
if (bp->offset >= bp->numBytes)
{
npuBipBufRelease(bp);
}
else
{
npuBipQueuePrepend(bp, &tcbp->outputQ);
break;
}
}
if (tcbp->state == StTermConnected)
{
npuNjeTransmitQueuedBlocks(pcbp);
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Process downline data from host.
**
** Parameters: Name Description.
** tcbp pointer to TCB
** bp buffer with downline data message.
** last last buffer.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNjeProcessDownlineData(Tcb *tcbp, NpuBuffer *bp, bool last)
{
u8 blockType;
u8 dbc;
u8 *dp;
int len;
u8 *limit;
Pcb *pcbp;
int status;
u8 *start;
pcbp = tcbp->pcbp;
blockType = bp->data[BlkOffBTBSN] & BlkMaskBT;
dp = bp->data + BlkOffDbc;
len = bp->numBytes - BlkOffDbc;
dbc = *dp++; // extract data block clarifier
len -= 1;
#if DEBUG
fprintf(npuNjeLog, "Port %02x: downline data received for %.7s, size %d, block type %u, dbc %02x\n",
pcbp->claPort, tcbp->termName, len, blockType, dbc);
npuNjeLogBytes(bp->data, bp->numBytes, (dbc & DbcTransparent) != 0 ? EBCDIC : ASCII);
npuNjeLogFlush();
#endif
if ((dbc & DbcTransparent) != 0)
{
status = npuNjeAppendRecords(pcbp, dp, len, blockType);
npuTipNotifySent(tcbp, bp->data[BlkOffBTBSN]);
if (status != NjeStatusOk)
{
npuNjeCloseConnection(pcbp);
}
}
else
{
/*
** Scan the non-transparent block to look for messages indicating that NJF has failed or
** is not running. If any such messages are found, close the connection.
**
** Each record in a non-transparent block begins with a format effector and ends with <US>
*/
limit = dp + len;
while (dp < limit)
{
start = dp++; // start points to format effector
while (dp < limit && *dp != US)
{
dp += 1;
}
len = dp - start;
if (((len > ApplicationFailedLen) && (memcmp(ApplicationFailed, start + 1, ApplicationFailedLen) == 0))
|| ((len > ApplicationNotPresentLen) && (memcmp(ApplicationNotPresent, start + 1, ApplicationNotPresentLen) == 0))
|| ((len > ApplicationBusyLen) && (memcmp(ApplicationBusy, start + 1, ApplicationBusyLen) == 0))
|| ((len > LoggedOutLen) && (memcmp(LoggedOut, start + 1, LoggedOutLen) == 0)))
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: %.7s disconnected from NJF\n", pcbp->claPort, tcbp->termName);
#endif
npuTipNotifySent(tcbp, bp->data[BlkOffBTBSN]);
npuNjeCloseConnection(pcbp);
return;
}
dp += 1;
}
npuTipNotifySent(tcbp, bp->data[BlkOffBTBSN]);
}
}
/*--------------------------------------------------------------------------
** Purpose: Process upline data from terminal.
**
** Parameters: Name Description.
** pcbp PCB pointer
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNjeProcessUplineData(Pcb *pcbp)
{
time_t delay;
bool done;
u8 *dp;
bool isComplete;
u8 *limit;
char ohost[9];
u32 oip;
Pcb *pcbp2;
u8 r;
u8 rcb;
char rhost[9];
u32 rip;
int size;
u8 *sp;
u8 srcb;
int status;
Tcb *tcbp;
#if DEBUG
if (pcbp->controls.nje.state > StNjeRcvOpen)
{
fprintf(npuNjeLog, "Port %02x: TCP data received from %s, state %s\n",
pcbp->claPort, pcbp->ncbp->hostName, NjeConnStates[pcbp->controls.nje.state]);
}
else
{
fprintf(npuNjeLog, "Port %02x: TCP data received, state %s\n",
pcbp->claPort, NjeConnStates[pcbp->controls.nje.state]);
}
npuNjeLogBytes(pcbp->inputData, pcbp->inputCount, EBCDIC);
npuNjeLogFlush();
#endif
sp = pcbp->inputData;
dp = pcbp->controls.nje.inputBufPtr;
if (dp + pcbp->inputCount > pcbp->controls.nje.inputBuf + pcbp->controls.nje.inputBufSize)
{
fprintf(stderr, "Port %02x: NJE input buffer overflow, data discarded\n", pcbp->claPort);
return;
}
while (pcbp->inputCount-- > 0)
{
*dp++ = *sp++;
}
pcbp->controls.nje.inputBufPtr = limit = dp;
dp = pcbp->controls.nje.inputBuf;
done = FALSE;
while (dp < limit && done == FALSE)
{
switch (pcbp->controls.nje.state)
{
case StNjeDisconnected:
// discard any data received while disconnected
#if DEBUG
fprintf(npuNjeLog, "Port %02x: disconnected, data discarded\n", pcbp->claPort);
#endif
dp = limit;
break;
case StNjeRcvOpen:
/*
** Expect and process an OPEN control record
*/
if ((limit - dp) < CrLength)
{
done = TRUE;
}
else if (memcmp(dp, CrTypeOpen, 8) == 0)
{
npuNjeParseControlRecord(dp + 8, rhost, &rip, ohost, &oip, &r);
dp += CrLength;
pcbp2 = npuNjeFindPcbForCr(rhost, rip, ohost, oip);
r = 0;
if (pcbp2 == NULL)
{
r = CrNakNoSuchLink;
}
else if (pcbp2->claPort == pcbp->claPort)
{
tcbp = npuNjeFindTcb(pcbp);
if (tcbp != NULL)
{
r = CrNakAttemptingActiveOpen;
}
}
else if (pcbp2->connFd > 0)
{
r = CrNakLinkActive;
if ((getSeconds() - pcbp2->controls.nje.lastXmit) >= 600)
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: close connection due to inactivity and active link conflict\n", pcbp2->claPort);
#endif
npuNetCloseConnection(pcbp2);
}
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: connection reassigned to port %02x\n", pcbp->claPort, pcbp2->claPort);
#endif
npuNjeResetPcb(pcbp2);
pcbp2->connFd = pcbp->connFd;
pcbp2->controls.nje.state = pcbp->controls.nje.state;
pcbp2->controls.nje.isPassive = pcbp->controls.nje.isPassive;
pcbp2->controls.nje.lastXmit = pcbp->controls.nje.lastXmit;
pcbp2->ncbp->state = pcbp->ncbp->state;
pcbp->connFd = 0;
pcbp->controls.nje.state = StNjeDisconnected;
pcbp->ncbp->state = StConnInit;
pcbp = pcbp2;
dp = pcbp->controls.nje.inputBufPtr;
limit = pcbp->controls.nje.inputBufPtr;
}
if (r == 0)
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: send upline request to connect terminal\n", pcbp->claPort);
#endif
if (npuNjeConnectTerminal(pcbp))
{
pcbp->controls.nje.state = StNjeRcvSOH_ENQ;
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: failed to issue terminal connection request\n", pcbp->claPort);
#endif
r = CrNakTemporaryFailure;
}
}
if ((npuNjeSendControlRecord(pcbp, (r == 0) ? CrTypeAck : CrTypeNak, ohost, oip, rhost, rip, r) == FALSE)
|| (r != 0))
{
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting OPEN\n", pcbp->claPort);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
break;
case StNjeRcvSOH_ENQ:
/*
** Expect <SOH><ENQ> or <SYN><NAK> after sending ACK control record
*/
dp = npuNjeCollectBlock(pcbp, dp, limit, &isComplete, &size, &status);
if (isComplete)
{
if (status == 0)
{
status = npuNjeUploadBlock(pcbp, pcbp->controls.nje.inputBuf, size, &rcb, &srcb);
switch (status)
{
case NjeStatusSOH_ENQ:
if (npuNjeSend(pcbp, DLE_ACK0, sizeof(DLE_ACK0)) == sizeof(DLE_ACK0))
{
pcbp->controls.nje.state = StNjeRcvSignon;
}
else
{
npuNjeCloseConnection(pcbp);
dp = limit;
}
break;
case NjeStatusSYN_NAK:
if (npuNjeSend(pcbp, SOH_ENQ, sizeof(SOH_ENQ)) == sizeof(SOH_ENQ))
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: send upline request to connect terminal\n", pcbp->claPort);
#endif
if (npuNjeConnectTerminal(pcbp))
{
/*
** The terminal connection request should cause the terminal to be
** connected to NJF, and NJF should respond by sending an initial
** signon request, so the next NJE protocol element received from
** the peer should be a response signon.
*/
pcbp->controls.nje.state = StNjeRcvResponseSignon;
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: failed to issue terminal connection request\n", pcbp->claPort);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else
{
npuNjeCloseConnection(pcbp);
dp = limit;
}
break;
case NjeStatusDLE_ACK0:
// Ignore idle block
break;
case NjeStatusNothingUploaded:
// Retransmitted blocks were probably detected, so remain in this state
break;
default:
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting <SOH><ENQ> or <SYN><NAK>, received status %d\n",
pcbp->claPort, status);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
break;
}
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: block collection error %d\n", pcbp->claPort, status);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else // isComplete == FALSE
{
done = TRUE;
}
break;
case StNjeRcvAck:
/*
** Expect and process an ACK or NAK control record
*/
if ((limit - dp) < CrLength)
{
done = TRUE;
}
else if (memcmp(dp, CrTypeAck, 8) == 0)
{
dp += CrLength;
if (npuNjeSend(pcbp, SOH_ENQ, sizeof(SOH_ENQ)) == sizeof(SOH_ENQ))
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: send upline request to connect terminal\n", pcbp->claPort);
#endif
if (npuNjeConnectTerminal(pcbp))
{
/*
** The terminal connection request should cause the terminal to be
** connected to NJF, and NJF should respond by sending an initial
** signon request, so the next NJE protocol element received from
** the peer should be a response signon.
*/
pcbp->controls.nje.state = StNjeRcvResponseSignon;
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: failed to issue terminal connection request\n", pcbp->claPort);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else
{
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else if (memcmp(dp, CrTypeNak, 8) == 0)
{
npuNjeParseControlRecord(dp + 8, rhost, &rip, ohost, &oip, &r);
dp += CrLength;
#if DEBUG
fprintf(npuNjeLog, "Port %02x: OPEN request denied: %s\n", pcbp->claPort, CrNakReasons[r]);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
delay = (time_t)((getMilliseconds() % 5) + 3);
// Arrange to attempt reconnection after a relatively short and random-ish interval
#if DEBUG
fprintf(npuNjeLog, "Port %02x: delay %ld secs before attempting reconnection\n", pcbp->claPort, delay);
#endif
pcbp->ncbp->nextConnectionAttempt = getSeconds() + delay;
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting ACK or NAK\n", pcbp->claPort);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
break;
case StNjeRcvSignon:
/*
** Expect and process an initial signon record
*/
dp = npuNjeCollectBlock(pcbp, dp, limit, &isComplete, &size, &status);
if (isComplete)
{
if (status == 0)
{
status = npuNjeUploadBlock(pcbp, pcbp->controls.nje.inputBuf, size, &rcb, &srcb);
if ((status == NjeStatusOk) && (rcb == RCB_GCR) && (srcb == SRCB_InitialSignon))
{
/*
** An initial signon was received and sent upline, so enter normal
** data exchange mode. The next downline block received from NJF
** should be the response signon, and then subsequent blocks should
** stream-related blocks.
*/
pcbp->controls.nje.state = StNjeExchangeData;
#if DEBUG
fprintf(npuNjeLog, "Port %02x: enter data exchange state with ping interval %d secs\n",
pcbp->claPort, pcbp->controls.nje.pingInterval);
#endif
}
else if ((status != NjeStatusNothingUploaded) && (status != NjeStatusDLE_ACK0))
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting initial signon\n", pcbp->claPort);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting initial signon\n", pcbp->claPort);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else // isComplete == FALSE
{
done = TRUE;
}
break;
case StNjeRcvResponseSignon:
/*
** Expect and process a signon response record
*/
dp = npuNjeCollectBlock(pcbp, dp, limit, &isComplete, &size, &status);
if (isComplete)
{
if (status == 0)
{
status = npuNjeUploadBlock(pcbp, pcbp->controls.nje.inputBuf, size, &rcb, &srcb);
if ((status == NjeStatusOk) && (rcb == RCB_GCR) && (srcb == SRCB_RespSignon))
{
npuNetSend(npuNjeFindTcb(pcbp), DLE_ACK0, sizeof(DLE_ACK0));
pcbp->controls.nje.state = StNjeExchangeData;
#if DEBUG
fprintf(npuNjeLog, "Port %02x: enter data exchange state with ping interval %d secs\n",
pcbp->claPort, pcbp->controls.nje.pingInterval);
#endif
}
else if ((status != NjeStatusNothingUploaded) && (status != NjeStatusDLE_ACK0))
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting response signon\n", pcbp->claPort);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting response signon\n", pcbp->claPort);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else // isComplete == FALSE
{
done = TRUE;
}
break;
case StNjeExchangeData:
/*
** Process ordinary data exchanges
*/
dp = npuNjeCollectBlock(pcbp, dp, limit, &isComplete, &size, &status);
if (isComplete)
{
if (status == 0)
{
status = npuNjeUploadBlock(pcbp, pcbp->controls.nje.inputBuf, size, &rcb, &srcb);
if ((status != NjeStatusOk) && (status != NjeStatusNothingUploaded) && (status != NjeStatusDLE_ACK0))
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting normal data exchange, detected status %d\n",
pcbp->claPort, status);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else
{
#if DEBUG
fprintf(npuNjeLog, "Port %02x: expecting normal data exchange, detected status %d\n",
pcbp->claPort, status);
#endif
npuNjeCloseConnection(pcbp);
dp = limit;
}
}
else // isComplete == FALSE
{
done = TRUE;
}
break;
default:
#if DEBUG
fprintf(npuNjeLog, "Invalid NJE state: %d\n", pcbp->controls.nje.state);
#endif
dp = limit;
break;
}
}
//
// Move residual data, if any, to the beginning of the input buffer.
//
if (dp < pcbp->controls.nje.inputBufPtr)
{
if (dp > pcbp->controls.nje.inputBuf)
{
size = pcbp->controls.nje.inputBufPtr - dp;
sp = dp;
dp = pcbp->controls.nje.inputBuf;
while (size-- > 0)
{
*dp++ = *sp++;
}
pcbp->controls.nje.inputBufPtr = dp;
}
}
else
{
pcbp->controls.nje.inputBufPtr = pcbp->controls.nje.inputBuf;
}
}
/*--------------------------------------------------------------------------
** Purpose: Handle upline block acknowledgement
**