\section{TCP Overview}\label{2-TCP Overview}
In this section we offer a brief description of the main functionality provided by the Transmission Control Protocol (TCP). TCP is a byte-stream, connection-oriented and reliable transport layer protocol commonly used over the unreliable Internet Protocol (IP), providing higher level end-to-end communication between systems. In particular, TCP ensures a reliable stream delivery service between processes located in different hosts without duplication or losing data. TCP is extensively used as the transport protocol by many popular application layer protocols, e.g. hypertext transfer protocol (HTTP), secure shell (SSH), simple mail transfer protocol (SMTP) or file transfer protocol (FTP).

TCP is a complex protocol, not only because of the requirement to provide a reliable transfer service, but also because a number of significant enhancements -such as congestion avoidance and delayed acknowledgement- have been made and proposed over the years. However, while such performance enhancing features have been added after the original design of TCP, its most basic operation has not changed significantly since its first specification RFC 675 in 1974, and the v4 specification RFC 793, published in September 1981 and to which this technical report will mainly refer~\cite{1}.

As mentioned, the primary purpose of the TCP is to provide reliable, securable logical circuit or connection service between pairs of processes. The implementation of the TCP functionality on top of a less reliable internet layer requires facilities in the areas of basic data transfer, reliability, flow control and multiplexing. As a consequence of traffic load balancing, network congestion or other unpredictable network behavior, network layer packets can be lost or delivered out of order. TCP is able to detect these issues and react by requesting lost packet retransmissions or rearranging out-of-order packets. Once the TCP receiver has finally reassembled a perfect copy of the data originally transmitted, it passes that segment to the application process, thus abstracting the application layer from the underlying details in the network.

Unlike its traditional unreliable datagram-oriented counterpart, User Datagram Protocol, where the data transmission can immediately start without prior preparation, TCP provides a connection-oriented service between two end points in which connections need to be established before sending data. TCP connections can be divided into three main phases: connection establishment, data transfer and connection termination.

Before TCP can be employed for data transmission, a connection must be set up between the two hosts that wish to exchange information. This initial phase, usually denominated connection establishment, involves a message exchange mechanism known as three-way handshake that allows a pair of TCP devices to move from a closed state to a established, ready-to-use connection. The TCP protocols on the hosts are required to exchange data concerning the parameters that will control how the connection operates, as well as the initial send and receive sequence numbers (ISS and IRS, respectively) each device is going to use (sequence number synchronization). This initial synchronization process requires each side to send its own initial sequence number and to receive a confirmation of it in acknowledgment from the other side. Reciprocally, each host must also receive the other device's initial sequence number and send a confirming acknowledgment.

Given that TCP places no restriction on a particular connection being used over and over again, new instances -usually referred to as incarnations- of an already existing connection between a couple of devices can be created over time. The problem that arises from this functionality issue is that the protocol requires to implement a mechanism in order to identify duplicate segments from previous incarnations of the connection, even if a TCP process crashes and loses all the information regarding the sequence numbers it had been using. This is achieved by making use of an initial sequence number generator which is bound to an internal clock whose low order bit is continuously incremented, cycling approximately every 4.55 hours. Through the use of such method, TCP is able to prevent segments from one incarnation of a connection from being used while the same sequence numbers may still be present in the network from an earlier incarnation.

Once the connection setup has been successfully completed and both communication sides have entered the ESTABLISHED state, the TCP connection is in its normal data transfer operation mode. Data Bytes are packaged into segments for transmission and the sliding window scheme is used as a means to control segment size and to provide flow control, data retransmission and congestion handling as required.

The TCP connection between a pair of hosts can remain open for a considerable amount of time, under the assumption that no problem occurs that forces the connection to be abnormally aborted. Eventually, however, one of the participating processes runs out of data to be sent and shuts down the connection by initiating the connection termination procedure. This mechanism involves an ordered sequence of operations of certain complexity to be performed by both communication devices. Given that TCP establishes a full-duplex channel, connection termination is implemented so that each end must release the channel independently, and therefore several steps are required to ensure that the connection has been shut down in both sides without any data being lost in the process. The initiation of the termination procedure by one host means that device is no longer transmit data, but can continue receiving information from the other side until it equally runs out of data to be transmitted. Such a methodology ensures that all segments that are pending to be sent by both participating hosts are transmitted before the communication ends.

TCP must be able to recover from transmitted segments that are damaged, lost, duplicated or received out of order from the internet network layer. The protocol provides a reliable delivery by adding a sequence number to each sent segment to coordinate which segment has been transmitted and received, and requiring a positive achnowledgement (ACK) from the receiver. TCP uses a cumulative acknowledgement scheme, where each ACK transmitted by the receiver signifies that the reception host has successfully processed all data preceding the acknowledged sequence number. When TCP sends a segment containing data, it stores a copy in a retransmission buffer and initializes a timer, waiting for the other end to acknowledge reception of the segment. If an acknowledgment is received before the timer runs out, the segment is deleted from the queue; otherwise, the data is retransmitted. Additionally, the receiver makes use of the sequence numbers to identify duplicates and rearrange a number of segments that might have been received out of order. Moreover, damaged TCP segments are handled by generating a checksum of the transmitted information prior to their transmission. This checksum enables the receiver to detect any modification of the data in transit, thus being able to discard damaged segments. If a segment arrives with an invalid checksum, the receiver discards it and doesn't acknowledge its reception.

TCP functionality also implements end-to-end flow control, providing the receiving host a mechanism to govern the amount of segments originated by the sender. Flow control is accomplished by using the sliding window mechanism. In each TCP segment, the receiver specifies in the receive window field the amount of additional octets that it is prepared to buffer for the connection. This window is utilized to limit the maximum number of octets a sender can transmit before a further acknowledgement and window update is received, thus preventing a fast host from overflowing or collapsing a slower receiver presenting available reception buffer constraints.

Additionally, TCP must be able to support handling multiple connections simultaneously. To allow many processes within a single host to make use of the TCP functionality at the same time, TCP uses the notion of port numbers, an unsigned number associated to each side of a TCP connection. Given that the port identifiers are selected by each TCP in an independent fashion, they might not be unique. To ensure the use of unique addresses within each TCP, the host internet address from the internet layer is concatenated with a port identifier to create a socket which will be unique throughout all networks connected together. Thus, an active connection can be uniquely identified by a pair of sockets. The binding of ports to processes is handled independently by each host, and arriving data packets are identified as belonging to a specific TCP connection through its socket.

The reliability and flow control mechanisms require TCP to initialize and maintain certain status information, including sockets, sequence numbers, and window sizes. Given that each connection is distinct, it is necessary to maintain data about every active connection separately in each device. For this purpose, TCP uses a specific data structure called Transmission Control Block (TCB). A TCB contains all the important information required for the administration of the connection, such as the pair of sockets that identify it and the pointers to buffers where incoming and outgoing data are temporarily stored. The TCB is used as well to implement the sliding window mechanism, holding variables that keep track of the current transmission window size, bytes received and acknowledged or bytes received but not yet acknowledged.
\begin{table*}
\begin{center}
    \textbf{Table I. }Constants used in the TCP implementation
\end{center}
\centering
\begin{tabular}{l|l}
\hline
\textbf{Constant}&\textbf{Value}\\ \hline
Retransmission Timeout (RTO)&12 sec\\ \hline
Payload&30 bytes\\ \hline
Transmission Window Size&1\\ \hline
Connection Establishment Timeout&10 sec\\ \hline
Connection Termination Timeout&10 sec\\ \hline
TCB List Size&10\\ \hline
Retransmission Buffer Size&20\\ \hline
\end{tabular}
\end{table*}
