netutils/ping: Support ICMP filter for ping.

Add setsockopt support to filter ICMP packets.

Signed-off-by: liqinhui <liqinhui@xiaomi.com>
This commit is contained in:
liqinhui 2025-05-15 14:46:47 +08:00 committed by Donny(董九柱)
parent 056ba984e8
commit 45a6b0365c
2 changed files with 18 additions and 0 deletions

View file

@ -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));