-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp.c
2391 lines (2140 loc) · 74.8 KB
/
tcp.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
/* File MSNTCP.C
* Main TCP protocol code
*
* Copyright (C) 1991, University of Waterloo.
* Copyright (C) 1982, 1999, Trustees of Columbia University in the
* City of New York. The MS-DOS Kermit software may not be, in whole
* or in part, licensed or sold for profit as a software product itself,
* nor may it be included in or distributed with commercial products
* or otherwise distributed by commercial concerns to their clients
* or customers without written permission of the Office of Kermit
* Development and Distribution, Columbia University. This copyright
* notice must not be removed, altered, or obscured.
*
* Original version created by Erick Engelke of the University of
* Waterloo, Waterloo, Ontario, Canada.
* Adapted and redesigned for MS-DOS Kermit by Joe R. Doupnik,
* Utah State University, [email protected], [email protected].
*
* Last edit
* 12 Jan 1995 v3.14
* Class edition: 2 Feb 1996
*
*/
#include "tcp.h"
#include "netlibc.h"
extern int arp_handler(in_Header *);
extern int rarp_handler(void *);
extern int icmp_handler(void *);
extern void icmp_noport(void *);
extern void server_hello(tcp_Socket *s);
extern void eth_release(void);
extern void icmp_init(void);
extern int eth_init(void);
extern word tcp_status;
extern void krto(word);
extern void end_bootp(void);
extern int DHCP_refresh(void);
extern int request_busy; /* msnbtp.c, DHCP request() lock */
extern int odi_busy(void);
static int tcp_handler(in_Header *);
static int tcp_rst(in_Header *, tcp_Header FAR *);
static int tcp_read(tcp_Socket *, byte FAR *, int);
static int tcp_write(tcp_Socket *, byte FAR *, int);
static int tcp_close(tcp_Socket *);
static int tcp_processdata(tcp_Socket *, tcp_Header FAR *, int);
static int tcp_send(tcp_Socket *);
static int tcp_retransmitter(void);
static int tcp_unthread(tcp_Socket *);
static int udp_handler(in_Header *);
static int udp_read(udp_Socket *, byte FAR *, int);
static int udp_write(udp_Socket *, byte FAR *, int);
static int udp_close(udp_Socket *);
static void lost_ack(tcp_Socket *);
static void new_rto(tcp_Socket *, int);
longword ipbcast = 0xffffffffL; /* default IP broadcast address */
static initialized = 0; /* if have started the stack */
static imposter = 0; /* ARP for own IP toggle */
extern byte kdebug; /* non-zero if debug mode is active */
extern word ktcpmss; /* MSS override */
/*
* Local IP address
*/
longword my_ip_addr = 0L; /* for external references */
longword sin_mask = 0xfffffe00L; /* IP subnet mask */
static int ip_id = 0; /* IP packet number */
static int next_tcp_port = 1024; /* auto incremented */
static int next_udp_port = 1024;
static tcp_Socket *tcp_allsocs = NULL; /* TCP socket linked list head */
static udp_Socket *udp_allsocs = NULL; /* UDP socket linked list head */
#define tcp_FlagFIN 0x0001
#define tcp_FlagSYN 0x0002
#define tcp_FlagRST 0x0004
#define tcp_FlagPUSH 0x0008
#define tcp_FlagACK 0x0010
#define tcp_FlagURG 0x0020
#define tcp_FlagDO 0xF000
#define tcp_GetDataOffset(tp) (ntohs((tp)->flags) >> 12)
/* IP More Fragments header bit, in network order */
#define IP_MF 0x2000
/* Timer definitions */
#define tcp_LONGTIMEOUT 13 /* timeout, sec, for opens */
#define tcp_TIMEOUT 13 /* timeout, sec, during a connection */
#define TCP_SBUFSIZE 4096 /* max bytes to buffer in a tcp socket */
#define TCP_RBUFSIZE 4096
#define UDP_BUFSIZE ETH_MSS /* max bytes to buffer in a udp socket */
#define UDP_LENGTH (sizeof(udp_Header))
#define CONNECTION_REJECTED 9 /* a returnable status */
#define tcp_NOSEND 0 /* for tcp send_kind */
#define tcp_SENDNOW 1 /* repeat sending anything */
#define tcp_SENDACK 2 /* send an ACK even if our data is blocked */
word mss = ETH_MSS; /* Maximum Segment Size */
static word do_window_probe = 0; /* to probe closed windows in tcp_send() */
longword start_time; /* debugging, time first session began */
#define in_GetVersion(ip) ((ip)->hdrlen_ver >> 4)
#define in_GetHdrlen(ip) ((ip)->hdrlen_ver & 0xf) /* 32 bit word size */
#define in_GetHdrlenBytes(ip) (in_GetHdrlen(ip) << 2) /* 8 bit byte size */
/* Start reassembly section */
static in_Header * reasm(in_Header *);
static int use_reasmbuf; /* 0 = not doing fragmented IP datagram */
/* else is 1 + slot number to clean later*/
#define MAXSLOTS 4 /* number of datagrams in progress */
#define FRAGDATALEN (576-20) /* size of each datagram */
typedef struct {
int next; /* must be first element */
int first; /* offsets from slot->ipdata[0] */
int last;
} Hole;
/* Fragmentation buffer is organized as MAXSLOTS slots, each of which is
FRAGDATALEN + 20 IP header plus three words of local overhead.
*/
struct reasmstruct {
longword frag_tmo; /* reassembly timeout, 0 = unused slot */
int hole_count; /* count of holes in datagram */
in_Header iphdr; /* IP header, 20 bytes */
byte ipdata[FRAGDATALEN]; /* IP data, 556 bytes */
} reasmbuf[MAXSLOTS], * slot;
/* End reassembly section */
/* Network order diagrams
IP header
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
UDP header
0 7 8 15 16 23 24 31
+--------+--------+--------+--------+
| Source | Destination |
| Port | Port |
+--------+--------+--------+--------+
| | |
| Length | Checksum |
+--------+--------+--------+--------+
|
| data octets ...
+---------------- ...
TCP Header
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
/*
* tcp_init - Initialize the tcp implementation
* - may be called more than once without hurting
*/
int
tcp_init(void)
{
if (initialized != 0) return (1); /* success, inited already */
/* initialize TCP/IP stack */
icmp_init(); /* clear ICMP material */
request_busy = 0; /* clear DHCP request() logic */
last_nameserver = 0; /* reset the name server table */
arp_last_gateway = 0; /* clear old gateway info */
tcp_allsocs = NULL; /* zero socket pointers */
udp_allsocs = NULL;
ip_id = (int)(set_timeout(0) & 0xffff);/* packet number, convenient */
use_reasmbuf = 0; /* 0 = not using IP reassembly buf */
memset(reasmbuf, 0, sizeof (reasmbuf));
imposter = 0; /* ARP for own IP flag */
initialized = eth_init(); /* init the hardware, can fail */
/* next port numbers to choose */
next_udp_port = next_tcp_port = 1024 + (int)(set_timeout(0) & 0x1ff);
eth_free (NULL); /* clear all pkt rcv bufs */
return (initialized); /* success (1) or failure (0) */
}
/*
* Shut down the data link and all services
*/
void
tcp_shutdown(void)
{
if (initialized)
{
while (tcp_abort(tcp_allsocs)) ; /* stop each TCP session */
end_bootp(); /* release DHCP IP lease */
}
initialized = 0;
}
int
udp_open(udp_Socket *s, word lport, longword ina, word port)
{
if (s == NULL) return (0);
if (imposter == 0 && arp_resolve(my_ip_addr, NULL))/* imposter check */
{
outs("\r\n WARNING: our IP address is used by");
outs(" another station! Quitting.");
return(0); /* fail back to user */
}
imposter++; /* say have done the check */
if (s->rdata) free(s->rdata); /* free any allocated buffer*/
memset(s, 0, sizeof(udp_Socket));
if (lport == 0) lport = ++next_udp_port; /* get a nonzero port val */
s->myport = lport;
/* check for broadcast */
if (ina == 0xffffffffL || ina == 0L || ina == ipbcast)
memset(s->hisethaddr, 0xff, sizeof(eth_address));
else
if (arp_resolve(ina, &s->hisethaddr[0]) == 0)
return (0); /* fail */
if ((s->rdata = malloc(UDP_BUFSIZE + sizeof(udp_Header))) == NULL)
return (0); /* fail */
s->hisaddr = ina;
s->hisport = port;
s->ip_type = UDP_PROTO;
s->next = udp_allsocs;
udp_allsocs = s;
s->sock_mode = UDP_MODE_CHKSUM; /* turn on checksums */
s->sisopen = SOCKET_OPEN;
return (1); /* success */
}
/*
* Actively open a TCP connection to a particular destination.
* return 0 on error
*/
int
tcp_open(tcp_Socket *s, word lport, longword ina, word port)
{
if (s == NULL) return (0);
if (s->sisopen != SOCKET_CLOSED)
return (0);
if (imposter == 0 && arp_resolve(my_ip_addr, NULL))/* imposter check */
{
outs("\r\n WARNING: our IP address is used by");
outs(" another station! Quitting.");
return(0); /* fail back to user */
}
imposter++; /* say have done the check */
if (s->rdata) free(s->rdata); /* free preallocated buf */
if (s->sdata) free(s->sdata); /* free preallocated buf */
memset(s, 0, sizeof(tcp_Socket)); /* zero everything */
s->mss = mss; /* hardware limit */
if ((ina ^ my_ip_addr) & sin_mask) /* if not on this network */
s->mss = (mss > 536)? 536: mss; /* non-fragmentable max-40 */
if (ktcpmss < s->mss) /* user override */
s->mss = ktcpmss; /* is an upper limit */
if (lport == 0)
lport = ++next_tcp_port; /* get a nonzero port value */
s->myport = lport;
if (arp_resolve(ina, &s->hisethaddr[0]) == 0)
return (0); /* failed to get host Ethernet addr */
/* create data bufs */
if ((s->rdata = malloc(TCP_RBUFSIZE + sizeof(tcp_Header))) == NULL)
return (0); /* fail */
if ((s->sdata = malloc(TCP_SBUFSIZE)) == NULL)
{
free(s->rdata);
s->rdata = NULL;
return (0);
}
s->next = tcp_allsocs;
tcp_allsocs = s;
s->hisaddr = ina;
s->hisport = port;
s->seqnum = ntohl(set_timeout(0)) >> 16;
s->flags = tcp_FlagSYN;
s->state = tcp_StateSYNSENT;
s->ip_type = TCP_PROTO;
s->sisopen = SOCKET_OPEN;
s->sock_mode = TCP_MODE_NAGLE; /* Nagle slow start */
s->rto = 18 * 4; /* assume four seconds */
s->cwindow = s->mss; /* slow start VJ algorithm */
s->ssthresh = TCP_RBUFSIZE;
s->timeout = set_timeout(tcp_LONGTIMEOUT); /* seconds */
start_time = set_ttimeout(0); /* log time reference */
return (tcp_send(s)); /* fail if send fails */
}
/*
* Passive open: listen for a connection on a particular port
*/
tcp_listen(tcp_Socket *s, word lport, longword ina, word port, word timeout)
{
if (s == NULL) return (0);
if (s->rdata) free(s->rdata); /* free preallocated buf */
if (s->sdata) free(s->sdata); /* free preallocated buf */
memset(s, 0, sizeof(tcp_Socket)); /* zero everything */
s->ip_type = TCP_PROTO;
s->mss = mss; /* hardware limit */
s->cwindow = s->mss; /* slow start VJ algorithm */
s->rto = 18 * 4; /* assume four seconds */
s->state = tcp_StateLISTEN;
if (timeout != 0)
s->timeout = set_timeout(timeout); /* seconds */
s->myport = lport;
s->hisport = port;
s->hisaddr = ina;
s->seqnum = ntohl(set_timeout(0)) >> 16;
if ((s->rdata = malloc(TCP_RBUFSIZE + sizeof(tcp_Header))) == NULL)
return (0); /* fail */
if ((s->sdata = malloc(TCP_SBUFSIZE)) == NULL)
{
free(s->rdata);
s->rdata = NULL;
return (0);
}
s->next = tcp_allsocs; /* active socket list */
tcp_allsocs = s; /* remember active socket */
s->ip_type = TCP_PROTO; /* we speak TCP here */
s->sisopen = SOCKET_OPEN; /* socket is open */
s->sock_mode = TCP_MODE_PASSIVE + TCP_MODE_NAGLE;
s->send_kind = tcp_NOSEND; /* say no pending sends */
timeout = 0;
return (1);
}
static int
udp_close(udp_Socket *ds)
{
register udp_Socket *sp;
sp = udp_allsocs; /* ptr to first socket */
if (sp == NULL) return (0); /* failure */
ds->sisopen = SOCKET_CLOSED; /* say socket is closed, do first */
if (sp == ds)
udp_allsocs = ds->next; /* if we are first, unlink */
while (sp != NULL)
{
if (sp->next == ds) /* if current points to us */
{
sp->next = ds->next; /* point it to our successor */
break;
}
sp = sp->next; /* look at next socket */
}
if (ds->rdata != NULL)
{
free(ds->rdata); /* free received data buffer */
ds->rdata = NULL; /* invalidate data buffer address */
}
ds->rdatalen = 0; /* flush pending reads */
return (1); /* success */
}
/*
* Send a FIN on a particular port -- only works if it is open
* Must still allow receives
* Returns 0 for failure
*/
static int
tcp_close(tcp_Socket *s)
{
if (s == NULL || s->ip_type != TCP_PROTO)
return (0); /* failure */
s->sdatalen = s->sdatahwater; /* cannot send new data now */
if (s->state & (tcp_StateESTAB | tcp_StateSYNREC | tcp_StateCLOSWT))
{
if (kdebug & DEBUG_STATUS) outs("FIN sent, closing\r\n");
if (s->state == tcp_StateCLOSWT)
s->state = tcp_StateLASTACK;
else
s->state = tcp_StateFINWT1;
s->flags = tcp_FlagACK | tcp_FlagFIN;
s->timeout = set_timeout(tcp_TIMEOUT); /* added */
s->send_kind = tcp_SENDACK; /* sending our FIN */
tcp_send(s);
return (1); /* success */
}
s->state = tcp_StateCLOSED;
tcp_unthread(s); /* remove socket from list */
return (0);
}
/*
* Abort a tcp connection
* Returns 0 if failure
*/
int
tcp_abort(tcp_Socket *s)
{
if (s == NULL) return (0); /* failure */
if (kdebug & DEBUG_STATUS) outs("TCP_ABORT\r\n");
if ((s->state != tcp_StateLISTEN) && (s->state != tcp_StateCLOSED))
{
s->flags = tcp_FlagRST | tcp_FlagACK;
s->send_kind = tcp_SENDACK; /* force a response */
tcp_send(s);
}
s->sdatalen = 0;
s->state = tcp_StateCLOSED;
tcp_unthread(s);
return (1); /* success */
}
/*
* Retransmitter - called periodically to perform tcp retransmissions
* Returns 0 if failure
*/
static int
tcp_retransmitter(void)
{
register tcp_Socket *s;
register int i;
for (s = tcp_allsocs; s != NULL; s = s->next)
{
/* Trigger to send, s->send_kind, from functions, */
/* or there is data not yet sent */
if ((s->send_kind != tcp_NOSEND) ||
(s->sdatalen - s->sdatahwater))
tcp_send(s);
#ifdef DELAY_ACKS
/* If outgoing ACK is in delay queue */
if (s->delayed_ack &&
chk_timeout(s->delayed_ack) == TIMED_OUT)
{ /* if timed out send ACK now */
s->delayed_ack = 0;
s->send_kind |= tcp_SENDACK;
tcp_send(s);
}
#endif /* DELAY_ACKS */
/* Check for timeout of returned ACKs */
for (i = 0; i < s->sendqnum; i++) /* waiting for ACK */
if (chk_timeout(s->sendq[i].timeout) == TIMED_OUT)
{
lost_ack(s); /* lost ACK actions */
break; /* exit sendq for-loop */
}
/* If probing closed window, or sending keepalive check */
if (s->winprobe ||
(s->keepalive && (chk_timeout(s->keepalive) == TIMED_OUT)))
{
do_window_probe++; /* window probe */
tcp_send(s); /* send probe */
}
/* if no transmission for idle ticks */
if (s->idle && (chk_timeout(s->idle) == TIMED_OUT))
s->cwindow = s->mss; /* use slow start */
/* IP reassembly buffer timeout checks */
for (i = 0; i < MAXSLOTS; i++)
if (reasmbuf[i].frag_tmo &&
chk_timeout(reasmbuf[i].frag_tmo) == TIMED_OUT)
reasmbuf[i].frag_tmo = 0; /* empty the slot */
/* TCP machine states which can timeout via variable s->timeout */
if (s->timeout && (chk_timeout(s->timeout) == TIMED_OUT))
switch (s->state)
{
case tcp_StateSYNREC:
case tcp_StateSYNSENT:
tcp_close(s); /* graceful close */
break;
case tcp_StateTIMEWT:
s->state = tcp_StateCLOSED;
tcp_unthread(s); /* purge it */
break;
default:
tcp_abort(s);
break;
} /* end of switch */
} /* foot of for loop */
if (DHCP_refresh() == -1) /* if failed to refresh lease */
{ /* create no new output from here on, else loops */
tcp_shutdown(); /* end all traffic */
return (0); /* failure */
}
return (1); /* success */
}
/*
* Unthread a socket from the socket list, if it's there. Return 0 on failure
*/
static int
tcp_unthread(tcp_Socket *ds)
{
register tcp_Socket *sp;
if (ds == NULL) return (0); /* failure */
ds->sisopen = SOCKET_CLOSED; /* say socket is closed */
if ((sp = tcp_allsocs) == NULL) /* no socket in the queue */
return (0);
if (sp == ds)
tcp_allsocs = ds->next; /* if we are first, unlink */
while (sp != NULL)
{
if (sp->next == ds) /* if current points to us */
{
sp->next = ds->next; /* point it to our successor */
break;
}
sp = sp->next; /* look at next socket */
}
return (1); /* success */
}
/*
* tcp_tick - called periodically by user application
* - returns 0 when our socket closes
* - called with socket parameter or NULL
*/
tcp_tick(sock_type *s)
{
in_Header *ip;
in_Header * temp_ip;
int packettype;
/* read a packet */
while ((ip = (in_Header *)eth_arrived(&packettype)) != NULL)
{
switch (packettype) /* network big endian form */
{
case TYPE_IP: /* do IP */
if ((in_GetVersion(ip) != 4) ||
(checksum(ip, in_GetHdrlenBytes(ip)) != 0xffff))
{
if (kdebug & DEBUG_STATUS)
outs("IP Received BAD Checksum \r\n");
break;
}
if ((my_ip_addr == 0L) ||
(htonl(ip->destination) == my_ip_addr))
{
if ((temp_ip = reasm(ip)) == NULL)
break; /* reassembly in progress, wait */
if (use_reasmbuf > 0)
{ /* reassembly completed, process */
eth_free(ip); /* free lan adapter queue */
ip = temp_ip; /* look at reassembly buf */
}
switch (ip->proto & 0xff)
{
case TCP_PROTO:
tcp_handler(ip);
break;
case UDP_PROTO:
udp_handler(ip);
break;
case ICMP_PROTO:
icmp_handler(ip);
break;
default:
break;
} /* end of switch (ip->proto) */
}
break;
case TYPE_ARP: /* do ARP */
arp_handler(ip);
break;
case TYPE_RARP: /* do RARP */
rarp_handler(ip);
break;
default: /* unknown type */
break;
} /* end of switch */
if (use_reasmbuf > 0) /* if processing from reassembly buffer */
{
reasmbuf[use_reasmbuf - 1].frag_tmo = 0; /* clear buffer */
use_reasmbuf = 0; /* done using it */
temp_ip = NULL;
}
else
eth_free(ip); /* free the processed packet */
} /* end of while */
if (tcp_retransmitter() == 0)
return (0); /* check on pending sends */
return ((s != NULL)? s->tcp.sisopen: 0); /* 0 means closed socket */
}
/* IP fragment reassembly process. Enter with packet IP pointer, ip.
Return that IP pointer if packet is not fragmented.
Return NULL if do not process packet further (such as building pieces).
Return new IP pointer for reassembled packet, pointing to a reasmbuf.
Global variable use_reasmbuf is 0 to not use reassembly results, else
is 1 + index of reasmbuf to be cleared after use.
Global variables reasmbuf[..]->frag_tmo hold buffer timeout (Bios ticks);
clear these to dis-use the buffer.
*/
in_Header *
reasm(in_Header *ip)
{
int ip_hlen, frag, frag_first, frag_last, frag_len;
Hole * previous;
register int i;
register Hole * hole;
use_reasmbuf = 0; /* assume not finishing IP datagram */
/* Fragmented datagrams have either More Frags flag bit set or the
fragment offset field non-zero. Unfragmented datagrams have both clear.
*/
if ((frag = ntohs(ip->frag) & 0x3fff) == 0) /* frag flags and offset */
return (ip); /* not fragmented */
/* See if this fragment has a reassembly buffer assigned. Match on protocol,
IP identification, source IP, and if not allocated a timeout.
*/
for (i = 0; i < MAXSLOTS; i++)
{
slot = &reasmbuf[i];
use_reasmbuf = i + 1; /* assume finishing IP datagram */
if ((slot->iphdr.identification == ip->identification) &&
(slot->iphdr.source == ip->source) &&
(slot->iphdr.proto == ip->proto) &&
(slot->frag_tmo != 0))
goto include; /* have some pieces already */
}
for (i = 0; i < MAXSLOTS; i++) /* find an empty slot for new reasm */
{
slot = &reasmbuf[i];
if (slot->frag_tmo == 0) /* if slot is unused */
break;
}
if (i >= MAXSLOTS)
{
use_reasmbuf = 0;
return (NULL); /* insufficient space, discard pkt */
}
slot->frag_tmo = set_timeout(1 + ip->ttl >> 2); /* timeout from TTL */
bcopy((byte *)ip, &slot->iphdr, 20); /* copy basic IP header */
hole = (Hole *)&slot->ipdata[0]; /* first hole struct goes here */
previous = (Hole *)&slot->iphdr.checksum;
previous->next = 0; /* pointer to first hole struct */
hole->first = hole->next = 0;
hole->last = FRAGDATALEN; /* infinity */
slot->hole_count = 1; /* count of holes, just initial one */
/* Move data into the existing reassembly buffer */
include:
ip_hlen = in_GetHdrlenBytes(ip); /* IP header length */
frag_len = ntohs(ip->length) - ip_hlen; /* bytes of IP data */
frag_first = (frag & 0x1fff) << 3; /* offset of first data byte*/
frag_last = frag_first + frag_len - 1; /* offset of last data byte */
/* if More Frags bit is clear then have end of
/* datagram and hence can get datagram length */
if ((frag & IP_MF) == 0) /* if More Frags is clear */
slot->iphdr.length = ntohs(frag_last + ip_hlen + 1);
previous = (Hole *)&slot->iphdr.checksum; /* first hole */
hole = (Hole *)&slot->ipdata[previous->next];
/* find hole where at least one edge fits */
for ( i = 0; i < slot->hole_count; i++)
if (frag_first > hole->last || frag_last < hole->first)
{ /* does not go into this hole, look at next */
previous = hole; /* remember backward */
hole = (Hole *)&slot->ipdata[hole->next];
}
else
break; /* goes in hole, figure where below */
if (i >= slot->hole_count) /* exhausted search for opening */
{
slot->frag_tmo = 0;
use_reasmbuf = 0;
return (NULL); /* Failed, no slot matching offset */
}
slot->hole_count--; /* delete current hole pointer */
previous->next = hole->next; /* forward link over this hole */
if (frag_first > hole->first) /* leaves empty beginning */
{
/* recreate forward pointer to this new hole */
previous->next = hole->first; /* point to this hole */
hole->last = frag_first - 1; /* end of this hole */
slot->hole_count++;
/* move view to next hole for trailing edge test */
if (hole->last < FRAGDATALEN) /* if so there is another */
{
previous = hole;
hole = (Hole *)&slot->ipdata[hole->next];
}
}
if ((frag & IP_MF) && /* if More Frags bit is set */
(frag_last < hole->last)) /* and leaves empty ending */
{ /* create a new hole for empty portion */
int old_next, old_last;
old_next = hole->next; /* old forward pointer */
old_last = hole->last;
hole = (Hole *)&slot->ipdata[frag_last + 1]; /* make new hole*/
hole->next = old_next; /* copy old pointers */
hole->last = old_last;
hole->first = previous->next = frag_last + 1;
slot->hole_count++;
}
if ((frag_first + frag_len) > FRAGDATALEN) /* length safety check */
{
slot->frag_tmo = 0; /* delete this buffer */
use_reasmbuf = 0;
return (NULL); /* fail */
}
bcopy((byte *)ip + ip_hlen, &slot->ipdata[frag_first], frag_len);
if (slot->hole_count) /* fully reassembled yet? */
{
use_reasmbuf = 0; /* not finished IP datagram */
return (NULL); /* not fully reassembled yet */
}
slot->iphdr.frag = 0; /* clear frags information */
return (&slot->iphdr); /* use this datagram pointer */
}
static int
udp_write(udp_Socket *s, byte FAR *datap, int len)
{
tcp_PseudoHeader ph;
struct pkt
{
in_Header in;
udp_Header udp;
int data;
} register *pkt;
if (s == NULL || datap == NULL)
return (0); /* failure */
pkt = (struct pkt *)eth_formatpacket(&s->hisethaddr[0], TYPE_IP);
pkt->in.length = htons(sizeof(in_Header) + UDP_LENGTH + len);
/* UDP header */
pkt->udp.srcPort = htons(s->myport);
pkt->udp.dstPort = htons(s->hisport);
pkt->udp.length = htons(UDP_LENGTH + len);
bcopyff(datap, &pkt->data, len);
/* Internet header */
pkt->in.hdrlen_ver = 0x45; /* version 4, hdrlen 5 */
pkt->in.tos = 0;
pkt->in.identification = htons(++ip_id); /* was post inc */
pkt->in.frag = 0;
pkt->in.ttl = 60;
pkt->in.proto = UDP_PROTO; /* UDP */
pkt->in.checksum = 0;
pkt->in.source = htonl(my_ip_addr);
pkt->in.destination = htonl(s->hisaddr);
pkt->in.checksum = ~checksum(&pkt->in, sizeof(in_Header));
/* compute udp checksum if desired */
if (s->sock_mode & UDP_MODE_CHKSUM == 0)
pkt->udp.checksum = 0;
else
{
ph.src = pkt->in.source; /* big endian now */
ph.dst = pkt->in.destination;
ph.mbz = 0;
ph.protocol = UDP_PROTO; /* UDP */
ph.length = pkt->udp.length; /* big endian now */
ph.checksum = checksum(&pkt->udp, htons(ph.length));
pkt->udp.checksum = ~checksum(&ph, sizeof(ph));
}
if (eth_send(ntohs(pkt->in.length)) != 0) /* send pkt */
return (len);
if (kdebug & DEBUG_STATUS) outs("Failed to put packet on wire\r\n");
return (0); /* sending failed */
}
/*
* udp_read - read data from buffer, does large buffering.
* Return 0 on failure.
*/
static int
udp_read(udp_Socket *s, byte FAR * datap, int maxlen)
{
register int x;
if (s == NULL || datap == NULL || s->rdata == NULL || maxlen ==0)
return (0); /* failure */
if ((x = s->rdatalen) > 0)
{
if (x > maxlen) x = maxlen;
bcopyff(s->rdata, datap, x);
s->rdatalen -= x;
}
return (x);
}
udp_cancel(in_Header *ip)
{
int len;
register udp_Header *up;
register udp_Socket *s;
/* match to a udp socket */
len = in_GetHdrlenBytes(ip);
up = (udp_Header *)((byte *)ip + len); /* udp frame pointer */
/* demux to active sockets */
for (s = udp_allsocs; s != NULL; s = s->next)
{
if (s->hisport != 0 &&
ntohs(up->dstPort) == s->myport &&
ntohs(up->srcPort) == s->hisport &&
ntohl(ip->source) == s->hisaddr)
break;
if (s->hisport != 0 && /* ICMP repeat of our pkt */
ntohs(up->dstPort) == s->hisport &&
ntohs(up->srcPort) == s->myport &&
ntohl(ip->source) == my_ip_addr)
break;
}
if (s == NULL) /* demux to passive sockets */
for (s = udp_allsocs; s != NULL; s = s->next)
if (s->hisport == 0 &&
ntohs(up->dstPort) == s->myport)
break;
if (s != NULL)
udp_close(s);
return (1); /* success */
}
int
tcp_cancel(in_Header *ip)
{
int len;
register tcp_Socket *s;
register tcp_Header *tp;
if (ip == NULL) return (0); /* failure */
len = in_GetHdrlenBytes(ip); /* check work */
tp = (tcp_Header *)((byte *)ip + len); /* TCP frame pointer */
/* demux to active sockets */
for (s = tcp_allsocs; s != NULL; s = s->next)
{
if (s->hisport != 0 && /* them to us */
ntohs(tp->dstPort) == s->myport &&
ntohs(tp->srcPort) == s->hisport &&
ntohl(ip->source) == s->hisaddr)
break;
if (s->hisport != 0 && /* ICMP repeat of our pkt */
ntohs(tp->dstPort) == s->hisport &&
ntohs(tp->srcPort) == s->myport &&
ntohl(ip->source) == my_ip_addr)
break;
}
if (s == NULL) /* demux to passive sockets */
for (s = tcp_allsocs; s != NULL; s = s->next)
if (s->hisport == 0 &&
ntohs(tp->dstPort) == s->myport)
break;
if (s != NULL)
{
s->rdatalen = 0;
s->state = tcp_StateCLOSED;
tcp_unthread(s);
}
return (1); /* success */
}
static int
tcp_read(tcp_Socket *s, byte FAR *datap, int maxlen)
{
register int x;
int obtained = 0;
if (s == NULL || datap == NULL || s->rdata == NULL || maxlen <= 0)
return (0); /* failure or read nothing */
if (s->rdatalen <= 0) /* if nothing to read */
return (0);
/* read from rdata, destuff CR NUL, deliver up to maxlen
bytes to output buffer datap, report # obtained in int
obtained, remember last read byte for CR NUL state.
Returns raw bytes consumed from buffer, in x, and number
of bytes delivered to caller, in obtained. */
x = destuff(s->rdata, s->rdatalen, datap, maxlen, &obtained,
&s->last_read);
if (x <= 0)
return (obtained);
s->rdatalen -= x; /* bytes consumed */
bcopyff(&s->rdata[x], s->rdata, s->rdatalen); /* copy down */
/* Send a window update if either the window has opened
by two MSS' since our last transmission, or opening
across half the total buffer */
if ( (TCP_RBUFSIZE - s->rdatalen >=
(int)s->window_last_sent + 2 * (int)s->mss) ||
(s->rdatalen < TCP_RBUFSIZE / 2 &&
s->rdatalen + x >= TCP_RBUFSIZE / 2) )
s->send_kind = tcp_SENDACK; /* send now */
return (obtained); /* return bytes obtained */
}
/*
* Write data to a connection.
* Returns number of bytes written, 0 when connection is not in
* established state. tcp_retransmitter senses data to be transmitted.
*/
static int
tcp_write(tcp_Socket *s, byte FAR *dp, int len)
{
register int x;
if (s == NULL || dp == NULL || s->sdata == NULL)
return (0); /* failure */
if (s->state != tcp_StateESTAB)
return (0);
x = TCP_SBUFSIZE - s->sdatalen; /* space remaining in buf */
if (x > len) x = len;
if (x < 0)
x = 0; /* catch negative errors */
if (x > 0)
{
bcopyff(dp, &s->sdata[s->sdatalen], x); /* append to buf */
s->sdatalen += x; /* data in send buffer */
}
return (x);
}
/*
* Handler for incoming UDP packets. If no socket tell the host via ICMP.
*/
static int
udp_handler(in_Header *ip)
{
register udp_Header FAR * up;
register udp_Socket *s;
tcp_PseudoHeader ph;
byte FAR * dp;
word len;
int ip_hlen;
ip_hlen = in_GetHdrlenBytes(ip);
up = (udp_Header FAR *)((byte *)ip + ip_hlen); /* UDP dgram pointer */
len = ntohs(up->length);
if (ntohs(ip->frag) & 0x3fff) /* frag flags and offset */
return (0); /* can't deal with fragments here */
if (up->checksum) /* if checksum field used */
{
ph.src = ip->source; /* already network order */