2017-10-23 16:50:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/ping/ping.c
|
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-10-23 16:50:01 +02:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-10-23 16:50:01 +02:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-10-23 16:50:01 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2022-03-14 09:12:11 +01:00
|
|
|
#include <nuttx/clock.h>
|
2017-10-23 16:50:01 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2022-03-14 09:12:11 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <fixedmath.h>
|
2017-10-23 16:50:01 +02:00
|
|
|
|
2023-08-18 21:57:42 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
|
2018-11-08 16:32:05 +01:00
|
|
|
#include "netutils/icmp_ping.h"
|
2017-10-23 16:50:01 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define ICMP_PING_DATALEN 56
|
|
|
|
|
|
|
|
#define ICMP_NPINGS 10 /* Default number of pings */
|
|
|
|
#define ICMP_POLL_DELAY 1000 /* 1 second in milliseconds */
|
|
|
|
|
2021-02-22 05:43:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct ping_priv_s
|
|
|
|
{
|
|
|
|
int code; /* Notice code ICMP_I/E/W_XXX */
|
2022-03-14 09:12:11 +01:00
|
|
|
long tmin; /* Minimum round trip time */
|
|
|
|
long tmax; /* Maximum round trip time */
|
|
|
|
long long tsum; /* Sum of all times, for doing average */
|
|
|
|
long long tsum2; /* Sum2 is the sum of the squares of sum ,for doing mean deviation */
|
2021-02-22 05:43:10 +01:00
|
|
|
};
|
|
|
|
|
2017-10-23 16:50:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: show_usage
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-10 06:09:43 +01:00
|
|
|
static void show_usage(FAR const char *progname, int exitcode)
|
|
|
|
noreturn_function;
|
2017-10-23 16:50:01 +02:00
|
|
|
static void show_usage(FAR const char *progname, int exitcode)
|
|
|
|
{
|
2017-10-24 23:38:37 +02:00
|
|
|
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
|
2020-11-10 06:09:43 +01:00
|
|
|
printf("\nUsage: %s [-c <count>] [-i <interval>] [-W <timeout>] "
|
|
|
|
"[-s <size>] <hostname>\n", progname);
|
2017-10-24 23:38:37 +02:00
|
|
|
printf(" %s -h\n", progname);
|
|
|
|
printf("\nWhere:\n");
|
2020-11-10 06:09:43 +01:00
|
|
|
printf(" <hostname> is either an IPv4 address or the name of "
|
|
|
|
"the remote host\n");
|
2018-08-23 14:47:33 +02:00
|
|
|
printf(" that is requested the ICMPv4 ECHO reply.\n");
|
2017-10-24 23:38:37 +02:00
|
|
|
#else
|
2020-11-10 06:09:43 +01:00
|
|
|
printf("\nUsage: %s [-c <count>] [-i <interval>] [-W <timeout>] "
|
|
|
|
"[-s <size>] <ip-address>\n", progname);
|
2017-10-23 16:50:01 +02:00
|
|
|
printf(" %s -h\n", progname);
|
|
|
|
printf("\nWhere:\n");
|
2020-11-10 06:09:43 +01:00
|
|
|
printf(" <ip-address> is the IPv4 address request the ICMP "
|
|
|
|
"ECHO reply.\n");
|
2017-10-24 23:38:37 +02:00
|
|
|
#endif
|
2017-10-23 16:50:01 +02:00
|
|
|
printf(" -c <count> determines the number of pings. Default %u.\n",
|
|
|
|
ICMP_NPINGS);
|
2020-11-10 06:09:43 +01:00
|
|
|
printf(" -i <interval> is the default delay between pings "
|
|
|
|
"(milliseconds).\n");
|
2017-10-23 16:50:01 +02:00
|
|
|
printf(" Default %d.\n", ICMP_POLL_DELAY);
|
2020-11-10 06:09:43 +01:00
|
|
|
printf(" -W <timeout> is the timeout for wait response "
|
|
|
|
"(milliseconds).\n");
|
2018-11-08 16:05:54 +01:00
|
|
|
printf(" Default %d.\n", ICMP_POLL_DELAY);
|
2020-11-10 06:09:43 +01:00
|
|
|
printf(" -s <size> specifies the number of data bytes to be sent. "
|
|
|
|
"Default %u.\n",
|
2018-11-08 16:05:54 +01:00
|
|
|
ICMP_PING_DATALEN);
|
2017-10-24 20:27:27 +02:00
|
|
|
printf(" -h shows this text and exits.\n");
|
2017-10-23 16:50:01 +02:00
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ping_result
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ping_result(FAR const struct ping_result_s *result)
|
|
|
|
{
|
2021-02-22 05:43:10 +01:00
|
|
|
FAR struct ping_priv_s *priv = result->info->priv;
|
|
|
|
|
|
|
|
if (result->code < 0)
|
|
|
|
{
|
|
|
|
priv->code = result->code;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
switch (result->code)
|
|
|
|
{
|
|
|
|
case ICMP_E_HOSTIP:
|
|
|
|
fprintf(stderr, "ERROR: ping_gethostip(%s) failed\n",
|
|
|
|
result->info->hostname);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_E_MEMORY:
|
|
|
|
fprintf(stderr, "ERROR: Failed to allocate memory\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_E_SOCKET:
|
2022-03-14 09:12:11 +01:00
|
|
|
fprintf(stderr, "ERROR: socket() failed: %ld\n", result->extra);
|
2018-11-08 16:05:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_I_BEGIN:
|
|
|
|
printf("PING %u.%u.%u.%u %u bytes of data\n",
|
2023-08-18 21:57:42 +02:00
|
|
|
ip4_addr1(result->dest.s_addr),
|
|
|
|
ip4_addr2(result->dest.s_addr),
|
|
|
|
ip4_addr3(result->dest.s_addr),
|
|
|
|
ip4_addr4(result->dest.s_addr),
|
2018-11-08 16:05:54 +01:00
|
|
|
result->info->datalen);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_E_SENDTO:
|
2022-03-14 09:12:11 +01:00
|
|
|
fprintf(stderr, "ERROR: sendto failed at seqno %u: %ld\n",
|
2018-11-08 16:05:54 +01:00
|
|
|
result->seqno, result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_E_SENDSMALL:
|
2022-03-14 09:12:11 +01:00
|
|
|
fprintf(stderr, "ERROR: sendto returned %ld, expected %u\n",
|
2018-11-08 16:05:54 +01:00
|
|
|
result->extra, result->outsize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_E_POLL:
|
2022-03-14 09:12:11 +01:00
|
|
|
fprintf(stderr, "ERROR: poll failed: %ld\n", result->extra);
|
2018-11-08 16:05:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_W_TIMEOUT:
|
2022-03-14 09:12:11 +01:00
|
|
|
printf("No response from %u.%u.%u.%u: icmp_seq=%u time=%ld ms\n",
|
2023-08-18 21:57:42 +02:00
|
|
|
ip4_addr1(result->dest.s_addr),
|
|
|
|
ip4_addr2(result->dest.s_addr),
|
|
|
|
ip4_addr3(result->dest.s_addr),
|
|
|
|
ip4_addr4(result->dest.s_addr),
|
2018-11-08 16:05:54 +01:00
|
|
|
result->seqno, result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_E_RECVFROM:
|
2022-03-14 09:12:11 +01:00
|
|
|
fprintf(stderr, "ERROR: recvfrom failed: %ld\n", result->extra);
|
2018-11-08 16:05:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_E_RECVSMALL:
|
2022-03-14 09:12:11 +01:00
|
|
|
fprintf(stderr, "ERROR: short ICMP packet: %ld\n", result->extra);
|
2018-11-08 16:05:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_W_IDDIFF:
|
|
|
|
fprintf(stderr,
|
2022-03-14 09:12:11 +01:00
|
|
|
"WARNING: Ignoring ICMP reply with ID %ld. "
|
2018-11-08 16:05:54 +01:00
|
|
|
"Expected %u\n",
|
|
|
|
result->extra, result->id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_W_SEQNOBIG:
|
|
|
|
fprintf(stderr,
|
2022-03-14 09:12:11 +01:00
|
|
|
"WARNING: Ignoring ICMP reply to sequence %ld. "
|
2018-11-08 16:05:54 +01:00
|
|
|
"Expected <= %u\n",
|
|
|
|
result->extra, result->seqno);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_W_SEQNOSMALL:
|
|
|
|
fprintf(stderr, "WARNING: Received after timeout\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_I_ROUNDTRIP:
|
2022-03-14 09:12:11 +01:00
|
|
|
priv->tsum += result->extra;
|
|
|
|
priv->tsum2 += (long long)result->extra * result->extra;
|
|
|
|
if (result->extra < priv->tmin)
|
|
|
|
{
|
|
|
|
priv->tmin = result->extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result->extra > priv->tmax)
|
|
|
|
{
|
|
|
|
priv->tmax = result->extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%u bytes from %u.%u.%u.%u: icmp_seq=%u time=%ld.%ld ms\n",
|
2018-11-08 16:05:54 +01:00
|
|
|
result->info->datalen,
|
2023-08-18 21:57:42 +02:00
|
|
|
ip4_addr1(result->dest.s_addr),
|
|
|
|
ip4_addr2(result->dest.s_addr),
|
|
|
|
ip4_addr3(result->dest.s_addr),
|
|
|
|
ip4_addr4(result->dest.s_addr),
|
2022-03-14 09:12:11 +01:00
|
|
|
result->seqno, result->extra / USEC_PER_MSEC,
|
|
|
|
result->extra % USEC_PER_MSEC / MSEC_PER_DSEC);
|
2018-11-08 16:05:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_W_RECVBIG:
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: Ignoring ICMP reply with different payload "
|
2022-03-14 09:12:11 +01:00
|
|
|
"size: %ld vs %u\n",
|
2018-11-08 16:05:54 +01:00
|
|
|
result->extra, result->outsize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_W_DATADIFF:
|
|
|
|
fprintf(stderr, "WARNING: Echoed data corrupted\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_W_TYPE:
|
2022-03-14 09:12:11 +01:00
|
|
|
fprintf(stderr, "WARNING: ICMP packet with unknown type: %ld\n",
|
2018-11-08 16:05:54 +01:00
|
|
|
result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_I_FINISH:
|
|
|
|
if (result->nrequests > 0)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
|
|
|
|
|
|
|
/* Calculate the percentage of lost packets */
|
|
|
|
|
|
|
|
tmp = (100 * (result->nrequests - result->nreplies) +
|
|
|
|
(result->nrequests >> 1)) /
|
|
|
|
result->nrequests;
|
|
|
|
|
2020-11-10 06:09:43 +01:00
|
|
|
printf("%u packets transmitted, %u received, %u%% packet loss, "
|
2022-03-14 09:12:11 +01:00
|
|
|
"time %ld ms\n",
|
|
|
|
result->nrequests, result->nreplies, tmp,
|
|
|
|
result->extra / USEC_PER_MSEC);
|
|
|
|
if (result->nreplies > 0)
|
|
|
|
{
|
2022-04-22 02:47:58 +02:00
|
|
|
long avg = 0;
|
|
|
|
long long tempnum = 0;
|
|
|
|
long tmdev = 0;
|
|
|
|
|
|
|
|
if (priv->tsum > 0)
|
|
|
|
{
|
|
|
|
avg = priv->tsum / result->nreplies;
|
|
|
|
tempnum = priv->tsum2 / result->nreplies -
|
|
|
|
(long long)avg * avg;
|
|
|
|
tmdev = ub16toi(ub32sqrtub16(uitoub32(tempnum)));
|
|
|
|
}
|
2022-03-14 09:12:11 +01:00
|
|
|
|
|
|
|
printf("rtt min/avg/max/mdev = %ld.%03ld/%ld.%03ld/"
|
|
|
|
"%ld.%03ld/%ld.%03ld ms\n",
|
|
|
|
priv->tmin / USEC_PER_MSEC,
|
|
|
|
priv->tmin % USEC_PER_MSEC,
|
|
|
|
avg / USEC_PER_MSEC, avg % USEC_PER_MSEC,
|
|
|
|
priv->tmax / USEC_PER_MSEC,
|
|
|
|
priv->tmax % USEC_PER_MSEC,
|
|
|
|
tmdev / USEC_PER_MSEC, tmdev % USEC_PER_MSEC);
|
|
|
|
}
|
2018-11-08 16:05:54 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 16:50:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
struct ping_info_s info;
|
2021-02-22 05:43:10 +01:00
|
|
|
struct ping_priv_s priv;
|
2017-10-23 16:50:01 +02:00
|
|
|
FAR char *endptr;
|
|
|
|
int exitcode;
|
|
|
|
int option;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
info.count = ICMP_NPINGS;
|
|
|
|
info.datalen = ICMP_PING_DATALEN;
|
|
|
|
info.delay = ICMP_POLL_DELAY;
|
|
|
|
info.timeout = ICMP_POLL_DELAY;
|
|
|
|
info.callback = ping_result;
|
2021-02-22 05:43:10 +01:00
|
|
|
info.priv = &priv;
|
|
|
|
priv.code = ICMP_I_OK;
|
2022-03-14 09:12:11 +01:00
|
|
|
priv.tmin = LONG_MAX;
|
|
|
|
priv.tmax = 0;
|
|
|
|
priv.tsum = 0;
|
|
|
|
priv.tsum2 = 0;
|
2017-10-23 16:50:01 +02:00
|
|
|
|
|
|
|
/* Parse command line options */
|
|
|
|
|
|
|
|
exitcode = EXIT_FAILURE;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
while ((option = getopt(argc, argv, ":c:i:W:s:h")) != ERROR)
|
2017-10-23 16:50:01 +02:00
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
long count = strtol(optarg, &endptr, 10);
|
|
|
|
if (count < 1 || count > UINT16_MAX)
|
|
|
|
{
|
2020-11-10 06:09:43 +01:00
|
|
|
fprintf(stderr, "ERROR: <count> out of range: %ld\n",
|
|
|
|
count);
|
2017-10-23 16:50:01 +02:00
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
info.count = (uint16_t)count;
|
2017-10-23 16:50:01 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
long delay = strtol(optarg, &endptr, 10);
|
2018-11-08 16:05:54 +01:00
|
|
|
if (delay < 1 || delay > UINT16_MAX)
|
2017-10-23 16:50:01 +02:00
|
|
|
{
|
2020-11-10 06:09:43 +01:00
|
|
|
fprintf(stderr, "ERROR: <interval> out of range: %ld\n",
|
|
|
|
delay);
|
2017-10-23 16:50:01 +02:00
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
info.delay = (int16_t)delay;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'W':
|
|
|
|
{
|
|
|
|
long timeout = strtol(optarg, &endptr, 10);
|
|
|
|
if (timeout < 1 || timeout > UINT16_MAX)
|
|
|
|
{
|
2020-11-10 06:09:43 +01:00
|
|
|
fprintf(stderr, "ERROR: <timeout> out of range: %ld\n",
|
|
|
|
timeout);
|
2018-11-08 16:05:54 +01:00
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.timeout = (int16_t)timeout;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
long datalen = strtol(optarg, &endptr, 10);
|
|
|
|
if (datalen < 1 || datalen > UINT16_MAX)
|
|
|
|
{
|
2020-11-10 06:09:43 +01:00
|
|
|
fprintf(stderr, "ERROR: <size> out of range: %ld\n",
|
|
|
|
datalen);
|
2018-11-08 16:05:54 +01:00
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.datalen = (uint16_t)datalen;
|
2017-10-23 16:50:01 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
exitcode = EXIT_SUCCESS;
|
|
|
|
goto errout_with_usage;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
fprintf(stderr, "ERROR: Missing required argument\n");
|
|
|
|
goto errout_with_usage;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "ERROR: Unrecognized option\n");
|
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be one final parameters remaining on the command line */
|
|
|
|
|
|
|
|
if (optind >= argc)
|
|
|
|
{
|
|
|
|
printf("ERROR: Missing required <ip-address> argument\n");
|
2018-11-08 16:05:54 +01:00
|
|
|
goto errout_with_usage;
|
2017-10-23 16:50:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
info.hostname = argv[optind];
|
|
|
|
icmp_ping(&info);
|
2021-02-22 05:43:10 +01:00
|
|
|
return priv.code < 0 ? EXIT_FAILURE: EXIT_SUCCESS;
|
2017-10-23 16:50:01 +02:00
|
|
|
|
|
|
|
errout_with_usage:
|
2018-11-08 16:05:54 +01:00
|
|
|
optind = 0;
|
2017-10-23 16:50:01 +02:00
|
|
|
show_usage(argv[0], exitcode);
|
|
|
|
return exitcode; /* Not reachable */
|
|
|
|
}
|