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-2024 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 2.4.0
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 {
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 //CC-RX, CodeWarrior or Win32 compiler?
96 #if defined(__CCRX__)
97  #pragma pack
98 #elif defined(__CWCC__) || defined(_WIN32)
99  #pragma pack(push, 1)
100 #endif
101 
102 
103 /**
104  * @brief ICMP header
105  **/
106 
108 {
109  uint8_t type; //0
110  uint8_t code; //1
111  uint16_t checksum; //2-3
112  uint8_t data[]; //4
114 
115 
116 /**
117  * @brief ICMP Error message
118  **/
119 
120 typedef __packed_struct
121 {
122  uint8_t type; //0
123  uint8_t code; //1
124  uint16_t checksum; //2-3
125  uint8_t parameter; //4
126  uint8_t unused[3]; //5-7
127  uint8_t data[]; //8
129 
130 
131 /**
132  * @brief ICMP Destination Unreachable message
133  **/
134 
135 typedef __packed_struct
136 {
137  uint8_t type; //0
138  uint8_t code; //1
139  uint16_t checksum; //2-3
140  uint32_t unused; //4-7
141  uint8_t data[]; //8
143 
144 
145 /**
146  * @brief ICMP Time Exceeded message
147  **/
148 
149 typedef __packed_struct
150 {
151  uint8_t type; //0
152  uint8_t code; //1
153  uint16_t checksum; //2-3
154  uint32_t unused; //4-7
155  uint8_t data[]; //8
157 
158 
159 /**
160  * @brief ICMP Parameter Problem message
161  **/
162 
163 typedef __packed_struct
164 {
165  uint8_t type; //0
166  uint8_t code; //1
167  uint16_t checksum; //2-3
168  uint8_t pointer; //4
169  uint8_t unused[3]; //5-7
170  uint8_t data[]; //8
172 
173 
174 /**
175  * @brief ICMP Echo Request and Echo Reply messages
176  **/
177 
178 typedef __packed_struct
179 {
180  uint8_t type; //0
181  uint8_t code; //1
182  uint16_t checksum; //2-3
183  uint16_t identifier; //4-5
184  uint16_t sequenceNumber; //6-7
185  uint8_t data[]; //8
187 
188 
189 //CC-RX, CodeWarrior or Win32 compiler?
190 #if defined(__CCRX__)
191  #pragma unpack
192 #elif defined(__CWCC__) || defined(_WIN32)
193  #pragma pack(pop)
194 #endif
195 
196 //ICMP related functions
198 
200  bool_t enable);
201 
202 void icmpProcessMessage(NetInterface *interface,
203  const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer,
204  size_t offset);
205 
206 void icmpProcessEchoRequest(NetInterface *interface,
207  const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *request,
208  size_t requestOffset);
209 
210 error_t icmpSendErrorMessage(NetInterface *interface, uint8_t type,
211  uint8_t code, uint8_t parameter, const NetBuffer *ipPacket,
212  size_t ipPacketOffset);
213 
214 void icmpUpdateInStats(uint8_t type);
215 void icmpUpdateOutStats(uint8_t type);
216 
217 void icmpDumpMessage(const IcmpHeader *message);
220 
221 //C++ guard
222 #ifdef __cplusplus
223 }
224 #endif
225 
226 #endif
uint8_t message[]
Definition: chap.h:154
uint8_t type
Definition: coap_common.h:176
int bool_t
Definition: compiler_port.h:53
error_t
Error codes.
Definition: error.h:43
uint8_t pointer
Definition: icmp.h:168
IcmpType
ICMP message type.
Definition: icmp.h:52
@ ICMP_TYPE_TIMESTAMP_REPLY
Definition: icmp.h:61
@ ICMP_TYPE_INFO_REQUEST
Definition: icmp.h:62
@ ICMP_TYPE_TIMESTAMP_REQUEST
Definition: icmp.h:60
@ ICMP_TYPE_PARAM_PROBLEM
Definition: icmp.h:59
@ ICMP_TYPE_ECHO_REPLY
Definition: icmp.h:53
@ ICMP_TYPE_ECHO_REQUEST
Definition: icmp.h:57
@ ICMP_TYPE_INFO_REPLY
Definition: icmp.h:63
@ ICMP_TYPE_REDIRECT
Definition: icmp.h:56
@ ICMP_TYPE_TIME_EXCEEDED
Definition: icmp.h:58
@ ICMP_TYPE_ADDR_MASK_REQUEST
Definition: icmp.h:64
@ ICMP_TYPE_ADDR_MASK_REPLY
Definition: icmp.h:65
@ ICMP_TYPE_DEST_UNREACHABLE
Definition: icmp.h:54
@ ICMP_TYPE_SOURCE_QUENCH
Definition: icmp.h:55
void icmpDumpMessage(const IcmpHeader *message)
Dump ICMP message for debugging purpose.
Definition: icmp.c:595
void icmpUpdateInStats(uint8_t type)
Update ICMP input statistics.
Definition: icmp.c:441
IcmpErrorMessage
Definition: icmp.h:128
IcmpTimeExceededCode
Time Exceeded message codes.
Definition: icmp.h:89
@ ICMP_CODE_TTL_EXCEEDED
Definition: icmp.h:90
@ ICMP_CODE_REASSEMBLY_TIME_EXCEEDED
Definition: icmp.h:91
uint16_t sequenceNumber
Definition: icmp.h:184
IcmpHeader
Definition: icmp.h:113
uint8_t parameter
Definition: icmp.h:125
void icmpProcessEchoRequest(NetInterface *interface, const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *request, size_t requestOffset)
Echo Request message processing.
Definition: icmp.c:193
uint16_t identifier
Definition: icmp.h:183
uint8_t data[]
Definition: icmp.h:112
void icmpDumpErrorMessage(const IcmpErrorMessage *message)
Dump generic ICMP Error message.
Definition: icmp.c:625
uint16_t checksum
Definition: icmp.h:111
void icmpDumpEchoMessage(const IcmpEchoMessage *message)
Dump ICMP Echo Request or Echo Reply message.
Definition: icmp.c:609
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:338
uint8_t code
Definition: icmp.h:110
IcmpEchoMessage
Definition: icmp.h:186
uint8_t unused[3]
Definition: icmp.h:126
void icmpProcessMessage(NetInterface *interface, const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer, size_t offset)
Incoming ICMP message processing.
Definition: icmp.c:111
IcmpDestUnreachableCode
Destination Unreachable message codes.
Definition: icmp.h:74
@ ICMP_CODE_HOST_UNREACHABLE
Definition: icmp.h:76
@ ICMP_CODE_NET_UNREACHABLE
Definition: icmp.h:75
@ ICMP_CODE_PROTOCOL_UNREACHABLE
Definition: icmp.h:77
@ ICMP_CODE_SOURCE_ROUTE_FAILED
Definition: icmp.h:80
@ ICMP_CODE_PORT_UNREACHABLE
Definition: icmp.h:78
@ ICMP_CODE_FRAG_NEEDED_AND_DF_SET
Definition: icmp.h:79
typedef __packed_struct
ICMP header.
Definition: icmp.h:108
IcmpTimeExceededMessage
Definition: icmp.h:156
IcmpDestUnreachableMessage
Definition: icmp.h:142
IcmpParamProblemMessage
Definition: icmp.h:171
error_t icmpEnableEchoRequests(NetInterface *interface, bool_t enable)
Enable support for ICMP Echo Request messages.
Definition: icmp.c:57
error_t icmpEnableBroadcastEchoRequests(NetInterface *interface, bool_t enable)
Enable support for broadcast ICMP Echo Request messages.
Definition: icmp.c:84
void icmpUpdateOutStats(uint8_t type)
Update ICMP output statistics.
Definition: icmp.c:516
#define Ipv4PseudoHeader
Definition: ipv4.h:39
uint8_t ipPacket[]
Definition: ndp.h:431
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89