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 54fe0cdda9d5c009df5443d2a1c2e8faf2a974b9 Author: yangjianqing <[email protected]> AuthorDate: Mon Mar 10 10:26:02 2025 +0800 netutils/arping: add an ARP request/reply library. Add arping() which sends an ARP REQUEST through an AF_PACKET raw socket and waits for the ARP REPLY to resolve the MAC address of an IP address on the local link. The timeout and the number of retries (CONFIG_NETUTILS_ARPING_RETRIES) are configurable. Signed-off-by: wenquan1 <[email protected]> Assisted-by: Kiro:claude-opus-4 --- include/netutils/arping.h | 59 +++++++++ netutils/arping/CMakeLists.txt | 25 ++++ netutils/arping/Kconfig | 23 ++++ netutils/arping/Make.defs | 25 ++++ netutils/arping/Makefile | 27 +++++ netutils/arping/arping.c | 267 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 426 insertions(+) diff --git a/include/netutils/arping.h b/include/netutils/arping.h new file mode 100644 index 000000000..311561b94 --- /dev/null +++ b/include/netutils/arping.h @@ -0,0 +1,59 @@ +/**************************************************************************** + * apps/include/netutils/arping.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __APPS_INCLUDE_NETUTILS_ARPING_H +#define __APPS_INCLUDE_NETUTILS_ARPING_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <netinet/if_ether.h> + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct arping_info_s +{ + int timeout; + FAR char *src_dev; + FAR const char *dst_ip; + uint8_t dst_mac[ETH_ALEN]; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +int arping(FAR struct arping_info_s *info); + +#ifdef __cplusplus +} +#endif + +#endif /* __APPS_INCLUDE_NETUTILS_ARPING_H */ diff --git a/netutils/arping/CMakeLists.txt b/netutils/arping/CMakeLists.txt new file mode 100644 index 000000000..21236d2f6 --- /dev/null +++ b/netutils/arping/CMakeLists.txt @@ -0,0 +1,25 @@ +# ############################################################################## +# apps/netutils/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_NETUTILS_ARPING) + target_sources(apps PRIVATE arping.c) +endif() diff --git a/netutils/arping/Kconfig b/netutils/arping/Kconfig new file mode 100644 index 000000000..432cba07b --- /dev/null +++ b/netutils/arping/Kconfig @@ -0,0 +1,23 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config NETUTILS_ARPING + bool "arping support" + default n + depends on NET_ARP && NET_PKT + ---help--- + Build in support for a arping command. This command arping will + send the ARP REQUEST and wait for the ARP RESPONSE from the + remote peer. + +if NETUTILS_ARPING + +config NETUTILS_ARPING_RETRIES + int "Number of retries for arp request" + default 1 + ---help--- + This setting determines how many times host retries request. + +endif # NETUTILS_ARPING diff --git a/netutils/arping/Make.defs b/netutils/arping/Make.defs new file mode 100644 index 000000000..2f051a612 --- /dev/null +++ b/netutils/arping/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/netutils/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_NETUTILS_ARPING),) +CONFIGURED_APPS += $(APPDIR)/netutils/arping +endif diff --git a/netutils/arping/Makefile b/netutils/arping/Makefile new file mode 100644 index 000000000..e6b122150 --- /dev/null +++ b/netutils/arping/Makefile @@ -0,0 +1,27 @@ +############################################################################ +# apps/netutils/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 + +CSRCS += arping.c + +include $(APPDIR)/Application.mk diff --git a/netutils/arping/arping.c b/netutils/arping/arping.c new file mode 100644 index 000000000..853ab1227 --- /dev/null +++ b/netutils/arping/arping.c @@ -0,0 +1,267 @@ +/**************************************************************************** + * apps/netutils/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 <debug.h> +#include <netpacket/packet.h> +#include <sys/select.h> + +#include "netutils/arping.h" +#include "netutils/netlib.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct arping_priv_s +{ + FAR struct arping_info_s *info; + struct in_addr src_addr; + struct in_addr dst_addr; + uint8_t src_mac[ETH_ALEN]; + int sockfd; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: open_socket + ****************************************************************************/ + +static int open_socket(int ifindex, int timeout) +{ + int sockfd; + struct sockaddr_ll addr; + struct timeval tv; + + tv.tv_sec = timeout / MSEC_PER_SEC; + tv.tv_usec = (timeout % MSEC_PER_SEC) * USEC_PER_MSEC; + + sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); + if (sockfd < 0) + { + nerr("ERROR: Create sockfd failed: %d\n", errno); + return -errno; + } + + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) + { + nerr("ERROR: Setsockopt(SO_RCVTIMEO) failed: %d\n", errno); + close(sockfd); + return -errno; + } + + /* Prepare sockaddr struct */ + + addr.sll_family = AF_PACKET; + addr.sll_ifindex = ifindex; + addr.sll_protocol = htons(ETH_P_ARP); + if (bind(sockfd, (FAR const struct sockaddr *)&addr, sizeof(addr)) < 0) + { + nerr("ERROR: Bind failed: %d\n", errno); + close(sockfd); + return -errno; + } + + return sockfd; +} + +/**************************************************************************** + * Name: send_packet + ****************************************************************************/ + +static int send_packet(FAR struct arping_priv_s *priv) +{ + uint8_t buf[256]; + FAR struct ethhdr *eh = (FAR struct ethhdr *)buf; + FAR struct ether_arp *ah = (FAR struct ether_arp *)(eh + 1); + size_t pack_size = sizeof(*eh) + sizeof(*ah); + ssize_t len; + + memset(&eh->h_dest, 0xff, ETH_ALEN); + memcpy(eh->h_source, &priv->src_mac, ETH_ALEN); + eh->h_proto = htons(ETH_P_ARP); + + memset(&ah->arp_tha, 0x00, ETH_ALEN); + memcpy(&ah->arp_sha, &priv->src_mac, ETH_ALEN); + memcpy(&ah->arp_tpa, &priv->dst_addr, sizeof(ah->arp_tpa)); + memcpy(&ah->arp_spa, &priv->src_addr, sizeof(ah->arp_spa)); + + ah->arp_hrd = htons(ARPHRD_ETHER); + ah->arp_pro = htons(ETH_P_IP); + ah->arp_hln = ETH_ALEN; + ah->arp_pln = 4; + ah->arp_op = htons(ARPOP_REQUEST); + + len = write(priv->sockfd, buf, pack_size); + if (len < 0) + { + return -errno; + } + + return len == pack_size ? 0 : -EINVAL; +} + +/**************************************************************************** + * Name: check_packet + ****************************************************************************/ + +static bool check_packet(FAR struct arping_priv_s *priv, + FAR const uint8_t *packet) +{ + FAR const struct ethhdr *eh = (FAR const struct ethhdr *)packet; + FAR const struct ether_arp *ah = (FAR const struct ether_arp *)(eh + 1); + struct in_addr src_ip; + + /* Get source IP addresses from received ARP packet */ + + memcpy(&src_ip, ah->arp_spa, sizeof(struct in_addr)); + + /* Check if the packet is sent by itself */ + + if (memcmp(eh->h_source, priv->src_mac, ETH_ALEN) == 0) + { + return false; + } + + /* Check if the packet is an ARP reply and the destination IP matches */ + + if (ah->arp_op != htons(ARPOP_REPLY) || + src_ip.s_addr != priv->dst_addr.s_addr) + { + return false; + } + + memcpy(priv->info->dst_mac, eh->h_source, ETH_ALEN); + + return true; +} + +/**************************************************************************** + * Name: recv_packet + ****************************************************************************/ + +static int recv_packet(FAR struct arping_priv_s *priv) +{ + uint8_t packet[MAX_NETDEV_PKTSIZE]; + ssize_t len; + + len = read(priv->sockfd, packet, sizeof(packet)); + if (len < 0) + { + nerr("ERROR: Read failed: %d\n", errno); + return -errno; + } + else if (len == 0) + { + return 0; + } + + return check_packet(priv, packet); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: arping + ****************************************************************************/ + +int arping(FAR struct arping_info_s *info) +{ + struct arping_priv_s priv; + int ifindex; + int retries; + int ret = -EINVAL; + + if (info->dst_ip == NULL || info->src_dev == NULL || info->timeout <= 0) + { + nerr("ERROR: Parameter check failed: %d\n", EINVAL); + return ret; + } + + priv.info = info; + + ifindex = if_nametoindex(info->src_dev); + if (ifindex == 0) + { + nerr("ERROR: Failed to get index of device: %d\n", errno); + return -errno; + } + + priv.sockfd = open_socket(ifindex, info->timeout); + if (priv.sockfd < 0) + { + return priv.sockfd; + } + + if (inet_aton(info->dst_ip, &priv.dst_addr) == 0) + { + nerr("ERROR: Destination IP is invalid: %d\n", EINVAL); + close(priv.sockfd); + return ret; + } + + netlib_get_ipv4addr(info->src_dev, &priv.src_addr); + netlib_getmacaddr(info->src_dev, priv.src_mac); + + for (retries = 0; retries < CONFIG_NETUTILS_ARPING_RETRIES; retries++) + { + ret = send_packet(&priv); + if (ret < 0) + { + continue; + } + + do + { + ret = recv_packet(&priv); + if (ret > 0) + { + close(priv.sockfd); + return ret; + } + } + while (ret != -EAGAIN); + } + + if (ret == -EAGAIN) + { + ret = -ETIMEDOUT; + nerr("ERROR: Arping request timed out: %d\n", ret); + } + else + { + nerr("ERROR: Write failed: %d\n", ret); + } + + close(priv.sockfd); + return ret; +}
