Sponge
CS144's user-space TCP library
tcp_state.cc
Go to the documentation of this file.
1 #include "tcp_state.hh"
2 
3 using namespace std;
4 
5 bool TCPState::operator==(const TCPState &other) const {
6  return _active == other._active and _linger_after_streams_finish == other._linger_after_streams_finish and
7  _sender == other._sender and _receiver == other._receiver;
8 }
9 
10 bool TCPState::operator!=(const TCPState &other) const { return not operator==(other); }
11 
12 string TCPState::name() const {
13  return "sender=`" + _sender + "`, receiver=`" + _receiver + "`, active=" + to_string(_active) +
14  ", linger_after_streams_finish=" + to_string(_linger_after_streams_finish);
15 }
16 
18  switch (state) {
22  break;
26  break;
30  break;
34  break;
38  _linger_after_streams_finish = false;
39  break;
43  _linger_after_streams_finish = false;
44  break;
48  break;
52  break;
56  break;
60  break;
64  _linger_after_streams_finish = false;
65  _active = false;
66  break;
70  _linger_after_streams_finish = false;
71  _active = false;
72  break;
73  }
74 }
75 
76 TCPState::TCPState(const TCPSender &sender, const TCPReceiver &receiver, const bool active, const bool linger)
77  : _sender(state_summary(sender))
78  , _receiver(state_summary(receiver))
79  , _active(active)
80  , _linger_after_streams_finish(active ? linger : false) {}
81 
82 string TCPState::state_summary(const TCPReceiver &receiver) {
83  if (receiver.stream_out().error()) {
85  } else if (not receiver.ackno().has_value()) {
87  } else if (receiver.stream_out().input_ended()) {
89  } else {
91  }
92 }
93 
94 string TCPState::state_summary(const TCPSender &sender) {
95  if (sender.stream_in().error()) {
97  } else if (sender.next_seqno_absolute() == 0) {
99  } else if (sender.next_seqno_absolute() == sender.bytes_in_flight()) {
101  } else if (not sender.stream_in().eof()) {
103  } else if (sender.next_seqno_absolute() < sender.stream_in().bytes_written() + 2) {
105  } else if (sender.bytes_in_flight()) {
107  } else {
109  }
110 }
tcp_state.hh
TCPSenderStateSummary::SYN_SENT
const std::string SYN_SENT
Definition: tcp_state.hh:76
TCPState::State::SYN_RCVD
@ SYN_RCVD
Got the peer's SYN.
ByteStream::bytes_written
size_t bytes_written() const
Total number of bytes written.
Definition: byte_stream.cc:49
ByteStream::eof
bool eof() const
Definition: byte_stream.cc:47
TCPSenderStateSummary::FIN_SENT
const std::string FIN_SENT
Definition: tcp_state.hh:78
TCPState::operator!=
bool operator!=(const TCPState &other) const
Definition: tcp_state.cc:10
TCPReceiver::ackno
std::optional< WrappingInt32 > ackno() const
The ackno that should be sent to the peer.
Definition: tcp_receiver.cc:17
TCPSender
The "sender" part of a TCP implementation.
Definition: tcp_sender.hh:18
TCPState::State
State
Official state names from the TCP specification.
Definition: tcp_state.hh:35
TCPState::TCPState
TCPState(const TCPSender &sender, const TCPReceiver &receiver, const bool active, const bool linger)
Construct a TCPState given a sender, a receiver, and the TCPConnection's active and linger bits.
Definition: tcp_state.cc:76
TCPState::State::RESET
@ RESET
A connection that terminated abnormally.
TCPReceiverStateSummary::FIN_RECV
const std::string FIN_RECV
Definition: tcp_state.hh:70
TCPState::State::TIME_WAIT
@ TIME_WAIT
Both sides have sent FIN and ACK'd, waiting for 2 MSL.
TCPSender::bytes_in_flight
size_t bytes_in_flight() const
How many sequence numbers are occupied by segments sent but not yet acknowledged?
Definition: tcp_sender.cc:25
TCPState::State::LAST_ACK
@ LAST_ACK
Local side sent a FIN from CLOSE_WAIT, waiting for ACK.
TCPState::State::LISTEN
@ LISTEN
Listening for a peer to connect.
TCPReceiver
The "receiver" part of a TCP implementation.
Definition: tcp_receiver.hh:16
TCPState::State::CLOSE_WAIT
@ CLOSE_WAIT
Remote side has sent a FIN, connection is half-open.
std::to_string
T to_string(T... args)
TCPReceiverStateSummary::LISTEN
const std::string LISTEN
Definition: tcp_state.hh:68
TCPState::State::CLOSED
@ CLOSED
A connection that has terminated normally.
TCPState::State::ESTABLISHED
@ ESTABLISHED
Three-way handshake complete.
TCPState
Summary of a TCPConnection's internal state.
Definition: tcp_state.hh:23
TCPState::_sender
std::string _sender
Definition: tcp_state.hh:25
TCPState::operator==
bool operator==(const TCPState &other) const
Definition: tcp_state.cc:5
TCPState::State::CLOSING
@ CLOSING
Received a FIN just after we sent one.
TCPSenderStateSummary::CLOSED
const std::string CLOSED
Definition: tcp_state.hh:75
TCPState::_linger_after_streams_finish
bool _linger_after_streams_finish
Definition: tcp_state.hh:28
TCPSenderStateSummary::ERROR
const std::string ERROR
Definition: tcp_state.hh:74
ByteStream::error
bool error() const
Definition: byte_stream.hh:62
std
TCPSender::stream_in
ByteStream & stream_in()
Definition: tcp_sender.hh:43
operator==
bool operator==(WrappingInt32 a, WrappingInt32 b)
Whether the two integers are equal.
Definition: wrapping_integers.hh:50
TCPState::State::FIN_WAIT_2
@ FIN_WAIT_2
Received an ACK for previously-sent FIN.
TCPState::State::FIN_WAIT_1
@ FIN_WAIT_1
Sent a FIN to the remote side, not yet ACK'd.
TCPState::_receiver
std::string _receiver
Definition: tcp_state.hh:26
TCPReceiverStateSummary::SYN_RECV
const std::string SYN_RECV
Definition: tcp_state.hh:69
TCPState::name
std::string name() const
Summarize the TCPState in a string.
Definition: tcp_state.cc:12
TCPSenderStateSummary::SYN_ACKED
const std::string SYN_ACKED
Definition: tcp_state.hh:77
TCPSender::next_seqno_absolute
uint64_t next_seqno_absolute() const
absolute seqno for the next byte to be sent
Definition: tcp_sender.hh:85
TCPReceiver::stream_out
ByteStream & stream_out()
Definition: tcp_receiver.hh:61
TCPState::state_summary
static std::string state_summary(const TCPReceiver &receiver)
Summarize the state of a TCPReceiver in a string.
Definition: tcp_state.cc:82
TCPSenderStateSummary::FIN_ACKED
const std::string FIN_ACKED
Definition: tcp_state.hh:79
TCPState::State::SYN_SENT
@ SYN_SENT
Sent a SYN to initiate a connection.
TCPState::_active
bool _active
Definition: tcp_state.hh:27
ByteStream::input_ended
bool input_ended() const
Definition: byte_stream.cc:41
TCPReceiverStateSummary::ERROR
const std::string ERROR
Definition: tcp_state.hh:67