An Overview of the User Datagram Protocol

Posted by Jarren Long at 2016-12-06 12:24:06

As a five year follow-up to my previous article, An Overview of the Transmission Control Protocol, I figured I would go ahead and crank out its companion piece to show how the User Datagram Protocol (UDP). This one is going to be much shorter; the entire RFC that defines UDP is a mere 1.5 pages in length.

A brief history: The User Datagram Protocol (UDP) was created by David P.Reed in 1980 to run over Internet Protocol (IP) packet-switched networks. UDP is a connectionless protocol, and does not require a connection to be established to a destination prior to transmitting data. It is stateless, and best used for unidirectional and simple query-response data transactions.

From the introduction to RFC 768: "This User Datagram Protocol (UDP) is defined to make available a datagram mode of packet-switched computer communication in the environment of an interconnected set of computer networks. This protocol assumes that the Internet Protocol (IP) is used as the underlying protocol. This protocol provides a procedure for application programs to send messages to other programs with a minimum of protocol mechanism. The protocol is transaction oriented, and delivery and duplicate protection are not guaranteed. Applications requiring ordered reliable delivery of streams of data should use the Transmission Control Protocol (TCP)" (Jon Postel).

That pretty well sums up the purpose of UDP; to send transaction data with next to zero overhead, or any guarantee that your data will arrive in order, or at all. That being said, if you need fast data delivery, but don't care if or how it gets there, then UDP is the protocol for you!

The UDP protocol is ridiculously simple, and can be represented by a 12-byte header, as defined (C-style) below:

typedef struct UDP_HEADER {
  unsigned short sourcePort; // Optional: The port that is sending the data, and where replies should be directed
  unsigned short destPort; // The destination port on a device, meaningless without a destination address, which is usually provided by the underlying IP stack
  unsigned short length; // The length of the octets (number of bytes) in this packet, including this header. Minimum is 8.
  unsigned short checksum; // The 16-bit ones complement of the ones complement sum of a pseudo IP (IPv4 or IPv6) header, UDP header, and data, padded with zero octets at the end (if necessary) to make a multiple of two octets.
  void *data; // The UDP packet data itself. Because of the 16-bit Length size, a UDP packet is limited to no more than 64Kb data (including the header).
} UDP_HEADER;

As you can see, UDP is tiny. And because it makes no attempts at guaranteeing ordered delivery like TCP does, it is extremely fast. It is also tailored for supporting broadcast communications, something TCP can handle, but not well. Overall, UDP is heavily used by some core networking services, such as DNS, DHCP, SNMP, and RIP, as well as many game engines for online gaming (Doom is a notable example).