.. _TransLayer:

.. raw:: html

   <script>ODSA.SETTINGS.DISP_MOD_COMP = true;ODSA.SETTINGS.MODULE_NAME = "TransLayer";ODSA.SETTINGS.MODULE_LONG_NAME = "Transport Layer";ODSA.SETTINGS.MODULE_CHAPTER = "The Internet and Connectivity"; ODSA.SETTINGS.BUILD_DATE = "2021-06-01 12:51:47"; ODSA.SETTINGS.BUILD_CMAP = false;JSAV_OPTIONS['lang']='en';JSAV_EXERCISE_OPTIONS['code']='java';</script>


.. |--| unicode:: U+2013   .. en dash
.. |---| unicode:: U+2014  .. em dash, trimming surrounding whitespace
   :trim:


.. This file is part of the OpenCSF eTextbook project. It was
.. auto-generated by scripts from the OpenDSA eTextbook project.
.. See https://OpenCSF.org for more details. OpenCSF is distributed
.. under a Creative Commons Attribution-NonCommercial 4.0 International
.. License (see http://creativecommons.org/licenses/by-nc/4.0/),
.. Copyright (c) 2019-2021 by Michael S. Kirkpatrick. OpenDSA is
.. distributed under an MIT open source license, Copyright (c) 2012-2021
.. by the OpenDSA Project Contributors.

.. avmetadata::
   :author: Michael S. Kirkpatrick
   :requires:
   :satisfies: 
   :topic: 

Transport Layer
===============

Up to this point, we have used application-layer socket programming essentially as another form of
IPC for exchanging data between processes on different hosts. Communication at that level can follow
a protocol that defines human-readable message formats, such as HTTP. Other applications, such as
DNS or DHCP, exchange highly structured binary messages that are intended to be interpreted by peer
processes. In either regard, the communication is specific to that application, and there is an
assumption that "the network"—a mysterious, almost magical entity—transmits the data. That is,
application-layer network programming can be easily conflated with other forms of IPC, replacing the
OS with the network as the service provider. There is truth to this conflation, as some layers and
protocols (including TCP and IP) are typically implemented within the OS. However, other layers are
implemented in hardware, including network interface cards, cables between devices, and radios.

The :term:`transport layer` provides the first level of abstraction that application-layer
programmers rely on. Specifically, the transport layer establishes the logical structure of a
virtual :term:`end-to-end communication` channel between processes. When you use a web browser to
access ``www.example.com``, you are intending to communicate with a particular process running on
that other host, not just the machine in general; you would probably be surprised if your web
browser suddenly began showing you binary DHCP or DNS responses instead of HTML-formatted
information. The transport layer ensures that your request gets delivered to the process running the
web server on the remote host, while returning the web page to your web browser process.

In contrast to a monolithic entity like an OS, the network is a layered architecture of distributed
components that cooperate to exchange data. For a variety of reasons, the distributed nature of
these components causes failed attempts at a significantly higher rate than an OS would ever
encounter. As an example, consider the effect of someone tripping over the cord of a server and
unplugging it; there is no possible way for your laptop to communicate with another machine that is
turned off. As a less extreme example, the message might not actually be lost, but it may encounter
a delay in the network that causes it to arrive too late. Failures and disruptions like this can
happen at any time. One key feature for choosing a transport layer protocol is whether or not the
application is requesting a :term:`reliable transport` service that attempts to correct failures
that occur. UDP provides fast but :term:`unreliable transport`, while TCP features reliable
transport.

Unreliable Transport: UDP
-------------------------

The simpler approach to transport-layer service is to provide an unreliable transport, which is the
purpose of the :term:`User Datagram Protocol` (UDP), defined in RFC 768.  UDP provides a fast
*best effort* delivery service, which is a polite way to say that it will try but it makes
no guarantees. UDP provides a minimal amount of end-to-end error checking to determine if the full
payload has been received and the contents have not been corrupted during transmission.

.. _tbl5-2:

.. raw:: html

   <center>
   <div class="col-md-6">
   <table class="table table-bordered">
     <thead class="jmu-dark-purple-bg text-light">
       <tr>
         <th class="p-0 center" style="width: 50%">0-15</th>
         <th class="p-0 center" style="width: 50%">16-31</th>
       </tr>
     </thead>
     <tbody>
       <tr>
         <td class="py-0 center"><code>source port</code></td>
         <td class="py-0 center"><code>destination port</code></td>
       </tr>
       <tr>
         <td class="py-0 center"><code>length</code></td>
         <td class="py-0 center"><code>checksum</code></td>
       </tr>
       <tr>
         <td class="py-0 center" colspan="2"><code>payload</code><br />(application-layer data)</td>
       </tr>
     </tbody>
   </table>
   <p>
   Table 5.2: Structure of a UDP segment
   </p>
   </div>
   </center>

`Table 5.2 <#tbl5-2>`_ shows the structure of a UDP segment. The datagram contains a header with
four fields: the port numbers of the source and destination processes, the length of the segment
(including both the header and the payload), and a 16-bit checksum calculation. The following
examples show the UDP segment for a DNS request from the previous chapter.

.. topic:: Example

   .. figure:: Images/CSF-Images-Example.png
      :align: left
      :width: 100%
      :alt: Decorative example icon

   In this example, the client had opened ephemeral port 5000 (``0x1388``) and sent the request to the
   OpenDNS server, which was listening on port 53 (``0x0035``). The header requires eight bytes and the
   request payload was 29 bytes, so the total length of the UDP segment was 37 bytes; the response was
   53 bytes, due to the longer response in the payload.

   .. raw:: html

      <center>
      <table class="table table-bordered">
        <tbody>
          <tr>
            <td class="py-0">Header</td>
            <td class="py-0" width="40%"><code>1388<br />0035<br />0025<br />f693</code></td>
            <td class="py-0" width="50%"><code>source port = 5000 (0x1388)<br />destination port = 53 (0x0035)<br />length = 37 (0x0025)<br />checksum</code></td>
          </tr>
          <tr>
            <td class="py-0">Payload</td>
            <td class="py-0"><code>1234 0100 0001 0000 0000 0000<br />0765 7861 6d70 6d64 0363 6f6d<br />0000 0100 01</code></td>
            <td class="py-0">DNS request for <code>example.com</code></td>
          </tr>
        </tbody>
      </table>
      <table class="table table-bordered">
        <tbody>
          <tr>
            <td class="py-0">Header</td>
            <td class="py-0" width="40%"><code>0035<br />1388<br />0035<br />af04</code></td>
            <td class="py-0" width="50%"><code>source port = 53 (0x0035)<br />destination port = 5000 (0x1388)<br />length = 53 (0x0035)<br />checksum</code></td>
          </tr>
          <tr>
            <td class="py-0">Payload</td>
            <td class="py-0"><code>1234 8180 0001 0001 0000 0000<br />0765 7861 6d70 6d64 0363 6f6d<br />0000 0100 01c0 0c00 0100 0100<br />00e9 4900 045d b8d8 22</code></td>
            <td class="py-0">DNS response for <code>example.com</code></td>
          </tr>
        </tbody>
      </table>
      </center>


The ``checksum`` value is the result of repeated one's complement addition of 16-bit values in the
UDP segment, as shown in `Code Listing 5.1 <#cl5-1>`_. In UDP, the checksum is evaluated over the
payload, parts of the IP header (which we are ignoring here, as we have not examined IP yet), and a
UDP :term:`pseudo-header`. The UDP pseudo-header contains the ``source port``, ``destination port``,
and the ``length`` fields of the regular UDP header. The ``checksum`` field is replaced with a
16-bit value containing information about the protocol, which is defined in RFC 762; for UDP, this
16-bit value is 0x0011. Regarding the payload, if there are an odd number of bytes (as shown in the
previous messages), that last byte is concatenated with zeroes to create a 16-bit value.

.. _cl5-1:

.. codeinclude:: Internet/CodeListing-5-1.c
   :linenos: true

`Table 5.3 <#tbl5-3>`_ illustrates the mechanics of how one's complement addition works by adding the
values ``0x7d09`` and ``0xb5fc.`` The result of this calculation would be ``0x13305`` based on
binary arithmetic that does not require a fixed size. However, UDP checksums are fixed at a length
of 16 bits. Whenever the addition would produce a carry out of 1 (requiring a 17\ :sup:`th` bit),
that bit is folded around to the least-significant bit location. Consequently, the leading bit of
``0x13305`` would be removed (creating ``0x3305``) and added to the least-significant bit, yielding
the result ``0x3306``.

.. _tbl5-3:
 
.. raw:: html

   <center>
   <div class="col-md-8">
   <table class="table table-bordered">
     <tbody>
       <tr>
         <td class="py-0 center"><code>7d09</code></td>
         <td class="py-0 center">&nbsp;</td>
         <td class="py-0 center"><code>0 1 1 1</code></td>
         <td class="py-0 center"><code>1 1 0 1</code></td>
         <td class="py-0 center"><code>0 0 0 0</code></td>
         <td class="py-0 center"><code>1 0 0 1</code></td>
       </tr>
       <tr>
         <td class="py-0 center"><code>b5fc</code></td>
         <td class="py-0 center">&nbsp;</td>
         <td class="py-0 center"><code>1 0 1 1</code></td>
         <td class="py-0 center"><code>0 1 0 1</code></td>
         <td class="py-0 center"><code>1 1 1 1</code></td>
         <td class="py-0 center"><code>1 1 0 0</code></td>
       </tr>
       <tr>
         <td class="py-0 center"><code>13305</code></td>
         <td class="py-0 center"><code>1</code></td>
         <td class="py-0 center"><code>0 0 1 1</code></td>
         <td class="py-0 center"><code>0 0 1 1</code></td>
         <td class="py-0 center"><code>0 0 0 0</code></td>
         <td class="py-0 center"><code>0 1 0 1</code></td>
       </tr>
       <tr>
         <td class="py-0 center"><code>3306</code></td>
         <td class="py-0 center">&nbsp;</td>
         <td class="py-0 center"><code>0 0 1 1</code></td>
         <td class="py-0 center"><code>0 0 1 1</code></td>
         <td class="py-0 center"><code>0 0 0 0</code></td>
         <td class="py-0 center"><code>0 1 1 0</code></td>
       </tr>
     </tbody>
   </table>
   <p>
   UDP checksum for two 16-bit values
   </p>
   </div>
   </center>

The UDP checksum provides a minimal form of error-checking for end-to-end communication. As we will
note later in this chapter, both IP and Ethernet also provide error detection. As such, if the
segment is sent with these network and link layer protocols, UDP is performing a redundant service.
Furthermore, as UDP will make no attempt to correct the errors it detects, the UDP checksum may seem
to be pointless redundancy. This criticism is fair, although UDP can also be used with other network
or link-layer protocols that do not perform error correction. One disadvantage of a layered
architecture, such as the Internet protocol stack, is that the individual layers must be designed
without underlying assumptions of the other layers.

At first glance, UDP's unreliable transport service may appear to be a poor choice, and for certain
applications it is. But UDP provides service that is good enough for other applications, while
avoiding the overhead penalties associated with a reliable transport. For instance, UDP is commonly
used in streaming multimedia applications that only require *most* of the data to be successfully
transmitted; the application can compensate for lost data by providing slightly worse quality, such
as pixelated images or temporarily pausing the media until more data can be received and buffered.
The application can detect the lost or corrupted data by checking the return value from
``recvfrom()``, sending new requests only as needed. UDP is also commonly used in application-layer
services that users do not typically interact with directly, such as DNS or DHCP.

Reliable Transport: TCP
-----------------------

Although UDP provides a lightweight, fast transport service, the unreliability is simply not
appropriate for some applications. Consider how frustrating it would be to visit a web page that was
missing a portion of text; if the missing data occurred in the middle of HTML formatting or link
tags, then the appearance would be wrong or links to other web pages would be broken. As such, many
applications require the reliable transport service provided by the :term:`Transmission Control
Protocol` (TCP), which is defined primarily in RFC 793. TCP is a :term:`connection-oriented <connection-oriented protocol>`
protocol, indicating that the hosts maintain some form of state between messages. That is, the hosts
create a connection to establish a :term:`session` that is likely to contain multiple messages sent
back and forth between the hosts; during this session, hosts may resend messages that are lost or
corrupted, while also taking steps to avoid overwhelming each other with too much data at any time.
The full operation of TCP is rather complex, but we will restrict our focus to the key concepts of
*reliable data delivery* and :term:`flow control`.

As with UDP, TCP segments include the 16-bit ``source`` and ``destination port`` numbers to designate the
processes at either end of the connection, as shown in `Table 5.4 <#tbl5-4>`_. Similarly, the TCP
header contains a ``checksum`` that is used to detect errors that may have occurred in transmission; in
contrast to UDP, the TCP pseudo-header is based on information from the IP header. The ``urgent data
pointer`` is not used in modern practice, and the ``optional fields`` are beyond the scope of this book.
The other fields are explained below to illustrate the functioning of TCP reliable transport and flow control.

.. _tbl5-4:

.. raw:: html

   <center>
   <div class="col-md-6">
   <table class="table table-bordered">
     <thead class="jmu-dark-purple-bg text-light">
       <tr>
         <th class="p-0 center" style="width: 25%">0-7</th>
         <th class="p-0 center" style="width: 25%">8-15</th>
         <th class="p-0 center" style="width: 25%">16-23</th>
         <th class="p-0 center" style="width: 25%">24-31</th>
       </tr>
     </thead>
     <tbody>
       <tr>
         <td class="py-0 center" colspan="2"><code>source port</code></td>
         <td class="py-0 center" colspan="2"><code>destination port</code></td>
       </tr>
       <tr>
         <td class="py-0 center" colspan="4"><code>sequence number (SEQ)</code></td>
       </tr>
       <tr>
         <td class="py-0 center" colspan="4"><code>acknowledgement number (ACK)</code></td>
       </tr>
       <tr>
         <td class="py-0 center" colspan="2"><code>flags</code></td>
         <td class="py-0 center" colspan="2"><code>receive window</code></td>
       </tr>
       <tr>
         <td class="py-0 center" colspan="2"><code>checksum</code></td>
         <td class="py-0 center" colspan="2"><code>urgent data ptr</code></td>
       </tr>
       <tr>
         <td class="p-0 center" colspan="4"><div class="xborder-highlight"><code>optional fields</code></div></td>
       </tr>
       <tr>
         <td class="py-0 center" colspan="4"><code>payload</code><br />(application-layer data)</td>
       </tr>
     </tbody>
   </table>
   <p>
   Table 5.4: Structure of a TCP segment
   </p>
   </div>
   </center>

In TCP segments, the :term:`sequence number` (``SEQ``) is an identifier associated with the current
segment. Each host randomly chooses an initial value for the sequence number. Each time a host sends
a segment, it increments its internal counter for the sequence number by the size of the payload.
For example, consider a client application that uses ``SEQ=25`` for a segment containing the five
bytes ``"Hello"``. If the client's next segment is to sends the seven bytes ``"Goodbye"``, it would
use ``SEQ=30``. As this segment contains seven bytes, the client's next segment would use
``SEQ=37``. In short, the sequence number denotes the order and the size of the application-layer
payload data.

.. _InternetTCP:

.. figure:: Images/CSF-Images.5.2.png
   :align: right
   :width: 90%
   :figwidth: 35%
   :alt: A TCP data exchange of four messages

   A TCP data exchange of four messages

At the other end, the :term:`acknowledgement number` (``ACK``) allows a receiving host to inform the
sender that the segment was received. In the previous scenario, when the server receives the
client's ``"Hello"`` segment (five bytes sent with ``SEQ=25``), the server's next segment would
contain the ``ACK=30`` (``SEQ + 5``). :num:`Figure <Figure #InternetTCP>` shows the sample exchange that we
have been describing. The client's first segment used ``SEQ=25``; the client sent the ``ACK=42``,
indicating that was the value it expected for the server's next segment. (This ``ACK`` is based on
segments shown before this sequence.) The server then responds with a segment that does, in fact,
use ``SEQ=42``. Since this segment (``"Hello back"``) contains 10 bytes, the client indicates it
received the segment by using ``ACK=52`` in its next segment.

