Skip to content

Commit d8a9600

Browse files
liqinhuixm13627105546
authored andcommitted
ping4:Set the filter for reply packet.
VELAPLATFO-63377 Change-Id: I23f51047698555afca11bb1e1b5de8304b43800b Signed-off-by: liqinhui <[email protected]>
1 parent b0b90ac commit d8a9600

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-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: 13 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) (UINT32_MAX - (1U << (t)))
5657

5758
/****************************************************************************
5859
* Private Types
@@ -197,6 +198,7 @@ void icmp_ping(FAR const struct ping_info_s *info)
197198
int ret;
198199
int ch;
199200
int i;
201+
int filter;
200202

201203
g_exiting = false;
202204
signal(SIGINT, sigexit);
@@ -246,6 +248,17 @@ void icmp_ping(FAR const struct ping_info_s *info)
246248
}
247249
#endif
248250

251+
filter = ICMP_SET_FILTER(ICMP_ECHO_REPLY);
252+
ret = setsockopt(priv->sockfd, IPPROTO_ICMP, ICMP_FILTER,
253+
&filter, sizeof(filter));
254+
if (ret < 0)
255+
{
256+
icmp_callback(&result, ICMP_E_FILTER, errno);
257+
close(priv->sockfd);
258+
free(priv);
259+
return;
260+
}
261+
249262
priv->kickoff = clock();
250263

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

0 commit comments

Comments
 (0)