Network by Example - Datagrams: UDP

2 minute read


Datagrams: UDP

It’s a Datagram orinted protocol that preserves message boundaries.

Each UDP datagram is an independent entity, carrying no context that relates it to another. Each UDP operation produces one UDP datagram/IP Datagram to be sent, unllike the stream-oriented TCP. Each is an independent entity, carrying no context that relates it to another.

It can provide error detection, and it includes true E2E checksum at the transport layer.

There’s no error protection/correction, sequencing, non-duplicates, flow control, or garanty of delivery. Apps that need data reliably delivered and sequenced must implement those features themselves, such as RTP. However, this connectionless nature makes has less overhead. The Header is 8 bytes (56 bits):

                  0      7       15       23       31
                 +--------+--------+--------+--------+
                 |    src port     |    dst port     |
                 +--------+--------+--------+--------+
                 |     Length      |    Checksum     |
                 +--------+--------+--------+--------+
                 |          data octets ...
                 +---------------- ...
  • Ports: The source is optional, can be 0 if a reply is not needed. IP demultiplex by using the protocol field (udp value is 17) in the IP header to identify the transport protocol. This means that 2 apps can use the same ip:port, once for tcp and another for udp.
  • Lenght: It’s redundant, since the IP header already has the lenght.

Checksum

The checksum in the IP is only for the IP header, not the data and is recompute at each hop (since the TTL is decremented). In UDP/TCP the checksum is for data and headers.

It’s one’s complement sum of a pseudo header of information from the IP header, the UDP header, and the data, padded with 0:

                  0      7       15       23       31
                 +--------+--------+--------+--------+
                 |          source address           |
                 +--------+--------+--------+--------+
                 |        destination address        |
                 +--------+--------+--------+--------+
                 |  zero  |protocol|   UDP length    |
                 +--------+--------+--------+--------+

If the computed checksum is zero, it is transmitted as all ones. If the sender computes a checksum, the receiver must too. The Pseudo header is not transmitted.

Testing
## running a server in udp port 5000
~ $ nc -ul 5000
## alternative is $ socat - UDP-LISTEN:5000
hello ## receives hello from the client
## capture
~ $ tshark -i lo
## alternative is $ tcpdump -n -p -s 1500 -vvv -i lo
    1 0.000000000 127.0.0.1     → 127.0.0.1     UDP 48 34604 → 5000 Len=6
## client sends a udp datagram to the server
~ $ echo hello | nc -q 0 -u localhost 5000
## alternative is $ echo hello | socat - UDP:localhost:5000

With the server stopped, sending a datagram to the server will result in an ICMP error message: ICMP 76 Destination unreachable (Port unreachable) We can also see, with the -V in tshark:

    Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
        Identification: 0xa7b4 (42932)
        Flags: 0x4000, Don't fragment
        Fragment offset: 0
        Protocol: UDP (17)
    User Datagram Protocol, Src Port: 59143, Dst Port: 5000
        Source Port: 59143
        Destination Port: 5000
        Length: 14
        Checksum: 0xfe21 [unverified]
        [Checksum Status: Unverified]
        [Stream index: 2]
    Data (6 bytes)

References

Updated:

Leave a Comment