```UDP CHECKSUM CALCULATION

Protocol		2 bytes
UDP Length  		2 bytes

The checksum is calculated over all the octets of the pseudo header, UDP header and data.
If the data contains an odd number of octets a pad, zero octet is added to the end of data.
The pseudo header and the pad are not transmitted with the packet.

In the example code,
u16 buff[] is an array containing all the octets in the UDP header and data.
u16 len_udp is the length (number of octets) of the UDP header and data.
BOOL padding is 1 if data has an even number of octets and 0 for an odd number.

/*
**************************************************************************
Function: udp_sum_calc()
Description: Calculate UDP checksum
***************************************************************************
*/
typedef unsigned short u16;
typedef unsigned long u32;

{
u16 prot_udp=17;
u16 word16;
u32 sum;

// Find out if the length of data is even or odd number. If odd,
// add a padding byte = 0 at the end of packet
buff[len_udp]=0;
}

//initialize sum to zero
sum=0;

// make 16 bit words out of every two adjacent 8 bit words and
// calculate the sum of all 16 vit words
word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
sum = sum + (unsigned long)word16;
}
for (i=0;i<4;i=i+2){
sum=sum+word16;
}
for (i=0;i<4;i=i+2){
sum=sum+word16;
}
// the protocol number and the length of the UDP packet
sum = sum + prot_udp + len_udp;

// keep only the last 16 bits of the 32 bit calculated sum and add the carries
while (sum>>16)
sum = (sum & 0xFFFF)+(sum >> 16);

// Take the one's complement of sum
sum = ~sum;

return ((u16) sum);
}```

mail to alex