Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1ddadbd

Browse filesBrowse files
committed
patches: add ICMPSocket ping
1 parent 4cd3c25 commit 1ddadbd
Copy full SHA for 1ddadbd

File tree

1 file changed

+106
-0
lines changed
Filter options

1 file changed

+106
-0
lines changed
+106Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
From 933694e0f35451d21eed77a93fa346570de20878 Mon Sep 17 00:00:00 2001
2+
From: pennam <m.pennasilico@arduino.cc>
3+
Date: Tue, 4 Feb 2025 14:31:59 +0100
4+
Subject: [PATCH] ICMPSocket: add ping
5+
6+
---
7+
.../netsocket/include/netsocket/ICMPSocket.h | 4 ++
8+
connectivity/netsocket/source/ICMPSocket.cpp | 61 +++++++++++++++++++
9+
2 files changed, 65 insertions(+)
10+
11+
diff --git a/connectivity/netsocket/include/netsocket/ICMPSocket.h b/connectivity/netsocket/include/netsocket/ICMPSocket.h
12+
index 1837bc8e09..5e1ee8fb03 100644
13+
--- a/connectivity/netsocket/include/netsocket/ICMPSocket.h
14+
+++ b/connectivity/netsocket/include/netsocket/ICMPSocket.h
15+
@@ -37,6 +37,10 @@ public:
16+
*/
17+
ICMPSocket();
18+
19+
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED
20+
+ int ping(SocketAddress &socketAddress, uint32_t timeout);
21+
+#endif
22+
+
23+
#if !defined(DOXYGEN_ONLY)
24+
25+
protected:
26+
diff --git a/connectivity/netsocket/source/ICMPSocket.cpp b/connectivity/netsocket/source/ICMPSocket.cpp
27+
index f6c9b98de1..d8ea954835 100644
28+
--- a/connectivity/netsocket/source/ICMPSocket.cpp
29+
+++ b/connectivity/netsocket/source/ICMPSocket.cpp
30+
@@ -16,12 +16,73 @@
31+
*/
32+
33+
#include "ICMPSocket.h"
34+
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED
35+
+#include "drivers/Timer.h"
36+
+#include "lwip/prot/icmp.h"
37+
+#include "lwip/inet_chksum.h"
38+
+#include "lwip/prot/ip4.h"
39+
+#endif
40+
41+
ICMPSocket::ICMPSocket()
42+
{
43+
_socket_stats.stats_update_proto(this, NSAPI_ICMP);
44+
}
45+
46+
+#if MBED_CONF_LWIP_RAW_SOCKET_ENABLED
47+
+int ICMPSocket::ping(SocketAddress &socketAddress, uint32_t timeout)
48+
+{
49+
+ struct __attribute__((__packed__)) {
50+
+ struct icmp_echo_hdr header;
51+
+ uint8_t data[32];
52+
+ } request;
53+
+
54+
+ ICMPH_TYPE_SET(&request.header, ICMP_ECHO);
55+
+ ICMPH_CODE_SET(&request.header, 0);
56+
+ request.header.chksum = 0;
57+
+ request.header.id = 0xAFAF;
58+
+ request.header.seqno = random();
59+
+
60+
+ for (size_t i = 0; i < sizeof(request.data); i++) {
61+
+ request.data[i] = i;
62+
+ }
63+
+
64+
+ request.header.chksum = inet_chksum(&request, sizeof(request));
65+
+
66+
+ int res = sendto(socketAddress, &request, sizeof(request));
67+
+ if (res <= 0){
68+
+ return -1;
69+
+ }
70+
+
71+
+ mbed::Timer timer;
72+
+ timer.start();
73+
+ int elapsed = -1;
74+
+ do {
75+
+ struct __attribute__((__packed__)) {
76+
+ struct ip_hdr ipHeader;
77+
+ struct icmp_echo_hdr header;
78+
+ } response;
79+
+
80+
+ int rxSize = recvfrom(&socketAddress, &response, sizeof(response));
81+
+ if (rxSize < 0) {
82+
+ // time out
83+
+ break;
84+
+ }
85+
+
86+
+ if (rxSize < sizeof(response)) {
87+
+ // too short
88+
+ continue;
89+
+ }
90+
+
91+
+ if ((response.header.id == request.header.id) && (response.header.seqno == request.header.seqno)) {
92+
+ elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time()).count();
93+
+ timer.stop();
94+
+ }
95+
+ } while (elapsed == -1 && std::chrono::duration_cast<std::chrono::milliseconds>(timer.elapsed_time()).count() < timeout);
96+
+
97+
+ return elapsed;
98+
+}
99+
+#endif
100+
+
101+
nsapi_protocol_t ICMPSocket::get_proto()
102+
{
103+
return NSAPI_ICMP;
104+
--
105+
2.47.2
106+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.