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 9c85568c4 icmpv6_ping.c: change socket type from SOCK_DGRAM to 
SOCK_RAW.
9c85568c4 is described below

commit 9c85568c4f0f2c585401302f1fb36785c4a26a10
Author: zhanghongyu <[email protected]>
AuthorDate: Thu Jul 3 15:31:33 2025 +0800

    icmpv6_ping.c: change socket type from SOCK_DGRAM to SOCK_RAW.
    
    standardize the implementation of ping6 to better comply with socket
    permissions on certain systems.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 netutils/ping/icmpv6_ping.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/netutils/ping/icmpv6_ping.c b/netutils/ping/icmpv6_ping.c
index e6a93fc98..c62c329d6 100644
--- a/netutils/ping/icmpv6_ping.c
+++ b/netutils/ping/icmpv6_ping.c
@@ -42,6 +42,7 @@
 #endif
 
 #include <arpa/inet.h>
+#include <netinet/icmp6.h>
 
 #include <nuttx/clock.h>
 #include <nuttx/net/icmpv6.h>
@@ -169,6 +170,7 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
   struct ping6_result_s result;
   struct sockaddr_in6 destaddr;
   struct sockaddr_in6 fromaddr;
+  struct icmp6_filter filter;
   struct icmpv6_echo_request_s outhdr;
   FAR struct icmpv6_echo_reply_s *inhdr;
   struct pollfd recvfd;
@@ -210,7 +212,7 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
       return;
     }
 
-  sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMP6);
+  sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
   if (sockfd < 0)
     {
       icmp6_callback(&result, ICMPv6_E_SOCKET, errno);
@@ -232,6 +234,20 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
     }
 #endif
 
+  memset(&filter, 0xff, sizeof(filter));
+
+  /* ICMPv6_ECHO_REPLY >> 5 = 4 */
+
+  filter.icmp6_filt[4] &= ~(1 << (ICMPv6_ECHO_REPLY & 31));
+  if (setsockopt(sockfd, SOL_ICMPV6, ICMP6_FILTER, &filter,
+                 sizeof(filter)) < 0)
+    {
+      icmp6_callback(&result, ICMPv6_E_SOCKET, errno);
+      close(sockfd);
+      free(iobuffer);
+      return;
+    }
+
   kickoff = clock();
 
   memset(&destaddr, 0, sizeof(struct sockaddr_in6));

Reply via email to