Sponge
CS144's user-space TCP library
fd_adapter.cc
Go to the documentation of this file.
1 #include "fd_adapter.hh"
2 
3 #include <iostream>
4 #include <stdexcept>
5 #include <utility>
6 
7 using namespace std;
8 
21 optional<TCPSegment> TCPOverUDPSocketAdapter::read() {
22  auto datagram = _sock.recv();
23 
24  // is it for us?
25  if (not listening() and (datagram.source_address != config().destination)) {
26  return {};
27  }
28 
29  // is the payload a valid TCP segment?
30  TCPSegment seg;
31  if (ParseResult::NoError != seg.parse(move(datagram.payload), 0)) {
32  return {};
33  }
34 
35  // should we target this source in all future replies?
36  if (listening()) {
37  if (seg.header().syn and not seg.header().rst) {
38  config_mutable().destination = datagram.source_address;
39  set_listening(false);
40  } else {
41  return {};
42  }
43  }
44 
45  return seg;
46 }
47 
51  seg.header().sport = config().source.port();
52  seg.header().dport = config().destination.port();
53  _sock.sendto(config().destination, seg.serialize(0));
54 }
55 
ParseResult::NoError
@ NoError
Success.
fd_adapter.hh
std::move
T move(T... args)
TCPHeader::rst
bool rst
rst flag
Definition: tcp_header.hh:45
TCPSegment::serialize
BufferList serialize(const uint32_t datagram_layer_checksum=0) const
Serialize the segment to a string.
Definition: tcp_segment.cc:30
TCPOverUDPSocketAdapter::write
void write(TCPSegment &seg)
Writes a TCP segment into a UDP payload.
Definition: fd_adapter.cc:50
TCPOverUDPSocketAdapter::read
std::optional< TCPSegment > read()
Attempts to read and return a TCP segment related to the current connection from a UDP payload.
Definition: fd_adapter.cc:21
TCPHeader::dport
uint16_t dport
destination port
Definition: tcp_header.hh:38
TCPHeader::syn
bool syn
syn flag
Definition: tcp_header.hh:46
TCPSegment::parse
ParseResult parse(const Buffer buffer, const uint32_t datagram_layer_checksum=0)
Parse the segment from a string.
Definition: tcp_segment.cc:12
LossyFdAdapter
An adapter class that adds random dropping behavior to an FD adapter.
Definition: lossy_fd_adapter.hh:15
TCPHeader::sport
uint16_t sport
source port
Definition: tcp_header.hh:37
std
TCPSegment
TCP segment
Definition: tcp_segment.hh:10
TCPSegment::header
const TCPHeader & header() const
Definition: tcp_segment.hh:24