While developing the logger for GNU/Linux Anywhere USB Controller (https://savannah.nongnu.org/projects/awusb/), I needed to know the length of the UDP packet to be read so that I could do `malloc()' with the appropriate size. Aproposing for `datagram length' and googling for `how to get the length of a UDP datagram' didn't return any result. But, I found the answer when I tried `man 7 udp'.
Specifically, `ioctl()' is used like in the following code:
if (ioctl (socket_fd, FIONREAD, &datagram_len) == -1)
{
perror_with_location ("Cannot retrieve datagram length");
continue;
}