During the session, the OS on each host maintains a buffer for storing data until the application
reads it from the socket. As this buffer is finite in size, the hosts need to cooperate to prevent a
buffer overflow. The :term:`receive window` achieves this by declaring the maximum number of bytes
that the sender is capable of receiving in the next segment. To observe how this value is used,
consider the following analogous line of code when working with strings:

.. codeinclude:: Internet/Strncpy.c

The third parameter indicates the maximum number of bytes that will be copied into the ``buffer``.
For instance, if the ``buffer`` has space for 20 bytes, but the ``input`` is a string that is 50
bytes in length, the third parameter prevents the additional 30 bytes from being written beyond the
end of the ``buffer``. The receive window serves the same purpose within the context of TCP
segments. If the receiver's next segment exceeds this size, then that host would need to break up
its response and send it across multiple segments. This cooperation is the TCP :term:`flow control`
service, as each host takes steps to avoid sending too much data at a time.

There is subtle point regarding flow control that is easily misunderstood. Consider the sequence of
segments in :num:`Figure <Figure #InternetTCP>`. In that scenario, the server sent two segments to the
client: one containing the string ``"Hello back"`` and one containing ``"Farewell"``. One possible
explanation is that the application issued two system calls to write to the socket. Flow control
provides another explanation. Although this string in particular is unlikely, the server application
may have written the string ``"Hello backFarewell"`` to the socket. The client's first segment
(sequence number 25) may have included a receive window indicating it only had space for 10 bytes,
so the server's TCP implementation split the segment. The key point is that TCP itself provides
flow, and the application is typically not made aware it is happening. The sender splits the segment
without informing the application, and the receiving host's TCP implementation concatenates all
segments in its internal buffers as needed.

