Rawsock library  v0.3.4
Library to make the use of raw sockets easier, under Linux. Currently supporting IPv4, UDP and a custom latency measurement protocol (LaMP - supported both in raw and non-raw sockets).
minirighi_udp_checksum.c
1 // Rawsock library, licensed under GPLv2
2 // Version 0.3.4
4 
18 uint16_t minirighi_udp_checksum(const void *buff, size_t len, in_addr_t src_addr, in_addr_t dest_addr) {
19  const uint16_t *buf=buff;
20  uint16_t *ip_src=(void *)&src_addr, *ip_dst=(void *)&dest_addr;
21  uint32_t sum;
22  size_t length=len;
23 
24  sum = 0;
25  while (len > 1) {
26  sum += *buf++;
27  if (sum & 0x80000000)
28  sum = (sum & 0xFFFF) + (sum >> 16);
29  len -= 2;
30  }
31 
32  if ( len & 1 )
33  sum += *((uint8_t *)buf);
34 
35  sum += *(ip_src++);
36  sum += *ip_src;
37  sum += *(ip_dst++);
38  sum += *ip_dst;
39 
40  sum += htons(IPPROTO_UDP);
41  sum += htons(length);
42 
43  while (sum >> 16)
44  sum = (sum & 0xFFFF) + (sum >> 16);
45 
46  return ( (uint16_t)(~sum) );
47 }
uint16_t minirighi_udp_checksum(const void *buff, size_t len, in_addr_t src_addr, in_addr_t dest_addr)
Calculate the UDP checksum (calculated with the whole packet)