Sponge
CS144's user-space TCP library
tcp_state.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_TCP_STATE
2 #define SPONGE_LIBSPONGE_TCP_STATE
3 
4 #include "tcp_receiver.hh"
5 #include "tcp_sender.hh"
6 
7 #include <string>
8 
23 class TCPState {
24  private:
27  bool _active{true};
29 
30  public:
31  bool operator==(const TCPState &other) const;
32  bool operator!=(const TCPState &other) const;
33 
35  enum class State {
36  LISTEN = 0,
37  SYN_RCVD,
38  SYN_SENT,
39  ESTABLISHED,
40  CLOSE_WAIT,
41  LAST_ACK,
42  FIN_WAIT_1,
43  FIN_WAIT_2,
44  CLOSING,
45  TIME_WAIT,
46  CLOSED,
47  RESET,
48  };
49 
51  std::string name() const;
52 
54  TCPState(const TCPSender &sender, const TCPReceiver &receiver, const bool active, const bool linger);
55 
57  TCPState(const TCPState::State state);
58 
60  static std::string state_summary(const TCPReceiver &receiver);
61 
63  static std::string state_summary(const TCPSender &receiver);
64 };
65 
67 const std::string ERROR = "error (connection was reset)";
68 const std::string LISTEN = "waiting for SYN: ackno is empty";
69 const std::string SYN_RECV = "SYN received (ackno exists), and input to stream hasn't ended";
70 const std::string FIN_RECV = "input to stream has ended";
71 } // namespace TCPReceiverStateSummary
72 
74 const std::string ERROR = "error (connection was reset)";
75 const std::string CLOSED = "waiting for stream to begin (no SYN sent)";
76 const std::string SYN_SENT = "stream started but nothing acknowledged";
77 const std::string SYN_ACKED = "stream ongoing";
78 const std::string FIN_SENT = "stream finished (FIN sent) but not fully acknowledged";
79 const std::string FIN_ACKED = "stream finished and fully acknowledged";
80 } // namespace TCPSenderStateSummary
81 
82 #endif // SPONGE_LIBSPONGE_TCP_STATE
TCPSenderStateSummary::SYN_SENT
const std::string SYN_SENT
Definition: tcp_state.hh:76
TCPState::State::SYN_RCVD
@ SYN_RCVD
Got the peer's SYN.
std::string
tcp_receiver.hh
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
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
tcp_sender.hh
TCPReceiverStateSummary
Definition: tcp_state.hh:66
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.
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.
TCPReceiverStateSummary::LISTEN
const std::string LISTEN
Definition: tcp_state.hh:68
TCPSenderStateSummary
Definition: tcp_state.hh:73
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
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
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
TCPReceiverStateSummary::ERROR
const std::string ERROR
Definition: tcp_state.hh:67