-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtcp_transmit.hpp
791 lines (752 loc) · 42.3 KB
/
tcp_transmit.hpp
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
#pragma once
#include "packets.hpp"
#include "tcb.hpp"
namespace mstack {
class tcp_transmit {
public:
static void tcp_send_ack() {}
static void tcp_send_syn_ack() {}
static void tcp_send_rst() {}
static void tcp_send_ctl() {}
static int generate_iss() { return 0; }
static bool tcp_handle_close_state(std::shared_ptr<tcb_t> in_tcb, tcp_packet_t& in_packet) {
tcp_header_t in_tcp = tcp_header_t::consume(in_packet.buffer->get_pointer());
/**
* If the state is CLOSED (i.e., TCB does not exist) then
* all data in the incoming segment is discarded. An incoming
* segment containing a RST is discarded.
*/
if (in_tcp.RST == 1) {
return true;
}
/**
* An incoming segment not
* containing a RST causes a RST to be sent in response. The
* acknowledgment and sequence field values are selected to make the
* reset sequence acceptable to the TCP that sent the offending
* segment.
*
* If the ACK bit is off, sequence number zero is used,
* <SEQ=0><ACK=SEG.SEQ+SEG.LEN><CTL=RST,ACK>
*/
if (in_tcp.ACK == 0) {
return true;
}
/**
* If the ACK bit is on,
* <SEQ=SEG.ACK><CTL=RST>. Return.
*/
if (in_tcp.ACK == 1) {
return true;
}
return false;
}
static bool tcp_handle_listen_state(std::shared_ptr<tcb_t> in_tcb,
tcp_packet_t& in_packet) {
// If the state is LISTEN then
if (in_tcb->state != TCP_LISTEN) {
return false;
}
tcp_header_t in_tcp = tcp_header_t::consume(in_packet.buffer->get_pointer());
DLOG(INFO) << "[TCP LISTEN] " << in_tcp;
/**
* first check for an RST
* An incoming RST should be ignored. Return.
*/
if (in_tcp.RST == 1) {
return true;
}
/**
* TODO: second check for an ACK
* Any acknowledgment is bad if it arrives on a connection still in
* the LISTEN state. An acceptable reset segment should be formed
* for any arriving ACK-bearing segment. The RST should be
* formatted as follows: <SEQ=SEG.ACK><CTL=RST>
*/
if (in_tcp.ACK == 1) {
tcp_send_rst();
return true;
}
/**
* TODO: third check for a SYN
* If the SYN bit is set, check the security. If the
* security/compartment on the incoming segment does not exactly
* match the security/compartment in the TCB then send a reset and
* return. <SEQ=SEG.ACK><CTL=RST>
*
* If the SEG.PRC is greater than the TCB.PRC then if allowed by
* the user and the system set TCB.PRC<-SEG.PRC, if not allowed
* send a reset and return. <SEQ=SEG.ACK><CTL=RST>
*
* If the SEG.PRC is less than the TCB.PRC then continue.
*/
if (in_tcp.SYN == 1) {
}
/**
* Set RCV.NXT to SEG.SEQ+1, IRS is set to SEG.SEQ and any other
* control or text should be queued for processing later. ISS
* should be selected and a SYN segment sent of the form:
* <SEQ=ISS><ACK=RCV.NXT><CTL=SYN,ACK>
*
* SND.NXT is set to ISS+1 and SND.UNA to ISS. The connection
* state should be changed to SYN-RECEIVED. Note that any other
* incoming control or data (combined with SYN) will be processed
* in the SYN-RECEIVED state, but processing of SYN and ACK should
* not be repeated. If the listen was not fully specified (i.e.,
* the foreign socket was not fully specified), then the
* unspecified fields should be filled in now.
*/
if (in_tcp.SYN == 1) {
uint32_t iss = generate_iss();
in_tcb->receive.next = in_tcp.seq_no + 1;
in_tcb->receive.window = 0xFAF0;
in_tcb->send.next = iss + 1;
in_tcb->send.unacknowledged = iss;
in_tcb->next_state = TCP_SYN_RECEIVED;
in_tcb->active_self();
DLOG(INFO) << "[SEND SYN ACK]";
return false;
}
/**
* TODO: fourth other text or control
* must have an ACK and thus would be discarded by the ACK
* processing. An incoming RST segment could not be valid, since
* it could not have been sent in response to anything sent by this
* incarnation of the connection. So you are unlikely to get here,
* but if you do, drop the segment, and return.
*/
return true;
}
static bool tcp_handle_syn_sent(std::shared_ptr<tcb_t> in_tcb, tcp_packet_t& in_packet) {
// If the state is SYN-SENT then
if (in_tcb->state != TCP_SYN_SENT) {
return false;
}
tcp_header_t in_tcp = tcp_header_t::consume(in_packet.buffer->get_pointer());
// first check the ACK bit
if (in_tcp.ACK == 1) {
/**
* If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send a reset (unless
* the RST bit is set, if so drop the segment and return)
* <SEQ=SEG.ACK><CTL=RST>
* and discard the segment. Return.
* If SND.UNA =< SEG.ACK =< SND.NXT then the ACK is acceptable.
*/
}
// second check the RST bit
if (in_tcp.RST == 1) {
/**
* If the ACK was acceptable then signal the user "error:
* connection reset", drop the segment, enter CLOSED state,
* delete TCB, and return. Otherwise (no ACK) drop the segment
* and return.
*/
}
// third check the security and precedence
/**
* If the security/compartment in the segment does not exactly
* match the security/compartment in the TCB, send a reset
*
* If there is an ACK
*
* <SEQ=SEG.ACK><CTL=RST>
*
* Otherwise
*
* <SEQ=0><ACK=SEG.SEQ+SEG.LEN><CTL=RST,ACK>
*
* If there is an ACK
*
* The precedence in the segment must match the precedence in the
* TCB, if not, send a reset
*
* <SEQ=SEG.ACK><CTL=RST>
*
* If there is no ACK
*
* If the precedence in the segment is higher than the precedence
* in the TCB then if allowed by the user and the system raise
* the precedence in the TCB to that in the segment, if not
* allowed to raise the prec then send a reset.
*
* <SEQ=0><ACK=SEG.SEQ+SEG.LEN><CTL=RST,ACK>
*
* If the precedence in the segment is lower than the precedence
* in the TCB continue.
*
* If a reset was sent, discard the segment and return.
*/
// fourth check the SYN bit
/**
* This step should be reached only if the ACK is ok, or there is
* no ACK, and it the segment did not contain a RST.
*
* If the SYN bit is on and the security/compartment and precedence
* are acceptable then, RCV.NXT is set to SEG.SEQ+1, IRS is set to
* SEG.SEQ. SND.UNA should be advanced to equal SEG.ACK (if there
* is an ACK), and any segments on the retransmission queue which
* are thereby acknowledged should be removed.
*
* If SND.UNA > ISS (our SYN has been ACKed), change the connection
* state to ESTABLISHED, form an ACK segment
*
* <SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>
*
* and send it. Data or controls which were queued for
* transmission may be included. If there are other controls or
* text in the segment then continue processing at the sixth step
* below where the URG bit is checked, otherwise return.
*
* Otherwise enter SYN-RECEIVED, form a SYN,ACK segment
*
* <SEQ=ISS><ACK=RCV.NXT><CTL=SYN,ACK>
*
* and send it. If there are other controls or text in the
* segment, queue them for processing after the ESTABLISHED state
* has been reached, return.
*/
// fifth, if neither of the SYN or RST bits is set then drop the segment
// and return.
return true;
}
static bool tcp_check_segment(std::shared_ptr<tcb_t> in_tcb, tcp_packet_t& in_packet) {
/**
* SYN-RECEIVED STATE
* ESTABLISHED STATE
* FIN-WAIT-1 STATE
* FIN-WAIT-2 STATE
* CLOSE-WAIT STATE
* CLOSING STATE
* LAST-ACK STATE
* TIME-WAIT STATE
*
* Segments are processed in sequence. Initial tests on arrival
* are used to discard old duplicates, but further processing is
* done in SEG.SEQ order. If a segment's contents straddle the
* boundary between old and new, only the new parts should be
* processed.
*
* There are four cases for the acceptability test for an incoming
* segment:
*
* Segment Receive Test
* Length Window
* ------- ------- -------------------------------------------
*
* 0 0 SEG.SEQ = RCV.NXT
*
* 0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
*
* >0 0 not acceptable
*
* >0 >0 RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
* or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT+RCV.WND
*
* If the RCV.WND is zero, no segments will be acceptable, but
* special allowance should be made to accept valid ACKs, URGs and
* RSTs.
*
* If an incoming segment is not acceptable, an acknowledgment
* should be sent in reply (unless the RST bit is set, if so drop
* the segment and return):
*
* <SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>
*
* After sending the acknowledgment, drop the unacceptable segment
* and return.
* In the following it is assumed that the segment is the idealized
* segment that begins at RCV.NXT and does not exceed the window.
* One could tailor actual segments to fit this assumption by
* trimming off any portions that lie outside the window (including
* SYN and FIN), and only processing further if the segment then
* begins at RCV.NXT. Segments with higher begining sequence
* numbers may be held for later processing.
*/
tcp_header_t in_tcp = tcp_header_t::consume(in_packet.buffer->get_pointer());
uint16_t segment_length =
in_packet.buffer->get_remaining_len() - in_tcp.header_length * 4;
// DLOG(INFO) << "SN: " << in_tcp.seq_no << " ";
// DLOG(INFO) << "SL: " << segment_length << " ";
// DLOG(INFO) << "RW: " << in_tcb->receive.window << " ";
// DLOG(INFO) << "RN: " << in_tcb->receive.next;
switch (in_tcb->state) {
case TCP_SYN_RECEIVED:
case TCP_ESTABLISHED:
case TCP_FIN_WAIT_1:
case TCP_FIN_WAIT_2:
case TCP_CLOSE_WAIT:
case TCP_CLOSING:
case TCP_LAST_ACK:
case TCP_TIME_WAIT:
if (segment_length == 0 && in_tcb->receive.window == 0) {
if (in_tcp.seq_no == in_tcb->receive.next) {
return true;
} else {
return false;
}
}
if (segment_length == 0 && in_tcb->receive.window > 0) {
// RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
if (in_tcb->receive.next <= in_tcp.seq_no &&
in_tcp.seq_no <
in_tcb->receive.next + in_tcb->receive.window) {
return true;
} else {
return false;
}
}
if (segment_length > 0 && in_tcb->receive.window == 0) {
return false;
}
if (segment_length > 0 && in_tcb->receive.window > 0) {
// RCV.NXT =< SEG.SEQ < RCV.NXT+RCV.WND
// or RCV.NXT =< SEG.SEQ+SEG.LEN-1 < RCV.NXT +
// RCV.WND
if ((in_tcb->receive.next <= in_tcp.seq_no &&
in_tcb->receive.next + in_tcb->receive.window) ||
(in_tcb->receive.next <=
in_tcp.seq_no + segment_length - 1 &&
in_tcp.seq_no + segment_length - 1 <
in_tcb->receive.next +
in_tcb->receive.window)) {
return true;
} else {
return false;
}
}
}
return false;
}
static void tcp_in(std::shared_ptr<tcb_t> in_tcb, tcp_packet_t& in_packet) {
DLOG(INFO) << "[TCP] [CHECK TCP_CLOSED] " << *in_tcb;
if (in_tcb->state == TCP_CLOSED && tcp_handle_close_state(in_tcb, in_packet)) {
return;
}
DLOG(INFO) << "[TCP] [CHECK TCP_LISTEN] " << *in_tcb;
if (in_tcb->state == TCP_LISTEN && tcp_handle_listen_state(in_tcb, in_packet)) {
return;
}
DLOG(INFO) << "[TCP] [CHECK TCP_SYN_SENY] " << *in_tcb;
if (in_tcb->state == TCP_SYN_SENT && tcp_handle_syn_sent(in_tcb, in_packet)) {
return;
}
tcp_header_t in_tcp = tcp_header_t::consume(in_packet.buffer->get_pointer());
DLOG(INFO) << "[TCP] [PROCESS 1] " << *in_tcb;
// first check sequence number
if (!tcp_check_segment(in_tcb, in_packet)) {
DLOG(INFO) << "[SEGMENT SEQ FAIL]";
if (!in_tcp.RST) {
// <SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>
tcp_send_ack();
}
return;
}
DLOG(INFO) << "[TCP] [PROCESS 2] " << *in_tcb;
// TODO: second check the RST bit
if (in_tcp.RST == 1) {
switch (in_tcb->state) {
/**
* SYN-RECEIVED STATE
* If the RST bit is set
* If this connection was initiated with a passive OPEN
(i.e.,
* came from the LISTEN state), then return this connection
to
* LISTEN state and return. The user need not be informed.
If
* this connection was initiated with an active OPEN (i.e.,
* came from SYN-SENT state) then the connection was refused,
signal
* the user "connection refused". In either case, all
segments
* on the retransmission queue should be removed. And in
the
* active OPEN case, enter the CLOSED state and delete the
TCB,
* and return.
*/
case TCP_SYN_RECEIVED:
return;
/**
* ESTABLISHED
* FIN-WAIT-1
* FIN-WAIT-2
* CLOSE-WAIT
* If the RST bit is set then, any outstanding RECEIVEs and
* SEND should receive "reset" responses. All segment queues
should
* be flushed. Users should also receive an unsolicited general
* "connection reset" signal. Enter the CLOSED state,
delete
* the TCB, and return.
*/
case TCP_ESTABLISHED:
case TCP_FIN_WAIT_1:
case TCP_FIN_WAIT_2:
case TCP_CLOSE_WAIT:
return;
/**
* CLOSING STATE
* LAST-ACK STATE
* TIME-WAIT
* If the RST bit is set then, enter the CLOSED state,
delete
* the TCB, and return.
*/
case TCP_CLOSING:
case TCP_LAST_ACK:
case TCP_TIME_WAIT:
return;
}
}
DLOG(INFO) << "[TCP] [PROCESS 3] " << *in_tcb;
// TODO: third check security and precedence
/**
* SYN-RECEIVED
*
* If the security/compartment and precedence in the segment do not
* exactly match the security/compartment and precedence in the TCB
* then send a reset, and return.
*
* ESTABLISHED STATE
*
* If the security/compartment and precedence in the segment do not
* exactly match the security/compartment and precedence in the TCB
* then send a reset, any outstanding RECEIVEs and SEND should
* receive "reset" responses. All segment queues should be
* flushed. Users should also receive an unsolicited general
* "connection reset" signal. Enter the CLOSED state, delete the
* TCB, and return.
*
* Note this check is placed following the sequence check to prevent
* a segment from an old connection between these ports with a
* different security or precedence from causing an abort of the
* current connection.
*/
DLOG(INFO) << "[TCP] [PROCESS 4] " << *in_tcb;
// TODO: fourth, check the SYN bit
if (in_tcp.SYN) {
switch (in_tcb->state) {
/**
* SYN-RECEIVED
* ESTABLISHED STATE
* FIN-WAIT STATE-1
* FIN-WAIT STATE-2
* CLOSE-WAIT STATE
* CLOSING STATE
* LAST-ACK STATE
* TIME-WAIT STATE
*
* If the SYN is in the window it is an error, send a
* reset, any outstanding RECEIVEs and SEND should
* receive "reset" responses, all segment queues should be
* flushed, the user should also receive an unsolicited
* general "connection reset" signal, enter the CLOSED
* state, delete the TCB, and return.
*
* If the SYN is not in the window this step would not
* be reached and an ack would have been sent in the
* first step (sequence number check).
*/
case TCP_SYN_RECEIVED:
case TCP_ESTABLISHED:
case TCP_FIN_WAIT_1:
case TCP_FIN_WAIT_2:
case TCP_CLOSE_WAIT:
case TCP_CLOSING:
case TCP_LAST_ACK:
case TCP_TIME_WAIT:
return;
}
}
// fifth check the ACK field
DLOG(INFO) << "[TCP] [PROCESS 5] " << *in_tcb;
if (in_tcp.ACK) {
switch (in_tcb->state) {
/**
* SYN-RECEIVED STATE
* If SND.UNA =< SEG.ACK =< SND.NXT then enter ESTABLISHED
* state and continue processing. If the segment acknowledgment
* is not acceptable, form a reset segment, <SEQ=SEG.ACK><CTL=RST>
*/
case TCP_SYN_RECEIVED:
if (in_tcb->send.unacknowledged <= in_tcp.ack_no &&
in_tcp.ack_no <= in_tcb->send.next) {
in_tcb->state = TCP_ESTABLISHED;
in_tcb->next_state = TCP_ESTABLISHED;
in_tcb->listen_finish();
// in_tcb->receive.next += 1;
} else {
tcp_send_rst();
return;
}
break;
/**
* ESTABLISHED STATE
* If SND.UNA < SEG.ACK =< SND.NXT then, set SND.UNA <-
* SEG.ACK. Any segments on the retransmission queue which are
* thereby entirely acknowledged are removed. Users should
* receive positive acknowledgments for buffers which have been SENT
* and fully acknowledged (i.e., SEND buffer should be returned
* with "ok" response). If the ACK is a duplicate (SEG.ACK <
* SND.UNA), it can be ignored. If the ACK acks something not yet
* sent (SEG.ACK > SND.NXT) then send an ACK, drop the segment, and
* return.
*
* If SND.UNA < SEG.ACK =< SND.NXT, the send window should be
* updated. If (SND.WL1 < SEG.SEQ or (SND.WL1 = SEG.SEQ and
* SND.WL2 =< SEG.ACK)), set SND.WND <- SEG.WND, set
* SND.WL1 <- SEG.SEQ, and set SND.WL2 <- SEG.ACK.
*
* Note that SND.WND is an offset from SND.UNA, that SND.WL1
* records the sequence number of the last segment used to
* update SND.WND, and that SND.WL2 records the acknowledgment
* number of the last segment used to update SND.WND. The
* check here prevents using old segments to update the window.
*/
case TCP_ESTABLISHED:
case TCP_FIN_WAIT_1:
case TCP_FIN_WAIT_2:
case TCP_CLOSE_WAIT:
case TCP_CLOSING:
if (in_tcb->send.unacknowledged < in_tcp.ack_no &&
in_tcp.ack_no <= in_tcb->send.next) {
in_tcb->send.unacknowledged = in_tcp.ack_no;
// TODO: update windows
}
if (in_tcp.ack_no <
in_tcb->send.unacknowledged) { /* ignore */
}
if (in_tcp.ack_no > in_tcb->send.next) {
in_tcb->active_self();
return;
}
/**
* FIN-WAIT-1 STATE
* In addition to the processing for the ESTABLISHED
* state, if our FIN is now acknowledged then enter
* FIN-WAIT-2 and continue processing in that state.
*/
if (in_tcb->state == TCP_FIN_WAIT_1) {
in_tcb->next_state = TCP_FIN_WAIT_2;
}
/**
* FIN-WAIT-2 STATE
* In addition to the processing for the ESTABLISHED
* state, if the retransmission queue is empty, the
* user's CLOSE can be acknowledged ("ok") but do not delete
* the TCB.
*/
if (in_tcb->state == TCP_FIN_WAIT_2) {
// * CLOSE FINISH
}
/**
* CLOSE-WAIT STATE
* Do the same processing as for the ESTABLISHED state.
*/
if (in_tcb->state == TCP_CLOSE_WAIT) {
}
/**
* CLOSING STATE
* In addition to the processing for the ESTABLISHED
* state, if the ACK acknowledges our FIN then enter
* the TIME-WAIT state, otherwise ignore the segment.
*/
if (in_tcb->state == TCP_CLOSING) {
in_tcb->next_state = TCP_TIME_WAIT;
}
break;
/**
* LAST-ACK STATE
* The only thing that can arrive in this state is an
* acknowledgment of our FIN. If our FIN is now acknowledged,
* delete the TCB, enter the CLOSED state, and return.
*/
case TCP_LAST_ACK:
in_tcb->next_state = TCP_CLOSED;
return;
/**
* TIME-WAIT STATE
* The only thing that can arrive in this state is a
* retransmission of the remote FIN. Acknowledge it, and
* restart the 2 MSL timeout.
*/
case TCP_TIME_WAIT:
in_tcb->active_self();
break;
}
}
DLOG(INFO) << "[TCP] [PROCESS 6] " << *in_tcb;
// TODO: sixth, check the URG bit
if (in_tcp.URG == 1) {
switch (in_tcb->state) {
/**
* ESTABLISHED STATE
* FIN-WAIT-1 STATE
* FIN-WAIT-2 STATE
* If the URG bit is set, RCV.UP <- max(RCV.UP,SEG.UP), and
* signal the user that the remote side has urgent data if the
* urgent pointer (RCV.UP) is in advance of the data consumed. If
* the user has already been signaled (or is still in the "urgent
* mode") for this continuous sequence of urgent data, do
not
* signal the user again.
*/
case TCP_ESTABLISHED:
case TCP_FIN_WAIT_1:
case TCP_FIN_WAIT_2:
/**
* CLOSE-WAIT STATE
* CLOSING STATE
* LAST-ACK STATE
* TIME-WAIT
* This should not occur, since a FIN has been received from
* the remote side. Ignore the URG.
*/
case TCP_CLOSE_WAIT:
case TCP_CLOSING:
case TCP_LAST_ACK:
case TCP_TIME_WAIT:
break;
}
}
int header_len = in_tcp.header_length * 4;
int segment_len = in_packet.buffer->get_remaining_len() - header_len;
DLOG(INFO) << "[TCP] [PROCESS 7] " << *in_tcb;
// seventh, process the segment text
if (segment_len > 0) {
switch (in_tcb->state) {
/**
* ESTABLISHED STATE
* FIN-WAIT-1 STATE
* FIN-WAIT-2 STATE
*
* Once in the ESTABLISHED state, it is possible to deliver
* segment text to user RECEIVE buffers. Text from segments
* can be moved into buffers until either the buffer is full or
* the segment is empty. If the segment empties and carries an PUSH
* flag, then the user is informed, when the buffer is
* returned, that a PUSH has been received.
*
* When the TCP takes responsibility for delivering the data
* to the user it must also acknowledge the receipt of the
* data.
*
* Once the TCP takes responsibility for the data it advances
* RCV.NXT over the data accepted, and adjusts RCV.WND as
* apporopriate to the current buffer availability. The total
* of RCV.NXT and RCV.WND should not be reduced.
*
* Please note the window management suggestions in
* section 3.7.
*
* Send an acknowledgment of the form:
*
* <SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>
*
* This acknowledgment should be piggybacked on a segment
* being transmitted if possible without incurring undue delay
*/
case TCP_ESTABLISHED:
case TCP_FIN_WAIT_1:
case TCP_FIN_WAIT_2: {
DLOG(INFO) << "[RECEIVE DATA] " << segment_len;
in_tcb->receive.next += segment_len;
std::unique_ptr<base_packet> out_buffer =
std::make_unique<base_packet>(segment_len);
in_packet.buffer->export_payload(out_buffer->get_pointer(),
header_len);
raw_packet r_packet = {.buffer = std::move(out_buffer)};
in_tcb->receive_queue.push_back(std::move(r_packet));
in_tcb->active_self();
break;
}
/**
* CLOSE-WAIT STATE
* CLOSING STATE
* LAST-ACK STATE
* TIME-WAIT STATE
* This should not occur, since a FIN has been received from
* the remote side. Ignore the segment text.
*/
case TCP_CLOSE_WAIT:
case TCP_CLOSING:
case TCP_LAST_ACK:
case TCP_TIME_WAIT:
break;
}
}
DLOG(INFO) << "[TCP] [PROCESS 8] " << *in_tcb;
// eighth, check the FIN bit
if (in_tcp.FIN == 1) {
switch (in_tcb->state) {
/**
* Do not process the FIN if the state is CLOSED, LISTEN or
* SYN-SENT since the SEG.SEQ cannot be validated; drop the
* segment and return.
*
* If the FIN bit is set, signal the user "connection
* closing" and return any pending RECEIVEs with same
* message, advance RCV.NXT over the FIN, and send an
* acknowledgment for the FIN. Note that FIN implies PUSH
* for any segment text not yet delivered to the user.
*/
/**
* SYN-RECEIVED STATE
* ESTABLISHED STATE
* Enter the CLOSE-WAIT state.
*/
case TCP_SYN_RECEIVED:
case TCP_ESTABLISHED:
in_tcb->receive.next += 1;
in_tcb->next_state = TCP_CLOSE_WAIT;
in_tcb->active_self();
/**
* FIN-WAIT-1 STATE
* If our FIN has been ACKed (perhaps in this segment),
* then enter TIME-WAIT, start the time-wait timer,
* turn off the other timers; otherwise enter the CLOSING
* state.
*/
case TCP_FIN_WAIT_1:
if (in_tcb->next_state == TCP_FIN_WAIT_2) {
in_tcb->next_state = TCP_TIME_WAIT;
} else {
in_tcb->next_state = TCP_CLOSING;
}
/**
* FIN-WAIT-2 STATE
* Enter the TIME-WAIT state. Start the time-wait
* timer, turn off the other timers.
*/
case TCP_FIN_WAIT_2:
// * start time_wait
in_tcb->next_state = TCP_TIME_WAIT;
/**
* CLOSE-WAIT STATE
* Remain in the CLOSE-WAIT state.
*/
case TCP_CLOSE_WAIT:
/**
* CLOSING STATE
* Remain in the CLOSING state.
*/
case TCP_CLOSING:
/**
* LAST-ACK STATE
* Remain in the LAST-ACK state.
*/
case TCP_LAST_ACK:
/**
* TIME-WAIT STATE
* Remain in the TIME-WAIT state. Restart the 2 MSL
* time-wait timeout.
*/
case TCP_TIME_WAIT:
return;
}
DLOG(INFO) << "[TCP] [PROCESS 9] " << *in_tcb;
}
}
};
} // namespace mstack