tcprecv - TCP Receiver

Table of Contents

A simple and reliable implementation of a TCP receiver from scratch in C++. Which is capable of reassembling datagrams in the environment where the network might reorder datagrams, drop them or deliver them more than once.

Building the project

git clone https://github.com/hyouteki/tcprecv
cd build && make

ByteStream

A ByteStream is a container that stores a collection of bytes from which bytes can be read or written to. These are some of the properties of this byte stream implementation:

Note: This ByteStream implementation should be used in a single threaded context; as locking or race conditions are not handled.

The interface for the ByteStream is available here byte_stream.hh and the implementation is available here byte_stream.cc.

Tests

cd build
ctest -R '^byte_stream'

Reassembler

Reassembler is responsible for reassembling the string of bytes obtained from the sender and storing it in the ByteStream. The TCP sender is dividing its byte stream up into short segments (substrings not more than 1460 bytes apiece) so that they can fit inside a datagram. In reality, the network might reorder these datagrams, drop them or deliver them more than once.

It is the job of the receiver to reassemble the segments into a contiguous stream of bytes that the sender originally intended. This is where reassembler comes into play. The Reassembler receives substrings from the sender which consists of a string of bytes along with an index of the first byte of the string that represents its position within the larger stream. As a simple example, let’s say the sender wants to send “abcdefgh” to the receiver and assume that the sender has divided the entire payload into two substrings “abcd” and “efgh”.

Now the indexes for these substream will be the following:

  1. [0] : abcd
  2. [4] : efgh

Now on the receiver end lets say the the datagrams are received in following order (efgh,4) -> (abcd,0). The Reassembler using these unique indexes will paste the substrings into the byte stream in the correct order (abcdefgh).

The interface for the Reassembler is available here stream_reassembler.hh and the implementation is available here stream_reassembler.cc

Tests

TCP receiver

TCP receiver accepts a TCPSegment from the sender and utilizes the Reassembler to correctly write the bytes into the byte stream. One thing to note is that apart from accepting the bytes, the TCPReciever also needs to generate messages back to the sender. These “receiver messages” are responsible for telling the sender:

The interface for the TCPReceiver is available here tcp_receiver.hh and the implementation is available here tcp_receiver.cc

Tests


RepositoryLicense GPL-3.0

Go to top File an issue