forked from k5mwe/DtCyber
-
Notifications
You must be signed in to change notification settings - Fork 1
/
npu_net.c
1014 lines (912 loc) · 27.2 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>
#include "const.h"
#include "types.h"
#include "proto.h"
#include "npu.h"
#include <sys/types.h>
#include <memory.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 <signal.h>
#endif
/*
** -----------------
** Private Constants
** -----------------
*/
#define Ms200 200000
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
/*
** Registered NPU connection types.
*/
typedef struct npuConnType
{
u16 tcpPort;
int numConns;
u8 connType;
Tcb *startTcb;
} NpuConnType;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static void npuNetCreateThread(void);
#if defined(_WIN32)
static void npuNetThread(void *param);
#else
static void *npuNetThread(void *param);
#endif
static void npuNetProcessNewConnection(int acceptFd, NpuConnType *ct);
static void npuNetQueueOutput(Tcb *tp, u8 *data, int len);
static void npuNetTryOutput(Tcb *tp);
/*
** ----------------
** Public Variables
** ----------------
*/
u16 npuNetTcpConns = 0;
/*
** -----------------
** Private Variables
** -----------------
*/
static char connectingMsg[] = "\r\nConnecting to host - please wait ...\r\n";
static char connectedMsg[] = "\r\nConnected\r\n\n";
static char abortMsg[] = "\r\nConnection aborted\r\n";
static char networkDownMsg[] = "Network 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 NpuConnType connTypes[MaxConnTypes];
static int numConnTypes = 0;
static int pollIndex = 0;
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Register connection type
**
** Parameters: Name Description.
** tcpPort TCP port number
** numConns Number of connections on this TCP port
** connType Connection type (raw/pterm/rs232)
**
** Returns: NpuNetRegOk: successfully registered
** NpuNetRegOvfl: too many connection types
** NpuNetRegDupl: duplicate TCP port specified
**
**------------------------------------------------------------------------*/
int npuNetRegister(int tcpPort, int numConns, int connType)
{
int i;
/*
** Check for too many registrations.
*/
if (numConnTypes >= MaxConnTypes)
{
return(NpuNetRegOvfl);
}
/*
** Check for duplicate TCP ports.
*/
for (i = 0; i < numConnTypes; i++)
{
if (connTypes[i].tcpPort == tcpPort)
{
return(NpuNetRegDupl);
}
}
/*
** Register this port.
*/
connTypes[numConnTypes].tcpPort = tcpPort;
connTypes[numConnTypes].numConns = numConns;
connTypes[numConnTypes].connType = connType;
numConnTypes += 1;
npuNetTcpConns += numConns;
return(NpuNetRegOk);
}
/*--------------------------------------------------------------------------
** 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)
{
int i;
int j;
int numConns;
u8 connType;
Tcb *tp;
/*
** Initialise network part of TCBs.
*/
tp = npuTcbs;
for (i = 0; i < npuNetTcpConns; i++, tp++)
{
tp->state = StTermIdle;
tp->connFd = 0;
}
/*
** Initialise connection type specific TCB values.
*/
tp = npuTcbs;
for (i = 0; i < numConnTypes; i++)
{
connTypes[i].startTcb = tp;
numConns = connTypes[i].numConns;
connType = connTypes[i].connType;
for (j = 0; j < numConns; j++, tp++)
{
tp->connType = connType;
}
}
/*
** Setup for input data processing.
*/
pollIndex = npuNetTcpConns;
/*
** Only do the following when the emulator starts up.
*/
if (startup)
{
/*
** Disable SIGPIPE which some non-Win32 platform generate on disconnect.
*/
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
/*
** Create the thread which will deal with TCP connections.
*/
npuNetCreateThread();
}
}
/*--------------------------------------------------------------------------
** Purpose: Reset network connection handler when network is going
** down.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetReset(void)
{
int i;
Tcb *tp = npuTcbs;
/*
** Iterate through all TCBs.
*/
for (i = 0; i < npuNetTcpConns; i++, tp++)
{
if (tp->state != StTermIdle)
{
/*
** Notify user that network is going down and then disconnect.
*/
send(tp->connFd, networkDownMsg, sizeof(networkDownMsg) - 1, 0);
#if defined(_WIN32)
closesocket(tp->connFd);
#else
close(tp->connFd);
#endif
tp->state = StTermIdle;
tp->connFd = 0;
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Signal from host that connection has been established.
**
** Parameters: Name Description.
** tp TCB pointer
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetConnected(Tcb *tp)
{
tp->state = StTermHostConnected;
send(tp->connFd, connectedMsg, sizeof(connectedMsg) - 1, 0);
}
/*--------------------------------------------------------------------------
** Purpose: Signal from host that connection has been terminated.
**
** Parameters: Name Description.
** tp TCB pointer
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetDisconnected(Tcb *tp)
{
/*
** Received disconnect - close socket.
*/
#if defined(_WIN32)
closesocket(tp->connFd);
#else
close(tp->connFd);
#endif
/*
** Cleanup connection.
*/
tp->state = StTermIdle;
npuLogMessage("npuNet: Connection dropped on port %d\n", tp->portNumber);
}
/*--------------------------------------------------------------------------
** 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)
{
u8 *p;
int count;
switch (tp->connType)
{
case ConnTypePterm:
/*
** Telnet escape processing as required by Pterm.
*/
for (p = data; len > 0; len -= 1)
{
switch (*p++)
{
case 0xFF:
/*
** Double FF to escape the Telnet IAC code making it a real FF.
*/
count = p - data;
npuNetQueueOutput(tp, data, count);
npuNetQueueOutput(tp, (u8 *)"\xFF", 1);
data = p;
break;
case 0x0D:
/*
** Append zero to CR otherwise real zeroes will be stripped by Telnet.
*/
count = p - data;
npuNetQueueOutput(tp, data, count);
npuNetQueueOutput(tp, (u8 *)"\x00", 1);
data = p;
break;
}
}
if ((count = p - data) > 0)
{
npuNetQueueOutput(tp, data, count);
}
break;
case ConnTypeRaw:
case ConnTypeRs232:
/*
** Standard (non-Telnet) TCP connection.
*/
npuNetQueueOutput(tp, data, len);
break;
}
}
/*--------------------------------------------------------------------------
** 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);
}
/*--------------------------------------------------------------------------
** Purpose: Check for network status.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void npuNetCheckStatus(void)
{
static fd_set readFds;
static fd_set writeFds;
struct timeval timeout;
int readySockets = 0;
Tcb *tp;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
while (pollIndex < npuNetTcpConns)
{
tp = npuTcbs + pollIndex++;
if (tp->state == StTermIdle)
{
continue;
}
/*
** Handle transparent input timeout.
*/
if (tp->xInputTimerRunning && (cycles - tp->xStartCycle) >= Ms200)
{
npuAsyncFlushUplineTransparent(tp);
}
/*
** Handle network traffic.
*/
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
FD_SET(tp->connFd, &readFds);
FD_SET(tp->connFd, &writeFds);
readySockets = select(tp->connFd + 1, &readFds, &writeFds, NULL, &timeout);
if (readySockets <= 0)
{
continue;
}
if (npuBipQueueNotEmpty(&tp->outputQ) && FD_ISSET(tp->connFd, &writeFds))
{
/*
** Send data if any is pending.
*/
npuNetTryOutput(tp);
}
if (FD_ISSET(tp->connFd, &readFds))
{
/*
** Receive a block of data.
*/
tp->inputCount = recv(tp->connFd, tp->inputData, sizeof(tp->inputData), 0);
if (tp->inputCount <= 0)
{
/*
** Received disconnect - close socket.
*/
#if defined(_WIN32)
closesocket(tp->connFd);
#else
close(tp->connFd);
#endif
npuLogMessage("npuNet: Connection dropped on port %d\n", tp->portNumber);
/*
** Notify SVM.
*/
npuSvmDiscRequestTerminal(tp);
}
else if (tp->state == StTermHostConnected)
{
/*
** Hand up to the ASYNC TIP.
*/
npuAsyncProcessUplineData(tp);
}
/*
** 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;
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Create thread which will deal with all TCP
** connections.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void npuNetCreateThread(void)
{
#if defined(_WIN32)
DWORD dwThreadId;
HANDLE hThread;
/*
** Create TCP thread.
*/
hThread = CreateThread(
NULL, // no security attribute
0, // default stack size
(LPTHREAD_START_ROUTINE)npuNetThread,
(LPVOID)NULL, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (hThread == NULL)
{
fprintf(stderr, "Failed to create npuNet thread\n");
exit(1);
}
#else
int rc;
pthread_t thread;
pthread_attr_t attr;
/*
** Create POSIX thread with default attributes.
*/
pthread_attr_init(&attr);
rc = pthread_create(&thread, &attr, npuNetThread, NULL);
if (rc < 0)
{
fprintf(stderr, "Failed to create npuNet thread\n");
exit(1);
}
#endif
}
/*--------------------------------------------------------------------------
** Purpose: TCP network connection thread.
**
** Parameters: Name Description.
** param unused
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
#if defined(_WIN32)
static void npuNetThread(void *param)
#else
static void *npuNetThread(void *param)
#endif
{
int rc;
static fd_set selectFds;
static fd_set acceptFds;
int listenFd[MaxConnTypes];
int acceptFd;
int maxFd = 0;
struct sockaddr_in server;
struct sockaddr_in from;
int i;
int optEnable = 1;
#if defined(_WIN32)
int fromLen;
u_long blockEnable = 1;
#else
socklen_t fromLen;
#endif
FD_ZERO(&selectFds);
/*
** Create a listening socket for every configured connection type.
*/
for (i = 0; i < numConnTypes; i++)
{
/*
** Create TCP socket and bind to specified port.
*/
listenFd[i] = socket(AF_INET, SOCK_STREAM, 0);
if (listenFd[i] < 0)
{
fprintf(stderr, "npuNet: Can't create socket\n");
#if defined(_WIN32)
return;
#else
return(NULL);
#endif
}
/*
** Accept will block if client drops connection attempt between select and accept.
** We can't block so make listening socket non-blocking to avoid this condition.
*/
#if defined(_WIN32)
ioctlsocket(listenFd[i], FIONBIO, &blockEnable);
#else
fcntl(listenFd[i], F_SETFL, O_NONBLOCK);
#endif
/*
** Bind to configured TCP port number
*/
setsockopt(listenFd[i], SOL_SOCKET, SO_REUSEADDR, (void *)&optEnable, sizeof(optEnable));
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("0.0.0.0");
server.sin_port = htons(connTypes[i].tcpPort);
if (bind(listenFd[i], (struct sockaddr *)&server, sizeof(server)) < 0)
{
fprintf(stderr, "npuNet: Can't bind to socket\n");
#if defined(_WIN32)
return;
#else
return(NULL);
#endif
}
/*
** Start listening for new connections on this TCP port number
*/
if (listen(listenFd[i], 5) < 0)
{
fprintf(stderr, "npuNet: Can't listen\n");
#if defined(_WIN32)
return;
#else
return(NULL);
#endif
}
/*
** Determine highest FD for later select
*/
if (maxFd < listenFd[i])
{
maxFd = listenFd[i];
}
/*
** Add to set of listening FDs for later select
*/
FD_SET(listenFd[i], &selectFds);
}
for (;;)
{
/*
** Wait for a connection on all sockets for the configured connection types.
*/
memcpy(&acceptFds, &selectFds, sizeof(selectFds));
rc = select(maxFd + 1, &acceptFds, NULL, NULL, NULL);
if (rc <= 0)
{
fprintf(stderr, "npuNetThread: select returned unexpected %d\n", rc);
#if defined(_WIN32)
Sleep(1000);
#else
sleep(1);
#endif
continue;
}
/*
** Find the listening socket(s) with pending connections and accept them.
*/
for (i = 0; i < numConnTypes; i++)
{
if (FD_ISSET(listenFd[i], &acceptFds))
{
fromLen = sizeof(from);
acceptFd = accept(listenFd[i], (struct sockaddr *)&from, &fromLen);
if (acceptFd == -1)
{
printf("npuNetThread: spurious connection attempt\n");
continue;
}
npuNetProcessNewConnection(acceptFd, connTypes + i);
}
}
}
#if !defined(_WIN32)
return(NULL);
#endif
}
/*--------------------------------------------------------------------------
** Purpose: Process new TCP connection
**
** Parameters: Name Description.
** acceptFd New connection's FD
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void npuNetProcessNewConnection(int acceptFd, NpuConnType *ct)
{
u8 i;
Tcb *tp;
int optEnable = 1;
#if defined(_WIN32)
u_long blockEnable = 1;
#endif
/*
** Set Keepalive option so that we can eventually discover if
** a client has been rebooted.
*/
setsockopt(acceptFd, SOL_SOCKET, SO_KEEPALIVE, (void *)&optEnable, sizeof(optEnable));
/*
** Make socket non-blocking.
*/
#if defined(_WIN32)
ioctlsocket(acceptFd, FIONBIO, &blockEnable);
#else
fcntl(acceptFd, F_SETFL, O_NONBLOCK);
#endif
/*
** Check if the host is ready to accept connections.
*/
if (!npuSvmIsReady())
{
/*
** Tell the user.
*/
send(acceptFd, notReadyMsg, sizeof(notReadyMsg) - 1, 0);
/*
** Wait a bit and then disconnect.
*/
#if defined(_WIN32)
Sleep(2000);
closesocket(acceptFd);
#else
sleep(2);
close(acceptFd);
#endif
return;
}
/*
** Find a free TCB in the set of ports associated with this connection type.
*/
tp = ct->startTcb;
for (i = 0; i < ct->numConns; i++)
{
if (tp->state == StTermIdle)
{
break;
}
tp += 1;
}
/*
** Did we find a free TCB?
*/
if (i == ct->numConns)
{
/*
** No free port found - tell the user.
*/
send(acceptFd, noPortsAvailMsg, sizeof(noPortsAvailMsg) - 1, 0);
/*
** Wait a bit and then disconnect.
*/
#if defined(_WIN32)
Sleep(2000);
closesocket(acceptFd);
#else
sleep(2);
close(acceptFd);
#endif
return;
}
/*
** Mark connection as active.
*/
tp->connFd = acceptFd;
tp->state = StTermNetConnected;
npuLogMessage("npuNet: Received connection on port %u\n", tp->portNumber);
/*
** Notify user of connect attempt.
*/
send(tp->connFd, connectingMsg, sizeof(connectingMsg) - 1, 0);
/*
** Attempt connection to host.
*/
if (!npuSvmConnectTerminal(tp))
{
/*
** No buffers, notify user.
*/
send(tp->connFd, abortMsg, sizeof(abortMsg) - 1, 0);
/*
** Wait a bit and then disconnect.
*/
#if defined(_WIN32)
Sleep(1000);
closesocket(tp->connFd);
#else
sleep(1);
close(tp->connFd);
#endif
tp->state = StTermIdle;
}
}
/*--------------------------------------------------------------------------
** Purpose: Queue output to terminal and do basic Telnet formatting.
**
** Parameters: Name Description.
** tp TCB pointer
** data data address
** len data length
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void npuNetQueueOutput(Tcb *tp, u8 *data, int len)
{
NpuBuffer *bp;
u8 *startAddress;
int byteCount;
/*
** 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);
}
while (bp != NULL && len > 0)
{
/*
** Append data to the buffer.
*/
startAddress = bp->data + bp->offset + bp->numBytes;
byteCount = MaxBuffer - bp->offset - bp->numBytes;
if (byteCount >= len)
{
byteCount = len;
}
memcpy(startAddress, data, byteCount);
bp->numBytes += byteCount;
/*
** If there is still data left get a new buffer, queue it and
** copy what is left.
*/
len -= byteCount;
if (len > 0)
{
bp = npuBipBufGet();
npuBipQueueAppend(bp, &tp->outputQ);
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Try to send any queued data.
**
** Parameters: Name Description.
** tp TCB pointer
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void npuNetTryOutput(Tcb *tp)
{
NpuBuffer *bp;
u8 *data;
int result;
/*
** Return if we are flow controlled.
*/
if (tp->xoff)
{
return;
}
/*
** Process all queued output buffers.
*/
while ((bp = npuBipQueueExtract(&tp->outputQ)) != NULL)
{
data = bp->data + bp->offset;
/*
** Don't call into TCP if there is no data to send.
*/
if (bp->numBytes > 0)
{
result = send(tp->connFd, data, bp->numBytes, 0);
}
else
{
result = 0;
}
if (result >= bp->numBytes)
{
/*
** The socket took all our data - let TIP know what block sequence
** number we processed, free the buffer and then continue.
*/
if (bp->blockSeqNo != 0)
{
npuTipNotifySent(tp, bp->blockSeqNo);
}
npuBipBufRelease(bp);
continue;
}
/*
** Not all has been sent. Put the buffer back into the queue.
*/
npuBipQueuePrepend(bp, &tp->outputQ);
/*
** Was there an error?
*/
if (result < 0)
{
/*
** Likely this is a "would block" type of error - no need to do
** anything here. The select() call will later tell us when we
** can send again. Any disconnects or other errors will be handled
** by the receive handler.
*/
return;