# UDP (User Datagram Protocol)

UDP (User Datagram Protocol) is a **connectionless, unreliable** transport layer protocol in the TCP/IP protocol suite. It provides a simple mechanism for applications to send messages (called datagrams) to other hosts without establishing a connection or guaranteeing delivery.

**Key Characteristics:**

* **Connectionless**: No handshake before sending data
    
* **Unreliable**: No guarantee of delivery, ordering, or duplicate protection
    
* **Lightweight**: Minimal protocol overhead
    
* **Fast**: No connection setup/teardown delays
    

## UDP vs TCP Comparison

| **Feature** | **UDP** | **TCP** |
| --- | --- | --- |
| **Connection** | Connectionless | Connection-oriented |
| **Reliability** | Unreliable | Reliable |
| **Delivery Guarantee** | No | Yes |
| **Ordering** | No guarantee | Guaranteed |
| **Duplicate Detection** | No | Yes |
| **Flow Control** | No | Yes |
| **Congestion Control** | No | Yes |
| **Header Size** | 8 bytes | 20+ bytes |
| **Speed** | Faster | Slower |
| **Use Cases** | Real-time apps, DNS, DHCP | Web browsing, email, file transfer |

## UDP Packet Structure

The UDP header is simple and consists of only 4 fields:

```plaintext
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|            Length             |           Checksum            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                            Data                               |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
```

### Field Details

1. **Source Port** (16 bits / 2 bytes)
    
    * Range: 0-65535
        
    * Identifies the sending application/process
        
    * Optional (can be 0 if no reply expected)
        
2. **Destination Port** (16 bits / 2 bytes)
    
    * Range: 0-65535
        
    * Identifies the receiving application/process
        
    * Mandatory for proper delivery
        
3. **Length** (16 bits / 2 bytes)
    
    * Total length of UDP header + data
        
    * Minimum value: 8 (header only)
        
    * Maximum value: 65535 bytes
        
4. **Checksum** (16 bits / 2 bytes)
    
    * Error detection mechanism
        
    * Optional in IPv4 (can be 0)
        
    * Mandatory in IPv6
        

## UDP Packet Sizes

**MTU (Maximum Transmission Unit)**: The largest size packet or frame that can be sent in a single network layer transaction. It represents the maximum payload size that the underlying network can handle without fragmentation.

### Without MTU Restriction

* **Maximum theoretical UDP packet**: 65,535 bytes total
    
    * This is limited by the 16-bit Length field in UDP header
        
    * **Maximum UDP data**: 65,535 - 8 = 65,527 bytes of actual data
        
    * However, this would require IP fragmentation across multiple network frames
        

### With MTU Restriction (Practical Limits)

* **Ethernet MTU**: 1,500 bytes (most common)
    
* **Maximum UDP packet without fragmentation**: 1,500 bytes total
    
* **Calculation**: MTU - IP header - UDP header = usable data
    
    * Standard IPv4 header: 20 bytes
        
    * UDP header: 8 bytes
        
    * **Maximum UDP data**: 1,500 - 20 - 8 = **1,472 bytes**
        

## Common UDP Port Numbers

* **53**: DNS (Domain Name System)
    
* **67/68**: DHCP (Dynamic Host Configuration Protocol)
    
* **69**: TFTP (Trivial File Transfer Protocol)
    
* **123**: NTP (Network Time Protocol)
    
* **161/162**: SNMP (Simple Network Management Protocol)
    

## Flow Control, Congestion Control, and Error Control

### Flow Control: **NO**

UDP does not implement flow control. The sender can transmit data as fast as it wants, regardless of the receiver's ability to process it.

### Congestion Control: **NO**

UDP has no built-in congestion control mechanisms. It doesn't monitor network conditions or adjust transmission rates.

### Error Control: **OPTIONAL**

UDP provides minimal error control through the checksum field:

* **Error Detection**: Optional - can detect corrupted packets
    
* **Error Correction**: NEVER performed - corrupted packets are simply discarded
    
* **Error Recovery**: No automatic retransmission - application must handle if needed
    

### UDP Checksum Calculation (Error Detection)

The Internet Checksum covers:

1. **Pseudo-header** (derived from IP header) - The pseudo header is a conceptual construct used in UDP checksum calculation that includes critical fields from the IP header, even though it's not actually transmitted as part of the UDP packet. The pseudo header consists of the source IP address, destination IP address, protocol number (17 for UDP), and UDP length.
    
2. **Complete UDP header** (with checksum field set to 0)
    
3. **UDP data** (padded with 0 if odd length)
    

**Sender Side Process**

**Step 1: Prepare Data** Example UDP packet:

