From c89e9330ccf0c70ef1a263d41b4c259cc79cf174 Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Mon, 22 Feb 2021 12:43:10 +0800 Subject: [PATCH] system/ping[6]: correct the ping return value MIRTOS-421 Change-Id: I68d8328ead736cd557d6142f611fae0540f74c1b Signed-off-by: chao.an --- include/netutils/icmp_ping.h | 7 ++++--- include/netutils/icmpv6_ping.h | 7 ++++--- system/ping/ping.c | 21 ++++++++++++++++++++- system/ping6/ping6.c | 20 +++++++++++++++++++- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/include/netutils/icmp_ping.h b/include/netutils/icmp_ping.h index a4e510b4f..93652c883 100644 --- a/include/netutils/icmp_ping.h +++ b/include/netutils/icmp_ping.h @@ -33,9 +33,10 @@ /* Positive number represent information */ -#define ICMP_I_BEGIN 0 /* extra: not used */ -#define ICMP_I_ROUNDTRIP 1 /* extra: packet delay */ -#define ICMP_I_FINISH 2 /* extra: elapsed time */ +#define ICMP_I_OK 0 /* extra: not used */ +#define ICMP_I_BEGIN 1 /* extra: not used */ +#define ICMP_I_ROUNDTRIP 2 /* extra: packet delay */ +#define ICMP_I_FINISH 3 /* extra: elapsed time */ /* Negative odd number represent error(unrecoverable) */ diff --git a/include/netutils/icmpv6_ping.h b/include/netutils/icmpv6_ping.h index 498b194af..e3014d8bb 100644 --- a/include/netutils/icmpv6_ping.h +++ b/include/netutils/icmpv6_ping.h @@ -33,9 +33,10 @@ /* Positive number represent information */ -#define ICMPv6_I_BEGIN 0 /* extra: not used */ -#define ICMPv6_I_ROUNDTRIP 1 /* extra: packet delay */ -#define ICMPv6_I_FINISH 2 /* extra: elapsed time */ +#define ICMPv6_I_OK 0 /* extra: not used */ +#define ICMPv6_I_BEGIN 1 /* extra: not used */ +#define ICMPv6_I_ROUNDTRIP 2 /* extra: packet delay */ +#define ICMPv6_I_FINISH 3 /* extra: elapsed time */ /* Negative odd number represent error(unrecoverable) */ diff --git a/system/ping/ping.c b/system/ping/ping.c index 20d08c0b4..e259fd9c8 100644 --- a/system/ping/ping.c +++ b/system/ping/ping.c @@ -41,6 +41,15 @@ #define ICMP_NPINGS 10 /* Default number of pings */ #define ICMP_POLL_DELAY 1000 /* 1 second in milliseconds */ +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ping_priv_s +{ + int code; /* Notice code ICMP_I/E/W_XXX */ +}; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -90,6 +99,13 @@ static void show_usage(FAR const char *progname, int exitcode) static void ping_result(FAR const struct ping_result_s *result) { + FAR struct ping_priv_s *priv = result->info->priv; + + if (result->code < 0) + { + priv->code = result->code; + } + switch (result->code) { case ICMP_E_HOSTIP: @@ -215,6 +231,7 @@ static void ping_result(FAR const struct ping_result_s *result) int main(int argc, FAR char *argv[]) { struct ping_info_s info; + struct ping_priv_s priv; FAR char *endptr; int exitcode; int option; @@ -224,6 +241,8 @@ int main(int argc, FAR char *argv[]) info.delay = ICMP_POLL_DELAY; info.timeout = ICMP_POLL_DELAY; info.callback = ping_result; + info.priv = &priv; + priv.code = ICMP_I_OK; /* Parse command line options */ @@ -314,7 +333,7 @@ int main(int argc, FAR char *argv[]) info.hostname = argv[optind]; icmp_ping(&info); - return EXIT_SUCCESS; + return priv.code < 0 ? EXIT_FAILURE: EXIT_SUCCESS; errout_with_usage: optind = 0; diff --git a/system/ping6/ping6.c b/system/ping6/ping6.c index 4ef3772e2..5fb6c1a72 100644 --- a/system/ping6/ping6.c +++ b/system/ping6/ping6.c @@ -43,6 +43,15 @@ #define ICMPv6_NPINGS 10 /* Default number of pings */ #define ICMPv6_POLL_DELAY 1000 /* 1 second in milliseconds */ +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ping6_priv_s +{ + int code; /* Notice code ICMP_I/E/W_XXX */ +}; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -92,8 +101,14 @@ static void show_usage(FAR const char *progname, int exitcode) static void ping6_result(FAR const struct ping6_result_s *result) { + FAR struct ping6_priv_s *priv = result->info->priv; char strbuffer[INET6_ADDRSTRLEN]; + if (result->code < 0) + { + priv->code = result->code; + } + switch (result->code) { case ICMPv6_E_HOSTIP: @@ -213,6 +228,7 @@ static void ping6_result(FAR const struct ping6_result_s *result) int main(int argc, FAR char *argv[]) { struct ping6_info_s info; + struct ping6_priv_s priv; FAR char *endptr; int exitcode; int option; @@ -222,6 +238,8 @@ int main(int argc, FAR char *argv[]) info.delay = ICMPv6_POLL_DELAY; info.timeout = ICMPv6_POLL_DELAY; info.callback = ping6_result; + info.priv = &priv; + priv.code = ICMPv6_I_OK; /* Parse command line options */ @@ -312,7 +330,7 @@ int main(int argc, FAR char *argv[]) info.hostname = argv[optind]; icmp6_ping(&info); - return EXIT_SUCCESS; + return priv.code < 0 ? EXIT_FAILURE: EXIT_SUCCESS; errout_with_usage: optind = 0;