Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/netutils/icmp_ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#define ICMP_E_POLL -11 /* extra: error code */
#define ICMP_E_RECVFROM -13 /* extra: error code */
#define ICMP_E_RECVSMALL -15 /* extra: recv bytes */
#define ICMP_E_BINDDEV -17 /* extra: error bind */

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

Expand All @@ -70,6 +71,9 @@ struct ping_result_s;
struct ping_info_s
{
FAR const char *hostname; /* Host name to ping */
#ifdef CONFIG_NET_BINDTODEVICE
FAR const char *devname; /* Device name to bind */
#endif
uint16_t count; /* Number of pings requested */
uint16_t datalen; /* Number of bytes to be sent */
uint16_t delay; /* Deciseconds to delay between pings */
Expand Down
4 changes: 4 additions & 0 deletions include/netutils/icmpv6_ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#define ICMPv6_E_POLL -11 /* extra: error code */
#define ICMPv6_E_RECVFROM -13 /* extra: error code */
#define ICMPv6_E_RECVSMALL -15 /* extra: recv bytes */
#define ICMPV6_E_BINDDEV -17 /* extra: error bind */

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

Expand All @@ -70,6 +71,9 @@ struct ping6_result_s;
struct ping6_info_s
{
FAR const char *hostname; /* Host name to ping */
#ifdef CONFIG_NET_BINDTODEVICE
FAR const char *devname; /* Device name to bind */
#endif
uint16_t count; /* Number of pings requested */
uint16_t datalen; /* Number of bytes to be sent */
uint16_t delay; /* Deciseconds to delay between pings */
Expand Down
14 changes: 14 additions & 0 deletions netutils/ping/icmp_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@ void icmp_ping(FAR const struct ping_info_s *info)
return;
}

#ifdef CONFIG_NET_BINDTODEVICE
if (info->devname)
{
ret = setsockopt(priv->sockfd, SOL_SOCKET, SO_BINDTODEVICE,
info->devname, strlen(info->devname));
if (ret < 0)
{
icmp_callback(&result, ICMP_E_BINDDEV, errno);
free(priv);
return;
}
}
#endif

priv->kickoff = clock();

memset(&priv->destaddr, 0, sizeof(struct sockaddr_in));
Expand Down
14 changes: 14 additions & 0 deletions netutils/ping/icmpv6_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
return;
}

#ifdef CONFIG_NET_BINDTODEVICE
if (info->devname)
{
ret = setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
info->devname, strlen(info->devname));
if (ret < 0)
{
icmp6_callback(&result, ICMPV6_E_BINDDEV, errno);
free(iobuffer);
return;
}
}
#endif

kickoff = clock();

memset(&destaddr, 0, sizeof(struct sockaddr_in6));
Expand Down
20 changes: 19 additions & 1 deletion system/ping/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static void show_usage(FAR const char *progname, int exitcode)
printf(" -s <size> specifies the number of data bytes to be sent. "
"Default %u.\n",
ICMP_PING_DATALEN);
printf(" -I <interface> is the bind device for traffic\n");
printf(" -h shows this text and exits.\n");
exit(exitcode);
}
Expand Down Expand Up @@ -172,6 +173,12 @@ static void ping_result(FAR const struct ping_result_s *result)
fprintf(stderr, "ERROR: short ICMP packet: %ld\n", result->extra);
break;

#ifdef CONFIG_NET_BINDTODEVICE
case ICMP_E_BINDDEV:
fprintf(stderr, "ERROR: setsockopt error: %ld\n", result->extra);
break;
#endif

case ICMP_W_IDDIFF:
fprintf(stderr,
"WARNING: Ignoring ICMP reply with ID %ld. "
Expand Down Expand Up @@ -289,6 +296,9 @@ int main(int argc, FAR char *argv[])
info.delay = ICMP_POLL_DELAY;
info.timeout = ICMP_POLL_DELAY;
info.callback = ping_result;
#ifdef CONFIG_NET_BINDTODEVICE
info.devname = NULL;
#endif
info.priv = &priv;
priv.code = ICMP_I_OK;
priv.tmin = LONG_MAX;
Expand All @@ -300,7 +310,7 @@ int main(int argc, FAR char *argv[])

exitcode = EXIT_FAILURE;

while ((option = getopt(argc, argv, ":c:i:W:s:h")) != ERROR)
while ((option = getopt(argc, argv, ":c:i:W:s:I:h")) != ERROR)
{
switch (option)
{
Expand Down Expand Up @@ -360,6 +370,14 @@ int main(int argc, FAR char *argv[])
}
break;

case 'I':
#ifdef CONFIG_NET_BINDTODEVICE
info.devname = optarg;
#else
fprintf(stderr, "ERROR: Bind to device not supported\n");
#endif
break;

case 'h':
exitcode = EXIT_SUCCESS;
goto errout_with_usage;
Expand Down
17 changes: 16 additions & 1 deletion system/ping6/ping6.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static void show_usage(FAR const char *progname, int exitcode)
printf(" -s <size> specifies the number of data bytes to be sent. "
" Default %u.\n",
ICMPv6_PING6_DATALEN);
printf(" -I <interface> is the bind device for traffic\n");
printf(" -h shows this text and exits.\n");
exit(exitcode);
}
Expand Down Expand Up @@ -176,6 +177,12 @@ static void ping6_result(FAR const struct ping6_result_s *result)
result->extra, result->id);
break;

#ifdef CONFIG_NET_BINDTODEVICE
case ICMPV6_E_BINDDEV:
fprintf(stderr, "ERROR: setsockopt error: %ld\n", result->extra);
break;
#endif

case ICMPv6_W_SEQNOBIG:
fprintf(stderr,
"WARNING: Ignoring ICMP reply to sequence %ld. "
Expand Down Expand Up @@ -295,7 +302,7 @@ int main(int argc, FAR char *argv[])

exitcode = EXIT_FAILURE;

while ((option = getopt(argc, argv, ":c:i:W:s:h")) != ERROR)
while ((option = getopt(argc, argv, ":c:i:W:s:I:h")) != ERROR)
{
switch (option)
{
Expand Down Expand Up @@ -355,6 +362,14 @@ int main(int argc, FAR char *argv[])
}
break;

case 'I':
#ifdef CONFIG_NET_BINDTODEVICE
info.devname = optarg;
#else
fprintf(stderr, "ERROR: Bind to device not supported\n");
#endif
break;

case 'h':
exitcode = EXIT_SUCCESS;
goto errout_with_usage;
Expand Down
Loading