ping.h
Go to the documentation of this file.
1 /**
2  * @file ping.h
3  * @brief Ping utility
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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.6.0
29  **/
30 
31 #ifndef _PING_H
32 #define _PING_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ipv4/icmp.h"
37 #include "ipv6/icmpv6.h"
38 
39 //Ping utility support
40 #ifndef PING_SUPPORT
41  #define PING_SUPPORT ENABLED
42 #elif (PING_SUPPORT != ENABLED && PING_SUPPORT != DISABLED)
43  #error PING_SUPPORT parameter is not valid
44 #endif
45 
46 //Default timeout value
47 #ifndef PING_DEFAULT_TIMEOUT
48  #define PING_DEFAULT_TIMEOUT 1000
49 #elif (PING_DEFAULT_TIMEOUT < 0)
50  #error PING_DEFAULT_TIMEOUT parameter is not valid
51 #endif
52 
53 //Maximum size of the data payload
54 #ifndef PING_MAX_DATA_SIZE
55  #define PING_MAX_DATA_SIZE 32
56 #elif (PING_MAX_DATA_SIZE < 0)
57  #error PING_MAX_DATA_SIZE parameter is not valid
58 #endif
59 
60 //Size of the internal buffer
61 #define PING_BUFFER_SIZE (sizeof(IcmpEchoMessage) + PING_MAX_DATA_SIZE)
62 
63 //C++ guard
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 
69 /**
70  * @brief Ping context
71  **/
72 
73 typedef struct
74 {
75  NetContext *netContext; ///<TCP/IP stack context
76  NetInterface *interface; ///<Underlying network interface
77  Socket *socket; ///<Raw socket
79  uint16_t identifier;
80  uint16_t sequenceNumber;
84  uint8_t buffer[PING_BUFFER_SIZE];
85 } PingContext;
86 
87 
88 //Ping related functions
89 error_t ping(NetInterface *interface, const IpAddr *targetIpAddr, size_t size,
90  uint8_t ttl, systime_t timeout, systime_t *rtt);
91 
92 void pingInit(PingContext *context);
93 error_t pingSetTimeout(PingContext *context, systime_t timeout);
95 
96 error_t pingSendRequest(PingContext *context, const IpAddr *targetIpAddr,
97  size_t size, uint8_t ttl);
98 
99 error_t pingWaitForReply(PingContext *context, IpAddr *hostIpAddr,
100  systime_t *rtt);
101 
102 void pingRelease(PingContext *context);
103 
104 //C++ guard
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif
#define NetContext
Definition: net.h:36
error_t pingSendRequest(PingContext *context, const IpAddr *targetIpAddr, size_t size, uint8_t ttl)
Send an ICMP Echo Request message.
Definition: ping.c:193
error_t ping(NetInterface *interface, const IpAddr *targetIpAddr, size_t size, uint8_t ttl, systime_t timeout, systime_t *rtt)
Test the reachability of a host.
Definition: ping.c:69
IP network address.
Definition: ip.h:90
systime_t timeout
Definition: ping.h:82
#define PING_BUFFER_SIZE
Definition: ping.h:61
NetContext * netContext
TCP/IP stack context.
Definition: ping.h:75
Socket * socket
Raw socket.
Definition: ping.h:77
Ping context.
Definition: ping.h:74
systime_t timestamp
Definition: ping.h:81
ICMPv6 (Internet Control Message Protocol Version 6)
ICMP (Internet Control Message Protocol)
error_t
Error codes.
Definition: error.h:43
uint16_t sequenceNumber
Definition: ping.h:80
error_t pingBindToInterface(PingContext *context, NetInterface *interface)
Select a particular network interface.
Definition: ping.c:170
#define NetInterface
Definition: net.h:40
void pingInit(PingContext *context)
Initialize ping context.
Definition: ping.c:125
uint32_t systime_t
System time.
#define Socket
Definition: socket.h:36
error_t pingWaitForReply(PingContext *context, IpAddr *hostIpAddr, systime_t *rtt)
Wait for a matching ICMP Echo Reply message.
Definition: ping.c:496
NetInterface * interface
Underlying network interface.
Definition: ping.h:76
void pingRelease(PingContext *context)
Release ping context.
Definition: ping.c:608
error_t pingSetTimeout(PingContext *context, systime_t timeout)
Set timeout value.
Definition: ping.c:149
uint32_t ttl
Definition: dns_common.h:224
systime_t rtt
Definition: ping.h:83
TCP/IP stack core.
uint16_t identifier
Definition: ping.h:79
size_t dataPayloadSize
Definition: ping.h:78