6.0 Transport Layer
Open book notes for the Transport Layer section, focusing on potentially examinable material and strictly excluding Sockets, Flow Control, and Congestion Control, based on the provided sources and our conversation history.
1. Role of the Transport Layer
- The Transport Layer is responsible for providing host-to-host (or process-to-process) communication services for applications.
- It sits above the Network Layer.
- It manages delivery of a message from one process to another, whereas the Network layer oversees source to destination delivery.
- The Transport Layer is the lowest OSI layer that has to deal with end-to-end transmission. Layers above it are also concerned with end-to-end transmission.
Layer Responsibility Comparison
Layer | Primary Focus | Addressing Unit | Key Responsibility |
---|
Network Layer | Host-to-Host | IP Address | Routing packets between hosts |
Transport Layer | Process-to-Process | Port Number | End-to-end delivery between applications |
Application Layer | Service-to-Service | Application Protocol | Specific application services |
2. Ports
- When delivering packets, Network Layer IP addresses alone are not enough as they only address hosts, and not individual operating system processes on those hosts.
- Transport protocols identify software endpoints (applications) on a host using a 16-bit positive port number.
- An endpoint is a combination of an IP address and a port number.
- Port numbers are needed because multiple applications may be running on a host and communicating over the network simultaneously. The transport layer uses port numbers to direct incoming packets to the correct application process.
- There are predefined port numbers for common applications (e.g., FTP, SSH, Telnet, SMTP, HTTP). Many of these common application protocols use TCP.
Port Number Ranges
Range | Type | Usage | Examples |
---|
0-1023 | Well-known Ports | System/Reserved | HTTP(80), HTTPS(443), FTP(21), SSH(22) |
1024-49151 | Registered Ports | User applications | Custom services |
49152-65535 | Dynamic/Private | Temporary/Client | Ephemeral connections |
Endpoint Address Structure
Complete Endpoint = IP Address + Port Number
Example: 192.168.1.100:8080
├─────────────┴─ IP Address (Host identification)
└─────────────── Port Number (Process identification)
3. Transport Protocol Comparison Overview
Before diving into specific protocols, here's a fundamental comparison:
Aspect | UDP | TCP |
---|
Connection Type | Connectionless | Connection-oriented |
Reliability | Unreliable (Best-effort) | Reliable |
Ordering | No ordering guarantee | Ordered delivery |
Header Size | 8 bytes | 20+ bytes |
Speed | Faster | Slower (due to overhead) |
Use Cases | Streaming, DNS, Gaming | Web browsing, Email, File transfer |
Error Detection | Basic checksum | Comprehensive error handling |
Flow Control | None | Yes |
Congestion Control | None | Yes |
4. User Datagram Protocol (UDP)
UDP Properties & Characteristics
- Properties:
- Unreliable, unordered datagram service.
- Connectionless.
- Best-effort delivery. Packets may be lost, delivered out of order, duplicated, or delayed.
- UDP packets are free packets once they are out, meaning they don't necessarily follow the same route and have no information about other packets in the stream.
- Advantages:
- Lightweight, low overhead.
- Speed: Provides faster delivery compared to protocols with more overhead.
- Finer control over what data is sent and when.
- No delay for connection establishment.
- No connection state: Does not allocate buffers, parameters, or sequence numbers for the connection itself.
- Use Cases/Applications: Applications where some packet loss or reordering is acceptable, or handled by the application layer.
- Streaming media (video calls, video/music streaming).
- DNS (Domain Name Service).
- Network Time Protocol (NTP).
UDP Header (8 bytes total)
┌──────────────────┬──────────────────┐
│ Source Port │ Destination Port │ (16 bits each)
├──────────────────┼──────────────────┤
│ Length │ Checksum │ (16 bits each)
└──────────────────┴──────────────────┘
│ Data │
└─────────────────────────────────────┘
- UDP Header:
- Small packet header, typically 8 bytes long.
- Contains fields for Source Port, Destination Port, Length, and Checksum.
- The IP address information is handled at the Network (IP) layer, not in the UDP header.
UDP Checksum Mechanism
- Checksum:
- Aids in error detection within the UDP packet.
- It is optional in IPv4 but mandatory in IPv6.
- Calculated by adding 16-bit words of the data (including a pseudo-header derived from the IP header) using one's complement arithmetic, carrying bits over to the least significant bit, and then flipping the bits of the final sum.
- It can detect any one-bit errors but not all two-bit errors. Two-bit errors where the bit flips cancel each other out during addition cannot be detected.
- It checks for errors in the packet header and data.
Checksum Calculation Example
Example Data: 0x4500 + 0x003C + 0x1C46 = 0x7C82
One's Complement: ~0x7C82 = 0x837D
Final Checksum: 0x837D
Error Detection Capability:
✓ Single bit errors: Always detected
✗ Some double bit errors: May not be detected if they cancel out
UDP Limitations
- Problems with UDP:
- Due to potentially taking different routes, packets can have varying round-trip times and there is no guarantee of packet ordering.
- These problems mean the application layer is often responsible for handling packet ordering and recovery if needed.
UDP Communication Flow
Client Server
│ │
│──── UDP Packet 1 ──────→│ (No connection setup)
│──── UDP Packet 2 ──────→│ (Independent packets)
│──── UDP Packet 3 ──────→│ (May arrive out of order)
│ │
│←──── Response ─────────│ (Optional, application-dependent)
5. Transmission Control Protocol (TCP)
TCP Properties & Guarantees
- Role/Properties/Guarantees:
- Provides a reliable end-to-end service.
- Connection-oriented: A logical connection is established and maintained between sender and receiver before data transfer begins and until terminated.
- Full-duplex: Data can be transmitted in both directions simultaneously.
- Point-to-point communication: Only two parties are connected at a time.
- Stream interface: Presents a reliable frame stream to the network layer.
- Graceful connection shutdown.
- Guarantees: Guaranteed delivery, in-order delivery, unique delivery (no duplicates).
- Advantages: Reliability, suitability for applications requiring accurate data transfer like file transfer and email.
- Applications: Most widely used protocol, likely implemented by almost any application using the internet. Examples include FTP, SSH, Telnet, SMTP, HTTP/HTTPS, file transfer, email.
TCP Header (20+ bytes)
┌────────────────┬────────────────┬─────────────────┬─────────────────┐
│ Source Port │ Destination Port│ Sequence Number (32 bits) │
├────────────────┼────────────────┼─────────────────┼─────────────────┤
│ Acknowledgement Number (32 bits) │Data│Res│ Flags │
├─────────────────────────────────────────────────┤Off │ │U A P R S F│
│ Window Size │ Checksum │set │ │R C S S Y I│
├────────────────────────────────┼────────────────┤ │ │G K H T N N│
│ Urgent Pointer │ Options... │ │ │ │
└────────────────────────────────┴────────────────┴────┴───┴─────────┘
- TCP Header:
- Requires more information than UDP.
- Typically 20 bytes long, plus optional fields and padding.
- Contains fields for Source Port, Destination Port, Sequence Number, Acknowledgement Number, Data offset, Reserved, Flags (URG, ACK, PSH, RST, SYN, FIN), Window, Checksum, Urgent Pointer, Options, Padding.
TCP Flags and Their Functions
Flag | Name | Purpose | Usage |
---|
SYN | Synchronize | Establish connection | Connection initiation |
ACK | Acknowledge | Confirm receipt | Data acknowledgment |
FIN | Finish | Terminate connection | Connection closure |
RST | Reset | Abort connection | Error/rejection |
PSH | Push | Immediate delivery | Priority data |
URG | Urgent | Urgent data pointer | Emergency data |
TCP Segmentation
- Segments:
- TCP divides application data into segments for transmission. This is necessary as application messages may be larger than the Maximum Segment Size (MSS).
- A segment is sent when it is full (MSS reached), times out, or is pushed by the application.
- MSS is typically up to about 1460 bytes.
Segmentation Example
Application Data (5000 bytes)
┌─────────────────────────────────────────────────────────┐
│ Large Message │
└─────────────────────────────────────────────────────────┘
↓ TCP Segmentation
┌────────────┬────────────┬────────────┬────────────┐
│ Segment 1 │ Segment 2 │ Segment 3 │ Segment 4 │
│ (1460 bytes│ (1460 bytes│ (1460 bytes│ (620 bytes)│
└────────────┴────────────┴────────────┴────────────┘
TCP Sequence and Acknowledgment Numbers
- Sequence Numbers:
- Used for ordering segments to allow for reassembly at the receiver in the correct order.
- Necessary because TCP segments may arrive out of order when traveling through the network.
- Acknowledgement Numbers:
- Used to confirm received bytes or sequence numbers to the sender.
- The receiver sends an acknowledgement number indicating the sequence number of the next byte it expects to receive.
Sequence Number Example
Sender Receiver
│ │
│─── Seq=1000, Len=100 ───────→│ (Bytes 1000-1099)
│ │
│←────── ACK=1100 ─────────────│ (Expecting byte 1100)
│ │
│─── Seq=1100, Len=200 ───────→│ (Bytes 1100-1299)
│ │
│←────── ACK=1300 ─────────────│ (Expecting byte 1300)
TCP Connection Management
3-way Handshake
- 3-way Handshake:
- Process used to establish a TCP connection.
- Involves three packets: Sender sends a SYN segment (SYN flag set), Receiver replies with a SYN-ACK segment (SYN and ACK flags set), Sender acknowledges with an ACK segment (ACK flag set).
- Purpose: To synchronize sequence numbers and ensure both parties are aware that the connection is established and two-way communication is possible.
- If the initial SYN or the SYN-ACK is lost, the sender will timeout and can retry the connection.
Client Server
│ │
│────── SYN ─────────────→│ Step 1: Connection request
│ │
│←───── SYN-ACK ─────────│ Step 2: Acknowledgment + request
│ │
│────── ACK ─────────────→│ Step 3: Final acknowledgment
│ │
│ CONNECTED │ Connection established
- Reset (RST) flag: Can be sent if the receiver is not working, not expecting a connection, or too busy to accept the SYN packet.
- FIN flag: Used to terminate a TCP connection. Indicates the last packet from the sender.
Connection Termination
- Graceful Connection Shutdown:
- TCP guarantees a graceful connection shutdown.
- This is implemented using a sequence of FIN and ACK flags, allowing both sides to finish sending data before the connection is fully closed. A typical sequence involves one side sending FIN, the other acknowledging with FIN-ACK, and then the first side sending ACK. There are variations.
- This system could fail under conditions such as connection disruption (e.g., due to interference or cable being pulled out), which would cause a timeout.
Client Server
│ │
│────── FIN ─────────────→│ Step 1: Client wants to close
│ │
│←───── ACK ─────────────│ Step 2: Server acknowledges
│ │
│←───── FIN ─────────────│ Step 3: Server wants to close
│ │
│────── ACK ─────────────→│ Step 4: Client acknowledges
│ │
│ CLOSED │ Connection terminated
TCP Reliability Mechanisms
- Retransmission:
- Retransmission is regularly used in TCP.
- It occurs if a segment is lost or arrives out of order.
- The sender typically sets a timer after sending a segment. If an acknowledgement is not received within the timeout period, the segment is retransmitted.
- The Stop-and-Wait ARQ protocol is a simple example where the sender sends one packet and waits for an ACK before sending the next.
Retransmission Example
Sender Receiver
│ │
│─── Segment 1 ──────────→│ ✓ Received
│ │
│←────── ACK ─────────────│
│ │
│─── Segment 2 ──────────→│ ✗ Lost in network
│ │
│ TIMEOUT │
│ │
│─── Segment 2 ──────────→│ ✓ Retransmitted & received
│ │
│←────── ACK ─────────────│
6. Practical Application Scenarios
When to Use UDP vs TCP
Scenario | Protocol Choice | Justification |
---|
Video Streaming | UDP | Speed over reliability; lost frames acceptable |
Web Browsing | TCP | Complete data transfer required |
Online Gaming | UDP | Low latency critical; some packet loss tolerable |
Email | TCP | Message integrity essential |
DNS Queries | UDP | Simple request-response; retry if needed |
File Transfer | TCP | Data integrity and completeness required |
Performance Comparison
TCP UDP
Reliability: ████████████ ██
Speed: ███████ ████████████
Overhead: ████████████ ██
Complexity: ████████████ ███
7. Key Calculations and Examples
Checksum Calculation (Concept Example)
For UDP/TCP checksum verification:
Given data words: 0x4500, 0x003C, 0x1C46, 0x4000, 0x4011
Step 1: Sum all 16-bit words
0x4500 + 0x003C + 0x1C46 + 0x4000 + 0x4011 = 0x01B593
Step 2: Add carry bits
0x01B5 + 0x93 = 0x0248
Step 3: One's complement
~0x0248 = 0xFDB7
Checksum = 0xFDB7
Sequence Number Tracking
Initial Sequence Number (ISN): 1000
Data sent: "Hello World" (11 bytes)
Segment 1: Seq=1000, Data="Hello", Len=5
Segment 2: Seq=1005, Data=" World", Len=6
Expected ACK after Segment 1: 1005
Expected ACK after Segment 2: 1011
This comprehensive visualization and organization helps understand the relationships between transport layer concepts while maintaining all original knowledge points with enhanced clarity and practical examples.