.. _TransTCPExample:

.. topic:: Example

   .. figure:: Images/CSF-Images-Example.png
      :align: left
      :width: 100%
      :alt: Decorative example icon

   The example shown here illustrates the full TCP header and payload for an HTTP ``GET`` request sent to ``example.com``. The destination port number is 80, which is the well-known port number for HTTP, whereas the source port is an ephemeral port number, generated by the client's OS. The receive window indicates that the client is requesting a maximum of 4096  bytes in response. The ``flags`` field will be explained in the next discussion on the TCP handshake, and the ``urgent data pointer`` field is not used. The ``payload`` here contains the HTTP application-layer request.

   .. raw:: html

      <center>
      <table class="table table-bordered">
        <tbody>
          <tr>
            <td class="py-0">Header</td>
            <td class="py-0" width="40%"><code>1388<br />0050<br />0000 0017<br />0000 002a<br />5010<br />1000<br />cf33<br />0000</code> </td>
            <td class="py-0" width="50%"><code>source port = 5000 (0x1388)<br />destination port = 80 (0x0050)<br />sequence number = 23 (0x17)<br />acknowledgement number = 42 (0x2a)<br />flags<br />receive window = 4096 (0x1000)<br />checksum<br />urgent data ptr</code></td>
          </tr>
          <tr>
            <td class="py-0">Payload</td>
            <td class="py-0"><code>4745 5420 2f20 4854 5450 2f31<br />2e31 0d0a 486f 7374 3a20 6578<br />616d 706c 652e 636f 6d0d 0a43<br />6f6e 6e65 6374 696f 6e3a 2063<br />6c6f 7365 0d0a 0d0a</code></td>
            <td class="py-0"><code>GET / HTTP/1<br />.1\r\nHost: ex<br />ample.com\r\nC<br />onnection: c<br />lose\r\n\r\n</code></td>
          </tr>
        </tbody>
      </table>
      </center>


