Checksum checking in UDP using UDP socket

1.6k Views Asked by At

Is it possible for a UDP socket (SOCK_DGRAM) to access checksum field from an incoming UDP packet and check for errors? I know that we can do that using raw sockets (SOCK_RAW), but I want to know whether we can do it using datagram sockets. If so, how can we do it in C?

2

There are 2 best solutions below

2
Steffen Ullrich On

If you create a normal UDP socket you don't have access to the UDP header and thus also not to the checksum. But the kernel will already discard packets where the checksum is incorrect so you would not see these packets anyway.

1
Javier Silva Ortíz On

You can't do it using datagram sockets (SOCK_DGRAM), because the TCP/IP stack removes those UDP header bytes from the received buffer before passing it up to higher layer APIs. You need to use raw sockets (SOCK_RAW) so that these bytes are preserved.