Monday, April 22, 2013

UDP header explained

A UDP header is used when sending data via the User Datagram Protocol (UDP) in the Transport Layer, as opposed to sending via TCP (Transmission Control Protocol). UDP is connectionless, doesn't guarantee reliable data transfer and has a smaller overhead than TCP. The UDP header is only 8 bytes long (as compared to 20 bytes for a TCP header). Below is the header structure:

UDP header

  • Source Port: specifies the sender's port where replies should be sent to. Set to zero if not used.
  • Destination Port: port number of the receiver. While the source port is optional, this one is required.
  • Length: length of the entire datagram in bytes, namely 8 bytes for the header + size of the payload. Note that potential outer headers, like IP header should not be counted towards it. Thus here the datagram refers to the UDP datagram
  • Checksum: a checksum is calculated over the header plus the data. If no checksum is generated, the checksum field should be all zero. However, it's recommended to use a checksum, and IPv6 in fact requires it. The checksum is calculated via a specific algorithm over the header + data with the checksum field initially set to zero.

Some Notes

The port numbers in the range 0-1023 represents the well-known ports. Most of them are assigned to a specific service (for example DNS on port 53) and are used by either TCP or UDP, or by both in some cases. Note that there are not two separate port number spaces for TCP and UDP.

Note that for all multi-byte fields, the number you're storing in the field is encoded in network byte order. This is usually different from how the number is stored on the computer internally. For conversion you will need the functions ntohl(), ntohs(), htonl() and htons() that convert integers between host and network byte order.

The checksum is calculated over the header and the data. This is for example different to the checksum in the IP header which is only calculated for the IP header.

For a more detailed discussion on UDP and a comparison to TCP, check out this article.

-------------------------------
Let me know what you think of this article and where you see room for improvement. I only just started this blog, so feedback of any kind is appreciated!


Related Articles:
What's the difference between TCP and UDP?
Reading Notes: A Protocol for Packet Network Intercommunication
Reading Notes: Ethernet paper by Metcalfe and Boggs

No comments:

Post a Comment