Decoding UDP headers
In the Decoding IP headers recipe of this chapter, we created a decodeIp()
function that decoded the IP headers of a packet. If the protocol type was UDP in that function, we called a decodeUdp()
function. We will create the decodeUdp()
function in this recipe.
The UDP header is a part of the third layer (Protocol layer) of our header stack. This is what the UDP header looks like:
Let's take a look at the fields of the UDP header:
Source Port: This field identifies the port used by the sender, and it can be assumed that any reply should be sent to this port. If no reply is needed or wanted, we should set this port to
0
, indicating that we are not expecting a reply.Destination Port: This field identifies the port on the client to which the datagram has to be sent. The port should be a valid port number between
0
and65535
.Length: This field indicates the size of the UDP header and the payload.
Checksum: This field indicates the checksum for the UDP header.
If you recall from...