Sponge
CS144's user-space TCP library
tcp_sponge_socket.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_TCP_SPONGE_SOCKET_HH
2 #define SPONGE_LIBSPONGE_TCP_SPONGE_SOCKET_HH
3 
4 #include "byte_stream.hh"
5 #include "eventloop.hh"
6 #include "fd_adapter.hh"
7 #include "file_descriptor.hh"
8 #include "network_interface.hh"
9 #include "tcp_config.hh"
10 #include "tcp_connection.hh"
11 #include "tuntap_adapter.hh"
12 
13 #include <atomic>
14 #include <cstdint>
15 #include <optional>
16 #include <thread>
17 #include <vector>
18 
20 template <typename AdaptT>
22  private:
25 
28 
30  void _initialize_TCP(const TCPConfig &config);
31 
33  std::optional<TCPConnection> _tcp{};
34 
37 
39  void _tcp_loop(const std::function<bool()> &condition);
40 
42  void _tcp_main();
43 
46 
48  TCPSpongeSocket(std::pair<FileDescriptor, FileDescriptor> data_socket_pair, AdaptT &&datagram_interface);
49 
50  std::atomic_bool _abort{false};
51 
52  bool _inbound_shutdown{false};
53 
54  bool _outbound_shutdown{false};
55 
56  bool _fully_acked{false};
57 
58  public:
60  explicit TCPSpongeSocket(AdaptT &&datagram_interface);
61 
65  void wait_until_closed();
66 
68  void connect(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad);
69 
71  void listen_and_accept(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad);
72 
75 
78 
80  TCPSpongeSocket(const TCPSpongeSocket &) = delete;
81  TCPSpongeSocket(TCPSpongeSocket &&) = delete;
82  TCPSpongeSocket &operator=(const TCPSpongeSocket &) = delete;
85 
88 
90  void bind(const Address &address) = delete;
91  Address local_address() const = delete;
92  Address peer_address() const = delete;
93  void set_reuseaddr() = delete;
95 };
96 
100 
103 
123 
126  public:
127  CS144TCPSocket();
128  void connect(const Address &address);
129 };
130 
133  public:
137  FullStackSocket();
138  void connect(const Address &address);
139 };
140 
141 #endif // SPONGE_LIBSPONGE_TCP_SPONGE_SOCKET_HH
TCPSpongeSocket::_thread_data
LocalStreamSocket _thread_data
Stream socket for reads and writes between owner and TCP thread.
Definition: tcp_sponge_socket.hh:24
fd_adapter.hh
TCPSpongeSocket::wait_until_closed
void wait_until_closed()
Definition: tcp_sponge_socket.cc:194
file_descriptor.hh
EventLoop
Waits for events on file descriptors and executes corresponding callbacks.
Definition: eventloop.hh:12
std::pair
FullStackSocket::connect
void connect(const Address &address)
Definition: tcp_sponge_socket.cc:321
FullStackSocket
Helper class that makes a TCPOverIPv4overEthernetSpongeSocket behave more like a (kernel) TCPSocket.
Definition: tcp_sponge_socket.hh:132
TCPSpongeSocket::set_reuseaddr
void set_reuseaddr()=delete
CS144TCPSocket::CS144TCPSocket
CS144TCPSocket()
Definition: tcp_sponge_socket.cc:288
std::function
byte_stream.hh
eventloop.hh
CS144TCPSocket::connect
void connect(const Address &address)
Definition: tcp_sponge_socket.cc:290
TCPSpongeSocket::_tcp
std::optional< TCPConnection > _tcp
TCP state machine.
Definition: tcp_sponge_socket.hh:33
TCPSpongeSocket::_tcp_thread
std::thread _tcp_thread
Handle to the TCPConnection thread; owner thread calls join() in the destructor.
Definition: tcp_sponge_socket.hh:45
tcp_connection.hh
Address
Wrapper around IPv4 addresses and DNS operations.
Definition: address.hh:13
TCPSpongeSocket::_abort
std::atomic_bool _abort
Flag used by the owner to force the TCPConnection thread to shut down.
Definition: tcp_sponge_socket.hh:50
FdAdapterConfig
Config for classes derived from FdAdapter.
Definition: tcp_config.hh:26
tcp_config.hh
std::thread
LocalStreamSocket
A wrapper around Unix-domain stream sockets.
Definition: socket.hh:113
tuntap_adapter.hh
TCPSpongeSocket::_datagram_adapter
AdaptT _datagram_adapter
Adapter to underlying datagram socket (e.g., UDP or IP)
Definition: tcp_sponge_socket.hh:27
TCPSpongeSocket::peer_address
Address peer_address() const =delete
TCPSpongeSocket::connect
void connect(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad)
Connect using the specified configurations; blocks until connect succeeds or fails.
Definition: tcp_sponge_socket.cc:206
TCPSpongeSocket::TCPSpongeSocket
TCPSpongeSocket(std::pair< FileDescriptor, FileDescriptor > data_socket_pair, AdaptT &&datagram_interface)
Construct LocalStreamSocket fds from socket pair, initialize eventloop.
Definition: tcp_sponge_socket.cc:45
TCPSpongeSocket::local_address
Address local_address() const =delete
TCPSpongeSocket::_fully_acked
bool _fully_acked
Has the outbound data been fully acknowledged by the peer?
Definition: tcp_sponge_socket.hh:56
TCPConfig
Config for TCP sender and receiver.
Definition: tcp_config.hh:12
TCPSpongeSocket::_tcp_loop
void _tcp_loop(const std::function< bool()> &condition)
Process events while specified condition is true.
Definition: tcp_sponge_socket.cc:25
CS144TCPSocket
Helper class that makes a TCPOverIPv4SpongeSocket behave more like a (kernel) TCPSocket.
Definition: tcp_sponge_socket.hh:125
TCPSpongeSocket::listen_and_accept
void listen_and_accept(const TCPConfig &c_tcp, const FdAdapterConfig &c_ad)
Listen and accept using the specified configurations; blocks until accept succeeds or fails.
Definition: tcp_sponge_socket.cc:234
TCPSpongeSocket::_inbound_shutdown
bool _inbound_shutdown
Has TCPSpongeSocket shut down the incoming data to the owner?
Definition: tcp_sponge_socket.hh:52
TCPSpongeSocket::~TCPSpongeSocket
~TCPSpongeSocket()
When a connected socket is destructed, it will send a RST.
Definition: tcp_sponge_socket.cc:180
TCPSpongeSocket::bind
void bind(const Address &address)=delete
TCPSpongeSocket::operator=
TCPSpongeSocket & operator=(const TCPSpongeSocket &)=delete
TCPSpongeSocket::_eventloop
EventLoop _eventloop
eventloop that handles all the events (new inbound datagram, new outbound bytes, new inbound bytes)
Definition: tcp_sponge_socket.hh:36
FullStackSocket::FullStackSocket
FullStackSocket()
Definition: tcp_sponge_socket.cc:315
TCPSpongeSocket::_outbound_shutdown
bool _outbound_shutdown
Has the owner shut down the outbound data to the TCP connection?
Definition: tcp_sponge_socket.hh:54
network_interface.hh
TCPSpongeSocket::_initialize_TCP
void _initialize_TCP(const TCPConfig &config)
Set up the TCPConnection and the event loop.
Definition: tcp_sponge_socket.cc:54
TCPSpongeSocket
Multithreaded wrapper around TCPConnection that approximates the Unix sockets API.
Definition: tcp_sponge_socket.hh:21
TCPSpongeSocket::_tcp_main
void _tcp_main()
Main loop of TCPConnection thread.
Definition: tcp_sponge_socket.cc:255