0

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 Answers2

1

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.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • But, we can still access the source port number of the sender, which is a transport layer field. I thought, maybe we can access the checksum also. Anyways, thanks for the confirmation. Also, are you sure the kernel discards incorrect packets using checksum? I remember reading that UDP warns the application layer in case of incorrect packets. So, I'm thinking that there might be an interface to catch that warning. – Karthik Chennupati Oct 25 '19 at 11:45
  • @KarthikChennupati: see [Are UDP Packets dropped when UDP header checksum is incorrect?](https://stackoverflow.com/questions/18452716/are-udp-packets-dropped-when-udp-header-checksum-is-incorrect/18452930). I think you confuse this was information about truncated messages. – Steffen Ullrich Oct 25 '19 at 11:55
0

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.

Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21
  • I'm developing a reliable transport service on top of UDP. In raw sockets, there is no concept of ports. So, I thought it might make more sense if we use a UDP socket. Anyways, thanks for the clarification. – Karthik Chennupati Oct 25 '19 at 11:47