TCP Handshake and Connections
-----------------------------

Unlike the connectionless UDP that allows applications to send and receive data at any time, TCP
requires that the two hosts must first establish a connection to begin a communication session. That
is, prior to exchanging application-layer data, the hosts must send and receive some initial
segments to ensure both hosts know to expect the data exchange. At each end, this procedure involves
allocating internal buffers and variables, such as those needed for flow control. Just as
importantly, the hosts must also execute the :term:`TCP handshake`, a lightweight initial protocol
that allows the hosts to declare their initial sequence numbers, receive windows, and other values.

The TCP handshake is implemented by exchanging empty segments with particular bits set in the TCP
header. Specifically, recall from `Table 5.4 <#tbl5-4>`_ that the TCP header contains a 16-bit
``flags`` field. `Table 5.5 <#tbl5-5>`_ shows the internal structure of this field for a normal TCP
segment. The ``data offset`` field denotes the length of the TCP header in terms of 32-bit words.
The standard header, which contains no optional fields, has a length of five words; that constitutes
the minimum value of this field. If optional fields are used, this value can increase to 15, which
declares that TCP headers can be no longer than 60 bytes. The remainder of this section describes
the use of the ``ACK``, ``SYN``, and ``FIN`` bits.

.. _tbl5-5:

.. raw:: html

   <center>
   <div class="col-md-10">
   <table class="table table-bordered">
     <thead class="jmu-dark-purple-bg text-light">
       <tr>
         <th class="p-0 center bg-light text-dark">Index</th>
         <th class="p-0 center" colspan="4">0-3</th>
         <th class="p-0 center" colspan="6">4-9</th>
         <th class="p-0 center" colspan="6">10-15</th>
       </tr>
     </thead>
     <tbody>
       <tr>
         <th class="py-0 px-2 center bg-light">Meaning</th>
         <td class="py-0 center" colspan="4">data<br />offset</td>
         <td class="py-0 center" colspan="6">unused</td>
         <td class="py-0 center"><code>U<br />R<br />G</code></td>
         <td class="py-0 center"><code>A<br />C<br />K</code></td>
         <td class="py-0 center"><code>P<br />S<br />H</code></td>
         <td class="py-0 center"><code>R<br />S<br />T</code></td>
         <td class="py-0 center"><code>S<br />Y<br />N</code></td>
         <td class="py-0 center"><code>F<br />I<br />N</code></td>
       </tr>
       <tr>
         <th class="py-0 px-2 center bg-light">Value</th>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>1</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>1</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
         <td class="py-0 center"><code>0</code></td>
       </tr>
       <tr>
         <th class="py-0 px-2 center bg-light">Hex</th>
         <td class="py-0 center" colspan="4"><code>5</code></td>
         <td class="py-0 center" colspan="4"><code>0</code></td>
         <td class="py-0 center" colspan="4"><code>0</code></td>
         <td class="py-0 center" colspan="4"><code>0</code></td>
       </tr>
     </tbody>
   </table>
   <p>
   Table 5.5: Structure of the 16-bit TCP flags field
   </p>
   </div>
   </center>

