friendev EtherDune TCP/IP library
/home/jander/temp/etherdune/ICMP.cpp
Go to the documentation of this file.
1 // EtherDune ICMP Abstract base class
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Implements common ICMP packet building routines and ICMP checksum
4 // Summary: Used as base class for ICMP Echo (ping)
5 //
6 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
7 //
8 // This source is subject to the GPLv2 license.
9 // Please see the License.txt file for more information.
10 // All other rights reserved.
11 //
12 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
13 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
15 // PARTICULAR PURPOSE.
16 
17 #include "ICMP.h"
18 #include "Checksum.h"
19 
20 #define AC_LOGLEVEL 2
21 #include <ACLog.h>
22 ACROSS_MODULE("ICMP");
23 
24 bool ICMP::onPacketReceived()
25 {
26  if (!(packet.ip.protocol == IP_PROTO_ICMP))
27  {
28  return false;
29  }
30 
31 
32  return onICMPMessage();
33 
34 }
35 
41 {
42  NetworkService::loadAll(); //bring in the rest of the packet.
43 
44 #if ENABLE_ICMP_RX_CHECKSUM
45 
47  if (packet.icmp.checksum != 0)
48  return false; //checksum error, drop packet
49 
50 #endif
51 
52  return true;
53 }
54 
60 {
61  packet.icmp.checksum = ~Checksum::calc(packet.ip.totalLength - sizeof(IPHeader), (uint8_t*)&packet.icmp);
62 }
63 
69 void ICMP::sendICMPPacket(const IPAddress& targetIP, uint16_t dataLength)
70 {
71  ACASSERT(dataLength < EtherDune_BUFFER_SIZE - sizeof(EthernetHeader) - sizeof(IPHeader) - sizeof(ICMPHeader), "ICMP packet too big. Actual size=%d, max size=%d", dataLength, EtherDune_BUFFER_SIZE - sizeof(EthernetHeader) - sizeof(IPHeader) - sizeof(ICMPHeader));
72 
74  dataLength += sizeof(IPHeader) + sizeof(ICMPHeader);
75  packet.ip.totalLength = dataLength;
76  prepareIPPacket(targetIP);
77 
78  packet.icmp.checksum = 0;
80  sendIPPacket(dataLength);
81 
82 
83 }
uint16_t checksum
Error checking data.
Definition: inet.h:435
ICMPHeader icmp
Definition: inet.h:509
bool loadAll()
Loads the entire ICMP packet into memory.
Definition: ICMP.cpp:40
static EthBuffer packet
in-memory packet buffer currently being processed.
virtual bool onICMPMessage()=0
Represents the header of an Ethernet frame.
Definition: inet.h:457
represents an IP address in memory
Definition: inet.h:181
uint8_t protocol
Protocol.
Definition: inet.h:318
IP header.
Definition: inet.h:296
Contains functions to perform IP checksum operations.
Definition: Checksum.h:26
ICMP Header and Echo request/reply.
Definition: inet.h:431
void sendICMPPacket(const IPAddress &targetIP, uint16_t dataLength)
Sends an ICMP packet
Definition: ICMP.cpp:69
void calcICMPChecksum()
Calculates the ICMP checksum and populates the checksum field in the ICMP header. ...
Definition: ICMP.cpp:59
#define EtherDune_BUFFER_SIZE
RAM memory buffer size to hold a packet.
Definition: config.h:41
static bool sendIPPacket(uint16_t length)
Puts the current in-memory packet in the network
static void prepareIPPacket(const IPAddress &remoteIP)
Sets up common IP header values for all outgoing IP packets and calculates the IP header checksum ...
static void loadAll()
Definition: ENC28J60.cpp:400
ACROSS_MODULE("ICMP")
static const uint8_t IP_PROTO_ICMP
IP header protocol type for ICMP.
Definition: inet.h:570
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