Contents

CS 144 Checkpoint 2 - TCP Receiver

“In this lab you’ll implement the “receiver” part of TCP, responsible for receiving messages from the sender, reassembling the byte stream (including its ending, when that occurs), and determining messages that should be sent back to the sender for acknowledgment and flow control.”

The Sequence Number is a sequence {Sn}nN\{S_n\}_{n \in \mathbb{N}} that satisfies

  1. S0=ISNS_0=\text{ISN} where ISN\text{ISN} is a random 32-bit unsigned integer.
  2. Si=(S0+i)mod232S_i=(S_0+i)\bmod 2^{32} for i>0i > 0.

The Absolute Sequence Numbers is a sequence {An}nN\{A_n\}_{n\in\mathbb{N}} that satisfies Ai=iA_i=i for all iNi \in \mathbb{N}.

Given an Absolute Sequence Number AiA_i, the corresponding Sequence Number

Si=(S0+i)mod232=(S0+Ai)mod232. \begin{align*} S_i&=(S_0 + i) \bmod 2^{32} \\ &= (S_0 + A_i) \bmod 2^{32}. \end{align*}

Conveniently, unsigned integer addition overflow is well defined in C++ and it behaves exactly like the modulo operation. Therefore, the implementation of Wrap32::wrap is simple:

wrapping_integers.cc

Wrap32 Wrap32::wrap( uint64_t n, Wrap32 zero_point )
{
  return zero_point + n;
}

The inverse of the operation is more difficult. Given a Sequence Number SiS_i, we cannot uniquely determine the index ii, because

Si=(S0+i)mod232=(S0+i+k232)mod232=Si+k232(kN). S_i=(S_0 + i) \bmod 2^{32} = (S_0 + i + k \cdot 2^{32}) \bmod 2^{32} = S_{i+k\cdot 2^{32}} (\forall k \in \mathbb{N}).

To uniquely determine the index, we are also given a checkpoint AcA_c. We need to find Aj=jA_j = j such that Si=SjS_i = S_j and AjAc|A_j - A_c| is minimised. We have

Si=(S0+i)mod232,i{s:s=Si+k232S0,s0,kN}=I,jI,AjAc=miniIAiAc. \begin{align*} S_i&=(S_0+i)\bmod 2^{32}, \\ i&\in\{s : s =S_i + k \cdot 2^{32} - S_0, s \geq 0, k \in \mathbb{N}\}=I, \\ j &\in I, |A_j-A_c|=\min_{i \in I} |A_i-A_c|. \end{align*}

To find jj, we have the following algorithm. We first find the smallest mm inside the set II, if it is greater than AcA_c, then we are done, because anything greater than mm will have a larger distance to AcA_c. If mm is less than AcA_c, we find j1Acj_1 \leq A_c and j2>Acj_2 > A_c such that j1+232=j2j_1 + 2^{32} = j_2, j1,j2Ij_1, j_2 \in I as two candidates for jj. We then compare AcAj1A_{c}-A_{j_1} and Aj2AcA_{j_2}-A_c and choose the smaller one as jj.

There isn’t much to talk about inside the TCP receiver. Checkout the test cases if anything is unclear in the specification.

This concludes Checkpoint 2.