0.01.00
icmp6.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef ICMP6_HPP_
35 #define ICMP6_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <openthread/icmp6.h>
40 
41 #include "common/encoding.hpp"
42 #include "common/locator.hpp"
43 #include "net/ip6_headers.hpp"
44 
45 using ot::Encoding::BigEndian::HostSwap16;
46 
47 namespace ot {
48 namespace Ip6 {
49 
60 /*
61  * This class implements ICMPv6 header generation and parsing.
62  *
63  */
66 {
67 public:
72  void Init(void) { mType = 0; mCode = 0; mChecksum = 0; mData.m32[0] = 0; }
73 
78  enum Type
79  {
83  };
84 
89  enum Code
90  {
92  };
93 
100  Type GetType(void) const { return static_cast<Type>(mType); }
101 
108  void SetType(Type aType) { mType = static_cast<uint8_t>(aType); }
109 
116  Code GetCode(void) const { return static_cast<Code>(mCode); }
117 
123  void SetCode(Code aCode) { mCode = static_cast<uint8_t>(aCode); }
124 
131  uint16_t GetChecksum(void) const { return HostSwap16(mChecksum); }
132 
139  void SetChecksum(uint16_t aChecksum) { mChecksum = HostSwap16(aChecksum); }
140 
147  uint16_t GetId(void) const { return HostSwap16(mData.m16[0]); }
148 
155  void SetId(uint16_t aId) { mData.m16[0] = HostSwap16(aId); }
156 
163  uint16_t GetSequence(void) const { return HostSwap16(mData.m16[1]); }
164 
171  void SetSequence(uint16_t aSequence) { mData.m16[1] = HostSwap16(aSequence); }
172 
179  static uint8_t GetChecksumOffset(void) { return offsetof(otIcmp6Header, mChecksum); }
180 
187  static uint8_t GetDataOffset(void) { return offsetof(otIcmp6Header, mData); }
188 
190 
196 {
197  friend class Icmp;
198 
199 public:
207  IcmpHandler(otIcmp6ReceiveCallback aCallback, void *aContext) {
208  mReceiveCallback = aCallback;
209  mContext = aContext;
210  mNext = NULL;
211  }
212 
213 private:
214  void HandleReceiveMessage(Message &aMessage, const MessageInfo &aMessageInfo, const IcmpHeader &aIcmp6Header) {
215  mReceiveCallback(mContext, &aMessage, &aMessageInfo, &aIcmp6Header);
216  }
217 
218  IcmpHandler *GetNext(void) { return static_cast<IcmpHandler *>(mNext); }
219 };
220 
225 class Icmp: public InstanceLocator
226 {
227 public:
234  Icmp(otInstance &aInstance);
235 
244  Message *NewMessage(uint16_t aReserved);
245 
255  otError RegisterHandler(IcmpHandler &aHandler);
256 
269  otError SendEchoRequest(Message &aMessage, const MessageInfo &aMessageInfo, uint16_t aIdentifier);
270 
283  otError SendError(IcmpHeader::Type aType, IcmpHeader::Code aCode, const MessageInfo &aMessageInfo,
284  const Header &aHeader);
285 
297  otError HandleMessage(Message &aMessage, MessageInfo &aMessageInfo);
298 
309  otError UpdateChecksum(Message &aMessage, uint16_t aPseudoHeaderChecksum);
310 
318  bool IsEchoEnabled(void) { return mIsEchoEnabled; }
319 
326  void SetEchoEnabled(bool aEnabled) { mIsEchoEnabled = aEnabled; }
327 
328 private:
329  otError HandleEchoRequest(Message &aMessage, const MessageInfo &aMessageInfo);
330 
331  IcmpHandler *mHandlers;
332 
333  uint16_t mEchoSequence;
334  bool mIsEchoEnabled;
335 };
336 
342 } // namespace Ip6
343 } // namespace ot
344 
345 #endif // NET_ICMP6_HPP_
Echo Request.
Definition: icmp6.h:62
This class implements IPv6 header generation and parsing.
Definition: ip6_headers.hpp:136
This type represents all the static / global variables used by OpenThread allocated in one place...
Definition: openthread-instance.h:59
Definition: cli.cpp:90
uint16_t GetChecksum(void) const
This method returns the ICMPv6 message checksum.
Definition: icmp6.hpp:131
void SetCode(Code aCode)
This method sets the ICMPv6 message code.
Definition: icmp6.hpp:123
void SetType(Type aType)
This method sets the ICMPv6 message type.
Definition: icmp6.hpp:108
Type GetType(void) const
This method returns the ICMPv6 message type.
Definition: icmp6.hpp:100
This file includes definitions for locator class for OpenThread objects.
bool IsEchoEnabled(void)
This method indicates whether or not ICMPv6 Echo processing is enabled.
Definition: icmp6.hpp:318
Type
ICMPv6 Message Types.
Definition: icmp6.hpp:78
This class implements ICMPv6.
Definition: icmp6.hpp:225
This class implements message information for an IPv6 message.
Definition: socket.hpp:57
uint16_t GetSequence(void) const
This method returns the ICMPv6 message sequence for Echo Requests and Replies.
Definition: icmp6.hpp:163
static uint8_t GetDataOffset(void)
This static method returns the byte offset of the ICMPv6 payload.
Definition: icmp6.hpp:187
Echo Request.
Definition: icmp6.hpp:81
#define OT_TOOL_PACKED_BEGIN
Compiler-specific indication that a class or struct must be byte packed.
Definition: toolchain.h:170
uint8_t mType
Type.
Definition: icmp6.h:86
void Init(void)
This method initializes the ICMPv6 header to all zeros.
Definition: icmp6.hpp:72
uint16_t GetId(void) const
This method returns the ICMPv6 message ID for Echo Requests and Replies.
Definition: icmp6.hpp:147
void SetEchoEnabled(bool aEnabled)
This method sets whether or not ICMPv6 Echo processing is enabled.
Definition: icmp6.hpp:326
void SetSequence(uint16_t aSequence)
This method sets the ICMPv6 message sequence for Echo Requests and Replies.
Definition: icmp6.hpp:171
This file defines the top-level icmp6 functions for the OpenThread library.
Destination Unreachable.
Definition: icmp6.hpp:80
This class represents a message.
Definition: message.hpp:195
Echo Reply.
Definition: icmp6.hpp:82
Code GetCode(void) const
This method returns the ICMPv6 message code.
Definition: icmp6.hpp:116
This class implements ICMPv6 message handlers.
Definition: icmp6.hpp:195
This class implements locator for otInstance object.
Definition: locator.hpp:63
This file includes definitions for byte-ordering encoding.
static uint8_t GetChecksumOffset(void)
This static method returns the byte offset of the Checksum field in the ICMPv6 header.
Definition: icmp6.hpp:179
uint16_t mChecksum
Checksum.
Definition: icmp6.h:88
This structure implements ICMPv6 message handler.
Definition: icmp6.h:119
uint8_t mCode
Code.
Definition: icmp6.h:87
#define OT_TOOL_PACKED_END
Compiler-specific indication at the end of a byte packed class or struct.
Definition: toolchain.h:172
Destination Unreachable No Route.
Definition: icmp6.h:72
This structure represents an ICMPv6 header.
Definition: icmp6.h:84
void SetId(uint16_t aId)
This method sets the ICMPv6 message ID for Echo Requests and Replies.
Definition: icmp6.hpp:155
void SetChecksum(uint16_t aChecksum)
This method sets the ICMPv6 message checksum.
Definition: icmp6.hpp:139
Destination Unreachable.
Definition: icmp6.h:61
Echo Reply.
Definition: icmp6.h:63
This file includes definitions for IPv6 packet processing.
Destination Unreachable No Route.
Definition: icmp6.hpp:91
Code
ICMPv6 Message Codes.
Definition: icmp6.hpp:89
union otIcmp6Header::OT_TOOL_PACKED_FIELD mData
Message-specific data.
IcmpHandler(otIcmp6ReceiveCallback aCallback, void *aContext)
This constructor creates an ICMPv6 message handler.
Definition: icmp6.hpp:207
otError
This enumeration represents error codes used throughout OpenThread.
Definition: types.h:107
This file includes compile-time configuration constants for OpenThread.
Definition: icmp6.hpp:65
void(* otIcmp6ReceiveCallback)(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, const otIcmp6Header *aIcmpHeader)
This callback allows OpenThread to inform the application of a received ICMPv6 message.
Definition: icmp6.h:112