.. _InternetHandshake:

.. figure:: Images/CSF-Images.5.3.png
   :align: right
   :width: 90%
   :figwidth: 40%
   :alt: The TCP handshake

   The TCP handshake

The TCP handshake is initiated by a client application that calls the ``connect()`` socket function
described in the previous chapter. The steps of the TCP handshake are shown in :num:`Figure
<Figure #InternetHandshake>` and consists of three segments, commonly referred to as ``"SYN"``,
``"SYN-ACK"``, and ``"ACK"`` because of the bits they set. Note that this figure uses ``"ACKnum"``
to refer to the 32-bit acknowledgement number and ``"ACK"`` to denote the bit in the 16-bit flags
field. A client initiates a TCP connection by sending a SYN segment (``"synchronize"``) that
contains its randomly chosen ``SEQ`` value, setting ``SYN=1`` and putting ``0x5002`` into the 16-bit
flags field. The server acknowledges receiving the synchronization request (setting ``SYN=1`` and
``ACK=1`` to get ``0x5012``) and indicates its own initial sequence number. The client then responds
again with the ``ACK=1`` (``0x5010``). Note that all of these segments use an empty payload, but the
sequence numbers are incremented as if they contained a single byte.

After completing the TCP handshake, the client and server share a logical connection. Both hosts
know each other's sequence numbers and initial receive window sizes. The OS on both hosts has
established internal buffers, variables, and other data structures as needed. This internal state is
maintained until the two parties close the connection. To close the connection, one host sends a
segment with an empty payload and the *finish* bit ``FIN=1``; the other host responds with an
``ACK=1`` segment. These two segments are then repeated, but with the other party sending the
``FIN=1`` segment. As such, either side can sever the connection at any time.

.. topic:: Note

   .. figure:: Images/CSF-Images-Note.png
      :align: left
      :width: 100%
      :alt: Decorative note icon

   A common misunderstanding is that the ``ACK`` bit is used only in the TCP handshake or the
   ``FIN-ACK`` closing exchange. That is not corret. The ``ACK`` bit is set any time that the
   acknowledgement number is significant. That is, any time that the sender intends for the receiver
   to interpret this number as an acknowledgement of a previous message, the sender sets the ``ACK``
   bit.

