Skip to content

Commit 9d80355

Browse files
liqinhuixm13627105546
authored andcommitted
netutils/ping: Support ICMP filter for ping.
Add setsockopt support to filter ICMP packets. Signed-off-by: liqinhui <[email protected]>
1 parent b0b90ac commit 9d80355

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

include/netutils/icmp_ping.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#define ICMP_E_RECVFROM -13 /* extra: error code */
5252
#define ICMP_E_RECVSMALL -15 /* extra: recv bytes */
5353
#define ICMP_E_BINDDEV -17 /* extra: error bind */
54+
#define ICMP_E_FILTER -19 /* extra: error filter */
5455

5556
/* Negative even number represent warning(recoverable) */
5657

netutils/ping/icmp_ping.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
****************************************************************************/
5454

5555
#define ICMP_IOBUFFER_SIZE(x) (sizeof(struct icmp_hdr_s) + (x))
56+
#define ICMP_SET_FILTER(t) (~(1U << (t)))
5657

5758
/****************************************************************************
5859
* Private Types
@@ -197,6 +198,9 @@ void icmp_ping(FAR const struct ping_info_s *info)
197198
int ret;
198199
int ch;
199200
int i;
201+
#ifdef CONFIG_NET_SOCKOPTS
202+
int filter;
203+
#endif
200204

201205
g_exiting = false;
202206
signal(SIGINT, sigexit);
@@ -246,6 +250,19 @@ void icmp_ping(FAR const struct ping_info_s *info)
246250
}
247251
#endif
248252

253+
#ifdef CONFIG_NET_SOCKOPTS
254+
filter = ICMP_SET_FILTER(ICMP_ECHO_REPLY);
255+
ret = setsockopt(priv->sockfd, IPPROTO_ICMP, ICMP_FILTER,
256+
&filter, sizeof(filter));
257+
if (ret < 0)
258+
{
259+
icmp_callback(&result, ICMP_E_FILTER, errno);
260+
close(priv->sockfd);
261+
free(priv);
262+
return;
263+
}
264+
#endif
265+
249266
priv->kickoff = clock();
250267

251268
memset(&priv->destaddr, 0, sizeof(struct sockaddr_in));

0 commit comments

Comments
 (0)