OpENer - Open Source EtherNet/IP(TM) I/O Target Stack  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
udp_protocol.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2018, Rockwell Automation, Inc.
3  * All rights reserved.
4  *
5  ******************************************************************************/
6 
7 #include "udp_protocol.h"
8 
9 #ifdef WIN32
10 #include <winsock.h>
11 #endif
12 
13 void UDPHeaderSetSourcePort(UDPHeader *const header,
14  const uint16_t source_port) {
15  header->source_port = source_port;
16 }
17 
18 uint16_t UDPHeaderGetSourcePort(const UDPHeader *const header) {
19  return header->source_port;
20 }
21 
23  const uint16_t destination_port) {
24  header->destination_port = destination_port;
25 }
26 
27 uint16_t UDPHeaderGetDestinationPort(const UDPHeader *const header) {
28  return header->destination_port;
29 }
30 
32  const uint16_t packet_length) {
33  header->packet_length = packet_length;
34 }
35 
36 uint16_t UDPHeaderGetPacketLength(const UDPHeader *const header) {
37  return header->packet_length;
38 }
39 
40 void UDPHeaderSetChecksum(UDPHeader *const header,
41  const uint16_t checksum) {
42  header->checksum = checksum;
43 }
44 
45 uint16_t UDPHeaderGetChecksum(const UDPHeader *const header) {
46  return header->checksum;
47 }
48 
49 void UDPHeaderGenerate(const UDPHeader *header,
50  char *message) {
51  *( (uint16_t *)message ) = htons(UDPHeaderGetSourcePort(header) );
52  message += 2;
53  *( (uint16_t *)message ) = htons(UDPHeaderGetDestinationPort(header) );
54  message += 2;
55  *( (uint16_t *)message ) = htons(UDPHeaderGetPacketLength(header) );
56  message += 2;
57  *( (uint16_t *)message ) = htons(UDPHeaderGetChecksum(header) );
58  message += 2;
59 }
60 
61 uint16_t UDPHeaderCalculateChecksum(const void *udp_packet,
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;
68 
69  // Process UDP packet
70  while(length > 1) {
71  checksum += *udp_packet_words++;
72  if(checksum & 0x8000000) {
73  checksum = (checksum & 0xFFFF) + (checksum >> 16);
74  }
75  length -= 2;
76  }
77 
78  if(0 != length % 2) {
79  // Add padding if packet length is odd
80  checksum += *( (uint8_t *)udp_packet_words );
81  }
82 
83  //Process IP pseudo header
84  uint16_t *source_addr_as_words = (void *)&source_addr;
85  checksum += *source_addr_as_words + *(source_addr_as_words + 1);
86 
87  uint16_t *destination_addr_as_words = (void *)&destination_addr;
88  checksum += *destination_addr_as_words + *(destination_addr_as_words + 1);
89 
90  checksum += htons(IPPROTO_UDP);
91  checksum += htons(udp_packet_length);
92 
93  //Add the carries
94  while(0 != checksum >> 16) {
95  checksum = (checksum & 0xFFFF) + (checksum >> 16);
96  }
97 
98  // Return one's complement
99  return (uint16_t)(~checksum);
100 }
uint16_t UDPHeaderGetChecksum(const UDPHeader *const header)
Gets checksum field.
Definition: udp_protocol.c:45
uint16_t UDPHeaderGetPacketLength(const UDPHeader *const header)
Gets packet length field.
Definition: udp_protocol.c:36
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.
Definition: udp_protocol.c:61
uint16_t source_port
Definition: udp_protocol.h:34
void UDPHeaderSetChecksum(UDPHeader *const header, const uint16_t checksum)
Sets checksum field.
Definition: udp_protocol.c:40
Representing the needed information for the UDP header.
Definition: udp_protocol.h:33
void UDPHeaderGenerate(const UDPHeader *header, char *message)
Generate the UDP header in the message according to the header.
Definition: udp_protocol.c:49
uint16_t packet_length
Definition: udp_protocol.h:36
uint16_t UDPHeaderGetDestinationPort(const UDPHeader *const header)
Gets destination port field.
Definition: udp_protocol.c:27
uint16_t checksum
Definition: udp_protocol.h:37
void UDPHeaderSetSourcePort(UDPHeader *const header, const uint16_t source_port)
Sets source port field.
Definition: udp_protocol.c:13
Includes a basic set of operations for UDP header creation and checksum calculation.
uint16_t UDPHeaderGetSourcePort(const UDPHeader *const header)
Gets source port field.
Definition: udp_protocol.c:18
uint16_t destination_port
Definition: udp_protocol.h:35
void UDPHeaderSetDestinationPort(UDPHeader *const header, const uint16_t destination_port)
Sets destination port field.
Definition: udp_protocol.c:22
void UDPHeaderSetPacketLength(UDPHeader *const header, const uint16_t packet_length)
Sets packet length field.
Definition: udp_protocol.c:31