* Source IP: 192.168.1.10
    
* Dest IP: 192.168.1.1
    
* Source Port: 8080
    
* Dest Port: 80
    
* Data: "Hello" (5 bytes)
    

**Step 2: Create Pseudo-header (12 bytes)**

```plaintext
C0 A8 01 0A    (Source IP: 192.168.1.10)
C0 A8 01 01    (Dest IP: 192.168.1.1)
00 11          (Protocol: 17 = UDP)
00 0D          (UDP Length: 13 bytes = 8 header + 5 data)
```

**Step 3: Create UDP Header + Data (13 bytes)**

```plaintext
1F 90          (Source Port: 8080)
00 50          (Dest Port: 80)
00 0D          (Length: 13)
00 00          (Checksum: set to 0 for calculation)
48 65 6C 6C 6F (Data: "Hello")
00             (Padding: add 0 byte since data length is odd)
```

**Step 4: Combine and Arrange in 16-bit Words** Total: 26 bytes = 13 sixteen-bit words

```plaintext
Word 1:  C0A8  (pseudo-header)
Word 2:  010A
Word 3:  C0A8
Word 4:  0101
Word 5:  0011
Word 6:  000D
Word 7:  1F90  (UDP header starts)
Word 8:  0050
Word 9:  000D
Word 10: 0000  (checksum field = 0)
Word 11: 4865  (data starts: "He")
Word 12: 6C6C  ("ll")
Word 13: 6F00  ("o" + padding)
```

**Step 5: Calculate Checksum (Sender)**

```plaintext
Sum all words:
  C0A8
  010A
  C0A8
  0101
  0011
  000D
  1F90
  0050
  000D
  0000
  4865
  6C6C
+ 6F00
------
2 C737  (carry = 2, sum = C737)

Add carries: C737 + 2 = C739
One's complement: ~C739 = 38C6
```

**Step 6: Insert Checksum** Replace the 0000 in the UDP header with 38C6:

```plaintext
Final UDP packet:
1F 90 00 50 00 0D 38 C6 48 65 6C 6C 6F
```

**Receiver Side Process**

**Step 1: Extract Received Packet** Received UDP packet: 1F 90 00 50 00 0D 38 C6 48 65 6C 6C 6F

**Step 2: Reconstruct Pseudo-header** Same as sender (derived from IP header)

**Step 3: Arrange All Data Including Received Checksum**

```plaintext
Word 1:  C0A8  (pseudo-header)
Word 2:  010A
Word 3:  C0A8
Word 4:  0101
Word 5:  0011
Word 6:  000D
Word 7:  1F90  (UDP header)
Word 8:  0050
Word 9:  000D
Word 10: 38C6  (received checksum)
Word 11: 4865  (data)
Word 12: 6C6C
Word 13: 6F00  (with padding)
```

**Step 4: Calculate Verification Sum**

```plaintext
Sum all words (including received checksum):
  C0A8
  010A
  C0A8
  0101
  0011
  000D
  1F90
  0050
  000D
  38C6
  4865
  6C6C
+ 6F00
------
2 FFFD  (carry = 2, sum = FFFD)

Add carries: FFFD + 2 = FFFF
```

**Step 5: Check Result**

* **If result = FFFF**: Packet is error-free (or errors not detected)
    
* **If result ≠ FFFF**: Packet has errors → **DISCARD PACKET**
    

**Step 6: Action on Error Detection**

* **Corrupted packet**: Silently discard (no error correction)
    
* **No notification**: No ICMP error message sent
    
* **Application responsibility**: Must handle missing data if needed
    

### Key Points About UDP Error Control

1. **Detection Only**: Can detect some errors, cannot correct them
    
2. **Silent Discard**: Corrupted packets are dropped without notification
    
3. **Optional in IPv4**: Checksum can be 0 (disabled)
    
4. **Mandatory in IPv6**: Checksum must be calculated
    
5. **No Retransmission**: UDP never retransmits lost or corrupted packets
    
6. **Application Layer**: Must implement its own reliability if needed
    

### Checksum Limitations

The Internet Checksum can miss certain error patterns:

* Cannot detect reordering of 16-bit words
    
* Cannot detect insertion/deletion of zero bytes
    
* May miss some multiple-bit errors
    
* Provides basic but not cryptographic integrity
    

### **When to use UDP:**

* Real-time applications (gaming, video streaming)
    
* Simple request-response protocols (DNS)
    
* Broadcast/multicast communications
    
* Applications that implement their own reliability
    

### **When NOT to use UDP:**

* File transfers requiring integrity
    
* Applications needing guaranteed delivery
    
* Order-sensitive data transmission
