Sponge
CS144's user-space TCP library
tuntap_adapter.cc
Go to the documentation of this file.
1 #include "tuntap_adapter.hh"
2 
3 using namespace std;
4 
10  const EthernetAddress &eth_address,
11  const Address &ip_address,
12  const Address &next_hop)
13  : _tap(move(tap)), _interface(eth_address, ip_address), _next_hop(next_hop) {
14  // Linux seems to ignore the first frame sent on a TAP device, so send a dummy frame to prime the pump :-(
15  EthernetFrame dummy_frame;
16  _tap.write(dummy_frame.serialize());
17 }
18 
19 optional<TCPSegment> TCPOverIPv4OverEthernetAdapter::read() {
20  // Read Ethernet frame from the raw device
21  EthernetFrame frame;
22  if (frame.parse(_tap.read()) != ParseResult::NoError) {
23  return {};
24  }
25 
26  // Give the frame to the NetworkInterface. Get back an Internet datagram if frame was carrying one.
27  optional<InternetDatagram> ip_dgram = _interface.recv_frame(frame);
28 
29  // The incoming frame may have caused the NetworkInterface to send a frame.
30  send_pending();
31 
32  // Try to interpret IPv4 datagram as TCP
33  if (ip_dgram) {
34  return unwrap_tcp_in_ip(ip_dgram.value());
35  }
36  return {};
37 }
38 
40 void TCPOverIPv4OverEthernetAdapter::tick(const size_t ms_since_last_tick) {
41  _interface.tick(ms_since_last_tick);
42  send_pending();
43 }
44 
48  send_pending();
49 }
50 
52  while (not _interface.frames_out().empty()) {
53  _tap.write(_interface.frames_out().front().serialize());
55  }
56 }
57 
ParseResult::NoError
@ NoError
Success.
NetworkInterface::recv_frame
std::optional< InternetDatagram > recv_frame(const EthernetFrame &frame)
Receives an Ethernet frame and responds appropriately.
Definition: network_interface.cc:40
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
NetworkInterface::frames_out
std::queue< EthernetFrame > & frames_out()
Access queue of Ethernet frames awaiting transmission.
Definition: network_interface.hh:48
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
std::move
T move(T... args)
EthernetFrame
Ethernet frame.
Definition: ethernet_frame.hh:8
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
std::queue::front
T front(T... args)
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::send_datagram
void send_datagram(const InternetDatagram &dgram, const Address &next_hop)
Sends an IPv4 datagram, encapsulated in an Ethernet frame (if it knows the Ethernet destination addre...
Definition: network_interface.cc:32
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
tuntap_adapter.hh
std::queue::pop
T pop(T... args)
std::array
EthernetFrame::parse
ParseResult parse(const Buffer buffer)
Parse the frame from a string.
Definition: ethernet_frame.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
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
NetworkInterface::tick
void tick(const size_t ms_since_last_tick)
Called periodically when time elapses.
Definition: network_interface.cc:46
std::queue::empty
T empty(T... args)
TapFD
A FileDescriptor to a Linux TAP device.
Definition: tun.hh:23
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
EthernetFrame::serialize
BufferList serialize() const
Serialize the frame to a string.
Definition: ethernet_frame.cc:19