friendev EtherDune TCP/IP library
/home/jander/temp/etherdune/UDPSocket.cpp
Go to the documentation of this file.
1 // EtherDune UDP implementation as a NetworkService
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Implements the UDP protocol
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
16 #include "UDPSocket.h"
17 #include "Checksum.h"
18 
19 #define AC_LOGLEVEL 2
20 #include <ACLog.h>
21 ACROSS_MODULE("UDPSocket");
22 
28 void UDPSocket::onReceive(uint16_t len) { }
29 
30 
31 UDPSocket::UDPSocket() :sending(false)
32 {
33 #if _DEBUG
34  localPort.l = random(255);
35 #else
37 #endif
39 }
40 
46 {
47  if (sending)
48  return false;
49 
50  sending = true;
52 
53  return true;
54 }
55 
56 
62 void UDPSocket::prepareUDPPacket(uint16_t dataLength, uint16_t dataChecksum)
63 {
64  packet.ip.totalLength=dataLength + sizeof(IPHeader) + sizeof(UDPHeader);
69 
70  packet.udp.dataLength = dataLength + sizeof(UDPHeader);
72 
73  packet.udp.checksum.rawValue = calcUDPChecksum(dataLength, dataChecksum);
74 
75 }
76 
82 {
83  uint16_t dataChecksum = 0;
84  uint16_t dataLength;
85 
86  dataLength = buffer.fillTxBuffer(sizeof(EthernetHeader) + sizeof(IPHeader) + sizeof(UDPHeader),/*out*/ dataChecksum);
87 
88  prepareUDPPacket(dataLength, dataChecksum);
89 
90  if (sendIPPacket(sizeof(IPHeader) + sizeof(UDPHeader)))
91  {
92  buffer.flush();
93  return false;
94  }
95  else
96  return true;
97 }
98 
100 {
101  if (sending)
102  sending = sendPacket();
103 
104 }
105 
106 bool UDPSocket::onPacketReceived()
107 {
108  if (!(
112  {
113  return false;
114  }
115 
116  loadAll();
117 
118 #if ENABLE_UDPTCP_RX_CHECKSUM
119 
120  if (!verifyUDPTCPChecksum())
121  {
122  ACWARN("UDP checksum error");
123  return true;// drop packet, UDP checksum error
124  }
125 
126 #endif
127 
129  return true;
130 }
nint16_t localPort
local TCP or UDP port
Definition: Socket.h:54
virtual void onReceive(uint16_t len)
Called when a datagram has arrived.
Definition: UDPSocket.cpp:28
nint16_t etherType
Protocol type, e.g.
Definition: inet.h:461
static const uint16_t ETHTYPE_IP
Ethernet header protocol type for IP.
Definition: inet.h:578
Structure to represent an UDP header.
Definition: inet.h:388
void prepareIPPacket()
Definition: Socket.cpp:27
static EthBuffer packet
in-memory packet buffer currently being processed.
nint16_t remotePort
remote TCP or UDP port
Definition: Socket.h:53
uint16_t rawValue
provides low level access to the memory containing the network-order integer
Definition: inet.h:61
static bool verifyUDPTCPChecksum()
Verifies if the UDP or TCP checksum of the current packet is correct.
Definition: Socket.cpp:205
Represents the header of an Ethernet frame.
Definition: inet.h:457
void tick()
This is a timer function that is called every NETWORK_TIMER_RESOLUTION milliseconds on every service...
Definition: UDPSocket.cpp:99
bool send()
Sends the packet currently in the outgoing buffer
Definition: UDPSocket.cpp:45
UDPHeader udp
Definition: inet.h:512
SharedBuffer buffer
output buffer for this socket
Definition: Socket.h:42
uint8_t protocol
Protocol.
Definition: inet.h:318
void flush()
Releases all data in this buffer, freeing up space.
IP header.
Definition: inet.h:296
static uint16_t calcUDPChecksum(uint16_t dataLength, uint16_t dataChecksum)
Calculates the UDP checksum.
Definition: Socket.cpp:192
ACROSS_MODULE("UDPSocket")
nint16_t destinationPort
Identifies the receiving port.
Definition: inet.h:391
void prepareUDPPacket(uint16_t dataLength, uint16_t dataChecksum)
Fills out the UDP header and calculates the datagram checksum
Definition: UDPSocket.cpp:62
nint16_t dataLength
Total length of payload and header.
Definition: inet.h:392
static bool sendIPPacket(uint16_t length)
Puts the current in-memory packet in the network
nint16_t checksum
The 16-bit checksum field is used for error-checking of the header and data.
Definition: inet.h:393
nint16_t sourcePort
Identifies the sending port.
Definition: inet.h:390
static uint8_t srcPort_L_count
self-incrementing counter for local ports.
Definition: Socket.h:43
static void loadAll()
Definition: ENC28J60.cpp:400
uint8_t l
least significant byte
Definition: inet.h:65
IPHeader ip
Definition: inet.h:506
nint16_t totalLength
This 16-bit field defines the entire packet (fragment) size, including header and data...
Definition: inet.h:310
uint8_t h
most significant byte
Definition: inet.h:64
virtual bool sendPacket()
Sends the packet currently in the shared buffer
Definition: UDPSocket.cpp:81
static const uint8_t UDP_SRC_PORT_H
Indicates the most significant byte of the source port number that EtherDune will use by default when...
Definition: config.h:148
EthernetHeader eth
Definition: inet.h:500
void zero()
sets the variable to 0.
Definition: inet.h:97
uint16_t fillTxBuffer(uint16_t dstOffset, uint16_t &checksum, uint16_t count=0xFFFF)
Copies up to count fragments to the transmit buffer via DMA, concatenating them starting at given off...
static const uint8_t IP_PROTO_UDP
IP header protocol type for UDP.
Definition: inet.h:572