This is an automated email from the ASF dual-hosted git repository.

jiuzhudong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 45a6b0365 netutils/ping: Support ICMP filter for ping.
45a6b0365 is described below

commit 45a6b0365c743574648575ba4a28d2c59ca3e884
Author: liqinhui <[email protected]>
AuthorDate: Thu May 15 14:46:47 2025 +0800

    netutils/ping: Support ICMP filter for ping.
    
    Add setsockopt support to filter ICMP packets.
    
    Signed-off-by: liqinhui <[email protected]>
---
 include/netutils/icmp_ping.h |  1 +
 netutils/ping/icmp_ping.c    | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/include/netutils/icmp_ping.h b/include/netutils/icmp_ping.h
index 3f8d6c29d..d9164afc1 100644
--- a/include/netutils/icmp_ping.h
+++ b/include/netutils/icmp_ping.h
@@ -51,6 +51,7 @@
 #define ICMP_E_RECVFROM    -13 /* extra: error code    */
 #define ICMP_E_RECVSMALL   -15 /* extra: recv bytes    */
 #define ICMP_E_BINDDEV     -17 /* extra: error bind    */
+#define ICMP_E_FILTER      -19 /* extra: error filter  */
 
 /* Negative even number represent warning(recoverable) */
 
diff --git a/netutils/ping/icmp_ping.c b/netutils/ping/icmp_ping.c
index aa88d0a18..88f006b8f 100644
--- a/netutils/ping/icmp_ping.c
+++ b/netutils/ping/icmp_ping.c
@@ -54,6 +54,7 @@
  ****************************************************************************/
 
 #define ICMP_IOBUFFER_SIZE(x) (sizeof(struct icmp_hdr_s) + (x))
+#define ICMP_SET_FILTER(t) (~(1U << (t)))
 
 /****************************************************************************
  * Private Types
@@ -226,6 +227,9 @@ void icmp_ping(FAR const struct ping_info_s *info)
   int ret;
   int ch;
   int i;
+#ifdef CONFIG_NET_SOCKOPTS
+  int filter;
+#endif
 
   g_exiting = false;
 #ifdef CONFIG_ENABLE_ALL_SIGNALS
@@ -278,6 +282,19 @@ void icmp_ping(FAR const struct ping_info_s *info)
     }
 #endif
 
+#ifdef CONFIG_NET_SOCKOPTS
+  filter = ICMP_SET_FILTER(ICMP_ECHO_REPLY);
+  ret = setsockopt(priv->sockfd, IPPROTO_ICMP, ICMP_FILTER,
+                   &filter, sizeof(filter));
+  if (ret < 0)
+    {
+      icmp_callback(&result, ICMP_E_FILTER, errno);
+      close(priv->sockfd);
+      free(priv);
+      return;
+    }
+#endif
+
   priv->kickoff = clock();
 
   memset(&priv->destaddr, 0, sizeof(struct sockaddr_in));

Reply via email to