Recall the distinction between HTTP/1.0 and HTTP/1.1 discussed in the previous chapter. HTTP
applications use TCP for their reliable transport layer. As such, any HTTP data exchange (such as
accessing a web page, as well as retrieving the needed images and script files, require an initial
TCP handshake to establish the connection. In HTTP/1.0, every object retrieved requires its own TCP
connection session; even if all of the objects are stored on the same server, the seven segments
(three for the handshake and four to close the connection) all must be performed, and both hosts' OS
must repeated set up and destroy internal buffers and data structures. With HTTP/1.1, the TCP
handshake is only done once per server. The connection is maintained until the client application
sends a segment containing the HTTP header ``"Connection: close"``. The server and client then
exchange segments to close the connection.

.. topic:: Example

   .. figure:: Images/CSF-Images-Example.png
      :align: left
      :width: 100%
      :alt: Decorative example icon

   This example illustrates the flow of a TCP handshake to set up an HTTP request. As in :num:`Example 
   #TransTCPExample`, the source and destination port numbers consist of the well-know port 80 and
   an ephemeral port. When the client (typically a web browser) establishes the connection, it starts
   by picking a random sequence number (4973 in this case) and using 0 as the acknowledgement number.
   The client sends an empty request (i.e., there is no payload and the message is just the TCP
   header) to the server as a ``SYN`` request.

   The server responds with a ``SYN-ACK`` that sets both of these bits in the ``flags`` field. (Observe
   that the port numbers reverse in this middle message to indicate the direction switched to be
   "server to client.") As with the ``SYN`` request, the server selects a random initial sequence
   number (627). The acknowledgement number here is the client's sequence number incremented by 1
   (4973 + 1). Finally, the client completes the handshake with an ``ACK`` message back to the server.
   This ACK message uses the incremented sequence number (4974) and the incremented acknowledgement of
   the server's sequence number (628). At this point, the connection is established and both hosts
   have established the sequence and acknowledgement numbers for future messages.
   

   .. raw:: html

      <center>
      <table class="table table-bordered">
        <tbody>
          <tr>
            <td class="py-0"><code>SYN</code> request (client to server)</td>
            <td class="py-0" width="40%"><code>1388<br />0050<br />0000 136d<br />0000 0000<br />5002<br />1000<br />2e67<br />0000</code></td>
            <td class="py-0" width="50%"><code>source port = 5000 (0x1388)<br />destination port = 80 (0x0050)<br />sequence number = 4973 (0x136d)<br />acknowledgement number = 0<br />flags = SYN<br />receive window = 4096 (0x1000)<br />checksum<br />urgent data ptr<br /></code></td>
          </tr>
        </tbody>
      </table>
      <table class="table table-bordered">
        <tbody>
          <tr>
            <td class="py-0"><code>SYN-ACK</code> response (server to client)</td>
            <td class="py-0" width="40%"><code>0050<br />1388<br />0000 0273<br />0000 136e<br />5012<br />1000<br />2be3<br />0000</code></td>
            <td class="py-0" width="50%"><code>source port = 80 (0x0050)<br />destination port = 5000 (0x1388)<br />sequence number = 627 (0x273)<br />acknowledgement number = 4973 (0x136d)<br />flags = SYN and ACK<br />receive window = 4096 (0x1000)<br />checksum<br />urgent data ptr</code></td>
          </tr>
        </tbody>
      </table>
      <table class="table table-bordered">
        <tbody>
          <tr>
            <td class="py-0"><code>ACK</code> response (client to server)</td>
            <td class="py-0" width="40%"><code>1388<br />0050<br />0000 136e<br />0000 0274<br />5010<br />1000<br />2bf6<br />0000</code></td>
            <td class="py-0" width="50%"><code>source port = 5000 (0x1388)<br />destination port = 80 (0x0050)<br />sequence number = 4974 (0x136d)<br />acknowledgement number = 628 (0x274)<br />flags = ACK<br />receive window = 4096 (0x1000)<br />checksum<br />urgent data ptr</code></td>
          </tr>
        </tbody>
      </table>
      </center>


TCP Timeout and Packet Loss
---------------------------

The combination of the sequence number (``SEQ``) acknowledgement number (``ACK``) and the
``checksum`` forms a rudimentary error detection scheme to support reliable transport. If a host
receives a segment with unexpected values in any of these fields, it can determine that *something*
has gone wrong in the data exchange. While the receiver may not be able to determine the exact cause
of the problem, the incorrect information can provide some guidance:

 * **Incorrect ACK**: The sender may be indicating that the previous segment was corrupted or not
   received. For example, assume a client sends ``"Hello"`` with ``SEQ=10``; if the response contains
   ``ACK=10``, the ``"Hello"`` segment has not been acknowledged and should be re-sent.

 * **Incorrect SEQ**: A previous segment from the server may be delayed or lost, and the server is
   not aware of this fact. As an example, assume a client is expecting ``SEQ=42`` based on a previous
   segment from the server. If the next segment it receives has ``SEQ=52``, then there is some data
   the client has not received. The missing data could be 10 one-byte segments or a single 10-byte
   segment; the client cannot know which is true and does not actually need to. Again, the client
   could re-send its last segment that contained ``ACK=42``, effectively refusing to acknowledge the
   newer data until the lost data is recovered.

 * **Incorrect checksum**: Some part of the TCP segment or the payload has been corrupted or removed
   in some way. As with the previous two cases, re-sending the last segment based on acknowledged data
   serves as a request for the server to repeat its own segment.

.. _InternetResend:

.. figure:: Images/CSF-Images.5.4.png
   :align: right
   :width: 90%
   :figwidth: 40%
   :alt: The client resends the message if the acknowledgement is lost

   The client resends the message if the acknowledgement is lost

:num:`Figure <Figure #InternetResend>` illustrates the notion of :term:`packet loss`, another common
scenario involving TCP reliability. In this case, the server received and acknowledge the client's
segment, but the client did not receive the server's response. As a result, the client re-sent the
segment as a second attempt. From an omniscient perspective of all network traffic, this segment
seems unnecessary: We can observe that the server did, in fact, receive the segment the first time.
However, such a perspective is impossible; hosts can only observe the segments they send and
receive. From the client's perspective, it is possible that the first segment—not the reply—was lost.

This simple scenario illustrates a critical design question for reliable transport: how long must
the client want before declaring a packet lost? Hosts need to wait long enough for packets to
traverse the network physically but waiting too long for a lost packet delays the recovery process.
To complicate matters further, network conditions can change, so determining the optimal amount of
time to wait is a moving target.

The original TCP specification in RFC 793 proposed measuring the :term:`round-trip time` (RTT),
which is the amount of time that elapses between sending a segment and receiving the response. As
the host sends and receives additional segments, it computes a :term:`smoothed round-trip time`
(SRTT) as a rolling average of these delays. Based on an update in RFC 6298, in modern systems, the
SRTT is initialized to the first RTT measurement. Once a new measurement (denoted as $R'$) is made,
the SRTT is updated according to the following formula, with $\\alpha$ typically set to 1/8.

.. raw:: html

   <center>
   <span class="math inline">$\large SRTT := (1 - \alpha) SRTT + \alpha R'$</span>
   </center>
   <br />

.. _RTTExample:

.. topic:: Example

   .. figure:: Images/CSF-Images-Example.png
      :align: left
      :width: 100%
      :alt: Decorative example icon

   To illustrate this calculation, assume that the first measurement is $R =
   4\ ms$. This value becomes the initial SRTT. The table below shows the changes that would occur if
   the next three segments arrive with updated $R'$ values of 10 ms,
   20 ms, and 2 ms, in that order.Note that all time units get rounded based on the granularity of the
   clock, which is assumed to be 1 ms in this case.

   .. raw:: html
   
      <center>
      <table class="table table-bordered">
        <thead class="jmu-dark-purple-bg text-light">
          <tr>
            <th class="p-0 center">Old SRTT</th>
            <th class="p-0 center"><span class="math inline">$R'$</span></th>
            <th class="p-0 center">Updated SRTT</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="py-0 center">4</td>
            <td class="py-0 center">10</td>
            <td class="py-0">(7/8) * 4 + (1/8) * 10 = 4.75&nbsp;&nbsp;&nbsp;<i>[rounded to 5]</i></td>
          </tr>
          <tr>
            <td class="py-0 center">5</td>
            <td class="py-0 center">20</td>
            <td class="py-0">(7/8) * 5 + (1/8) * 20 = 6.875&nbsp;&nbsp;&nbsp;<i>[rounded to 7]</i></td>
          </tr>
          <tr>
            <td class="py-0 center">7</td>
            <td class="py-0 center">2</td>
            <td class="py-0">(7/8) * 7 + (1/8) * 2 = 6.375&nbsp;&nbsp;&nbsp;<i>[rounded to 6]</i></td>
          </tr>
        </tbody>
      </table>
      <p>
      Table 5.6: SRTT calculations for a sequence of three received segments
      </p>
      </center>

The SRTT serves as an estimate for predicting the next RTT. The coefficients $\\alpha$ and $(1 -
\\alpha)$ act as a relative weighting factor that influences how much the new observed value changes
the estimate. Increasing the value of $\\alpha$ places more weight on the most recent measurement,
thus decreasing then influence of historical values. On the other hand, decreasing $\\alpha$ has the
opposite effect, relying more on the new RTT and less on the historical values.

The new SRTT value is used to update the retransmission timeout (RTO), which is the amount of time
that the host will wait before declaring a lost packet. While RFC 793 proposed a formula for
calculating RTO based just on SRTT, RFC 6298 updated this calculation. One problem with the original
formulation is that it does not consider the impact of variance in RTT. For example, a system that
repeatedly experiences an RTT of 20 ms every time should not be treated the same as one that
alternates between 1 ms and 39 ms, despite both systems having the same average RTT. To compensate
for this difference, RFC 6298 defines another rolling average, the RTT variation (RTTVAR), using
$\\beta = 1/4$:

.. raw:: html

   <center>
   <span class="math inline">$\large RTTVAR := (1 - \beta) RTTVAR + \beta | SRTT - R' |$</span>
   </center>
   <br />

To make sense of this factor, consider the role of the absolute value $\| SRTT – R' \|$. This value
denotes the difference between the predicted RTT (SRTT) and the RTT that was actually observed
($R'$). If the prediction was perfect every time, then this absolute value would be 0. Then, each
new segment would shrink the RTTVAR to become 3/4 of its previous value. However, if there is great
variance from segment to segment, then this absolute value will be positive, potentially increasing
RTTVAR. As before, $\\beta$ acts as a weighting factor to determine how much influence the new
variation should have compared with the historical values.

.. topic:: Example

   .. figure:: Images/CSF-Images-Example.png
      :align: left
      :width: 100%
      :alt: Decorative example icon

   When calculating the new RTTVAR, the SRTT value used should be the old SRTT, not the updated value
   from :num:`Example #RTTExample`. The initial RTTVAR is set to $R/2$ when there are no previous
   measurements to use. Since the example above used $R = 4$ as the initially observed RTT, RTTVAR is
   initialized to 2. The table below illustrates the calculations for RTTVAR for the sequence of
   observations $R'$ from before, using the standard value of $\\beta = 1/4$.

   .. raw:: html
   
      <center>
      <table class="table table-bordered">
        <thead class="jmu-dark-purple-bg text-light">
          <tr>
            <th class="p-0 center">SRTT</th>
            <th class="p-0 center"><span class="math inline">$R'$</span></th>
            <th class="p-0 center">Old RTTVAR</th>
            <th class="p-0 center">Updated RTTVAR</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="py-0 center">4</td>
            <td class="py-0 center">10</td>
            <td class="py-0 center">2</td>
            <td class="py-0">(3/4) * 2 + (1/4) * | 4 – 10 | = 3</td>
          </tr>
          <tr>
            <td class="py-0 center">5</td>
            <td class="py-0 center">20</td>
            <td class="py-0 center">3</td>
            <td class="py-0">(3/4) * 3 + (1/4) * | 5 – 20 | = 6</td>
          </tr>
          <tr>
            <td class="py-0 center">7</td>
            <td class="py-0 center">2</td>
            <td class="py-0 center">6</td>
            <td class="py-0">(3/4) * 6 + (1/4) * | 7 – 2 | = 5.75&nbsp;&nbsp;&nbsp;<i>[rounded to 6]</i></td>
          </tr>
        </tbody>
      </table>
      <p>
      Table 5.7: SRTT calculations for a sequence of three received segments
      </p>
      </center>

Based on these pieces, we can now define the RTO calculation. This formula uses a constant value $K
= 4$, while G denotes the clock's minimum granularity. That is, if the clock can only measure time
to the accuracy of ms, it would not make sense to adjust the RTO by an unmeasurable fraction of a
ms. The new RTO is calculated by adding the SRTT and the maximum of $G$ and $K * RTTVAR$. 

.. raw:: html

   <center>
   <span class="math inline">$\large RTO := SRTT + max (G, K * RTTVAR)$</span>
   </center>
   <br />

The RTO will always be the rolling average of the RTT plus a small amount of extra time for leeway.
As the variance of RTT increases, the quantity added to RTT would increase. However, if the system
experiences perfectly consistent RTT values, the $max(G, K * RTTVAR)$ would eventually shrink to the
minimum clock granularity, providing the bare minimum of flexibility in deviation from the SRTT. 

.. topic:: Example

   .. figure:: Images/CSF-Images-Example.png
      :align: left
      :width: 100%
      :alt: Decorative example icon

   The table below shows the RTO calculations, assuming all values are in terms of ms and the clock
   granularity has a minimum value of 1 ms. To summarize the results in this table, the first
   measurement of $R = 4$ created the initial SRTT of 4 and RTTVAR of 2. By combining these values in
   the RTO calculation, the system would wait for up to 12 ms before declaring a packet loss. The next
   message arrived with an observed RTT of 10 ms, so this segment arrived before the timeout clock
   expires. This segment increased the RTO value to 17 ms, but the next segment missed the timeout by
   arriving with an RTT of 20 ms. After the 17 ms had elapsed, the host would have considered this a
   packet loss and re-sent the previous segment. After the retransmission, the observed RTT dropped to
   2; this drop increased the variation RTTVAR to 6, which has the effect of raising the RTO to 31.

   .. raw:: html
   
      <center>
      <table class="table table-bordered">
        <thead class="jmu-dark-purple-bg text-light">
          <tr>
            <th class="p-0 center">SRTT</th>
            <th class="p-0 center">RTTVAR</th>
            <th class="p-0 center">Updated RTO</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="py-0 center">4</td>
            <td class="py-0 center">2</td>
            <td class="py-0">4 + max(1, 4 * 2) = 4 + 8 = 12&nbsp;&nbsp;&nbsp;<i>[initial value]</i></td>
          </tr>
          <tr>
            <td class="py-0 center">5</td>
            <td class="py-0 center">3</td>
            <td class="py-0">5 + max(1, 4 * 3) = 5 + 12 = 17</td>
          </tr>
          <tr>
            <td class="py-0 center">7</td>
            <td class="py-0 center">6</td>
            <td class="py-0">7 + max(1, 4 * 6) = 7 + 24 = 31</td>
          </tr>
          <tr>
            <td class="py-0 center">6</td>
            <td class="py-0 center">6</td>
            <td class="py-0">6 + max(1, 4 * 6) = 6 + 24 = 30</td>
          </tr>
        </tbody>
      </table>
      <p>
      Table 5.8: Combining the SRTT and RTTVAR values for the new RTO
      </p>
      </center>
   
It may seem odd to increase the RTO when the observed RTT actually dropped, but the drop creates a
higher level of variance. Consequently, TCP interprets these measurements as an indication that the
network is behaving unpredictably and grants the system more time before giving up on an expected
segment. That is, since the expected and actual RTT values differ significantly, TCP has less
confidence that replies will be received within a short amount of time. If the variance later
decreases, TCP would shrink the RTO value, under the premise that the predictable behavior is likely
to continue.

.. topic:: Note

   .. figure:: Images/CSF-Images-Note.png
      :align: left
      :width: 100%
      :alt: Decorative note icon

   Our description of TCP here has focused on the basic principle of reliable data transport,
   specifically focusing on the role of the TCP handshake and detecting packet loss. TCP is a
   significantly more complex protocol than we have presented, as it uses additional techniques to
   further increase the reliability of communication over the Internet. For instance, rather than
   acknowledging every message, TCP hosts can use *cumulative acknowledgement*, in which
   multiple messages can be acknowledged all at once. In addition, TCP *congestion control*
   allows a host to detect delays in the network, throttling their sending rate to reduce traffic and
   prevent future delays. For a more complete discussion of these topics, we refer interested readers
   to textbooks that focus exclusively on the topic of networking, such as *Computer Networking: A
   Top-Down Approach (7th Edition)* [Kurose2016]_ by Kurose and Ross.

In summary, TCP provides two key transport services that UDP does not: reliable data transport and
flow control. These services are implemented by combining multiple pieces of information in the TCP
segment header with observations of the network behavior. Hosts implement TCP reliability by
tracking the sequence and acknowledgement numbers of each message, along with maintaining a rolling
average of the expected wait times for segments. If a segment is not acknowledged within an expected
time frame, hosts can implicitly request re-transmission by re-sending previous messages. Throughout
this process, TCP hosts use the receive window as a means of flow control; by informing each other
of the current capacity of their internal data buffers, the hosts are taking proactive steps to
support reliability by preventing buffer overflows at the other end. These features make TCP the
preferred choice for applications that depend on the successful transmission of all data, rather
than compensating for lost data with reduced quality of service.

.. topic:: Note

   .. figure:: Images/CSF-Images-Note.png
      :align: left
      :width: 100%
      :alt: Decorative note icon

   Unlike the application layer, there is no universal, straightforward mechanism to access the
   transport, network, or link layer protocol headers. Accessing these layers requires the use of a
   raw socket, created by passing ``SOCK_RAW`` as the type parameter when calling ``socket()``. The
   difficulty here is that many systems require root privileges to create raw sockets. Consequently,
   normal user-mode programs cannot easily gain access to these headers.

   From a learning standpoint, there is also little value to demonstrating code for accessing these
   headers. The technique would be the same as we used in the section describing DNS. Specifically,
   the primary difference between that example and one that illustrates what UDP adds would involve
   increasing the size of the message buffer to contain the eight bytes of the UDP header at the
   beginning.

   Readers interested in the intricacies of these lower layers should consider using a packet
   analyzer, such as ``tcpdump`` (``http://www.tcpdump.org/``) or Wireshark
   (``https://www.wireshark.org/``). These programs are freely available as open-source software. In
   addition, the ``tcpdump`` site provides a reusable library, ``libpcap``, for building additional
   tools. Readers should exercise caution, however, as using these tools—particularly in a way that
   capture's other people's data—may violate the terms of use for access to the local network or may
   even be illegal based on one's locality.
   
.. avembed:: Exercises/Internet/InterTransSumm.html ka
   :module: TransLayer
   :points: 1.0
   :required: True
   :exer_opts: JXOP-debug=true&amp;JOP-lang=en&amp;JXOP-code=java
   :long_name: Transport layer questions
   :threshold: 5

