-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcdcnet.c
3222 lines (2927 loc) · 102 KB
/
cdcnet.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) 2003-2019, Kevin Jordan, Tom Hunter
**
** Name: cdcnet_gw.c
**
** Author: Kevin Jordan
**
** Description:
** Perform emulation of the Network Host Products TCP/IP gateway running
** in a CDCNet MDI.
**
** 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
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <memory.h>
#include <time.h>
#if defined(_WIN32)
#include <winsock.h>
#else
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <netdb.h>
#include <signal.h>
#endif
#include "const.h"
#include "types.h"
#include "proto.h"
#include "npu.h"
#if defined(__APPLE__)
#include <execinfo.h>
#endif
/*
** -----------------
** Private Constants
** -----------------
*/
#define cdcnetInitUpline 0x01
#define cdcnetInitDownline 0x02
/* --- TCP Gateway header types --- */
#define cdcnetTcpHTIndication 0
#define cdcnetTcpHTRequest 0
#define cdcnetTcpHTResponse 1
/* --- Gateway TCP version --- */
#define cdcnetTcpVersion 0x10
#define cdcnetUdpVersion 0x02
/* --- Offsets common to all TCP gateway commands and responses --- */
#define BlkOffTcpCmdName 5
#define BlkOffTcpHeaderType 12
#define BlkOffTcpHeaderLen 13
#define BlkOffTcpDataLen 15
#define BlkOffTcpStatus 17
#define BlkOffTcpTcpVersion 19
/* --- Offsets to fields in TCP Open SAP command --- */
#define BlkOffTcpOSUserSapId 20
#define BlkOffTcpOSTcpIpGwVer 24
#define BlkOffTcpOSTcpSapId 28
/* --- Offsets to fields in TCP Close SAP command --- */
#define BlkOffTcpCSTcpSapId 20
/* --- Offsets to fields in TCP Active Connect command --- */
#define BlkOffTcpACTcpSapId 20
#define BlkOffTcpACUserCepId 28
#define BlkOffTcpACTcpCepId 35
#define BlkOffTcpACSrcAddr 50
#define BlkOffTcpACDstAddr 80
#define BlkOffTcpACAllocation 110
#define BlkOffTcpACIpHeader 125
#define BlkOffTcpACIpOptions 155
#define BlkOffTcpACUlpTimeout 485
#define BlkOffTcpACPushFlag 500
#define BlkOffTcpACUrgentFlag 500
/* --- Offsets to fields in TCP Passive Connect command --- */
#define BlkOffTcpPCTcpSapId 20
#define BlkOffTcpPCUserCepId 28
#define BlkOffTcpPCTcpCepId 35
#define BlkOffTcpPCSrcAddr 50
#define BlkOffTcpPCDstAddr 80
#define BlkOffTcpPCAllocation 110
#define BlkOffTcpPCIpHeader 125
#define BlkOffTcpPCIpOptions 155
#define BlkOffTcpPCUlpTimeout 485
#define BlkOffTcpPCPushFlag 500
#define BlkOffTcpPCUrgentFlag 500
/* --- Offsets to fields in TCP Allocation command --- */
#define BlkOffTcpATcpCepId 20
#define BlkOffTcpASize 28
/* --- Offsets to fields in TCP Status command --- */
#define BlkOffTcpSTcpCepId 20
#define BlkOffTcpSUserCepId 28
#define BlkOffTcpSTcpSapId 35
#define BlkOffTcpSUserSapId 43
#define BlkOffTcpSSrcAddr 50
#define BlkOffTcpSDstAddr 80
#define BlkOffTcpSIpHeader 110
#define BlkOffTcpSIpOptions 140
#define BlkOffTcpSUlpTimeout 470
#define BlkOffTcpSTcpStatRec 485
/* --- Offsets to fields in TCP Disconnect command --- */
#define BlkOffTcpDTcpCepId 20
/* --- Offsets to fields in TCP Abort Current Connection command --- */
#define BlkOffTcpACCTcpCepId 20
/* --- Offsets to fields in Send Data command --- */
#define BlkOffTcpSDTcpCepId 20
#define BlkOffTcpSDPushFlag 24
#define BlkOffTcpSDUrgent 24
#define BlkOffTcpSDUlpTimeout 35
/* --- Offsets to fields in TCP Connection Indication --- */
#define BlkOffTcpCIUserCepId 20
#define BlkOffTcpCISrcAddr 35
#define BlkOffTcpCIDstAddr 65
#define BlkOffTcpCIIpHeader 95
#define BlkOffTcpCIIpOptions 125
#define BlkOffTcpCIUlpTimeout 455
#define cdcnetTcpCILength (470 - BlkOffTcpCmdName)
/* --- Offsets to fields in TCP Receive indication --- */
#define BlkOffTcpRUserCepId 20
#define BlkOffTcpRUrgent 24
#define cdcnetTcpRLength (35 - BlkOffTcpCmdName)
/* --- Offsets to fields in TCP Close SAP Indication --- */
#define BlkOffTcpCSIUserSapId 20
#define cdcnetTcpCSILength (35 - BlkOffTcpCmdName)
/* --- Offsets to fields in TCP Abort Indication --- */
#define BlkOffTcpABIUserCepId 20
#define cdcnetTcpABILength (35 - BlkOffTcpCmdName)
/* --- Offsets to fields in TCP Disconnect Confirmation --- */
#define BlkOffTcpDCUserCepId 20
#define cdcnetTcpDCLength (35 - BlkOffTcpCmdName)
/* --- Offsets to fields in TCP Disconnect Indication --- */
#define BlkOffTcpDIUserCepId 20
#define cdcnetTcpDILength (35 - BlkOffTcpCmdName)
/* --- Offsets to fields in TCP Error Indication --- */
#define BlkOffTcpEIUserCepId 20
#define cdcnetTcpEILength (35 - BlkOffTcpCmdName)
/* --- IP address fields in use indicators --- */
#define cdcnetIpAddrFieldsNone 0
#define cdcnetIpAddrFieldsNetwork 1
#define cdcnetIpAddrFieldsHost 2
#define cdcnetIpAddrFieldsBoth 3
/* --- Relative offsets in TCP IP address structures --- */
#define RelOffTcpIpAddress 0
#define RelOffTcpIpAddrFieldsInUse 0
#define RelOffTcpIpAddressNetwork 1
#define RelOffTcpIpAddressHost 4
#define cdcnetTcpIpAddressLength 7
/* --- Relative offsets in TCP IP address structures --- */
#define RelOffTcpPortInUse 15
#define RelOffTcpPort 16
#define cdcnetTcpAddressLength 30
/* --- Relative offsets in ULP timeout structure --- */
#define RelOffGwUlpTimeout 0
#define RelOffGwUlpTimeoutAbort 4
/* --- Offsets to attributes in NAM A-A connection request --- */
#define BlkOffDwnBlkLimit 12
#define BlkOffDwnBlkSize 13
#define BlkOffUplBlkLimit 16
#define BlkOffUplBlkSize 17
#define BlkOffAppName 29
/* --- Reason codes for NAM A-A connection failure --- */
#define cdcnetErrAppMaxConns 20
#define cdcnetErrAppNotAvail 22
/* --- UDP gateway request primitives --- */
#define cdcnetUdpCallRequest 0x10
#define cdcnetUdpDataRequest 0x11
#define cdcnetUdpDataRequestDest 0x12
#define cdcnetUdpDataIndication 0x13
#define cdcnetUdpCallResponse 0x14
/* --- UDP gateway error codes --- */
#define cdcnetUdpHostUnreachable 0x3f
#define cdcnetUdpNetUnreachable 0x4f
#define cdcnetUdpProtoUnreachable 0x5f
#define cdcnetUdpPortUnreachable 0x6f
#define cdcnetUdpRouteFailed 0x7f
#define cdcnetUdpPortInUse 0x8f
#define cdcnetUdpNoResources 0x9f
#define cdcnetUdpSapNotOpen 0xcf
#define cdcnetUdpSapAlreadyOpen 0xdf
/* --- Relative offsets in UDP address structures --- */
#define RelOffUdpIpAddress 0
#define RelOffUdpIpAddrFieldsInUse 0
#define RelOffUdpIpAddressNetwork 1
#define RelOffUdpIpAddressHost 5
#define RelOffUdpPortInUse 9
#define RelOffUdpPort 10
#define cdcnetUdpAddressLength 13
/* --- UDP gateway common header offsets --- */
#define BlkOffUdpHeader 5
#define BlkOffUdpRequestType 5
#define BlkOffUdpVersion 6
/* --- UDP gateway Open SAP request offsets --- */
#define BlkOffUdpOpenSapUnused 7
#define BlkOffUdpOpenSapSrcAddr 8
#define BlkOffUdpOpenSapDstAddr 21
/* --- UDP gateway Data Indication offsets --- */
#define BlkOffUdpDataIndUnused 7
#define BlkOffUdpDataIndSrcAddr 8
#define BlkOffUdpDataIndData 21
/* --- UDP gateway Data Request offsets --- */
#define BlkOffUdpDataReqDstAddr 10
#define BlkOffUdpDataReqData 23
/*
** -----------------------
** 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
** -----------------------------------------
*/
typedef enum tcpGwStatus // from CDCNet source file
{
tcp_successful, // Function completed.
tcp_connection_inuse, // The connection already exists.
tcp_connection_not_open, // Exists, only half open.
tcp_host_unreachable, // Unable to route.
tcp_illegal_workcode, // Work code illegal.
tcp_internal_error, // Internal error occurred.
tcp_invalid_routine, // NIL pointer to user cmr or lmr.
tcp_net_unreachable, // Unable to route.
tcp_no_connection, // Connection doesn't exist.
tcp_no_resources, // No memory available.
tcp_option_error, // Error in IP header option.
tcp_port_unreachable, // Unable to route.
tcp_protocol_error, // Pdu has fatal error.
tcp_protocol_unreachable, // Unable to route.
tcp_remote_abort, // Abort received from peer.
tcp_route_failed, // Source route option bad.
tcp_sap_not_open, // The SAPID is invalid.
tcp_sap_unavailable, // No SAPs are available.
tcp_sec$prec_mismatch, // security/precedence mismatch.
tcp_ulp_timeout, // Pdu timed out.
tcp_not_configured // TCP has not been defined.
} TcpGwStatus;
typedef enum gwConnState
{
StGwIdle,
StGwStartingInit,
StGwInitializing,
StGwConnected,
StGwInitiateTermination,
StGwTerminating,
StGwAwaitTermBlock,
StGwError
} GwConnState;
typedef enum tcpUdpConnState
{
StTcpUdpIdle,
StTcpConnecting,
StTcpIndicatingConnection,
StTcpListening,
StTcpConnected,
StTcpDisconnecting,
StUdpBound
} TcpUdpConnState;
typedef enum gwConnType
{
TypeTcpActive,
TypeTcpPassive,
TypeUdp
} GwConnType;
typedef struct gcb // Gateway Control Block
{
u16 ordinal;
GwConnState gwState;
TcpUdpConnState tcpUdpState;
GwConnType connType;
u8 initStatus;
u8 cn;
u8 bsn;
u8 unackedBlocks;
u16 maxUplineBlockSize;
u32 tcpSapId;
u32 tcpCepId;
u32 userSapId;
u32 userCepId;
u8 reasonCode;
u8 tcpSrcAddress[cdcnetTcpAddressLength];
u8 tcpDstAddress[cdcnetTcpAddressLength];
char srcIpAddress[20];
u16 srcPort;
char dstIpAddress[20];
u16 dstPort;
#if defined(_WIN32)
SOCKET connFd;
#else
int connFd;
#endif
u32 localAddr;
u16 localPort;
u32 peerAddr;
u16 peerPort;
time_t deadline;
NpuQueue downlineQueue;
NpuQueue outputQueue;
} Gcb;
typedef struct pccb // Passive Connection Control Block
{
u16 ordinal;
u16 tcpGcbOrdinal;
u16 srcPort;
u16 dstPort;
#if defined(_WIN32)
SOCKET connFd;
#else
int connFd;
#endif
time_t deadline;
} Pccb;
typedef struct tcpGwCommand
{
char *command;
bool (*handler)(Gcb *gp, NpuBuffer *bp);
} TcpGwCommand;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static Pccb *cdcnetAddPccb();
static Gcb *cdcnetAddGcb();
static void cdcnetCloseConnection(Gcb *gp);
#if defined(_WIN32)
static SOCKET cdcnetCreateTcpSocket();
#else
static int cdcnetCreateTcpSocket();
#endif
static Pccb *cdcnetFindPccb(u16 port);
static Gcb *cdcnetFindGcb(u8 cn);
static u32 cdcnetGetIdFromMessage(u8 *ip);
static Pccb *cdcnetGetPccb();
#if defined(_WIN32)
static bool cdcnetGetEndpoints(SOCKET sd, u32 *localAddr, u16 *localPort,
u32 *peerAddr, u16 *peerPort);
#else
static bool cdcnetGetEndpoints(int sd, u32 *localAddr, u16 *localPort,
u32 *peerAddr, u16 *peerPort);
#endif
static Gcb *cdcnetGetGcb();
static void cdcnetPutIdToMessage(u32 id, u8 *mp);
static void cdcnetPutU32ToMessage(u32 value, u8 *mp);
static void cdcnetSendBack(Gcb *gp, NpuBuffer *bp, u8 bsn);
static bool cdcnetSendInitializeConnectionRequest(Gcb *gp);
static void cdcnetSendInitializeConnectionResponse(NpuBuffer *bp, Gcb *gp);
static void cdcnetSendInitiateConnectionResponse(NpuBuffer *bp, u8 cn, u8 rc);
static void cdcnetSendTerminateConnectionBlock(NpuBuffer *bp, u8 cn);
static void cdcnetSendTerminateConnectionRequest(NpuBuffer *bp, u8 cn);
static void cdcnetSendTerminateConnectionResponse(NpuBuffer *bp, u8 cn);
static bool cdcnetTcpAbortCurrentConnectionHandler(Gcb *gp, NpuBuffer *bp);
static bool cdcnetTcpActiveConnectHandler(Gcb *gp, NpuBuffer *bp);
static bool cdcnetTcpAllocateHandler(Gcb *gp, NpuBuffer *bp);
static bool cdcnetTcpCloseSAPHandler(Gcb *gp, NpuBuffer *bp);
static bool cdcnetTcpDisconnectHandler(Gcb *gp, NpuBuffer *bp);
static u32 cdcnetTcpGetIpAddress(u8 *ap);
static u16 cdcnetTcpGetPort(u8 *ap);
static bool cdcnetTcpOpenSAPHandler(Gcb *gp, NpuBuffer *bp);
static bool cdcnetTcpPassiveConnectHandler(Gcb *gp, NpuBuffer *bp);
static void cdcnetTcpRequestUplineTransfer(Gcb *gp, NpuBuffer *bp, u8 blockType, u8 headerType, TcpGwStatus status);
static bool cdcnetTcpSendConnectionIndication(Gcb *gp);
static void cdcnetTcpSendDataIndication(Gcb *gp);
static bool cdcnetTcpSendErrorIndication(Gcb *gp);
static void cdcnetTcpSetIpAddress(u8 *ap, u32 ipAddr);
static void cdcnetTcpSetPort(u8 *ap, u16 port);
static bool cdcnetUdpBindAddress(Gcb *gp, NpuBuffer *bp);
static u32 cdcnetUdpGetIpAddress(u8 *ap);
static u16 cdcnetUdpGetPort(u8 *ap);
static bool cdcnetUdpSendDownlineData(Gcb *gp, NpuBuffer *bp);
static void cdcnetUdpSendUplineData(Gcb *gp);
static void cdcnetUdpSetAddress(u8 *dp, u32 ipAddress, u16 port);
#if DEBUG
static void cdcnetLogBytes(u8 *bytes, int len);
static void cdcnetLogFlush(void);
static void cdcnetPrintStackTrace(FILE *fp);
#endif
/*
** ----------------
** Public Variables
** ----------------
*/
u8 cdcnetNode = 255;
u16 cdcnetPrivilegedTcpPortOffset = 6600;
u16 cdcnetPrivilegedUdpPortOffset = 6600;
/*
** -----------------
** Private Variables
** -----------------
*/
static u16 cdcnetPassivePort = 7600;
static Pccb *cdcnetPccbs = (Pccb *)NULL;
static u8 cdcnetPccbCount = 0;
static Gcb *cdcnetGcbs = (Gcb *)NULL;
static u8 cdcnetGcbCount = 0;
static TcpGwCommand tcpGwCommands [] =
{
{ "TCPA ", cdcnetTcpAllocateHandler },
{ "TCPAC ", cdcnetTcpActiveConnectHandler },
{ "TCPACC ", cdcnetTcpAbortCurrentConnectionHandler },
{ "TCPCS ", cdcnetTcpCloseSAPHandler },
{ "TCPD ", cdcnetTcpDisconnectHandler },
{ "TCPOS ", cdcnetTcpOpenSAPHandler },
{ "TCPPC ", cdcnetTcpPassiveConnectHandler },
/*
* {"TCPS ", cdcnetTcpStatusHandler},
* {"TCPSD ", cdcnetTcpSendDataHandler},
* {"TCPCSI ", cdcnetTcpCloseSAPIndicationHandler},
* {"TCPABI ", cdcnetTcpAbortIndicationHandler},
* {"TCPDC ", cdcnetTcpDisconnectConfirmationHandler},
* {"TCPDI ", cdcnetTcpDisconnectIndicationHandler},
*/
{ NULL, NULL }
};
#if DEBUG
static FILE *cdcnetLog = NULL;
static char cdcnetLogBuf[LogLineLength + 1];
static int cdcnetLogBytesCol = 0;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Reset CDCNet Gateway when network is going down.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cdcnetReset(void)
{
int i;
Pccb *pp;
Gcb *gp;
if (cdcnetGcbCount < 1)
{
return;
}
#if DEBUG
fputs("Reset gateway\n", cdcnetLog);
#endif
/*
** Iterate through all Gcbs.
*/
for (i = 0, gp = cdcnetGcbs; i < cdcnetGcbCount; i++, gp++)
{
if (gp->gwState != StGwIdle)
{
cdcnetCloseConnection(gp);
}
}
/*
** Iterate through all Pccbs.
*/
for (i = 0, pp = cdcnetPccbs; i < cdcnetPccbCount; i++, pp++)
{
if (pp->connFd != 0)
{
netCloseConnection(pp->connFd);
pp->connFd = 0;
pp->dstPort = 0;
pp->tcpGcbOrdinal = 0;
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Process a downline block.
**
** Parameters: Name Description.
** bp pointer to first downline buffer.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cdcnetProcessDownlineData(NpuBuffer *bp)
{
char *appName;
u8 blockType;
u8 cn;
int nameLen;
u8 pfc;
u8 rc;
u8 sfc;
Gcb *gp;
if (bp == NULL)
{
return;
}
#if DEBUG
if (cdcnetLog == NULL)
{
cdcnetLog = fopen("cdcnetlog.txt", "wt");
if (cdcnetLog == NULL)
{
fprintf(stderr, "cdcnetlog.txt - aborting\n");
exit(1);
}
cdcnetLogFlush(); // initialize log buffer
}
fprintf(cdcnetLog, "Process downline block (%d bytes)\n", bp->numBytes);
cdcnetLogBytes(bp->data, bp->numBytes);
cdcnetLogFlush();
#endif
blockType = bp->data[BlkOffBTBSN] & BlkMaskBT;
cn = bp->data[BlkOffCN];
switch (blockType)
{
case BtHTBLK:
case BtHTMSG:
gp = cdcnetFindGcb(cn);
if (gp != NULL)
{
bp->blockSeqNo = (bp->data[BlkOffBTBSN] >> BlkShiftBSN) & BlkMaskBSN;
npuBipQueueAppend(bp, &gp->downlineQueue);
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Connection not found: %02X\n", cn);
#endif
npuBipBufRelease(bp);
}
break;
case BtHTBACK:
gp = cdcnetFindGcb(cn);
if ((gp != NULL) && (gp->unackedBlocks > 0))
{
gp->unackedBlocks--;
}
npuBipBufRelease(bp);
break;
case BtHTCMD:
pfc = bp->data[BlkOffPfc];
sfc = bp->data[BlkOffSfc];
switch (pfc)
{
case 0x02: // initiate connection
if (sfc == 0x09) // A-A connection
{
rc = 0;
cn = bp->data[BlkOffP3];
appName = (char *)bp->data + BlkOffAppName;
nameLen = bp->numBytes - BlkOffAppName;
gp = cdcnetGetGcb();
if ((nameLen < 9) || (strncmp(appName, "GW_TCPIP_", 9) != 0))
{
#if DEBUG
fprintf(cdcnetLog, "Application name '%.9s' does not match GW_TCPIP_ ...\n", appName);
#endif
rc = cdcnetErrAppNotAvail;
}
else if (gp == NULL)
{
#if DEBUG
fprintf(cdcnetLog, "Unable to allocate TCP/IP control block for %*s (CN=%02X)\n",
nameLen, appName, cn);
#endif
rc = cdcnetErrAppMaxConns;
}
else
{
gp->cn = cn;
gp->unackedBlocks = 0;
gp->maxUplineBlockSize = *(bp->data + BlkOffUplBlkSize) * 100;
gp->gwState = StGwStartingInit;
gp->initStatus = 0;
}
cdcnetSendInitiateConnectionResponse(bp, cn, rc);
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Received unexpected command type: %02X/%02X\n", pfc, sfc);
#endif
npuBipBufRelease(bp);
}
break;
case 0x03: // terminate connection
if ((sfc == 0x08) || (sfc == (0x08 | SfcResp))) // terminate connection request or response
{
cn = bp->data[BlkOffP3];
gp = cdcnetFindGcb(cn);
if (gp != NULL)
{
if (sfc == 0x08) // terminate connection request
{
cdcnetSendTerminateConnectionBlock(bp, cn);
gp->gwState = StGwTerminating;
}
else // terminate connection response
{
cdcnetCloseConnection(gp);
npuBipBufRelease(bp);
}
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Connection not found: %02X\n", cn);
#endif
npuBipBufRelease(bp);
}
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Received unexpected command type: %02X/%02X\n", pfc, sfc);
#endif
npuBipBufRelease(bp);
}
break;
default:
#if DEBUG
fprintf(cdcnetLog, "Received unexpected command type: %02X/%02X\n", pfc, sfc);
#endif
npuBipBufRelease(bp);
break;
}
break;
case BtHTQBLK:
case BtHTQMSG:
gp = cdcnetFindGcb(cn);
if (gp != NULL)
{
bp->blockSeqNo = (bp->data[BlkOffBTBSN] >> BlkShiftBSN) & BlkMaskBSN;
npuBipQueueAppend(bp, &gp->downlineQueue);
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Connection not found: %02X\n", cn);
#endif
npuBipBufRelease(bp);
}
break;
case BtHTRINIT:
gp = cdcnetFindGcb(cn);
if (gp != NULL)
{
cdcnetSendInitializeConnectionResponse(bp, gp);
gp->initStatus |= cdcnetInitDownline;
if (gp->initStatus == (cdcnetInitDownline | cdcnetInitUpline))
{
gp->gwState = StGwConnected;
#if DEBUG
fprintf(cdcnetLog, "Gateway connection established, CN=%02X\n", cn);
#endif
}
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Connection not found: %02X\n", cn);
#endif
npuBipBufRelease(bp);
}
break;
case BtHTNINIT:
gp = cdcnetFindGcb(cn);
if (gp != NULL)
{
gp->initStatus |= cdcnetInitUpline;
if (gp->initStatus == (cdcnetInitDownline | cdcnetInitUpline))
{
gp->gwState = StGwConnected;
#if DEBUG
fprintf(cdcnetLog, "Gateway connection established, CN=%02X\n", cn);
#endif
}
}
#if DEBUG
else
{
fprintf(cdcnetLog, "Connection not found: %02X\n", cn);
}
#endif
npuBipBufRelease(bp);
break;
case BtHTTERM:
gp = cdcnetFindGcb(cn);
if (gp != NULL)
{
if (gp->gwState == StGwAwaitTermBlock)
{
cdcnetSendTerminateConnectionBlock(bp, cn); // echo TERM block
gp->gwState = StGwIdle;
}
else if (gp->gwState == StGwTerminating)
{
cdcnetSendTerminateConnectionResponse(bp, cn);
cdcnetCloseConnection(gp);
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Invalid state %d on reception of TERM block: CN=%02X\n", gp->gwState, cn);
#endif
npuBipBufRelease(bp);
}
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Connection not found: %02X\n", cn);
#endif
npuBipBufRelease(bp);
}
break;
default:
#if DEBUG
fprintf(cdcnetLog, "Received unexpected block type: %02X, CN=%02X\n", blockType, cn);
#endif
npuBipBufRelease(bp);
break;
}
}
/*--------------------------------------------------------------------------
** Purpose: Check TCP/IP Gateway connection status.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cdcnetCheckStatus(void)
{
u8 b;
u8 blockType;
NpuBuffer *bp;
NpuBuffer *bp2;
time_t currentTime;
struct sockaddr_in from;
TcpGwCommand *gcp;
int i;
u32 localAddr;
u16 localPort;
int n;
int optEnable = 1;
u32 peerAddr;
u16 peerPort;
Pccb *pp;
fd_set readFds;
int rc;
int readySockets;
struct timeval timeout;
Gcb *gp;
fd_set writeFds;
#if defined(_WIN32)
SOCKET fd;
u_long blockEnable = 1;
int fromLen;
SOCKET maxFd;
#else
int fd;
socklen_t fromLen;
int maxFd;
#endif
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
currentTime = getSeconds();
maxFd = 0;
for (i = 0, pp = cdcnetPccbs; i < cdcnetPccbCount; i++, pp++)
{
if (pp->connFd > 0)
{
if (pp->tcpGcbOrdinal != 0)
{
FD_SET(pp->connFd, &readFds);
if (pp->connFd > maxFd)
{
maxFd = pp->connFd;
}
}
else if (currentTime >= pp->deadline)
{
#if DEBUG
fprintf(cdcnetLog, "Unassigned listen port timeout, port=%d\n", pp->dstPort);
#endif
netCloseConnection(pp->connFd);
pp->dstPort = 0;
pp->connFd = 0;
}
}
}
for (i = 0, gp = cdcnetGcbs; i < cdcnetGcbCount; i++, gp++)
{
switch (gp->gwState)
{
case StGwStartingInit:
if (cdcnetSendInitializeConnectionRequest(gp))
{
gp->gwState = StGwInitializing;
gp->deadline = currentTime + 10;
}
break;
case StGwInitializing:
if (gp->deadline < currentTime)
{
#if DEBUG
fprintf(cdcnetLog, "Connection initialization timed out, CN=%02X\n", gp->cn);
#endif
cdcnetCloseConnection(gp);
}
break;
case StGwInitiateTermination:
bp = npuBipBufGet();
if (bp != NULL)
{
cdcnetCloseConnection(gp);
cdcnetSendTerminateConnectionRequest(bp, gp->cn);
gp->gwState = StGwAwaitTermBlock;
}
break;
case StGwConnected:
bp = npuBipQueueExtract(&gp->downlineQueue);
if (bp != NULL)
{
blockType = bp->data[BlkOffBTBSN] & BlkMaskBT;
switch (blockType)
{
case BtHTBLK:
case BtHTMSG:
bp->offset = BlkOffDbc + 1;
npuBipQueueAppend(bp, &gp->outputQueue);
break;
case BtHTQBLK:
case BtHTQMSG:
if (bp->blockSeqNo)
{
bp2 = npuBipBufGet();
if (bp2 == NULL)
{
npuBipQueuePrepend(bp, &gp->downlineQueue);
break;
}
cdcnetSendBack(gp, bp2, bp->blockSeqNo);
bp->blockSeqNo = 0;
}
for (gcp = tcpGwCommands; gcp->command; gcp++)
{
if (strncmp(gcp->command, (char *)bp->data + BlkOffTcpCmdName, 7) == 0)
{
if (!(*gcp->handler)(gp, bp))
{
npuBipQueuePrepend(bp, &gp->downlineQueue);
}
break;
}
}
if (!gcp->command)
{
/*
** If the message does not have a recognized TCP gateway command,
** check for a UDP gateway primitive.
*/
b = *(bp->data + BlkOffUdpRequestType);
if ((b >= cdcnetUdpCallRequest) && (b <= cdcnetUdpDataRequestDest)
&& (*(bp->data + BlkOffUdpVersion) == cdcnetUdpVersion))
{
if (b == cdcnetUdpCallRequest)
{
if (!cdcnetUdpBindAddress(gp, bp))
{
npuBipQueuePrepend(bp, &gp->downlineQueue);
}
}
else if (!cdcnetUdpSendDownlineData(gp, bp))
{
npuBipQueuePrepend(bp, &gp->downlineQueue);
}
}
else
{
#if DEBUG
fprintf(cdcnetLog, "Unrecognized TCP gateway command: %7s\n", (char *)bp->data + BlkOffTcpCmdName);