UDP checksum calculation

前端 未结 3 1380
灰色年华
灰色年华 2020-12-28 14:52

The UDP header struct defined at /usr/include/netinet/udp.h is as follows

struct udphdr
{
  u_int16_t source;
  u_int16_t dest;
  u_int16_t len;
  u_int16_t          


        
3条回答
  •  抹茶落季
    2020-12-28 15:31

    A nice and easy to understand example of UDP checksum calculation is done by Gerd Hoffmann.

    You can google for "net-checksum.c Gerd Hoffmann" or look at the file here:

    https://gist.github.com/fxlv/81209bbd150abfeaceb1f85ff076c9f3

    You can use net_checksum_tcpudp function, feed it the UDP payload length, proto, src and dst IPs and then the UDP payload itself and it will do the right thing.

    At the end you have to call htons() on the checksum and you're good.

提交回复
热议问题