Sponge
CS144's user-space TCP library
tuntap_adapter.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_TUNFD_ADAPTER_HH
2 #define SPONGE_LIBSPONGE_TUNFD_ADAPTER_HH
3 
4 #include "ethernet_header.hh"
5 #include "network_interface.hh"
6 #include "tun.hh"
7 
8 #include <optional>
9 #include <unordered_map>
10 #include <utility>
11 
14  private:
16 
17  public:
19  explicit TCPOverIPv4OverTunFdAdapter(TunFD &&tun) : _tun(std::move(tun)) {}
20 
22  std::optional<TCPSegment> read() {
23  InternetDatagram ip_dgram;
24  if (ip_dgram.parse(_tun.read()) != ParseResult::NoError) {
25  return {};
26  }
27  return unwrap_tcp_in_ip(ip_dgram);
28  }
29 
31  void write(TCPSegment &seg) { _tun.write(wrap_tcp_in_ip(seg).serialize()); }
32 
34  operator TunFD &() { return _tun; }
35 
37  operator const TunFD &() const { return _tun; }
38 };
39 
42 
45  private:
47 
49 
51 
52  void send_pending();
53 
54  public:
57  const EthernetAddress &eth_address,
58  const Address &ip_address,
59  const Address &next_hop);
61  std::optional<TCPSegment> read();
62 
64  void write(TCPSegment &seg);
65 
67  void tick(const size_t ms_since_last_tick);
68 
70  operator TapFD &() { return _tap; }
71 
73  operator const TapFD &() const { return _tap; }
74 };
75 
76 #endif // SPONGE_LIBSPONGE_TUNFD_ADAPTER_HH
ParseResult::NoError
@ NoError
Success.
TCPOverIPv4OverEthernetAdapter::read
std::optional< TCPSegment > read()
Attempts to read and parse an Ethernet frame containing an IPv4 datagram that contains a TCP segment.
Definition: tuntap_adapter.cc:19
FileDescriptor::write
size_t write(const char *str, const bool write_all=true)
Write a string, possibly blocking until all is written.
Definition: file_descriptor.hh:65
TCPOverIPv4OverTunFdAdapter::_tun
TunFD _tun
Definition: tuntap_adapter.hh:15
TCPOverIPv4OverTunFdAdapter::write
void write(TCPSegment &seg)
Creates an IPv4 datagram from a TCP segment and writes it to the TUN device.
Definition: tuntap_adapter.hh:31
TCPOverIPv4OverEthernetAdapter::tick
void tick(const size_t ms_since_last_tick)
Called periodically when time elapses.
Definition: tuntap_adapter.cc:40
TCPOverIPv4OverEthernetAdapter::_interface
NetworkInterface _interface
NIC abstraction.
Definition: tuntap_adapter.hh:48
TCPOverIPv4OverEthernetAdapter::_tap
TapFD _tap
Raw Ethernet connection.
Definition: tuntap_adapter.hh:46
IPv4Datagram
IPv4 Internet datagram
Definition: ipv4_datagram.hh:8
FileDescriptor::read
std::string read(const size_t limit=std::numeric_limits< size_t >::max())
Read up to limit bytes.
Address
Wrapper around IPv4 addresses and DNS operations.
Definition: address.hh:13
TCPOverIPv4Adapter::wrap_tcp_in_ip
InternetDatagram wrap_tcp_in_ip(TCPSegment &seg)
Definition: tcp_over_ip.cc:75
NetworkInterface
A "network interface" that connects IP (the internet layer, or network layer) with Ethernet (the netw...
Definition: network_interface.hh:32
ethernet_header.hh
tun.hh
TCPOverIPv4OverEthernetAdapter::TCPOverIPv4OverEthernetAdapter
TCPOverIPv4OverEthernetAdapter(TapFD &&tap, const EthernetAddress &eth_address, const Address &ip_address, const Address &next_hop)
Construct from a TapFD.
Definition: tuntap_adapter.cc:9
std::array
TCPOverIPv4OverTunFdAdapter::read
std::optional< TCPSegment > read()
Attempts to read and parse an IPv4 datagram containing a TCP segment related to the current connectio...
Definition: tuntap_adapter.hh:22
TCPOverIPv4OverEthernetAdapter
A FD adapter for IPv4 datagrams read from and written to a TAP device.
Definition: tuntap_adapter.hh:44
IPv4Datagram::parse
ParseResult parse(const Buffer buffer)
Parse the segment from a string.
Definition: ipv4_datagram.cc:11
LossyFdAdapter
An adapter class that adds random dropping behavior to an FD adapter.
Definition: lossy_fd_adapter.hh:15
TCPOverIPv4OverEthernetAdapter::send_pending
void send_pending()
Sends any pending Ethernet frames.
Definition: tuntap_adapter.cc:51
TCPOverIPv4OverTunFdAdapter
A FD adapter for IPv4 datagrams read from and written to a TUN device.
Definition: tuntap_adapter.hh:13
TCPOverIPv4OverEthernetAdapter::_next_hop
Address _next_hop
IP address of the next hop.
Definition: tuntap_adapter.hh:50
TCPOverIPv4OverEthernetAdapter::write
void write(TCPSegment &seg)
Sends a TCP segment (in an IPv4 datagram, in an Ethernet frame).
Definition: tuntap_adapter.cc:46
std
TCPOverIPv4OverTunFdAdapter::TCPOverIPv4OverTunFdAdapter
TCPOverIPv4OverTunFdAdapter(TunFD &&tun)
Construct from a TunFD.
Definition: tuntap_adapter.hh:19
TapFD
A FileDescriptor to a Linux TAP device.
Definition: tun.hh:23
TCPOverIPv4Adapter
A converter from TCP segments to serialized IPv4 datagrams.
Definition: tcp_over_ip.hh:12
TunFD
A FileDescriptor to a Linux TUN device.
Definition: tun.hh:16
TCPOverIPv4Adapter::unwrap_tcp_in_ip
std::optional< TCPSegment > unwrap_tcp_in_ip(const InternetDatagram &ip_dgram)
Definition: tcp_over_ip.cc:26
TCPSegment
TCP segment
Definition: tcp_segment.hh:10
network_interface.hh