14 const uint16_t source_port) {
23 const uint16_t destination_port) {
32 const uint16_t packet_length) {
41 const uint16_t checksum) {
62 const size_t udp_packet_length,
63 const in_addr_t source_addr,
64 const in_addr_t destination_addr) {
65 const uint16_t *udp_packet_words = udp_packet;
66 uint32_t checksum = 0;
67 size_t length = udp_packet_length;
71 checksum += *udp_packet_words++;
72 if(checksum & 0x8000000) {
73 checksum = (checksum & 0xFFFF) + (checksum >> 16);
80 checksum += *( (uint8_t *)udp_packet_words );
84 uint16_t *source_addr_as_words = (
void *)&source_addr;
85 checksum += *source_addr_as_words + *(source_addr_as_words + 1);
87 uint16_t *destination_addr_as_words = (
void *)&destination_addr;
88 checksum += *destination_addr_as_words + *(destination_addr_as_words + 1);
90 checksum += htons(IPPROTO_UDP);
91 checksum += htons(udp_packet_length);
94 while(0 != checksum >> 16) {
95 checksum = (checksum & 0xFFFF) + (checksum >> 16);
99 return (uint16_t)(~checksum);
uint16_t UDPHeaderGetChecksum(const UDPHeader *const header)
Gets checksum field.
uint16_t UDPHeaderGetPacketLength(const UDPHeader *const header)
Gets packet length field.
uint16_t UDPHeaderCalculateChecksum(const void *udp_packet, const size_t udp_packet_length, const in_addr_t source_addr, const in_addr_t destination_addr)
Calculates the checksum based on the set UDP packet data and pseudo IP header.
void UDPHeaderSetChecksum(UDPHeader *const header, const uint16_t checksum)
Sets checksum field.
void UDPHeaderGenerate(const UDPHeader *header, char *message)
Generate the UDP header in the message according to the header.
uint16_t UDPHeaderGetDestinationPort(const UDPHeader *const header)
Gets destination port field.
void UDPHeaderSetSourcePort(UDPHeader *const header, const uint16_t source_port)
Sets source port field.
Includes a basic set of operations for UDP header creation and checksum calculation.
uint16_t UDPHeaderGetSourcePort(const UDPHeader *const header)
Gets source port field.
void UDPHeaderSetDestinationPort(UDPHeader *const header, const uint16_t destination_port)
Sets destination port field.
void UDPHeaderSetPacketLength(UDPHeader *const header, const uint16_t packet_length)
Sets packet length field.