0

I was reading a textbook which says the need of length header for UDP: enter image description here

The length field specifies the number of bytes in the UDP segment (header plus data). An explicit length value is needed since the size of the data field may differ from one UDP segment to the next.

So my question is:

Is the length header used to indicate the end of a segment?

And if the answer is yes, then why can't we use a special bit pattern to indicate the end of a segment?

3
  • We could, but that’s not the way it was designed. There are many ways to implement things. – Ron Trunk Aug 20 '20 at 16:24
  • There’s always the danger that the special pattern could be in the payload. – Ron Trunk Aug 20 '20 at 16:25
  • Did any answer help you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you can post and accept your own answer. – Ron Maupin Dec 17 '20 at 20:58
2

Is the length header used to indicate the end of a segment?

"segment" is the wrong term with UDP since it suggests that it is part of something bigger which gets eventually reassembled. UDP has the concept of a datagram or a message and such datagrams should be considered separate entities. This means that they can be lost, duplicated or reordered and UDP does not define what happens in this case. Dealing with this is up to the application.

... then why can't we use a special bit pattern to indicate the end of a segment?

This would be way more complex and time consuming to process than just prefixing the data with its length. It would mean that one would need to introduce a mechanism to either escape data which accidentally match the bit pattern or encode all the data in something like base64 (which is a waste of space compared to pure binary). It would also mean to actively scan the whole payload for the bit pattern and rewrite everything which was escaped or (in case of base64) to decode the data again.

2

It's just the way UDP was originally specified.

It could work without a length field in the header as long as the lower-layer protocol(s) made it possible to determine the length of a packet. That's why TCP is able to work without a built-in length field. UDP could have been designed that way; it just wasn't.

2
  • thanks for youe answer. if thats the case, then why the author says: An explicit length value is needed since the size of the data field may differ from one UDP segment to the next? this sounds like it is necessary to have length header in UDP? – slowjams Aug 21 '20 at 0:25
  • It's only necessary because that's how UDP is specified. Imagine if the length field were replaced with 0 for all UDP traffic between two hosts. Upon receipt, you could simply recover the original value by subtracting the UDP & IP header lengths from the total length of the encapsulating IP packet. The fact that UDP carries an explicit length field instead of making this information implicit doesn't mean it wasn't possible for it to be implicit; it was just a choice made by the protocol designers. – Jeff Wheeler Aug 21 '20 at 10:24
1

Another aspect that's not been mentioned is 32-bit alignment of the data field: with the other three 16-bit header fields, the UDP header wouldn't have been a multiple of 32-bit words which the IETF aims for to speed up processing on 32-bit systems. The (actually redundant) length field was likely inserted to fill the otherwise reserved 16 bit with something useful.

Note the difference to TCP which doesn't sport a length header field, likely for the exact same reason. (With TCP, the segment length is derived from the IP header length field which UDP could have used as well just the same.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.