-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnpu_net.c
1594 lines (1457 loc) · 45.5 KB
/
npu_net.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-2011, Tom Hunter
**
** Name: npu_net.c
**
** Description:
** Provides TCP/IP networking interface to the ASYNC TIP in an NPU
** consisting of a CDC 2550 HCP running CCP.
**
** 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>.
**
**--------------------------------------------------------------------------
*/
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "const.h"
#include "types.h"
#include "proto.h"
#include "npu.h"
#include "cci.h"
#include <sys/types.h>
#include <memory.h>
#include <time.h>
#if defined(_WIN32)
#include <winsock.h>
#else
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#define DEBUG 0
/*
** -----------------
** Private Constants
** -----------------
*/
#define MaxClaPorts 128
#define NamStartupTime 30
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static int npuNetAcceptConnections(fd_set *selectFds, int maxFd);
static int npuNetCreateConnections(void);
static bool npuNetCreateListeningSocket(Ncb *ncbp);
static void npuNetCreateThread(void);
static bool npuNetProcessNewConnection(int connFd, Ncb *ncbp, bool isPassive);
static int npuNetRegisterClaPort(Ncb *ncbp);
static void npuNetSendConsoleMsg(int connFd, int connType, char *msg);
static void npuNetTryOutput(Pcb *pcbp);
#if defined(_WIN32)
static void npuNetThread(void *param);
#else
static void *npuNetThread(void *param);
#endif
/*
** ----------------
** Public Variables
** ----------------
*/
char npuNetHostID[HostIdSize];
u32 npuNetHostIP = 0;
u8 npuNetMaxClaPort = 0;
u8 npuNetMaxCN = 0;
/*
** -----------------
** Private Variables
** -----------------
*/
static char abortMsg[] = "\r\nConnection aborted\r\n";
static char connectingMsg[] = "\r\nConnecting to host - please wait ...";
static char connectedMsg[] = "\r\nConnected\r\n";
static char *connStates[] =
{
//
// Indexed by connection type
//
"idle", // StConnInit
"connecting", // StConnConnecting
"connected", // StConnConnected
"busy" // StConnBusy
};
static char *connTypes[] =
{
//
// Indexed by connection type
//
"raw", // ConnTypeRaw
"pterm", // ConnTypePterm
"rs232", // ConnTypeRs232
"telnet", // ConnTypeTelnet
"hasp", // ConnTypeHasp
"rhasp", // ConnTypeRevHasp
"nje", // ConnTypeNje
"trunk" // ConnTypeTrunk
};
static char networkDownMsg[] = "\r\nNetwork going down - connection aborted\r\n";
static char notReadyMsg[] = "\r\nHost not ready to accept connections - please try again later.\r\n";
static char noPortsAvailMsg[] = "\r\nNo free ports available - please try again later.\r\n";
static char tcbNotConfiguredMsg[] = "\r\nCould not configure tcb - please try again later.\r\n";
static Pcb pcbs[MaxClaPorts];
static bool isPcbsPreset = FALSE;
static Ncb ncbs[MaxTermDefs];
static int numNcbs = 0;
static int pollIndex = 0;
/*
** Table of functions that queue data for sending to the network,
** indexed by connection type
*/
static void (*netSend[])(Tcb *tp, u8 *data, int len) =
{
npuNetQueueOutput, // ConnTypeRaw
npuAsyncPtermNetSend, // ConnTypePterm
npuNetQueueOutput, // ConnTypeRs232
npuAsyncTelnetNetSend, // ConnTypeTelnet
npuNetQueueOutput, // ConnTypeHasp
npuNetQueueOutput, // ConnTypeRevHasp
npuNetQueueOutput, // ConnTypeNje
npuNetQueueOutput // ConnTypeTrunk
};
/*
** Table of functions that notify of network connection, indexed by connection type
*/
static bool (*notifyNetConnect[])(Pcb *pcbp, bool isPassive) =
{
npuAsyncNotifyNetConnect, // ConnTypeRaw
npuAsyncNotifyNetConnect, // ConnTypePterm
npuAsyncNotifyNetConnect, // ConnTypeRs232
npuAsyncNotifyNetConnect, // ConnTypeTelnet
npuHaspNotifyNetConnect, // ConnTypeHasp
npuHaspNotifyNetConnect, // ConnTypeRevHasp
npuNjeNotifyNetConnect, // ConnTypeNje
npuLipNotifyNetConnect // ConnTypeTrunk
};
/*
** Table of functions that notify of network disconnection, indexed by connection type
*/
static void (*notifyNetDisconnect[])(Pcb *pcbp) =
{
npuAsyncNotifyNetDisconnect, // ConnTypeRaw
npuAsyncNotifyNetDisconnect, // ConnTypePterm
npuAsyncNotifyNetDisconnect, // ConnTypeRs232
npuAsyncNotifyNetDisconnect, // ConnTypeTelnet
npuHaspNotifyNetDisconnect, // ConnTypeHasp
npuHaspNotifyNetDisconnect, // ConnTypeRevHasp
npuNjeNotifyNetDisconnect, // ConnTypeNje
npuLipNotifyNetDisconnect // ConnTypeTrunk
};
/*
** Table of functions that preset PCB's, indexed by connection type
*/
static void (*presetPcb[])(Pcb *pcbp) =
{
npuAsyncPresetPcb, // ConnTypeRaw
npuAsyncPresetPcb, // ConnTypePterm
npuAsyncPresetPcb, // ConnTypeRs232
npuAsyncPresetPcb, // ConnTypeTelnet
npuHaspPresetPcb, // ConnTypeHasp
npuHaspPresetPcb, // ConnTypeRevHasp
npuNjePresetPcb, // ConnTypeNje
npuLipPresetPcb // ConnTypeTrunk
};
/*
** Table of functions that process data received from network, indexed by connection type
*/
static void (*processUplineData[])(Pcb *pcbp) =
{
npuAsyncProcessUplineData, // ConnTypeRaw
npuAsyncProcessUplineData, // ConnTypePterm
npuAsyncProcessUplineData, // ConnTypeRs232
npuAsyncProcessTelnetData, // ConnTypeTelnet
npuHaspProcessUplineData, // ConnTypeHasp
npuHaspProcessUplineData, // ConnTypeRevHasp
npuNjeProcessUplineData, // ConnTypeNje
npuLipProcessUplineData // ConnTypeTrunk
};
/*
** Table of functions that reset PCB's, indexed by connection type
*/
static void (*resetPcb[])(Pcb *pcbp) =
{
npuAsyncResetPcb, // ConnTypeRaw
npuAsyncResetPcb, // ConnTypePterm
npuAsyncResetPcb, // ConnTypeRs232
npuAsyncResetPcb, // ConnTypeTelnet
npuHaspResetPcb, // ConnTypeHasp
npuHaspResetPcb, // ConnTypeRevHasp
npuNjeResetPcb, // ConnTypeNje
npuLipResetPcb // ConnTypeTrunk
};
/*
** Table of functions that attempt network output, indexed by connection type
*/
static void (*tryOutput[])(Pcb *pcbp) =
{
npuAsyncTryOutput, // ConnTypeRaw
npuAsyncTryOutput, // ConnTypePterm
npuAsyncTryOutput, // ConnTypeRs232
npuAsyncTryOutput, // ConnTypeTelnet
npuHaspTryOutput, // ConnTypeHasp
npuHaspTryOutput, // ConnTypeRevHasp
npuNjeTryOutput, // ConnTypeNje
npuLipTryOutput // ConnTypeTrunk
};
/*
** Function tables to interface to either CCP or CCI functions
*/
static bool (*svmIsReady[])() =
{
npuSvmIsReady,
cciSvmIsReady
};
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Register connection type
**
** Parameters: Name Description.
** tcpPort TCP port number for listening or client connections
** claPort Starting CLA port number on NPU
** numPorts Number of CLA ports on this TCP port
** connType Connection type (hasp/nje/pterm/raw/rhasp/rs232/telnet)
** ncbpp Pointer to Ncb pointer (return value)
**
** Returns: NpuNetRegOk: successfully registered
** NpuNetRegOvfl: too many connection types
** NpuNetRegDupTcp: duplicate TCP port specified
** NpuNetRegDupCla: duplicate CLA port specified
**
**------------------------------------------------------------------------*/
int npuNetRegisterConnType(int tcpPort, int claPort, int numPorts, int connType, Ncb **ncbpp)
{
int i;
Ncb *ncbp;
int status;
/*
** Check for too many registrations.
*/
if (numNcbs >= MaxTermDefs)
{
return NpuNetRegOvfl;
}
/*
** Check for duplicate TCP ports.
*/
if (tcpPort != 0)
{
for (i = 0; i < numNcbs; i++)
{
ncbp = &ncbs[i];
if (ncbp->tcpPort == tcpPort)
{
/*
** Different connection types may not share a port number.
** More than one NJE terminal definition may share the same
** port number, and more than one Trunk definition may share
** the same port number. All others must be unique.
*/
if ((ncbp->connType != connType)
|| ((connType != ConnTypeNje) && (connType != ConnTypeTrunk)))
{
return NpuNetRegDupTcp;
}
}
}
}
ncbp = &ncbs[numNcbs];
if (ncbpp != NULL)
{
*ncbpp = ncbp;
}
/*
** Register this port.
*/
ncbp->state = StConnInit;
ncbp->tcpPort = tcpPort;
ncbp->claPort = claPort;
ncbp->numPorts = numPorts;
ncbp->connType = connType;
ncbp->connFd = 0;
ncbp->lstnFd = 0;
ncbp->hostName = NULL;
ncbp->nextConnectionAttempt = getSeconds() + (time_t)NamStartupTime;
/*
** Register CLA ports associated with this connection and check for duplicates.
*/
status = npuNetRegisterClaPort(ncbp);
if (status != NpuNetRegOk)
{
return status;
}
numNcbs += 1;
return NpuNetRegOk;
}
/*--------------------------------------------------------------------------
** Purpose: Close the connection associated with a PCB
**
** Parameters: Name Description.
** pcbp pointer to PCB
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetCloseConnection(Pcb *pcbp)
{
Ncb *ncbp;
if ((pcbp != NULL) && (pcbp->connFd > 0))
{
netCloseConnection(pcbp->connFd);
ncbp = pcbp->ncbp;
if (ncbp != NULL)
{
if ((pcbp->connFd == ncbp->connFd) || (ncbp->state == StConnBusy))
{
ncbp->state = StConnInit;
ncbp->nextConnectionAttempt = getSeconds() + (time_t)ConnectionRetryInterval;
}
resetPcb[ncbp->connType](pcbp);
}
}
pcbp->connFd = 0;
}
/*--------------------------------------------------------------------------
** Purpose: Find the PCB for a CLA port number
**
** Parameters: Name Description.
** claPort CLA port number
**
** Returns: pointer to PCB, or NULL if PCB not found.
**
**------------------------------------------------------------------------*/
Pcb *npuNetFindPcb(int claPort)
{
if ((claPort >= 0) && (claPort < MaxClaPorts))
{
return &pcbs[claPort];
}
return NULL;
}
/*--------------------------------------------------------------------------
** Purpose: Set the current highest active connection number
**
** Parameters: Name Description.
** cn Connection number of connection just
** created or terminated
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetSetMaxCN(u8 cn)
{
Tcb *tp;
tp = &npuTcbs[cn];
if ((tp->state == StTermIdle) && (cn >= npuNetMaxCN))
{
while (cn > 0)
{
if (npuTcbs[cn].state != StTermIdle)
{
npuNetMaxCN = cn;
break;
}
cn--;
}
}
else if (cn > npuNetMaxCN)
{
npuNetMaxCN = cn;
}
}
/*--------------------------------------------------------------------------
** Purpose: Initialise network connection handler.
**
** Parameters: Name Description.
** startup FALSE when restarting (NAM restart),
** TRUE on first call during initialisation.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetInit(bool startup)
{
/*
** Setup for input data processing.
*/
pollIndex = 0;
/*
** Only do the following when the emulator starts up.
*/
if (startup)
{
/*
** Create the thread which will deal with TCP connections.
*/
npuNetCreateThread();
}
}
/*--------------------------------------------------------------------------
** Purpose: Preset network data structures during DtCyber
** initialization.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetPreset(void)
{
int i;
for (i = 0; i < MaxClaPorts; i++)
{
memset(&pcbs[i], 0, sizeof(Pcb));
pcbs[i].claPort = (u8)i;
}
}
/*--------------------------------------------------------------------------
** Purpose: Reset network connection handler when network is going
** down.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetReset(void)
{
int i;
Ncb *ncbp;
Pcb *pcbp;
Tcb *tp;
/*
** Iterate through all TCBs.
*/
for (i = npuNetMaxCN; i > 0; i--)
{
tp = &npuTcbs[i];
pcbp = tp->pcbp;
if ((tp->state != StTermIdle) && (pcbp != NULL) && (pcbp->connFd > 0))
{
/*
** Notify user that network is going down and then disconnect.
*/
ncbp = pcbp->ncbp;
if ((ncbp != NULL) && (ncbp->connType != ConnTypePterm)
&& (tp->deviceType == DtCONSOLE))
{
npuNetSendConsoleMsg(pcbp->connFd, ncbp->connType, networkDownMsg);
}
npuNetCloseConnection(pcbp);
tp->state = StTermIdle;
npuNetSetMaxCN(tp->cn);
}
}
/*
** Iterate over PCBs and close any remaining open non-listening connections.
*/
for (i = 0; i <= npuNetMaxClaPort; i++)
{
npuNetCloseConnection(&pcbs[i]);
}
}
/*--------------------------------------------------------------------------
** Purpose: Signal from host that connection has been established.
**
** Parameters: Name Description.
** tp TCB pointer
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetConnected(Tcb *tp)
{
if (tp->deviceType == DtCONSOLE)
{
npuNetSendConsoleMsg(tp->pcbp->connFd, tp->pcbp->ncbp->connType, connectedMsg);
}
}
/*--------------------------------------------------------------------------
** Purpose: Signal from host that connection has been terminated.
**
** Parameters: Name Description.
** tp TCB pointer
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetDisconnected(Tcb *tp)
{
if (tp->deviceType == DtCONSOLE)
{
/*
** Received disconnect - close socket.
*/
npuNetCloseConnection(tp->pcbp);
}
/*
** Cleanup connection.
*/
npuNetSetMaxCN(tp->cn);
npuLogMessage("(npu_net) Connection %02x dropped on port %d", tp->cn, tp->pcbp->claPort);
}
/*--------------------------------------------------------------------------
** Purpose: Prepare to send data to terminal.
**
** Parameters: Name Description.
** tp TCB pointer
** data data address
** len data length
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetSend(Tcb *tp, u8 *data, int len)
{
netSend[tp->pcbp->ncbp->connType](tp, data, len);
}
/*--------------------------------------------------------------------------
** Purpose: Store block sequence number to acknowledge when send
** has completed in last buffer.
**
** Parameters: Name Description.
** tp TCB pointer
** blockSeqNo block sequence number to acknowledge.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetQueueAck(Tcb *tp, u8 blockSeqNo)
{
NpuBuffer *bp;
/*
** Try to use the last pending buffer unless it carries a sequence number
** which must be acknowledged. If there is none, get a new one and queue it.
*/
bp = npuBipQueueGetLast(&tp->outputQ);
if ((bp == NULL) || (bp->blockSeqNo != 0))
{
bp = npuBipBufGet();
npuBipQueueAppend(bp, &tp->outputQ);
}
if (bp != NULL)
{
bp->blockSeqNo = blockSeqNo;
}
/*
** Try to output the data on the network connection.
*/
npuNetTryOutput(tp->pcbp);
}
/*--------------------------------------------------------------------------
** Purpose: Check for network status.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetCheckStatus(void)
{
Pcb *pcbp;
fd_set readFds;
int readySockets = 0;
struct timeval timeout;
fd_set writeFds;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
while (pollIndex <= npuNetMaxClaPort)
{
pcbp = &pcbs[pollIndex++];
if (pcbp->connFd <= 0)
{
continue;
}
if (pcbp->cciWaitForTcb)
{
if (getSeconds() - pcbp->cciTcbWaitStart > CciWaitForTcbTimeout)
{
npuNetSendConsoleMsg(pcbp->connFd, pcbp->ncbp->connType, tcbNotConfiguredMsg);
netCloseConnection(pcbp->connFd);
pcbp->connFd = 0;
pcbp->ncbp->state = StConnInit;
}
continue;
}
/*
** Handle network traffic.
*/
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
FD_SET(pcbp->connFd, &readFds);
readySockets = select(pcbp->connFd + 1, &readFds, NULL, NULL, &timeout);
if ((readySockets > 0) && FD_ISSET(pcbp->connFd, &readFds))
{
/*
** Receive a block of data.
*/
pcbp->inputCount = recv(pcbp->connFd, pcbp->inputData, MaxBuffer, 0);
if (pcbp->inputCount <= 0)
{
notifyNetDisconnect[pcbp->ncbp->connType](pcbp);
continue;
}
processUplineData[pcbp->ncbp->connType](pcbp);
}
if (pcbp->connFd > 0)
{
FD_ZERO(&writeFds);
FD_SET(pcbp->connFd, &writeFds);
readySockets = select(pcbp->connFd + 1, NULL, &writeFds, NULL, &timeout);
if ((readySockets > 0) && FD_ISSET(pcbp->connFd, &writeFds))
{
/*
** Try sending data if any is pending.
*/
npuNetTryOutput(pcbp);
}
}
/*
** The following return ensures that we resume with polling the next
** connection in sequence otherwise low-numbered connections would get
** preferential treatment.
*/
return;
}
pollIndex = 0;
}
/*--------------------------------------------------------------------------
** Purpose: Show status of NPU/MDI data communication (operator interface).
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetShowStatus()
{
u8 channelNo;
char chEqStr[10];
DevSlot *dp;
char *dts;
u8 eqNo;
int i;
u32 ipAddr;
Ncb *ncbp;
Pcb *pcbp;
char peerAddress[24];
u16 port;
char outBuf[200];
dp = NULL;
for (channelNo = 0; channelNo < MaxChannels; channelNo++)
{
dp = channelFindDevice(channelNo, DtMdi);
if (dp != NULL)
{
dts = "MDI ";
break;
}
dp = channelFindDevice(channelNo, DtNpu);
if (dp != NULL)
{
dts = "2550 ";
break;
}
dp = channelFindDevice(channelNo, DtHcp);
if (dp != NULL)
{
dts = "HCP ";
break;
}
}
if (dp == NULL) return;
sprintf(chEqStr, "C%02o E%02o", dp->channel->id, dp->eqNo);
for (i = 0; i < numNcbs; i++)
{
ncbp = &ncbs[i];
switch (ncbp->connType)
{
case ConnTypeRaw:
case ConnTypePterm:
case ConnTypeRs232:
case ConnTypeTelnet:
case ConnTypeHasp:
case ConnTypeNje:
case ConnTypeTrunk:
if (ncbp->lstnFd > 0)
{
sprintf(outBuf, " > %-8s %-7s "FMTNETSTATUS"\n", dts, chEqStr, netGetLocalTcpAddress(ncbp->lstnFd), "",
connTypes[ncbp->connType], "listening");
opDisplay(outBuf);
chEqStr[0] = '\0';
}
break;
case ConnTypeRevHasp:
ipAddr = ntohl(ncbp->hostAddr.sin_addr.s_addr);
port = ntohs(ncbp->hostAddr.sin_port);
sprintf(peerAddress, "%d.%d.%d.%d:%d",
(ipAddr >> 24) & 0xff,
(ipAddr >> 16) & 0xff,
(ipAddr >> 8) & 0xff,
ipAddr & 0xff,
port);
if (ncbp->state == StConnConnecting)
{
sprintf(outBuf, " > %-8s %-7s "FMTNETSTATUS"\n", dts, chEqStr, netGetLocalTcpAddress(ncbp->connFd),
peerAddress, connTypes[ncbp->connType], "connecting");
opDisplay(outBuf);
chEqStr[0] = '\0';
}
else if (ncbp->state != StConnConnected)
{
sprintf(outBuf, " > %-8s %-7s "FMTNETSTATUS"\n", dts, chEqStr, ipAddress, peerAddress,
connTypes[ncbp->connType], "disconnected");
opDisplay(outBuf);
chEqStr[0] = '\0';
}
break;
default:
break;
}
}
for (i = 0; i < MaxClaPorts; i++)
{
pcbp = &pcbs[i];
if (pcbp->ncbp != NULL && pcbp->connFd > 0)
{
sprintf(outBuf, " > %-8s %-7s P%02x "FMTNETSTATUS"\n", dts, chEqStr, pcbp->claPort, netGetLocalTcpAddress(pcbp->connFd),
netGetPeerTcpAddress(pcbp->connFd), connTypes[pcbp->ncbp->connType], connStates[pcbp->ncbp->state]),
opDisplay(outBuf);
chEqStr[0] = '\0';
}
}
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Register CLA port numbers and associated connection types
**
** Parameters: Name Description.
** ncbp pointer to network connection control block to be
** associated with the ports. The NCB provides the
** starting CLA port number, number of CLA ports, and
** connection type.
**
** Returns: NpuNetRegOk: successfully registered
** NpuNetRegOvfl: invalid CLA port number
** NpuNetRegNoMem: memory exhausted
** NpuNetRegDupCla: duplicate CLA port specified
**
**------------------------------------------------------------------------*/
static int npuNetRegisterClaPort(Ncb *ncbp)
{
int i;
int limit;
Pcb *pcbp;
if (isPcbsPreset == FALSE)
{
for (i = 0; i < MaxClaPorts; i++)
{
memset(&pcbs[i], 0, sizeof(Pcb));
}
isPcbsPreset = TRUE;
}
limit = ncbp->claPort + ncbp->numPorts;
if ((ncbp->claPort < 1) || (limit > MaxClaPorts))
{
return NpuNetRegOvfl;
}
for (i = ncbp->claPort; i < limit; i++)
{
pcbp = &pcbs[i];
if (pcbp->claPort == 0)
{
pcbp->claPort = i;
pcbp->ncbp = ncbp;
pcbp->inputData = (u8 *)malloc(MaxBuffer);
if (pcbp->inputData == NULL)
{
return NpuNetRegNoMem;
}
presetPcb[ncbp->connType](pcbp);
}
else
{
return NpuNetRegDupCla;
}
}
if (limit > npuNetMaxClaPort)
{
npuNetMaxClaPort = (u8)(limit - 1);
}
return NpuNetRegOk;
}
/*--------------------------------------------------------------------------
** Purpose: Send a message to a console device
**
** Parameters: Name Description.
** connFd Socket descriptor
** connType Connection type (raw/pterm/telnet/hasp/etc.)
** msg Pointer to message
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void npuNetSendConsoleMsg(int connFd, int connType, char *msg)
{
switch (connType)
{
case ConnTypeRaw:
case ConnTypePterm:
case ConnTypeRs232:
case ConnTypeTelnet:
send(connFd, msg, strlen(msg), 0);
break;
case ConnTypeHasp:
case ConnTypeRevHasp:
case ConnTypeNje:
case ConnTypeTrunk:
// discard messages to HASP/Reverse HASP/NJE/LIP
break;
}
}
/*--------------------------------------------------------------------------
** Purpose: Accepts connections pending on listening sockets.
**
** Parameters: Name Description.
** selectFds pointer to set of listening socket descriptors
** maxFd maximum socket descriptor value in the set
**
** Returns: number of connections accepted
**
**------------------------------------------------------------------------*/
static int npuNetAcceptConnections(fd_set *selectFds, int maxFd)
{
#if defined(_WIN32)
SOCKET acceptFd;
#else
int acceptFd;
#endif
fd_set acceptFds;
int i;
int n;
Ncb *ncbp;
int rc;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
memcpy(&acceptFds, selectFds, sizeof(fd_set));
rc = select(maxFd + 1, &acceptFds, NULL, NULL, &timeout);
if (rc < 0)
{
fprintf(stderr, "(npu_net) select returned unexpected %d\n", rc);
sleepMsec(1000);
}
else if (rc < 1)
{
return 0;
}
/*
** Find the listening socket(s) with pending connections and accept them.
*/
n = 0;
for (i = 0; i < numNcbs; i++)
{
ncbp = &ncbs[i];
switch (ncbp->connType)
{
case ConnTypeRaw:
case ConnTypePterm:
case ConnTypeRs232: