SEN2008 · Week 7 · Transport Layer Concepts (I): Services, elements of a transport protocol, TCP, UDP

Transport layer I: ports, UDP, the TCP segment and the handshake

The job of the transport layer, multiplexing with ports, UDP checksum calculation, the TCP header, seq/ack numbers and the three-way handshake. The seq/ack and checksum questions on the midterm come from here.

transport layermultiplexingdemultiplexingport numbersocketUDPchecksumone's complementTCP segmentsequence numberacknowledgment numbercumulative ACKthree-way handshakeSYNFINreliable data transfer

The network layer carries a packet from one host to another. But on that host the browser, Spotify and SSH are all running at the same time. It is the transport layer that decides which program the packet goes to. This week you will see two protocols: UDP, which is fast but makes no promises, and TCP, which is reliable but heavier. The most frequent exam questions are checksum calculation and seq/ack numbers. Golden rules:

  1. The port selects the process. The IP address finds the host, the port number finds the socket on that host.
  2. A UDP socket is identified by 2 values, a TCP socket by 4 values.
  3. Checksum: add, wrap the carry around, flip. The sum at the receiver must come out as 0xFFFF.
  4. ack = the next expected byte. That is seq + length.
  5. SYN and FIN each take one number. Even though they carry no data.

What does the transport layer do?

The application layer produces the message. The transport layer splits it into segments, adds a header and hands it to the network layer. On the other side, it looks at the header and delivers the data to the right socket. IP is a “best effort” service: packets can be lost, corrupted or reordered. The transport layer chooses how much assurance to put on top of this.

FeatureUDPTCP
ConnectionNone (connectionless)Set up with a handshake
Reliable deliveryNoYes (ACK + retransmission)
In-order deliveryNoYes
Flow controlNoneYes, with rwnd
Congestion controlNoneYes, with cwnd
Header8 bytesat least 20 bytes
Typical useDNS, video/audio streaming, gamesWeb, email, file transfer

Neither gives a guarantee of delay or bandwidth. This is a classic “which is not true” option on the exam.

Multiplexing and demultiplexing

Collecting data from many sockets on the sending side and writing the port into the header is called multiplexing. Looking at the port in the header on the receiving side and distributing the data to the right socket is called demultiplexing.

The port number is 16 bits, so there are 216=655362^{16} = 65536 values. Ports 0-1023 are the well-known ports. The client side usually uses an ephemeral port given by the operating system.

  • UDP: the socket is found by (destination IP, destination port). Datagrams from different clients arriving at the same port land in the same socket. The source information is used only as the reply address.
  • TCP: the socket is found by (source IP, source port, destination IP, destination port). A web server listens on 80 but opens a separate socket for each client connection.

PITFALL · “same destination port = same socket” is true only for UDP. In TCP, even if two different clients connect to the same server port, there are two separate sockets because the source IP or source port differs.

UDP and the checksum

The UDP header has four fields, each 16 bits: source port, destination port, length (in bytes, including the header), checksum. In total 8 bytes. A datagram carrying 120 bytes of data has a length field of 128. The largest UDP data over IPv4 is 65535 − 20 − 8 = 65507 bytes.

The checksum is meant to catch bit corruption in transit. The UDP checksum covers the header, the data, and a pseudo-header containing the IP addresses. The calculation method:

  1. Split the data into 16-bit words and add them.
  2. If the sum exceeds 16 bits, the overflowing carry is added to the lowest bit (wraparound).
  3. Take the one’s complement of the result: flip every bit.

Worked example: words 0xB4A2, 0x6C3F, 0x1E90.

0xB4A2                         1011 0100 1010 0010
+ 0x6C3F  = 0x120E1  (17 bits, there is a carry)
            0x20E1 + 1 = 0x20E2   0010 0000 1110 0010
+ 0x1E90  = 0x3F72   (no carry)   0011 1111 0111 0010
sum       = 0x3F72
checksum  = flip               1100 0000 1000 1101  = 0xC08D

The receiver adds all the words together with the checksum: 0x3F72 + 0xC08D = 0xFFFF. If all bits are 1, no error was seen. If there is even a single 0, the packet is corrupted.

TIP · flipping in hex. One’s complement = 0xFFFF − x. For 0x3F72: F−3=C, F−F=0, F−7=8, F−2=D → 0xC08D. You don’t need to write it bit by bit.

PITFALL · the checksum doesn’t catch every error. If in two words, at the same bit position, one flips 0→1 and the other 1→0, the sum doesn’t change and the error is invisible. The checksum detects, it does not correct.

The idea of reliable data transfer (rdt)

TCP’s reliability comes from a model built up step by step. You can summarize the rdt versions in the textbook like this:

VersionChannel assumptionMechanism added
rdt1.0Perfect channelNothing, just send
rdt2.0Bits may be corruptedChecksum + ACK/NAK + retransmission
rdt2.1ACK/NAK may also be corruptedDuplicate detection with a 1-bit sequence number (0/1)
rdt2.2Same, without NAKACK carrying the number of the last correct packet (duplicate ACK = NAK)
rdt3.0Loss is possible tooTimer, resend when it expires

rdt3.0 is a stop-and-wait protocol. Because its efficiency is low, TCP uses pipelining: it sends several segments without waiting for an ACK. You saw the details of Go-Back-N and Selective Repeat in week 3.

The TCP segment

 0                   16                  31
+---------------------+--------------------+
|   source port       |   destination port |
+---------------------+--------------------+
|              sequence number             |
+------------------------------------------+
|          acknowledgment number           |
+------+------+-------+--------------------+
|offset| rsv  | flags |  receive window    |
+------+------+-------+--------------------+
|     checksum        |  urgent pointer    |
+---------------------+--------------------+
|        options (variable)                |
+------------------------------------------+
|        data                              |
  • Sequence number: the number of the first byte in the segment. TCP numbers bytes, not segments.
  • Acknowledgment number: the next byte the receiver expects. It is cumulative: it means “everything up to this has arrived”.
  • Data offset (header length): the header size, in 4-byte words. If 5 it is 20 bytes, if 8 it is 32 bytes (12 bytes of options).
  • Flags: SYN (open the connection), ACK (the ack field is valid), FIN (close), RST (reset), PSH, URG.
  • Receive window: the receiver’s free buffer space, for flow control (week 8).

Worked example: a client with ISN 7200 sends three segments of 1000, 1000 and 600 bytes.

segment   seq     length    ACK for this segment
1         7201    1000      8201
2         8201    1000      9201
3         9201     600      9801

Three-way handshake and teardown

Before sending data, TCP has the two sides share their initial sequence numbers (ISN). Let the client ISN = 4000 and the server ISN = 9000:

1) client → server   SYN=1          seq=4000
2) server → client   SYN=1, ACK=1   seq=9000   ack=4001
3) client → server   ACK=1          seq=4001   ack=9001   (may carry data)
4) client 500 bytes  seq=4001  → server's reply ack=4501

Why three steps and not two? With two steps the server can’t know that the client received its own ISN. A connection could be opened pointlessly because of an old delayed SYN.

At teardown each direction is closed separately. The client sends a FIN (seq 5501), and the server acknowledges it with ack 5502. When the server finishes its work, it sends its own FIN (seq 12001), the client returns ack 12002 and waits for a while in the TIME_WAIT state. This way, if the last ACK is lost, it can resend it.

PITFALL · SYN flood. If the server allocates resources for every SYN and holds half-open connections, an attacker can fill the table with SYNs with forged source addresses. That is why SYN cookies exist. You will see it again in the DoS topic in week 10.

Cumulative ACK and loss

Four segments of 50 bytes each, numbered 100, 150, 200, 250, were sent, and 150 was lost:

arrives   receiver's ACK
100       150
150       (lost)
200       150   (duplicate ACK)
250       150   (duplicate ACK)

The receiver can buffer out-of-order segments, but the ACK number is always the first missing byte. When 150 arrives again, the ACK jumps straight to 300.

Common mistakes

Wrong ideaCorrect
ack = the number of the last byte receivedack = the next expected byte (seq + length)
A SYN doesn’t consume a number because it carries no dataSYN and FIN consume 1 number
TCP numbers segmentsTCP numbers bytes
UDP also separates sockets by the 4-tupleUDP: (destination IP, destination port)
The checksum corrects errorsIt only detects, and not always
I drop the overflowing carryThe carry is added to the lowest bit (wraparound)
Data offset = length in bytesThe number of 4-byte words: × 4
TCP guarantees delayNeither TCP nor UDP guarantees timing or rate

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Which transport-layer protocol provides a reliable, in-order byte stream with connection setup?

  1. UDP
  2. IP
  3. TCP
  4. ICMP
Show answer
C) TCP

UDP is connectionless and gives no reliability. IP and ICMP are network-layer protocols. The protocol that sets up a connection (handshake), compensates for loss and delivers bytes in order is TCP.

Question 2easy

Port numbers are 16-bit fields. How many distinct port numbers exist?

Show answer
65536

16 bits can represent 216=655362^{16} = 65536 different values: 0 to 65535. Ports 0-1023 are well-known ports: HTTP 80, HTTPS 443, DNS 53, SSH 22.

Question 3medium

A host receives UDP datagrams from two different clients, both sent to destination port 5000 on the host. How does the host demultiplex them, and how would this differ for TCP?

  1. UDP uses the 4-tuple, TCP uses only the destination port
  2. Both go to the same UDP socket (identified by destination IP and port); TCP would use separate sockets identified by the 4-tuple
  3. UDP creates one socket per client; TCP uses one socket for all clients
  4. Both protocols use only the source port
Show answer
B) Both go to the same UDP socket (identified by destination IP and port); TCP would use separate sockets identified by the 4-tuple

A UDP socket is identified by two values: (destination IP, destination port). Even if their sources differ, the two datagrams land in the same socket. A TCP socket is identified by four values: (source IP, source port, destination IP, destination port). Each client connection becomes a separate socket.

Question 4medium

Compute the 16-bit Internet checksum (one's complement of the one's complement sum) of the two words 0x8F21 and 0x9A4C.

Show answer
0xD691

Add: 0x8F21 + 0x9A4C = 0x1296D. The carry (1) beyond 16 bits is added at the bottom: 0x296D + 1 = 0x296E.
One's complement: every bit is flipped. 0x296E = 0010 1001 0110 1110 → 1101 0110 1001 0001 = 0xD691.
Quick check: 0x296E + 0xD691 = 0xFFFF.

Question 5medium

A receiver gets the data words 0x3A10, 0x7C05 and the checksum field 0x49AE. Does it detect an error?

Show answer
Yes, the sum is 0xFFC3, not 0xFFFF

The receiver adds the data words and the checksum together: 0x3A10 + 0x7C05 = 0xB615, 0xB615 + 0x49AE = 0xFFC3 (no carry).
Without errors the result would be 0xFFFF (all ones). Since it is 0xFFC3, there is an error. The correct checksum should have been 0x49EA.

Question 6easy

A TCP segment carries sequence number 2300 and 700 bytes of data. What acknowledgment number does the receiver send if everything before it has arrived?

Show answer
3000

The ACK number is the number of the next expected byte: 2300 + 700 = 3000. The segment carries bytes 2300 through 2999.

Question 7medium

In a three-way handshake, the client's ISN is 4000 and the server's ISN is 9000. Give the acknowledgment number in the SYNACK, and the sequence and acknowledgment numbers of the client's final ACK.

Show answer
SYNACK ack = 4001; final ACK seq = 4001, ack = 9001

A SYN carries no data but consumes 1 sequence number. In the SYNACK the server sends seq = 9000, ack = 4000 + 1 = 4001.
The client's third segment: seq = 4001 (its own ISN + 1), ack = 9000 + 1 = 9001.

Question 8medium

After a handshake with client ISN 7200, the client sends three segments of 1000, 1000 and 600 bytes. What is the sequence number of the third segment, and the ACK number that acknowledges all three?

Show answer
9201; ACK 9801

The first data byte is 7201 (the SYN consumed 7200).
Segment 1: seq 7201, 1000 bytes → next 8201.
Segment 2: seq 8201 → next 9201.
Segment 3: seq 9201, 600 bytes → the ACK that acknowledges all of them is 9801.

Question 9medium

A TCP header has its data offset (header length) field set to 8. How long is the header, and how many bytes of options does it carry?

Show answer
32 bytes; 12 bytes of options

The data offset is the number of 32-bit (4-byte) words: 8 × 4 = 32 bytes. A TCP header without options is 20 bytes, so the remaining 32 − 20 = 12 bytes are options.

Question 10hard

A sender transmits four segments with sequence numbers 100, 150, 200, 250 (50 bytes each). The segment with seq 150 is lost; the others arrive. Using cumulative ACKs, which ACK numbers does the receiver send for the three arriving segments, in order?

Show answer
150, 150, 150

Segment 100 arrives: 100-149 are complete, ACK 150.
Segment 200 arrives but 150 is missing. The cumulative ACK says "I'm waiting for 150": ACK 150 (first duplicate).
Segment 250 arrives, 150 is still missing: ACK 150 (second duplicate). The sender sees the same ACK again and again; three duplicate ACKs trigger fast retransmit (week 8).

Question 11hard

Why does a protocol like rdt2.1 need sequence numbers even though the channel only corrupts (never loses) packets?

  1. To order packets that arrive out of order
  2. Because a corrupted ACK/NAK forces the sender to retransmit, and the receiver must recognize the duplicate
  3. To compute the checksum
  4. To identify the destination port
Show answer
B) Because a corrupted ACK/NAK forces the sender to retransmit, and the receiver must recognize the duplicate

If an ACK or NAK is corrupted, the sender can't know what happened and resends the packet. If the receiver had already received this packet, it would take it for new data and deliver it twice. A 1-bit sequence number (0/1) makes it possible to tell the duplicate apart.

Question 12medium

During teardown the client sends FIN with sequence number 5501. What acknowledgment number does the server return for it?

Show answer
5502

A FIN, like a SYN, consumes 1 sequence number without carrying data. Its acknowledgment is 5501 + 1 = 5502.

Verification: Checksums (16-bit one's complement sum with wraparound), seq/ack arithmetic and header sizes were computed with node (verify/SEN2008/07.mjs).

Review card

Transport layer: process-to-process. IP: host-to-host. Port 16 bit: 0-65535; 0-1023 well-known (HTTP 80, HTTPS 443, DNS 53, SSH 22). UDP socket = (dst IP, dst port). TCP socket = 4-tuple (src IP, port, dst IP, port). UDP header 8 bytes: src port, dst port, length, checksum. Connectionless, unordered, no reliability. Checksum: add the 16-bit words, the overflowing carry is added at the bottom, then flip the bits. Receiver: if data + checksum does not sum to 0xFFFF, error. The checksum doesn't catch every error. TCP header min 20 bytes; data offset × 4 = header length. Flags: SYN ACK FIN RST PSH URG. seq = number of the first byte in the segment. ack = NEXT expected byte (cumulative). ack = seq + data length. SYN and FIN consume 1 number, a pure ACK consumes none. Handshake: SYN(seq=x) → SYNACK(seq=y, ack=x+1) → ACK(seq=x+1, ack=y+1). Teardown: FIN → ACK → FIN → ACK; whoever sends the FIN first waits in TIME_WAIT. rdt: checksum (corruption) + ACK/NAK + seq no (duplicates) + timer (loss) = rdt3.0.