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 "tcp_over_ip.hh"
5 #include "tun.hh"
6 
7 #include <optional>
8 #include <unordered_map>
9 #include <utility>
10 
13  private:
15 
16  public:
18  explicit TCPOverIPv4OverTunFdAdapter(TunFD &&tun) : _tun(std::move(tun)) {}
19 
21  std::optional<TCPSegment> read() {
22  InternetDatagram ip_dgram;
23  if (ip_dgram.parse(_tun.read()) != ParseResult::NoError) {
24  return {};
25  }
26  return unwrap_tcp_in_ip(ip_dgram);
27  }
28 
30  void write(TCPSegment &seg) { _tun.write(wrap_tcp_in_ip(seg).serialize()); }
31 
33  operator TunFD &() { return _tun; }
34 
36  operator const TunFD &() const { return _tun; }
37 };
38 
41 
42 #endif // SPONGE_LIBSPONGE_TUNFD_ADAPTER_HH
ParseResult::NoError
@ NoError
Success.
tcp_over_ip.hh
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:14
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:30
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.
TCPOverIPv4Adapter::wrap_tcp_in_ip
InternetDatagram wrap_tcp_in_ip(TCPSegment &seg)
Definition: tcp_over_ip.cc:75
tun.hh
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:21
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
TCPOverIPv4OverTunFdAdapter
A FD adapter for IPv4 datagrams read from and written to a TUN device.
Definition: tuntap_adapter.hh:12
std
TCPOverIPv4OverTunFdAdapter::TCPOverIPv4OverTunFdAdapter
TCPOverIPv4OverTunFdAdapter(TunFD &&tun)
Construct from a TunFD.
Definition: tuntap_adapter.hh:18
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