This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 504b14554b849e7a3ffd4693ba4c9c325d057f1c Author: yangjianqing <[email protected]> AuthorDate: Thu Mar 13 16:34:03 2025 +0800 system/arping: add the arping command. Add an NSH 'arping' command on top of the netutils arping library: arping -i <device> [-w <timeout>] <destination_ip> It sends an ARP REQUEST to the destination and prints the MAC address of the host that answered together with the round-trip time. Signed-off-by: wenquan1 <[email protected]> Assisted-by: Kiro:claude-opus-4 --- system/arping/CMakeLists.txt | 33 +++++++++ system/arping/Kconfig | 30 ++++++++ system/arping/Make.defs | 25 +++++++ system/arping/Makefile | 36 ++++++++++ system/arping/arping.c | 165 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 289 insertions(+) diff --git a/system/arping/CMakeLists.txt b/system/arping/CMakeLists.txt new file mode 100644 index 000000000..02d7a963b --- /dev/null +++ b/system/arping/CMakeLists.txt @@ -0,0 +1,33 @@ +# ############################################################################## +# apps/system/arping/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +# +# ############################################################################## + +if(CONFIG_SYSTEM_ARPING) + nuttx_add_application( + NAME + arping + SRCS + arping.c + STACKSIZE + ${CONFIG_SYSTEM_ARPING_STACKSIZE} + PRIORITY + ${CONFIG_SYSTEM_ARPING_PRIORITY}) +endif() diff --git a/system/arping/Kconfig b/system/arping/Kconfig new file mode 100644 index 000000000..4511b1996 --- /dev/null +++ b/system/arping/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config SYSTEM_ARPING + tristate "arping command" + default n + select NETUTILS_ARPING + ---help--- + Enable support for the arping command. + +if SYSTEM_ARPING + +config SYSTEM_ARPING_PROGNAME + string "Arping program name" + default "arping" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config SYSTEM_ARPING_PRIORITY + int "Arping task priority" + default 100 + +config SYSTEM_ARPING_STACKSIZE + int "Arping stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/system/arping/Make.defs b/system/arping/Make.defs new file mode 100644 index 000000000..9042871d3 --- /dev/null +++ b/system/arping/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/system/arping/Make.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +# +############################################################################ + +ifneq ($(CONFIG_SYSTEM_ARPING),) +CONFIGURED_APPS += $(APPDIR)/system/arping +endif diff --git a/system/arping/Makefile b/system/arping/Makefile new file mode 100644 index 000000000..763e6221e --- /dev/null +++ b/system/arping/Makefile @@ -0,0 +1,36 @@ +############################################################################ +# apps/system/arping/Makefile +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# arping command + +PROGNAME = $(CONFIG_SYSTEM_ARPING_PROGNAME) +PRIORITY = $(CONFIG_SYSTEM_ARPING_PRIORITY) +STACKSIZE = $(CONFIG_SYSTEM_ARPING_STACKSIZE) +MODULE = $(CONFIG_SYSTEM_ARPING) + +# Files + +MAINSRC = arping.c + +include $(APPDIR)/Application.mk diff --git a/system/arping/arping.c b/system/arping/arping.c new file mode 100644 index 000000000..7f49e9f5b --- /dev/null +++ b/system/arping/arping.c @@ -0,0 +1,165 @@ +/**************************************************************************** + * apps/system/arping/arping.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> + +#include "netutils/arping.h" +#include "netutils/netlib.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: show_usage + ****************************************************************************/ + +static void usage(void) +{ + fprintf(stderr, "\nUsage:\n"); + fprintf(stderr, " %s [options] <destination>\n", + CONFIG_SYSTEM_ARPING_PROGNAME); + fprintf(stderr, "\nOptions:\n"); + fprintf(stderr, " -w <timeout> how long to wait for a reply\n"); + fprintf(stderr, " -i <device> which ethernet device to use\n"); + fprintf(stderr, " -h shows this text and exits.\n"); + fprintf(stderr, "\n"); + fprintf(stderr, " <destination> IP address\n"); + exit(EXIT_FAILURE); +} + +/**************************************************************************** + * Name: print_result + ****************************************************************************/ + +static void print_result(FAR const struct arping_info_s *info, + FAR const struct timespec *start) +{ + uint64_t usecs; + uint64_t msecs; + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + + usecs = (ts.tv_sec - start->tv_sec) * USEC_PER_SEC + + (ts.tv_nsec - start->tv_nsec + NSEC_PER_USEC / 2) / + NSEC_PER_USEC; + msecs = usecs / USEC_PER_MSEC; + usecs -= msecs * USEC_PER_MSEC; + + printf("Unicast reply from %s [%02x:%02x:%02x:%02x:%02x:%02x] %" + PRIu64 ".%03" PRIu64 "ms\n", info->dst_ip, + info->dst_mac[0], info->dst_mac[1], info->dst_mac[2], + info->dst_mac[3], info->dst_mac[4], info->dst_mac[5], + msecs, usecs); + + return; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char **argv) +{ + struct arping_info_s info = + { + .src_dev = NULL, + .timeout = 500, + 0 + }; + + struct timespec start_time; + struct in_addr src_addr; + int option; + int ret = -EINVAL; + + /* Parse command line options */ + + while ((option = getopt(argc, argv, "w:i:")) != EOF) + { + switch (option) + { + case 'w': + info.timeout = atoi(optarg) * MSEC_PER_SEC; + break; + + case 'i': + info.src_dev = optarg; + break; + + case 'h': + goto errout_with_usage; + + default: + fprintf(stderr, "ERROR: Unrecognized option\n"); + goto errout_with_usage; + } + } + + argc -= optind; + argv += optind; + + /* There should be one final parameters remaining on the command line */ + + if (argc != 1) + { + fprintf(stderr, "ERROR: Missing required <ip-address> argument\n"); + goto errout_with_usage; + } + + info.dst_ip = *argv; + + if (info.src_dev == NULL) + { + fprintf(stderr, "ERROR: Missing required <device> argument\n"); + goto errout_with_usage; + } + + netlib_get_ipv4addr(info.src_dev, &src_addr); + printf("ARPING %s from %s %s\n", info.dst_ip, inet_ntoa(src_addr), + info.src_dev); + + clock_gettime(CLOCK_MONOTONIC, &start_time); + + ret = arping(&info); + + if (ret > 0) + { + print_result(&info, &start_time); + } + + return ret; + +errout_with_usage: + optind = 0; + usage(); + return ret; +}
