icmp.h
Go to the documentation of this file.
1 /**
2  * @file icmp.h
3  * @brief ICMP (Internet Control Message Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2020 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.8
29  **/
30 
31 #ifndef _ICMP_H
32 #define _ICMP_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //C++ guard
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 
43 /**
44  * @brief ICMP message type
45  *
46  * The type field indicates the type of the message. Its
47  * value determines the format of the remaining data
48  *
49  **/
50 
51 typedef enum
52 {
66 } IcmpType;
67 
68 
69 /**
70  * @brief Destination Unreachable message codes
71  **/
72 
73 typedef enum
74 {
82 
83 
84 /**
85  * @brief Time Exceeded message codes
86  **/
87 
88 typedef enum
89 {
93 
94 
95 //CodeWarrior or Win32 compiler?
96 #if defined(__CWCC__) || defined(_WIN32)
97  #pragma pack(push, 1)
98 #endif
99 
100 
101 /**
102  * @brief ICMP header
103  **/
104 
105 typedef __start_packed struct
106 {
107  uint8_t type; //0
108  uint8_t code; //1
109  uint16_t checksum; //2-3
110  uint8_t data[]; //4
112 
113 
114 /**
115  * @brief ICMP Error message
116  **/
117 
118 typedef __start_packed struct
119 {
120  uint8_t type; //0
121  uint8_t code; //1
122  uint16_t checksum; //2-3
123  uint32_t parameter : 8; //4
124  uint32_t unused : 24; //5-7
125  uint8_t data[]; //8
127 
128 
129 /**
130  * @brief ICMP Destination Unreachable message
131  **/
132 
133 typedef __start_packed struct
134 {
135  uint8_t type; //0
136  uint8_t code; //1
137  uint16_t checksum; //2-3
138  uint32_t unused; //4-7
139  uint8_t data[]; //8
141 
142 
143 /**
144  * @brief ICMP Time Exceeded message
145  **/
146 
147 typedef __start_packed struct
148 {
149  uint8_t type; //0
150  uint8_t code; //1
151  uint16_t checksum; //2-3
152  uint32_t unused; //4-7
153  uint8_t data[]; //8
155 
156 
157 /**
158  * @brief ICMP Parameter Problem message
159  **/
160 
161 typedef __start_packed struct
162 {
163  uint8_t type; //0
164  uint8_t code; //1
165  uint16_t checksum; //2-3
166  uint32_t pointer : 8; //4
167  uint32_t unused : 24; //5-7
168  uint8_t data[]; //8
170 
171 
172 /**
173  * @brief ICMP Echo Request and Echo Reply messages
174  **/
175 
176 typedef __start_packed struct
177 {
178  uint8_t type; //0
179  uint8_t code; //1
180  uint16_t checksum; //2-3
181  uint16_t identifier; //4-5
182  uint16_t sequenceNumber; //6-7
183  uint8_t data[]; //8
185 
186 
187 //CodeWarrior or Win32 compiler?
188 #if defined(__CWCC__) || defined(_WIN32)
189  #pragma pack(pop)
190 #endif
191 
192 
193 //ICMP related functions
194 void icmpProcessMessage(NetInterface *interface,
195  Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer,
196  size_t offset);
197 
198 void icmpProcessEchoRequest(NetInterface *interface,
199  Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *request,
200  size_t requestOffset);
201 
202 error_t icmpSendErrorMessage(NetInterface *interface, uint8_t type, uint8_t code,
203  uint8_t parameter, const NetBuffer *ipPacket, size_t ipPacketOffset);
204 
205 void icmpUpdateInStats(uint8_t type);
206 void icmpUpdateOutStats(uint8_t type);
207 
208 void icmpDumpMessage(const IcmpHeader *message);
211 
212 //C++ guard
213 #ifdef __cplusplus
214 }
215 #endif
216 
217 #endif
uint16_t checksum
Definition: icmp.h:109
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
__start_packed struct @65 IcmpParamProblemMessage
ICMP Parameter Problem message.
uint32_t unused
Definition: icmp.h:124
IcmpDestUnreachableCode
Destination Unreachable message codes.
Definition: icmp.h:73
__start_packed struct @64 IcmpTimeExceededMessage
ICMP Time Exceeded message.
uint8_t ipPacket[]
Definition: ndp.h:430
uint32_t parameter
Definition: icmp.h:123
__start_packed struct @62 IcmpErrorMessage
ICMP Error message.
void icmpProcessMessage(NetInterface *interface, Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer, size_t offset)
Incoming ICMP message processing.
Definition: icmp.c:57
error_t
Error codes.
Definition: error.h:42
error_t icmpSendErrorMessage(NetInterface *interface, uint8_t type, uint8_t code, uint8_t parameter, const NetBuffer *ipPacket, size_t ipPacketOffset)
Send an ICMP Error message.
Definition: icmp.c:269
#define NetInterface
Definition: net.h:36
void icmpUpdateInStats(uint8_t type)
Update ICMP input statistics.
Definition: icmp.c:369
__start_packed struct _Ipv4Header __end_packed
__start_packed struct @66 IcmpEchoMessage
ICMP Echo Request and Echo Reply messages.
void icmpUpdateOutStats(uint8_t type)
Update ICMP output statistics.
Definition: icmp.c:433
#define Ipv4PseudoHeader
Definition: ipv4.h:39
void icmpDumpEchoMessage(const IcmpEchoMessage *message)
Dump ICMP Echo Request or Echo Reply message.
Definition: icmp.c:515
IcmpTimeExceededCode
Time Exceeded message codes.
Definition: icmp.h:88
uint16_t identifier
Definition: icmp.h:181
uint8_t code
Definition: icmp.h:108
uint32_t pointer
Definition: icmp.h:166
void icmpDumpMessage(const IcmpHeader *message)
Dump ICMP message for debugging purpose.
Definition: icmp.c:501
uint8_t type
Definition: icmp.h:107
uint8_t message[]
Definition: chap.h:152
__start_packed struct @61 IcmpHeader
ICMP header.
void icmpProcessEchoRequest(NetInterface *interface, Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *request, size_t requestOffset)
Echo Request message processing.
Definition: icmp.c:138
void icmpDumpErrorMessage(const IcmpErrorMessage *message)
Dump generic ICMP Error message.
Definition: icmp.c:531
TCP/IP stack core.
uint8_t data[]
Definition: icmp.h:110
IcmpType
ICMP message type.
Definition: icmp.h:51
uint16_t sequenceNumber
Definition: icmp.h:182
__start_packed struct @63 IcmpDestUnreachableMessage
ICMP Destination Unreachable message.